Merge pull request #2851 from ilya-lavrenov:tapi_set_identity
[profile/ivi/opencv.git] / modules / imgcodecs / src / grfmt_jpeg2000.cpp
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_JASPER
46
47 #include "grfmt_jpeg2000.hpp"
48
49 #ifdef WIN32
50 #define JAS_WIN_MSVC_BUILD 1
51 #ifdef __GNUC__
52 #define HAVE_STDINT_H 1
53 #endif
54 #endif
55
56 #undef VERSION
57
58 #include <jasper/jasper.h>
59 // FIXME bad hack
60 #undef uchar
61 #undef ulong
62
63 namespace cv
64 {
65
66 struct JasperInitializer
67 {
68     JasperInitializer() { jas_init(); }
69     ~JasperInitializer() { jas_cleanup(); }
70 };
71
72 static JasperInitializer initialize_jasper;
73
74
75 /////////////////////// Jpeg2KDecoder ///////////////////
76
77 Jpeg2KDecoder::Jpeg2KDecoder()
78 {
79     m_signature = '\0' + String() + '\0' + String() + '\0' + String("\x0cjP  \r\n\x87\n");
80     m_stream = 0;
81     m_image = 0;
82 }
83
84
85 Jpeg2KDecoder::~Jpeg2KDecoder()
86 {
87 }
88
89 ImageDecoder Jpeg2KDecoder::newDecoder() const
90 {
91     return makePtr<Jpeg2KDecoder>();
92 }
93
94 void  Jpeg2KDecoder::close()
95 {
96     if( m_stream )
97     {
98         jas_stream_close( (jas_stream_t*)m_stream );
99         m_stream = 0;
100     }
101
102     if( m_image )
103     {
104         jas_image_destroy( (jas_image_t*)m_image );
105         m_image = 0;
106     }
107 }
108
109
110 bool  Jpeg2KDecoder::readHeader()
111 {
112     bool result = false;
113
114     close();
115     jas_stream_t* stream = jas_stream_fopen( m_filename.c_str(), "rb" );
116     m_stream = stream;
117
118     if( stream )
119     {
120         jas_image_t* image = jas_image_decode( stream, -1, 0 );
121         m_image = image;
122         if( image ) {
123             m_width = jas_image_width( image );
124             m_height = jas_image_height( image );
125
126             int cntcmpts = 0; // count the known components
127             int numcmpts = jas_image_numcmpts( image );
128             int depth = 0;
129             for( int i = 0; i < numcmpts; i++ )
130             {
131                 int depth_i = jas_image_cmptprec( image, i );
132                 depth = MAX(depth, depth_i);
133                 if( jas_image_cmpttype( image, i ) > 2 )
134                     continue;
135                 cntcmpts++;
136             }
137
138             if( cntcmpts )
139             {
140                 m_type = CV_MAKETYPE(depth <= 8 ? CV_8U : CV_16U, cntcmpts > 1 ? 3 : 1);
141                 result = true;
142             }
143         }
144     }
145
146     if( !result )
147         close();
148
149     return result;
150 }
151
152
153 bool  Jpeg2KDecoder::readData( Mat& img )
154 {
155     bool result = false;
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;
161
162     if( stream && image )
163     {
164         bool convert;
165         int colorspace;
166         if( color )
167         {
168             convert = (jas_image_clrspc( image ) != JAS_CLRSPC_SRGB);
169             colorspace = JAS_CLRSPC_SRGB;
170         }
171         else
172         {
173             convert = (jas_clrspc_fam( jas_image_clrspc( image ) ) != JAS_CLRSPC_FAM_GRAY);
174             colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY?
175         }
176
177         // convert to the desired colorspace
178         if( convert )
179         {
180             jas_cmprof_t *clrprof = jas_cmprof_createfromclrspc( colorspace );
181             if( clrprof )
182             {
183                 jas_image_t *_img = jas_image_chclrspc( image, clrprof, JAS_CMXFORM_INTENT_RELCLR );
184                 if( _img )
185                 {
186                     jas_image_destroy( image );
187                     m_image = image = _img;
188                     result = true;
189                 }
190                 else
191                     fprintf(stderr, "JPEG 2000 LOADER ERROR: cannot convert colorspace\n");
192                 jas_cmprof_destroy( clrprof );
193             }
194             else
195                 fprintf(stderr, "JPEG 2000 LOADER ERROR: unable to create colorspace\n");
196         }
197         else
198             result = true;
199
200         if( result )
201         {
202             int ncmpts;
203             int cmptlut[3];
204             if( color )
205             {
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 )
210                     result = false;
211                 ncmpts = 3;
212             }
213             else
214             {
215                 cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_GRAY_Y );
216                 if( cmptlut[0] < 0 )
217                     result = false;
218                 ncmpts = 1;
219             }
220
221             if( result )
222             {
223                 for( int i = 0; i < ncmpts; i++ )
224                 {
225                     int maxval = 1 << jas_image_cmptprec( image, cmptlut[i] );
226                     int offset =  jas_image_cmptsgnd( image, cmptlut[i] ) ? maxval / 2 : 0;
227
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] );
232
233                     jas_matrix_t *buffer = jas_matrix_create( yend / ystep, xend / xstep );
234                     if( buffer )
235                     {
236                         if( !jas_image_readcmpt( image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
237                         {
238                             if( img.depth() == CV_8U )
239                                 result = readComponent8u( data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts );
240                             else
241                                 result = readComponent16u( ((unsigned short *)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts );
242                             if( !result )
243                             {
244                                 i = ncmpts;
245                                 result = false;
246                             }
247                         }
248                         jas_matrix_destroy( buffer );
249                     }
250                 }
251             }
252         }
253         else
254             fprintf(stderr, "JPEG2000 LOADER ERROR: colorspace conversion failed\n" );
255     }
256
257     close();
258
259     return result;
260 }
261
262
263 bool  Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
264                                       int step, int cmpt,
265                                       int maxval, int offset, int ncmpts )
266 {
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 );
277     int x, y, x1, y1, j;
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;
282
283     for( y = 0; y < yend - ystart; )
284     {
285         jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
286         uchar* dst = data + (y - yoffset) * step - xoffset;
287
288         if( xstep == 1 )
289         {
290             if( maxval == 256 && offset == 0 )
291                 for( x = 0; x < xend - xstart; x++ )
292                 {
293                     int pix = pix_row[x];
294                     dst[x*ncmpts] = cv::saturate_cast<uchar>(pix);
295                 }
296             else
297                 for( x = 0; x < xend - xstart; x++ )
298                 {
299                     int pix = ((pix_row[x] + delta) >> rshift) << lshift;
300                     dst[x*ncmpts] = cv::saturate_cast<uchar>(pix);
301                 }
302         }
303         else if( xstep == 2 && offset == 0 )
304             for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
305             {
306                 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
307                 dst[x*ncmpts] = dst[(x+1)*ncmpts] = cv::saturate_cast<uchar>(pix);
308             }
309         else
310             for( x = 0, j = 0; x < xend - xstart; j++ )
311             {
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;
316             }
317         y1 = y + ystep;
318         for( ++y; y < y1; y++, dst += step )
319             for( x = 0; x < xend - xstart; x++ )
320                 dst[x*ncmpts + step] = dst[x*ncmpts];
321     }
322
323     return true;
324 }
325
326
327 bool  Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
328                                        int step, int cmpt,
329                                        int maxval, int offset, int ncmpts )
330 {
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 );
341     int x, y, x1, y1, j;
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;
346
347     for( y = 0; y < yend - ystart; )
348     {
349         jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
350         ushort* dst = data + (y - yoffset) * step - xoffset;
351
352         if( xstep == 1 )
353         {
354             if( maxval == 65536 && offset == 0 )
355                 for( x = 0; x < xend - xstart; x++ )
356                 {
357                     int pix = pix_row[x];
358                     dst[x*ncmpts] = cv::saturate_cast<ushort>(pix);
359                 }
360             else
361                 for( x = 0; x < xend - xstart; x++ )
362                 {
363                     int pix = ((pix_row[x] + delta) >> rshift) << lshift;
364                     dst[x*ncmpts] = cv::saturate_cast<ushort>(pix);
365                 }
366         }
367         else if( xstep == 2 && offset == 0 )
368             for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
369             {
370                 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
371                 dst[x*ncmpts] = dst[(x+1)*ncmpts] = cv::saturate_cast<ushort>(pix);
372             }
373         else
374             for( x = 0, j = 0; x < xend - xstart; j++ )
375             {
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;
380             }
381         y1 = y + ystep;
382         for( ++y; y < y1; y++, dst += step )
383             for( x = 0; x < xend - xstart; x++ )
384                 dst[x*ncmpts + step] = dst[x*ncmpts];
385     }
386
387     return true;
388 }
389
390
391 /////////////////////// Jpeg2KEncoder ///////////////////
392
393
394 Jpeg2KEncoder::Jpeg2KEncoder()
395 {
396     m_description = "JPEG-2000 files (*.jp2)";
397 }
398
399
400 Jpeg2KEncoder::~Jpeg2KEncoder()
401 {
402 }
403
404 ImageEncoder Jpeg2KEncoder::newEncoder() const
405 {
406     return makePtr<Jpeg2KEncoder>();
407 }
408
409 bool  Jpeg2KEncoder::isFormatSupported( int depth ) const
410 {
411     return depth == CV_8U || depth == CV_16U;
412 }
413
414
415 bool  Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
416 {
417     int width = _img.cols, height = _img.rows;
418     int depth = _img.depth(), channels = _img.channels();
419     depth = depth == CV_8U ? 8 : 16;
420
421     if( channels > 3 || channels < 1 )
422         return false;
423
424     jas_image_cmptparm_t component_info[3];
425     for( int i = 0; i < channels; i++ )
426     {
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;
435     }
436     jas_image_t *img = jas_image_create( channels, component_info, (channels == 1) ? JAS_CLRSPC_SGRAY : JAS_CLRSPC_SRGB );
437     if( !img )
438         return false;
439
440     if(channels == 1)
441         jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_GRAY_Y );
442     else
443     {
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 );
447     }
448
449     bool result;
450     if( depth == 8 )
451         result = writeComponent8u( img, _img );
452     else
453         result = writeComponent16u( img, _img );
454     if( result )
455     {
456         jas_stream_t *stream = jas_stream_fopen( m_filename.c_str(), "wb" );
457         if( stream )
458         {
459             result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)"" );
460
461             jas_stream_close( stream );
462         }
463
464     }
465     jas_image_destroy( img );
466
467     return result;
468 }
469
470
471 bool  Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
472 {
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 );
476     if(!row)
477         return false;
478
479     for( int y = 0; y < h; y++ )
480     {
481         uchar* data = _img.data + _img.step*y;
482         for( int i = 0; i < ncmpts; i++ )
483         {
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 );
487         }
488     }
489
490     jas_matrix_destroy( row );
491     return true;
492 }
493
494
495 bool  Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
496 {
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 );
500     if(!row)
501         return false;
502
503     for( int y = 0; y < h; y++ )
504     {
505         uchar* data = _img.data + _img.step*y;
506         for( int i = 0; i < ncmpts; i++ )
507         {
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 );
511         }
512     }
513
514     jas_matrix_destroy( row );
515
516     return true;
517 }
518
519 }
520
521 #endif
522
523 /* End of file. */