Mercurial > hg > orthanc
annotate Core/Images/JpegReader.cpp @ 2170:baf8dd89b4e0
improved support for sandboxed environments
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 23 Nov 2016 11:45:02 +0100 |
parents | fd5875662670 |
children | 35febe19e874 |
rev | line source |
---|---|
1604 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1604 | 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 "JpegReader.h" | |
35 | |
36 #include "JpegErrorManager.h" | |
37 #include "../OrthancException.h" | |
38 #include "../Logging.h" | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2140
diff
changeset
|
39 #include "../SystemToolbox.h" |
1604 | 40 |
41 namespace Orthanc | |
42 { | |
43 static void Uncompress(struct jpeg_decompress_struct& cinfo, | |
44 std::string& content, | |
45 ImageAccessor& accessor) | |
46 { | |
47 jpeg_read_header(&cinfo, TRUE); | |
48 jpeg_start_decompress(&cinfo); | |
49 | |
50 PixelFormat format; | |
51 if (cinfo.output_components == 1 && | |
52 cinfo.out_color_space == JCS_GRAYSCALE) | |
53 { | |
54 format = PixelFormat_Grayscale8; | |
55 } | |
56 else if (cinfo.output_components == 3 && | |
57 cinfo.out_color_space == JCS_RGB) | |
58 { | |
59 format = PixelFormat_RGB24; | |
60 } | |
61 else | |
62 { | |
63 throw OrthancException(ErrorCode_NotImplemented); | |
64 } | |
65 | |
66 unsigned int pitch = cinfo.output_width * cinfo.output_components; | |
67 | |
68 /* Make a one-row-high sample array that will go away when done with image */ | |
69 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, pitch, 1); | |
70 | |
71 try | |
72 { | |
73 content.resize(pitch * cinfo.output_height); | |
74 } | |
75 catch (...) | |
76 { | |
77 throw OrthancException(ErrorCode_NotEnoughMemory); | |
78 } | |
79 | |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1605
diff
changeset
|
80 accessor.AssignWritable(format, cinfo.output_width, cinfo.output_height, pitch, |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1605
diff
changeset
|
81 content.empty() ? NULL : &content[0]); |
1604 | 82 |
83 uint8_t* target = reinterpret_cast<uint8_t*>(&content[0]); | |
84 while (cinfo.output_scanline < cinfo.output_height) | |
85 { | |
86 jpeg_read_scanlines(&cinfo, buffer, 1); | |
87 memcpy(target, buffer[0], pitch); | |
88 target += pitch; | |
89 } | |
90 | |
91 // Everything went fine, "setjmp()" didn't get called | |
92 | |
93 jpeg_finish_decompress(&cinfo); | |
94 } | |
95 | |
96 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
97 #if ORTHANC_SANDBOXED == 0 |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
98 void JpegReader::ReadFromFile(const std::string& filename) |
1604 | 99 { |
2140 | 100 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_ReadBinary); |
1604 | 101 if (!fp) |
102 { | |
103 throw OrthancException(ErrorCode_InexistentFile); | |
104 } | |
105 | |
106 struct jpeg_decompress_struct cinfo; | |
107 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct)); | |
108 | |
109 Internals::JpegErrorManager jerr; | |
110 cinfo.err = jerr.GetPublic(); | |
111 | |
112 if (setjmp(jerr.GetJumpBuffer())) | |
113 { | |
114 jpeg_destroy_decompress(&cinfo); | |
115 fclose(fp); | |
2085 | 116 LOG(ERROR) << "Error during JPEG decoding: " << jerr.GetMessage(); |
1604 | 117 throw OrthancException(ErrorCode_InternalError); |
118 } | |
119 | |
120 // Below this line, we are under the scope of a "setjmp" | |
121 | |
122 jpeg_create_decompress(&cinfo); | |
123 jpeg_stdio_src(&cinfo, fp); | |
124 | |
125 try | |
126 { | |
127 Uncompress(cinfo, content_, *this); | |
128 } | |
129 catch (OrthancException&) | |
130 { | |
131 jpeg_destroy_decompress(&cinfo); | |
132 fclose(fp); | |
133 throw; | |
134 } | |
135 | |
136 jpeg_destroy_decompress(&cinfo); | |
137 fclose(fp); | |
138 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
139 #endif |
1604 | 140 |
141 | |
142 void JpegReader::ReadFromMemory(const void* buffer, | |
143 size_t size) | |
144 { | |
145 struct jpeg_decompress_struct cinfo; | |
146 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct)); | |
147 | |
148 Internals::JpegErrorManager jerr; | |
149 cinfo.err = jerr.GetPublic(); | |
150 | |
151 if (setjmp(jerr.GetJumpBuffer())) | |
152 { | |
153 jpeg_destroy_decompress(&cinfo); | |
2085 | 154 LOG(ERROR) << "Error during JPEG decoding: " << jerr.GetMessage(); |
1604 | 155 throw OrthancException(ErrorCode_InternalError); |
156 } | |
157 | |
158 // Below this line, we are under the scope of a "setjmp" | |
159 jpeg_create_decompress(&cinfo); | |
160 jpeg_mem_src(&cinfo, const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(buffer)), size); | |
161 | |
162 try | |
163 { | |
164 Uncompress(cinfo, content_, *this); | |
165 } | |
166 catch (OrthancException&) | |
167 { | |
168 jpeg_destroy_decompress(&cinfo); | |
169 throw; | |
170 } | |
171 | |
172 jpeg_destroy_decompress(&cinfo); | |
173 } | |
174 | |
175 | |
176 void JpegReader::ReadFromMemory(const std::string& buffer) | |
177 { | |
178 if (buffer.empty()) | |
179 { | |
180 ReadFromMemory(NULL, 0); | |
181 } | |
182 else | |
183 { | |
184 ReadFromMemory(buffer.c_str(), buffer.size()); | |
185 } | |
186 } | |
187 } |