35#include "loaders/SILLYPNGImageLoader.h"
37#ifndef SILLY_OPT_INLINE
39#include "loaders/SILLYPNGImageLoader.icpp"
43#include "loaders/SILLYPNGImageContext.h"
48void PNG_read_function(png_structp png_ptr, png_bytep data, png_size_t length)
51 int readed = png->read(data, length);
52 if (readed != (
int)length)
54 png_error(png_ptr,
"PNG_read_function error");
58void PNG_warning_function(png_structp png_ptr,
59 png_const_charp error)
64void PNG_error_function(png_structp png_ptr,
65 png_const_charp error)
68 png_longjmp(png_ptr, 1);
72PNGImageLoader::PNGImageLoader()
76PNGImageLoader::~PNGImageLoader()
90 png->d_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
91 if (png->d_png_ptr == 0)
96 png->d_info_ptr = png_create_info_struct(png->d_png_ptr);
97 if (png->d_info_ptr == 0)
102 if (setjmp(png_jmpbuf(png->d_png_ptr)))
107 png_set_error_fn(png->d_png_ptr, 0, PNG_error_function, PNG_warning_function);
108 png_set_read_fn(png->d_png_ptr, png, PNG_read_function);
114 int png_transform = PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_EXPAND;
116 png_read_png(png->d_png_ptr, png->d_info_ptr, png_transform, 0);
118 png->d_bit_depth = png_get_bit_depth(png->d_png_ptr, png->d_info_ptr);
119 png->d_num_channels = png_get_channels(png->d_png_ptr, png->d_info_ptr);
121 if (png->d_bit_depth == 8)
123 if (png->d_num_channels == 4)
125 formatSource = PF_RGBA;
127 else if (png->d_num_channels == 3)
129 formatSource = PF_RGB;
156 size_t width = png->getWidth();
157 size_t height = png->getHeight();
158 png_bytepp row_pointers = png_get_rows(png->d_png_ptr, png->d_info_ptr);
159 if (png->d_bit_depth == 8)
162 if (png->d_num_channels == 4)
164 for (
size_t j = 0 ; j < height ; ++j)
166 for(
size_t i = 0 ; i < width ; ++i)
168 size_t pixel_offset = 4 * i;
169 red = *(row_pointers[j] + pixel_offset);
170 green = *(row_pointers[j] + pixel_offset + 1);
171 blue = *(row_pointers[j] + pixel_offset + 2);
172 alpha = *(row_pointers[j] + pixel_offset + 3);
177 else if (png->d_num_channels == 3)
180 for (
size_t j = 0 ; j < height ; ++j)
182 for(
size_t i = 0 ; i < width ; ++i)
184 size_t pixel_offset = 3 * i;
185 red = *(row_pointers[j] + pixel_offset);
186 green = *(row_pointers[j] + pixel_offset + 1);
187 blue = *(row_pointers[j] + pixel_offset + 2);
194 if (origin == PO_BOTTOM_LEFT)
Simple Image Loading LibrarY namespace.
PixelFormat
List all pixel format supported.
PixelOrigin
List all pixel origin supported.
Image Context for PNG Image Loader.
ImageContext * loadHeader(PixelFormat &formatSource, DataSource *data)
Parse the header of the image and fill the header struct.
bool loadImageData(PixelOrigin origin, DataSource *data, ImageContext *context)
Parse the pixels data of the image and fill the header struct.
This is an abstract class used to provide data to the loader.
Store the data needed by an ImageLoader object during the parsing of an image.
void setNextPixel(byte red, byte green, byte bleu, byte alpha)
Set the next pixel of the image.
bool flipVertically()
Flip pixel ordering.
This is an abstract class that define the interface of all image loader.