comparison Core/ImageFormats/ImageAccessor.cpp @ 874:87791ebc1f50

download matlab images
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 11 Jun 2014 12:23:02 +0200
parents a546b05a43da
children 80d4f1618b33
comparison
equal deleted inserted replaced
873:fc34356283e1 874:87791ebc1f50
32 32
33 #include "../PrecompiledHeaders.h" 33 #include "../PrecompiledHeaders.h"
34 #include "ImageAccessor.h" 34 #include "ImageAccessor.h"
35 35
36 #include "../OrthancException.h" 36 #include "../OrthancException.h"
37 #include "../ChunkedBuffer.h"
37 38
38 #include <stdint.h> 39 #include <stdint.h>
39 #include <cassert> 40 #include <cassert>
40 #include <glog/logging.h> 41 #include <glog/logging.h>
42 #include <boost/lexical_cast.hpp>
41 43
42 namespace Orthanc 44 namespace Orthanc
43 { 45 {
46 template <typename PixelType>
47 static void ToMatlabStringInternal(ChunkedBuffer& target,
48 const ImageAccessor& source)
49 {
50 target.AddChunk("double([ ");
51
52 for (unsigned int y = 0; y < source.GetHeight(); y++)
53 {
54 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y));
55
56 std::string s;
57 if (y > 0)
58 {
59 s = "; ";
60 }
61
62 s.reserve(source.GetWidth() * 8);
63
64 for (unsigned int x = 0; x < source.GetWidth(); x++, p++)
65 {
66 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
67 }
68
69 target.AddChunk(s);
70 }
71
72 target.AddChunk("])");
73 }
74
75
76 static void RGB24ToMatlabString(ChunkedBuffer& target,
77 const ImageAccessor& source)
78 {
79 assert(source.GetFormat() == PixelFormat_RGB24);
80
81 target.AddChunk("double(permute(reshape([ ");
82
83 for (unsigned int y = 0; y < source.GetHeight(); y++)
84 {
85 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
86
87 std::string s;
88 s.reserve(source.GetWidth() * 3 * 8);
89
90 for (unsigned int x = 0; x < 3 * source.GetWidth(); x++, p++)
91 {
92 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
93 }
94
95 target.AddChunk(s);
96 }
97
98 target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(source.GetHeight()) +
99 " " + boost::lexical_cast<std::string>(source.GetWidth()) + " ]), [ 3 2 1 ]))");
100 }
101
102
44 void* ImageAccessor::GetBuffer() const 103 void* ImageAccessor::GetBuffer() const
45 { 104 {
46 if (readOnly_) 105 if (readOnly_)
47 { 106 {
48 LOG(ERROR) << "Trying to write on a read-only image"; 107 LOG(ERROR) << "Trying to write on a read-only image";
126 pitch_ = pitch; 185 pitch_ = pitch;
127 buffer_ = buffer; 186 buffer_ = buffer;
128 187
129 assert(GetBytesPerPixel(format_) * width_ <= pitch_); 188 assert(GetBytesPerPixel(format_) * width_ <= pitch_);
130 } 189 }
190
191
192 void ImageAccessor::ToMatlabString(std::string& target) const
193 {
194 ChunkedBuffer buffer;
195
196 switch (GetFormat())
197 {
198 case PixelFormat_Grayscale8:
199 ToMatlabStringInternal<uint8_t>(buffer, *this);
200 break;
201
202 case PixelFormat_Grayscale16:
203 ToMatlabStringInternal<uint16_t>(buffer, *this);
204 break;
205
206 case PixelFormat_SignedGrayscale16:
207 ToMatlabStringInternal<int16_t>(buffer, *this);
208 break;
209
210 case PixelFormat_RGB24:
211 RGB24ToMatlabString(buffer, *this);
212 break;
213
214 default:
215 throw OrthancException(ErrorCode_NotImplemented);
216 }
217
218 buffer.Flatten(target);
219 }
220
131 } 221 }