1604
|
1 /**
|
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
|
|
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"
|
|
39
|
|
40 namespace Orthanc
|
|
41 {
|
|
42 static void Uncompress(struct jpeg_decompress_struct& cinfo,
|
|
43 std::string& content,
|
|
44 ImageAccessor& accessor)
|
|
45 {
|
|
46 jpeg_read_header(&cinfo, TRUE);
|
|
47 jpeg_start_decompress(&cinfo);
|
|
48
|
|
49 PixelFormat format;
|
|
50 if (cinfo.output_components == 1 &&
|
|
51 cinfo.out_color_space == JCS_GRAYSCALE)
|
|
52 {
|
|
53 format = PixelFormat_Grayscale8;
|
|
54 }
|
|
55 else if (cinfo.output_components == 3 &&
|
|
56 cinfo.out_color_space == JCS_RGB)
|
|
57 {
|
|
58 format = PixelFormat_RGB24;
|
|
59 }
|
|
60 else
|
|
61 {
|
|
62 throw OrthancException(ErrorCode_NotImplemented);
|
|
63 }
|
|
64
|
|
65 unsigned int pitch = cinfo.output_width * cinfo.output_components;
|
|
66
|
|
67 /* Make a one-row-high sample array that will go away when done with image */
|
|
68 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, pitch, 1);
|
|
69
|
|
70 try
|
|
71 {
|
|
72 content.resize(pitch * cinfo.output_height);
|
|
73 }
|
|
74 catch (...)
|
|
75 {
|
|
76 throw OrthancException(ErrorCode_NotEnoughMemory);
|
|
77 }
|
|
78
|
|
79 accessor.AssignReadOnly(format, cinfo.output_width, cinfo.output_height, pitch,
|
|
80 content.empty() ? NULL : content.c_str());
|
|
81
|
|
82 uint8_t* target = reinterpret_cast<uint8_t*>(&content[0]);
|
|
83 while (cinfo.output_scanline < cinfo.output_height)
|
|
84 {
|
|
85 jpeg_read_scanlines(&cinfo, buffer, 1);
|
|
86 memcpy(target, buffer[0], pitch);
|
|
87 target += pitch;
|
|
88 }
|
|
89
|
|
90 // Everything went fine, "setjmp()" didn't get called
|
|
91
|
|
92 jpeg_finish_decompress(&cinfo);
|
|
93 }
|
|
94
|
|
95
|
|
96 void JpegReader::ReadFromFile(const char* filename)
|
|
97 {
|
|
98 FILE* fp = fopen(filename, "rb");
|
|
99 if (!fp)
|
|
100 {
|
|
101 throw OrthancException(ErrorCode_InexistentFile);
|
|
102 }
|
|
103
|
|
104 struct jpeg_decompress_struct cinfo;
|
|
105 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
|
|
106
|
|
107 Internals::JpegErrorManager jerr;
|
|
108 cinfo.err = jerr.GetPublic();
|
|
109
|
|
110 if (setjmp(jerr.GetJumpBuffer()))
|
|
111 {
|
|
112 jpeg_destroy_decompress(&cinfo);
|
|
113 fclose(fp);
|
|
114 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
|
|
115 throw OrthancException(ErrorCode_InternalError);
|
|
116 }
|
|
117
|
|
118 // Below this line, we are under the scope of a "setjmp"
|
|
119
|
|
120 jpeg_create_decompress(&cinfo);
|
|
121 jpeg_stdio_src(&cinfo, fp);
|
|
122
|
|
123 try
|
|
124 {
|
|
125 Uncompress(cinfo, content_, *this);
|
|
126 }
|
|
127 catch (OrthancException&)
|
|
128 {
|
|
129 jpeg_destroy_decompress(&cinfo);
|
|
130 fclose(fp);
|
|
131 throw;
|
|
132 }
|
|
133
|
|
134 jpeg_destroy_decompress(&cinfo);
|
|
135 fclose(fp);
|
|
136 }
|
|
137
|
|
138
|
|
139 void JpegReader::ReadFromMemory(const void* buffer,
|
|
140 size_t size)
|
|
141 {
|
|
142 struct jpeg_decompress_struct cinfo;
|
|
143 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
|
|
144
|
|
145 Internals::JpegErrorManager jerr;
|
|
146 cinfo.err = jerr.GetPublic();
|
|
147
|
|
148 if (setjmp(jerr.GetJumpBuffer()))
|
|
149 {
|
|
150 jpeg_destroy_decompress(&cinfo);
|
|
151 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
|
|
152 throw OrthancException(ErrorCode_InternalError);
|
|
153 }
|
|
154
|
|
155 // Below this line, we are under the scope of a "setjmp"
|
|
156 jpeg_create_decompress(&cinfo);
|
|
157 jpeg_mem_src(&cinfo, const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(buffer)), size);
|
|
158
|
|
159 try
|
|
160 {
|
|
161 Uncompress(cinfo, content_, *this);
|
|
162 }
|
|
163 catch (OrthancException&)
|
|
164 {
|
|
165 jpeg_destroy_decompress(&cinfo);
|
|
166 throw;
|
|
167 }
|
|
168
|
|
169 jpeg_destroy_decompress(&cinfo);
|
|
170 }
|
|
171
|
|
172
|
|
173 void JpegReader::ReadFromMemory(const std::string& buffer)
|
|
174 {
|
|
175 if (buffer.empty())
|
|
176 {
|
|
177 ReadFromMemory(NULL, 0);
|
|
178 }
|
|
179 else
|
|
180 {
|
|
181 ReadFromMemory(buffer.c_str(), buffer.size());
|
|
182 }
|
|
183 }
|
|
184 }
|