Skip to content

Commit 15fae27

Browse files
schuellerfsfan5
authored andcommittedAug 9, 2016
Zoom implementation (--zoom option)
1 parent 7c1989a commit 15fae27

File tree

4 files changed

+78
-46
lines changed

4 files changed

+78
-46
lines changed
 

Diff for: ‎README.rst

+16-13
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,44 @@ Parameters
4343
^^^^^^^^^^
4444

4545
bgcolor:
46-
Background color of image, `--bgcolor #ffffff`
46+
Background color of image, e.g. ``--bgcolor #ffffff``
4747

4848
scalecolor:
49-
Color of scale, `--scalecolor #000000`
49+
Color of scale, e.g. ``--scalecolor #000000``
5050

5151
playercolor:
52-
Color of player indicators, `--playercolor #ff0000`
52+
Color of player indicators, e.g. ``--playercolor #ff0000``
5353

5454
origincolor:
55-
Color of origin indicator, `--origincolor #ff0000`
55+
Color of origin indicator, e.g. ``--origincolor #ff0000``
5656

5757
drawscale:
58-
Draw tick marks, `--drawscale`
58+
Draw tick marks, ``--drawscale``
5959

6060
drawplayers:
61-
Draw player indicators, `--drawplayers`
61+
Draw player indicators, ``--drawplayers``
6262

6363
draworigin:
64-
Draw origin indicator, `--draworigin`
64+
Draw origin indicator, ``--draworigin``
6565

6666
drawalpha:
67-
Allow nodes to be drawn with transparency, `--drawalpha`
67+
Allow nodes to be drawn with transparency, ``--drawalpha``
6868

6969
noshading:
70-
Don't draw shading on nodes, `--noshading`
70+
Don't draw shading on nodes, ``--noshading``
7171

7272
min-y:
73-
Don't draw nodes below this y value, `--min-y -25`
73+
Don't draw nodes below this y value, e.g. ``--min-y -25``
7474

7575
max-y:
76-
Don't draw nodes above this y value, `--max-y 75`
76+
Don't draw nodes above this y value, e.g. ``--max-y 75``
7777

7878
backend:
79-
Use specific map backend, supported: sqlite3, leveldb, redis, `--backend leveldb`
79+
Use specific map backend, supported: *sqlite3*, *leveldb*, *redis*, e.g. ``--backend leveldb``
8080

8181
geometry:
82-
Limit area to specific geometry, `--geometry -800:-800+1600+1600`
82+
Limit area to specific geometry, e.g. ``--geometry -800:-800+1600+1600``
83+
84+
zoom:
85+
"Zoom" the image by using more than one pixel per node, e.g. ``--zoom 4``
8386

Diff for: ‎TileGenerator.cpp

+48-32
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static inline int rgb2int(uint8_t r, uint8_t g, uint8_t b, uint8_t a=0xFF)
3636

3737
static inline int color2int(Color c)
3838
{
39-
return rgb2int(c.r, c.g, c.b, c.a);
39+
return rgb2int(c.r, c.g, c.b, c.a);
4040
}
4141

4242
// rounds n (away from 0) to a multiple of f while preserving the sign of n
@@ -87,15 +87,15 @@ static inline int colorSafeBounds(int color)
8787

8888
static inline Color mixColors(Color a, Color b)
8989
{
90-
Color result;
91-
double a1 = a.a / 255.0;
92-
double a2 = b.a / 255.0;
90+
Color result;
91+
double a1 = a.a / 255.0;
92+
double a2 = b.a / 255.0;
9393

94-
result.r = (int) (a1 * a.r + a2 * (1 - a1) * b.r);
95-
result.g = (int) (a1 * a.g + a2 * (1 - a1) * b.g);
96-
result.b = (int) (a1 * a.b + a2 * (1 - a1) * b.b);
97-
result.a = (int) (255 * (a1 + a2 * (1 - a1)));
98-
return result;
94+
result.r = (int) (a1 * a.r + a2 * (1 - a1) * b.r);
95+
result.g = (int) (a1 * a.g + a2 * (1 - a1) * b.g);
96+
result.b = (int) (a1 * a.b + a2 * (1 - a1) * b.b);
97+
result.a = (int) (255 * (a1 + a2 * (1 - a1)));
98+
return result;
9999
}
100100

101101
TileGenerator::TileGenerator():
@@ -120,7 +120,8 @@ TileGenerator::TileGenerator():
120120
m_geomX(-2048),
121121
m_geomY(-2048),
122122
m_geomX2(2048),
123-
m_geomY2(2048)
123+
m_geomY2(2048),
124+
m_zoom(1)
124125
{
125126
}
126127

@@ -148,6 +149,14 @@ void TileGenerator::setPlayerColor(const std::string &playerColor)
148149
m_playerColor = parseColor(playerColor);
149150
}
150151

152+
void TileGenerator::setZoom(int zoom)
153+
{
154+
if (zoom < 1) {
155+
throw std::runtime_error("Zoom level needs to be a number: 1 or higher");
156+
}
157+
m_zoom = zoom;
158+
}
159+
151160
Color TileGenerator::parseColor(const std::string &color)
152161
{
153162
Color parsed;
@@ -186,7 +195,7 @@ void TileGenerator::setDrawScale(bool drawScale)
186195

187196
void TileGenerator::setDrawAlpha(bool drawAlpha)
188197
{
189-
m_drawAlpha = drawAlpha;
198+
m_drawAlpha = drawAlpha;
190199
}
191200

192201
void TileGenerator::setShading(bool shading)
@@ -346,10 +355,10 @@ void TileGenerator::createImage()
346355
{
347356
m_mapWidth = (m_xMax - m_xMin + 1) * 16;
348357
m_mapHeight = (m_zMax - m_zMin + 1) * 16;
349-
m_image = gdImageCreateTrueColor(m_mapWidth + m_border, m_mapHeight + m_border);
358+
m_image = gdImageCreateTrueColor((m_mapWidth * m_zoom) + m_border, (m_mapHeight * m_zoom) + m_border);
350359
m_blockPixelAttributes.setWidth(m_mapWidth);
351360
// Background
352-
gdImageFilledRectangle(m_image, 0, 0, m_mapWidth + m_border - 1, m_mapHeight + m_border -1, color2int(m_bgColor));
361+
gdImageFilledRectangle(m_image, 0, 0, (m_mapWidth * m_zoom) + m_border - 1, (m_mapHeight * m_zoom) + m_border -1, color2int(m_bgColor));
353362
}
354363

355364
void TileGenerator::renderMap()
@@ -497,12 +506,12 @@ inline void TileGenerator::renderMapBlock(const ustring &mapBlock, const BlockPo
497506
int minY = (pos.y * 16 > m_yMin) ? 0 : m_yMin - pos.y * 16;
498507
int maxY = (pos.y * 16 < m_yMax) ? 15 : m_yMax - pos.y * 16;
499508
for (int z = 0; z < 16; ++z) {
500-
int imageY = getImageY(zBegin + 15 - z);
509+
int imageY = zBegin + 15 - z;
501510
for (int x = 0; x < 16; ++x) {
502511
if (m_readPixels[z] & (1 << x)) {
503512
continue;
504513
}
505-
int imageX = getImageX(xBegin + x);
514+
int imageX = xBegin + x;
506515

507516
for (int y = maxY; y >= minY; --y) {
508517
int position = x + (y << 4) + (z << 8);
@@ -523,15 +532,15 @@ inline void TileGenerator::renderMapBlock(const ustring &mapBlock, const BlockPo
523532
else
524533
m_color[z][x] = mixColors(m_color[z][x], c);
525534
if(m_color[z][x].a == 0xFF) {
526-
m_image->tpixels[imageY][imageX] = color2int(m_color[z][x]);
535+
setZoomed(m_image,imageY,imageX,color2int(m_color[z][x]));
527536
m_readPixels[z] |= (1 << x);
528537
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thickness = m_thickness[z][x];
529538
} else {
530539
m_thickness[z][x] = (m_thickness[z][x] + color->second.t) / 2.0;
531540
continue;
532541
}
533542
} else {
534-
m_image->tpixels[imageY][imageX] = color2int(c);
543+
setZoomed(m_image,imageY,imageX,color2int(c));
535544
m_readPixels[z] |= (1 << x);
536545
}
537546
if(!(m_readInfo[z] & (1 << x))) {
@@ -553,15 +562,15 @@ inline void TileGenerator::renderMapBlockBottom(const BlockPos &pos)
553562
int xBegin = (pos.x - m_xMin) * 16;
554563
int zBegin = (m_zMax - pos.z) * 16;
555564
for (int z = 0; z < 16; ++z) {
556-
int imageY = getImageY(zBegin + 15 - z);
565+
int imageY = zBegin + 15 - z;
557566
for (int x = 0; x < 16; ++x) {
558567
if (m_readPixels[z] & (1 << x)) {
559568
continue;
560569
}
561-
int imageX = getImageX(xBegin + x);
570+
int imageX = xBegin + x;
562571

563572
if (m_drawAlpha) {
564-
m_image->tpixels[imageY][imageX] = color2int(m_color[z][x]);
573+
setZoomed(m_image,imageY,imageX, color2int(m_color[z][x]));
565574
m_readPixels[z] |= (1 << x);
566575
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thickness = m_thickness[z][x];
567576
}
@@ -577,7 +586,6 @@ inline void TileGenerator::renderShading(int zPos)
577586
if (imageY >= m_mapHeight) {
578587
continue;
579588
}
580-
imageY = getImageY(imageY);
581589
for (int x = 0; x < m_mapWidth; ++x) {
582590
if (!m_blockPixelAttributes.attribute(z, x).valid_height() || !m_blockPixelAttributes.attribute(z, x - 1).valid_height() || !m_blockPixelAttributes.attribute(z - 1, x).valid_height()) {
583591
continue;
@@ -592,15 +600,14 @@ inline void TileGenerator::renderShading(int zPos)
592600
// more thickness -> less visible shadows: t=0 -> 100% visible, t=255 -> 0% visible
593601
if (m_drawAlpha)
594602
d = d * ((0xFF - m_blockPixelAttributes.attribute(z, x).thickness) / 255.0);
595-
int sourceColor = m_image->tpixels[imageY][getImageX(x)] & 0xffffff;
603+
int sourceColor = m_image->tpixels[getImageY(imageY)][getImageX(x)] & 0xffffff;
596604
uint8_t r = (sourceColor & 0xff0000) >> 16;
597605
uint8_t g = (sourceColor & 0x00ff00) >> 8;
598606
uint8_t b = (sourceColor & 0x0000ff);
599607
r = colorSafeBounds(r + d);
600608
g = colorSafeBounds(g + d);
601609
b = colorSafeBounds(b + d);
602-
603-
m_image->tpixels[imageY][getImageX(x)] = rgb2int(r, g, b);
610+
setZoomed(m_image,imageY,x, rgb2int(r, g, b));
604611
}
605612
}
606613
m_blockPixelAttributes.scroll();
@@ -619,7 +626,7 @@ void TileGenerator::renderScale()
619626
buf << i * 16;
620627
scaleText = buf.str();
621628

622-
int xPos = m_xMin * -16 + i * 16 + m_border;
629+
int xPos = (m_xMin * -16 + i * 16)*m_zoom + m_border;
623630
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, 0, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
624631
gdImageLine(m_image, xPos, 0, xPos, m_border - 1, color);
625632
}
@@ -629,16 +636,16 @@ void TileGenerator::renderScale()
629636
buf << i * 16;
630637
scaleText = buf.str();
631638

632-
int yPos = m_mapHeight - 1 - (i * 16 - m_zMin * 16) + m_border;
639+
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16))*m_zoom + m_border;
633640
gdImageString(m_image, gdFontGetMediumBold(), 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
634641
gdImageLine(m_image, 0, yPos, m_border - 1, yPos, color);
635642
}
636643
}
637644

638645
void TileGenerator::renderOrigin()
639646
{
640-
int imageX = -m_xMin * 16 + m_border;
641-
int imageY = m_mapHeight - m_zMin * -16 + m_border;
647+
int imageX = (-m_xMin * 16)*m_zoom + m_border;
648+
int imageY = (m_mapHeight - m_zMin * -16)*m_zoom + m_border;
642649
gdImageArc(m_image, imageX, imageY, 12, 12, 0, 360, color2int(m_originColor));
643650
}
644651

@@ -648,8 +655,8 @@ void TileGenerator::renderPlayers(const std::string &inputPath)
648655

649656
PlayerAttributes players(inputPath);
650657
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
651-
int imageX = player->x / 10 - m_xMin * 16 + m_border;
652-
int imageY = m_mapHeight - (player->z / 10 - m_zMin * 16) + m_border;
658+
int imageX = (player->x / 10 - m_xMin * 16)*m_zoom + m_border;
659+
int imageY = (m_mapHeight - (player->z / 10 - m_zMin * 16))*m_zoom + m_border;
653660

654661
gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color);
655662
gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(player->name.c_str())), color);
@@ -694,10 +701,19 @@ void TileGenerator::printUnknown()
694701

695702
inline int TileGenerator::getImageX(int val) const
696703
{
697-
return val + m_border;
704+
return (m_zoom*val) + m_border;
698705
}
699706

700707
inline int TileGenerator::getImageY(int val) const
701708
{
702-
return val + m_border;
709+
return (m_zoom*val) + m_border;
710+
}
711+
712+
inline void TileGenerator::setZoomed(gdImagePtr image, int y, int x, int color) {
713+
int xx,yy;
714+
for (xx = 0; xx < m_zoom; xx++) {
715+
for (yy = 0; yy < m_zoom; yy++) {
716+
image->tpixels[m_border + (y*m_zoom) + xx][m_border + (x*m_zoom) + yy] = color;
717+
}
718+
}
703719
}

Diff for: ‎TileGenerator.h

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TileGenerator
7171
void parseColorsFile(const std::string &fileName);
7272
void setBackend(std::string backend);
7373
void generate(const std::string &input, const std::string &output);
74+
void setZoom(int zoom);
7475

7576
private:
7677
void parseColorsStream(std::istream &in);
@@ -89,6 +90,7 @@ class TileGenerator
8990
void printUnknown();
9091
int getImageX(int val) const;
9192
int getImageY(int val) const;
93+
void setZoomed(gdImagePtr image, int x, int y, int color);
9294

9395
private:
9496
Color m_bgColor;
@@ -129,6 +131,7 @@ class TileGenerator
129131

130132
int m_blockAirId;
131133
int m_blockIgnoreId;
134+
int m_zoom;
132135
}; // class TileGenerator
133136

134137
#endif // TILEGENERATOR_HEADER

Diff for: ‎mapper.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void usage()
3636
" --max-y <y>\n"
3737
" --backend <backend>\n"
3838
" --geometry x:y+w+h\n"
39+
" --zoom <zoomlevel>\n"
3940
"Color format: '#000000'\n";
4041
std::cout << usage_text;
4142
}
@@ -59,7 +60,8 @@ int main(int argc, char *argv[])
5960
{"backend", required_argument, 0, 'd'},
6061
{"geometry", required_argument, 0, 'g'},
6162
{"min-y", required_argument, 0, 'a'},
62-
{"max-y", required_argument, 0, 'c'}
63+
{"max-y", required_argument, 0, 'c'},
64+
{"zoom", required_argument, 0, 'z'}
6365
};
6466

6567
string input;
@@ -148,6 +150,14 @@ int main(int argc, char *argv[])
148150
generator.setGeometry(x, y, w, h);
149151
}
150152
break;
153+
case 'z': {
154+
istringstream iss;
155+
iss.str(optarg);
156+
int zoom;
157+
iss >> zoom;
158+
generator.setZoom(zoom);
159+
}
160+
break;
151161
default:
152162
exit(1);
153163
}

0 commit comments

Comments
 (0)