comparison Plugin/ViewerToolbox.cpp @ 79:abdde1dfb3eb

use sdk 0.9.4
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Sep 2015 12:32:37 +0200
parents d7bd116777eb
children 46ec13a1177c
comparison
equal deleted inserted replaced
78:d6da56f86e5a 79:abdde1dfb3eb
22 22
23 #include "../Orthanc/Core/OrthancException.h" 23 #include "../Orthanc/Core/OrthancException.h"
24 #include "../Orthanc/Core/Toolbox.h" 24 #include "../Orthanc/Core/Toolbox.h"
25 25
26 #include <json/reader.h> 26 #include <json/reader.h>
27 #include <zlib.h>
28 #include <stdexcept> 27 #include <stdexcept>
29 #include <boost/lexical_cast.hpp> 28 #include <boost/lexical_cast.hpp>
30 #include <sys/stat.h> 29 #include <sys/stat.h>
31 30
32 namespace OrthancPlugins 31 namespace OrthancPlugins
49 content.assign(reinterpret_cast<const char*>(answer.data), answer.size); 48 content.assign(reinterpret_cast<const char*>(answer.data), answer.size);
50 } 49 }
51 catch (std::bad_alloc&) 50 catch (std::bad_alloc&)
52 { 51 {
53 OrthancPluginFreeMemoryBuffer(context, &answer); 52 OrthancPluginFreeMemoryBuffer(context, &answer);
54 throw Orthanc::OrthancException("Not enough memory"); 53 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
55 } 54 }
56 } 55 }
57 56
58 OrthancPluginFreeMemoryBuffer(context, &answer); 57 OrthancPluginFreeMemoryBuffer(context, &answer);
59 return true; 58 return true;
125 124
126 return true; 125 return true;
127 } 126 }
128 127
129 128
130 bool CompressUsingDeflate(std::string& compressed, 129 void CompressUsingDeflate(std::string& compressed,
130 OrthancPluginContext* context,
131 const void* uncompressed, 131 const void* uncompressed,
132 size_t uncompressedSize, 132 size_t uncompressedSize)
133 uint8_t compressionLevel) 133 {
134 { 134 OrthancPluginMemoryBuffer tmp;
135 if (uncompressedSize == 0) 135
136 { 136 OrthancPluginErrorCode code = OrthancPluginBufferCompression(
137 compressed.clear(); 137 context, &tmp, uncompressed, uncompressedSize,
138 return true; 138 OrthancPluginCompressionType_Zlib, 0 /*compress*/);
139 } 139
140 140 if (code != OrthancPluginErrorCode_Success)
141 uLongf compressedSize = compressBound(uncompressedSize); 141 {
142 compressed.resize(compressedSize); 142 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code));
143 143 }
144 int error = compress2 144
145 (reinterpret_cast<uint8_t*>(&compressed[0]), 145 try
146 &compressedSize, 146 {
147 const_cast<Bytef *>(static_cast<const Bytef *>(uncompressed)), 147 compressed.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
148 uncompressedSize, 148 }
149 compressionLevel); 149 catch (...)
150 150 {
151 if (error == Z_OK) 151 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
152 { 152 }
153 compressed.resize(compressedSize); 153
154 return true; 154 OrthancPluginFreeMemoryBuffer(context, &tmp);
155 } 155 }
156 else
157 {
158 compressed.clear();
159 return false;
160 }
161 }
162
163 156
164 157
165 const char* GetMimeType(const std::string& path) 158 const char* GetMimeType(const std::string& path)
166 { 159 {
167 size_t dot = path.find_last_of('.'); 160 size_t dot = path.find_last_of('.');
274 return configuration[key].asInt(); 267 return configuration[key].asInt();
275 } 268 }
276 } 269 }
277 270
278 271
279 bool ReadFile(std::string& content, 272 OrthancPluginPixelFormat Convert(Orthanc::PixelFormat format)
280 const std::string& path) 273 {
281 { 274 switch (format)
282 struct stat s; 275 {
283 if (stat(path.c_str(), &s) != 0 || 276 case Orthanc::PixelFormat_Grayscale16:
284 !(s.st_mode & S_IFREG)) 277 return OrthancPluginPixelFormat_Grayscale16;
285 { 278
286 // Either the path does not exist, or it is not a regular file 279 case Orthanc::PixelFormat_Grayscale8:
287 return false; 280 return OrthancPluginPixelFormat_Grayscale8;
288 } 281
289 282 case Orthanc::PixelFormat_RGB24:
290 FILE* fp = fopen(path.c_str(), "rb"); 283 return OrthancPluginPixelFormat_RGB24;
291 if (fp == NULL) 284
292 { 285 case Orthanc::PixelFormat_RGBA32:
293 return false; 286 return OrthancPluginPixelFormat_RGBA32;
294 } 287
295 288 case Orthanc::PixelFormat_SignedGrayscale16:
296 long size; 289 return OrthancPluginPixelFormat_SignedGrayscale16;
297 290
298 if (fseek(fp, 0, SEEK_END) == -1 || 291 default:
299 (size = ftell(fp)) < 0) 292 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
300 { 293 }
301 fclose(fp); 294 }
302 return false; 295
303 } 296
304 297 Orthanc::PixelFormat Convert(OrthancPluginPixelFormat format)
305 content.resize(size); 298 {
306 299 switch (format)
307 if (fseek(fp, 0, SEEK_SET) == -1) 300 {
308 { 301 case OrthancPluginPixelFormat_Grayscale16:
309 fclose(fp); 302 return Orthanc::PixelFormat_Grayscale16;
310 return false; 303
311 } 304 case OrthancPluginPixelFormat_Grayscale8:
312 305 return Orthanc::PixelFormat_Grayscale8;
313 bool ok = true; 306
314 307 case OrthancPluginPixelFormat_RGB24:
315 if (size > 0 && 308 return Orthanc::PixelFormat_RGB24;
316 fread(&content[0], size, 1, fp) != 1) 309
317 { 310 case OrthancPluginPixelFormat_RGBA32:
318 ok = false; 311 return Orthanc::PixelFormat_RGBA32;
319 } 312
320 313 case OrthancPluginPixelFormat_SignedGrayscale16:
321 fclose(fp); 314 return Orthanc::PixelFormat_SignedGrayscale16;
322 315
323 return ok; 316 default:
317 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
318 }
319 }
320
321
322 void WriteJpegToMemory(std::string& result,
323 OrthancPluginContext* context,
324 const Orthanc::ImageAccessor& accessor,
325 uint8_t quality)
326 {
327 OrthancPluginMemoryBuffer tmp;
328
329 OrthancPluginErrorCode code = OrthancPluginCompressJpegImage
330 (context, &tmp, Convert(accessor.GetFormat()),
331 accessor.GetWidth(), accessor.GetHeight(), accessor.GetPitch(),
332 accessor.GetBuffer(), quality);
333
334 if (code != OrthancPluginErrorCode_Success)
335 {
336 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code));
337 }
338
339 try
340 {
341 result.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
342 }
343 catch (...)
344 {
345 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
346 }
347
348 OrthancPluginFreeMemoryBuffer(context, &tmp);
349 }
350
351
352
353 ImageReader::ImageReader(OrthancPluginContext* context,
354 const std::string& image,
355 OrthancPluginImageFormat format) : context_(context)
356 {
357 image_ = OrthancPluginUncompressImage(context_, image.c_str(), image.size(), format);
358
359 if (image_ == NULL)
360 {
361 throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile);
362 }
363 }
364
365
366 ImageReader::~ImageReader()
367 {
368 OrthancPluginFreeImage(context_, image_);
369 }
370
371
372 Orthanc::ImageAccessor ImageReader::GetAccessor() const
373 {
374 Orthanc::ImageAccessor accessor;
375
376 accessor.AssignReadOnly(Convert(OrthancPluginGetImagePixelFormat(context_, image_)),
377 OrthancPluginGetImageWidth(context_, image_),
378 OrthancPluginGetImageHeight(context_, image_),
379 OrthancPluginGetImagePitch(context_, image_),
380 OrthancPluginGetImageBuffer(context_, image_));
381
382 return accessor;
324 } 383 }
325 } 384 }