Mercurial > hg > orthanc
annotate Core/Images/JpegReader.cpp @ 2044:0f35383dd6cc
doc
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 23 Jun 2016 17:32:01 +0200 |
parents | 08ce34cfacad |
children | 5d4743fd8a85 |
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" | |
2017 | 39 #include "../Toolbox.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 | |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
97 void JpegReader::ReadFromFile(const std::string& filename) |
1604 | 98 { |
2017 | 99 FILE* fp = Toolbox::OpenFile(filename, FileMode_ReadBinary); |
1604 | 100 if (!fp) |
101 { | |
102 throw OrthancException(ErrorCode_InexistentFile); | |
103 } | |
104 | |
105 struct jpeg_decompress_struct cinfo; | |
106 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct)); | |
107 | |
108 Internals::JpegErrorManager jerr; | |
109 cinfo.err = jerr.GetPublic(); | |
110 | |
111 if (setjmp(jerr.GetJumpBuffer())) | |
112 { | |
113 jpeg_destroy_decompress(&cinfo); | |
114 fclose(fp); | |
115 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage(); | |
116 throw OrthancException(ErrorCode_InternalError); | |
117 } | |
118 | |
119 // Below this line, we are under the scope of a "setjmp" | |
120 | |
121 jpeg_create_decompress(&cinfo); | |
122 jpeg_stdio_src(&cinfo, fp); | |
123 | |
124 try | |
125 { | |
126 Uncompress(cinfo, content_, *this); | |
127 } | |
128 catch (OrthancException&) | |
129 { | |
130 jpeg_destroy_decompress(&cinfo); | |
131 fclose(fp); | |
132 throw; | |
133 } | |
134 | |
135 jpeg_destroy_decompress(&cinfo); | |
136 fclose(fp); | |
137 } | |
138 | |
139 | |
140 void JpegReader::ReadFromMemory(const void* buffer, | |
141 size_t size) | |
142 { | |
143 struct jpeg_decompress_struct cinfo; | |
144 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct)); | |
145 | |
146 Internals::JpegErrorManager jerr; | |
147 cinfo.err = jerr.GetPublic(); | |
148 | |
149 if (setjmp(jerr.GetJumpBuffer())) | |
150 { | |
151 jpeg_destroy_decompress(&cinfo); | |
152 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage(); | |
153 throw OrthancException(ErrorCode_InternalError); | |
154 } | |
155 | |
156 // Below this line, we are under the scope of a "setjmp" | |
157 jpeg_create_decompress(&cinfo); | |
158 jpeg_mem_src(&cinfo, const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(buffer)), size); | |
159 | |
160 try | |
161 { | |
162 Uncompress(cinfo, content_, *this); | |
163 } | |
164 catch (OrthancException&) | |
165 { | |
166 jpeg_destroy_decompress(&cinfo); | |
167 throw; | |
168 } | |
169 | |
170 jpeg_destroy_decompress(&cinfo); | |
171 } | |
172 | |
173 | |
174 void JpegReader::ReadFromMemory(const std::string& buffer) | |
175 { | |
176 if (buffer.empty()) | |
177 { | |
178 ReadFromMemory(NULL, 0); | |
179 } | |
180 else | |
181 { | |
182 ReadFromMemory(buffer.c_str(), buffer.size()); | |
183 } | |
184 } | |
185 } |