comparison Orthanc/Core/ImageFormats/ImageAccessor.cpp @ 3:d5027f9f676a

fix build
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 01 Jun 2015 15:12:22 +0200
parents
children e59bf2554e59
comparison
equal deleted inserted replaced
2:8f22ed9d48d5 3:d5027f9f676a
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 "../OrthancException.h"
37 #include "../ChunkedBuffer.h"
38
39 #include <stdint.h>
40 #include <cassert>
41 #include <boost/lexical_cast.hpp>
42
43 #if HAVE_GOOGLE_LOG == 1
44 #include <glog/logging.h>
45 #endif
46
47
48 namespace Orthanc
49 {
50 template <typename PixelType>
51 static void ToMatlabStringInternal(ChunkedBuffer& target,
52 const ImageAccessor& source)
53 {
54 target.AddChunk("double([ ");
55
56 for (unsigned int y = 0; y < source.GetHeight(); y++)
57 {
58 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y));
59
60 std::string s;
61 if (y > 0)
62 {
63 s = "; ";
64 }
65
66 s.reserve(source.GetWidth() * 8);
67
68 for (unsigned int x = 0; x < source.GetWidth(); x++, p++)
69 {
70 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
71 }
72
73 target.AddChunk(s);
74 }
75
76 target.AddChunk("])");
77 }
78
79
80 static void RGB24ToMatlabString(ChunkedBuffer& target,
81 const ImageAccessor& source)
82 {
83 assert(source.GetFormat() == PixelFormat_RGB24);
84
85 target.AddChunk("double(permute(reshape([ ");
86
87 for (unsigned int y = 0; y < source.GetHeight(); y++)
88 {
89 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
90
91 std::string s;
92 s.reserve(source.GetWidth() * 3 * 8);
93
94 for (unsigned int x = 0; x < 3 * source.GetWidth(); x++, p++)
95 {
96 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
97 }
98
99 target.AddChunk(s);
100 }
101
102 target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(source.GetHeight()) +
103 " " + boost::lexical_cast<std::string>(source.GetWidth()) + " ]), [ 3 2 1 ]))");
104 }
105
106
107 void* ImageAccessor::GetBuffer() const
108 {
109 if (readOnly_)
110 {
111 #if HAVE_GOOGLE_LOG == 1
112 LOG(ERROR) << "Trying to write on a read-only image";
113 #endif
114
115 throw OrthancException(ErrorCode_ReadOnly);
116 }
117
118 return buffer_;
119 }
120
121
122 const void* ImageAccessor::GetConstRow(unsigned int y) const
123 {
124 if (buffer_ != NULL)
125 {
126 return reinterpret_cast<const uint8_t*>(buffer_) + y * pitch_;
127 }
128 else
129 {
130 return NULL;
131 }
132 }
133
134
135 void* ImageAccessor::GetRow(unsigned int y) const
136 {
137 if (readOnly_)
138 {
139 #if HAVE_GOOGLE_LOG == 1
140 LOG(ERROR) << "Trying to write on a read-only image";
141 #endif
142
143 throw OrthancException(ErrorCode_ReadOnly);
144 }
145
146 if (buffer_ != NULL)
147 {
148 return reinterpret_cast<uint8_t*>(buffer_) + y * pitch_;
149 }
150 else
151 {
152 return NULL;
153 }
154 }
155
156
157 void ImageAccessor::AssignEmpty(PixelFormat format)
158 {
159 readOnly_ = false;
160 format_ = format;
161 width_ = 0;
162 height_ = 0;
163 pitch_ = 0;
164 buffer_ = NULL;
165 }
166
167
168 void ImageAccessor::AssignReadOnly(PixelFormat format,
169 unsigned int width,
170 unsigned int height,
171 unsigned int pitch,
172 const void *buffer)
173 {
174 readOnly_ = true;
175 format_ = format;
176 width_ = width;
177 height_ = height;
178 pitch_ = pitch;
179 buffer_ = const_cast<void*>(buffer);
180
181 assert(GetBytesPerPixel() * width_ <= pitch_);
182 }
183
184
185 void ImageAccessor::AssignWritable(PixelFormat format,
186 unsigned int width,
187 unsigned int height,
188 unsigned int pitch,
189 void *buffer)
190 {
191 readOnly_ = false;
192 format_ = format;
193 width_ = width;
194 height_ = height;
195 pitch_ = pitch;
196 buffer_ = buffer;
197
198 assert(GetBytesPerPixel() * width_ <= pitch_);
199 }
200
201
202 void ImageAccessor::ToMatlabString(std::string& target) const
203 {
204 ChunkedBuffer buffer;
205
206 switch (GetFormat())
207 {
208 case PixelFormat_Grayscale8:
209 ToMatlabStringInternal<uint8_t>(buffer, *this);
210 break;
211
212 case PixelFormat_Grayscale16:
213 ToMatlabStringInternal<uint16_t>(buffer, *this);
214 break;
215
216 case PixelFormat_SignedGrayscale16:
217 ToMatlabStringInternal<int16_t>(buffer, *this);
218 break;
219
220 case PixelFormat_RGB24:
221 RGB24ToMatlabString(buffer, *this);
222 break;
223
224 default:
225 throw OrthancException(ErrorCode_NotImplemented);
226 }
227
228 buffer.Flatten(target);
229 }
230
231 }