forked from ShadowNinja/minetest-mapper-cpp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move everything that deals with images directly to an Image class
- Loading branch information
Showing
6 changed files
with
176 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <cstdio> | ||
#include <cerrno> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <gd.h> | ||
#include <gdfontmb.h> | ||
|
||
#include "Image.h" | ||
|
||
#ifndef NDEBUG | ||
#define SIZECHECK(x, y) do { \ | ||
if((x) < 0 || (x) >= m_width) \ | ||
throw std::out_of_range("sizecheck x"); \ | ||
if((y) < 0 || (y) >= m_height) \ | ||
throw std::out_of_range("sizecheck y"); \ | ||
} while(0) | ||
#else | ||
#define SIZECHECK(x, y) do {} while(0) | ||
#endif | ||
|
||
// BGRA but with inverted alpha | ||
|
||
static inline int color2int(Color c) | ||
{ | ||
u8 a = 255 - c.a; | ||
return (a << 24) | (c.r << 16) | (c.g << 8) | c.b; | ||
} | ||
|
||
static inline Color int2color(int c) | ||
{ | ||
Color c2; | ||
u8 a; | ||
c2.b = c & 0xff; | ||
c2.g = (c >> 8) & 0xff; | ||
c2.r = (c >> 16) & 0xff; | ||
a = (c >> 24) & 0xff; | ||
c2.a = 255 - a; | ||
return c2; | ||
} | ||
|
||
|
||
Image::Image(int width, int height) : | ||
m_width(width), m_height(height), m_image(NULL) | ||
{ | ||
m_image = gdImageCreateTrueColor(m_width, m_height); | ||
} | ||
|
||
Image::~Image() | ||
{ | ||
gdImageDestroy(m_image); | ||
} | ||
|
||
void Image::setPixel(int x, int y, const Color &c) | ||
{ | ||
SIZECHECK(x, y); | ||
m_image->tpixels[y][x] = color2int(c); | ||
} | ||
|
||
Color Image::getPixel(int x, int y) | ||
{ | ||
#ifndef NDEBUG | ||
if(x < 0 || x > m_width || y < 0 || y > m_height) | ||
throw std::out_of_range("sizecheck"); | ||
#endif | ||
return int2color(m_image->tpixels[y][x]); | ||
} | ||
|
||
void Image::drawLine(int x1, int y1, int x2, int y2, const Color &c) | ||
{ | ||
SIZECHECK(x1, y1); | ||
SIZECHECK(x2, y2); | ||
gdImageLine(m_image, x1, y1, x2, y2, color2int(c)); | ||
} | ||
|
||
void Image::drawText(int x, int y, const std::string &s, const Color &c) | ||
{ | ||
SIZECHECK(x, y); | ||
gdImageString(m_image, gdFontGetMediumBold(), x, y, (unsigned char*) s.c_str(), color2int(c)); | ||
} | ||
|
||
void Image::drawFilledRect(int x, int y, int w, int h, const Color &c) | ||
{ | ||
SIZECHECK(x, y); | ||
SIZECHECK(x + w - 1, y + h - 1); | ||
gdImageFilledRectangle(m_image, x, y, x + w - 1, y + h - 1, color2int(c)); | ||
} | ||
|
||
void Image::drawCircle(int x, int y, int diameter, const Color &c) | ||
{ | ||
SIZECHECK(x, y); | ||
gdImageArc(m_image, x, y, diameter, diameter, 0, 360, color2int(c)); | ||
} | ||
|
||
void Image::save(const std::string &filename) | ||
{ | ||
FILE *f = fopen(filename.c_str(), "wb"); | ||
if(!f) { | ||
std::ostringstream oss; | ||
oss << "Error writing image file: " << std::strerror(errno); | ||
throw std::runtime_error(oss.str()); | ||
} | ||
gdImagePng(m_image, f); // other formats? | ||
fclose(f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef IMAGE_HEADER | ||
#define IMAGE_HEADER | ||
|
||
#include "types.h" | ||
#include <string> | ||
#include <gd.h> | ||
|
||
struct Color { | ||
Color() : r(0), g(0), b(0), a(0) {}; | ||
Color(u8 r, u8 g, u8 b) : r(r), g(g), b(b), a(255) {}; | ||
Color(u8 r, u8 g, u8 b, u8 a) : r(r), g(g), b(b), a(a) {}; | ||
u8 r, g, b, a; | ||
}; | ||
|
||
class Image { | ||
public: | ||
Image(int width, int height); | ||
~Image(); | ||
|
||
void setPixel(int x, int y, const Color &c); | ||
Color getPixel(int x, int y); | ||
void drawLine(int x1, int y1, int x2, int y2, const Color &c); | ||
void drawText(int x, int y, const std::string &s, const Color &c); | ||
void drawFilledRect(int x, int y, int w, int h, const Color &c); | ||
void drawCircle(int x, int y, int diameter, const Color &c); | ||
void save(const std::string &filename); | ||
|
||
private: | ||
Image(const Image&); | ||
|
||
int m_width, m_height; | ||
gdImagePtr m_image; | ||
}; | ||
|
||
#endif // IMAGE_HEADER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.