comparison Resources/Orthanc/Core/Images/JpegWriter.cpp @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
comparison
equal deleted inserted replaced
199:dabe9982fca3 200:03afbee0cc7b
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-2018 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 jpeg_set_quality(&cinfo, quality, TRUE);
100 jpeg_start_compress(&cinfo, TRUE);
101 jpeg_write_scanlines(&cinfo, &lines[0], height);
102 jpeg_finish_compress(&cinfo);
103 jpeg_destroy_compress(&cinfo);
104 }
105
106
107 void JpegWriter::SetQuality(uint8_t quality)
108 {
109 if (quality == 0 || quality > 100)
110 {
111 throw OrthancException(ErrorCode_ParameterOutOfRange);
112 }
113
114 quality_ = quality;
115 }
116
117
118 #if ORTHANC_SANDBOXED == 0
119 void JpegWriter::WriteToFileInternal(const std::string& filename,
120 unsigned int width,
121 unsigned int height,
122 unsigned int pitch,
123 PixelFormat format,
124 const void* buffer)
125 {
126 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary);
127 if (fp == NULL)
128 {
129 throw OrthancException(ErrorCode_CannotWriteFile);
130 }
131
132 std::vector<uint8_t*> lines;
133 GetLines(lines, height, pitch, format, buffer);
134
135 struct jpeg_compress_struct cinfo;
136 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
137
138 Internals::JpegErrorManager jerr;
139 cinfo.err = jerr.GetPublic();
140
141 if (setjmp(jerr.GetJumpBuffer()))
142 {
143 /* If we get here, the JPEG code has signaled an error.
144 * We need to clean up the JPEG object, close the input file, and return.
145 */
146 jpeg_destroy_compress(&cinfo);
147 fclose(fp);
148 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
149 throw OrthancException(ErrorCode_InternalError);
150 }
151
152 // Do not allocate data on the stack below this line!
153
154 jpeg_create_compress(&cinfo);
155 jpeg_stdio_dest(&cinfo, fp);
156 Compress(cinfo, lines, width, height, format, quality_);
157
158 // Everything went fine, "setjmp()" didn't get called
159
160 fclose(fp);
161 }
162 #endif
163
164
165 #if ORTHANC_SANDBOXED == 0
166 void JpegWriter::WriteToMemoryInternal(std::string& jpeg,
167 unsigned int width,
168 unsigned int height,
169 unsigned int pitch,
170 PixelFormat format,
171 const void* buffer)
172 {
173 std::vector<uint8_t*> lines;
174 GetLines(lines, height, pitch, format, buffer);
175
176 struct jpeg_compress_struct cinfo;
177 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
178
179 Internals::JpegErrorManager jerr;
180
181 unsigned char* data = NULL;
182 unsigned long size;
183
184 if (setjmp(jerr.GetJumpBuffer()))
185 {
186 jpeg_destroy_compress(&cinfo);
187
188 if (data != NULL)
189 {
190 free(data);
191 }
192
193 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
194 throw OrthancException(ErrorCode_InternalError);
195 }
196
197 // Do not allocate data on the stack below this line!
198
199 jpeg_create_compress(&cinfo);
200 cinfo.err = jerr.GetPublic();
201 jpeg_mem_dest(&cinfo, &data, &size);
202
203 Compress(cinfo, lines, width, height, format, quality_);
204
205 // Everything went fine, "setjmp()" didn't get called
206
207 jpeg.assign(reinterpret_cast<const char*>(data), size);
208 free(data);
209 }
210 #endif
211 }