#include "gdcmFile.h"
#include "gdcmUtil.h"
#include "iddcmjpeg.h" // for the 'LibIDO' Jpeg LossLess
+#include "jpeg/ljpg/jpegless.h"
/////////////////////////////////////////////////////////////////
/**
if ( fseek(fp, GetPixelOffset(), SEEK_SET) == -1 ) {
CloseFile();
return false;
- }
+ }
+
+// ------------------------- Compacted File (12 Bits Per Pixel)
+
+ /* unpack 12 Bits pixels into 16 Bits pixels */
+ /* 2 pixels 12bit = [0xABCDEF] */
+ /* 2 pixels 16bit = [0x0ABD] + [0x0FCE] */
+
+ if (GetBitsAllocated()==12) {
+ int nbPixels = GetXSize()*GetYSize();
+ unsigned char b0, b1, b2;
+
+ for(int p=0;p<nbPixels;p+=2) {
+ fread(&b0,1,1,fp);
+ fread(&b1,1,1,fp);
+ fread(&b2,1,1,fp);
+ *((unsigned short int*)destination)++ =
+ ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 & 0x0f);
+ /* A */ /* B */ /* D */
+ *((unsigned short int*)destination)++ =
+ ((b2 & 0x0f) << 8) + ((b1 >> 4) << 4) + (b2 >> 4);
+ /* F */ /* C */ /* E */
+
+ // Troubles expected on Big-Endian processors ?
+ }
+ return(true);
+ }
+
// ------------------------- Uncompressed File
return true;
}
}
+
+
+// ------------------------- Run Length Encoding
+
+ if (gdcmHeader::IsRLELossLessTransferSyntax()) {
+ int res = (bool)gdcm_read_RLE_file (destination);
+ return res;
+ }
+
+
- // ------------------------ Compressed File .
+// ----------------- SingleFrame/Multiframe JPEG Lossless/Lossy/2000
int nb;
std::string str_nb=gdcmHeader::GetPubElValByNumber(0x0028,0x0100);
nb = 16;
} else {
nb = atoi(str_nb.c_str() );
- if (nb == 12) nb =16;
+ if (nb == 12) nb =16; // ?? 12 should be ACR-NEMA only ?
}
int nBytes= nb/8;
- int taille = GetXSize() * GetYSize() * GetSamplesPerPixel();
-
-
- // ------------------------------- JPEG LossLess : call to Jpeg Libido
-
- if (IsJPEGLossless() && GetZSize() == 1) {
-
- int ln; // Position on begining of Jpeg Pixels
- fseek(fp,4,SEEK_CUR); // skipping (fffe,e000) : Basic Offset Table Item
- fread(&ln,4,1,fp);
- if(GetSwapCode())
- ln=SwapLong(ln); // Item length
- fseek(fp,ln,SEEK_CUR); // skipping Basic Offset Table ('ln' bytes)
- fseek(fp,4,SEEK_CUR); // skipping (fffe,e000) : First fragment Item Tag
- fread(&ln,4,1,fp); // First fragment length (just to know)
- if(GetSwapCode())
- ln=SwapLong(ln);
-
- ClbJpeg* jpg = _IdDcmJpegRead(fp); // TODO : find a 'full' one.
- // (We use the LibIDO one :-(
- if(jpg == NULL) {
- CloseFile();
- return false;
- }
- int * dataJpg = jpg->DataImg;
-
- switch (nBytes) {
- case 1:
- {
- unsigned short *dest = (unsigned short *)destination;
- for (int i=0; i<taille; i++) {
- *((unsigned char *)dest+i) = *(dataJpg +i);
- }
- }
- break;
-
- case 2:
- {
- unsigned short *dest = (unsigned short *)destination;
- for (int i=0; i<taille; i++) {
- *((unsigned short *)dest+i) = *(dataJpg +i);
- }
- }
- break;
- }
- _IdDcmJpegFree (jpg);
- return true;
- }
-
- // ------------------------------- RLE
-
- if (gdcmHeader::IsRLELossLessTransferSyntax()) {
- int res = (bool)gdcm_read_RLE_file (destination);
- return res;
- }
-
- // ------------------------------- Multiframe JPEG
-
+ int taille = GetXSize() * GetYSize() * GetSamplesPerPixel();
long fragmentBegining; // for ftell, fseek
- bool b = gdcmHeader::IsJPEG2000();
+
+ bool jpg2000 = IsJPEG2000();
+ bool jpgLossless = IsJPEGLossless();
- bool res;
+ bool res = true;
guint16 ItemTagGr,ItemTagEl;
- int ln; // Position on begining of Jpeg Pixels
+ int ln;
+
+ // Position on begining of Jpeg Pixels
fread(&ItemTagGr,2,1,fp); // Reading (fffe) : Item Tag Gr
fread(&ItemTagEl,2,1,fp); // Reading (e000) : Item Tag El
}
// parsing fragments until Sequence Delim. Tag found
- //unsigned short *dest = (unsigned short *)destination;
-
- while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
+
+ while ( ( ItemTagGr == 0xfffe) && (ItemTagEl != 0xe0dd) ) {
+
+ // --- for each Fragment
+
fread(&ln,4,1,fp);
if(GetSwapCode())
ln=SwapLong(ln); // Fragment Item length
- // FIXME : multi fragments
fragmentBegining=ftell(fp);
- if (b)
+ if (jpg2000) { // JPEG 2000 : call to ???
+
res = (bool)gdcm_read_JPEG2000_file (destination); // Not Yet written
-
- else if (IsJPEGLossless()) { // JPEG LossLess : call to LibIDO Jpeg
-
- // ------------- for each Fragment
-
- // Warning : Works only if there is one fragment per frame
- // (Or a single fragment for the multiframe file)
- ClbJpeg* jpg = _IdDcmJpegRead(fp); // TODO : find a 'full' one.
- // (We use the LibIDO one :-(
- if(jpg == NULL) {
- CloseFile();
- return false;
- }
- int * dataJpg = jpg->DataImg;
- unsigned short *dest = (unsigned short *)destination;
- switch (nBytes) {
- case 1:
- {
- for (int i=0; i<taille; i++) {
- *((unsigned char *)dest+i) = *(dataJpg +i);
- }
- break;
- }
-
- case 2:
- {
- for (int i=0; i<taille; i++) {
- *((unsigned short *)dest+i) = *(dataJpg +i);
- }
- break;
- }
- }
- _IdDcmJpegFree (jpg);
+
+ } // ------------------------------------- endif (JPEG2000)
- res=1; // in order not to break the loop
+ else if (jpgLossless) { // JPEG LossLess : call to xmedcom JPEG
+
+ JPEGLosslessDecodeImage (fp, // Reading Fragment pixels
+ (unsigned short *)destination,
+ GetPixelSize()*8* GetSamplesPerPixel(),
+ ln);
+ res=1; // in order not to break the loop
- } // ------------------------------------- endif (IsJPEGLossless())
+ } // ------------------------------------- endif (JPEGLossless)
- else // JPEG Lossy : call to IJG 6b
+ else { // JPEG Lossy : call to IJG 6b
+
if (GetBitsStored() == 8) {
res = (bool)gdcm_read_JPEG_file (destination); // Reading Fragment pixels
} else {
res = (bool)gdcm_read_JPEG_file12 (destination);// Reading Fragment pixels
- }
+ }
+ } // ------------------------------------- endif (JPEGLossy)
if (!res) break;
}
} // endWhile parsing fragments until Sequence Delim. Tag found
-
+
return res;
}
// *Try* to deal with the color
// ----------------------------
-
+
+ std::string str_PhotometricInterpretation =
+ gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
+
+ if ( (str_PhotometricInterpretation == "MONOCHROME1 ")
+ || (str_PhotometricInterpretation == "MONOCHROME2 ") ) {
+ return lgrTotale;
+ }
+
// Planar configuration = 0 : Pixels are already RGB
// Planar configuration = 1 : 3 planes : R, G, B
// Planar configuration = 2 : 1 gray Plane + 3 LUT
// Well ... supposed to be !
- // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,PhotometricInterpretation=PALETTE COLOR
+ // See US-PAL-8-10x-echo.dcm: PlanarConfiguration=0,
+ // PhotometricInterpretation=PALETTE COLOR
// and heuristic has to be found :-(
- std::string str_PhotometricInterpretation = gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
-
- if ( (str_PhotometricInterpretation == "MONOCHROME1 ")
- || (str_PhotometricInterpretation == "MONOCHROME2 ")
- || (str_PhotometricInterpretation == "RGB")) {
- return lgrTotale;
- }
int planConf=GetPlanarConfiguration();
- // Whatever Planar Configuration is, "PALETTE COLOR " implies that we deal with the palette.
+ // Whatever Planar Configuration is,
+ // "PALETTE COLOR " implies that we deal with the palette.
if (str_PhotometricInterpretation == "PALETTE COLOR ")
planConf=2;
// integer computation counterpart
for (int i=0;i<nbFrames;i++) {
for (int j=0;j<l; j++) {
- R= 1.164 *( *a-16) + 1.596 *( *c -128) + 0.5;
- G= 1.164 *( *a-16) - 0.813 *( *c -128) - 0.392 *(*b -128) + 0.5;
- B= 1.164 *( *a-16) + 2.017 *( *b -128) + 0.5;
+ R= 1.164 *(*a-16) + 1.596 *(*c -128) + 0.5;
+ G= 1.164 *(*a-16) - 0.813 *(*c -128) - 0.392 *(*b -128) + 0.5;
+ B= 1.164 *(*a-16) + 2.017 *(*b -128) + 0.5;
if (R<0.0) R=0.0;
if (G<0.0) G=0.0;
memmove(destination,newDest,lgrTotale);
free(newDest);
}
-
- // now, it's an RGB image
- // Lets's write it in the Header
- std::string spp = "3";
- gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002);
- std::string rgb="RGB";
- gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
+
break;
}
int l = lgrTotale/3;
memmove(newDest, destination, l);// move Gray pixels to temp area
- unsigned char * x = newDest;
-
int j;
// See PS 3.3-2003 C.11.1.1.2 p 619
//
// if we get a black image, let's just remove the '+1'
// and check again
// if it works, we shall have to check the 3 Palettes
- // to see which byte is ==0 (first one, on second one)
-
+ // to see which byte is ==0 (first one, or second one)
+
for (int i=0;i<l; i++) {
j=newDest[i]*mult +1;
*a++ = lutR[j];
}
free(newDest);
-
- // now, it's an RGB image
- std::string spp = "3";
- gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002);
- std::string rgb="RGB";
- gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
} else { // need to make RGB Pixels (?)
// from grey Pixels (?!)
// Well . I'll wait till I find such an image
}
break;
- }
+ }
}
-
+
+ // now, it's an RGB image
+ // Lets's write it in the Header
+
+ // CreateOrReplaceIfExist ?
+
+ std::string spp = "3"; // Samples Per Pixel
+ gdcmHeader::SetPubElValByNumber(spp,0x0028,0x0002);
+ std::string rgb="RGB "; // Photometric Interpretation
+ gdcmHeader::SetPubElValByNumber(rgb,0x0028,0x0004);
+ std::string planConfig = "0"; // Planar Configuration
+ gdcmHeader::SetPubElValByNumber(planConfig,0x0028,0x0006);
+
+ // + Drop Palette Color ?
+
return lgrTotale;
}