]> Creatis software - gdcm.git/blob - src/gdcmJpeg2000.cxx
ENH: Remove debug printf
[gdcm.git] / src / gdcmJpeg2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/07 20:21:57 $
7   Version:   $Revision: 1.23 $
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 raw raw
31  * @param inputdata inputdata
32  * @param inputlength inputlength 
33  * @return 1 on success, 0 on error
34  */
35
36 bool gdcm_read_JPEG2000_file (void* raw, char *inputdata, size_t inputlength)
37 {
38   jas_init(); //important...
39   jas_stream_t *jasStream = 
40     jas_stream_memopen((char *)inputdata, inputlength);
41     
42   int fmtid;
43   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
44     {
45     gdcmErrorMacro("unknown image format");
46     return false;
47     }
48
49   // Decode the image. 
50   jas_image_t *jasImage = NULL;
51   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
52     {
53     gdcmErrorMacro("cannot decode image");
54     return false;
55     }
56
57   // close the stream. 
58   jas_stream_close(jasStream);
59   int numcmpts = jas_image_numcmpts(jasImage);
60   int width = jas_image_cmptwidth(jasImage, 0);
61   int height = jas_image_cmptheight(jasImage, 0);
62   int prec = jas_image_cmptprec(jasImage, 0);
63   int i, j, k;
64   char *fmtname = jas_image_fmttostr(fmtid);
65
66   // The following should serioulsy be rewritten I cannot belive we need to
67   // do a per pixel decompression, there should be a way to read a full
68   // scanline...
69   if (prec == 8)
70     {
71     uint8_t *data8 = (uint8_t*)raw;
72     for ( i = 0; i < height; i++)
73       for ( j = 0; j < width; j++)
74         for ( k= 0; k < numcmpts; k++)
75           *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
76     }
77   else if (prec <= 16)
78     {
79     uint16_t *data16 = (uint16_t*)raw;
80     for ( i = 0; i < height; i++) 
81       for ( j = 0; j < width; j++) 
82         for ( k= 0; k < numcmpts; k++)
83           *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
84     }
85   else if (prec <= 32)
86     {
87     uint32_t *data32 = (uint32_t*)raw;
88     for ( i = 0; i < height; i++) 
89       for ( j = 0; j < width; j++) 
90         for ( k= 0; k < numcmpts; k++)
91           *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
92     }
93
94   jas_image_destroy(jasImage);
95   jas_image_clearfmts();
96
97   //FIXME
98   //delete the jpeg temp buffer
99   delete[] inputdata;
100
101   return true;
102 }
103
104 //-----------------------------------------------------------------------------
105 } // end namespace gdcm
106