Update to 2.0.0 tree from current Fremantle build
[opencv] / src / highgui / 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 "_highgui.h"
44
45 #ifdef HAVE_JASPER
46
47 #include "grfmt_jpeg2000.h"
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 PACKAGE
57 #undef PACKAGE_BUGREPORT
58 #undef PACKAGE_NAME
59 #undef PACKAGE_STRING
60 #undef PACKAGE_TARNAME
61 #undef PACKAGE_VERSION
62 #undef VERSION
63
64 #include <jasper/jasper.h>
65 // FIXME bad hack
66 #undef uchar
67 #undef ulong
68
69 namespace cv
70 {
71
72 struct JasperInitializer
73 {
74     JasperInitializer() { jas_init(); }
75     ~JasperInitializer() { jas_cleanup(); }
76 };
77
78 static JasperInitializer initialize_jasper;
79
80
81 /////////////////////// Jpeg2KDecoder ///////////////////
82
83 Jpeg2KDecoder::Jpeg2KDecoder()
84 {
85     m_signature = '\0' + string() + '\0' + string() + '\0' + string("\x0cjP  \r\n\x87\n");
86     m_stream = 0;
87     m_image = 0;
88 }
89
90
91 Jpeg2KDecoder::~Jpeg2KDecoder()
92 {
93 }
94
95 ImageDecoder Jpeg2KDecoder::newDecoder() const
96 {
97     return new Jpeg2KDecoder;
98 }
99
100 void  Jpeg2KDecoder::close()
101 {
102     if( m_stream )
103     {
104         jas_stream_close( (jas_stream_t*)m_stream );
105         m_stream = 0;
106     }
107
108     if( m_image )
109     {
110         jas_image_destroy( (jas_image_t*)m_image );
111         m_image = 0;
112     }
113 }
114
115
116 bool  Jpeg2KDecoder::readHeader()
117 {
118     bool result = false;
119
120     close();
121     jas_stream_t* stream = jas_stream_fopen( m_filename.c_str(), "rb" );
122     m_stream = stream;
123
124     if( stream )
125     {
126         jas_image_t* image = jas_image_decode( stream, -1, 0 );
127         m_image = image;
128         if( image ) {
129             m_width = jas_image_width( image );
130             m_height = jas_image_height( image );
131
132             int cntcmpts = 0; // count the known components
133             int numcmpts = jas_image_numcmpts( image );
134             int depth = 0;
135             for( int i = 0; i < numcmpts; i++ )
136             {
137                 int depth_i = jas_image_cmptprec( image, i );
138                 depth = MAX(depth, depth_i);
139                 if( jas_image_cmpttype( image, i ) > 2 )
140                     continue;
141                 cntcmpts++;
142             }
143
144             if( cntcmpts )
145             {
146                 m_type = CV_MAKETYPE(depth <= 8 ? CV_8U : CV_16U, cntcmpts > 1 ? 3 : 1);
147                 result = true;
148             }
149         }
150     }
151
152     if( !result )
153         close();
154
155     return result;
156 }
157
158
159 bool  Jpeg2KDecoder::readData( Mat& img )
160 {
161     bool result = false;
162     int color = img.channels() > 1;
163     uchar* data = img.data;
164     int step = img.step;
165     jas_stream_t* stream = (jas_stream_t*)m_stream;
166     jas_image_t* image = (jas_image_t*)m_image;
167
168     if( stream && image )
169     {
170         bool convert;
171         int colorspace;
172         if( color )
173         {
174             convert = (jas_image_clrspc( image ) != JAS_CLRSPC_SRGB);
175             colorspace = JAS_CLRSPC_SRGB;
176         }
177         else
178         {
179             convert = (jas_clrspc_fam( jas_image_clrspc( image ) ) != JAS_CLRSPC_FAM_GRAY);
180             colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY?
181         }
182
183         // convert to the desired colorspace
184         if( convert )
185         {
186             jas_cmprof_t *clrprof = jas_cmprof_createfromclrspc( colorspace );
187             if( clrprof )
188             {
189                 jas_image_t *_img = jas_image_chclrspc( image, clrprof, JAS_CMXFORM_INTENT_RELCLR );
190                 if( _img )
191                 {
192                     jas_image_destroy( image );
193                     m_image = image = _img;
194                     result = true;
195                 }
196                 else
197                     fprintf(stderr, "JPEG 2000 LOADER ERROR: cannot convert colorspace\n");
198                 jas_cmprof_destroy( clrprof );
199             }
200             else
201                 fprintf(stderr, "JPEG 2000 LOADER ERROR: unable to create colorspace\n");
202         }
203         else
204             result = true;
205
206         if( result )
207         {
208             int ncmpts;
209             int cmptlut[3];
210             if( color )
211             {
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[0] < 0 )
216                     result = false;
217                 ncmpts = 3;
218             }
219             else
220             {
221                 cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_GRAY_Y );
222                 if( cmptlut[0] < 0 )
223                     result = false;
224                 ncmpts = 1;
225             }
226
227             if( result )
228             {
229                 for( int i = 0; i < ncmpts; i++ )
230                 {
231                     int maxval = 1 << jas_image_cmptprec( image, cmptlut[i] );
232                     int offset =  jas_image_cmptsgnd( image, cmptlut[i] ) ? maxval / 2 : 0;
233
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] );
238
239                     jas_matrix_t *buffer = jas_matrix_create( yend / ystep, xend / xstep );
240                     if( buffer )
241                     {
242                         if( !jas_image_readcmpt( image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
243                         {
244                             if( img.depth() == CV_8U )
245                                 result = readComponent8u( data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts );
246                             else
247                                 result = readComponent16u( ((unsigned short *)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts );
248                             if( !result )
249                             {
250                                 i = ncmpts;
251                                 result = false;
252                             }
253                         }
254                         jas_matrix_destroy( buffer );
255                     }
256                 }
257             }
258         }
259         else
260             fprintf(stderr, "JPEG2000 LOADER ERROR: colorspace conversion failed\n" );
261     }
262
263     close();
264
265     return result;
266 }
267
268
269 bool  Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
270                                       int step, int cmpt,
271                                       int maxval, int offset, int ncmpts )
272 {
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 );
283     int x, y, x1, y1, j;
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;
288
289     for( y = 0; y < yend - ystart; )
290     {
291         jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
292         uchar* dst = data + (y - yoffset) * step - xoffset;
293
294         if( xstep == 1 )
295         {
296             if( maxval == 256 && offset == 0 )
297                 for( x = 0; x < xend - xstart; x++ )
298                 {
299                     int pix = pix_row[x];
300                     dst[x*ncmpts] = CV_CAST_8U(pix);
301                 }
302             else
303                 for( x = 0; x < xend - xstart; x++ )
304                 {
305                     int pix = ((pix_row[x] + delta) >> rshift) << lshift;
306                     dst[x*ncmpts] = CV_CAST_8U(pix);
307                 }
308         }
309         else if( xstep == 2 && offset == 0 )
310             for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
311             {
312                 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
313                 dst[x*ncmpts] = dst[(x+1)*ncmpts] = CV_CAST_8U(pix);
314             }
315         else
316             for( x = 0, j = 0; x < xend - xstart; j++ )
317             {
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;
322             }
323         y1 = y + ystep;
324         for( ++y; y < y1; y++, dst += step )
325             for( x = 0; x < xend - xstart; x++ )
326                 dst[x*ncmpts + step] = dst[x*ncmpts];
327     }
328
329     return true;
330 }
331
332
333 bool  Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
334                                        int step, int cmpt,
335                                        int maxval, int offset, int ncmpts )
336 {
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 );
347     int x, y, x1, y1, j;
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;
352
353     for( y = 0; y < yend - ystart; )
354     {
355         jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
356         ushort* dst = data + (y - yoffset) * step - xoffset;
357
358         if( xstep == 1 )
359         {
360             if( maxval == 65536 && offset == 0 )
361                 for( x = 0; x < xend - xstart; x++ )
362                 {
363                     int pix = pix_row[x];
364                     dst[x*ncmpts] = CV_CAST_16U(pix);
365                 }
366             else
367                 for( x = 0; x < xend - xstart; x++ )
368                 {
369                     int pix = ((pix_row[x] + delta) >> rshift) << lshift;
370                     dst[x*ncmpts] = CV_CAST_16U(pix);
371                 }
372         }
373         else if( xstep == 2 && offset == 0 )
374             for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
375             {
376                 int pix = ((pix_row[j] + delta) >> rshift) << lshift;
377                 dst[x*ncmpts] = dst[(x+1)*ncmpts] = CV_CAST_16U(pix);
378             }
379         else
380             for( x = 0, j = 0; x < xend - xstart; j++ )
381             {
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;
386             }
387         y1 = y + ystep;
388         for( ++y; y < y1; y++, dst += step )
389             for( x = 0; x < xend - xstart; x++ )
390                 dst[x*ncmpts + step] = dst[x*ncmpts];
391     }
392
393     return true;
394 }
395
396
397 /////////////////////// Jpeg2KEncoder ///////////////////
398
399
400 Jpeg2KEncoder::Jpeg2KEncoder()
401 {
402     m_description = "JPEG-2000 files (*.jp2)";
403 }
404
405
406 Jpeg2KEncoder::~Jpeg2KEncoder()
407 {
408 }
409
410 ImageEncoder Jpeg2KEncoder::newEncoder() const
411 {
412     return new Jpeg2KEncoder;
413 }
414
415 bool  Jpeg2KEncoder::isFormatSupported( int depth ) const
416 {
417     return depth == CV_8U || depth == CV_16U;
418 }
419
420
421 bool  Jpeg2KEncoder::write( const Mat& _img, const vector<int>& )
422 {
423     int width = _img.cols, height = _img.rows;
424     int depth = _img.depth(), channels = _img.channels();
425     depth = depth == CV_8U ? 8 : 16;
426
427     if( channels > 3 || channels < 1 )
428         return false;
429
430     jas_image_cmptparm_t component_info[3];
431     for( int i = 0; i < channels; i++ )
432     {
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;
441     }
442     jas_image_t *img = jas_image_create( channels, component_info, (channels == 1) ? JAS_CLRSPC_SGRAY : JAS_CLRSPC_SRGB );
443     if( !img )
444         return false;
445
446     if(channels == 1)
447         jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_GRAY_Y );
448     else
449     {
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 );
453     }
454
455     bool result;
456     if( depth == 8 )
457         result = writeComponent8u( img, _img );
458     else
459         result = writeComponent16u( img, _img );
460     if( result )
461     {
462         jas_stream_t *stream = jas_stream_fopen( m_filename.c_str(), "wb" );
463         if( stream )
464         {
465             result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)"" );
466
467             jas_stream_close( stream );
468         }
469
470     }
471     jas_image_destroy( img );
472
473     return result;
474 }
475
476
477 bool  Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
478 {
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 );
482     if(!row)
483         return false;
484
485     for( int y = 0; y < h; y++ )
486     {
487         uchar* data = _img.data + _img.step*y;
488         for( int i = 0; i < ncmpts; i++ )
489         {
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 );
493         }
494     }
495
496     jas_matrix_destroy( row );
497     return true;
498 }
499
500
501 bool  Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
502 {
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 );
506     if(!row)
507         return false;
508
509     for( int y = 0; y < h; y++ )
510     {
511         uchar* data = _img.data + _img.step*y;
512         for( int i = 0; i < ncmpts; i++ )
513         {
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 );
517         }
518     }
519
520     jas_matrix_destroy( row );
521
522     return true;
523 }
524
525 }
526
527 #endif
528
529 /* End of file. */