comparison Framework/Outputs/InMemoryTiledImage.cpp @ 0:4a7a53257c7d

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 22 Oct 2016 21:48:33 +0200
parents
children 7a88c614be04
comparison
equal deleted inserted replaced
-1:000000000000 0:4a7a53257c7d
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 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the 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 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "InMemoryTiledImage.h"
22
23 #include "../ImageToolbox.h"
24 #include "../Orthanc/Core/Logging.h"
25 #include "../Orthanc/Core/OrthancException.h"
26
27 namespace OrthancWSI
28 {
29 static void CheckLevel(unsigned int level)
30 {
31 if (level != 0)
32 {
33 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
34 }
35 }
36
37
38 InMemoryTiledImage::InMemoryTiledImage(Orthanc::PixelFormat format,
39 unsigned int countTilesX,
40 unsigned int countTilesY,
41 unsigned int tileWidth,
42 unsigned int tileHeight) :
43 format_(format),
44 countTilesX_(countTilesX),
45 countTilesY_(countTilesY),
46 tileWidth_(tileWidth),
47 tileHeight_(tileHeight)
48 {
49 }
50
51
52 InMemoryTiledImage::~InMemoryTiledImage()
53 {
54 for (Tiles::iterator it = tiles_.begin(); it != tiles_.end(); ++it)
55 {
56 delete it->second;
57 }
58 }
59
60
61 unsigned int InMemoryTiledImage::GetCountTilesX(unsigned int level) const
62 {
63 CheckLevel(level);
64 return countTilesX_;
65 }
66
67
68 unsigned int InMemoryTiledImage::GetCountTilesY(unsigned int level) const
69 {
70 CheckLevel(level);
71 return countTilesY_;
72 }
73
74
75 unsigned int InMemoryTiledImage::GetLevelWidth(unsigned int level) const
76 {
77 CheckLevel(level);
78 return tileWidth_ * countTilesX_;
79 }
80
81
82 unsigned int InMemoryTiledImage::GetLevelHeight(unsigned int level) const
83 {
84 CheckLevel(level);
85 return tileHeight_ * countTilesY_;
86 }
87
88
89 bool InMemoryTiledImage::ReadRawTile(std::string& tile,
90 unsigned int level,
91 unsigned int tileX,
92 unsigned int tileY)
93 {
94 CheckLevel(level);
95 return false; // Unavailable
96 }
97
98
99 Orthanc::ImageAccessor* InMemoryTiledImage::DecodeTile(unsigned int level,
100 unsigned int tileX,
101 unsigned int tileY)
102 {
103 CheckLevel(level);
104
105 if (tileX >= countTilesX_ ||
106 tileY >= countTilesY_)
107 {
108 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
109 }
110
111 {
112 boost::mutex::scoped_lock lock(mutex_);
113
114 Tiles::const_iterator it = tiles_.find(std::make_pair(tileX, tileY));
115 if (it != tiles_.end())
116 {
117 return new Orthanc::ImageAccessor(*it->second);
118 }
119 else
120 {
121 LOG(ERROR) << "The following tile has not been set: " << tileX << "," << tileY;
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
123 }
124 }
125 }
126
127
128 void InMemoryTiledImage::WriteRawTile(const std::string& raw,
129 ImageCompression compression,
130 unsigned int level,
131 unsigned int tileX,
132 unsigned int tileY)
133 {
134 std::auto_ptr<Orthanc::ImageAccessor> decoded(ImageToolbox::DecodeTile(raw, compression));
135 EncodeTile(*decoded, level, tileX, tileY);
136 }
137
138
139 void InMemoryTiledImage::EncodeTile(const Orthanc::ImageAccessor& tile,
140 unsigned int level,
141 unsigned int tileX,
142 unsigned int tileY)
143 {
144 CheckLevel(level);
145
146 if (tileX >= countTilesX_ ||
147 tileY >= countTilesY_)
148 {
149 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
150 }
151
152 {
153 boost::mutex::scoped_lock lock(mutex_);
154
155 Tiles::iterator it = tiles_.find(std::make_pair(tileX, tileY));
156 if (it == tiles_.end())
157 {
158 tiles_[std::make_pair(tileX, tileY)] = ImageToolbox::Clone(tile);
159 }
160 else
161 {
162 if (it->second)
163 {
164 delete it->second;
165 }
166
167 it->second = ImageToolbox::Clone(tile);
168 }
169 }
170 }
171 }