7 /////////////////////////////////////////////////////////////////
10 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
11 * file (see SetFileName, SetDcmTag and Write)
12 * Opens (in read only and when possible) an existing file and checks
13 * for DICOM compliance. Returns NULL on failure.
14 * \Note the in-memory representation of all available tags found in
15 * the DICOM header is post-poned to first header information access.
16 * This avoid a double parsing of public part of the header when
17 * one sets an a posteriori shadow dictionary (efficiency can be
18 * seen as a side effect).
20 * @param filename file to be opened for parsing
25 gdcmFile::gdcmFile(string & filename)
26 :gdcmHeader(filename.c_str())
28 SetPixelDataSizeFromHeader();
31 gdcmFile::gdcmFile(const char * filename)
34 SetPixelDataSizeFromHeader();
39 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
41 * ou DES images dans le cas d'un multiframe
42 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
43 * (dans le cas d'images compressees, elle n'a pas de sens).
45 * @return longueur a allouer
47 void gdcmFile::SetPixelDataSizeFromHeader(void) {
51 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
52 if (str_nb == "gdcm::Unfound" ) {
55 nb = atoi(str_nb.c_str() );
58 lgrTotale = GetXSize() * GetYSize() * GetZSize() * (nb/8);
63 * \brief Returns the size (in bytes) of required memory to hold
64 * the pixel data represented in this file.
65 * @return The size of pixel data in bytes.
67 size_t gdcmFile::GetImageDataSize(void) {
73 * \brief Read pixel data from disk (optionaly decompressing) into the
74 * caller specified memory location.
75 * @param destination Where the pixel data should be stored.
78 bool gdcmFile::ReadPixelData(void* destination) {
82 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
88 IsImplicitVRLittleEndianTransferSyntax() ||
89 IsExplicitVRLittleEndianTransferSyntax() ||
90 IsExplicitVRBigEndianTransferSyntax() ||
91 IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
93 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
94 if ( ItemRead != 1 ) {
103 if (IsJPEGLossless()) {
105 fseek(fp,4,SEEK_CUR);
110 printf ("ln %d\n",ln);
111 fseek(fp,ln,SEEK_CUR);
112 fseek(fp,4,SEEK_CUR);
117 printf ("ln image comprimée %d\n",ln);
119 ClbJpeg* jpg = _IdDcmJpegRead(fp);
124 memcpy(destination,jpg->DataImg,lgrTotale);
125 _IdDcmJpegFree (jpg);
130 printf ("Sorry, TransfertSyntax not yet taken into account ...\n");
138 * \brief Allocates necessary memory, copies the pixel data
139 * (image[s]/volume[s]) to newly allocated zone.
140 * @return Pointer to newly allocated pixel data.
142 void * gdcmFile::GetImageData (void) {
143 PixelData = (void *) malloc(lgrTotale);
144 GetImageDataIntoVector(PixelData, lgrTotale);
150 * \brief Copies at most MaxSize bytes of pixel data to caller's
152 * @param destination Address (in caller's memory space) at which the
153 * pixel data should be copied
154 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
155 * is not sufficient to hold the pixel data the copy is not
156 * executed (i.e. no partial copy).
157 * @return On success, the number of bytes actually copied. Zero on
158 * failure e.g. MaxSize is lower than necessary.
161 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
163 int nb, nbu, highBit, signe;
164 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
166 if ( lgrTotale > MaxSize ) {
167 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
168 "than caller's expected MaxSize");
172 (void)ReadPixelData(destination);
174 // Nombre de Bits Alloues pour le stockage d'un Pixel
175 str_nb = GetPubElValByNumber(0x0028,0x0100);
176 if (str_nb == "gdcm::Unfound" ) {
179 nb = atoi(str_nb.c_str() );
182 // Nombre de Bits Utilises
183 str_nbu=GetPubElValByNumber(0x0028,0x0101);
184 if (str_nbu == "gdcm::Unfound" ) {
187 nbu = atoi(str_nbu.c_str() );
190 // Position du Bit de Poids Fort
191 str_highBit=GetPubElValByNumber(0x0028,0x0102);
192 if (str_highBit == "gdcm::Unfound" ) {
195 highBit = atoi(str_highBit.c_str() );
199 str_signe=GetPubElValByNumber(0x0028,0x0103);
200 if (str_signe == "gdcm::Unfound" ) {
203 signe = atoi(str_signe.c_str() );
206 // On remet les Octets dans le bon ordre si besoin est
208 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
210 // On remet les Bits des Octets dans le bon ordre si besoin est
212 int l = (int)lgrTotale / (nb/8);
214 guint16 mask = 0xffff;
215 mask = mask >> (nb-nbu);
216 guint16 *deb = (guint16 *)destination;
217 for(int i = 0; i<l; i++) {
218 *deb = (*deb >> (nbu-highBit-1)) & mask;
221 } else if (nb == 32 ) {
222 guint32 mask = 0xffffffff;
223 mask = mask >> (nb-nbu);
224 guint32 *deb = (guint32 *)destination;
225 for(int i = 0; i<l; i++) {
226 *deb = (*deb >> (nbu-highBit-1)) & mask;
230 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
239 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
242 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
261 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
262 | ((((unsigned short int*)im)[i])<<8);
266 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
278 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
279 fort =((unsigned long int*)im)[i]>>16;
280 fort= (fort>>8) | (fort<<8);
281 faible=(faible>>8) | (faible<<8);
283 ((unsigned long int*)im)[i]=(s32<<16)|fort;
289 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
290 fort=((unsigned long int*)im)[i]>>16;
291 fort= (fort>>8) | (fort<<8);
292 faible=(faible>>8) | (faible<<8);
294 ((unsigned long int*)im)[i]=(s32<<16)|faible;
300 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
301 fort=((unsigned long int*)im)[i]>>16;
303 ((unsigned long int*)im)[i]=(s32<<16)|fort;
308 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
313 /////////////////////////////////////////////////////////////////
317 * \warning doit-etre etre publique ? FIXME JPR
318 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
320 * @param inData TODO JPR
321 * @param ExpectedSize TODO JPR
325 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
326 SetImageDataSize(ExpectedSize);
328 lgrTotale = ExpectedSize;
335 /////////////////////////////////////////////////////////////////
340 * \warning WARNING doit-etre etre publique ? FIXME JPR
341 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
343 * @param ImageDataSize TODO JPR
347 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
352 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
354 sprintf(car,"%d",ImageDataSize);
356 gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
357 a->SetLength(ImageDataSize);
360 sprintf(car,"%d",ImageDataSize);
362 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
366 /////////////////////////////////////////////////////////////////
369 * \brief Ecrit sur disque les pixels d'UNE image
370 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
371 * Ca sera à l'utilisateur d'appeler son Reader correctement
372 * (Equivalent a IdImaWriteRawFile) FIXME JPR
374 * @param nomFichier TODO JPR
379 int gdcmFile::WriteRawData (string nomFichier) {
382 fp1 = fopen(nomFichier.c_str(),"wb");
384 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
388 fwrite (PixelData,lgrTotale, 1, fp1);
395 /////////////////////////////////////////////////////////////////
398 * \brief Ecrit sur disque UNE image Dicom
399 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
400 * Ca fonctionnera correctement (?) sur processeur Intel
401 * (Equivalent a IdDcmWrite) FIXME JPR
403 * @param nomFichier TODO JPR
408 int gdcmFile::WriteDcmImplVR (string nomFichier) {
409 return WriteBase(nomFichier, ImplicitVR);
412 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
413 return WriteDcmImplVR (string (nomFichier));
416 /////////////////////////////////////////////////////////////////
420 * @param nomFichier TODO JPR
425 int gdcmFile::WriteDcmExplVR (string nomFichier) {
426 return WriteBase(nomFichier, ExplicitVR);
429 /////////////////////////////////////////////////////////////////
432 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
433 * (a l'attention des logiciels cliniques
434 * qui ne prennent en entrée QUE des images ACR ...
435 * \warning si un header DICOM est fourni en entree,
436 * les groupes < 0x0008 et les groupes impairs sont ignores)
437 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
438 * Ca fonctionnera correctement (?) sur processeur Intel
439 * (Equivalent a IdDcmWrite)
441 * @param nomFichier TODO JPR
446 int gdcmFile::WriteAcr (string nomFichier) {
447 return WriteBase(nomFichier, ACR);
450 int gdcmFile::WriteBase (string nomFichier, FileType type) {
453 fp1 = fopen(nomFichier.c_str(),"wb");
455 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
459 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
461 // Ecriture Dicom File Preamble
462 filePreamble=(char*)calloc(128,1);
463 fwrite(filePreamble,128,1,fp1);
464 fwrite("DICM",4,1,fp1);
467 gdcmHeader::Write(fp1, type);
468 fwrite(PixelData, lgrTotale, 1, fp1);