1602
|
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 "JpegWriter.h"
|
|
34
|
|
35 #include "../OrthancException.h"
|
|
36 #include "../Logging.h"
|
|
37
|
|
38 #include <jpeglib.h>
|
|
39 #include <setjmp.h>
|
|
40 #include <stdio.h>
|
|
41 #include <vector>
|
|
42 #include <string.h>
|
|
43 #include <stdlib.h>
|
|
44
|
|
45 namespace Orthanc
|
|
46 {
|
|
47 namespace
|
|
48 {
|
|
49 class ErrorManager
|
|
50 {
|
|
51 private:
|
|
52 struct jpeg_error_mgr pub; /* "public" fields */
|
|
53 jmp_buf setjmp_buffer; /* for return to caller */
|
|
54 std::string message;
|
|
55
|
|
56 static void OutputMessage(j_common_ptr cinfo)
|
|
57 {
|
|
58 char message[JMSG_LENGTH_MAX];
|
|
59 (*cinfo->err->format_message) (cinfo, message);
|
|
60
|
|
61 ErrorManager* that = reinterpret_cast<ErrorManager*>(cinfo->err);
|
|
62 that->message = std::string(message);
|
|
63 }
|
|
64
|
|
65
|
|
66 static void ErrorExit(j_common_ptr cinfo)
|
|
67 {
|
|
68 (*cinfo->err->output_message) (cinfo);
|
|
69
|
|
70 ErrorManager* that = reinterpret_cast<ErrorManager*>(cinfo->err);
|
|
71 longjmp(that->setjmp_buffer, 1);
|
|
72 }
|
|
73
|
|
74
|
|
75 public:
|
|
76 ErrorManager()
|
|
77 {
|
|
78 memset(&pub, 0, sizeof(struct jpeg_error_mgr));
|
|
79 memset(&setjmp_buffer, 0, sizeof(jmp_buf));
|
|
80
|
|
81 jpeg_std_error(&pub);
|
|
82 pub.error_exit = ErrorExit;
|
|
83 pub.output_message = OutputMessage;
|
|
84 }
|
|
85
|
|
86 struct jpeg_error_mgr* GetPublic()
|
|
87 {
|
|
88 return &pub;
|
|
89 }
|
|
90
|
|
91 jmp_buf& GetJumpBuffer()
|
|
92 {
|
|
93 return setjmp_buffer;
|
|
94 }
|
|
95
|
|
96 const std::string& GetMessage() const
|
|
97 {
|
|
98 return message;
|
|
99 }
|
|
100 };
|
|
101 }
|
|
102
|
|
103
|
|
104 static void GetLines(std::vector<uint8_t*>& lines,
|
|
105 unsigned int height,
|
|
106 unsigned int pitch,
|
|
107 PixelFormat format,
|
|
108 const void* buffer)
|
|
109 {
|
|
110 if (format != PixelFormat_Grayscale8 &&
|
|
111 format != PixelFormat_RGB24)
|
|
112 {
|
|
113 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
114 }
|
|
115
|
|
116 lines.resize(height);
|
|
117
|
|
118 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer));
|
|
119 for (unsigned int y = 0; y < height; y++)
|
|
120 {
|
|
121 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch);
|
|
122 }
|
|
123 }
|
|
124
|
|
125
|
|
126 static void Compress(struct jpeg_compress_struct& cinfo,
|
|
127 std::vector<uint8_t*>& lines,
|
|
128 unsigned int width,
|
|
129 unsigned int height,
|
|
130 PixelFormat format,
|
|
131 int quality)
|
|
132 {
|
|
133 cinfo.image_width = width;
|
|
134 cinfo.image_height = height;
|
|
135
|
|
136 switch (format)
|
|
137 {
|
|
138 case PixelFormat_Grayscale8:
|
|
139 cinfo.input_components = 1;
|
|
140 cinfo.in_color_space = JCS_GRAYSCALE;
|
|
141 break;
|
|
142
|
|
143 case PixelFormat_RGB24:
|
|
144 cinfo.input_components = 3;
|
|
145 cinfo.in_color_space = JCS_RGB;
|
|
146 break;
|
|
147
|
|
148 default:
|
|
149 throw OrthancException(ErrorCode_InternalError);
|
|
150 }
|
|
151
|
|
152 jpeg_set_defaults(&cinfo);
|
|
153 jpeg_set_quality(&cinfo, quality, TRUE);
|
|
154 jpeg_start_compress(&cinfo, TRUE);
|
|
155 jpeg_write_scanlines(&cinfo, &lines[0], height);
|
|
156 jpeg_finish_compress(&cinfo);
|
|
157 jpeg_destroy_compress(&cinfo);
|
|
158 }
|
|
159
|
|
160
|
|
161 void JpegWriter::SetQuality(uint8_t quality)
|
|
162 {
|
|
163 if (quality <= 0 || quality > 100)
|
|
164 {
|
|
165 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
166 }
|
|
167
|
|
168 quality_ = quality;
|
|
169 }
|
|
170
|
|
171
|
|
172 void JpegWriter::WriteToFile(const char* filename,
|
|
173 unsigned int width,
|
|
174 unsigned int height,
|
|
175 unsigned int pitch,
|
|
176 PixelFormat format,
|
|
177 const void* buffer)
|
|
178 {
|
|
179 FILE* fp = fopen(filename, "wb");
|
|
180 if (fp == NULL)
|
|
181 {
|
|
182 throw OrthancException(ErrorCode_FullStorage);
|
|
183 }
|
|
184
|
|
185 std::vector<uint8_t*> lines;
|
|
186 GetLines(lines, height, pitch, format, buffer);
|
|
187
|
|
188 struct jpeg_compress_struct cinfo;
|
|
189 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
|
|
190
|
|
191 ErrorManager jerr;
|
|
192 cinfo.err = jerr.GetPublic();
|
|
193
|
|
194 if (setjmp(jerr.GetJumpBuffer()))
|
|
195 {
|
|
196 /* If we get here, the JPEG code has signaled an error.
|
|
197 * We need to clean up the JPEG object, close the input file, and return.
|
|
198 */
|
|
199 jpeg_destroy_compress(&cinfo);
|
|
200 fclose(fp);
|
|
201 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
|
|
202 throw OrthancException(ErrorCode_InternalError);
|
|
203 }
|
|
204
|
|
205 // Do not allocate data on the stack below this line!
|
|
206
|
|
207 jpeg_create_compress(&cinfo);
|
|
208 jpeg_stdio_dest(&cinfo, fp);
|
|
209 Compress(cinfo, lines, width, height, format, quality_);
|
|
210
|
|
211 // Everything went fine, "setjmp()" didn't get called
|
|
212
|
|
213 fclose(fp);
|
|
214 }
|
|
215
|
|
216
|
|
217 void JpegWriter::WriteToMemory(std::string& jpeg,
|
|
218 unsigned int width,
|
|
219 unsigned int height,
|
|
220 unsigned int pitch,
|
|
221 PixelFormat format,
|
|
222 const void* buffer)
|
|
223 {
|
|
224 std::vector<uint8_t*> lines;
|
|
225 GetLines(lines, height, pitch, format, buffer);
|
|
226
|
|
227 struct jpeg_compress_struct cinfo;
|
|
228 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
|
|
229
|
|
230 ErrorManager jerr;
|
|
231
|
|
232 unsigned char* data = NULL;
|
|
233 unsigned long size;
|
|
234
|
|
235 if (setjmp(jerr.GetJumpBuffer()))
|
|
236 {
|
|
237 jpeg_destroy_compress(&cinfo);
|
|
238
|
|
239 if (data != NULL)
|
|
240 {
|
|
241 free(data);
|
|
242 }
|
|
243
|
|
244 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
|
|
245 throw OrthancException(ErrorCode_InternalError);
|
|
246 }
|
|
247
|
|
248 // Do not allocate data on the stack below this line!
|
|
249
|
|
250 jpeg_create_compress(&cinfo);
|
|
251 cinfo.err = jerr.GetPublic();
|
|
252 jpeg_mem_dest(&cinfo, &data, &size);
|
|
253
|
|
254 Compress(cinfo, lines, width, height, format, quality_);
|
|
255
|
|
256 // Everything went fine, "setjmp()" didn't get called
|
|
257
|
|
258 jpeg.assign(reinterpret_cast<const char*>(data), size);
|
|
259 free(data);
|
|
260 }
|
|
261 }
|