Add a load function to Image, also add some consts.

This commit is contained in:
Martijn Versteegh 2018-11-15 12:35:10 +01:00
parent 503f6b6277
commit f84828b0bf
2 changed files with 23 additions and 4 deletions

View File

@ -74,7 +74,7 @@ void Image::setPixel(int x, int y, const Color &c)
m_image->tpixels[y][x] = color2int(c);
}
Color Image::getPixel(int x, int y)
Color Image::getPixel(int x, int y) const
{
SIZECHECK_FUZZY(x, y);
return int2color(m_image->tpixels[y][x]);
@ -106,7 +106,7 @@ void Image::drawCircle(int x, int y, int diameter, const Color &c)
gdImageArc(m_image, x, y, diameter, diameter, 0, 360, color2int(c));
}
void Image::save(const std::string &filename)
void Image::save(const std::string &filename) const
{
#if (GD_MAJOR_VERSION == 2 && GD_MINOR_VERSION == 1 && GD_RELEASE_VERSION >= 1) || (GD_MAJOR_VERSION == 2 && GD_MINOR_VERSION > 1) || GD_MAJOR_VERSION > 2
const char *f = filename.c_str();
@ -128,6 +128,23 @@ void Image::save(const std::string &filename)
#endif
}
Image::Image(std::string const &fileName)
{
m_image = gdImageCreateFromFile(fileName.c_str());
if (!m_image)
{
std::ostringstream oss;
oss << "Error loading image file: " << fileName;
throw std::runtime_error(oss.str());
}
}
void Image::scaleBlit(Image *to, int x, int y, int w, int h) const
{
gdImageCopyResampled(to->m_image, m_image, x,y, 0,0, w, h, gdImageSX(m_image),gdImageSY(m_image));
}
void Image::fill(Color &c)
{

View File

@ -16,16 +16,18 @@ struct Color {
class Image {
public:
Image(const std::string &fileName);
Image(int width, int height);
~Image();
void scaleBlit(Image *to, int x, int y, int w, int h) const;
void setPixel(int x, int y, const Color &c);
Color getPixel(int x, int y);
Color getPixel(int x, int y) const;
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);
void save(const std::string &filename) const;
void fill(Color &c);
private: