comparison OrthancFramework/Sources/Images/JpegWriter.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Images/JpegWriter.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
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 "JpegWriter.h"
36
37 #include "../OrthancException.h"
38 #include "../Logging.h"
39 #include "JpegErrorManager.h"
40
41 #if ORTHANC_SANDBOXED == 0
42 # include "../SystemToolbox.h"
43 #endif
44
45 #include <stdlib.h>
46 #include <vector>
47
48 namespace Orthanc
49 {
50 static void GetLines(std::vector<uint8_t*>& lines,
51 unsigned int height,
52 unsigned int pitch,
53 PixelFormat format,
54 const void* buffer)
55 {
56 if (format != PixelFormat_Grayscale8 &&
57 format != PixelFormat_RGB24)
58 {
59 throw OrthancException(ErrorCode_ParameterOutOfRange);
60 }
61
62 lines.resize(height);
63
64 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer));
65 for (unsigned int y = 0; y < height; y++)
66 {
67 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch);
68 }
69 }
70
71
72 static void Compress(struct jpeg_compress_struct& cinfo,
73 std::vector<uint8_t*>& lines,
74 unsigned int width,
75 unsigned int height,
76 PixelFormat format,
77 uint8_t quality)
78 {
79 cinfo.image_width = width;
80 cinfo.image_height = height;
81
82 switch (format)
83 {
84 case PixelFormat_Grayscale8:
85 cinfo.input_components = 1;
86 cinfo.in_color_space = JCS_GRAYSCALE;
87 break;
88
89 case PixelFormat_RGB24:
90 cinfo.input_components = 3;
91 cinfo.in_color_space = JCS_RGB;
92 break;
93
94 default:
95 throw OrthancException(ErrorCode_InternalError);
96 }
97
98 jpeg_set_defaults(&cinfo);
99
100 // The "static_cast" is necessary on OS X:
101 // https://github.com/simonfuhrmann/mve/issues/371
102 jpeg_set_quality(&cinfo, quality, static_cast<boolean>(true));
103 jpeg_start_compress(&cinfo, static_cast<boolean>(true));
104
105 jpeg_write_scanlines(&cinfo, &lines[0], height);
106 jpeg_finish_compress(&cinfo);
107 jpeg_destroy_compress(&cinfo);
108 }
109
110
111 void JpegWriter::SetQuality(uint8_t quality)
112 {
113 if (quality == 0 || quality > 100)
114 {
115 throw OrthancException(ErrorCode_ParameterOutOfRange);
116 }
117
118 quality_ = quality;
119 }
120
121
122 #if ORTHANC_SANDBOXED == 0
123 void JpegWriter::WriteToFileInternal(const std::string& filename,
124 unsigned int width,
125 unsigned int height,
126 unsigned int pitch,
127 PixelFormat format,
128 const void* buffer)
129 {
130 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary);
131 if (fp == NULL)
132 {
133 throw OrthancException(ErrorCode_CannotWriteFile);
134 }
135
136 std::vector<uint8_t*> lines;
137 GetLines(lines, height, pitch, format, buffer);
138
139 struct jpeg_compress_struct cinfo;
140 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
141
142 Internals::JpegErrorManager jerr;
143 cinfo.err = jerr.GetPublic();
144
145 if (setjmp(jerr.GetJumpBuffer()))
146 {
147 /* If we get here, the JPEG code has signaled an error.
148 * We need to clean up the JPEG object, close the input file, and return.
149 */
150 jpeg_destroy_compress(&cinfo);
151 fclose(fp);
152 throw OrthancException(ErrorCode_InternalError,
153 "Error during JPEG encoding: " + jerr.GetMessage());
154 }
155
156 // Do not allocate data on the stack below this line!
157
158 jpeg_create_compress(&cinfo);
159 jpeg_stdio_dest(&cinfo, fp);
160 Compress(cinfo, lines, width, height, format, quality_);
161
162 // Everything went fine, "setjmp()" didn't get called
163
164 fclose(fp);
165 }
166 #endif
167
168
169 void JpegWriter::WriteToMemoryInternal(std::string& jpeg,
170 unsigned int width,
171 unsigned int height,
172 unsigned int pitch,
173 PixelFormat format,
174 const void* buffer)
175 {
176 std::vector<uint8_t*> lines;
177 GetLines(lines, height, pitch, format, buffer);
178
179 struct jpeg_compress_struct cinfo;
180 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
181
182 Internals::JpegErrorManager jerr;
183
184 unsigned char* data = NULL;
185 unsigned long size;
186
187 if (setjmp(jerr.GetJumpBuffer()))
188 {
189 jpeg_destroy_compress(&cinfo);
190
191 if (data != NULL)
192 {
193 free(data);
194 }
195
196 throw OrthancException(ErrorCode_InternalError,
197 "Error during JPEG encoding: " + jerr.GetMessage());
198 }
199
200 // Do not allocate data on the stack below this line!
201
202 jpeg_create_compress(&cinfo);
203 cinfo.err = jerr.GetPublic();
204 jpeg_mem_dest(&cinfo, &data, &size);
205
206 Compress(cinfo, lines, width, height, format, quality_);
207
208 // Everything went fine, "setjmp()" didn't get called
209
210 jpeg.assign(reinterpret_cast<const char*>(data), size);
211 free(data);
212 }
213 }