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