1 : /******************************************************************************
2 : * $Id: jpeg2000dataset.cpp 18317 2009-12-16 20:36:48Z rouault $
3 : *
4 : * Project: JPEG-2000
5 : * Purpose: Partial implementation of the ISO/IEC 15444-1 standard
6 : * Author: Andrey Kiselev, dron@ak4719.spb.edu
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2002, Andrey Kiselev <dron@ak4719.spb.edu>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "gdal_pam.h"
31 : #include "cpl_string.h"
32 : #include "gdaljp2metadata.h"
33 :
34 : #include <jasper/jasper.h>
35 : #include "jpeg2000_vsil_io.h"
36 :
37 : CPL_CVSID("$Id: jpeg2000dataset.cpp 18317 2009-12-16 20:36:48Z rouault $");
38 :
39 : CPL_C_START
40 : void GDALRegister_JPEG2000(void);
41 : CPL_C_END
42 :
43 : // XXX: Part of code below extracted from the JasPer internal headers and
44 : // must be in sync with JasPer version (this one works with JasPer 1.900.1)
45 : #define JP2_FTYP_MAXCOMPATCODES 32
46 : #define JP2_BOX_IHDR 0x69686472 /* Image Header */
47 : #define JP2_BOX_BPCC 0x62706363 /* Bits Per Component */
48 : #define JP2_BOX_PCLR 0x70636c72 /* Palette */
49 : #define JP2_BOX_UUID 0x75756964 /* UUID */
50 : extern "C" {
51 : typedef struct {
52 : uint_fast32_t magic;
53 : } jp2_jp_t;
54 : typedef struct {
55 : uint_fast32_t majver;
56 : uint_fast32_t minver;
57 : uint_fast32_t numcompatcodes;
58 : uint_fast32_t compatcodes[JP2_FTYP_MAXCOMPATCODES];
59 : } jp2_ftyp_t;
60 : typedef struct {
61 : uint_fast32_t width;
62 : uint_fast32_t height;
63 : uint_fast16_t numcmpts;
64 : uint_fast8_t bpc;
65 : uint_fast8_t comptype;
66 : uint_fast8_t csunk;
67 : uint_fast8_t ipr;
68 : } jp2_ihdr_t;
69 : typedef struct {
70 : uint_fast16_t numcmpts;
71 : uint_fast8_t *bpcs;
72 : } jp2_bpcc_t;
73 : typedef struct {
74 : uint_fast8_t method;
75 : uint_fast8_t pri;
76 : uint_fast8_t approx;
77 : uint_fast32_t csid;
78 : uint_fast8_t *iccp;
79 : int iccplen;
80 : } jp2_colr_t;
81 : typedef struct {
82 : uint_fast16_t numlutents;
83 : uint_fast8_t numchans;
84 : int_fast32_t *lutdata;
85 : uint_fast8_t *bpc;
86 : } jp2_pclr_t;
87 : typedef struct {
88 : uint_fast16_t channo;
89 : uint_fast16_t type;
90 : uint_fast16_t assoc;
91 : } jp2_cdefchan_t;
92 : typedef struct {
93 : uint_fast16_t numchans;
94 : jp2_cdefchan_t *ents;
95 : } jp2_cdef_t;
96 : typedef struct {
97 : uint_fast16_t cmptno;
98 : uint_fast8_t map;
99 : uint_fast8_t pcol;
100 : } jp2_cmapent_t;
101 :
102 : typedef struct {
103 : uint_fast16_t numchans;
104 : jp2_cmapent_t *ents;
105 : } jp2_cmap_t;
106 :
107 : #ifdef HAVE_JASPER_UUID
108 : typedef struct {
109 : uint_fast32_t datalen;
110 : uint_fast8_t uuid[16];
111 : uint_fast8_t *data;
112 : } jp2_uuid_t;
113 : #endif
114 :
115 : struct jp2_boxops_s;
116 : typedef struct {
117 :
118 : struct jp2_boxops_s *ops;
119 : struct jp2_boxinfo_s *info;
120 :
121 : uint_fast32_t type;
122 :
123 : /* The length of the box including the (variable-length) header. */
124 : uint_fast32_t len;
125 :
126 : /* The length of the box data. */
127 : uint_fast32_t datalen;
128 :
129 : union {
130 : jp2_jp_t jp;
131 : jp2_ftyp_t ftyp;
132 : jp2_ihdr_t ihdr;
133 : jp2_bpcc_t bpcc;
134 : jp2_colr_t colr;
135 : jp2_pclr_t pclr;
136 : jp2_cdef_t cdef;
137 : jp2_cmap_t cmap;
138 : #ifdef HAVE_JASPER_UUID
139 : jp2_uuid_t uuid;
140 : #endif
141 : } data;
142 :
143 : } jp2_box_t;
144 :
145 : typedef struct jp2_boxops_s {
146 : void (*init)(jp2_box_t *box);
147 : void (*destroy)(jp2_box_t *box);
148 : int (*getdata)(jp2_box_t *box, jas_stream_t *in);
149 : int (*putdata)(jp2_box_t *box, jas_stream_t *out);
150 : void (*dumpdata)(jp2_box_t *box, FILE *out);
151 : } jp2_boxops_t;
152 :
153 : extern jp2_box_t *jp2_box_create(int type);
154 : extern void jp2_box_destroy(jp2_box_t *box);
155 : extern jp2_box_t *jp2_box_get(jas_stream_t *in);
156 : extern int jp2_box_put(jp2_box_t *box, jas_stream_t *out);
157 : #ifdef HAVE_JASPER_UUID
158 : int jp2_encode_uuid(jas_image_t *image, jas_stream_t *out,
159 : char *optstr, jp2_box_t *uuid);
160 : #endif
161 : }
162 : // XXX: End of JasPer header.
163 :
164 : /************************************************************************/
165 : /* ==================================================================== */
166 : /* JPEG2000Dataset */
167 : /* ==================================================================== */
168 : /************************************************************************/
169 :
170 : class JPEG2000Dataset : public GDALPamDataset
171 : {
172 : friend class JPEG2000RasterBand;
173 :
174 : jas_stream_t *psStream;
175 : jas_image_t *psImage;
176 : int iFormat;
177 :
178 : char *pszProjection;
179 : int bGeoTransformValid;
180 : double adfGeoTransform[6];
181 : int nGCPCount;
182 : GDAL_GCP *pasGCPList;
183 :
184 : int bAlreadyDecoded;
185 : int DecodeImage();
186 :
187 : public:
188 : JPEG2000Dataset();
189 : ~JPEG2000Dataset();
190 :
191 : static int Identify( GDALOpenInfo * );
192 : static GDALDataset *Open( GDALOpenInfo * );
193 :
194 : CPLErr GetGeoTransform( double* );
195 : virtual const char *GetProjectionRef(void);
196 : virtual int GetGCPCount();
197 : virtual const char *GetGCPProjection();
198 : virtual const GDAL_GCP *GetGCPs();
199 : };
200 :
201 : /************************************************************************/
202 : /* ==================================================================== */
203 : /* JPEG2000RasterBand */
204 : /* ==================================================================== */
205 : /************************************************************************/
206 :
207 : class JPEG2000RasterBand : public GDALPamRasterBand
208 : {
209 : friend class JPEG2000Dataset;
210 :
211 : // NOTE: poDS may be altered for NITF/JPEG2000 files!
212 : JPEG2000Dataset *poGDS;
213 :
214 : jas_matrix_t *psMatrix;
215 :
216 : int iDepth;
217 : int bSignedness;
218 :
219 : public:
220 :
221 : JPEG2000RasterBand( JPEG2000Dataset *, int, int, int );
222 : ~JPEG2000RasterBand();
223 :
224 : virtual CPLErr IReadBlock( int, int, void * );
225 : virtual GDALColorInterp GetColorInterpretation();
226 : };
227 :
228 :
229 : /************************************************************************/
230 : /* JPEG2000RasterBand() */
231 : /************************************************************************/
232 :
233 : JPEG2000RasterBand::JPEG2000RasterBand( JPEG2000Dataset *poDS, int nBand,
234 57 : int iDepth, int bSignedness )
235 :
236 : {
237 57 : this->poDS = poDS;
238 57 : poGDS = poDS;
239 57 : this->nBand = nBand;
240 57 : this->iDepth = iDepth;
241 57 : this->bSignedness = bSignedness;
242 :
243 : // XXX: JasPer can't handle data with depth > 32 bits
244 : // Maximum possible depth for JPEG2000 is 38!
245 57 : switch ( bSignedness )
246 : {
247 : case 1: // Signed component
248 6 : if (iDepth <= 8)
249 0 : this->eDataType = GDT_Byte; // FIXME: should be signed,
250 : // but we haven't signed byte
251 : // data type in GDAL
252 6 : else if (iDepth <= 16)
253 6 : this->eDataType = GDT_Int16;
254 0 : else if (iDepth <= 32)
255 0 : this->eDataType = GDT_Int32;
256 6 : break;
257 : case 0: // Unsigned component
258 : default:
259 51 : if (iDepth <= 8)
260 48 : this->eDataType = GDT_Byte;
261 3 : else if (iDepth <= 16)
262 3 : this->eDataType = GDT_UInt16;
263 0 : else if (iDepth <= 32)
264 0 : this->eDataType = GDT_UInt32;
265 : break;
266 : }
267 : // FIXME: Figure out optimal block size!
268 : // Should the block size be fixed or determined dynamically?
269 57 : nBlockXSize = MIN(256, poDS->nRasterXSize);
270 57 : nBlockYSize = MIN(256, poDS->nRasterYSize);
271 57 : psMatrix = jas_matrix_create(nBlockYSize, nBlockXSize);
272 57 : }
273 :
274 : /************************************************************************/
275 : /* ~JPEG2000RasterBand() */
276 : /************************************************************************/
277 :
278 57 : JPEG2000RasterBand::~JPEG2000RasterBand()
279 : {
280 57 : if ( psMatrix )
281 57 : jas_matrix_destroy( psMatrix );
282 57 : }
283 :
284 : /************************************************************************/
285 : /* IReadBlock() */
286 : /************************************************************************/
287 :
288 : CPLErr JPEG2000RasterBand::IReadBlock( int nBlockXOff, int nBlockYOff,
289 115 : void * pImage )
290 : {
291 : int i, j;
292 :
293 : // Decode image from the stream, if not yet
294 115 : if ( !poGDS->DecodeImage() )
295 : {
296 0 : return CE_Failure;
297 : }
298 :
299 : // Now we can calculate the pixel offset of the top left by multiplying
300 : // block offset with the block size.
301 :
302 : /* In case the dimensions of the image are not multiple of the block dimensions */
303 : /* take care of not requesting more pixels than available for the blocks at the */
304 : /* right or bottom of the image */
305 115 : int nWidthToRead = MIN(nBlockXSize, poGDS->nRasterXSize - nBlockXOff * nBlockXSize);
306 115 : int nHeightToRead = MIN(nBlockYSize, poGDS->nRasterYSize - nBlockYOff * nBlockYSize);
307 :
308 : jas_image_readcmpt( poGDS->psImage, nBand - 1,
309 : nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize,
310 115 : nWidthToRead, nHeightToRead, psMatrix );
311 :
312 115 : int nWordSize = GDALGetDataTypeSize(eDataType) / 8;
313 115 : int nLineSize = nBlockXSize * nWordSize;
314 115 : GByte* ptr = (GByte*)pImage;
315 :
316 : /* Pad incomplete blocks at the right or bottom of the image */
317 115 : if (nWidthToRead != nBlockXSize || nHeightToRead != nBlockYSize)
318 28 : memset(pImage, 0, nLineSize * nBlockYSize);
319 :
320 24927 : for( i = 0; i < nHeightToRead; i++, ptr += nLineSize )
321 : {
322 5987092 : for( j = 0; j < nWidthToRead; j++ )
323 : {
324 : // XXX: We need casting because matrix element always
325 : // has 32 bit depth in JasPer
326 : // FIXME: what about float values?
327 5962280 : switch( eDataType )
328 : {
329 : case GDT_Int16:
330 : {
331 1600 : ((GInt16*)ptr)[j] = (GInt16)jas_matrix_get(psMatrix, i, j);
332 : }
333 1600 : break;
334 : case GDT_Int32:
335 : {
336 0 : ((GInt32*)ptr)[j] = (GInt32)jas_matrix_get(psMatrix, i, j);
337 : }
338 0 : break;
339 : case GDT_UInt16:
340 : {
341 125000 : ((GUInt16*)ptr)[j] = (GUInt16)jas_matrix_get(psMatrix, i, j);
342 : }
343 125000 : break;
344 : case GDT_UInt32:
345 : {
346 0 : ((GUInt32*)ptr)[j] = (GUInt32)jas_matrix_get(psMatrix, i, j);
347 : }
348 0 : break;
349 : case GDT_Byte:
350 : default:
351 : {
352 5835680 : ((GByte*)ptr)[j] = (GByte)jas_matrix_get(psMatrix, i, j);
353 : }
354 : break;
355 : }
356 : }
357 : }
358 :
359 115 : return CE_None;
360 : }
361 :
362 : /************************************************************************/
363 : /* GetColorInterpretation() */
364 : /************************************************************************/
365 :
366 30 : GDALColorInterp JPEG2000RasterBand::GetColorInterpretation()
367 : {
368 : // Decode image from the stream, if not yet
369 30 : if ( !poGDS->DecodeImage() )
370 : {
371 0 : return GCI_Undefined;
372 : }
373 :
374 30 : if ( jas_clrspc_fam( jas_image_clrspc( poGDS->psImage ) ) ==
375 : JAS_CLRSPC_FAM_GRAY )
376 20 : return GCI_GrayIndex;
377 10 : else if ( jas_clrspc_fam( jas_image_clrspc( poGDS->psImage ) ) ==
378 : JAS_CLRSPC_FAM_RGB )
379 : {
380 10 : switch ( jas_image_cmpttype( poGDS->psImage, nBand - 1 ) )
381 : {
382 : case JAS_IMAGE_CT_RGB_R:
383 3 : return GCI_RedBand;
384 : case JAS_IMAGE_CT_RGB_G:
385 3 : return GCI_GreenBand;
386 : case JAS_IMAGE_CT_RGB_B:
387 3 : return GCI_BlueBand;
388 : case JAS_IMAGE_CT_OPACITY:
389 0 : return GCI_AlphaBand;
390 : default:
391 1 : return GCI_Undefined;
392 : }
393 : }
394 : else
395 0 : return GCI_Undefined;
396 : }
397 :
398 : /************************************************************************/
399 : /* ==================================================================== */
400 : /* JPEG2000Dataset */
401 : /* ==================================================================== */
402 : /************************************************************************/
403 :
404 : /************************************************************************/
405 : /* JPEG2000Dataset() */
406 : /************************************************************************/
407 :
408 39 : JPEG2000Dataset::JPEG2000Dataset()
409 : {
410 39 : psStream = NULL;
411 39 : psImage = NULL;
412 39 : nBands = 0;
413 39 : pszProjection = CPLStrdup("");
414 39 : nGCPCount = 0;
415 39 : pasGCPList = NULL;
416 39 : bGeoTransformValid = FALSE;
417 39 : adfGeoTransform[0] = 0.0;
418 39 : adfGeoTransform[1] = 1.0;
419 39 : adfGeoTransform[2] = 0.0;
420 39 : adfGeoTransform[3] = 0.0;
421 39 : adfGeoTransform[4] = 0.0;
422 39 : adfGeoTransform[5] = 1.0;
423 39 : bAlreadyDecoded = FALSE;
424 :
425 39 : poDriver = (GDALDriver *)GDALGetDriverByName("JPEG2000");
426 39 : }
427 :
428 : /************************************************************************/
429 : /* ~JPEG2000Dataset() */
430 : /************************************************************************/
431 :
432 39 : JPEG2000Dataset::~JPEG2000Dataset()
433 :
434 : {
435 39 : FlushCache();
436 :
437 39 : if ( psStream )
438 39 : jas_stream_close( psStream );
439 39 : if ( psImage )
440 36 : jas_image_destroy( psImage );
441 :
442 39 : if ( pszProjection )
443 39 : CPLFree( pszProjection );
444 39 : if( nGCPCount > 0 )
445 : {
446 2 : GDALDeinitGCPs( nGCPCount, pasGCPList );
447 2 : CPLFree( pasGCPList );
448 : }
449 39 : }
450 :
451 : /************************************************************************/
452 : /* DecodeImage() */
453 : /************************************************************************/
454 158 : int JPEG2000Dataset::DecodeImage()
455 : {
456 158 : if (bAlreadyDecoded)
457 121 : return psImage != NULL;
458 :
459 37 : bAlreadyDecoded = TRUE;
460 37 : if ( !( psImage = jas_image_decode(psStream, iFormat, 0) ) )
461 : {
462 : CPLDebug( "JPEG2000", "Unable to decode image. Format: %s, %d",
463 1 : jas_image_fmttostr( iFormat ), iFormat );
464 1 : return FALSE;
465 : }
466 :
467 : /* Case of a JP2 image : check that the properties given by */
468 : /* the JP2 boxes match the ones of the code stream */
469 36 : if (nBands != 0)
470 : {
471 24 : if (nBands != jas_image_numcmpts( psImage ))
472 : {
473 : CPLError(CE_Failure, CPLE_AppDefined,
474 : "The number of components indicated in the IHDR box (%d) mismatch "
475 : "the value specified in the code stream (%d)",
476 0 : nBands, jas_image_numcmpts( psImage ));
477 0 : jas_image_destroy( psImage );
478 0 : psImage = NULL;
479 0 : return FALSE;
480 : }
481 :
482 24 : if (nRasterXSize != jas_image_cmptwidth( psImage, 0 ) ||
483 : nRasterYSize != jas_image_cmptheight( psImage, 0 ) )
484 : {
485 : CPLError(CE_Failure, CPLE_AppDefined,
486 : "The dimensions indicated in the IHDR box (%d x %d) mismatch "
487 : "the value specified in the code stream (%d x %d)",
488 : nRasterXSize, nRasterYSize,
489 : jas_image_cmptwidth( psImage, 0 ),
490 0 : jas_image_cmptheight( psImage, 0 ));
491 0 : jas_image_destroy( psImage );
492 0 : psImage = NULL;
493 0 : return FALSE;
494 : }
495 :
496 : int iBand;
497 59 : for ( iBand = 0; iBand < nBands; iBand++ )
498 : {
499 35 : JPEG2000RasterBand* poBand = (JPEG2000RasterBand*) GetRasterBand(iBand+1);
500 35 : if (poBand->iDepth != jas_image_cmptprec( psImage, iBand ) ||
501 : poBand->bSignedness != jas_image_cmptsgnd( psImage, iBand ))
502 : {
503 : CPLError(CE_Failure, CPLE_AppDefined,
504 : "The bit depth of band %d indicated in the IHDR box (%d) mismatch "
505 : "the value specified in the code stream (%d)",
506 0 : iBand + 1, poBand->iDepth, jas_image_cmptprec( psImage, iBand ));
507 0 : jas_image_destroy( psImage );
508 0 : psImage = NULL;
509 0 : return FALSE;
510 : }
511 : }
512 : }
513 :
514 : /* Ask for YCbCr -> RGB translation */
515 36 : if ( jas_clrspc_fam( jas_image_clrspc( psImage ) ) ==
516 : JAS_CLRSPC_FAM_YCBCR )
517 : {
518 : jas_image_t *psRGBImage;
519 : jas_cmprof_t *psRGBProf;
520 1 : CPLDebug( "JPEG2000", "forcing conversion to sRGB");
521 1 : if (!(psRGBProf = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) {
522 0 : CPLDebug( "JPEG2000", "cannot create sRGB profile");
523 0 : return TRUE;
524 : }
525 1 : if (!(psRGBImage = jas_image_chclrspc(psImage, psRGBProf, JAS_CMXFORM_INTENT_PER))) {
526 0 : CPLDebug( "JPEG2000", "cannot convert to sRGB");
527 0 : jas_cmprof_destroy(psRGBProf);
528 0 : return TRUE;
529 : }
530 1 : jas_image_destroy(psImage);
531 1 : jas_cmprof_destroy(psRGBProf);
532 1 : psImage = psRGBImage;
533 : }
534 :
535 36 : return TRUE;
536 : }
537 :
538 :
539 : /************************************************************************/
540 : /* GetProjectionRef() */
541 : /************************************************************************/
542 :
543 25 : const char *JPEG2000Dataset::GetProjectionRef()
544 :
545 : {
546 25 : return( pszProjection );
547 : }
548 :
549 : /************************************************************************/
550 : /* GetGeoTransform() */
551 : /************************************************************************/
552 :
553 17 : CPLErr JPEG2000Dataset::GetGeoTransform( double * padfTransform )
554 : {
555 17 : if( bGeoTransformValid )
556 : {
557 7 : memcpy( padfTransform, adfGeoTransform, sizeof(adfGeoTransform[0]) * 6 );
558 7 : return CE_None;
559 : }
560 : else
561 10 : return CE_Failure;
562 : }
563 :
564 : /************************************************************************/
565 : /* GetGCPCount() */
566 : /************************************************************************/
567 :
568 4 : int JPEG2000Dataset::GetGCPCount()
569 :
570 : {
571 4 : return nGCPCount;
572 : }
573 :
574 : /************************************************************************/
575 : /* GetGCPProjection() */
576 : /************************************************************************/
577 :
578 1 : const char *JPEG2000Dataset::GetGCPProjection()
579 :
580 : {
581 1 : if( nGCPCount > 0 )
582 1 : return pszProjection;
583 : else
584 0 : return "";
585 : }
586 :
587 : /************************************************************************/
588 : /* GetGCP() */
589 : /************************************************************************/
590 :
591 1 : const GDAL_GCP *JPEG2000Dataset::GetGCPs()
592 :
593 : {
594 1 : return pasGCPList;
595 : }
596 :
597 48 : static void JPEG2000Init()
598 : {
599 : static int bHasInit = FALSE;
600 48 : if (!bHasInit)
601 : {
602 1 : bHasInit = TRUE;
603 1 : jas_init();
604 : }
605 48 : }
606 :
607 : /************************************************************************/
608 : /* Identify() */
609 : /************************************************************************/
610 :
611 9830 : int JPEG2000Dataset::Identify( GDALOpenInfo * poOpenInfo )
612 :
613 : {
614 : static const unsigned char jpc_header[] = {0xff,0x4f};
615 : static const unsigned char jp2_box_jp[] = {0x6a,0x50,0x20,0x20}; /* 'jP ' */
616 :
617 9830 : if( poOpenInfo->nHeaderBytes >= 16
618 : && (memcmp( poOpenInfo->pabyHeader, jpc_header,
619 : sizeof(jpc_header) ) == 0
620 : || memcmp( poOpenInfo->pabyHeader + 4, jp2_box_jp,
621 : sizeof(jp2_box_jp) ) == 0
622 : /* PGX file*/
623 : || (memcmp( poOpenInfo->pabyHeader, "PG", 2) == 0 &&
624 : (poOpenInfo->pabyHeader[2] == ' ' || poOpenInfo->pabyHeader[2] == '\t') &&
625 : (memcmp( poOpenInfo->pabyHeader + 3, "ML", 2) == 0 ||
626 : memcmp( poOpenInfo->pabyHeader + 3, "LM", 2) == 0))) )
627 39 : return TRUE;
628 :
629 : else
630 9791 : return FALSE;
631 : }
632 :
633 : /************************************************************************/
634 : /* Open() */
635 : /************************************************************************/
636 :
637 1594 : GDALDataset *JPEG2000Dataset::Open( GDALOpenInfo * poOpenInfo )
638 :
639 : {
640 : int iFormat;
641 1594 : char *pszFormatName = NULL;
642 : jas_stream_t *sS;
643 :
644 1594 : if (!Identify(poOpenInfo))
645 1555 : return NULL;
646 :
647 39 : JPEG2000Init();
648 39 : if( !(sS = JPEG2000_VSIL_fopen( poOpenInfo->pszFilename, "rb" )) )
649 : {
650 0 : return NULL;
651 : }
652 :
653 39 : iFormat = jas_image_getfmt( sS );
654 39 : if ( !(pszFormatName = jas_image_fmttostr( iFormat )) )
655 : {
656 0 : jas_stream_close( sS );
657 0 : return NULL;
658 : }
659 39 : if ( strlen( pszFormatName ) < 3 ||
660 : (!EQUALN( pszFormatName, "jp2", 3 ) &&
661 : !EQUALN( pszFormatName, "jpc", 3 ) &&
662 : !EQUALN( pszFormatName, "pgx", 3 )) )
663 : {
664 : CPLDebug( "JPEG2000", "JasPer reports file is format type `%s'.",
665 0 : pszFormatName );
666 0 : jas_stream_close( sS );
667 0 : return NULL;
668 : }
669 :
670 : /* -------------------------------------------------------------------- */
671 : /* Confirm the requested access is supported. */
672 : /* -------------------------------------------------------------------- */
673 39 : if( poOpenInfo->eAccess == GA_Update )
674 : {
675 0 : jas_stream_close(sS);
676 : CPLError( CE_Failure, CPLE_NotSupported,
677 : "The JPEG2000 driver does not support update access to existing"
678 0 : " datasets.\n" );
679 0 : return NULL;
680 : }
681 : /* -------------------------------------------------------------------- */
682 : /* Create a corresponding GDALDataset. */
683 : /* -------------------------------------------------------------------- */
684 : JPEG2000Dataset *poDS;
685 39 : int *paiDepth = NULL, *pabSignedness = NULL;
686 : int iBand;
687 :
688 39 : poDS = new JPEG2000Dataset();
689 :
690 39 : poDS->psStream = sS;
691 39 : poDS->iFormat = iFormat;
692 :
693 39 : if ( EQUALN( pszFormatName, "jp2", 3 ) )
694 : {
695 : // XXX: Hack to read JP2 boxes from input file. JasPer hasn't public
696 : // API call for such things, so we will use internal JasPer functions.
697 : jp2_box_t *box;
698 26 : box = 0;
699 248 : while ( ( box = jp2_box_get(poDS->psStream) ) )
700 : {
701 196 : switch (box->type)
702 : {
703 : case JP2_BOX_IHDR:
704 26 : poDS->nBands = box->data.ihdr.numcmpts;
705 26 : poDS->nRasterXSize = box->data.ihdr.width;
706 26 : poDS->nRasterYSize = box->data.ihdr.height;
707 : CPLDebug( "JPEG2000",
708 : "IHDR box found. Dump: "
709 : "width=%d, height=%d, numcmpts=%d, bpp=%d",
710 : (int)box->data.ihdr.width, (int)box->data.ihdr.height,
711 26 : (int)box->data.ihdr.numcmpts, (box->data.ihdr.bpc & 0x7F) + 1 );
712 : /* ISO/IEC 15444-1:2004 §I.5.3.1 specifies that 255 means that all */
713 : /* components have not the same bit depth and/or sign and that a */
714 : /* BPCC box must then follow to specify them for each component */
715 26 : if ( box->data.ihdr.bpc != 255 )
716 : {
717 26 : paiDepth = (int *)CPLMalloc(poDS->nBands * sizeof(int));
718 26 : pabSignedness = (int *)CPLMalloc(poDS->nBands * sizeof(int));
719 59 : for ( iBand = 0; iBand < poDS->nBands; iBand++ )
720 : {
721 33 : paiDepth[iBand] = (box->data.ihdr.bpc & 0x7F) + 1;
722 33 : pabSignedness[iBand] = box->data.ihdr.bpc >> 7;
723 : CPLDebug( "JPEG2000",
724 : "Component %d: bpp=%d, signedness=%d",
725 33 : iBand, paiDepth[iBand], pabSignedness[iBand] );
726 : }
727 : }
728 26 : break;
729 :
730 : case JP2_BOX_BPCC:
731 0 : CPLDebug( "JPEG2000", "BPCC box found. Dump:" );
732 0 : if ( !paiDepth && !pabSignedness )
733 : {
734 : paiDepth = (int *)
735 0 : CPLMalloc( box->data.bpcc.numcmpts * sizeof(int) );
736 : pabSignedness = (int *)
737 0 : CPLMalloc( box->data.bpcc.numcmpts * sizeof(int) );
738 0 : for( iBand = 0; iBand < (int)box->data.bpcc.numcmpts; iBand++ )
739 : {
740 0 : paiDepth[iBand] = (box->data.bpcc.bpcs[iBand] & 0x7F) + 1;
741 0 : pabSignedness[iBand] = box->data.bpcc.bpcs[iBand] >> 7;
742 : CPLDebug( "JPEG2000",
743 : "Component %d: bpp=%d, signedness=%d",
744 0 : iBand, paiDepth[iBand], pabSignedness[iBand] );
745 : }
746 : }
747 0 : break;
748 :
749 : case JP2_BOX_PCLR:
750 : CPLDebug( "JPEG2000",
751 : "PCLR box found. Dump: number of LUT entries=%d, "
752 : "number of resulting channels=%d",
753 2 : (int)box->data.pclr.numlutents, box->data.pclr.numchans );
754 2 : poDS->nBands = box->data.pclr.numchans;
755 2 : if ( paiDepth )
756 2 : CPLFree( paiDepth );
757 2 : if ( pabSignedness )
758 2 : CPLFree( pabSignedness );
759 : paiDepth = (int *)
760 2 : CPLMalloc( box->data.pclr.numchans * sizeof(int) );
761 : pabSignedness = (int *)
762 2 : CPLMalloc( box->data.pclr.numchans * sizeof(int) );
763 8 : for( iBand = 0; iBand < (int)box->data.pclr.numchans; iBand++ )
764 : {
765 6 : paiDepth[iBand] = (box->data.pclr.bpc[iBand] & 0x7F) + 1;
766 6 : pabSignedness[iBand] = box->data.pclr.bpc[iBand] >> 7;
767 : CPLDebug( "JPEG2000",
768 : "Component %d: bpp=%d, signedness=%d",
769 6 : iBand, paiDepth[iBand], pabSignedness[iBand] );
770 : }
771 : break;
772 : }
773 196 : jp2_box_destroy( box );
774 196 : box = 0;
775 : }
776 26 : if( !paiDepth || !pabSignedness )
777 : {
778 0 : delete poDS;
779 0 : CPLDebug( "JPEG2000", "Unable to read JP2 header boxes.\n" );
780 0 : return NULL;
781 : }
782 26 : if ( jas_stream_rewind( poDS->psStream ) < 0 )
783 : {
784 0 : delete poDS;
785 0 : CPLDebug( "JPEG2000", "Unable to rewind input stream.\n" );
786 0 : return NULL;
787 : }
788 : }
789 : else
790 : {
791 13 : if ( !poDS->DecodeImage() )
792 : {
793 1 : delete poDS;
794 1 : return NULL;
795 : }
796 :
797 12 : poDS->nBands = jas_image_numcmpts( poDS->psImage );
798 12 : poDS->nRasterXSize = jas_image_cmptwidth( poDS->psImage, 0 );
799 12 : poDS->nRasterYSize = jas_image_cmptheight( poDS->psImage, 0 );
800 12 : paiDepth = (int *)CPLMalloc( poDS->nBands * sizeof(int) );
801 12 : pabSignedness = (int *)CPLMalloc( poDS->nBands * sizeof(int) );
802 32 : for ( iBand = 0; iBand < poDS->nBands; iBand++ )
803 : {
804 20 : paiDepth[iBand] = jas_image_cmptprec( poDS->psImage, iBand );
805 20 : pabSignedness[iBand] = jas_image_cmptsgnd( poDS->psImage, iBand );
806 : }
807 : }
808 :
809 38 : if ( !GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize) ||
810 : !GDALCheckBandCount(poDS->nBands, 0) )
811 : {
812 0 : CPLFree( paiDepth );
813 0 : CPLFree( pabSignedness );
814 0 : delete poDS;
815 0 : return NULL;
816 : }
817 :
818 : /* -------------------------------------------------------------------- */
819 :
820 : /* Create band information objects. */
821 : /* -------------------------------------------------------------------- */
822 190 : for( iBand = 1; iBand <= poDS->nBands; iBand++ )
823 : {
824 : poDS->SetBand( iBand, new JPEG2000RasterBand( poDS, iBand,
825 57 : paiDepth[iBand - 1], pabSignedness[iBand - 1] ) );
826 :
827 : }
828 :
829 38 : if ( paiDepth )
830 38 : CPLFree( paiDepth );
831 38 : if ( pabSignedness )
832 38 : CPLFree( pabSignedness );
833 :
834 : /* -------------------------------------------------------------------- */
835 : /* Check for georeferencing information. */
836 : /* -------------------------------------------------------------------- */
837 38 : GDALJP2Metadata oJP2Geo;
838 :
839 38 : if( oJP2Geo.ReadAndParse( poOpenInfo->pszFilename ) )
840 : {
841 10 : if ( poDS->pszProjection )
842 10 : CPLFree( poDS->pszProjection );
843 10 : poDS->pszProjection = CPLStrdup(oJP2Geo.pszProjection);
844 10 : poDS->bGeoTransformValid = oJP2Geo.bHaveGeoTransform;
845 : memcpy( poDS->adfGeoTransform, oJP2Geo.adfGeoTransform,
846 10 : sizeof(double) * 6 );
847 10 : poDS->nGCPCount = oJP2Geo.nGCPCount;
848 : poDS->pasGCPList =
849 10 : GDALDuplicateGCPs( oJP2Geo.nGCPCount, oJP2Geo.pasGCPList );
850 : }
851 :
852 : /* -------------------------------------------------------------------- */
853 : /* Initialize any PAM information. */
854 : /* -------------------------------------------------------------------- */
855 38 : poDS->SetDescription( poOpenInfo->pszFilename );
856 38 : poDS->TryLoadXML();
857 :
858 : /* -------------------------------------------------------------------- */
859 : /* Check for overviews. */
860 : /* -------------------------------------------------------------------- */
861 38 : poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename );
862 :
863 38 : return( poDS );
864 : }
865 :
866 : /************************************************************************/
867 : /* JPEG2000CreateCopy() */
868 : /************************************************************************/
869 :
870 : static GDALDataset *
871 : JPEG2000CreateCopy( const char * pszFilename, GDALDataset *poSrcDS,
872 : int bStrict, char ** papszOptions,
873 10 : GDALProgressFunc pfnProgress, void * pProgressData )
874 :
875 : {
876 10 : int nBands = poSrcDS->GetRasterCount();
877 10 : int nXSize = poSrcDS->GetRasterXSize();
878 10 : int nYSize = poSrcDS->GetRasterYSize();
879 :
880 10 : if( nBands == 0 )
881 : {
882 : CPLError( CE_Failure, CPLE_NotSupported,
883 1 : "Unable to export files with zero bands." );
884 1 : return NULL;
885 : }
886 :
887 9 : if (poSrcDS->GetRasterBand(1)->GetColorTable() != NULL)
888 : {
889 : CPLError( (bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported,
890 : "JPEG2000 driver ignores color table. "
891 : "The source raster band will be considered as grey level.\n"
892 0 : "Consider using color table expansion (-expand option in gdal_translate)\n");
893 0 : if (bStrict)
894 0 : return NULL;
895 : }
896 :
897 9 : if( !pfnProgress( 0.0, NULL, pProgressData ) )
898 0 : return NULL;
899 :
900 : /* -------------------------------------------------------------------- */
901 : /* Create the dataset. */
902 : /* -------------------------------------------------------------------- */
903 : int iBand;
904 : jas_stream_t *psStream;
905 : jas_image_t *psImage;
906 :
907 9 : JPEG2000Init();
908 9 : const char* pszAccess = EQUALN(pszFilename, "/vsisubfile/", 12) ? "r+b" : "w+b";
909 9 : if( !(psStream = JPEG2000_VSIL_fopen( pszFilename, pszAccess) ) )
910 : {
911 : CPLError( CE_Failure, CPLE_FileIO, "Unable to create file %s.\n",
912 0 : pszFilename );
913 0 : return NULL;
914 : }
915 :
916 9 : if ( !(psImage = jas_image_create0()) )
917 : {
918 : CPLError( CE_Failure, CPLE_OutOfMemory, "Unable to create image %s.\n",
919 0 : pszFilename );
920 0 : return NULL;
921 : }
922 :
923 : /* -------------------------------------------------------------------- */
924 : /* Loop over image, copying image data. */
925 : /* -------------------------------------------------------------------- */
926 : GDALRasterBand *poBand;
927 : GUInt32 *paiScanline;
928 : int iLine, iPixel;
929 9 : CPLErr eErr = CE_None;
930 : jas_matrix_t *psMatrix;
931 : jas_image_cmptparm_t *sComps; // Array of pointers to image components
932 :
933 : sComps = (jas_image_cmptparm_t*)
934 9 : CPLMalloc( nBands * sizeof(jas_image_cmptparm_t) );
935 :
936 9 : if ( !(psMatrix = jas_matrix_create( 1, nXSize )) )
937 : {
938 : CPLError( CE_Failure, CPLE_OutOfMemory,
939 0 : "Unable to create matrix with size %dx%d.\n", 1, nYSize );
940 0 : CPLFree( sComps );
941 0 : jas_image_destroy( psImage );
942 0 : return NULL;
943 : }
944 : paiScanline = (GUInt32 *) CPLMalloc( nXSize *
945 9 : GDALGetDataTypeSize(GDT_UInt32) / 8 );
946 :
947 23 : for ( iBand = 0; iBand < nBands; iBand++ )
948 : {
949 14 : poBand = poSrcDS->GetRasterBand( iBand + 1);
950 :
951 14 : sComps[iBand].tlx = sComps[iBand].tly = 0;
952 14 : sComps[iBand].hstep = sComps[iBand].vstep = 1;
953 14 : sComps[iBand].width = nXSize;
954 14 : sComps[iBand].height = nYSize;
955 14 : sComps[iBand].prec = GDALGetDataTypeSize( poBand->GetRasterDataType() );
956 14 : switch ( poBand->GetRasterDataType() )
957 : {
958 : case GDT_Int16:
959 : case GDT_Int32:
960 : case GDT_Float32:
961 : case GDT_Float64:
962 2 : sComps[iBand].sgnd = 1;
963 2 : break;
964 : case GDT_Byte:
965 : case GDT_UInt16:
966 : case GDT_UInt32:
967 : default:
968 12 : sComps[iBand].sgnd = 0;
969 : break;
970 : }
971 14 : jas_image_addcmpt(psImage, iBand, sComps);
972 :
973 354 : for( iLine = 0; eErr == CE_None && iLine < nYSize; iLine++ )
974 : {
975 : eErr = poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1,
976 : paiScanline, nXSize, 1, GDT_UInt32,
977 340 : sizeof(GUInt32), sizeof(GUInt32) * nXSize );
978 22140 : for ( iPixel = 0; iPixel < nXSize; iPixel++ )
979 21800 : jas_matrix_setv( psMatrix, iPixel, paiScanline[iPixel] );
980 :
981 340 : if( (jas_image_writecmpt(psImage, iBand, 0, iLine,
982 : nXSize, 1, psMatrix)) < 0 )
983 : {
984 : CPLError( CE_Failure, CPLE_AppDefined,
985 : "Unable to write scanline %d of the component %d.\n",
986 0 : iLine, iBand );
987 0 : jas_matrix_destroy( psMatrix );
988 0 : CPLFree( paiScanline );
989 0 : CPLFree( sComps );
990 0 : jas_image_destroy( psImage );
991 0 : return NULL;
992 : }
993 :
994 340 : if( eErr == CE_None &&
995 : !pfnProgress( ((iLine + 1) + iBand * nYSize) /
996 : ((double) nYSize * nBands),
997 : NULL, pProgressData) )
998 : {
999 0 : eErr = CE_Failure;
1000 : CPLError( CE_Failure, CPLE_UserInterrupt,
1001 0 : "User terminated CreateCopy()" );
1002 : }
1003 : }
1004 : }
1005 :
1006 : /* -------------------------------------------------------------------- */
1007 : /* Read compression parameters and encode the image. */
1008 : /* -------------------------------------------------------------------- */
1009 : int i, j;
1010 9 : const int OPTSMAX = 4096;
1011 : const char *pszFormatName;
1012 : char pszOptionBuf[OPTSMAX + 1];
1013 :
1014 : const char *apszComprOptions[]=
1015 : {
1016 : "imgareatlx",
1017 : "imgareatly",
1018 : "tilegrdtlx",
1019 : "tilegrdtly",
1020 : "tilewidth",
1021 : "tileheight",
1022 : "prcwidth",
1023 : "prcheight",
1024 : "cblkwidth",
1025 : "cblkheight",
1026 : "mode",
1027 : "rate",
1028 : "ilyrrates",
1029 : "prg",
1030 : "numrlvls",
1031 : "sop",
1032 : "eph",
1033 : "lazy",
1034 : "termall",
1035 : "segsym",
1036 : "vcausal",
1037 : "pterm",
1038 : "resetprob",
1039 : "numgbits",
1040 : NULL
1041 9 : };
1042 :
1043 9 : pszFormatName = CSLFetchNameValue( papszOptions, "FORMAT" );
1044 9 : if ( !pszFormatName ||
1045 : (!EQUALN( pszFormatName, "jp2", 3 ) &&
1046 : !EQUALN( pszFormatName, "jpc", 3 ) ) )
1047 8 : pszFormatName = "jp2";
1048 :
1049 9 : pszOptionBuf[0] = '\0';
1050 9 : if ( papszOptions )
1051 : {
1052 1 : CPLDebug( "JPEG2000", "User supplied parameters:" );
1053 2 : for ( i = 0; papszOptions[i] != NULL; i++ )
1054 : {
1055 1 : CPLDebug( "JPEG2000", "%s\n", papszOptions[i] );
1056 25 : for ( j = 0; apszComprOptions[j] != NULL; j++ )
1057 24 : if( EQUALN( apszComprOptions[j], papszOptions[i],
1058 : strlen(apszComprOptions[j]) ) )
1059 : {
1060 : int m, n;
1061 :
1062 0 : n = strlen( pszOptionBuf );
1063 0 : m = n + strlen( papszOptions[i] ) + 1;
1064 0 : if ( m > OPTSMAX )
1065 0 : break;
1066 0 : if ( n > 0 )
1067 : {
1068 0 : strcat( pszOptionBuf, "\n" );
1069 : }
1070 0 : strcat( pszOptionBuf, papszOptions[i] );
1071 : }
1072 : }
1073 : }
1074 9 : CPLDebug( "JPEG2000", "Parameters, delivered to the JasPer library:" );
1075 9 : CPLDebug( "JPEG2000", "%s", pszOptionBuf );
1076 :
1077 9 : if ( nBands == 1 ) // Grayscale
1078 : {
1079 7 : jas_image_setclrspc( psImage, JAS_CLRSPC_SGRAY );
1080 7 : jas_image_setcmpttype( psImage, 0, JAS_IMAGE_CT_GRAY_Y );
1081 : }
1082 4 : else if ( nBands == 3 || nBands == 4 ) // Assume as RGB(A)
1083 : {
1084 2 : jas_image_setclrspc( psImage, JAS_CLRSPC_SRGB );
1085 9 : for ( iBand = 0; iBand < nBands; iBand++ )
1086 : {
1087 7 : poBand = poSrcDS->GetRasterBand( iBand + 1);
1088 7 : switch ( poBand->GetColorInterpretation() )
1089 : {
1090 : case GCI_RedBand:
1091 2 : jas_image_setcmpttype( psImage, iBand, JAS_IMAGE_CT_RGB_R );
1092 2 : break;
1093 : case GCI_GreenBand:
1094 2 : jas_image_setcmpttype( psImage, iBand, JAS_IMAGE_CT_RGB_G );
1095 2 : break;
1096 : case GCI_BlueBand:
1097 2 : jas_image_setcmpttype( psImage, iBand, JAS_IMAGE_CT_RGB_B );
1098 2 : break;
1099 : case GCI_AlphaBand:
1100 1 : jas_image_setcmpttype( psImage, iBand, JAS_IMAGE_CT_OPACITY );
1101 1 : break;
1102 : default:
1103 0 : jas_image_setcmpttype( psImage, iBand, JAS_IMAGE_CT_UNKNOWN );
1104 : break;
1105 : }
1106 : }
1107 : }
1108 : else // Unknown
1109 : {
1110 0 : jas_image_setclrspc( psImage, JAS_CLRSPC_UNKNOWN );
1111 0 : for ( iBand = 0; iBand < nBands; iBand++ )
1112 0 : jas_image_setcmpttype( psImage, iBand, JAS_IMAGE_CT_UNKNOWN );
1113 : }
1114 :
1115 : /* -------------------------------------------------------------------- */
1116 : /* Set the GeoTIFF box if georeferencing is available, and this */
1117 : /* is a JP2 file. */
1118 : /* -------------------------------------------------------------------- */
1119 9 : if ( EQUALN( pszFormatName, "jp2", 3 ) )
1120 : {
1121 : #ifdef HAVE_JASPER_UUID
1122 : double adfGeoTransform[6];
1123 : if( ((poSrcDS->GetGeoTransform(adfGeoTransform) == CE_None
1124 : && (adfGeoTransform[0] != 0.0
1125 : || adfGeoTransform[1] != 1.0
1126 : || adfGeoTransform[2] != 0.0
1127 : || adfGeoTransform[3] != 0.0
1128 : || adfGeoTransform[4] != 0.0
1129 : || ABS(adfGeoTransform[5]) != 1.0))
1130 : || poSrcDS->GetGCPCount() > 0) )
1131 : {
1132 : GDALJP2Metadata oJP2Geo;
1133 :
1134 : if( poSrcDS->GetGCPCount() > 0 )
1135 : {
1136 : oJP2Geo.SetProjection( poSrcDS->GetGCPProjection() );
1137 : oJP2Geo.SetGCPs( poSrcDS->GetGCPCount(), poSrcDS->GetGCPs() );
1138 : }
1139 : else
1140 : {
1141 : oJP2Geo.SetProjection( poSrcDS->GetProjectionRef() );
1142 : oJP2Geo.SetGeoTransform( adfGeoTransform );
1143 : }
1144 :
1145 : GDALJP2Box *poBox = oJP2Geo.CreateJP2GeoTIFF();
1146 : jp2_box_t *box = jp2_box_create( JP2_BOX_UUID );
1147 : memcpy( box->data.uuid.uuid, poBox->GetUUID(), 16 );
1148 : box->data.uuid.datalen = poBox->GetDataLength() - 16;
1149 : box->data.uuid.data =
1150 : (uint_fast8_t *)jas_malloc( poBox->GetDataLength() - 16 );
1151 : memcpy( box->data.uuid.data, poBox->GetWritableData() + 16,
1152 : poBox->GetDataLength() - 16 );
1153 : delete poBox;
1154 : poBox = NULL;
1155 :
1156 : if ( jp2_encode_uuid( psImage, psStream, pszOptionBuf, box) < 0 )
1157 : {
1158 : CPLError( CE_Failure, CPLE_FileIO,
1159 : "Unable to encode image %s.", pszFilename );
1160 : jp2_box_destroy( box );
1161 : jas_matrix_destroy( psMatrix );
1162 : CPLFree( paiScanline );
1163 : CPLFree( sComps );
1164 : jas_image_destroy( psImage );
1165 : return NULL;
1166 : }
1167 : jp2_box_destroy( box );
1168 : }
1169 : else
1170 : {
1171 : #endif
1172 8 : if ( jp2_encode( psImage, psStream, pszOptionBuf) < 0 )
1173 : {
1174 : CPLError( CE_Failure, CPLE_FileIO,
1175 0 : "Unable to encode image %s.", pszFilename );
1176 0 : jas_matrix_destroy( psMatrix );
1177 0 : CPLFree( paiScanline );
1178 0 : CPLFree( sComps );
1179 0 : jas_image_destroy( psImage );
1180 0 : return NULL;
1181 : }
1182 : #ifdef HAVE_JASPER_UUID
1183 : }
1184 : #endif
1185 : }
1186 : else // Write JPC code stream
1187 : {
1188 1 : if ( jpc_encode(psImage, psStream, pszOptionBuf) < 0 )
1189 : {
1190 : CPLError( CE_Failure, CPLE_FileIO,
1191 0 : "Unable to encode image %s.\n", pszFilename );
1192 0 : jas_matrix_destroy( psMatrix );
1193 0 : CPLFree( paiScanline );
1194 0 : CPLFree( sComps );
1195 0 : jas_image_destroy( psImage );
1196 0 : return NULL;
1197 : }
1198 : }
1199 :
1200 9 : jas_stream_flush( psStream );
1201 :
1202 9 : jas_matrix_destroy( psMatrix );
1203 9 : CPLFree( paiScanline );
1204 9 : CPLFree( sComps );
1205 9 : jas_image_destroy( psImage );
1206 9 : if ( jas_stream_close( psStream ) )
1207 : {
1208 : CPLError( CE_Failure, CPLE_FileIO, "Unable to close file %s.\n",
1209 0 : pszFilename );
1210 0 : return NULL;
1211 : }
1212 :
1213 : /* -------------------------------------------------------------------- */
1214 : /* Do we need a world file? */
1215 : /* -------------------------------------------------------------------- */
1216 9 : if( CSLFetchBoolean( papszOptions, "WORLDFILE", FALSE ) )
1217 : {
1218 : double adfGeoTransform[6];
1219 :
1220 0 : poSrcDS->GetGeoTransform( adfGeoTransform );
1221 0 : GDALWriteWorldFile( pszFilename, "wld", adfGeoTransform );
1222 : }
1223 :
1224 : /* -------------------------------------------------------------------- */
1225 : /* Re-open dataset, and copy any auxilary pam information. */
1226 : /* -------------------------------------------------------------------- */
1227 9 : GDALOpenInfo oOpenInfo(pszFilename, GA_ReadOnly);
1228 9 : GDALPamDataset *poDS = (GDALPamDataset*) JPEG2000Dataset::Open(&oOpenInfo);
1229 :
1230 9 : if( poDS )
1231 9 : poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT );
1232 :
1233 9 : return poDS;
1234 : }
1235 :
1236 : /************************************************************************/
1237 : /* GDALRegister_JPEG2000() */
1238 : /************************************************************************/
1239 :
1240 409 : void GDALRegister_JPEG2000()
1241 :
1242 : {
1243 : GDALDriver *poDriver;
1244 :
1245 409 : if (! GDAL_CHECK_VERSION("JPEG2000 driver"))
1246 0 : return;
1247 :
1248 409 : if( GDALGetDriverByName( "JPEG2000" ) == NULL )
1249 : {
1250 392 : poDriver = new GDALDriver();
1251 :
1252 392 : poDriver->SetDescription( "JPEG2000" );
1253 : poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
1254 392 : "JPEG-2000 part 1 (ISO/IEC 15444-1)" );
1255 : poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC,
1256 392 : "frmt_jpeg2000.html" );
1257 : poDriver->SetMetadataItem( GDAL_DMD_CREATIONDATATYPES,
1258 392 : "Byte Int16 UInt16 Int32 UInt32" );
1259 392 : poDriver->SetMetadataItem( GDAL_DMD_MIMETYPE, "image/jp2" );
1260 392 : poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "jp2" );
1261 :
1262 392 : poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" );
1263 :
1264 392 : poDriver->pfnIdentify = JPEG2000Dataset::Identify;
1265 392 : poDriver->pfnOpen = JPEG2000Dataset::Open;
1266 392 : poDriver->pfnCreateCopy = JPEG2000CreateCopy;
1267 :
1268 392 : GetGDALDriverManager()->RegisterDriver( poDriver );
1269 : }
1270 : }
1271 :
|