1
0
mirror of https://github.com/luanti-org/minetestmapper.git synced 2025-10-07 22:35:21 +02:00

Initial work on using FreeImage

This commit is contained in:
sfan5
2016-12-05 12:40:27 +01:00
parent 02e40d268b
commit 75009ad49c
4 changed files with 40 additions and 43 deletions

View File

@@ -4,8 +4,6 @@
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <gd.h>
#include <gdfontmb.h>
#include "Image.h"
@@ -22,40 +20,41 @@
// ARGB but with inverted alpha
static inline int color2int(Color c)
static inline RGBQUAD color2rgbquad(Color c)
{
u8 a = 255 - c.a;
return (a << 24) | (c.r << 16) | (c.g << 8) | c.b;
RGBQUAD c2;
c2.rgbRed = c.r;
c2.rgbGreen = c.g;
c2.rgbBlue = c.b;
return c2;
}
static inline Color int2color(int c)
static inline Color rgbquad2color(RGBQUAD 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;
return Color(c.rgbRed, c.rgbGreen, c.rgbBlue);
}
Image::Image(int width, int height) :
m_width(width), m_height(height), m_image(NULL)
{
m_image = gdImageCreateTrueColor(m_width, m_height);
// FIXME: This works because minetestmapper only creates one image
FreeImage_Initialise();
printf("Using FreeImage v%s\n", FreeImage_GetVersion());
m_image = FreeImage_Allocate(width, height, 24);
}
Image::~Image()
{
gdImageDestroy(m_image);
FreeImage_Unload(m_image);
}
void Image::setPixel(int x, int y, const Color &c)
{
SIZECHECK(x, y);
m_image->tpixels[y][x] = color2int(c);
RGBQUAD col = color2rgbquad(c);
FreeImage_SetPixelColor(m_image, x, y, &col);
}
Color Image::getPixel(int x, int y)
@@ -64,43 +63,41 @@ Color Image::getPixel(int x, int y)
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]);
RGBQUAD c;
FreeImage_GetPixelColor(m_image, x, y, &c);
return rgbquad2color(c);
}
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));
// TODO
}
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));
// TODO
}
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));
RGBQUAD col = color2rgbquad(c);
for(int xx = 0; xx < w; xx++)
for(int yy = 0; yy < h; yy++)
FreeImage_SetPixelColor(m_image, x + xx, y + yy, &col);
}
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));
// TODO
}
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);
FreeImage_Save(FIF_PNG, m_image, filename.c_str()); // other formats?
}