]> Creatis software - gdcm.git/blob - src/gdcmJpeg2000.cxx
ENH: Adding initial implementation for jasper/jpeg2000 support
[gdcm.git] / src / gdcmJpeg2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/05/22 18:38:52 $
7   Version:   $Revision: 1.20 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFileHelper.h"
19 #include "gdcmDebug.h"
20
21 #include <iostream>
22 #include <fstream>
23 #include <jasper/jasper.h>
24
25 namespace gdcm 
26 {
27 //-----------------------------------------------------------------------------
28  /**
29  * \brief   routine for JPEG decompression 
30  * @param fp pointer to an already open file descriptor 
31  *                      JPEG2000 encoded image
32  * @param image_buffer to receive uncompressed pixels
33  * @return 1 on success, 0 on error
34  * @warning : not yet made
35  */
36
37 bool gdcm_read_JPEG2000_file (std::ifstream* fp, void* raw, size_t inputlength)
38 {
39   jas_init(); //important...
40   // FIXME this is really ugly but it seems I have to load the complete
41   // jpeg2000 stream to use jasper:
42   uint8_t *inputdata = new uint8_t[inputlength];
43   //fp is already 'seek' to proper pos
44   fp->read((char*)inputdata, inputlength);
45   jas_stream_t *jasStream = jas_stream_memopen((char *)inputdata, inputlength);
46     
47   int fmtid;
48   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
49     {
50     gdcmErrorMacro("unknown image format");
51     return false;
52     }
53
54   // Decode the image. 
55   jas_image_t *jasImage = NULL;
56   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
57     {
58     gdcmErrorMacro("cannot decode image");
59     return false;
60     }
61
62   // close the stream. 
63   jas_stream_close(jasStream);
64   int numcmpts = jas_image_numcmpts(jasImage);
65   int width = jas_image_cmptwidth(jasImage, 0);
66   int height = jas_image_cmptheight(jasImage, 0);
67   int prec = jas_image_cmptprec(jasImage, 0);
68   int i, j, k;
69   char *fmtname = jas_image_fmttostr(fmtid);
70   printf("%s %d %d %d %d %ld\n", fmtname, numcmpts, width, height, prec, (long) jas_image_rawsize(jasImage));
71
72   // The following should serioulsy be rewritten I cannot belive we need to
73   // do a per pixel decompression, there should be a way to read a full
74   // scanline...
75   if (prec == 8)
76     {
77     uint8_t *data8 = (uint8_t*)raw;
78     for ( i = 0; i < height; i++)
79       for ( j = 0; j < width; j++)
80         for ( k= 0; k < numcmpts; k++)
81           *data8++ =
82             (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
83     }
84   else if (prec <= 16)
85     {
86     uint16_t *data16 = (uint16_t*)raw;
87     for ( i = 0; i < height; i++) 
88       for ( j = 0; j < width; j++) 
89         for ( k= 0; k < numcmpts; k++)
90           *data16++ = 
91             (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
92     }
93   else if (prec <= 32)
94     {
95     uint32_t *data32 = (uint32_t*)raw;
96     for ( i = 0; i < height; i++) 
97       for ( j = 0; j < width; j++) 
98         for ( k= 0; k < numcmpts; k++)
99           *data32++ = 
100             (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
101     }
102
103   jas_image_destroy(jasImage);
104   jas_image_clearfmts();
105
106   //FIXME
107   //delete the jpeg temp buffer
108   delete[] inputdata;
109
110   return true;
111 }
112
113 //-----------------------------------------------------------------------------
114 } // end namespace gdcm
115