comparison Core/Images/ImageAccessor.cpp @ 1612:96582230ddcb

Core/ImageFormats folder renamed as Core/Images
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Sep 2015 16:36:18 +0200
parents Core/ImageFormats/ImageAccessor.cpp@e460341872dc
children b1291df2f780
comparison
equal deleted inserted replaced
1611:5e9b2aac8b89 1612:96582230ddcb
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 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "../PrecompiledHeaders.h"
34 #include "ImageAccessor.h"
35
36 #include "../Logging.h"
37 #include "../OrthancException.h"
38 #include "../ChunkedBuffer.h"
39
40 #include <stdint.h>
41 #include <cassert>
42 #include <boost/lexical_cast.hpp>
43
44
45
46 namespace Orthanc
47 {
48 template <typename PixelType>
49 static void ToMatlabStringInternal(ChunkedBuffer& target,
50 const ImageAccessor& source)
51 {
52 target.AddChunk("double([ ");
53
54 for (unsigned int y = 0; y < source.GetHeight(); y++)
55 {
56 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y));
57
58 std::string s;
59 if (y > 0)
60 {
61 s = "; ";
62 }
63
64 s.reserve(source.GetWidth() * 8);
65
66 for (unsigned int x = 0; x < source.GetWidth(); x++, p++)
67 {
68 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
69 }
70
71 target.AddChunk(s);
72 }
73
74 target.AddChunk("])");
75 }
76
77
78 static void RGB24ToMatlabString(ChunkedBuffer& target,
79 const ImageAccessor& source)
80 {
81 assert(source.GetFormat() == PixelFormat_RGB24);
82
83 target.AddChunk("double(permute(reshape([ ");
84
85 for (unsigned int y = 0; y < source.GetHeight(); y++)
86 {
87 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
88
89 std::string s;
90 s.reserve(source.GetWidth() * 3 * 8);
91
92 for (unsigned int x = 0; x < 3 * source.GetWidth(); x++, p++)
93 {
94 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
95 }
96
97 target.AddChunk(s);
98 }
99
100 target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(source.GetHeight()) +
101 " " + boost::lexical_cast<std::string>(source.GetWidth()) + " ]), [ 3 2 1 ]))");
102 }
103
104
105 void* ImageAccessor::GetBuffer() const
106 {
107 if (readOnly_)
108 {
109 #if ORTHANC_ENABLE_LOGGING == 1
110 LOG(ERROR) << "Trying to write on a read-only image";
111 #endif
112
113 throw OrthancException(ErrorCode_ReadOnly);
114 }
115
116 return buffer_;
117 }
118
119
120 const void* ImageAccessor::GetConstRow(unsigned int y) const
121 {
122 if (buffer_ != NULL)
123 {
124 return reinterpret_cast<const uint8_t*>(buffer_) + y * pitch_;
125 }
126 else
127 {
128 return NULL;
129 }
130 }
131
132
133 void* ImageAccessor::GetRow(unsigned int y) const
134 {
135 if (readOnly_)
136 {
137 #if ORTHANC_ENABLE_LOGGING == 1
138 LOG(ERROR) << "Trying to write on a read-only image";
139 #endif
140
141 throw OrthancException(ErrorCode_ReadOnly);
142 }
143
144 if (buffer_ != NULL)
145 {
146 return reinterpret_cast<uint8_t*>(buffer_) + y * pitch_;
147 }
148 else
149 {
150 return NULL;
151 }
152 }
153
154
155 void ImageAccessor::AssignEmpty(PixelFormat format)
156 {
157 readOnly_ = false;
158 format_ = format;
159 width_ = 0;
160 height_ = 0;
161 pitch_ = 0;
162 buffer_ = NULL;
163 }
164
165
166 void ImageAccessor::AssignReadOnly(PixelFormat format,
167 unsigned int width,
168 unsigned int height,
169 unsigned int pitch,
170 const void *buffer)
171 {
172 readOnly_ = true;
173 format_ = format;
174 width_ = width;
175 height_ = height;
176 pitch_ = pitch;
177 buffer_ = const_cast<void*>(buffer);
178
179 assert(GetBytesPerPixel() * width_ <= pitch_);
180 }
181
182
183 void ImageAccessor::AssignWritable(PixelFormat format,
184 unsigned int width,
185 unsigned int height,
186 unsigned int pitch,
187 void *buffer)
188 {
189 readOnly_ = false;
190 format_ = format;
191 width_ = width;
192 height_ = height;
193 pitch_ = pitch;
194 buffer_ = buffer;
195
196 assert(GetBytesPerPixel() * width_ <= pitch_);
197 }
198
199
200 void ImageAccessor::ToMatlabString(std::string& target) const
201 {
202 ChunkedBuffer buffer;
203
204 switch (GetFormat())
205 {
206 case PixelFormat_Grayscale8:
207 ToMatlabStringInternal<uint8_t>(buffer, *this);
208 break;
209
210 case PixelFormat_Grayscale16:
211 ToMatlabStringInternal<uint16_t>(buffer, *this);
212 break;
213
214 case PixelFormat_SignedGrayscale16:
215 ToMatlabStringInternal<int16_t>(buffer, *this);
216 break;
217
218 case PixelFormat_RGB24:
219 RGB24ToMatlabString(buffer, *this);
220 break;
221
222 default:
223 throw OrthancException(ErrorCode_NotImplemented);
224 }
225
226 buffer.Flatten(target);
227 }
228
229 }