2
2
#include < cstdlib>
3
3
#include < climits>
4
4
#include < fstream>
5
- #include < gdfontmb.h>
6
5
#include < iostream>
7
6
#include < sstream>
8
7
#include < stdexcept>
9
- #include < cerrno>
10
8
#include < cstring>
11
9
#include < vector>
12
10
#include " config.h"
@@ -29,16 +27,6 @@ static inline uint16_t readU16(const unsigned char *data)
29
27
return data[0 ] << 8 | data[1 ];
30
28
}
31
29
32
- static inline int rgb2int (uint8_t r, uint8_t g, uint8_t b, uint8_t a=0xFF )
33
- {
34
- return (a << 24 ) + (r << 16 ) + (g << 8 ) + b;
35
- }
36
-
37
- static inline int color2int (Color c)
38
- {
39
- return rgb2int (c.r , c.g , c.b , c.a );
40
- }
41
-
42
30
// rounds n (away from 0) to a multiple of f while preserving the sign of n
43
31
static inline int round_multiple_nosign (int n, int f)
44
32
{
@@ -377,10 +365,9 @@ void TileGenerator::createImage()
377
365
std::cerr << " Warning: The width or height of the image to be created exceeds 4096 pixels!"
378
366
<< " (Dimensions: " << image_width << " x" << image_height << " )"
379
367
<< std::endl;
380
- m_image = gdImageCreateTrueColor (image_width, image_height);
368
+ m_image = new Image (image_width, image_height);
381
369
m_blockPixelAttributes.setWidth (m_mapWidth);
382
- // Background
383
- gdImageFilledRectangle (m_image, 0 , 0 , image_width - 1 , image_height - 1 , color2int (m_bgColor));
370
+ m_image->drawFilledRect (0 , 0 , image_width, image_height, m_bgColor); // Background
384
371
}
385
372
386
373
void TileGenerator::renderMap ()
@@ -560,15 +547,15 @@ inline void TileGenerator::renderMapBlock(const ustring &mapBlock, const BlockPo
560
547
else
561
548
m_color[z][x] = mixColors (m_color[z][x], c);
562
549
if (m_color[z][x].a == 0xFF ) {
563
- setZoomed (m_image, imageY,imageX, color2int ( m_color[z][x]) );
550
+ setZoomed (imageX, imageY, m_color[z][x]);
564
551
m_readPixels[z] |= (1 << x);
565
552
m_blockPixelAttributes.attribute (15 - z, xBegin + x).thickness = m_thickness[z][x];
566
553
} else {
567
554
m_thickness[z][x] = (m_thickness[z][x] + color->second .t ) / 2.0 ;
568
555
continue ;
569
556
}
570
557
} else {
571
- setZoomed (m_image, imageY,imageX, color2int (c) );
558
+ setZoomed (imageX, imageY, c );
572
559
m_readPixels[z] |= (1 << x);
573
560
}
574
561
if (!(m_readInfo[z] & (1 << x))) {
@@ -598,7 +585,7 @@ inline void TileGenerator::renderMapBlockBottom(const BlockPos &pos)
598
585
int imageX = xBegin + x;
599
586
600
587
if (m_drawAlpha) {
601
- setZoomed (m_image, imageY,imageX, color2int ( m_color[z][x]) );
588
+ setZoomed (imageX, imageY, m_color[z][x]);
602
589
m_readPixels[z] |= (1 << x);
603
590
m_blockPixelAttributes.attribute (15 - z, xBegin + x).thickness = m_thickness[z][x];
604
591
}
@@ -628,48 +615,43 @@ inline void TileGenerator::renderShading(int zPos)
628
615
// more thickness -> less visible shadows: t=0 -> 100% visible, t=255 -> 0% visible
629
616
if (m_drawAlpha)
630
617
d = d * ((0xFF - m_blockPixelAttributes.attribute (z, x).thickness ) / 255.0 );
631
- int sourceColor = m_image->tpixels [getImageY (imageY)][getImageX (x)] & 0xffffff ;
632
- uint8_t r = (sourceColor & 0xff0000 ) >> 16 ;
633
- uint8_t g = (sourceColor & 0x00ff00 ) >> 8 ;
634
- uint8_t b = (sourceColor & 0x0000ff );
635
- r = colorSafeBounds (r + d);
636
- g = colorSafeBounds (g + d);
637
- b = colorSafeBounds (b + d);
638
- setZoomed (m_image,imageY,x, rgb2int (r, g, b));
618
+ Color c = m_image->getPixel (getImageX (x), getImageY (imageY));
619
+ c.r = colorSafeBounds (c.r + d);
620
+ c.g = colorSafeBounds (c.g + d);
621
+ c.b = colorSafeBounds (c.b + d);
622
+ setZoomed (x, imageY, c);
639
623
}
640
624
}
641
625
m_blockPixelAttributes.scroll ();
642
626
}
643
627
644
628
void TileGenerator::renderScale ()
645
629
{
646
- int color = color2int (m_scaleColor);
647
-
648
630
string scaleText;
649
631
650
632
if (m_scales & SCALE_TOP) {
651
- gdImageString ( m_image, gdFontGetMediumBold (), 24 , 0 , reinterpret_cast < unsigned char *>( const_cast < char *>( " X" )), color );
633
+ m_image-> drawText ( 24 , 0 , " X" , m_scaleColor );
652
634
for (int i = (m_xMin / 4 ) * 4 ; i <= m_xMax; i += 4 ) {
653
635
stringstream buf;
654
636
buf << i * 16 ;
655
637
scaleText = buf.str ();
656
638
657
639
int xPos = (m_xMin * -16 + i * 16 )*m_zoom + m_xBorder;
658
- gdImageString ( m_image, gdFontGetMediumBold (), xPos + 2 , 0 , reinterpret_cast < unsigned char *>( const_cast < char *>( scaleText. c_str ())), color );
659
- gdImageLine ( m_image, xPos, 0 , xPos, m_yBorder - 1 , color );
640
+ m_image-> drawText ( xPos + 2 , 0 , scaleText, m_scaleColor );
641
+ m_image-> drawLine ( xPos, 0 , xPos, m_yBorder - 1 , m_scaleColor );
660
642
}
661
643
}
662
644
663
645
if (m_scales & SCALE_LEFT) {
664
- gdImageString ( m_image, gdFontGetMediumBold (), 2 , 24 , reinterpret_cast < unsigned char *>( const_cast < char *>( " Z" )), color );
646
+ m_image-> drawText ( 2 , 24 , " Z" , m_scaleColor );
665
647
for (int i = (m_zMax / 4 ) * 4 ; i >= m_zMin; i -= 4 ) {
666
648
stringstream buf;
667
649
buf << i * 16 ;
668
650
scaleText = buf.str ();
669
651
670
652
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16 ))*m_zoom + m_yBorder;
671
- gdImageString ( m_image, gdFontGetMediumBold (), 2 , yPos, reinterpret_cast < unsigned char *>( const_cast < char *>( scaleText. c_str ())), color );
672
- gdImageLine ( m_image, 0 , yPos, m_xBorder - 1 , yPos, color );
653
+ m_image-> drawText ( 2 , yPos, scaleText, m_scaleColor );
654
+ m_image-> drawLine ( 0 , yPos, m_xBorder - 1 , yPos, m_scaleColor );
673
655
}
674
656
}
675
657
@@ -680,9 +662,9 @@ void TileGenerator::renderScale()
680
662
scaleText = buf.str ();
681
663
682
664
int xPos = (m_xMin * -16 + i * 16 )*m_zoom + m_xBorder;
683
- int yPos = m_yBorder + m_mapHeight;
684
- gdImageString ( m_image, gdFontGetMediumBold (), xPos + 2 , yPos, reinterpret_cast < unsigned char *>( const_cast < char *>( scaleText. c_str ())), color );
685
- gdImageLine ( m_image, xPos, yPos, xPos, yPos + 39 , color );
665
+ int yPos = m_yBorder + m_mapHeight*m_zoom ;
666
+ m_image-> drawText ( xPos + 2 , yPos, scaleText, m_scaleColor );
667
+ m_image-> drawLine ( xPos, yPos, xPos, yPos + 39 , m_scaleColor );
686
668
}
687
669
}
688
670
@@ -692,10 +674,10 @@ void TileGenerator::renderScale()
692
674
buf << i * 16 ;
693
675
scaleText = buf.str ();
694
676
695
- int xPos = m_xBorder + m_mapWidth;
677
+ int xPos = m_xBorder + m_mapWidth*m_zoom ;
696
678
int yPos = (m_mapHeight - 1 - (i * 16 - m_zMin * 16 ))*m_zoom + m_yBorder;
697
- gdImageString ( m_image, gdFontGetMediumBold (), xPos + 2 , yPos, reinterpret_cast < unsigned char *>( const_cast < char *>( scaleText. c_str ())), color );
698
- gdImageLine ( m_image, xPos, yPos, xPos + 39 , yPos, color );
679
+ m_image-> drawText ( xPos + 2 , yPos, scaleText, m_scaleColor );
680
+ m_image-> drawLine ( xPos, yPos, xPos + 39 , yPos, m_scaleColor );
699
681
}
700
682
}
701
683
}
@@ -704,20 +686,18 @@ void TileGenerator::renderOrigin()
704
686
{
705
687
int imageX = (-m_xMin * 16 )*m_zoom + m_xBorder;
706
688
int imageY = (m_mapHeight - m_zMin * -16 )*m_zoom + m_yBorder;
707
- gdImageArc ( m_image, imageX, imageY, 12 , 12 , 0 , 360 , color2int ( m_originColor) );
689
+ m_image-> drawCircle ( imageX, imageY, 12 , m_originColor);
708
690
}
709
691
710
692
void TileGenerator::renderPlayers (const std::string &inputPath)
711
693
{
712
- int color = color2int (m_playerColor);
713
-
714
694
PlayerAttributes players (inputPath);
715
695
for (PlayerAttributes::Players::iterator player = players.begin (); player != players.end (); ++player) {
716
696
int imageX = (player->x / 10 - m_xMin * 16 )*m_zoom + m_xBorder;
717
697
int imageY = (m_mapHeight - (player->z / 10 - m_zMin * 16 ))*m_zoom + m_yBorder;
718
698
719
- gdImageArc ( m_image, imageX, imageY, 5 , 5 , 0 , 360 , color );
720
- gdImageString ( m_image, gdFontGetMediumBold (), imageX + 2 , imageY + 2 , reinterpret_cast < unsigned char *>( const_cast < char *>( player->name . c_str ())), color );
699
+ m_image-> drawCircle ( imageX, imageY, 5 , m_playerColor );
700
+ m_image-> drawText ( imageX + 2 , imageY + 2 , player->name , m_playerColor );
721
701
}
722
702
}
723
703
@@ -735,16 +715,9 @@ inline std::list<int> TileGenerator::getZValueList() const
735
715
736
716
void TileGenerator::writeImage (const std::string &output)
737
717
{
738
- FILE *out;
739
- out = fopen (output.c_str (), " wb" );
740
- if (!out) {
741
- std::ostringstream oss;
742
- oss << " Error opening '" << output.c_str () << " ': " << std::strerror (errno);
743
- throw std::runtime_error (oss.str ());
744
- }
745
- gdImagePng (m_image, out);
746
- fclose (out);
747
- gdImageDestroy (m_image);
718
+ m_image->save (output);
719
+ delete m_image;
720
+ m_image = NULL ;
748
721
}
749
722
750
723
void TileGenerator::printUnknown ()
@@ -767,11 +740,6 @@ inline int TileGenerator::getImageY(int val) const
767
740
return (m_zoom*val) + m_yBorder;
768
741
}
769
742
770
- inline void TileGenerator::setZoomed (gdImagePtr image, int y, int x, int color) {
771
- int xx,yy;
772
- for (xx = 0 ; xx < m_zoom; xx++) {
773
- for (yy = 0 ; yy < m_zoom; yy++) {
774
- image->tpixels [m_yBorder + (y*m_zoom) + xx][m_xBorder + (x*m_zoom) + yy] = color;
775
- }
776
- }
743
+ inline void TileGenerator::setZoomed (int x, int y, Color color) {
744
+ m_image->drawFilledRect (getImageX (x), getImageY (y), m_zoom, m_zoom, color);
777
745
}
0 commit comments