comparison Framework/Orthanc/Core/Images/JpegWriter.cpp @ 1:dc730d11b101

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