Mercurial > hg > orthanc
annotate Core/Images/JpegWriter.cpp @ 1959:45c4387a379c
Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 07 Apr 2016 16:26:22 +0200 |
parents | 5bcf721bde4f |
children | 08ce34cfacad |
rev | line source |
---|---|
1602 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1602 | 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 | |
1604 | 33 #include "../PrecompiledHeaders.h" |
1602 | 34 #include "JpegWriter.h" |
35 | |
36 #include "../OrthancException.h" | |
37 #include "../Logging.h" | |
38 | |
1604 | 39 #include "JpegErrorManager.h" |
40 | |
1914
501769757bf9
flag to remove network support in dcmtk, removal of unneeded sources in static builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
41 #include <stdlib.h> |
1602 | 42 #include <vector> |
43 | |
44 namespace Orthanc | |
45 { | |
46 static void GetLines(std::vector<uint8_t*>& lines, | |
47 unsigned int height, | |
48 unsigned int pitch, | |
49 PixelFormat format, | |
50 const void* buffer) | |
51 { | |
52 if (format != PixelFormat_Grayscale8 && | |
53 format != PixelFormat_RGB24) | |
54 { | |
55 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
56 } | |
57 | |
58 lines.resize(height); | |
59 | |
60 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)); | |
61 for (unsigned int y = 0; y < height; y++) | |
62 { | |
63 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch); | |
64 } | |
65 } | |
66 | |
67 | |
68 static void Compress(struct jpeg_compress_struct& cinfo, | |
69 std::vector<uint8_t*>& lines, | |
70 unsigned int width, | |
71 unsigned int height, | |
72 PixelFormat format, | |
1654
3727a09e7b53
fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1612
diff
changeset
|
73 uint8_t quality) |
1602 | 74 { |
75 cinfo.image_width = width; | |
76 cinfo.image_height = height; | |
77 | |
78 switch (format) | |
79 { | |
80 case PixelFormat_Grayscale8: | |
81 cinfo.input_components = 1; | |
82 cinfo.in_color_space = JCS_GRAYSCALE; | |
83 break; | |
84 | |
85 case PixelFormat_RGB24: | |
86 cinfo.input_components = 3; | |
87 cinfo.in_color_space = JCS_RGB; | |
88 break; | |
89 | |
90 default: | |
91 throw OrthancException(ErrorCode_InternalError); | |
92 } | |
93 | |
94 jpeg_set_defaults(&cinfo); | |
95 jpeg_set_quality(&cinfo, quality, TRUE); | |
96 jpeg_start_compress(&cinfo, TRUE); | |
97 jpeg_write_scanlines(&cinfo, &lines[0], height); | |
98 jpeg_finish_compress(&cinfo); | |
99 jpeg_destroy_compress(&cinfo); | |
100 } | |
101 | |
102 | |
103 void JpegWriter::SetQuality(uint8_t quality) | |
104 { | |
105 if (quality <= 0 || quality > 100) | |
106 { | |
107 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
108 } | |
109 | |
110 quality_ = quality; | |
111 } | |
112 | |
113 | |
1916 | 114 void JpegWriter::WriteToFileInternal(const std::string& filename, |
115 unsigned int width, | |
116 unsigned int height, | |
117 unsigned int pitch, | |
118 PixelFormat format, | |
119 const void* buffer) | |
1602 | 120 { |
1916 | 121 // TODO This will not work on Windows system if the path contains non-ASCII characters |
122 FILE* fp = fopen(filename.c_str(), "wb"); | |
1602 | 123 if (fp == NULL) |
124 { | |
125 throw OrthancException(ErrorCode_FullStorage); | |
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 | |
1604 | 134 Internals::JpegErrorManager jerr; |
1602 | 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 | |
1916 | 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) | |
1602 | 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 | |
1604 | 173 Internals::JpegErrorManager jerr; |
1602 | 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 } |