Mercurial > hg > orthanc
comparison Core/Images/PamReader.cpp @ 2699:52217dc47a4e
new image/pam MIME TYPE supported in /instances/../frames/../image-uint8... routes
author | am@osimis.io |
---|---|
date | Thu, 05 Jul 2018 12:25:00 +0200 |
parents | |
children | f77c7b48f798 |
comparison
equal
deleted
inserted
replaced
2687:2c684841da15 | 2699:52217dc47a4e |
---|---|
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 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "PamReader.h" | |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../Toolbox.h" | |
39 #include <istream> | |
40 #include <sstream> | |
41 #include <fstream> | |
42 #include <endian.h> | |
43 #if ORTHANC_SANDBOXED == 0 | |
44 # include "../SystemToolbox.h" | |
45 #endif | |
46 | |
47 #include <string.h> | |
48 | |
49 namespace Orthanc | |
50 { | |
51 namespace | |
52 { | |
53 void GetPixelFormat(PixelFormat& format, unsigned int& bytesPerChannel, const unsigned int& maxValue, const unsigned int& channelCount, const char* tupleType) | |
54 { | |
55 if (strcmp(tupleType, "GRAYSCALE") == 0 && channelCount == 1) | |
56 { | |
57 if (maxValue == 255) | |
58 { | |
59 format = PixelFormat_Grayscale8; | |
60 bytesPerChannel = 1; | |
61 return; | |
62 } | |
63 else if (maxValue == 65535) | |
64 { | |
65 format = PixelFormat_Grayscale16; | |
66 bytesPerChannel = 2; | |
67 return; | |
68 } | |
69 } | |
70 else if (strcmp(tupleType, "RGB") == 0 && channelCount == 3) | |
71 { | |
72 if (maxValue == 255) | |
73 { | |
74 format = PixelFormat_RGB24; | |
75 bytesPerChannel = 1; | |
76 return; | |
77 } | |
78 else if (maxValue == 65535) | |
79 { | |
80 format = PixelFormat_RGB48; | |
81 bytesPerChannel = 2; | |
82 return; | |
83 } | |
84 } | |
85 throw OrthancException(ErrorCode_NotImplemented); | |
86 } | |
87 | |
88 void ReadDelimiter(std::istream& input, const char* expectedDelimiter) | |
89 { | |
90 std::string delimiter; | |
91 input >> delimiter; | |
92 if (delimiter != expectedDelimiter) | |
93 { | |
94 throw OrthancException(ErrorCode_BadFileFormat); | |
95 } | |
96 } | |
97 | |
98 unsigned int ReadKeyValueUint(std::istream& input, const char* expectedKey) | |
99 { | |
100 std::string key; | |
101 unsigned int value; | |
102 input >> key >> value; | |
103 if (key != expectedKey) | |
104 { | |
105 throw OrthancException(ErrorCode_BadFileFormat); | |
106 } | |
107 return value; | |
108 } | |
109 | |
110 std::string ReadKeyValueString(std::istream& input, const char* expectedKey) | |
111 { | |
112 std::string key; | |
113 std::string value; | |
114 input >> key >> value; | |
115 if (key != expectedKey) | |
116 { | |
117 throw OrthancException(ErrorCode_BadFileFormat); | |
118 } | |
119 return value; | |
120 } | |
121 } | |
122 | |
123 void PamReader::ReadFromStream(std::istream& input) | |
124 { | |
125 ReadDelimiter(input, "P7"); | |
126 unsigned int width = ReadKeyValueUint(input, "WIDTH"); | |
127 unsigned int height = ReadKeyValueUint(input, "HEIGHT"); | |
128 unsigned int channelCount = ReadKeyValueUint(input, "DEPTH"); | |
129 unsigned int maxValue = ReadKeyValueUint(input, "MAXVAL"); | |
130 std::string tupleType = ReadKeyValueString(input, "TUPLTYPE"); | |
131 ReadDelimiter(input, "ENDHDR"); | |
132 // skip last EOL | |
133 char tmp[16]; | |
134 input.getline(tmp, 16); | |
135 | |
136 unsigned int bytesPerChannel; | |
137 PixelFormat format; | |
138 GetPixelFormat(format, bytesPerChannel, maxValue, channelCount, tupleType.c_str()); | |
139 | |
140 // read the pixels data | |
141 unsigned int sizeInBytes = width * height * channelCount * bytesPerChannel; | |
142 data_.reserve(sizeInBytes); | |
143 input.read(data_.data(), sizeInBytes); | |
144 | |
145 AssignWritable(format, width, height, width * channelCount * bytesPerChannel, data_.data()); | |
146 | |
147 // swap bytes | |
148 if (Toolbox::DetectEndianness() == Endianness_Little && bytesPerChannel == 2) | |
149 { | |
150 uint16_t* pixel = NULL; | |
151 for (unsigned int h = 0; h < height; ++h) | |
152 { | |
153 pixel = reinterpret_cast<uint16_t*> (data_.data() + h * width * channelCount * bytesPerChannel); | |
154 for (unsigned int w = 0; w < (width * channelCount); ++w, ++pixel) | |
155 { | |
156 *pixel = htobe16(*pixel); | |
157 } | |
158 } | |
159 } | |
160 | |
161 } | |
162 | |
163 #if ORTHANC_SANDBOXED == 0 | |
164 void PamReader::ReadFromFile(const std::string& filename) | |
165 { | |
166 std::ifstream inputStream(filename, std::ofstream::binary); | |
167 ReadFromStream(inputStream); | |
168 } | |
169 #endif | |
170 | |
171 void PamReader::ReadFromMemory(const void* buffer, | |
172 size_t size) | |
173 { | |
174 std::istringstream inputStream(std::string(reinterpret_cast<const char*>(buffer), size)); | |
175 ReadFromStream(inputStream); | |
176 } | |
177 | |
178 void PamReader::ReadFromMemory(const std::string& buffer) | |
179 { | |
180 std::istringstream inputStream(buffer); | |
181 ReadFromStream(inputStream); | |
182 } | |
183 | |
184 } |