1 /*=========================================================================
4 Module: $RCSfile: gdcmPixelConvert.cxx,v $
6 Date: $Date: 2004/09/29 17:33:17 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
19 ////// #include <stdio.h>
20 #define str2num(str, typeNum) *((typeNum *)(str))
22 #include "gdcmDebug.h"
23 #include "gdcmPixelConvert.h"
26 //-----------------------------------------------------------------------------
27 // Constructor / Destructor
28 gdcmPixelConvert::gdcmPixelConvert()
36 void gdcmPixelConvert::Squeeze()
42 delete [] Uncompressed;
46 gdcmPixelConvert::~gdcmPixelConvert()
51 void gdcmPixelConvert::AllocateRGB()
56 RGB = new uint8_t[RGBSize];
59 void gdcmPixelConvert::AllocateUncompressed()
62 delete [] Uncompressed;
64 Uncompressed = new uint8_t[ UncompressedSize ];
68 * \brief Read from file a 12 bits per pixel image and uncompress it
69 * into a 16 bits per pixel image.
71 bool gdcmPixelConvert::ReadAndUncompress12Bits( FILE* filePointer,
72 size_t uncompressedSize,
75 SetUncompressedSize( uncompressedSize );
76 AllocateUncompressed();
78 uint16_t* pdestination = (uint16_t*)Uncompressed;
80 for(int p = 0; p < PixelNumber; p += 2 )
82 // 2 pixels 12bit = [0xABCDEF]
83 // 2 pixels 16bit = [0x0ABD] + [0x0FCE]
86 ItemRead = fread( &b0, 1, 1, filePointer);
91 ItemRead = fread( &b1, 1, 1, filePointer);
96 ItemRead = fread( &b2, 1, 1, filePointer);
102 //Two steps are necessary to please VC++
103 *pdestination++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
105 *pdestination++ = ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
108 /// \todo JPR Troubles expected on Big-Endian processors ?
114 * \brief Read from file an uncompressed image.
116 bool gdcmPixelConvert::ReadUncompressed( FILE* filePointer,
117 size_t uncompressedSize,
118 size_t expectedSize )
120 if ( expectedSize > uncompressedSize )
122 dbg.Verbose(0, "gdcmPixelConvert::ReadUncompressed: expectedSize"
123 "is bigger than it should");
126 SetUncompressedSize( uncompressedSize );
127 AllocateUncompressed();
128 size_t ItemRead = fread( (void*)Uncompressed, expectedSize, 1, filePointer);
137 * \brief Convert a Gray plane and ( Lut R, Lut G, Lut B ) into an
139 * @return True on success.
141 bool gdcmPixelConvert::ConvertGrayAndLutToRGB( uint8_t *lutRGBA )
144 /// We assume Uncompressed contains the decompressed gray plane
145 /// and build the RGB image.
146 SetRGBSize( UncompressedSize );
149 //aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
150 //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
151 //COPY HERE THE CODE OF GetImageDataIntoVector
153 /// \todo check that operator new []didn't fail, and sometimes return false
158 * \brief Try to deal with RLE 16 Bits.
159 * We assume the RLE has allready been parsed and loaded in
160 * Uncompressed (through \ref ReadAndUncompressRLE8Bits ).
161 * We here need to make 16 Bits Pixels from Low Byte and
162 * High Byte 'Planes'...(for what it may mean)
165 bool gdcmPixelConvert::UncompressRLE16BitsFromRLE8Bits(
170 /// We assumed Uncompressed contains the decoded RLE pixels but as
171 /// 8 bits per pixel. In order to convert those pixels to 16 bits
172 /// per pixel we need to double the space. Hence we cannot work in
173 /// place within Uncompressed. So, here is how we handle things:
174 /// - First stage: copy Uncompressed in a safe place, say OldUncompressed
175 /// - Second stage: reallocate Uncompressed with the needed space
176 /// - Third stage: expand from OldUncompressed to Uncompressed
177 /// - Fourth stage: clean up OldUncompressed
180 uint8_t* OldUncompressed = new uint8_t[UncompressedSize * 2];
181 memmove( OldUncompressed, Uncompressed, UncompressedSize);
184 SetUncompressedSize( 2 * UncompressedSize );
185 AllocateUncompressed();
188 uint8_t* x = Uncompressed;
189 uint8_t* a = OldUncompressed;
190 uint8_t* b = a + PixelNumber;
192 for ( int i = 0; i < NumberOfFrames; i++ )
194 for ( int j = 0; j < PixelNumber; j++ )
202 delete[] OldUncompressed;
204 /// \todo check that operator new []didn't fail, and sometimes return false