1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
47 /****************************************************************************************\
48 This part of the file implements PNG codec on base of libpng library,
49 in particular, this code is based on example.c from libpng
50 (see otherlibs/_graphics/readme.txt for copyright notice)
51 and png2bmp sample from libpng distribution (Copyright (C) 1999-2001 MIYASAKA Masaru)
52 \****************************************************************************************/
54 #ifdef HAVE_LIBPNG_PNG_H
55 #include <libpng/png.h>
59 #include "grfmt_png.h"
64 /////////////////////// PngDecoder ///////////////////
66 PngDecoder::PngDecoder()
68 m_signature = "\x89\x50\x4e\x47\xd\xa\x1a\xa";
71 m_info_ptr = m_end_info = 0;
73 m_buf_supported = true;
78 PngDecoder::~PngDecoder()
83 ImageDecoder PngDecoder::newDecoder() const
85 return new PngDecoder;
88 void PngDecoder::close()
98 png_structp png_ptr = (png_structp)m_png_ptr;
99 png_infop info_ptr = (png_infop)m_info_ptr;
100 png_infop end_info = (png_infop)m_end_info;
101 png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
102 m_png_ptr = m_info_ptr = m_end_info = 0;
107 void PngDecoder::readDataFromBuf( void* _png_ptr, uchar* dst, size_t size )
109 png_structp png_ptr = (png_structp)_png_ptr;
110 PngDecoder* decoder = (PngDecoder*)(png_ptr->io_ptr);
111 CV_Assert( decoder );
112 const Mat& buf = decoder->m_buf;
113 if( decoder->m_buf_pos + size > buf.cols*buf.rows*buf.elemSize() )
115 png_error(png_ptr, "PNG input buffer is incomplete");
118 memcpy( dst, &decoder->m_buf.data[decoder->m_buf_pos], size );
119 decoder->m_buf_pos += size;
122 bool PngDecoder::readHeader()
127 png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
131 png_infop info_ptr = png_create_info_struct( png_ptr );
132 png_infop end_info = png_create_info_struct( png_ptr );
135 m_info_ptr = info_ptr;
136 m_end_info = end_info;
139 if( info_ptr && end_info )
141 if( setjmp( png_ptr->jmpbuf ) == 0 )
144 png_set_read_fn(png_ptr, this, (png_rw_ptr)readDataFromBuf );
147 m_f = fopen( m_filename.c_str(), "rb" );
149 png_init_io( png_ptr, m_f );
152 if( !m_buf.empty() || m_f )
154 png_uint_32 width, height;
155 int bit_depth, color_type;
157 png_read_info( png_ptr, info_ptr );
159 png_get_IHDR( png_ptr, info_ptr, &width, &height,
160 &bit_depth, &color_type, 0, 0, 0 );
162 m_width = (int)width;
163 m_height = (int)height;
164 m_color_type = color_type;
165 m_bit_depth = bit_depth;
167 if( bit_depth <= 8 || bit_depth == 16 )
169 m_type = color_type == PNG_COLOR_TYPE_RGB ||
170 color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
171 color_type == PNG_COLOR_TYPE_PALETTE ? CV_8UC3 : CV_8UC1;
172 if( bit_depth == 16 )
173 m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));
188 bool PngDecoder::readData( Mat& img )
191 AutoBuffer<uchar*> _buffer(m_height);
192 uchar** buffer = _buffer;
193 int color = img.channels() > 1;
194 uchar* data = img.data;
197 if( m_png_ptr && m_info_ptr && m_end_info && m_width && m_height )
199 png_structp png_ptr = (png_structp)m_png_ptr;
200 png_infop info_ptr = (png_infop)m_info_ptr;
201 png_infop end_info = (png_infop)m_end_info;
203 if( setjmp(png_ptr->jmpbuf) == 0 )
207 if( img.depth() == CV_8U && m_bit_depth == 16 )
208 png_set_strip_16( png_ptr );
209 else if( !isBigEndian() )
210 png_set_swap( png_ptr );
212 /* observation: png_read_image() writes 400 bytes beyond
213 * end of data when reading a 400x118 color png
214 * "mpplus_sand.png". OpenCV crashes even with demo
215 * programs. Looking at the loaded image I'd say we get 4
216 * bytes per pixel instead of 3 bytes per pixel. Test
217 * indicate that it is a good idea to always ask for
218 * stripping alpha.. 18.11.2004 Axel Walthelm
220 png_set_strip_alpha( png_ptr );
222 if( m_color_type == PNG_COLOR_TYPE_PALETTE )
223 png_set_palette_to_rgb( png_ptr );
225 if( m_color_type == PNG_COLOR_TYPE_GRAY && m_bit_depth < 8 )
226 png_set_gray_1_2_4_to_8( png_ptr );
228 if( CV_MAT_CN(m_type) > 1 && color )
229 png_set_bgr( png_ptr ); // convert RGB to BGR
231 png_set_gray_to_rgb( png_ptr ); // Gray->RGB
233 png_set_rgb_to_gray( png_ptr, 1, -1, -1 ); // RGB->Gray
235 png_read_update_info( png_ptr, info_ptr );
237 for( y = 0; y < m_height; y++ )
238 buffer[y] = data + y*step;
240 png_read_image( png_ptr, buffer );
241 png_read_end( png_ptr, end_info );
252 /////////////////////// PngEncoder ///////////////////
255 PngEncoder::PngEncoder()
257 m_description = "Portable Network Graphics files (*.png)";
258 m_buf_supported = true;
262 PngEncoder::~PngEncoder()
267 bool PngEncoder::isFormatSupported( int depth ) const
269 return depth == CV_8U || depth == CV_16U;
272 ImageEncoder PngEncoder::newEncoder() const
274 return new PngEncoder;
278 void PngEncoder::writeDataToBuf(void* _png_ptr, uchar* src, size_t size)
282 png_structp png_ptr = (png_structp)_png_ptr;
283 PngEncoder* encoder = (PngEncoder*)(png_ptr->io_ptr);
284 CV_Assert( encoder && encoder->m_buf );
285 size_t cursz = encoder->m_buf->size();
286 encoder->m_buf->resize(cursz + size);
287 memcpy( &(*encoder->m_buf)[cursz], src, size );
291 void PngEncoder::flushBuf(void*)
295 bool PngEncoder::write( const Mat& img, const vector<int>& params )
297 int compression_level = 0;
299 for( size_t i = 0; i < params.size(); i += 2 )
301 if( params[i] == CV_IMWRITE_PNG_COMPRESSION )
303 compression_level = params[i+1];
304 compression_level = MIN(MAX(compression_level, 0), MAX_MEM_LEVEL);
308 png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
309 png_infop info_ptr = 0;
311 int y, width = img.cols, height = img.rows;
312 int depth = img.depth(), channels = img.channels();
314 AutoBuffer<uchar*> buffer;
316 if( depth != CV_8U && depth != CV_16U )
321 info_ptr = png_create_info_struct( png_ptr );
325 if( setjmp( png_ptr->jmpbuf ) == 0 )
329 png_set_write_fn(png_ptr, this,
330 (png_rw_ptr)writeDataToBuf, (png_flush_ptr)flushBuf);
334 f = fopen( m_filename.c_str(), "wb" );
336 png_init_io( png_ptr, f );
341 if( compression_level > 0 )
343 png_set_compression_mem_level( png_ptr, compression_level );
347 // tune parameters for speed
348 // (see http://wiki.linuxquestions.org/wiki/Libpng)
349 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
350 png_set_compression_level(png_ptr, Z_BEST_SPEED);
352 png_set_compression_strategy(png_ptr, Z_HUFFMAN_ONLY);
354 png_set_IHDR( png_ptr, info_ptr, width, height, depth == CV_8U ? 8 : 16,
355 channels == 1 ? PNG_COLOR_TYPE_GRAY :
356 channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
357 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
358 PNG_FILTER_TYPE_DEFAULT );
360 png_write_info( png_ptr, info_ptr );
362 png_set_bgr( png_ptr );
364 png_set_swap( png_ptr );
366 buffer.allocate(height);
367 for( y = 0; y < height; y++ )
368 buffer[y] = img.data + y*img.step;
370 png_write_image( png_ptr, buffer );
371 png_write_end( png_ptr, info_ptr );
379 png_destroy_write_struct( &png_ptr, &info_ptr );