File: ViewerToolbox.cpp

package info (click to toggle)
orthanc-webviewer 2.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,932 kB
  • sloc: javascript: 21,840; cpp: 7,296; python: 345; sh: 93; makefile: 60
file content (392 lines) | stat: -rw-r--r-- 10,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
 * Orthanc - A Lightweight, RESTful DICOM Store
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2023 Osimis S.A., Belgium
 * Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
 * Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "ViewerToolbox.h"

#include <Logging.h>
#include <OrthancException.h>
#include <Toolbox.h>

// To gain access to ORTHANC_PLUGINS_VERSION_IS_ABOVE if Orthanc SDK <= 1.3.0
#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"

#include <stdexcept>
#include <boost/lexical_cast.hpp>
#include <sys/stat.h>

namespace OrthancPlugins
{
  bool GetStringFromOrthanc(std::string& content,
                            OrthancPluginContext* context,
                            const std::string& uri)
  {
    OrthancPluginMemoryBuffer answer;

    if (OrthancPluginRestApiGet(context, &answer, uri.c_str()))
    {
      return false;
    }

    if (answer.size)
    {
      try
      {
        content.assign(reinterpret_cast<const char*>(answer.data), answer.size);
      }
      catch (std::bad_alloc&)
      {
        OrthancPluginFreeMemoryBuffer(context, &answer);
        throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
      }
    }

    OrthancPluginFreeMemoryBuffer(context, &answer);
    return true;
  }


  bool GetJsonFromOrthanc(Json::Value& json,
                          OrthancPluginContext* context,
                          const std::string& uri)
  {
    OrthancPluginMemoryBuffer answer;

    if (OrthancPluginRestApiGet(context, &answer, uri.c_str()))
    {
      return false;
    }

    if (answer.size)
    {
      try
      {
        if (!Orthanc::Toolbox::ReadJsonWithoutComments(json, answer.data, answer.size))
        {
          return false;
        }
      }
      catch (std::runtime_error&)
      {
        OrthancPluginFreeMemoryBuffer(context, &answer);
        return false;
      }
    }

    OrthancPluginFreeMemoryBuffer(context, &answer);
    return true;
  }




  bool TokenizeVector(std::vector<float>& result,
                      const std::string& value,
                      unsigned int expectedSize)
  {
    std::vector<std::string> tokens;
    Orthanc::Toolbox::TokenizeString(tokens, value, '\\');

    if (tokens.size() != expectedSize)
    {
      return false;
    }

    result.resize(tokens.size());

    for (size_t i = 0; i < tokens.size(); i++)
    {
      try
      {
        result[i] = boost::lexical_cast<float>(tokens[i]);
      }
      catch (boost::bad_lexical_cast&)
      {
        return false;
      }
    }

    return true;
  }


  void CompressUsingDeflate(std::string& compressed,
                            OrthancPluginContext* context,
                            const void* uncompressed,
                            size_t uncompressedSize)
  {
    OrthancPluginMemoryBuffer tmp;
   
    OrthancPluginErrorCode code = OrthancPluginBufferCompression(
      context, &tmp, uncompressed, uncompressedSize, 
      OrthancPluginCompressionType_Zlib, 0 /*compress*/);
      
    if (code != OrthancPluginErrorCode_Success)
    {
      throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code));
    }

    try
    {
      compressed.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
    }
    catch (...)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
    }

    OrthancPluginFreeMemoryBuffer(context, &tmp);
  }


  const char* GetMimeType(const std::string& path)
  {
    size_t dot = path.find_last_of('.');

    std::string extension = (dot == std::string::npos) ? "" : path.substr(dot);
    std::transform(extension.begin(), extension.end(), extension.begin(), tolower);

    if (extension == ".html")
    {
      return "text/html";
    }
    else if (extension == ".css")
    {
      return "text/css";
    }
    else if (extension == ".js")
    {
      return "application/javascript";
    }
    else if (extension == ".gif")
    {
      return "image/gif";
    }
    else if (extension == ".svg")
    {
      return "image/svg+xml";
    }
    else if (extension == ".json")
    {
      return "application/json";
    }
    else if (extension == ".xml")
    {
      return "application/xml";
    }
    else if (extension == ".png")
    {
      return "image/png";
    }
    else if (extension == ".jpg" || extension == ".jpeg")
    {
      return "image/jpeg";
    }
    else
    {
      return "application/octet-stream";
    }
  }


  bool ReadConfiguration(Json::Value& configuration,
                         OrthancPluginContext* context)
  {
    std::string s;

    {
      char* tmp = OrthancPluginGetConfiguration(context);
      if (tmp == NULL)
      {
        LOG(ERROR) << "Error while retrieving the configuration from Orthanc";
        return false;
      }

      s.assign(tmp);
      OrthancPluginFreeString(context, tmp);      
    }

    if (Orthanc::Toolbox::ReadJson(configuration, s))
    {
      return true;
    }
    else
    {
      LOG(ERROR) << "Unable to parse the configuration";
      return false;
    }
  }


  std::string GetStringValue(const Json::Value& configuration,
                             const std::string& key,
                             const std::string& defaultValue)
  {
    if (configuration.type() != Json::objectValue ||
        !configuration.isMember(key) ||
        configuration[key].type() != Json::stringValue)
    {
      return defaultValue;
    }
    else
    {
      return configuration[key].asString();
    }
  }  


  int GetIntegerValue(const Json::Value& configuration,
                      const std::string& key,
                      int defaultValue)
  {
    if (configuration.type() != Json::objectValue ||
        !configuration.isMember(key) ||
        configuration[key].type() != Json::intValue)
    {
      return defaultValue;
    }
    else
    {
      return configuration[key].asInt();
    }
  }


  OrthancPluginPixelFormat Convert(Orthanc::PixelFormat format)
  {
    switch (format)
    {
      case Orthanc::PixelFormat_Grayscale16:
        return OrthancPluginPixelFormat_Grayscale16;

      case Orthanc::PixelFormat_Grayscale8:
        return OrthancPluginPixelFormat_Grayscale8;

      case Orthanc::PixelFormat_RGB24:
        return OrthancPluginPixelFormat_RGB24;

#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1)
      case Orthanc::PixelFormat_RGB48:
        return OrthancPluginPixelFormat_RGB48;
#endif

      case Orthanc::PixelFormat_RGBA32:
        return OrthancPluginPixelFormat_RGBA32;

      case Orthanc::PixelFormat_SignedGrayscale16:
        return OrthancPluginPixelFormat_SignedGrayscale16;

      default:
        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
    }
  }


  Orthanc::PixelFormat Convert(OrthancPluginPixelFormat format)
  {
    switch (format)
    {
      case OrthancPluginPixelFormat_Grayscale16:
        return Orthanc::PixelFormat_Grayscale16;

      case OrthancPluginPixelFormat_Grayscale8:
        return Orthanc::PixelFormat_Grayscale8;

      case OrthancPluginPixelFormat_RGB24:
        return Orthanc::PixelFormat_RGB24;

#if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1)
      case OrthancPluginPixelFormat_RGB48:
        return Orthanc::PixelFormat_RGB48;
#endif

      case OrthancPluginPixelFormat_RGBA32:
        return Orthanc::PixelFormat_RGBA32;

      case OrthancPluginPixelFormat_SignedGrayscale16:
        return Orthanc::PixelFormat_SignedGrayscale16;

      default:
        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
    }
  }


  void WriteJpegToMemory(std::string& result,
                         OrthancPluginContext* context,
                         const Orthanc::ImageAccessor& accessor,
                         uint8_t quality)
  {
    OrthancPluginMemoryBuffer tmp;
   
    OrthancPluginErrorCode code = OrthancPluginCompressJpegImage
      (context, &tmp, Convert(accessor.GetFormat()), 
       accessor.GetWidth(), accessor.GetHeight(), accessor.GetPitch(),
       accessor.GetConstBuffer(), quality);

    if (code != OrthancPluginErrorCode_Success)
    {
      throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code));
    }

    try
    {
      result.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
    }
    catch (...)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_NotEnoughMemory);
    }

    OrthancPluginFreeMemoryBuffer(context, &tmp);
  }



  ImageReader::ImageReader(OrthancPluginContext* context,
                           const std::string& image,
                           OrthancPluginImageFormat format) : context_(context)
  {
    image_ = OrthancPluginUncompressImage(context_, image.c_str(), image.size(), format);

    if (image_ == NULL)
    {
      throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile);
    }
  }


  ImageReader::~ImageReader()
  {
    OrthancPluginFreeImage(context_, image_);
  }


  void ImageReader::GetAccessor(Orthanc::ImageAccessor& target) const
  {
    target.AssignReadOnly(Convert(OrthancPluginGetImagePixelFormat(context_, image_)),
                          OrthancPluginGetImageWidth(context_, image_),
                          OrthancPluginGetImageHeight(context_, image_),
                          OrthancPluginGetImagePitch(context_, image_),
                          OrthancPluginGetImageBuffer(context_, image_));
  }
}