Mercurial > hg > orthanc-webviewer
comparison Plugin/JpegWriter.cpp @ 0:02f7a0400a91
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 25 Feb 2015 13:45:35 +0100 |
parents | |
children | a6492d20b2a8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:02f7a0400a91 |
---|---|
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 Affero General Public License | |
8 * as published by the Free Software Foundation, either version 3 of | |
9 * the License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "JpegWriter.h" | |
22 | |
23 #include "../Orthanc/OrthancException.h" | |
24 | |
25 #include <jpeglib.h> | |
26 #include <setjmp.h> | |
27 #include <stdio.h> | |
28 #include <vector> | |
29 #include <string.h> | |
30 #include <stdlib.h> | |
31 | |
32 namespace OrthancPlugins | |
33 { | |
34 namespace | |
35 { | |
36 class ErrorManager | |
37 { | |
38 private: | |
39 struct jpeg_error_mgr pub; /* "public" fields */ | |
40 jmp_buf setjmp_buffer; /* for return to caller */ | |
41 std::string message; | |
42 | |
43 static void OutputMessage(j_common_ptr cinfo) | |
44 { | |
45 char message[JMSG_LENGTH_MAX]; | |
46 (*cinfo->err->format_message) (cinfo, message); | |
47 | |
48 ErrorManager* that = reinterpret_cast<ErrorManager*>(cinfo->err); | |
49 that->message = std::string(message); | |
50 } | |
51 | |
52 | |
53 static void ErrorExit(j_common_ptr cinfo) | |
54 { | |
55 (*cinfo->err->output_message) (cinfo); | |
56 | |
57 ErrorManager* that = reinterpret_cast<ErrorManager*>(cinfo->err); | |
58 longjmp(that->setjmp_buffer, 1); | |
59 } | |
60 | |
61 | |
62 public: | |
63 ErrorManager() | |
64 { | |
65 memset(&pub, 0, sizeof(struct jpeg_error_mgr)); | |
66 memset(&setjmp_buffer, 0, sizeof(jmp_buf)); | |
67 | |
68 jpeg_std_error(&pub); | |
69 pub.error_exit = ErrorExit; | |
70 pub.output_message = OutputMessage; | |
71 } | |
72 | |
73 struct jpeg_error_mgr* GetPublic() | |
74 { | |
75 return &pub; | |
76 } | |
77 | |
78 jmp_buf& GetJumpBuffer() | |
79 { | |
80 return setjmp_buffer; | |
81 } | |
82 | |
83 const std::string& GetMessage() const | |
84 { | |
85 return message; | |
86 } | |
87 }; | |
88 } | |
89 | |
90 | |
91 static void GetLines(std::vector<uint8_t*>& lines, | |
92 unsigned int height, | |
93 unsigned int pitch, | |
94 Orthanc::PixelFormat format, | |
95 const void* buffer) | |
96 { | |
97 if (format != Orthanc::PixelFormat_Grayscale8 && | |
98 format != Orthanc::PixelFormat_RGB24) | |
99 { | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
101 } | |
102 | |
103 lines.resize(height); | |
104 | |
105 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)); | |
106 for (unsigned int y = 0; y < height; y++) | |
107 { | |
108 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch); | |
109 } | |
110 } | |
111 | |
112 | |
113 static void Compress(struct jpeg_compress_struct& cinfo, | |
114 std::vector<uint8_t*>& lines, | |
115 unsigned int width, | |
116 unsigned int height, | |
117 Orthanc::PixelFormat format, | |
118 int quality) | |
119 { | |
120 cinfo.image_width = width; | |
121 cinfo.image_height = height; | |
122 | |
123 switch (format) | |
124 { | |
125 case Orthanc::PixelFormat_Grayscale8: | |
126 cinfo.input_components = 1; | |
127 cinfo.in_color_space = JCS_GRAYSCALE; | |
128 break; | |
129 | |
130 case Orthanc::PixelFormat_RGB24: | |
131 cinfo.input_components = 3; | |
132 cinfo.in_color_space = JCS_RGB; | |
133 break; | |
134 | |
135 default: | |
136 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
137 } | |
138 | |
139 jpeg_set_defaults(&cinfo); | |
140 jpeg_set_quality(&cinfo, quality, TRUE); | |
141 jpeg_start_compress(&cinfo, TRUE); | |
142 jpeg_write_scanlines(&cinfo, &lines[0], height); | |
143 jpeg_finish_compress(&cinfo); | |
144 jpeg_destroy_compress(&cinfo); | |
145 } | |
146 | |
147 | |
148 void JpegWriter::SetQuality(uint8_t quality) | |
149 { | |
150 if (quality <= 0 || quality > 100) | |
151 { | |
152 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
153 } | |
154 | |
155 quality_ = quality; | |
156 } | |
157 | |
158 | |
159 void JpegWriter::WriteToFile(const char* filename, | |
160 unsigned int width, | |
161 unsigned int height, | |
162 unsigned int pitch, | |
163 Orthanc::PixelFormat format, | |
164 const void* buffer) | |
165 { | |
166 FILE* fp = fopen(filename, "wb"); | |
167 if (fp == NULL) | |
168 { | |
169 throw Orthanc::OrthancException(Orthanc::ErrorCode_FullStorage); | |
170 } | |
171 | |
172 std::vector<uint8_t*> lines; | |
173 GetLines(lines, height, pitch, format, buffer); | |
174 | |
175 struct jpeg_compress_struct cinfo; | |
176 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
177 | |
178 ErrorManager jerr; | |
179 cinfo.err = jerr.GetPublic(); | |
180 | |
181 if (setjmp(jerr.GetJumpBuffer())) | |
182 { | |
183 /* If we get here, the JPEG code has signaled an error. | |
184 * We need to clean up the JPEG object, close the input file, and return. | |
185 */ | |
186 jpeg_destroy_compress(&cinfo); | |
187 fclose(fp); | |
188 throw Orthanc::OrthancException("Error during JPEG encoding: " + jerr.GetMessage()); | |
189 } | |
190 | |
191 // Do not allocate data on the stack below this line! | |
192 | |
193 jpeg_create_compress(&cinfo); | |
194 jpeg_stdio_dest(&cinfo, fp); | |
195 Compress(cinfo, lines, width, height, format, quality_); | |
196 | |
197 // Everything went fine, "setjmp()" didn't get called | |
198 | |
199 fclose(fp); | |
200 } | |
201 | |
202 | |
203 void JpegWriter::WriteToMemory(std::string& jpeg, | |
204 unsigned int width, | |
205 unsigned int height, | |
206 unsigned int pitch, | |
207 Orthanc::PixelFormat format, | |
208 const void* buffer) | |
209 { | |
210 std::vector<uint8_t*> lines; | |
211 GetLines(lines, height, pitch, format, buffer); | |
212 | |
213 struct jpeg_compress_struct cinfo; | |
214 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
215 | |
216 ErrorManager jerr; | |
217 | |
218 unsigned char* data = NULL; | |
219 unsigned long size; | |
220 | |
221 if (setjmp(jerr.GetJumpBuffer())) | |
222 { | |
223 jpeg_destroy_compress(&cinfo); | |
224 | |
225 if (data != NULL) | |
226 { | |
227 free(data); | |
228 } | |
229 | |
230 throw Orthanc::OrthancException("Error during JPEG encoding: " + jerr.GetMessage()); | |
231 } | |
232 | |
233 // Do not allocate data on the stack below this line! | |
234 | |
235 jpeg_create_compress(&cinfo); | |
236 cinfo.err = jerr.GetPublic(); | |
237 jpeg_mem_dest(&cinfo, &data, &size); | |
238 | |
239 Compress(cinfo, lines, width, height, format, quality_); | |
240 | |
241 // Everything went fine, "setjmp()" didn't get called | |
242 | |
243 jpeg.assign(reinterpret_cast<const char*>(data), size); | |
244 free(data); | |
245 } | |
246 } |