]> Creatis software - gdcm.git/blob - src/gdcmJpeg2000.cxx
remove useless assignation, to avoid warning
[gdcm.git] / src / gdcmJpeg2000.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmJpeg2000.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/09/20 09:24:10 $
7   Version:   $Revision: 1.28 $
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 #if 0
39   std::cerr << "Inputlenght=" << inputlength << std::endl;
40   std::ofstream out("/tmp/jpeg2000.jpc", std::ios::binary);
41   out.write((char*)inputdata,inputlength);
42   out.close();
43 #endif
44   jas_init(); //important...
45   jas_stream_t *jasStream = 
46     jas_stream_memopen((char *)inputdata, inputlength);
47     
48   int fmtid;
49   if ((fmtid = jas_image_getfmt(jasStream)) < 0) 
50     {
51     gdcmErrorMacro("unknown image format");
52     return false;
53     }
54
55   // Decode the image. 
56   jas_image_t *jasImage /* = NULL*/; // Useless assignation
57   if (!(jasImage = jas_image_decode(jasStream, fmtid, 0))) 
58     {
59     gdcmErrorMacro("cannot decode image");
60     return false;
61     }
62
63   // close the stream. 
64   jas_stream_close(jasStream);
65   int numcmpts = jas_image_numcmpts(jasImage);
66   int width = jas_image_cmptwidth(jasImage, 0);
67   int height = jas_image_cmptheight(jasImage, 0);
68   int prec = jas_image_cmptprec(jasImage, 0);
69   int i, j, k;
70
71   // The following should serioulsy be rewritten I cannot believe we need to
72   // do a per pixel decompression, there should be a way to read a full
73   // scanline...
74   if (prec == 8)
75     {
76     uint8_t *data8 = (uint8_t*)raw;
77     for ( i = 0; i < height; i++)
78       for ( j = 0; j < width; j++)
79         for ( k= 0; k < numcmpts; k++)
80           *data8++ = (uint8_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
81     }
82   else if (prec <= 16)
83     {
84     uint16_t *data16 = (uint16_t*)raw;
85     for ( i = 0; i < height; i++) 
86       for ( j = 0; j < width; j++) 
87         for ( k= 0; k < numcmpts; k++)
88           *data16++ = (uint16_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
89     }
90   else if (prec <= 32)
91     {
92     uint32_t *data32 = (uint32_t*)raw;
93     for ( i = 0; i < height; i++) 
94       for ( j = 0; j < width; j++) 
95         for ( k= 0; k < numcmpts; k++)
96           *data32++ = (uint32_t)(jas_image_readcmptsample(jasImage, k, j ,i ));
97     }
98
99   jas_image_destroy(jasImage);
100   jas_image_clearfmts();
101
102   //FIXME
103   //delete the jpeg temp buffer
104 #if 0
105   std::ofstream rawout("/tmp/jpeg2000.raw");
106   rawout.write((char*)raw,height*width*numcmpts*((prec+4)/8));
107   rawout.close();
108 #endif
109   delete[] inputdata;
110
111   return true;
112 }
113
114 //-----------------------------------------------------------------------------
115 } // end namespace gdcm
116