Simple Image Loading LibrarY 0.1.0
SILLYPNGImageLoader.cpp
1/***********************************************************************
2 filename: SILLYPNGImageLoader.cpp
3 created: 11 Jun 2006
4 author: Olivier Delannoy
5
6 purpose: Definition of the PNGImageLoader methods
7*************************************************************************/
8/***************************************************************************
9 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * a copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 ***************************************************************************/
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#include <string.h>
33#endif
34
35#include "loaders/SILLYPNGImageLoader.h"
36
37#ifndef SILLY_OPT_INLINE
38#define inline
39#include "loaders/SILLYPNGImageLoader.icpp"
40#undef inline
41#endif
42
43#include "loaders/SILLYPNGImageContext.h"
44#include <png.h>
45// Start section of namespace SILLY
46namespace SILLY
47{
48void PNG_read_function(png_structp png_ptr, png_bytep data, png_size_t length)
49{
50 PNGImageContext* png = reinterpret_cast<PNGImageContext*>(png_get_io_ptr(png_ptr));
51 int readed = png->read(data, length);
52 if (readed != (int)length)
53 {
54 png_error(png_ptr, "PNG_read_function error");
55 }
56}
57
58void PNG_warning_function(png_structp png_ptr,
59 png_const_charp error)
60{
61// printf("PNG Warning: %s\n", error);
62}
63
64void PNG_error_function(png_structp png_ptr,
65 png_const_charp error)
66{
67 // printf("PNG Error: %s\n", error);
68 png_longjmp(png_ptr, 1);
69}
70
71
72PNGImageLoader::PNGImageLoader()
73 : ImageLoader("PNG Image Loader based on libpng")
74{
75}
76PNGImageLoader::~PNGImageLoader()
77{
78}
79
80
81ImageContext* PNGImageLoader::loadHeader(PixelFormat& formatSource, DataSource* data)
82{
83 PNGImageContext* png = new PNGImageContext(data);
84 if (!png)
85 {
86 return 0;
87
88 }
89 // Prepare png loading
90 png->d_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
91 if (png->d_png_ptr == 0)
92 {
93 delete png;
94 return 0;
95 }
96 png->d_info_ptr = png_create_info_struct(png->d_png_ptr);
97 if (png->d_info_ptr == 0)
98 {
99 delete png;
100 return 0;
101 }
102 if (setjmp(png_jmpbuf(png->d_png_ptr)))
103 {
104 delete png;
105 return 0;
106 }
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);
109 //png_set_sig_bytes(png->d_png_ptr, 8);
110
111
112
113 // Read header Check whether PNG can depaletize transparently or not
114 int png_transform = PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_EXPAND;
115 //printf("Start reading png\n");
116 png_read_png(png->d_png_ptr, png->d_info_ptr, png_transform, 0);
117 png->setImageSize();
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);
120 //printf("PNG Info: width: %d height: %d bpp: %d channels: %d\n", png->getWidth(), png->getHeight(), png->d_bit_depth, png->d_num_channels);
121 if (png->d_bit_depth == 8)
122 {
123 if (png->d_num_channels == 4)
124 {
125 formatSource = PF_RGBA;
126 }
127 else if (png->d_num_channels == 3)
128 {
129 formatSource = PF_RGB;
130 }
131 else
132 {
133 delete png;
134 return 0;
135 }
136 }
137 // Paletized or grayscale not yet handled
138 else
139 {
140 delete png;
141 return 0;
142 }
143 return png;
144}
145
146
147bool PNGImageLoader::loadImageData(PixelOrigin origin,
148 DataSource* data,
149 ImageContext* context)
150{
151 PNGImageContext* png = static_cast<PNGImageContext*>(context);
152 byte red;
153 byte green;
154 byte blue;
155 byte alpha;
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)
160 {
161 // Read RGBA
162 if (png->d_num_channels == 4)
163 {
164 for (size_t j = 0 ; j < height ; ++j)
165 {
166 for(size_t i = 0 ; i < width ; ++i)
167 {
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);
173 png->setNextPixel(red, green, blue, alpha);
174 }
175 }
176 }
177 else if (png->d_num_channels == 3)
178 {
179 alpha = 0xff;
180 for (size_t j = 0 ; j < height ; ++j)
181 {
182 for(size_t i = 0 ; i < width ; ++i)
183 {
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);
188 png->setNextPixel(red, green, blue, alpha);
189 }
190 }
191
192 }
193 }
194 if (origin == PO_BOTTOM_LEFT)
195 return png->flipVertically();
196
197 return true;
198}
199
200} // End section of namespace SILLY
Simple Image Loading LibrarY namespace.
PixelFormat
List all pixel format supported.
Definition SILLYBase.h:60
PixelOrigin
List all pixel origin supported.
Definition SILLYBase.h:71
Image Context for PNG Image Loader.
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.