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();
38 /////////////////////////////////////////////////////////////////
41 * \brief calcule la longueur (in bytes) A ALLOUER pour recevoir les
43 * ou DES images dans le cas d'un multiframe
44 * ATTENTION : il ne s'agit PAS de la longueur du groupe des Pixels
45 * (dans le cas d'images compressees, elle n'a pas de sens).
47 * @return longueur a allouer
50 void gdcmFile::SetPixelDataSizeFromHeader(void) {
54 str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
55 if (str_nb == "gdcm::Unfound" ) {
58 nb = atoi(str_nb.c_str() );
61 lgrTotale = GetXSize() * GetYSize() * GetZSize() * (nb/8);
68 size_t gdcmFile::GetImageDataSize(void) {
75 * \brief Read pixel data from disk (optionaly decompressing) into the
76 * caller specified memory location.
77 * @param destination Where the pixel data should be stored.
80 bool gdcmFile::ReadPixelData(void* destination) {
83 if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
87 if (IsJPEGLossless()) {
88 destination = _IdDcmJpegRead(fp);
90 size_t ItemRead = fread(destination, lgrTotale, 1, fp);
91 if ( ItemRead != 1 ) {
100 /////////////////////////////////////////////////////////////////
103 * \brief Allocates necessary memory, copies the pixel data
104 * (image[s]/volume[s]) to newly allocated zone and return a
107 void * gdcmFile::GetImageData (void) {
108 PixelData = (void *) malloc(lgrTotale);
109 GetImageDataIntoVector(PixelData, lgrTotale);
113 /////////////////////////////////////////////////////////////////
116 * \brief amene en mémoire dans une zone précisee par l'utilisateur
117 * les Pixels d'une image
122 * @return The number of bytes actually copied.
125 size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t MaxSize) {
127 int nb, nbu, highBit, signe;
128 string str_nbFrames, str_nb, str_nbu, str_highBit, str_signe;
130 if ( lgrTotale > MaxSize ) {
131 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: pixel data bigger"
132 "than caller's expected MaxSize");
136 (void)ReadPixelData(destination);
138 // Nombre de Bits Alloues pour le stockage d'un Pixel
139 str_nb = GetPubElValByNumber(0x0028,0x0100);
140 if (str_nb == "gdcm::Unfound" ) {
143 nb = atoi(str_nb.c_str() );
146 // Nombre de Bits Utilises
147 str_nbu=GetPubElValByNumber(0x0028,0x0101);
148 if (str_nbu == "gdcm::Unfound" ) {
151 nbu = atoi(str_nbu.c_str() );
154 // Position du Bit de Poids Fort
155 str_highBit=GetPubElValByNumber(0x0028,0x0102);
156 if (str_highBit == "gdcm::Unfound" ) {
159 highBit = atoi(str_highBit.c_str() );
163 str_signe=GetPubElValByNumber(0x0028,0x0103);
164 if (str_signe == "gdcm::Unfound" ) {
167 signe = atoi(str_signe.c_str() );
170 // On remet les Octets dans le bon ordre si besoin est
172 SwapZone(destination, GetSwapCode(), lgrTotale, nb);
174 // On remet les Bits des Octets dans le bon ordre si besoin est
176 int l = (int)lgrTotale / (nb/8);
178 guint16 mask = 0xffff;
179 mask = mask >> (nb-nbu);
180 guint16 *deb = (guint16 *)destination;
181 for(int i = 0; i<l; i++) {
182 *deb = (*deb >> (nbu-highBit-1)) & mask;
185 } else if (nb == 32 ) {
186 guint32 mask = 0xffffffff;
187 mask = mask >> (nb-nbu);
188 guint32 *deb = (guint32 *)destination;
189 for(int i = 0; i<l; i++) {
190 *deb = (*deb >> (nbu-highBit-1)) & mask;
194 dbg.Verbose(0, "gdcmFile::GetImageDataIntoVector: wierd image");
203 // Je laisse le code integral, au cas ça puisse etre reutilise ailleurs
206 void gdcmFile::SwapZone(void* im, int swap, int lgr, int nb) {
225 ((unsigned short int*)im)[i]= ((((unsigned short int*)im)[i])>>8)
226 | ((((unsigned short int*)im)[i])<<8);
230 printf("valeur de SWAP (16 bits) non autorisee : %d\n", swap);
242 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 4321 */
243 fort =((unsigned long int*)im)[i]>>16;
244 fort= (fort>>8) | (fort<<8);
245 faible=(faible>>8) | (faible<<8);
247 ((unsigned long int*)im)[i]=(s32<<16)|fort;
253 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 2143 */
254 fort=((unsigned long int*)im)[i]>>16;
255 fort= (fort>>8) | (fort<<8);
256 faible=(faible>>8) | (faible<<8);
258 ((unsigned long int*)im)[i]=(s32<<16)|faible;
264 faible= ((unsigned long int*)im)[i]&0x0000ffff; /* 3412 */
265 fort=((unsigned long int*)im)[i]>>16;
267 ((unsigned long int*)im)[i]=(s32<<16)|fort;
272 printf("valeur de SWAP (32 bits) non autorisee : %d\n", swap);
277 /////////////////////////////////////////////////////////////////
281 * \warning doit-etre etre publique ? FIXME JPR
283 * @param Data TODO JPR
284 * @param ExpectedSize TODO JPR
288 int gdcmFile::SetImageData(void * inData, size_t ExpectedSize) {
289 SetImageDataSize(ExpectedSize);
291 lgrTotale = ExpectedSize;
296 /////////////////////////////////////////////////////////////////
301 * \warning WARNING doit-etre etre publique ? FIXME JPR
303 * @param ImageDataSize TODO JPR
307 void gdcmFile::SetImageDataSize(size_t ImageDataSize) {
313 // suppose que le ElValue (0x7fe0, 0x0010) existe ...
315 sprintf(car,"%d",ImageDataSize);
317 SetPubElValByNumber(content2, 0x7fe0, 0x0010);
320 sprintf(car,"%d",ImageDataSize);
322 SetPubElValByNumber(content1, 0x7fe0, 0x0000);
326 /////////////////////////////////////////////////////////////////
329 * \brief Ecrit sur disque les pixels d'UNE image
330 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
331 * Ca sera à l'utilisateur d'appeler son Reader correctement
332 * (Equivalent a IdImaWriteRawFile) FIXME JPR
334 * @param nomFichier TODO JPR
339 int gdcmFile::WriteRawData (string nomFichier) {
342 fp1 = fopen(nomFichier.c_str(),"wb");
344 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
348 fwrite (PixelData,lgrTotale, 1, fp1);
355 /////////////////////////////////////////////////////////////////
358 * \brief Ecrit sur disque UNE image Dicom
359 * Aucun test n'est fait sur l'"Endiannerie" du processeur.
360 * Ca fonctionnera correctement (?) sur processeur Intel
361 * (Equivalent a IdDcmWrite) FIXME JPR
363 * @param nomFichier TODO JPR
368 int gdcmFile::WriteDcmImplVR (string nomFichier) {
369 return WriteBase(nomFichier, ImplicitVR);
372 int gdcmFile::WriteDcmImplVR (const char* nomFichier) {
373 return WriteDcmImplVR (string (nomFichier));
376 /////////////////////////////////////////////////////////////////
380 * @param nomFichier TODO JPR
385 int gdcmFile::WriteDcmExplVR (string nomFichier) {
386 return WriteBase(nomFichier, ExplicitVR);
389 /////////////////////////////////////////////////////////////////
392 * \brief Ecrit au format ACR-NEMA sur disque l'entete et les pixels
393 * (a l'attention des logiciels cliniques
394 * qui ne prennent en entrée QUE des images ACR ...
395 * \warning si un header DICOM est fourni en entree,
396 * les groupes < 0x0008 et les groupes impairs sont ignores)
397 * \warning Aucun test n'est fait sur l'"Endiannerie" du processeur.
398 * Ca fonctionnera correctement (?) sur processeur Intel
399 * (Equivalent a IdDcmWrite)
401 * @param nomFichier TODO JPR
406 int gdcmFile::WriteAcr (string nomFichier) {
407 return WriteBase(nomFichier, ACR);
410 int gdcmFile::WriteBase (string nomFichier, FileType type) {
413 fp1 = fopen(nomFichier.c_str(),"wb");
415 printf("Echec ouverture (ecriture) Fichier [%s] \n",nomFichier.c_str());
419 if ( (type == ImplicitVR) || (type == ExplicitVR) ) {
421 // Ecriture Dicom File Preamble
422 filePreamble=(char*)calloc(128,1);
423 fwrite(filePreamble,128,1,fp1);
424 fwrite("DICM",4,1,fp1);
427 gdcmHeader::Write(fp1, type);
428 fwrite(PixelData, lgrTotale, 1, fp1);