comparison UnitTestsSources/JpegLossless.cpp @ 844:502c49adb5ad jpeg

jpeg lossless
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Jun 2014 14:08:23 +0200
parents 6a5cceec04ef
children 48016722c770
comparison
equal deleted inserted replaced
843:6a5cceec04ef 844:502c49adb5ad
38 #include <dcmtk/dcmjpls/djlsutil.h> 38 #include <dcmtk/dcmjpls/djlsutil.h>
39 #include <dcmtk/dcmjpls/djdecode.h> 39 #include <dcmtk/dcmjpls/djdecode.h>
40 #include <dcmtk/dcmdata/dcfilefo.h> 40 #include <dcmtk/dcmdata/dcfilefo.h>
41 41
42 #include <dcmtk/dcmjpls/djcodecd.h> 42 #include <dcmtk/dcmjpls/djcodecd.h>
43 #include <dcmtk/dcmjpls/djcparam.h>
44 #include <dcmtk/dcmjpeg/djrplol.h>
45 #include <dcmtk/dcmdata/dcstack.h>
46 #include <dcmtk/dcmdata/dcpixseq.h>
43 47
44 #include "../OrthancServer/ParsedDicomFile.h" 48 #include "../OrthancServer/ParsedDicomFile.h"
45 #include "../OrthancServer/FromDcmtkBridge.h" 49 #include "../OrthancServer/FromDcmtkBridge.h"
50 #include "../OrthancServer/ToDcmtkBridge.h"
51 #include "../Core/OrthancException.h"
52 #include "../Core/ImageFormats/ImageBuffer.h"
53 #include "../Core/ImageFormats/PngWriter.h"
54
55 #include <boost/lexical_cast.hpp>
46 56
47 using namespace Orthanc; 57 using namespace Orthanc;
48 58
49 TEST(JpegLossless, Basic) 59 TEST(JpegLossless, Basic)
50 { 60 {
51 DJLSDecoderRegistration::registerCodecs( EJLSUC_default, EJLSPC_restore,OFFalse ); 61 //DJLSDecoderRegistration::registerCodecs( EJLSUC_default, EJLSPC_restore,OFFalse );
52 62
53 #if 0 63 #if 0
64 // Fallback
65
54 std::string s; 66 std::string s;
55 Toolbox::ReadFile(s, "IM-0001-1001-0001.dcm"); 67 Toolbox::ReadFile(s, "IM-0001-1001-0001.dcm");
56 68
57 ParsedDicomFile parsed(s); 69 ParsedDicomFile parsed(s);
58 DcmFileFormat& dicom = *reinterpret_cast<DcmFileFormat*>(parsed.GetDcmtkObject()); 70 DcmFileFormat& dicom = *reinterpret_cast<DcmFileFormat*>(parsed.GetDcmtkObject());
64 if (dataset->canWriteXfer(EXS_LittleEndianExplicit)) 76 if (dataset->canWriteXfer(EXS_LittleEndianExplicit))
65 { 77 {
66 printf("ICI\n"); 78 printf("ICI\n");
67 79
68 parsed.SaveToFile("tutu.dcm"); 80 parsed.SaveToFile("tutu.dcm");
81
82 // decompress data set if compressed
83 dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
84
85 DcmXfer original_xfer(dataset->getOriginalXfer());
86 std::cout << original_xfer.getXferName() << std::endl;
69 87
70 FromDcmtkBridge::ExtractPngImage(s, *dataset, 1, ImageExtractionMode_Preview); 88 FromDcmtkBridge::ExtractPngImage(s, *dataset, 1, ImageExtractionMode_Preview);
71 //fileformat.saveFile("test_decompressed.dcm", EXS_LittleEndianExplicit); 89 //fileformat.saveFile("test_decompressed.dcm", EXS_LittleEndianExplicit);
72 } 90 }
73 #else 91 #else
74 DcmFileFormat fileformat; 92 DcmFileFormat fileformat;
75 if (fileformat.loadFile("IM-0001-1001-0001.dcm").good()) 93 //if (fileformat.loadFile("IM-0001-1001-0001.dcm").good())
94 if (fileformat.loadFile("tata.dcm").good())
76 { 95 {
77 DcmDataset *dataset = fileformat.getDataset(); 96 DcmDataset *dataset = fileformat.getDataset();
78 97
79 // decompress data set if compressed 98 // <data-set xfer="1.2.840.10008.1.2.4.80" name="JPEG-LS Lossless">
80 dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL); 99
81 100 DcmTag k(DICOM_TAG_PIXEL_DATA.GetGroup(),
82 DcmXfer original_xfer(dataset->getOriginalXfer()); 101 DICOM_TAG_PIXEL_DATA.GetElement());
83 std::cout << original_xfer.getXferName() << std::endl; 102
84 103 DcmElement *element = NULL;
85 printf("OK1\n"); 104 if (dataset->findAndGetElement(k, element).good())
86
87 // check if everything went well
88 if (1) //dataset->canWriteXfer(EXS_LittleEndianExplicit))
89 { 105 {
90 printf("OK2\n"); 106 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element);
91 107 DcmPixelSequence* pixelSequence = NULL;
92 fileformat.saveFile("tutu.dcm", EXS_LittleEndianExplicit); 108 if (pixelData.getEncapsulatedRepresentation
109 (dataset->getOriginalXfer(), NULL, pixelSequence).good())
110 {
111 OFString value;
112
113 if (!dataset->findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_COLUMNS), value).good())
114 {
115 throw OrthancException(ErrorCode_BadFileFormat);
116 }
117
118 unsigned int width = boost::lexical_cast<unsigned int>(value.c_str());
119
120 if (!dataset->findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_ROWS), value).good())
121 {
122 throw OrthancException(ErrorCode_BadFileFormat);
123 }
124
125 unsigned int height = boost::lexical_cast<unsigned int>(value.c_str());
126
127 if (!dataset->findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_BITS_STORED), value).good())
128 {
129 throw OrthancException(ErrorCode_BadFileFormat);
130 }
131
132 unsigned int bitsStored = boost::lexical_cast<unsigned int>(value.c_str());
133
134 if (!dataset->findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_REPRESENTATION), value).good())
135 {
136 throw OrthancException(ErrorCode_BadFileFormat);
137 }
138
139 bool isSigned = (boost::lexical_cast<unsigned int>(value.c_str()) != 0);
140
141 unsigned int samplesPerPixel = 1; // By default
142 if (dataset->findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_SAMPLES_PER_PIXEL), value).good())
143 {
144 samplesPerPixel = boost::lexical_cast<unsigned int>(value.c_str());
145 }
146
147 ImageBuffer buffer;
148 buffer.SetHeight(height);
149 buffer.SetWidth(width);
150
151 if (bitsStored == 8 && samplesPerPixel == 1 && !isSigned)
152 {
153 buffer.SetFormat(PixelFormat_Grayscale8);
154 }
155 else if (bitsStored == 8 && samplesPerPixel == 3 && !isSigned)
156 {
157 buffer.SetFormat(PixelFormat_RGB24);
158 }
159 else if (bitsStored == 16 && samplesPerPixel == 1 && !isSigned)
160 {
161 buffer.SetFormat(PixelFormat_Grayscale16);
162 }
163 else if (bitsStored == 16 && samplesPerPixel == 1 && isSigned)
164 {
165 buffer.SetFormat(PixelFormat_SignedGrayscale16);
166 }
167 else
168 {
169 throw OrthancException(ErrorCode_NotImplemented);
170 }
171
172 ImageAccessor accessor(buffer.GetAccessor());
173
174 // http://support.dcmtk.org/docs/classDJLSLosslessDecoder.html
175 DJLSLosslessDecoder bb; DJLSCodecParameter cp;
176 //DJLSNearLosslessDecoder bb; DJLSCodecParameter cp;
177
178 Uint32 startFragment = 0; // Default
179 OFString decompressedColorModel; // Out
180 DJ_RPLossless rp;
181 OFCondition c = bb.decodeFrame(&rp, pixelSequence, &cp, dataset, 0, startFragment,
182 accessor.GetBuffer(), accessor.GetSize(), decompressedColorModel);
183
184
185
186 for (unsigned int y = 0; y < accessor.GetHeight(); y++)
187 {
188 int16_t *p = reinterpret_cast<int16_t*>(accessor.GetRow(y));
189 for (unsigned int x = 0; x < accessor.GetWidth(); x++, p ++)
190 {
191 if (*p < 0)
192 *p = 0;
193 }
194 }
195
196 PngWriter w;
197 w.WriteToFile("tata.png", accessor);
198 }
93 } 199 }
94 } 200 }
95 201
96
97 #endif 202 #endif
98 203
99 204
100 // http://support.dcmtk.org/docs/classDJLSLosslessDecoder.html 205 //DJLSDecoderRegistration::cleanup();
101 //DJLSDecoderBase b;
102 DJLSLosslessDecoder bb;
103
104 DJLSDecoderRegistration::cleanup();
105 } 206 }
106 207
107 208
108 #endif 209 #endif