45 ( png_structp png_ptr, png_bytep data, png_size_t length )
50 self->
read(data, length);
74 ( png_bytep data, png_size_t length )
76 m_input.read( (
char*)data, length *
sizeof(png_byte) );
83 const unsigned int claw::graphic::png::reader::s_rgba_pixel_size = 4;
118 std::istream::pos_type init_pos = f.tellg();
127 f.seekg( init_pos, std::ios_base::beg );
137 void claw::graphic::png::reader::read_from_file( std::istream& f )
139 source_manager infile(f);
143 create_read_structures(png_ptr, info_ptr);
145 if (setjmp(png_jmpbuf(png_ptr)))
149 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
153 check_if_png( png_ptr, f );
155 png_set_read_fn( png_ptr, (
void *)&infile,
158 png_set_strip_16(png_ptr);
159 png_set_expand_gray_1_2_4_to_8(png_ptr);
160 png_set_packing(png_ptr);
163 png_set_palette_to_rgb(png_ptr);
166 png_set_filler( png_ptr,
167 std::numeric_limits<rgba_pixel_8::component_type>::max(),
170 png_read_info(png_ptr, info_ptr);
171 read_image( png_ptr, info_ptr );
173 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
182 void claw::graphic::png::reader::check_if_png
183 ( png_structp png_ptr, std::istream& f )
const
187 const unsigned int bytes_to_check = 8;
188 png_byte buffer[bytes_to_check];
191 f.read( (
char*)buffer, bytes_to_check *
sizeof(png_byte) );
193 if ( (png_sig_cmp(buffer, (png_size_t)0, bytes_to_check) != 0) || !f )
196 png_set_sig_bytes(png_ptr, bytes_to_check);
205 void claw::graphic::png::reader::read_image
206 ( png_structp png_ptr, png_infop info_ptr )
211 m_image.set_size( png_get_image_width(png_ptr, info_ptr),
212 png_get_image_height(png_ptr, info_ptr) );
214 if ( png_get_interlace_type(png_ptr, info_ptr) == PNG_INTERLACE_NONE )
215 read_sequential_image(png_ptr, info_ptr);
217 read_interlaced_image( png_ptr, info_ptr,
218 png_set_interlace_handling(png_ptr) );
227 void claw::graphic::png::reader::read_sequential_image
228 ( png_structp png_ptr, png_infop info_ptr )
234 (png_bytep)png_malloc( png_ptr, s_rgba_pixel_size * m_image.width() );
238 for (
unsigned int y=0; y!=m_image.height(); ++y)
240 png_read_row(png_ptr, data, NULL);
241 copy_pixel_line( data, y );
246 png_free(png_ptr, data);
250 png_free(png_ptr, data);
260 void claw::graphic::png::reader::read_interlaced_image
261 ( png_structp png_ptr, png_infop info_ptr,
unsigned int passes )
267 const unsigned int row_length = s_rgba_pixel_size * m_image.width();
269 (png_bytepp)png_malloc( png_ptr,
sizeof(png_bytep) * m_image.height() );
274 for (i=0; i!=m_image.height(); ++i)
276 data[i] = (png_bytep)png_malloc( png_ptr, row_length );
279 throw std::bad_alloc();
281 copy_pixel_line( data[i], i );
284 for (
unsigned int p=0; p!=passes; ++p)
285 png_read_rows( png_ptr, data, NULL, m_image.height() );
287 for (
unsigned int y=0; y!=m_image.height(); ++y)
288 copy_pixel_line( data[y], y );
292 for(
unsigned int j=0; j!=i; ++j)
293 png_free(png_ptr, data[j]);
295 png_free(png_ptr, data);
299 for(i=0; i!=m_image.height(); ++i)
300 png_free(png_ptr, data[i]);
302 png_free(png_ptr, data);
312 claw::graphic::png::reader::copy_pixel_line( png_bytep data,
unsigned int y )
318 for (
unsigned int x=0; x!=m_image.width(); ++x, data+=s_rgba_pixel_size)
320 m_image[y][x].components.red = data[0];
321 m_image[y][x].components.green = data[1];
322 m_image[y][x].components.blue = data[2];
323 m_image[y][x].components.alpha = data[3];
333 void claw::graphic::png::reader::create_read_structures
334 ( png_structp& png_ptr, png_infop& info_ptr )
const
336 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
340 info_ptr = png_create_info_struct(png_ptr);
343 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
346 if (!png_ptr || !info_ptr)