1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#ifdef HAVE_JASPER
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "grfmt_jpeg2000.hpp"
#include "opencv2/imgproc.hpp"
#ifdef _WIN32
#define JAS_WIN_MSVC_BUILD 1
#ifdef __GNUC__
#define HAVE_STDINT_H 1
#endif
#endif
#undef VERSION
#include <jasper/jasper.h>
// FIXME bad hack
#undef uchar
#undef ulong
namespace cv
{
struct JasperInitializer
{
JasperInitializer() { jas_init(); }
~JasperInitializer() { jas_cleanup(); }
};
static JasperInitializer& _initJasper()
{
static JasperInitializer initialize_jasper;
return initialize_jasper;
}
static bool isJasperEnabled()
{
static const bool PARAM_ENABLE_JASPER = utils::getConfigurationParameterBool("OPENCV_IO_ENABLE_JASPER",
#ifdef OPENCV_IMGCODECS_FORCE_JASPER
true
#else
false
#endif
);
return PARAM_ENABLE_JASPER;
}
static JasperInitializer& initJasper()
{
if (isJasperEnabled())
{
return _initJasper();
}
else
{
const char* message = "imgcodecs: Jasper (JPEG-2000) codec is disabled. You can enable it via 'OPENCV_IO_ENABLE_JASPER' option. Refer for details and cautions here: https://github.com/opencv/opencv/issues/14058";
CV_LOG_WARNING(NULL, message);
CV_Error(Error::StsNotImplemented, message);
}
}
/////////////////////// Jpeg2KDecoder ///////////////////
Jpeg2KDecoder::Jpeg2KDecoder()
{
static const unsigned char signature_[12] = { 0, 0, 0, 0x0c, 'j', 'P', ' ', ' ', 13, 10, 0x87, 10};
m_signature = String((const char*)signature_, (const char*)signature_ + sizeof(signature_));
m_stream = 0;
m_image = 0;
}
Jpeg2KDecoder::~Jpeg2KDecoder()
{
}
ImageDecoder Jpeg2KDecoder::newDecoder() const
{
initJasper();
return makePtr<Jpeg2KDecoder>();
}
void Jpeg2KDecoder::close()
{
if( m_stream )
{
CV_Assert(isJasperEnabled());
jas_stream_close( (jas_stream_t*)m_stream );
m_stream = 0;
}
if( m_image )
{
CV_Assert(isJasperEnabled());
jas_image_destroy( (jas_image_t*)m_image );
m_image = 0;
}
}
bool Jpeg2KDecoder::readHeader()
{
CV_Assert(isJasperEnabled());
bool result = false;
close();
jas_stream_t* stream = jas_stream_fopen( m_filename.c_str(), "rb" );
m_stream = stream;
if( stream )
{
jas_image_t* image = jas_image_decode( stream, -1, 0 );
m_image = image;
if( image ) {
CV_Assert(0 == (jas_image_tlx(image)) && "not supported");
CV_Assert(0 == (jas_image_tly(image)) && "not supported");
m_width = jas_image_width( image );
m_height = jas_image_height( image );
int cntcmpts = 0; // count the known components
int numcmpts = jas_image_numcmpts( image );
int depth = 0;
for( int i = 0; i < numcmpts; i++ )
{
int depth_i = jas_image_cmptprec( image, i );
CV_Assert(depth == 0 || depth == depth_i); // component data type mismatch
depth = MAX(depth, depth_i);
if( jas_image_cmpttype( image, i ) > 2 )
continue;
int sgnd = jas_image_cmptsgnd(image, i);
int xstart = jas_image_cmpttlx(image, i);
int xend = jas_image_cmptbrx(image, i);
int xstep = jas_image_cmpthstep(image, i);
int ystart = jas_image_cmpttly(image, i);
int yend = jas_image_cmptbry(image, i);
int ystep = jas_image_cmptvstep(image, i);
CV_Assert(sgnd == 0 && "not supported");
CV_Assert(xstart == 0 && "not supported");
CV_Assert(ystart == 0 && "not supported");
CV_Assert(xstep == 1 && "not supported");
CV_Assert(ystep == 1 && "not supported");
CV_Assert(xend == m_width);
CV_Assert(yend == m_height);
cntcmpts++;
}
if( cntcmpts )
{
CV_Assert(depth == 8 || depth == 16);
CV_Assert(cntcmpts == 1 || cntcmpts == 3);
m_type = CV_MAKETYPE(depth <= 8 ? CV_8U : CV_16U, cntcmpts > 1 ? 3 : 1);
result = true;
}
}
}
if( !result )
close();
return result;
}
static void Jpeg2KDecoder_close(Jpeg2KDecoder* ptr)
{
ptr->close();
}
bool Jpeg2KDecoder::readData( Mat& img )
{
CV_Assert(isJasperEnabled());
Ptr<Jpeg2KDecoder> close_this(this, Jpeg2KDecoder_close);
bool result = false;
bool color = img.channels() > 1;
uchar* data = img.ptr();
size_t step = img.step;
jas_stream_t* stream = (jas_stream_t*)m_stream;
jas_image_t* image = (jas_image_t*)m_image;
#ifndef _WIN32
// At least on some Linux instances the
// system libjasper segfaults when
// converting color to grey.
// We do this conversion manually at the end.
Mat clr;
if (CV_MAT_CN(img.type()) < CV_MAT_CN(this->type()))
{
clr.create(img.size().height, img.size().width, this->type());
color = true;
data = clr.ptr();
step = (int)clr.step;
}
#endif
if( stream && image )
{
bool convert;
int colorspace;
if( color )
{
convert = (jas_image_clrspc( image ) != JAS_CLRSPC_SRGB);
colorspace = JAS_CLRSPC_SRGB;
}
else
{
convert = (jas_clrspc_fam( jas_image_clrspc( image ) ) != JAS_CLRSPC_FAM_GRAY);
colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY? (GENGRAY fails on Win.)
}
// convert to the desired colorspace
if( convert )
{
jas_cmprof_t *clrprof = jas_cmprof_createfromclrspc( colorspace );
if( clrprof )
{
jas_image_t *_img = jas_image_chclrspc( image, clrprof, JAS_CMXFORM_INTENT_RELCLR );
if( _img )
{
jas_image_destroy( image );
m_image = image = _img;
result = true;
}
else
{
jas_cmprof_destroy(clrprof);
CV_Error(Error::StsError, "JPEG 2000 LOADER ERROR: cannot convert colorspace");
}
jas_cmprof_destroy( clrprof );
}
else
{
CV_Error(Error::StsError, "JPEG 2000 LOADER ERROR: unable to create colorspace");
}
}
else
result = true;
if( result )
{
int ncmpts;
int cmptlut[3];
if( color )
{
cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_B );
cmptlut[1] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_G );
cmptlut[2] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_RGB_R );
if( cmptlut[0] < 0 || cmptlut[1] < 0 || cmptlut[2] < 0 )
result = false;
ncmpts = 3;
}
else
{
cmptlut[0] = jas_image_getcmptbytype( image, JAS_IMAGE_CT_GRAY_Y );
if( cmptlut[0] < 0 )
result = false;
ncmpts = 1;
}
if( result )
{
for( int i = 0; i < ncmpts; i++ )
{
int maxval = 1 << jas_image_cmptprec( image, cmptlut[i] );
int offset = jas_image_cmptsgnd( image, cmptlut[i] ) ? maxval / 2 : 0;
int yend = jas_image_cmptbry( image, cmptlut[i] );
int ystep = jas_image_cmptvstep( image, cmptlut[i] );
int xend = jas_image_cmptbrx( image, cmptlut[i] );
int xstep = jas_image_cmpthstep( image, cmptlut[i] );
jas_matrix_t *buffer = jas_matrix_create( yend / ystep, xend / xstep );
if( buffer )
{
if( !jas_image_readcmpt( image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
{
if( img.depth() == CV_8U )
result = readComponent8u( data + i, buffer, validateToInt(step), cmptlut[i], maxval, offset, ncmpts );
else
result = readComponent16u( ((unsigned short *)data) + i, buffer, validateToInt(step / 2), cmptlut[i], maxval, offset, ncmpts );
if( !result )
{
jas_matrix_destroy( buffer );
CV_Error(Error::StsError, "JPEG2000 LOADER ERROR: failed to read component");
}
}
jas_matrix_destroy( buffer );
}
}
}
}
else
{
CV_Error(Error::StsError, "JPEG2000 LOADER ERROR: colorspace conversion failed");
}
}
CV_Assert(result == true);
#ifndef _WIN32
if (!clr.empty())
{
cv::cvtColor(clr, img, COLOR_BGR2GRAY);
}
#endif
return result;
}
bool Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer,
int step, int cmpt,
int maxval, int offset, int ncmpts )
{
CV_Assert(isJasperEnabled());
jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
jas_image_t* image = (jas_image_t*)m_image;
int xstart = jas_image_cmpttlx( image, cmpt );
int xend = jas_image_cmptbrx( image, cmpt );
int xstep = jas_image_cmpthstep( image, cmpt );
int xoffset = jas_image_tlx( image );
int ystart = jas_image_cmpttly( image, cmpt );
int yend = jas_image_cmptbry( image, cmpt );
int ystep = jas_image_cmptvstep( image, cmpt );
int yoffset = jas_image_tly( image );
int x, y, x1, y1, j;
int rshift = cvRound(std::log(maxval/256.)/std::log(2.));
int lshift = MAX(0, -rshift);
rshift = MAX(0, rshift);
int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset;
for( y = 0; y < yend - ystart; )
{
jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
uchar* dst = data + (y - yoffset) * step - xoffset;
if( xstep == 1 )
{
if( maxval == 256 && offset == 0 )
for( x = 0; x < xend - xstart; x++ )
{
int pix = pix_row[x];
dst[x*ncmpts] = cv::saturate_cast<uchar>(pix);
}
else
for( x = 0; x < xend - xstart; x++ )
{
int pix = ((pix_row[x] + delta) >> rshift) << lshift;
dst[x*ncmpts] = cv::saturate_cast<uchar>(pix);
}
}
else if( xstep == 2 && offset == 0 )
for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
{
int pix = ((pix_row[j] + delta) >> rshift) << lshift;
dst[x*ncmpts] = dst[(x+1)*ncmpts] = cv::saturate_cast<uchar>(pix);
}
else
for( x = 0, j = 0; x < xend - xstart; j++ )
{
int pix = ((pix_row[j] + delta) >> rshift) << lshift;
pix = cv::saturate_cast<uchar>(pix);
for( x1 = x + xstep; x < x1; x++ )
dst[x*ncmpts] = (uchar)pix;
}
y1 = y + ystep;
for( ++y; y < y1; y++, dst += step )
for( x = 0; x < xend - xstart; x++ )
dst[x*ncmpts + step] = dst[x*ncmpts];
}
return true;
}
bool Jpeg2KDecoder::readComponent16u( unsigned short *data, void *_buffer,
int step, int cmpt,
int maxval, int offset, int ncmpts )
{
CV_Assert(isJasperEnabled());
jas_matrix_t* buffer = (jas_matrix_t*)_buffer;
jas_image_t* image = (jas_image_t*)m_image;
int xstart = jas_image_cmpttlx( image, cmpt );
int xend = jas_image_cmptbrx( image, cmpt );
int xstep = jas_image_cmpthstep( image, cmpt );
int xoffset = jas_image_tlx( image );
int ystart = jas_image_cmpttly( image, cmpt );
int yend = jas_image_cmptbry( image, cmpt );
int ystep = jas_image_cmptvstep( image, cmpt );
int yoffset = jas_image_tly( image );
int x, y, x1, y1, j;
int rshift = cvRound(std::log(maxval/65536.)/std::log(2.));
int lshift = MAX(0, -rshift);
rshift = MAX(0, rshift);
int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset;
for( y = 0; y < yend - ystart; )
{
jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 );
ushort* dst = data + (y - yoffset) * step - xoffset;
if( xstep == 1 )
{
if( maxval == 65536 && offset == 0 )
for( x = 0; x < xend - xstart; x++ )
{
int pix = pix_row[x];
dst[x*ncmpts] = cv::saturate_cast<ushort>(pix);
}
else
for( x = 0; x < xend - xstart; x++ )
{
int pix = ((pix_row[x] + delta) >> rshift) << lshift;
dst[x*ncmpts] = cv::saturate_cast<ushort>(pix);
}
}
else if( xstep == 2 && offset == 0 )
for( x = 0, j = 0; x < xend - xstart; x += 2, j++ )
{
int pix = ((pix_row[j] + delta) >> rshift) << lshift;
dst[x*ncmpts] = dst[(x+1)*ncmpts] = cv::saturate_cast<ushort>(pix);
}
else
for( x = 0, j = 0; x < xend - xstart; j++ )
{
int pix = ((pix_row[j] + delta) >> rshift) << lshift;
pix = cv::saturate_cast<ushort>(pix);
for( x1 = x + xstep; x < x1; x++ )
dst[x*ncmpts] = (ushort)pix;
}
y1 = y + ystep;
for( ++y; y < y1; y++, dst += step )
for( x = 0; x < xend - xstart; x++ )
dst[x*ncmpts + step] = dst[x*ncmpts];
}
return true;
}
/////////////////////// Jpeg2KEncoder ///////////////////
Jpeg2KEncoder::Jpeg2KEncoder()
{
m_description = "JPEG-2000 files (*.jp2)";
}
Jpeg2KEncoder::~Jpeg2KEncoder()
{
}
ImageEncoder Jpeg2KEncoder::newEncoder() const
{
initJasper();
return makePtr<Jpeg2KEncoder>();
}
bool Jpeg2KEncoder::isFormatSupported( int depth ) const
{
return depth == CV_8U || depth == CV_16U;
}
bool Jpeg2KEncoder::write( const Mat& _img, const std::vector<int>& )
{
CV_Assert(isJasperEnabled());
int width = _img.cols, height = _img.rows;
int depth = _img.depth(), channels = _img.channels();
depth = depth == CV_8U ? 8 : 16;
if( channels > 3 || channels < 1 )
return false;
jas_image_cmptparm_t component_info[3];
for( int i = 0; i < channels; i++ )
{
component_info[i].tlx = 0;
component_info[i].tly = 0;
component_info[i].hstep = 1;
component_info[i].vstep = 1;
component_info[i].width = width;
component_info[i].height = height;
component_info[i].prec = depth;
component_info[i].sgnd = 0;
}
jas_image_t *img = jas_image_create( channels, component_info, (channels == 1) ? JAS_CLRSPC_SGRAY : JAS_CLRSPC_SRGB );
if( !img )
return false;
if(channels == 1)
jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_GRAY_Y );
else
{
jas_image_setcmpttype( img, 0, JAS_IMAGE_CT_RGB_B );
jas_image_setcmpttype( img, 1, JAS_IMAGE_CT_RGB_G );
jas_image_setcmpttype( img, 2, JAS_IMAGE_CT_RGB_R );
}
bool result;
if( depth == 8 )
result = writeComponent8u( img, _img );
else
result = writeComponent16u( img, _img );
if( result )
{
jas_stream_t *stream = jas_stream_fopen( m_filename.c_str(), "wb" );
if( stream )
{
result = !jas_image_encode( img, stream, jas_image_strtofmt( (char*)"jp2" ), (char*)"" );
jas_stream_close( stream );
}
}
jas_image_destroy( img );
return result;
}
bool Jpeg2KEncoder::writeComponent8u( void *__img, const Mat& _img )
{
CV_Assert(isJasperEnabled());
jas_image_t* img = (jas_image_t*)__img;
int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
jas_matrix_t *row = jas_matrix_create( 1, w );
if(!row)
return false;
for( int y = 0; y < h; y++ )
{
const uchar* data = _img.ptr(y);
for( int i = 0; i < ncmpts; i++ )
{
for( int x = 0; x < w; x++)
jas_matrix_setv( row, x, data[x * ncmpts + i] );
jas_image_writecmpt( img, i, 0, y, w, 1, row );
}
}
jas_matrix_destroy( row );
return true;
}
bool Jpeg2KEncoder::writeComponent16u( void *__img, const Mat& _img )
{
CV_Assert(isJasperEnabled());
jas_image_t* img = (jas_image_t*)__img;
int w = _img.cols, h = _img.rows, ncmpts = _img.channels();
jas_matrix_t *row = jas_matrix_create( 1, w );
if(!row)
return false;
for( int y = 0; y < h; y++ )
{
const ushort* data = _img.ptr<ushort>(y);
for( int i = 0; i < ncmpts; i++ )
{
for( int x = 0; x < w; x++)
jas_matrix_setv( row, x, data[x * ncmpts + i] );
jas_image_writecmpt( img, i, 0, y, w, 1, row );
}
}
jas_matrix_destroy( row );
return true;
}
}
#endif
/* End of file. */