comparison Core/Images/JpegWriter.cpp @ 1612:96582230ddcb

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