From f84828b0bfdd9549a81ac12189d9c050ad8ebf60 Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Thu, 15 Nov 2018 12:35:10 +0100 Subject: [PATCH] Add a load function to Image, also add some consts. --- Image.cpp | 21 +++++++++++++++++++-- include/Image.h | 6 ++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Image.cpp b/Image.cpp index d8c631c..650083d 100644 --- a/Image.cpp +++ b/Image.cpp @@ -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) { diff --git a/include/Image.h b/include/Image.h index 216a0c2..08cd2cb 100644 --- a/include/Image.h +++ b/include/Image.h @@ -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: