comparison UnitTestsSources/JpegLossless.cpp @ 845:48016722c770 jpeg

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Jun 2014 15:58:07 +0200
parents 502c49adb5ad
children 03ea55da7429
comparison
equal deleted inserted replaced
844:502c49adb5ad 845:48016722c770
31 31
32 32
33 #include "PrecompiledHeadersUnitTests.h" 33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h" 34 #include "gtest/gtest.h"
35 35
36 #include "../OrthancServer/Internals/DicomImageDecoder.h"
37
36 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 38 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
37 39
38 #include <dcmtk/dcmjpls/djlsutil.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>
43 #include <dcmtk/dcmjpls/djcparam.h>
44 #include <dcmtk/dcmjpeg/djrplol.h>
45 #include <dcmtk/dcmdata/dcstack.h>
46 #include <dcmtk/dcmdata/dcpixseq.h>
47
48 #include "../OrthancServer/ParsedDicomFile.h" 42 #include "../OrthancServer/ParsedDicomFile.h"
49 #include "../OrthancServer/FromDcmtkBridge.h"
50 #include "../OrthancServer/ToDcmtkBridge.h"
51 #include "../Core/OrthancException.h" 43 #include "../Core/OrthancException.h"
52 #include "../Core/ImageFormats/ImageBuffer.h" 44 #include "../Core/ImageFormats/ImageBuffer.h"
53 #include "../Core/ImageFormats/PngWriter.h" 45 #include "../Core/ImageFormats/PngWriter.h"
54 46
55 #include <boost/lexical_cast.hpp> 47 using namespace Orthanc;
56 48
57 using namespace Orthanc;
58 49
59 TEST(JpegLossless, Basic) 50 TEST(JpegLossless, Basic)
60 { 51 {
61 //DJLSDecoderRegistration::registerCodecs( EJLSUC_default, EJLSPC_restore,OFFalse ); 52 //DJLSDecoderRegistration::registerCodecs( EJLSUC_default, EJLSPC_restore,OFFalse );
62 53
91 #else 82 #else
92 DcmFileFormat fileformat; 83 DcmFileFormat fileformat;
93 //if (fileformat.loadFile("IM-0001-1001-0001.dcm").good()) 84 //if (fileformat.loadFile("IM-0001-1001-0001.dcm").good())
94 if (fileformat.loadFile("tata.dcm").good()) 85 if (fileformat.loadFile("tata.dcm").good())
95 { 86 {
96 DcmDataset *dataset = fileformat.getDataset(); 87 DcmDataset& dataset = *fileformat.getDataset();
97 88
98 // <data-set xfer="1.2.840.10008.1.2.4.80" name="JPEG-LS Lossless"> 89 ASSERT_TRUE(DicomImageDecoder::IsJpegLossless(dataset));
99 90
100 DcmTag k(DICOM_TAG_PIXEL_DATA.GetGroup(), 91 ImageBuffer image;
101 DICOM_TAG_PIXEL_DATA.GetElement()); 92 DicomImageDecoder::DecodeJpegLossless(image, dataset);
102 93
103 DcmElement *element = NULL; 94 ImageAccessor accessor(image.GetAccessor());
104 if (dataset->findAndGetElement(k, element).good()) 95
96 for (unsigned int y = 0; y < accessor.GetHeight(); y++)
105 { 97 {
106 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element); 98 int16_t *p = reinterpret_cast<int16_t*>(accessor.GetRow(y));
107 DcmPixelSequence* pixelSequence = NULL; 99 for (unsigned int x = 0; x < accessor.GetWidth(); x++, p ++)
108 if (pixelData.getEncapsulatedRepresentation
109 (dataset->getOriginalXfer(), NULL, pixelSequence).good())
110 { 100 {
111 OFString value; 101 if (*p < 0)
112 102 *p = 0;
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 } 103 }
199 } 104 }
105
106 PngWriter w;
107 w.WriteToFile("tata.png", accessor);
200 } 108 }
201 109
202 #endif 110 #endif
203 111
204 112