Mercurial > hg > orthanc
annotate Core/Images/JpegWriter.cpp @ 2466:ae22df152ee2
todo
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Jan 2018 12:54:00 +0100 |
parents | 878b59270859 |
children | 964fb9a5786d |
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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2407
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
1602 | 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 | |
1604 | 34 #include "../PrecompiledHeaders.h" |
1602 | 35 #include "JpegWriter.h" |
36 | |
37 #include "../OrthancException.h" | |
38 #include "../Logging.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
39 #include "JpegErrorManager.h" |
1602 | 40 |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
41 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
42 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
43 #endif |
1604 | 44 |
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
|
45 #include <stdlib.h> |
1602 | 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, | |
1654
3727a09e7b53
fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1612
diff
changeset
|
77 uint8_t quality) |
1602 | 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 { | |
2223 | 109 if (quality == 0 || quality > 100) |
1602 | 110 { |
111 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
112 } | |
113 | |
114 quality_ = quality; | |
115 } | |
116 | |
117 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
118 #if ORTHANC_SANDBOXED == 0 |
1916 | 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) | |
1602 | 125 { |
2140 | 126 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
1602 | 127 if (fp == NULL) |
128 { | |
2017 | 129 throw OrthancException(ErrorCode_CannotWriteFile); |
1602 | 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 | |
1604 | 138 Internals::JpegErrorManager jerr; |
1602 | 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 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
162 #endif |
1602 | 163 |
164 | |
2407
5edec967055e
fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
165 #if ORTHANC_SANDBOXED == 0 |
1916 | 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) | |
1602 | 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 | |
1604 | 179 Internals::JpegErrorManager jerr; |
1602 | 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 } | |
2407
5edec967055e
fix sandboxed builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
210 #endif |
1602 | 211 } |