comparison Plugins/Samples/GdcmDecoder/GdcmDecoderCache.cpp @ 1834:b1a6f49b21dd

GDCM decoder sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Nov 2015 12:53:32 +0100
parents
children 2faa2abbf311
comparison
equal deleted inserted replaced
1833:47d032c48818 1834:b1a6f49b21dd
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "GdcmDecoderCache.h"
22
23 #include "OrthancImageWrapper.h"
24
25 namespace OrthancPlugins
26 {
27 std::string GdcmDecoderCache::ComputeMd5(OrthancPluginContext* context,
28 const void* dicom,
29 size_t size)
30 {
31 std::string result;
32
33 char* md5 = OrthancPluginComputeMd5(context, dicom, size);
34
35 if (md5 == NULL)
36 {
37 throw std::runtime_error("Cannot compute MD5 hash");
38 }
39
40 bool ok = false;
41 try
42 {
43 result.assign(md5);
44 ok = true;
45 }
46 catch (...)
47 {
48 }
49
50 OrthancPluginFreeString(context, md5);
51
52 if (!ok)
53 {
54 throw std::runtime_error("Not enough memory");
55 }
56 else
57 {
58 return result;
59 }
60 }
61
62
63 OrthancPluginImage* GdcmDecoderCache::Decode(OrthancPluginContext* context,
64 const void* dicom,
65 const uint32_t size,
66 uint32_t frameIndex)
67 {
68 std::string md5 = ComputeMd5(context, dicom, size);
69
70 // First check whether the previously decoded image is the same
71 // as this one
72 {
73 boost::mutex::scoped_lock lock(mutex_);
74
75 if (decoder_.get() != NULL &&
76 size_ == size &&
77 md5_ == md5)
78 {
79 // This is the same image: Reuse the previous decoding
80 return decoder_->Decode(context, frameIndex);
81 }
82 }
83
84 // This is not the same image
85 std::auto_ptr<GdcmImageDecoder> decoder(new GdcmImageDecoder(dicom, size));
86 OrthancImageWrapper image(context, decoder->Decode(context, frameIndex));
87
88 {
89 // Cache the newly created decoder for further use
90 boost::mutex::scoped_lock lock(mutex_);
91 decoder_ = decoder;
92 size_ = size;
93 md5_ = md5;
94 }
95
96 return image.Release();
97 }
98 }