Mercurial > hg > orthanc
comparison Core/Images/PamWriter.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 "PamWriter.h" | |
36 | |
37 #include <vector> | |
38 #include <stdint.h> | |
39 #include <iostream> | |
40 #include <sstream> | |
41 #include <fstream> | |
42 #include <endian.h> | |
43 #include "../OrthancException.h" | |
44 #include "../ChunkedBuffer.h" | |
45 #include "../Toolbox.h" | |
46 | |
47 #if ORTHANC_SANDBOXED == 0 | |
48 # include "../SystemToolbox.h" | |
49 #endif | |
50 | |
51 | |
52 namespace Orthanc | |
53 { | |
54 namespace | |
55 { | |
56 void GetPixelFormatInfo(const PixelFormat& format, unsigned int& maxValue, unsigned int& channelCount, unsigned int& bytesPerChannel, const char*& tupleType) { | |
57 maxValue = 255; | |
58 channelCount = 1; | |
59 bytesPerChannel = 1; | |
60 tupleType = NULL; | |
61 | |
62 switch (format) { | |
63 case PixelFormat_Grayscale8: | |
64 maxValue = 255; | |
65 channelCount = 1; | |
66 bytesPerChannel = 1; | |
67 tupleType = "GRAYSCALE"; | |
68 break; | |
69 case PixelFormat_Grayscale16: | |
70 maxValue = 65535; | |
71 channelCount = 1; | |
72 bytesPerChannel = 2; | |
73 tupleType = "GRAYSCALE"; | |
74 break; | |
75 case PixelFormat_RGB24: | |
76 maxValue = 255; | |
77 channelCount = 3; | |
78 bytesPerChannel = 1; | |
79 tupleType = "RGB"; | |
80 break; | |
81 case PixelFormat_RGB48: | |
82 maxValue = 255; | |
83 channelCount = 3; | |
84 bytesPerChannel = 2; | |
85 tupleType = "RGB"; | |
86 break; | |
87 default: | |
88 throw OrthancException(ErrorCode_NotImplemented); | |
89 } | |
90 | |
91 } | |
92 | |
93 void WriteToStream(std::ostream& output, | |
94 unsigned int width, | |
95 unsigned int height, | |
96 unsigned int pitch, | |
97 PixelFormat format, | |
98 const void* buffer) | |
99 { | |
100 unsigned int maxValue = 255; | |
101 unsigned int channelCount = 1; | |
102 unsigned int bytesPerChannel = 1; | |
103 const char* tupleType = "GRAYSCALE"; | |
104 GetPixelFormatInfo(format, maxValue, channelCount, bytesPerChannel, tupleType); | |
105 | |
106 output << "P7" << "\n"; | |
107 output << "WIDTH " << width << "\n"; | |
108 output << "HEIGHT " << height << "\n"; | |
109 output << "DEPTH " << channelCount << "\n"; | |
110 output << "MAXVAL " << maxValue << "\n"; | |
111 output << "TUPLTYPE " << tupleType << "\n"; | |
112 output << "ENDHDR" << "\n"; | |
113 | |
114 if (Toolbox::DetectEndianness() == Endianness_Little && bytesPerChannel == 2) | |
115 { | |
116 uint16_t tmp; | |
117 const uint16_t* pixel = NULL; | |
118 for (unsigned int h = 0; h < height; ++h) | |
119 { | |
120 pixel = reinterpret_cast<const uint16_t*> (reinterpret_cast<const uint8_t*>(buffer) + h * pitch); | |
121 for (unsigned int w = 0; w < (width * channelCount); ++w, ++pixel) | |
122 { | |
123 tmp = htobe16(*pixel); | |
124 output.write(reinterpret_cast<const char*>(&tmp), 2); | |
125 } | |
126 } | |
127 } | |
128 else | |
129 { | |
130 for (unsigned int h = 0; h < height; ++h) | |
131 { | |
132 output.write(reinterpret_cast<const char*>(reinterpret_cast<const uint8_t*>(buffer) + h * pitch), channelCount * bytesPerChannel * width); | |
133 } | |
134 } | |
135 | |
136 } | |
137 | |
138 } | |
139 | |
140 #if ORTHANC_SANDBOXED == 0 | |
141 void PamWriter::WriteToFileInternal(const std::string& filename, | |
142 unsigned int width, | |
143 unsigned int height, | |
144 unsigned int pitch, | |
145 PixelFormat format, | |
146 const void* buffer) | |
147 { | |
148 std::ofstream outfile (filename, std::ofstream::binary); | |
149 | |
150 WriteToStream(outfile, width, height, pitch, format, buffer); | |
151 outfile.close(); | |
152 } | |
153 #endif | |
154 | |
155 | |
156 void PamWriter::WriteToMemoryInternal(std::string& output, | |
157 unsigned int width, | |
158 unsigned int height, | |
159 unsigned int pitch, | |
160 PixelFormat format, | |
161 const void* buffer) | |
162 { | |
163 std::ostringstream outStream; // todo: try to write directly in output and avoid copy | |
164 | |
165 WriteToStream(outStream, width, height, pitch, format, buffer); | |
166 output = outStream.str(); | |
167 } | |
168 } |