comparison Plugins/Samples/GdcmDecoder/GdcmDecoderCache.cpp @ 2884:497a637366b4 db-changes

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