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