imagecore 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/ext/imagecore/analyze_image.cxx +58 -0
- data/ext/imagecore/analyze_image.h +6 -0
- data/ext/imagecore/extconf.rb +9 -0
- data/ext/imagecore/imagecore.cxx +34 -0
- data/ext/opencv/core/___.c +3 -0
- data/ext/opencv/core/alloc.cpp +697 -0
- data/ext/opencv/core/array.cpp +3206 -0
- data/ext/opencv/core/datastructs.cpp +4064 -0
- data/ext/opencv/core/extconf.rb +22 -0
- data/ext/opencv/core/matrix.cpp +3777 -0
- data/ext/opencv/core/precomp.hpp +216 -0
- data/ext/opencv/core/system.cpp +832 -0
- data/ext/opencv/core/tables.cpp +3512 -0
- data/ext/opencv/highgui/___.c +3 -0
- data/ext/opencv/highgui/bitstrm.cpp +582 -0
- data/ext/opencv/highgui/bitstrm.hpp +182 -0
- data/ext/opencv/highgui/extconf.rb +28 -0
- data/ext/opencv/highgui/grfmt_base.cpp +128 -0
- data/ext/opencv/highgui/grfmt_base.hpp +113 -0
- data/ext/opencv/highgui/grfmt_bmp.cpp +564 -0
- data/ext/opencv/highgui/grfmt_bmp.hpp +99 -0
- data/ext/opencv/highgui/grfmt_exr.hpp +113 -0
- data/ext/opencv/highgui/grfmt_imageio.hpp +56 -0
- data/ext/opencv/highgui/grfmt_jpeg.cpp +622 -0
- data/ext/opencv/highgui/grfmt_jpeg.hpp +90 -0
- data/ext/opencv/highgui/grfmt_jpeg2000.cpp +529 -0
- data/ext/opencv/highgui/grfmt_jpeg2000.hpp +95 -0
- data/ext/opencv/highgui/grfmt_png.cpp +406 -0
- data/ext/opencv/highgui/grfmt_png.hpp +101 -0
- data/ext/opencv/highgui/grfmt_pxm.cpp +513 -0
- data/ext/opencv/highgui/grfmt_pxm.hpp +92 -0
- data/ext/opencv/highgui/grfmt_sunras.cpp +425 -0
- data/ext/opencv/highgui/grfmt_sunras.hpp +105 -0
- data/ext/opencv/highgui/grfmt_tiff.cpp +718 -0
- data/ext/opencv/highgui/grfmt_tiff.hpp +136 -0
- data/ext/opencv/highgui/grfmts.hpp +56 -0
- data/ext/opencv/highgui/loadsave.cpp +535 -0
- data/ext/opencv/highgui/precomp.hpp +223 -0
- data/ext/opencv/highgui/utils.cpp +689 -0
- data/ext/opencv/highgui/utils.hpp +128 -0
- data/ext/opencv/imgproc/___.c +3 -0
- data/ext/opencv/imgproc/_geom.h +72 -0
- data/ext/opencv/imgproc/color.cpp +3179 -0
- data/ext/opencv/imgproc/contours.cpp +1780 -0
- data/ext/opencv/imgproc/extconf.rb +11 -0
- data/ext/opencv/imgproc/filter.cpp +3063 -0
- data/ext/opencv/imgproc/precomp.hpp +159 -0
- data/ext/opencv/imgproc/shapedescr.cpp +1306 -0
- data/ext/opencv/imgproc/smooth.cpp +1566 -0
- data/ext/opencv/imgproc/tables.cpp +214 -0
- data/ext/opencv/imgproc/thresh.cpp +636 -0
- data/ext/opencv/imgproc/utils.cpp +242 -0
- data/ext/opencv/include/opencv2/core/core.hpp +4344 -0
- data/ext/opencv/include/opencv2/core/core_c.h +1885 -0
- data/ext/opencv/include/opencv2/core/internal.hpp +710 -0
- data/ext/opencv/include/opencv2/core/mat.hpp +2557 -0
- data/ext/opencv/include/opencv2/core/operations.hpp +3623 -0
- data/ext/opencv/include/opencv2/core/types_c.h +1875 -0
- data/ext/opencv/include/opencv2/core/version.hpp +58 -0
- data/ext/opencv/include/opencv2/highgui/highgui.hpp +198 -0
- data/ext/opencv/include/opencv2/highgui/highgui_c.h +506 -0
- data/ext/opencv/include/opencv2/imgproc/imgproc.hpp +1139 -0
- data/ext/opencv/include/opencv2/imgproc/imgproc_c.h +783 -0
- data/ext/opencv/include/opencv2/imgproc/types_c.h +538 -0
- data/imagecore.gemspec +20 -0
- data/lib/imagecore.rb +16 -0
- data/lib/imagecore/version.rb +3 -0
- metadata +119 -0
@@ -0,0 +1,95 @@
|
|
1
|
+
/*M///////////////////////////////////////////////////////////////////////////////////////
|
2
|
+
//
|
3
|
+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
4
|
+
//
|
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.
|
8
|
+
//
|
9
|
+
//
|
10
|
+
// License Agreement
|
11
|
+
// For Open Source Computer Vision Library
|
12
|
+
//
|
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.
|
16
|
+
//
|
17
|
+
// Redistribution and use in source and binary forms, with or without modification,
|
18
|
+
// are permitted provided that the following conditions are met:
|
19
|
+
//
|
20
|
+
// * Redistribution's of source code must retain the above copyright notice,
|
21
|
+
// this list of conditions and the following disclaimer.
|
22
|
+
//
|
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.
|
26
|
+
//
|
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.
|
29
|
+
//
|
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.
|
40
|
+
//
|
41
|
+
//M*/
|
42
|
+
|
43
|
+
#ifndef _GRFMT_JASPER_H_
|
44
|
+
#define _GRFMT_JASPER_H_
|
45
|
+
|
46
|
+
#ifdef HAVE_JASPER
|
47
|
+
|
48
|
+
#include "grfmt_base.hpp"
|
49
|
+
|
50
|
+
namespace cv
|
51
|
+
{
|
52
|
+
|
53
|
+
class Jpeg2KDecoder : public BaseImageDecoder
|
54
|
+
{
|
55
|
+
public:
|
56
|
+
|
57
|
+
Jpeg2KDecoder();
|
58
|
+
virtual ~Jpeg2KDecoder();
|
59
|
+
|
60
|
+
bool readData( Mat& img );
|
61
|
+
bool readHeader();
|
62
|
+
void close();
|
63
|
+
ImageDecoder newDecoder() const;
|
64
|
+
|
65
|
+
protected:
|
66
|
+
bool readComponent8u( uchar *data, void *buffer, int step, int cmpt,
|
67
|
+
int maxval, int offset, int ncmpts );
|
68
|
+
bool readComponent16u( unsigned short *data, void *buffer, int step, int cmpt,
|
69
|
+
int maxval, int offset, int ncmpts );
|
70
|
+
|
71
|
+
void *m_stream;
|
72
|
+
void *m_image;
|
73
|
+
};
|
74
|
+
|
75
|
+
|
76
|
+
class Jpeg2KEncoder : public BaseImageEncoder
|
77
|
+
{
|
78
|
+
public:
|
79
|
+
Jpeg2KEncoder();
|
80
|
+
virtual ~Jpeg2KEncoder();
|
81
|
+
|
82
|
+
bool isFormatSupported( int depth ) const;
|
83
|
+
bool write( const Mat& img, const vector<int>& params );
|
84
|
+
ImageEncoder newEncoder() const;
|
85
|
+
|
86
|
+
protected:
|
87
|
+
bool writeComponent8u( void *img, const Mat& _img );
|
88
|
+
bool writeComponent16u( void *img, const Mat& _img );
|
89
|
+
};
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
#endif
|
94
|
+
|
95
|
+
#endif/*_GRFMT_JASPER_H_*/
|
@@ -0,0 +1,406 @@
|
|
1
|
+
/*M///////////////////////////////////////////////////////////////////////////////////////
|
2
|
+
//
|
3
|
+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
4
|
+
//
|
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.
|
8
|
+
//
|
9
|
+
//
|
10
|
+
// License Agreement
|
11
|
+
// For Open Source Computer Vision Library
|
12
|
+
//
|
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.
|
16
|
+
//
|
17
|
+
// Redistribution and use in source and binary forms, with or without modification,
|
18
|
+
// are permitted provided that the following conditions are met:
|
19
|
+
//
|
20
|
+
// * Redistribution's of source code must retain the above copyright notice,
|
21
|
+
// this list of conditions and the following disclaimer.
|
22
|
+
//
|
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.
|
26
|
+
//
|
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.
|
29
|
+
//
|
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.
|
40
|
+
//
|
41
|
+
//M*/
|
42
|
+
|
43
|
+
#include "precomp.hpp"
|
44
|
+
|
45
|
+
#ifdef HAVE_PNG
|
46
|
+
|
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
|
+
\****************************************************************************************/
|
53
|
+
|
54
|
+
#ifdef HAVE_LIBPNG_PNG_H
|
55
|
+
#include <libpng/png.h>
|
56
|
+
#else
|
57
|
+
#include <png.h>
|
58
|
+
#endif
|
59
|
+
#include <zlib.h>
|
60
|
+
#include "grfmt_png.hpp"
|
61
|
+
|
62
|
+
namespace cv
|
63
|
+
{
|
64
|
+
|
65
|
+
/////////////////////// PngDecoder ///////////////////
|
66
|
+
|
67
|
+
PngDecoder::PngDecoder()
|
68
|
+
{
|
69
|
+
m_signature = "\x89\x50\x4e\x47\xd\xa\x1a\xa";
|
70
|
+
m_color_type = 0;
|
71
|
+
m_png_ptr = 0;
|
72
|
+
m_info_ptr = m_end_info = 0;
|
73
|
+
m_f = 0;
|
74
|
+
m_buf_supported = true;
|
75
|
+
m_buf_pos = 0;
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
PngDecoder::~PngDecoder()
|
80
|
+
{
|
81
|
+
close();
|
82
|
+
}
|
83
|
+
|
84
|
+
ImageDecoder PngDecoder::newDecoder() const
|
85
|
+
{
|
86
|
+
return new PngDecoder;
|
87
|
+
}
|
88
|
+
|
89
|
+
void PngDecoder::close()
|
90
|
+
{
|
91
|
+
if( m_f )
|
92
|
+
{
|
93
|
+
fclose( m_f );
|
94
|
+
m_f = 0;
|
95
|
+
}
|
96
|
+
|
97
|
+
if( m_png_ptr )
|
98
|
+
{
|
99
|
+
png_structp png_ptr = (png_structp)m_png_ptr;
|
100
|
+
png_infop info_ptr = (png_infop)m_info_ptr;
|
101
|
+
png_infop end_info = (png_infop)m_end_info;
|
102
|
+
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
103
|
+
m_png_ptr = m_info_ptr = m_end_info = 0;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
void PngDecoder::readDataFromBuf( void* _png_ptr, uchar* dst, size_t size )
|
109
|
+
{
|
110
|
+
png_structp png_ptr = (png_structp)_png_ptr;
|
111
|
+
PngDecoder* decoder = (PngDecoder*)(png_get_io_ptr(png_ptr));
|
112
|
+
CV_Assert( decoder );
|
113
|
+
const Mat& buf = decoder->m_buf;
|
114
|
+
if( decoder->m_buf_pos + size > buf.cols*buf.rows*buf.elemSize() )
|
115
|
+
{
|
116
|
+
png_error(png_ptr, "PNG input buffer is incomplete");
|
117
|
+
return;
|
118
|
+
}
|
119
|
+
memcpy( dst, &decoder->m_buf.data[decoder->m_buf_pos], size );
|
120
|
+
decoder->m_buf_pos += size;
|
121
|
+
}
|
122
|
+
|
123
|
+
bool PngDecoder::readHeader()
|
124
|
+
{
|
125
|
+
bool result = false;
|
126
|
+
close();
|
127
|
+
|
128
|
+
png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
|
129
|
+
|
130
|
+
if( png_ptr )
|
131
|
+
{
|
132
|
+
png_infop info_ptr = png_create_info_struct( png_ptr );
|
133
|
+
png_infop end_info = png_create_info_struct( png_ptr );
|
134
|
+
|
135
|
+
m_png_ptr = png_ptr;
|
136
|
+
m_info_ptr = info_ptr;
|
137
|
+
m_end_info = end_info;
|
138
|
+
m_buf_pos = 0;
|
139
|
+
|
140
|
+
if( info_ptr && end_info )
|
141
|
+
{
|
142
|
+
if( setjmp( png_jmpbuf( png_ptr ) ) == 0 )
|
143
|
+
{
|
144
|
+
if( !m_buf.empty() )
|
145
|
+
png_set_read_fn(png_ptr, this, (png_rw_ptr)readDataFromBuf );
|
146
|
+
else
|
147
|
+
{
|
148
|
+
m_f = fopen( m_filename.c_str(), "rb" );
|
149
|
+
if( m_f )
|
150
|
+
png_init_io( png_ptr, m_f );
|
151
|
+
}
|
152
|
+
|
153
|
+
if( !m_buf.empty() || m_f )
|
154
|
+
{
|
155
|
+
png_uint_32 width, height;
|
156
|
+
int bit_depth, color_type;
|
157
|
+
|
158
|
+
png_read_info( png_ptr, info_ptr );
|
159
|
+
|
160
|
+
png_get_IHDR( png_ptr, info_ptr, &width, &height,
|
161
|
+
&bit_depth, &color_type, 0, 0, 0 );
|
162
|
+
|
163
|
+
m_width = (int)width;
|
164
|
+
m_height = (int)height;
|
165
|
+
m_color_type = color_type;
|
166
|
+
m_bit_depth = bit_depth;
|
167
|
+
|
168
|
+
if( bit_depth <= 8 || bit_depth == 16 )
|
169
|
+
{
|
170
|
+
switch(color_type)
|
171
|
+
{
|
172
|
+
case PNG_COLOR_TYPE_RGB:
|
173
|
+
case PNG_COLOR_TYPE_PALETTE:
|
174
|
+
m_type = CV_8UC3;
|
175
|
+
break;
|
176
|
+
case PNG_COLOR_TYPE_RGB_ALPHA:
|
177
|
+
m_type = CV_8UC4;
|
178
|
+
break;
|
179
|
+
default:
|
180
|
+
m_type = CV_8UC1;
|
181
|
+
}
|
182
|
+
if( bit_depth == 16 )
|
183
|
+
m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));
|
184
|
+
result = true;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
if( !result )
|
192
|
+
close();
|
193
|
+
|
194
|
+
return result;
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
bool PngDecoder::readData( Mat& img )
|
199
|
+
{
|
200
|
+
bool result = false;
|
201
|
+
AutoBuffer<uchar*> _buffer(m_height);
|
202
|
+
uchar** buffer = _buffer;
|
203
|
+
int color = img.channels() > 1;
|
204
|
+
uchar* data = img.data;
|
205
|
+
int step = (int)img.step;
|
206
|
+
|
207
|
+
if( m_png_ptr && m_info_ptr && m_end_info && m_width && m_height )
|
208
|
+
{
|
209
|
+
png_structp png_ptr = (png_structp)m_png_ptr;
|
210
|
+
png_infop info_ptr = (png_infop)m_info_ptr;
|
211
|
+
png_infop end_info = (png_infop)m_end_info;
|
212
|
+
|
213
|
+
if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
|
214
|
+
{
|
215
|
+
int y;
|
216
|
+
|
217
|
+
if( img.depth() == CV_8U && m_bit_depth == 16 )
|
218
|
+
png_set_strip_16( png_ptr );
|
219
|
+
else if( !isBigEndian() )
|
220
|
+
png_set_swap( png_ptr );
|
221
|
+
|
222
|
+
if(img.channels() < 4)
|
223
|
+
{
|
224
|
+
/* observation: png_read_image() writes 400 bytes beyond
|
225
|
+
* end of data when reading a 400x118 color png
|
226
|
+
* "mpplus_sand.png". OpenCV crashes even with demo
|
227
|
+
* programs. Looking at the loaded image I'd say we get 4
|
228
|
+
* bytes per pixel instead of 3 bytes per pixel. Test
|
229
|
+
* indicate that it is a good idea to always ask for
|
230
|
+
* stripping alpha.. 18.11.2004 Axel Walthelm
|
231
|
+
*/
|
232
|
+
png_set_strip_alpha( png_ptr );
|
233
|
+
}
|
234
|
+
|
235
|
+
if( m_color_type == PNG_COLOR_TYPE_PALETTE )
|
236
|
+
png_set_palette_to_rgb( png_ptr );
|
237
|
+
|
238
|
+
if( m_color_type == PNG_COLOR_TYPE_GRAY && m_bit_depth < 8 )
|
239
|
+
#if PNG_LIBPNG_VER_MAJOR*100 + PNG_LIBPNG_VER_MINOR >= 104
|
240
|
+
png_set_expand_gray_1_2_4_to_8( png_ptr );
|
241
|
+
#else
|
242
|
+
png_set_gray_1_2_4_to_8( png_ptr );
|
243
|
+
#endif
|
244
|
+
|
245
|
+
if( CV_MAT_CN(m_type) > 1 && color )
|
246
|
+
png_set_bgr( png_ptr ); // convert RGB to BGR
|
247
|
+
else if( color )
|
248
|
+
png_set_gray_to_rgb( png_ptr ); // Gray->RGB
|
249
|
+
else
|
250
|
+
png_set_rgb_to_gray( png_ptr, 1, 0.299, 0.587 ); // RGB->Gray
|
251
|
+
|
252
|
+
png_read_update_info( png_ptr, info_ptr );
|
253
|
+
|
254
|
+
for( y = 0; y < m_height; y++ )
|
255
|
+
buffer[y] = data + y*step;
|
256
|
+
|
257
|
+
png_read_image( png_ptr, buffer );
|
258
|
+
png_read_end( png_ptr, end_info );
|
259
|
+
|
260
|
+
result = true;
|
261
|
+
}
|
262
|
+
}
|
263
|
+
|
264
|
+
close();
|
265
|
+
return result;
|
266
|
+
}
|
267
|
+
|
268
|
+
|
269
|
+
/////////////////////// PngEncoder ///////////////////
|
270
|
+
|
271
|
+
|
272
|
+
PngEncoder::PngEncoder()
|
273
|
+
{
|
274
|
+
m_description = "Portable Network Graphics files (*.png)";
|
275
|
+
m_buf_supported = true;
|
276
|
+
}
|
277
|
+
|
278
|
+
|
279
|
+
PngEncoder::~PngEncoder()
|
280
|
+
{
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
bool PngEncoder::isFormatSupported( int depth ) const
|
285
|
+
{
|
286
|
+
return depth == CV_8U || depth == CV_16U;
|
287
|
+
}
|
288
|
+
|
289
|
+
ImageEncoder PngEncoder::newEncoder() const
|
290
|
+
{
|
291
|
+
return new PngEncoder;
|
292
|
+
}
|
293
|
+
|
294
|
+
|
295
|
+
void PngEncoder::writeDataToBuf(void* _png_ptr, uchar* src, size_t size)
|
296
|
+
{
|
297
|
+
if( size == 0 )
|
298
|
+
return;
|
299
|
+
png_structp png_ptr = (png_structp)_png_ptr;
|
300
|
+
PngEncoder* encoder = (PngEncoder*)(png_get_io_ptr(png_ptr));
|
301
|
+
CV_Assert( encoder && encoder->m_buf );
|
302
|
+
size_t cursz = encoder->m_buf->size();
|
303
|
+
encoder->m_buf->resize(cursz + size);
|
304
|
+
memcpy( &(*encoder->m_buf)[cursz], src, size );
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
void PngEncoder::flushBuf(void*)
|
309
|
+
{
|
310
|
+
}
|
311
|
+
|
312
|
+
bool PngEncoder::write( const Mat& img, const vector<int>& params )
|
313
|
+
{
|
314
|
+
int compression_level = 0;
|
315
|
+
|
316
|
+
for( size_t i = 0; i < params.size(); i += 2 )
|
317
|
+
{
|
318
|
+
if( params[i] == CV_IMWRITE_PNG_COMPRESSION )
|
319
|
+
{
|
320
|
+
compression_level = params[i+1];
|
321
|
+
compression_level = MIN(MAX(compression_level, 0), MAX_MEM_LEVEL);
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 );
|
326
|
+
png_infop info_ptr = 0;
|
327
|
+
FILE* f = 0;
|
328
|
+
int y, width = img.cols, height = img.rows;
|
329
|
+
int depth = img.depth(), channels = img.channels();
|
330
|
+
bool result = false;
|
331
|
+
AutoBuffer<uchar*> buffer;
|
332
|
+
|
333
|
+
if( depth != CV_8U && depth != CV_16U )
|
334
|
+
return false;
|
335
|
+
|
336
|
+
if( png_ptr )
|
337
|
+
{
|
338
|
+
info_ptr = png_create_info_struct( png_ptr );
|
339
|
+
|
340
|
+
if( info_ptr )
|
341
|
+
{
|
342
|
+
if( setjmp( png_jmpbuf ( png_ptr ) ) == 0 )
|
343
|
+
{
|
344
|
+
if( m_buf )
|
345
|
+
{
|
346
|
+
png_set_write_fn(png_ptr, this,
|
347
|
+
(png_rw_ptr)writeDataToBuf, (png_flush_ptr)flushBuf);
|
348
|
+
}
|
349
|
+
else
|
350
|
+
{
|
351
|
+
f = fopen( m_filename.c_str(), "wb" );
|
352
|
+
if( f )
|
353
|
+
png_init_io( png_ptr, f );
|
354
|
+
}
|
355
|
+
|
356
|
+
if( m_buf || f )
|
357
|
+
{
|
358
|
+
if( compression_level > 0 )
|
359
|
+
{
|
360
|
+
png_set_compression_mem_level( png_ptr, compression_level );
|
361
|
+
}
|
362
|
+
else
|
363
|
+
{
|
364
|
+
// tune parameters for speed
|
365
|
+
// (see http://wiki.linuxquestions.org/wiki/Libpng)
|
366
|
+
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_SUB);
|
367
|
+
png_set_compression_level(png_ptr, Z_BEST_SPEED);
|
368
|
+
}
|
369
|
+
png_set_compression_strategy(png_ptr, Z_HUFFMAN_ONLY);
|
370
|
+
|
371
|
+
png_set_IHDR( png_ptr, info_ptr, width, height, depth == CV_8U ? 8 : 16,
|
372
|
+
channels == 1 ? PNG_COLOR_TYPE_GRAY :
|
373
|
+
channels == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA,
|
374
|
+
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
|
375
|
+
PNG_FILTER_TYPE_DEFAULT );
|
376
|
+
|
377
|
+
png_write_info( png_ptr, info_ptr );
|
378
|
+
|
379
|
+
png_set_bgr( png_ptr );
|
380
|
+
if( !isBigEndian() )
|
381
|
+
png_set_swap( png_ptr );
|
382
|
+
|
383
|
+
buffer.allocate(height);
|
384
|
+
for( y = 0; y < height; y++ )
|
385
|
+
buffer[y] = img.data + y*img.step;
|
386
|
+
|
387
|
+
png_write_image( png_ptr, buffer );
|
388
|
+
png_write_end( png_ptr, info_ptr );
|
389
|
+
|
390
|
+
result = true;
|
391
|
+
}
|
392
|
+
}
|
393
|
+
}
|
394
|
+
}
|
395
|
+
|
396
|
+
png_destroy_write_struct( &png_ptr, &info_ptr );
|
397
|
+
if(f) fclose( f );
|
398
|
+
|
399
|
+
return result;
|
400
|
+
}
|
401
|
+
|
402
|
+
}
|
403
|
+
|
404
|
+
#endif
|
405
|
+
|
406
|
+
/* End of file. */
|