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.
43 #include "precomp.hpp"
47 #include "grfmt_jpeg2000.hpp"
50 #define JAS_WIN_MSVC_BUILD 1
52 #define HAVE_STDINT_H 1
57 #undef PACKAGE_BUGREPORT
60 #undef PACKAGE_TARNAME
61 #undef PACKAGE_VERSION
64 #include <jasper/jasper.h>
72 struct JasperInitializer
74 JasperInitializer() { jas_init(); }
75 ~JasperInitializer() { jas_cleanup(); }
78 static JasperInitializer initialize_jasper;
81 /////////////////////// Jpeg2KDecoder ///////////////////
83 Jpeg2KDecoder::Jpeg2KDecoder()
85 m_signature = '\0' + string() + '\0' + string() + '\0' + string("\x0cjP \r\n\x87\n");
91 Jpeg2KDecoder::~Jpeg2KDecoder()
95 ImageDecoder Jpeg2KDecoder::newDecoder() const
97 return new Jpeg2KDecoder;
100 void Jpeg2KDecoder::close()
104 jas_stream_close( (jas_stream_t*)m_stream );
110 jas_image_destroy( (jas_image_t*)m_image );
116 bool Jpeg2KDecoder::readHeader()
121 jas_stream_t* stream = jas_stream_fopen( m_filename.c_str(), "rb" );
126 jas_image_t* image = jas_image_decode( stream, -1, 0 );
129 m_width = jas_image_width( image );
130 m_height = jas_image_height( image );
132 int cntcmpts = 0; // count the known components
133 int numcmpts = jas_image_numcmpts( image );
135 for( int i = 0; i < numcmpts; i++ )
137 int depth_i = jas_image_cmptprec( image, i );
138 depth = MAX(depth, depth_i);
139 if( jas_image_cmpttype( image, i ) > 2 )
146 m_type = CV_MAKETYPE(depth <= 8 ? CV_8U : CV_16U, cntcmpts > 1 ? 3 : 1);
159 bool Jpeg2KDecoder::readData( Mat& img )
162 int color = img.channels() > 1;
163 uchar* data = img.data;
164 int step = (int)img.step;
165 jas_stream_t* stream = (jas_stream_t*)m_stream;
166 jas_image_t* image = (jas_image_t*)m_image;
168 if( stream && image )
174 convert = (jas_image_clrspc( image ) != JAS_CLRSPC_SRGB);
175 colorspace = JAS_CLRSPC_SRGB;
179 convert = (jas_clrspc_fam( jas_image_clrspc( image ) ) != JAS_CLRSPC_FAM_GRAY);
180 colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY?
183 // convert to the desired colorspace
186 jas_cmprof_t *clrprof = jas_cmprof_createfromclrspc( colorspace );
189 jas_image_t *_img = jas_image_chclrspc( image, clrprof, JAS_CMXFORM_INTENT_RELCLR );
192 jas_image_destroy( image );
193 m_image = image = _img;
197 fprintf(stderr, "JPEG 2000 LOADER ERROR: cannot convert colorspace\n");
198 jas_cmprof_destroy( clrprof );
201 fprintf(stderr, "JPEG 2000 LOADER ERROR: unable to create colorspace\n");
212 cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_B );
213 cmptlut[1] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_G );
214 cmptlut[2] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_R );
215 if( cmptlut[0] < 0 || cmptlut[1] < 0 || cmptlut[2] < 0 )
221 cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_GRAY_Y );
229 for( int i = 0; i < ncmpts; i++ )
231 int maxval = 1 << jas_image_cmptprec( image, cmptlut[i] );
232 int offset = jas_image_cmptsgnd( image, cmptlut[i] ) ? maxval / 2 : 0;
234 int yend = jas_image_cmptbry( image, cmptlut[i] );
235 int ystep = jas_image_cmptvstep( image, cmptlut[i] );
236 int xend = jas_image_cmptbrx( image, cmptlut[i] );
237 int xstep = jas_image_cmpthstep( image, cmptlut[i] );
239 jas_matrix_t *buffer = jas_matrix_create( yend / ystep, xend / xstep );
242 if( !jas_image_readcmpt( image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
244 if( img.depth() == CV_8U )
245 result = readComponent8u( data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts );
247 result = readComponent16u( ((unsigned short *)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts );
254 jas_matrix_destroy( buffer );
260 fprintf(stderr, "JPEG2000 LOADER ERROR: colorspace conversion failed\n" );
269 bool Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
271 int maxval, int offset, int ncmpts )
273 jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
274 jas_image_t* image = (jas_image_t*)m_image;
275 int xstart = jas_image_cmpttlx( image, cmpt );
276 int xend = jas_image_cmptbrx( image, cmpt );
277 int xstep = jas_image_cmpthstep( image, cmpt );
278 int xoffset = jas_image_tlx( image );
279 int ystart = jas_image_cmpttly( image, cmpt );
280 int yend = jas_image_cmptbry( image, cmpt );
281 int ystep = jas_image_cmptvstep( image, cmpt );
282 int yoffset = jas_image_tly( image );
284 int rshift = cvRound(std::log(maxval/256.)/std::log(2.));
285 int lshift = MAX(0, -rshift);
286 rshift = MAX(0, rshift);
287 int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset;
289 for( y = 0; y < yend - ystart; )
291 jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
292 uchar* dst = data + (y - yoffset) * step - xoffset;
296 if( maxval == 256 && offset == 0 )
297 for( x = 0; x < xend - xstart; x++ )
299 int pix = pix_row[x];
300 dst[x*ncmpts] = CV_CAST_8U(pix);
303 for( x = 0; x < xend - xstart; x++ )
305 int pix = ((pix_row[x] + delta) >> rshift) << lshift;
306 dst[x*ncmpts] = CV_CAST_8U(pix);
309 else if( xstep == 2 && offset == 0 )
310 for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
312 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
313 dst[x*ncmpts] = dst[(x+1)*ncmpts] = CV_CAST_8U(pix);
316 for( x = 0, j = 0; x < xend - xstart; j++ )
318 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
319 pix = CV_CAST_8U(pix);
320 for( x1 = x + xstep; x < x1; x++ )
321 dst[x*ncmpts] = (uchar)pix;
324 for( ++y; y < y1; y++, dst += step )
325 for( x = 0; x < xend - xstart; x++ )
326 dst[x*ncmpts + step] = dst[x*ncmpts];
333 bool Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
335 int maxval, int offset, int ncmpts )
337 jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
338 jas_image_t* image = (jas_image_t*)m_image;
339 int xstart = jas_image_cmpttlx( image, cmpt );
340 int xend = jas_image_cmptbrx( image, cmpt );
341 int xstep = jas_image_cmpthstep( image, cmpt );
342 int xoffset = jas_image_tlx( image );
343 int ystart = jas_image_cmpttly( image, cmpt );
344 int yend = jas_image_cmptbry( image, cmpt );
345 int ystep = jas_image_cmptvstep( image, cmpt );
346 int yoffset = jas_image_tly( image );
348 int rshift = cvRound(std::log(maxval/65536.)/std::log(2.));
349 int lshift = MAX(0, -rshift);
350 rshift = MAX(0, rshift);
351 int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset;
353 for( y = 0; y < yend - ystart; )
355 jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
356 ushort* dst = data + (y - yoffset) * step - xoffset;
360 if( maxval == 65536 && offset == 0 )
361 for( x = 0; x < xend - xstart; x++ )
363 int pix = pix_row[x];
364 dst[x*ncmpts] = CV_CAST_16U(pix);
367 for( x = 0; x < xend - xstart; x++ )
369 int pix = ((pix_row[x] + delta) >> rshift) << lshift;
370 dst[x*ncmpts] = CV_CAST_16U(pix);
373 else if( xstep == 2 && offset == 0 )
374 for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
376 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
377 dst[x*ncmpts] = dst[(x+1)*ncmpts] = CV_CAST_16U(pix);
380 for( x = 0, j = 0; x < xend - xstart; j++ )
382 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
383 pix = CV_CAST_16U(pix);
384 for( x1 = x + xstep; x < x1; x++ )
385 dst[x*ncmpts] = (ushort)pix;
388 for( ++y; y < y1; y++, dst += step )
389 for( x = 0; x < xend - xstart; x++ )
390 dst[x*ncmpts + step] = dst[x*ncmpts];
397 /////////////////////// Jpeg2KEncoder ///////////////////
400 Jpeg2KEncoder::Jpeg2KEncoder()
402 m_description = "JPEG-2000 files (*.jp2)";
406 Jpeg2KEncoder::~Jpeg2KEncoder()
410 ImageEncoder Jpeg2KEncoder::newEncoder() const
412 return new Jpeg2KEncoder;
415 bool Jpeg2KEncoder::isFormatSupported( int depth ) const
417 return depth == CV_8U || depth == CV_16U;
421 bool Jpeg2KEncoder::write( const Mat& _img, const vector<int>& )
423 int width = _img.cols, height = _img.rows;
424 int depth = _img.depth(), channels = _img.channels();
425 depth = depth == CV_8U ? 8 : 16;
427 if( channels > 3 || channels < 1 )
430 jas_image_cmptparm_t component_info[3];
431 for( int i = 0; i < channels; i++ )
433 component_info[i].tlx = 0;
434 component_info[i].tly = 0;
435 component_info[i].hstep = 1;
436 component_info[i].vstep = 1;
437 component_info[i].width = width;
438 component_info[i].height = height;
439 component_info[i].prec = depth;
440 component_info[i].sgnd = 0;
442 jas_image_t *img = jas_image_create( channels, component_info, (channels == 1) ? JAS_CLRSPC_SGRAY : JAS_CLRSPC_SRGB );
447 jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_GRAY_Y );
450 jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_RGB_B );
451 jas_image_setcmpttype( img, 1, JAS_IMAGE_CT_RGB_G );
452 jas_image_setcmpttype( img, 2, JAS_IMAGE_CT_RGB_R );
457 result = writeComponent8u( img, _img );
459 result = writeComponent16u( img, _img );
462 jas_stream_t *stream = jas_stream_fopen( m_filename.c_str(), "wb" );
465 result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)"" );
467 jas_stream_close( stream );
471 jas_image_destroy( img );
477 bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
479 jas_image_t* img = (jas_image_t*)__img;
480 int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
481 jas_matrix_t *row = jas_matrix_create( 1, w );
485 for( int y = 0; y < h; y++ )
487 uchar* data = _img.data + _img.step*y;
488 for( int i = 0; i < ncmpts; i++ )
490 for( int x = 0; x < w; x++)
491 jas_matrix_setv( row, x, data[x * ncmpts + i] );
492 jas_image_writecmpt( img, i, 0, y, w, 1, row );
496 jas_matrix_destroy( row );
501 bool Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
503 jas_image_t* img = (jas_image_t*)__img;
504 int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
505 jas_matrix_t *row = jas_matrix_create( 1, w );
509 for( int y = 0; y < h; y++ )
511 uchar* data = _img.data + _img.step*y;
512 for( int i = 0; i < ncmpts; i++ )
514 for( int x = 0; x < w; x++)
515 jas_matrix_setv( row, x, data[x * ncmpts + i] );
516 jas_image_writecmpt( img, i, 0, y, w, 1, row );
520 jas_matrix_destroy( row );