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
58 #include <jasper/jasper.h>
66 struct JasperInitializer
68 JasperInitializer() { jas_init(); }
69 ~JasperInitializer() { jas_cleanup(); }
72 static JasperInitializer initialize_jasper;
75 /////////////////////// Jpeg2KDecoder ///////////////////
77 Jpeg2KDecoder::Jpeg2KDecoder()
79 m_signature = '\0' + String() + '\0' + String() + '\0' + String("\x0cjP \r\n\x87\n");
85 Jpeg2KDecoder::~Jpeg2KDecoder()
89 ImageDecoder Jpeg2KDecoder::newDecoder() const
91 return makePtr<Jpeg2KDecoder>();
94 void Jpeg2KDecoder::close()
98 jas_stream_close( (jas_stream_t*)m_stream );
104 jas_image_destroy( (jas_image_t*)m_image );
110 bool Jpeg2KDecoder::readHeader()
115 jas_stream_t* stream = jas_stream_fopen( m_filename.c_str(), "rb" );
120 jas_image_t* image = jas_image_decode( stream, -1, 0 );
123 m_width = jas_image_width( image );
124 m_height = jas_image_height( image );
126 int cntcmpts = 0; // count the known components
127 int numcmpts = jas_image_numcmpts( image );
129 for( int i = 0; i < numcmpts; i++ )
131 int depth_i = jas_image_cmptprec( image, i );
132 depth = MAX(depth, depth_i);
133 if( jas_image_cmpttype( image, i ) > 2 )
140 m_type = CV_MAKETYPE(depth <= 8 ? CV_8U : CV_16U, cntcmpts > 1 ? 3 : 1);
153 bool Jpeg2KDecoder::readData( Mat& img )
156 int color = img.channels() > 1;
157 uchar* data = img.data;
158 int step = (int)img.step;
159 jas_stream_t* stream = (jas_stream_t*)m_stream;
160 jas_image_t* image = (jas_image_t*)m_image;
162 if( stream && image )
168 convert = (jas_image_clrspc( image ) != JAS_CLRSPC_SRGB);
169 colorspace = JAS_CLRSPC_SRGB;
173 convert = (jas_clrspc_fam( jas_image_clrspc( image ) ) != JAS_CLRSPC_FAM_GRAY);
174 colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY?
177 // convert to the desired colorspace
180 jas_cmprof_t *clrprof = jas_cmprof_createfromclrspc( colorspace );
183 jas_image_t *_img = jas_image_chclrspc( image, clrprof, JAS_CMXFORM_INTENT_RELCLR );
186 jas_image_destroy( image );
187 m_image = image = _img;
191 fprintf(stderr, "JPEG 2000 LOADER ERROR: cannot convert colorspace\n");
192 jas_cmprof_destroy( clrprof );
195 fprintf(stderr, "JPEG 2000 LOADER ERROR: unable to create colorspace\n");
206 cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_B );
207 cmptlut[1] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_G );
208 cmptlut[2] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_R );
209 if( cmptlut[0] < 0 || cmptlut[1] < 0 || cmptlut[2] < 0 )
215 cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_GRAY_Y );
223 for( int i = 0; i < ncmpts; i++ )
225 int maxval = 1 << jas_image_cmptprec( image, cmptlut[i] );
226 int offset = jas_image_cmptsgnd( image, cmptlut[i] ) ? maxval / 2 : 0;
228 int yend = jas_image_cmptbry( image, cmptlut[i] );
229 int ystep = jas_image_cmptvstep( image, cmptlut[i] );
230 int xend = jas_image_cmptbrx( image, cmptlut[i] );
231 int xstep = jas_image_cmpthstep( image, cmptlut[i] );
233 jas_matrix_t *buffer = jas_matrix_create( yend / ystep, xend / xstep );
236 if( !jas_image_readcmpt( image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
238 if( img.depth() == CV_8U )
239 result = readComponent8u( data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts );
241 result = readComponent16u( ((unsigned short *)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts );
248 jas_matrix_destroy( buffer );
254 fprintf(stderr, "JPEG2000 LOADER ERROR: colorspace conversion failed\n" );
263 bool Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
265 int maxval, int offset, int ncmpts )
267 jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
268 jas_image_t* image = (jas_image_t*)m_image;
269 int xstart = jas_image_cmpttlx( image, cmpt );
270 int xend = jas_image_cmptbrx( image, cmpt );
271 int xstep = jas_image_cmpthstep( image, cmpt );
272 int xoffset = jas_image_tlx( image );
273 int ystart = jas_image_cmpttly( image, cmpt );
274 int yend = jas_image_cmptbry( image, cmpt );
275 int ystep = jas_image_cmptvstep( image, cmpt );
276 int yoffset = jas_image_tly( image );
278 int rshift = cvRound(std::log(maxval/256.)/std::log(2.));
279 int lshift = MAX(0, -rshift);
280 rshift = MAX(0, rshift);
281 int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset;
283 for( y = 0; y < yend - ystart; )
285 jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
286 uchar* dst = data + (y - yoffset) * step - xoffset;
290 if( maxval == 256 && offset == 0 )
291 for( x = 0; x < xend - xstart; x++ )
293 int pix = pix_row[x];
294 dst[x*ncmpts] = cv::saturate_cast<uchar>(pix);
297 for( x = 0; x < xend - xstart; x++ )
299 int pix = ((pix_row[x] + delta) >> rshift) << lshift;
300 dst[x*ncmpts] = cv::saturate_cast<uchar>(pix);
303 else if( xstep == 2 && offset == 0 )
304 for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
306 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
307 dst[x*ncmpts] = dst[(x+1)*ncmpts] = cv::saturate_cast<uchar>(pix);
310 for( x = 0, j = 0; x < xend - xstart; j++ )
312 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
313 pix = cv::saturate_cast<uchar>(pix);
314 for( x1 = x + xstep; x < x1; x++ )
315 dst[x*ncmpts] = (uchar)pix;
318 for( ++y; y < y1; y++, dst += step )
319 for( x = 0; x < xend - xstart; x++ )
320 dst[x*ncmpts + step] = dst[x*ncmpts];
327 bool Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
329 int maxval, int offset, int ncmpts )
331 jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
332 jas_image_t* image = (jas_image_t*)m_image;
333 int xstart = jas_image_cmpttlx( image, cmpt );
334 int xend = jas_image_cmptbrx( image, cmpt );
335 int xstep = jas_image_cmpthstep( image, cmpt );
336 int xoffset = jas_image_tlx( image );
337 int ystart = jas_image_cmpttly( image, cmpt );
338 int yend = jas_image_cmptbry( image, cmpt );
339 int ystep = jas_image_cmptvstep( image, cmpt );
340 int yoffset = jas_image_tly( image );
342 int rshift = cvRound(std::log(maxval/65536.)/std::log(2.));
343 int lshift = MAX(0, -rshift);
344 rshift = MAX(0, rshift);
345 int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset;
347 for( y = 0; y < yend - ystart; )
349 jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
350 ushort* dst = data + (y - yoffset) * step - xoffset;
354 if( maxval == 65536 && offset == 0 )
355 for( x = 0; x < xend - xstart; x++ )
357 int pix = pix_row[x];
358 dst[x*ncmpts] = cv::saturate_cast<ushort>(pix);
361 for( x = 0; x < xend - xstart; x++ )
363 int pix = ((pix_row[x] + delta) >> rshift) << lshift;
364 dst[x*ncmpts] = cv::saturate_cast<ushort>(pix);
367 else if( xstep == 2 && offset == 0 )
368 for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
370 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
371 dst[x*ncmpts] = dst[(x+1)*ncmpts] = cv::saturate_cast<ushort>(pix);
374 for( x = 0, j = 0; x < xend - xstart; j++ )
376 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
377 pix = cv::saturate_cast<ushort>(pix);
378 for( x1 = x + xstep; x < x1; x++ )
379 dst[x*ncmpts] = (ushort)pix;
382 for( ++y; y < y1; y++, dst += step )
383 for( x = 0; x < xend - xstart; x++ )
384 dst[x*ncmpts + step] = dst[x*ncmpts];
391 /////////////////////// Jpeg2KEncoder ///////////////////
394 Jpeg2KEncoder::Jpeg2KEncoder()
396 m_description = "JPEG-2000 files (*.jp2)";
400 Jpeg2KEncoder::~Jpeg2KEncoder()
404 ImageEncoder Jpeg2KEncoder::newEncoder() const
406 return makePtr<Jpeg2KEncoder>();
409 bool Jpeg2KEncoder::isFormatSupported( int depth ) const
411 return depth == CV_8U || depth == CV_16U;
415 bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
417 int width = _img.cols, height = _img.rows;
418 int depth = _img.depth(), channels = _img.channels();
419 depth = depth == CV_8U ? 8 : 16;
421 if( channels > 3 || channels < 1 )
424 jas_image_cmptparm_t component_info[3];
425 for( int i = 0; i < channels; i++ )
427 component_info[i].tlx = 0;
428 component_info[i].tly = 0;
429 component_info[i].hstep = 1;
430 component_info[i].vstep = 1;
431 component_info[i].width = width;
432 component_info[i].height = height;
433 component_info[i].prec = depth;
434 component_info[i].sgnd = 0;
436 jas_image_t *img = jas_image_create( channels, component_info, (channels == 1) ? JAS_CLRSPC_SGRAY : JAS_CLRSPC_SRGB );
441 jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_GRAY_Y );
444 jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_RGB_B );
445 jas_image_setcmpttype( img, 1, JAS_IMAGE_CT_RGB_G );
446 jas_image_setcmpttype( img, 2, JAS_IMAGE_CT_RGB_R );
451 result = writeComponent8u( img, _img );
453 result = writeComponent16u( img, _img );
456 jas_stream_t *stream = jas_stream_fopen( m_filename.c_str(), "wb" );
459 result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)"" );
461 jas_stream_close( stream );
465 jas_image_destroy( img );
471 bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
473 jas_image_t* img = (jas_image_t*)__img;
474 int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
475 jas_matrix_t *row = jas_matrix_create( 1, w );
479 for( int y = 0; y < h; y++ )
481 uchar* data = _img.data + _img.step*y;
482 for( int i = 0; i < ncmpts; i++ )
484 for( int x = 0; x < w; x++)
485 jas_matrix_setv( row, x, data[x * ncmpts + i] );
486 jas_image_writecmpt( img, i, 0, y, w, 1, row );
490 jas_matrix_destroy( row );
495 bool Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
497 jas_image_t* img = (jas_image_t*)__img;
498 int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
499 jas_matrix_t *row = jas_matrix_create( 1, w );
503 for( int y = 0; y < h; y++ )
505 uchar* data = _img.data + _img.step*y;
506 for( int i = 0; i < ncmpts; i++ )
508 for( int x = 0; x < w; x++)
509 jas_matrix_setv( row, x, data[x * ncmpts + i] );
510 jas_image_writecmpt( img, i, 0, y, w, 1, row );
514 jas_matrix_destroy( row );