11 #include "iddcmjpeg.h"
14 /////////////////////////////////////////////////////////////////
17 * \brief Constructor dedicated to writing a new DICOMV3 part10 compliant
18 * file (see SetFileName, SetDcmTag and Write)
19 * Opens (in read only and when possible) an existing file and checks
20 * for DICOM compliance. Returns NULL on failure.
21 * \Note the in-memory representation of all available tags found in
22 * the DICOM header is post-poned to first header information access.
23 * This avoid a double parsing of public part of the header when
24 * one sets an a posteriori shadow dictionary (efficiency can be
25 * seen as a side effect).
27 * @param filename file to be opened for parsing
32 gdcmFile::gdcmFile(string & filename)
33 :gdcmHeader(filename.c_str())
35 SetPixelDataSizeFromHeader();
38 gdcmFile::gdcmFile(const char * filename)
41 SetPixelDataSizeFromHeader();
46 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
48 * ou DES images dans le cas d'un multiframe
49 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
50 * (dans le cas d'images compressees, elle n'a pas de sens).
52 * @return longueur a allouer
54 void gdcmFile::SetPixelDataSizeFromHeader(void) {
58 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
59 if (str_nb == "gdcm::Unfound" ) {
62 nb = atoi(str_nb.c_str() );
65 lgrTotale = GetXSize() * GetYSize() * GetZSize() * (nb/8)* GetSamplesPerPixel();;
68 /////////////////////////////////////////////////////////////////
71 * \brief Returns the size (in bytes) of required memory to hold
72 * the pixel data represented in this file.
73 * @return The size of pixel data in bytes.
75 size_t gdcmFile::GetImageDataSize(void) {
80 /////////////////////////////////////////////////////////////////
83 * \brief Read pixel data from disk (optionaly decompressing) into the
84 * caller specified memory location.
85 * @param destination where the pixel data should be stored.
88 bool gdcmFile::ReadPixelData(void* destination) {
92 printf("GetPixelOffset() %d\n",GetPixelOffset() );
94 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
100 IsImplicitVRLittleEndianTransferSyntax() ||
101 IsExplicitVRLittleEndianTransferSyntax() ||
102 IsExplicitVRBigEndianTransferSyntax() ||
103 IsDeflatedExplicitVRLittleEndianTransferSyntax() ) {
105 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
106 if ( ItemRead != 1 ) {
115 // ------------------------------- Position on begining of Jpeg Pixels
118 fseek(fp,4,SEEK_CUR);
123 printf ("ln %d\n",ln);
124 fseek(fp,ln,SEEK_CUR);
125 fseek(fp,4,SEEK_CUR);
130 printf ("ln image comprimée %d\n",ln);
132 // ------------------------------- JPEG LossLess : call to Jpeg Libido
134 if (IsJPEGLossless()) {
136 ClbJpeg* jpg = _IdDcmJpegRead(fp);
143 string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
144 if (str_nb == "gdcm::Unfound" ) {
147 nb = atoi(str_nb.c_str() );
148 if (nb == 12) nb =16;
151 int * dataJpg = jpg->DataImg;
152 int taille = GetXSize() * GetYSize() * GetZSize() * GetSamplesPerPixel();
157 unsigned short *dest = (unsigned short *)destination;
158 for (int i=0; i<taille; i++) {
159 *((unsigned char *)dest+i) = *(dataJpg +i);
166 unsigned short *dest = (unsigned short *)destination;
167 for (int i=0; i<taille; i++) {
168 *((unsigned short *)dest+i) = *(dataJpg +i);
175 unsigned int *dest=(unsigned int *)destination;
176 for (int i=0;i<taille; i++) {
177 *((unsigned int *)dest+i) = *(dataJpg +i);
183 _IdDcmJpegFree (jpg);
187 // ------------------------------- JPEG Lossy : call to IJG 6b
189 // TODO : faire qq chose d'intelligent pour limiter
190 // la duplication du code JPEG <bits=8 / bits=12>
191 // TODO : eplucher icelui pour traiter *egalement* bits=16
194 if ((nBS = GetBitsStored()) != 12) {
195 printf("Sorry, Bits Stored = %d not yet taken into account\n",nBS);
199 int res = gdcm_read_JPEG_file (destination);
201 printf ("res : %d\n",res);
207 * \brief Allocates necessary memory, copies the pixel data
208 * (image[s]/volume[s]) to newly allocated zone.
209 * @return Pointer to newly allocated pixel data.
211 void * gdcmFile::GetImageData (void) {
212 PixelData = (void *) malloc(lgrTotale);
213 GetImageDataIntoVector(PixelData, lgrTotale);
219 * \brief Copies at most MaxSize bytes of pixel data to caller's
221 * @param destination Address (in caller's memory space) at which the
222 * pixel data should be copied
223 * @param MaxSize Maximum number of bytes to be copied. When MaxSize
224 * is not sufficient to hold the pixel data the copy is not
225 * executed (i.e. no partial copy).
226 * @return On success, the number of bytes actually copied. Zero on
227 * failure e.g. MaxSize is lower than necessary.
230 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
232 int nb, nbu, highBit, signe;
233 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
235 if ( lgrTotale > MaxSize ) {
236 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
237 "than caller's expected MaxSize");
241 (void)ReadPixelData(destination);
243 // Nombre de Bits Alloues pour le stockage d'un Pixel
244 str_nb = GetPubElValByNumber(0x0028,0x0100);
245 if (str_nb == "gdcm::Unfound" ) {
248 nb = atoi(str_nb.c_str() );
251 // Nombre de Bits Utilises
252 str_nbu=GetPubElValByNumber(0x0028,0x0101);
253 if (str_nbu == "gdcm::Unfound" ) {
256 nbu = atoi(str_nbu.c_str() );
259 // Position du Bit de Poids Fort
260 str_highBit=GetPubElValByNumber(0x0028,0x0102);
261 if (str_highBit == "gdcm::Unfound" ) {
264 highBit = atoi(str_highBit.c_str() );
268 str_signe=GetPubElValByNumber(0x0028,0x0103);
269 if (str_signe == "gdcm::Unfound" ) {
272 signe = atoi(str_signe.c_str() );
275 // On remet les Octets dans le bon ordre si besoin est
277 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
279 // On remet les Bits des Octets dans le bon ordre si besoin est
281 int l = (int)lgrTotale / (nb/8);
283 guint16 mask = 0xffff;
284 mask = mask >> (nb-nbu);
285 guint16 *deb = (guint16 *)destination;
286 for(int i = 0; i<l; i++) {
287 *deb = (*deb >> (nbu-highBit-1)) & mask;
290 } else if (nb == 32 ) {
291 guint32 mask = 0xffffffff;
292 mask = mask >> (nb-nbu);
293 guint32 *deb = (guint32 *)destination;
294 for(int i = 0; i<l; i++) {
295 *deb = (*deb >> (nbu-highBit-1)) & mask;
299 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
305 string str_PlanarConfiguration = GetPubElValByNumber(0x0028,0x0006);
306 int PlanarConfiguration;
307 if (str_PlanarConfiguration == "gdcm::Unfound" ) {
308 PlanarConfiguration = 0;
310 PlanarConfiguration = atoi(str_PlanarConfiguration.c_str() );
315 // if (GetPlanarConfiguration() == 1) {
318 if (PlanarConfiguration == 1) { // need to make RGB Pixels
319 int l = lgrTotale/3 ;
321 char * a = (char *)destination;
324 char * newDest = (char*) malloc(lgrTotale);
326 // any trick not to have to allocate temporary buffer is welcome ...
328 for (int i=0;i<l; i++) {
333 a = (char *)destination;
335 for (int i=0;i<lgrTotale; i++) {
345 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
348 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
366 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
367 | ((((unsigned short int*)im)[i])<<8);
371 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
382 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
383 fort =((unsigned long int*)im)[i]>>16;
384 fort= (fort>>8) | (fort<<8);
385 faible=(faible>>8) | (faible<<8);
387 ((unsigned long int*)im)[i]=(s32<<16)|fort;
393 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
394 fort=((unsigned long int*)im)[i]>>16;
395 fort= (fort>>8) | (fort<<8);
396 faible=(faible>>8) | (faible<<8);
398 ((unsigned long int*)im)[i]=(s32<<16)|faible;
404 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
405 fort=((unsigned long int*)im)[i]>>16;
407 ((unsigned long int*)im)[i]=(s32<<16)|fort;
412 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
417 /////////////////////////////////////////////////////////////////
421 * \warning doit-etre etre publique ? FIXME JPR
422 * TODO : y a-t-il un inconvenient à fusioner ces 2 fonctions
424 * @param inData TODO JPR
425 * @param ExpectedSize TODO JPR
429 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
430 SetImageDataSize(ExpectedSize);
432 lgrTotale = ExpectedSize;
437 /////////////////////////////////////////////////////////////////
442 * \warning WARNING doit-etre etre publique ? FIXME JPR
443 * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
445 * @param ImageDataSize TODO JPR
449 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
454 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
456 sprintf(car,"%d",ImageDataSize);
458 gdcmElValue*a = GetElValueByNumber(0x7fe0, 0x0010);
459 a->SetLength(ImageDataSize);
462 sprintf(car,"%d",ImageDataSize);
464 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
468 /////////////////////////////////////////////////////////////////
471 * \brief Ecrit sur disque les pixels d'UNE image
472 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
473 * Ca sera à l'utilisateur d'appeler son Reader correctement
474 * (Equivalent a IdImaWriteRawFile) FIXME JPR
476 * @param nomFichier TODO JPR
481 int gdcmFile::WriteRawData (string nomFichier) {
484 fp1 = fopen(nomFichier.c_str(),"wb");
486 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
490 fwrite (PixelData,lgrTotale, 1, fp1);
497 /////////////////////////////////////////////////////////////////
500 * \brief Ecrit sur disque UNE image Dicom
501 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
502 * Ca fonctionnera correctement (?) sur processeur Intel
503 * (Equivalent a IdDcmWrite) FIXME JPR
505 * @param nomFichier TODO JPR
510 int gdcmFile::WriteDcmImplVR (string nomFichier) {
511 return WriteBase(nomFichier, ImplicitVR);
514 /////////////////////////////////////////////////////////////////
518 * @param nomFichier TODO JPR
523 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
524 return WriteDcmImplVR (string (nomFichier));
527 /////////////////////////////////////////////////////////////////
531 * @param nomFichier TODO JPR
536 int gdcmFile::WriteDcmExplVR (string nomFichier) {
537 return WriteBase(nomFichier, ExplicitVR);
540 /////////////////////////////////////////////////////////////////
543 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
544 * (a l'attention des logiciels cliniques
545 * qui ne prennent en entrée QUE des images ACR ...
546 * \warning si un header DICOM est fourni en entree,
547 * les groupes < 0x0008 et les groupes impairs sont ignores)
548 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
549 * Ca fonctionnera correctement (?) sur processeur Intel
550 * (Equivalent a IdDcmWrite)
552 * @param nomFichier TODO JPR
557 int gdcmFile::WriteAcr (string nomFichier) {
558 return WriteBase(nomFichier, ACR);
561 /////////////////////////////////////////////////////////////////
565 * @param FileName TODO JPR
566 * @param type TODO JPR
570 int gdcmFile::WriteBase (string FileName, FileType type) {
573 fp1 = fopen(FileName.c_str(),"wb");
575 printf("Echec ouverture (ecriture) Fichier [%s] \n",FileName.c_str());
579 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
581 // Ecriture Dicom File Preamble
582 filePreamble=(char*)calloc(128,1);
583 fwrite(filePreamble,128,1,fp1);
584 fwrite("DICM",4,1,fp1);
587 gdcmHeader::Write(fp1, type);
588 fwrite(PixelData, lgrTotale, 1, fp1);