2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
|
|
#ifdef HAVE_PNG
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
This part of the file implements PNG codec on base of libpng library,
|
|
|
|
in particular, this code is based on example.c from libpng
|
|
|
|
(see otherlibs/_graphics/readme.txt for copyright notice)
|
|
|
|
and png2bmp sample from libpng distribution (Copyright (C) 1999-2001 MIYASAKA Masaru)
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2012-06-21 18:28:57 +08:00
|
|
|
#ifndef _LFS64_LARGEFILE
|
|
|
|
# define _LFS64_LARGEFILE 0
|
|
|
|
#endif
|
|
|
|
#ifndef _FILE_OFFSET_BITS
|
|
|
|
# define _FILE_OFFSET_BITS 0
|
|
|
|
#endif
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
#ifdef HAVE_LIBPNG_PNG_H
|
|
|
|
#include <libpng/png.h>
|
|
|
|
#else
|
|
|
|
#include <png.h>
|
|
|
|
#endif
|
2011-07-22 03:50:20 +08:00
|
|
|
#include <zlib.h>
|
2012-06-21 18:28:57 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
#include "grfmt_png.hpp"
|
|
|
|
|
2012-04-14 05:50:59 +08:00
|
|
|
#if defined _MSC_VER && _MSC_VER >= 1200
|
2012-06-12 22:46:12 +08:00
|
|
|
// interaction between '_setjmp' and C++ object destruction is non-portable
|
2012-04-14 05:50:59 +08:00
|
|
|
#pragma warning( disable: 4611 )
|
|
|
|
#endif
|
|
|
|
|
2013-04-18 02:01:03 +08:00
|
|
|
// the following defines are a hack to avoid multiple problems with frame ponter handling and setjmp
|
|
|
|
// see http://gcc.gnu.org/ml/gcc/2011-10/msg00324.html for some details
|
|
|
|
#define mingw_getsp(...) 0
|
|
|
|
#define __builtin_frame_address(...) 0
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
|
|
|
/////////////////////// PngDecoder ///////////////////
|
|
|
|
|
|
|
|
PngDecoder::PngDecoder()
|
|
|
|
{
|
|
|
|
m_signature = "\x89\x50\x4e\x47\xd\xa\x1a\xa";
|
|
|
|
m_color_type = 0;
|
|
|
|
m_png_ptr = 0;
|
|
|
|
m_info_ptr = m_end_info = 0;
|
|
|
|
m_f = 0;
|
|
|
|
m_buf_supported = true;
|
|
|
|
m_buf_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PngDecoder::~PngDecoder()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageDecoder PngDecoder::newDecoder() const
|
|
|
|
{
|
2013-08-13 20:47:18 +08:00
|
|
|
return makePtr<PngDecoder>();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PngDecoder::close()
|
|
|
|
{
|
|
|
|
if( m_f )
|
|
|
|
{
|
|
|
|
fclose( m_f );
|
|
|
|
m_f = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_png_ptr )
|
|
|
|
{
|
|
|
|
png_structp png_ptr = (png_structp)m_png_ptr;
|
|
|
|
png_infop info_ptr = (png_infop)m_info_ptr;
|
|
|
|
png_infop end_info = (png_infop)m_end_info;
|
|
|
|
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
|
|
|
m_png_ptr = m_info_ptr = m_end_info = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PngDecoder::readDataFromBuf( void* _png_ptr, uchar* dst, size_t size )
|
|
|
|
{
|
|
|
|
png_structp png_ptr = (png_structp)_png_ptr;
|
2011-04-26 20:41:42 +08:00
|
|
|
PngDecoder* decoder = (PngDecoder*)(png_get_io_ptr(png_ptr));
|
2010-05-12 01:44:00 +08:00
|
|
|
CV_Assert( decoder );
|
|
|
|
const Mat& buf = decoder->m_buf;
|
|
|
|
if( decoder->m_buf_pos + size > buf.cols*buf.rows*buf.elemSize() )
|
|
|
|
{
|
|
|
|
png_error(png_ptr, "PNG input buffer is incomplete");
|
|
|
|
return;
|
|
|
|
}
|
2014-08-13 19:08:27 +08:00
|
|
|
memcpy( dst, decoder->m_buf.ptr() + decoder->m_buf_pos, size );
|
2010-05-12 01:44:00 +08:00
|
|
|
decoder->m_buf_pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PngDecoder::readHeader()
|
|
|
|
{
|
2015-09-01 05:59:08 +08:00
|
|
|
volatile bool result = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
close();
|
|
|
|
|
|
|
|
png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
|
|
|
|
|
|
|
|
if( png_ptr )
|
|
|
|
{
|
|
|
|
png_infop info_ptr = png_create_info_struct( png_ptr );
|
|
|
|
png_infop end_info = png_create_info_struct( png_ptr );
|
|
|
|
|
|
|
|
m_png_ptr = png_ptr;
|
|
|
|
m_info_ptr = info_ptr;
|
|
|
|
m_end_info = end_info;
|
|
|
|
m_buf_pos = 0;
|
|
|
|
|
|
|
|
if( info_ptr && end_info )
|
|
|
|
{
|
2011-04-26 20:41:42 +08:00
|
|
|
if( setjmp( png_jmpbuf( png_ptr ) ) == 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if( !m_buf.empty() )
|
|
|
|
png_set_read_fn(png_ptr, this, (png_rw_ptr)readDataFromBuf );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_f = fopen( m_filename.c_str(), "rb" );
|
|
|
|
if( m_f )
|
|
|
|
png_init_io( png_ptr, m_f );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_buf.empty() || m_f )
|
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
png_uint_32 wdth, hght;
|
2013-08-20 01:05:37 +08:00
|
|
|
int bit_depth, color_type, num_trans=0;
|
|
|
|
png_bytep trans;
|
|
|
|
png_color_16p trans_values;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
png_read_info( png_ptr, info_ptr );
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
png_get_IHDR( png_ptr, info_ptr, &wdth, &hght,
|
2010-05-12 01:44:00 +08:00
|
|
|
&bit_depth, &color_type, 0, 0, 0 );
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
m_width = (int)wdth;
|
|
|
|
m_height = (int)hght;
|
2010-05-12 01:44:00 +08:00
|
|
|
m_color_type = color_type;
|
|
|
|
m_bit_depth = bit_depth;
|
|
|
|
|
|
|
|
if( bit_depth <= 8 || bit_depth == 16 )
|
|
|
|
{
|
2016-01-16 00:35:10 +08:00
|
|
|
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values);
|
2012-06-09 23:00:04 +08:00
|
|
|
switch(color_type)
|
2011-08-12 06:38:09 +08:00
|
|
|
{
|
2013-08-20 01:05:37 +08:00
|
|
|
case PNG_COLOR_TYPE_RGB:
|
|
|
|
case PNG_COLOR_TYPE_PALETTE:
|
2016-01-16 00:35:10 +08:00
|
|
|
if( num_trans > 0 )
|
2013-08-20 01:05:37 +08:00
|
|
|
m_type = CV_8UC4;
|
|
|
|
else
|
|
|
|
m_type = CV_8UC3;
|
|
|
|
break;
|
|
|
|
case PNG_COLOR_TYPE_RGB_ALPHA:
|
|
|
|
m_type = CV_8UC4;
|
|
|
|
break;
|
2016-01-16 00:35:10 +08:00
|
|
|
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
|
|
|
m_type = CV_8UC2;
|
|
|
|
break;
|
2013-08-20 01:05:37 +08:00
|
|
|
default:
|
2016-01-16 00:35:10 +08:00
|
|
|
if( num_trans > 0 )
|
|
|
|
m_type = CV_8UC2;
|
|
|
|
else
|
|
|
|
m_type = CV_8UC1;
|
2011-08-12 06:38:09 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
if( bit_depth == 16 )
|
|
|
|
m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !result )
|
|
|
|
close();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PngDecoder::readData( Mat& img )
|
|
|
|
{
|
2015-07-22 21:12:31 +08:00
|
|
|
volatile bool result = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
AutoBuffer<uchar*> _buffer(m_height);
|
|
|
|
uchar** buffer = _buffer;
|
2016-01-16 00:35:10 +08:00
|
|
|
int color = img.channels() > 2;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( m_png_ptr && m_info_ptr && m_end_info && m_width && m_height )
|
|
|
|
{
|
|
|
|
png_structp png_ptr = (png_structp)m_png_ptr;
|
|
|
|
png_infop info_ptr = (png_infop)m_info_ptr;
|
|
|
|
png_infop end_info = (png_infop)m_end_info;
|
|
|
|
|
2011-04-26 20:41:42 +08:00
|
|
|
if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int y;
|
|
|
|
|
|
|
|
if( img.depth() == CV_8U && m_bit_depth == 16 )
|
|
|
|
png_set_strip_16( png_ptr );
|
|
|
|
else if( !isBigEndian() )
|
|
|
|
png_set_swap( png_ptr );
|
|
|
|
|
2016-01-16 00:35:10 +08:00
|
|
|
if(img.channels() != 4 && img.channels() != 2)
|
2011-08-12 06:38:09 +08:00
|
|
|
{
|
|
|
|
/* observation: png_read_image() writes 400 bytes beyond
|
|
|
|
* end of data when reading a 400x118 color png
|
|
|
|
* "mpplus_sand.png". OpenCV crashes even with demo
|
|
|
|
* programs. Looking at the loaded image I'd say we get 4
|
|
|
|
* bytes per pixel instead of 3 bytes per pixel. Test
|
|
|
|
* indicate that it is a good idea to always ask for
|
|
|
|
* stripping alpha.. 18.11.2004 Axel Walthelm
|
|
|
|
*/
|
|
|
|
png_set_strip_alpha( png_ptr );
|
2016-01-16 00:35:10 +08:00
|
|
|
} else
|
|
|
|
png_set_tRNS_to_alpha( png_ptr );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( m_color_type == PNG_COLOR_TYPE_PALETTE )
|
|
|
|
png_set_palette_to_rgb( png_ptr );
|
|
|
|
|
2016-01-19 00:56:26 +08:00
|
|
|
if( (m_color_type & PNG_COLOR_MASK_COLOR) == 0 && m_bit_depth < 8 )
|
2012-04-30 22:33:52 +08:00
|
|
|
#if (PNG_LIBPNG_VER_MAJOR*10000 + PNG_LIBPNG_VER_MINOR*100 + PNG_LIBPNG_VER_RELEASE >= 10209) || \
|
|
|
|
(PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR == 0 && PNG_LIBPNG_VER_RELEASE >= 18)
|
2010-07-16 20:54:53 +08:00
|
|
|
png_set_expand_gray_1_2_4_to_8( png_ptr );
|
2010-12-12 02:05:50 +08:00
|
|
|
#else
|
|
|
|
png_set_gray_1_2_4_to_8( png_ptr );
|
|
|
|
#endif
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2016-01-16 00:35:10 +08:00
|
|
|
if( CV_MAT_CN(m_type) > 2 && color )
|
2010-05-12 01:44:00 +08:00
|
|
|
png_set_bgr( png_ptr ); // convert RGB to BGR
|
|
|
|
else if( color )
|
|
|
|
png_set_gray_to_rgb( png_ptr ); // Gray->RGB
|
|
|
|
else
|
2010-11-25 03:50:52 +08:00
|
|
|
png_set_rgb_to_gray( png_ptr, 1, 0.299, 0.587 ); // RGB->Gray
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-08-30 02:32:50 +08:00
|
|
|
png_set_interlace_handling( png_ptr );
|
2010-05-12 01:44:00 +08:00
|
|
|
png_read_update_info( png_ptr, info_ptr );
|
|
|
|
|
|
|
|
for( y = 0; y < m_height; y++ )
|
2016-04-19 19:27:42 +08:00
|
|
|
buffer[y] = img.data + y*img.step;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
png_read_image( png_ptr, buffer );
|
|
|
|
png_read_end( png_ptr, end_info );
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////// PngEncoder ///////////////////
|
|
|
|
|
|
|
|
|
|
|
|
PngEncoder::PngEncoder()
|
|
|
|
{
|
|
|
|
m_description = "Portable Network Graphics files (*.png)";
|
|
|
|
m_buf_supported = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PngEncoder::~PngEncoder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PngEncoder::isFormatSupported( int depth ) const
|
|
|
|
{
|
|
|
|
return depth == CV_8U || depth == CV_16U;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageEncoder PngEncoder::newEncoder() const
|
|
|
|
{
|
2013-08-13 20:47:18 +08:00
|
|
|
return makePtr<PngEncoder>();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PngEncoder::writeDataToBuf(void* _png_ptr, uchar* src, size_t size)
|
|
|
|
{
|
|
|
|
if( size == 0 )
|
|
|
|
return;
|
|
|
|
png_structp png_ptr = (png_structp)_png_ptr;
|
2011-04-26 20:41:42 +08:00
|
|
|
PngEncoder* encoder = (PngEncoder*)(png_get_io_ptr(png_ptr));
|
2010-05-12 01:44:00 +08:00
|
|
|
CV_Assert( encoder && encoder->m_buf );
|
|
|
|
size_t cursz = encoder->m_buf->size();
|
|
|
|
encoder->m_buf->resize(cursz + size);
|
|
|
|
memcpy( &(*encoder->m_buf)[cursz], src, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PngEncoder::flushBuf(void*)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
bool PngEncoder::write( const Mat& img, const std::vector<int>& params )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
|
|
|
|
png_infop info_ptr = 0;
|
2015-07-22 21:12:31 +08:00
|
|
|
FILE * volatile f = 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
int y, width = img.cols, height = img.rows;
|
|
|
|
int depth = img.depth(), channels = img.channels();
|
2015-07-22 21:12:31 +08:00
|
|
|
volatile bool result = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
AutoBuffer<uchar*> buffer;
|
|
|
|
|
|
|
|
if( depth != CV_8U && depth != CV_16U )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( png_ptr )
|
|
|
|
{
|
|
|
|
info_ptr = png_create_info_struct( png_ptr );
|
|
|
|
|
|
|
|
if( info_ptr )
|
|
|
|
{
|
2011-04-26 20:41:42 +08:00
|
|
|
if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if( m_buf )
|
|
|
|
{
|
|
|
|
png_set_write_fn(png_ptr, this,
|
|
|
|
(png_rw_ptr)writeDataToBuf, (png_flush_ptr)flushBuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f = fopen( m_filename.c_str(), "wb" );
|
|
|
|
if( f )
|
2015-10-13 21:52:42 +08:00
|
|
|
png_init_io( png_ptr, (png_FILE_p)f );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 18:05:25 +08:00
|
|
|
int compression_level = -1; // Invalid value to allow setting 0-9 as valid
|
2016-06-26 21:39:19 +08:00
|
|
|
int compression_strategy = IMWRITE_PNG_STRATEGY_RLE; // Default strategy
|
2012-10-17 15:12:04 +08:00
|
|
|
bool isBilevel = false;
|
2012-06-20 20:27:02 +08:00
|
|
|
|
|
|
|
for( size_t i = 0; i < params.size(); i += 2 )
|
|
|
|
{
|
2016-06-26 21:39:19 +08:00
|
|
|
if( params[i] == IMWRITE_PNG_COMPRESSION )
|
2012-06-20 20:27:02 +08:00
|
|
|
{
|
2016-06-26 21:39:19 +08:00
|
|
|
compression_strategy = IMWRITE_PNG_STRATEGY_DEFAULT; // Default strategy
|
2012-06-20 20:27:02 +08:00
|
|
|
compression_level = params[i+1];
|
2012-09-11 18:05:25 +08:00
|
|
|
compression_level = MIN(MAX(compression_level, 0), Z_BEST_COMPRESSION);
|
2012-06-20 20:27:02 +08:00
|
|
|
}
|
2016-06-26 21:39:19 +08:00
|
|
|
if( params[i] == IMWRITE_PNG_STRATEGY )
|
2012-06-20 20:27:02 +08:00
|
|
|
{
|
|
|
|
compression_strategy = params[i+1];
|
|
|
|
compression_strategy = MIN(MAX(compression_strategy, 0), Z_FIXED);
|
|
|
|
}
|
2016-06-26 21:39:19 +08:00
|
|
|
if( params[i] == IMWRITE_PNG_BILEVEL )
|
2012-09-11 18:05:25 +08:00
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
isBilevel = params[i+1] != 0;
|
2012-09-11 18:05:25 +08:00
|
|
|
}
|
2012-06-20 20:27:02 +08:00
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( m_buf || f )
|
|
|
|
{
|
2012-09-11 18:05:25 +08:00
|
|
|
if( compression_level >= 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-09-11 18:05:25 +08:00
|
|
|
png_set_compression_level( png_ptr, compression_level );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// tune parameters for speed
|
|
|
|
// (see http://wiki.linuxquestions.org/wiki/Libpng)
|
|
|
|
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
|
|
|
|
png_set_compression_level(png_ptr, Z_BEST_SPEED);
|
|
|
|
}
|
2011-10-28 12:11:02 +08:00
|
|
|
png_set_compression_strategy(png_ptr, compression_strategy);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-09-11 18:05:25 +08:00
|
|
|
png_set_IHDR( png_ptr, info_ptr, width, height, depth == CV_8U ? isBilevel?1:8 : 16,
|
2010-05-12 01:44:00 +08:00
|
|
|
channels == 1 ? PNG_COLOR_TYPE_GRAY :
|
2016-01-16 00:35:10 +08:00
|
|
|
channels == 2 ? PNG_COLOR_TYPE_GRAY_ALPHA :
|
2010-05-12 01:44:00 +08:00
|
|
|
channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
|
|
|
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
|
|
|
PNG_FILTER_TYPE_DEFAULT );
|
|
|
|
|
|
|
|
png_write_info( png_ptr, info_ptr );
|
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
if (isBilevel)
|
|
|
|
png_set_packing(png_ptr);
|
2012-09-11 18:05:25 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
png_set_bgr( png_ptr );
|
|
|
|
if( !isBigEndian() )
|
|
|
|
png_set_swap( png_ptr );
|
|
|
|
|
|
|
|
buffer.allocate(height);
|
|
|
|
for( y = 0; y < height; y++ )
|
|
|
|
buffer[y] = img.data + y*img.step;
|
|
|
|
|
|
|
|
png_write_image( png_ptr, buffer );
|
|
|
|
png_write_end( png_ptr, info_ptr );
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
png_destroy_write_struct( &png_ptr, &info_ptr );
|
2015-10-13 21:52:42 +08:00
|
|
|
if(f) fclose( (FILE*)f );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* End of file. */
|