comparison Applications/DicomToTiff.cpp @ 167:605247fc8758

Fix issue #144 (OrthancWSIDicomizer PhotometricInterpretation)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Jul 2019 12:00:31 +0200
parents c23e31ac75ef
children e3cbf890b588
comparison
equal deleted inserted replaced
166:f0dac1e8f736 167:605247fc8758
181 181
182 182
183 static void Run(OrthancWSI::ITiledPyramid& source, 183 static void Run(OrthancWSI::ITiledPyramid& source,
184 const boost::program_options::variables_map& options) 184 const boost::program_options::variables_map& options)
185 { 185 {
186 bool reencode = (options.count(OPTION_REENCODE) &&
187 options[OPTION_REENCODE].as<bool>());
188
189 Orthanc::PhotometricInterpretation targetPhotometric;
190
191 if (reencode)
192 {
193 // The DicomToTiff tool only creates TIFF images with JPEG tiles (*)
194 targetPhotometric = Orthanc::PhotometricInterpretation_YBRFull422;
195 }
196 else
197 {
198 targetPhotometric = source.GetPhotometricInterpretation();
199 }
200
186 OrthancWSI::HierarchicalTiffWriter target(options[OPTION_OUTPUT].as<std::string>(), 201 OrthancWSI::HierarchicalTiffWriter target(options[OPTION_OUTPUT].as<std::string>(),
187 source.GetPixelFormat(), 202 source.GetPixelFormat(),
188 OrthancWSI::ImageCompression_Jpeg, 203 OrthancWSI::ImageCompression_Jpeg, // (*)
189 source.GetTileWidth(), 204 source.GetTileWidth(),
190 source.GetTileHeight()); 205 source.GetTileHeight(),
191 206 targetPhotometric);
192 bool reencode = (options.count(OPTION_REENCODE) &&
193 options[OPTION_REENCODE].as<bool>());
194 207
195 if (options.count(OPTION_JPEG_QUALITY)) 208 if (options.count(OPTION_JPEG_QUALITY))
196 { 209 {
197 target.SetJpegQuality(options[OPTION_JPEG_QUALITY].as<int>()); 210 target.SetJpegQuality(options[OPTION_JPEG_QUALITY].as<int>());
198 } 211 }
212
213 LOG(WARNING) << "Source photometric interpretation: " << EnumerationToString(source.GetPhotometricInterpretation());
214 LOG(WARNING) << "Target photometric interpretation: " << EnumerationToString(targetPhotometric);
199 215
200 std::auto_ptr<Orthanc::ImageAccessor> empty(CreateEmptyTile(target, options)); 216 std::auto_ptr<Orthanc::ImageAccessor> empty(CreateEmptyTile(target, options));
201 217
202 for (unsigned int level = 0; level < source.GetLevelCount(); level++) 218 for (unsigned int level = 0; level < source.GetLevelCount(); level++)
203 { 219 {
224 LOG(INFO) << "Dealing with tile (" << tileX << "," << tileY << ") at level " << level; 240 LOG(INFO) << "Dealing with tile (" << tileX << "," << tileY << ") at level " << level;
225 241
226 bool missing = false; 242 bool missing = false;
227 bool success = true; 243 bool success = true;
228 244
229 // Give a first try to get the raw tile
230 std::string tile; 245 std::string tile;
231 OrthancWSI::ImageCompression compression; 246 OrthancWSI::ImageCompression compression;
247
232 if (source.ReadRawTile(tile, compression, level, tileX, tileY)) 248 if (source.ReadRawTile(tile, compression, level, tileX, tileY))
233 { 249 {
234 if (reencode || 250 if (compression == OrthancWSI::ImageCompression_Jpeg)
235 compression == OrthancWSI::ImageCompression_Jpeg)
236 { 251 {
252 // Transcoding of JPEG tiles
237 target.WriteRawTile(tile, compression, level, tileX, tileY); 253 target.WriteRawTile(tile, compression, level, tileX, tileY);
254 }
255 else if (reencode)
256 {
257 std::auto_ptr<Orthanc::ImageAccessor> decoded;
258
259 if (compression == OrthancWSI::ImageCompression_None)
260 {
261 decoded.reset(OrthancWSI::ImageToolbox::DecodeRawTile(tile, source.GetPixelFormat(),
262 source.GetTileWidth(), source.GetTileHeight()));
263 }
264 else
265 {
266 decoded.reset(OrthancWSI::ImageToolbox::DecodeTile(tile, compression));
267 }
268
269 target.EncodeTile(*decoded, level, tileX, tileY);
238 } 270 }
239 else 271 else
240 { 272 {
241 success = false; // Re-encoding is mandatory 273 success = false; // Re-encoding is mandatory
242 } 274 }
243 } 275 }
244 else 276 else
245 { 277 {
246 // Give a second try to get the decoded tile 278 // Unable to read the raw tile: The tile is missing (sparse tiling)
247 compression = OrthancWSI::ImageCompression_Unknown; 279 missing = true;
248
249 std::auto_ptr<Orthanc::ImageAccessor> tile(source.DecodeTile(level, tileX, tileY));
250 if (tile.get() == NULL)
251 {
252 // Unable to read the raw tile or to decode it: The tile is missing (sparse tiling)
253 missing = true;
254 }
255 else if (reencode)
256 {
257 target.EncodeTile(*empty, level, tileX, tileY);
258 }
259 else
260 {
261 success = false; // Re-encoding is mandatory
262 }
263 } 280 }
264 281
265 if (!success) 282 if (!success)
266 { 283 {
267 LOG(WARNING) << "Cannot transcode a DICOM image that is not encoded using JPEG (it is " 284 LOG(WARNING) << "Cannot transcode a DICOM image that is not encoded using JPEG (it is "