34 : m_color_space(
Image::RGBA),
43 : m_tileable(tileable_)
49 class file_Destroyer {
51 file_Destroyer(FILE *
const &file_) : m_file(file_) {}
52 ~file_Destroyer() {fclose(m_file);}
56 } file_destroyer(file);
61 if(!fread(header, 8
u, 1
u, file) ||
png_sig_cmp(header, 0, 8)) {
70 ZENI_LOGE(
"png_create_read_struct(...) failed.");
76 class png_structp_Destroyer {
78 png_structp_Destroyer(
const png_structp &ptr) : m_ptr(ptr) {}
83 } png_structp_destroyer(png_ptr);
86 png_infop info_ptr = png_create_info_struct(png_ptr);
88 ZENI_LOGE(
"png_create_info_struct(...) failed.");
95 #pragma warning( push )
96 #pragma warning( disable : 4611 )
101 #pragma warning( pop )
103 ZENI_LOGE(
"setjmp(png_jmpbuf(...)) failed.");
121 int bit_depth, color_type;
127 ZENI_LOGE(
"Image using bit depth other than 8.");
133 m_bytes_per_pixel = 1;
138 m_bytes_per_pixel = 2;
143 m_bytes_per_pixel = 3;
147 m_color_space =
RGBA;
148 m_bytes_per_pixel = 4;
152 ZENI_LOGE(
"Image using must be Grayscale, RGB, or RGBA.");
157 m_size.
x =
int(twidth);
158 m_size.
y =
int(theight);
167 m_data.resize(m_row_size * theight);
169 std::vector<Uint8 *> row_pointers(theight);
171 for(
int j = 0;
j < m_size.
y; ++
j)
172 row_pointers[
j] = &m_data[0] +
j * m_row_size;
175 png_read_image(png_ptr, reinterpret_cast<png_bytep *>(&row_pointers[0]));
182 m_color_space(color_space_),
183 m_bytes_per_pixel(color_space_ == Luminance ? 1 : color_space_ == Luminance_Alpha ? 2 : color_space_ ==
RGB ? 3 : 4),
184 m_row_size(size_.
x * m_bytes_per_pixel),
185 m_data(size_.
y * m_row_size,
'\0'),
186 m_tileable(tileable_)
196 const Uint8 *
const rgba_value =
reinterpret_cast<const Uint8 *
>(&rgba);
197 Uint8 *
const pixel_value = get_pixel(pixel);
199 switch(m_color_space) {
201 pixel_value[1] = rgba_value[3];
207 pixel_value[3] = rgba_value[3];
209 pixel_value[2] = rgba_value[2];
210 pixel_value[1] = rgba_value[1];
211 pixel_value[0] = rgba_value[0];
220 const Uint8 *
const pixel_value = get_pixel(pixel);
222 switch(m_color_space) {
225 const float rgb = pixel_value[0] / 255.0f;
226 return Color(1.0
f, rgb, rgb, rgb);
231 const float rgb = pixel_value[0] / 255.0f;
232 const float alpha = pixel_value[1] / 255.0f;
233 return Color(alpha, rgb, rgb, rgb);
237 return Color(1.0
f, pixel_value[0] / 255.0
f, pixel_value[1] / 255.0
f, pixel_value[2] / 255.0
f);
240 return Color(pixel_value[3] / 255.0
f, pixel_value[0] / 255.0
f, pixel_value[1] / 255.0
f, pixel_value[2] / 255.0
f);
249 const Uint8 *
const pixel_value = get_pixel(pixel);
251 switch(m_color_space) {
254 const Uint32 rgb = pixel_value[0];
255 return (rgb << 24) | (rgb << 16) | (rgb << 8) | 0xFF;
260 const Uint32 rgb = pixel_value[0];
261 return (rgb << 24) | (rgb << 16) | (rgb << 8) | pixel_value[1];
265 return (
Uint32(pixel_value[0]) << 24) | (
Uint32(pixel_value[1]) << 16) | (
Uint32(pixel_value[2]) << 8) | 0xFF;
268 return (
Uint32(pixel_value[0]) << 24) | (
Uint32(pixel_value[1]) << 16) | (
Uint32(pixel_value[2]) << 8) | pixel_value[3];
277 const Point2f scaled(coordinate.
x * m_size.
x,
278 coordinate.
y * m_size.
y);
285 ul.
x = m_size.
x - ((-ul.
x) % m_size.
x);
287 ul.
x = ul.
x % m_size.
x;
290 ul.
y = m_size.
y - ((-ul.
y) % m_size.
y);
292 ul.
y = ul.
y % m_size.
y;
297 else if(ul.
x >= m_size.
x)
302 else if(ul.
y >= m_size.
y)
310 lr.
x = lr.
x % m_size.
x;
311 lr.
y = lr.
y % m_size.
y;
325 const float x_rhs_part = scaled.
x - float(
floor(scaled.
x));
326 const float y_rhs_part = scaled.
y - float(
floor(scaled.
y));
335 Image resized(
Point2i(width, height), m_color_space, m_tileable);
337 const float w = float(width);
338 const float h = float(height);
345 std::swap(m_color_space, resized.m_color_space);
346 std::swap(m_bytes_per_pixel, resized.m_bytes_per_pixel);
347 std::swap(m_row_size, resized.m_row_size);
349 std::swap(m_tileable, resized.m_tileable);
353 if(
this == &source || m_color_space != source.m_color_space)
379 const Point2i lower_right(upper_left.
x + source.m_size.
x,
380 upper_left.
y + source.m_size.
y);
382 const int dx0 =
std::max(0, upper_left.
x);
383 const int dy0 =
std::max(0, upper_left.
y);
384 const int dx1 =
std::min(m_size.
x, lower_right.
x);
385 const int dy1 =
std::min(m_size.
y, lower_right.
y);
386 const int dx = dx1 - dx0;
387 const int dy = dy1 - dy0;
388 const int sx0 = dx0 - upper_left.
x;
389 const int sy0 = dy0 - upper_left.
y;
398 const Uint8 *
const dend = dst + dy * m_row_size;
399 int sz = dx * m_bytes_per_pixel;
401 assert(&m_data[0] <= dst && dst + sz <= &m_data[0] + m_data.size());
402 assert(&source.m_data[0] <= src && src + sz <= &source.m_data[0] + source.m_data.size());
405 src += source.m_row_size;
411 const Uint8 * Image::get_pixel(
const Point2i &pixel)
const {
412 assert(0 <= pixel.
x && pixel.
x < m_size.
x);
413 assert(0 <= pixel.
y && pixel.
y < m_size.
y);
415 return &m_data[0] + pixel.
y * m_row_size + pixel.
x * m_bytes_per_pixel;
418 Uint8 * Image::get_pixel(
const Point2i &pixel) {
419 assert(0 <= pixel.x && pixel.x < m_size.
x);
420 assert(0 <= pixel.y && pixel.y < m_size.
y);
422 return &m_data[0] + pixel.y * m_row_size + pixel.x * m_bytes_per_pixel;
png_size_t PNGAPI png_get_rowbytes(png_const_structp png_ptr, png_const_infop info_ptr)
cannot open resource broken file module version is too low unimplemented feature broken offset within table missing module invalid character code cannot render this glyph format invalid composite glyph invalid pixel size invalid library handle invalid face handle invalid glyph slot handle invalid cache manager handle too many modules out of memory cannot open stream invalid stream skip invalid stream operation nested frame access raster uninitialized raster overflow too many registered caches too few arguments code overflow division by zero found debug opcode nested DEFS execution context too long too many instruction definitions horizontal header(hhea) table missing" ) FT_ERRORDEF_( Locations_Missing
Color extract_Color(const Point2i &pixel) const
Get the Color value of the pixel specified [0, 0] being the upper left, (width, height) being the low...
GLfloat GLfloat GLfloat GLfloat h
Uint32 extract_RGBA(const Point2i &pixel) const
Get the Color value of the pixel specified [0, 0] being the upper left, (width, height) being the low...
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_LIBPNG_VER_STRING
void PNGAPI png_set_sig_bytes(png_structp png_ptr, int num_bytes)
EGLSurface EGLint EGLint EGLint EGLint height
#define PNG_COLOR_TYPE_RGB_ALPHA
Uint32 get_rgba() const
Get a Uint32 representation of 0xRRGGBBAA.
#define png_jmpbuf(png_ptr)
uint32_t Uint32
An unsigned 32-bit integer type.
png_struct FAR * png_structp
png_uint_32 PNGAPI png_get_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_type, int *compression_type, int *filter_type)
EGLSurface EGLint EGLint EGLint width
#define PNG_COLOR_TYPE_GRAY
void set_Color(const Point2i &pixel, const Color &color)
Set the Color value of the pixel specified [0, 0] being the upper left, (width, height) being the low...
void resize(const int &width, const int &height)
Color interpolate_to(const float &rhs_part, const Color &rhs) const
Get a color that is inbetween this color and another color.
GLclampf GLclampf GLclampf alpha
bool blit(const Point2i &upper_left, const Image &source)
Copy a different Image in the same color-space into this Image. Returns true if blits successfully...
void ZENI_LOGE(const Zeni::String &str)
void PNGAPI png_read_info(png_structp png_ptr, png_infop info_ptr)
EGLSurface EGLint EGLint y
int width() const
Get the number of pixels in the image in the x-direction.
void set_RGBA(const Point2i &pixel, const Uint32 &rgba)
Set the Color value of the pixel specified [0, 0] being the upper left, (width, height) being the low...
void swap(Zeni::String &lhs, Zeni::String &rhs)
uint8_t Uint8
An unsigned 8-bit integer type.
int height() const
Get the number of pixels in the image in the y-direction.
static FILE * get_asset_FILE(const String &filename, off_t *const &start=0, off_t *const &length=0)
Get a FILE * from an Asset.
void PNGAPI png_read_image(png_structp png_ptr, png_bytepp image)
GLint GLint GLint GLint GLint w
uint16_t Uint16
An unsigned 16-bit integer type.
void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)
A 2D Point represented with floats.
GLsizei GLsizei GLchar * source
void PNGAPI png_read_update_info(png_structp png_ptr, png_infop info_ptr)
void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp)
int PNGAPI png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
A 2D Point represented with integers.