Mercurial > hg > orthanc
annotate Core/Images/JpegWriter.cpp @ 4031:e3b3af80732d
ServerResources, and moving EmbeddedResourceHttpHandler from Core to OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Jun 2020 21:31:13 +0200 |
parents | 94f4a18a79cc |
children |
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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 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); | |
2683 | 99 |
100 // The "static_cast" is necessary on OS X: | |
101 // https://github.com/simonfuhrmann/mve/issues/371 | |
102 jpeg_set_quality(&cinfo, quality, static_cast<boolean>(true)); | |
103 jpeg_start_compress(&cinfo, static_cast<boolean>(true)); | |
104 | |
1602 | 105 jpeg_write_scanlines(&cinfo, &lines[0], height); |
106 jpeg_finish_compress(&cinfo); | |
107 jpeg_destroy_compress(&cinfo); | |
108 } | |
109 | |
110 | |
111 void JpegWriter::SetQuality(uint8_t quality) | |
112 { | |
2223 | 113 if (quality == 0 || quality > 100) |
1602 | 114 { |
115 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
116 } | |
117 | |
118 quality_ = quality; | |
119 } | |
120 | |
121 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
122 #if ORTHANC_SANDBOXED == 0 |
1916 | 123 void JpegWriter::WriteToFileInternal(const std::string& filename, |
124 unsigned int width, | |
125 unsigned int height, | |
126 unsigned int pitch, | |
127 PixelFormat format, | |
128 const void* buffer) | |
1602 | 129 { |
2140 | 130 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
1602 | 131 if (fp == NULL) |
132 { | |
2017 | 133 throw OrthancException(ErrorCode_CannotWriteFile); |
1602 | 134 } |
135 | |
136 std::vector<uint8_t*> lines; | |
137 GetLines(lines, height, pitch, format, buffer); | |
138 | |
139 struct jpeg_compress_struct cinfo; | |
140 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
141 | |
1604 | 142 Internals::JpegErrorManager jerr; |
1602 | 143 cinfo.err = jerr.GetPublic(); |
144 | |
145 if (setjmp(jerr.GetJumpBuffer())) | |
146 { | |
147 /* If we get here, the JPEG code has signaled an error. | |
148 * We need to clean up the JPEG object, close the input file, and return. | |
149 */ | |
150 jpeg_destroy_compress(&cinfo); | |
151 fclose(fp); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
152 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
153 "Error during JPEG encoding: " + jerr.GetMessage()); |
1602 | 154 } |
155 | |
156 // Do not allocate data on the stack below this line! | |
157 | |
158 jpeg_create_compress(&cinfo); | |
159 jpeg_stdio_dest(&cinfo, fp); | |
160 Compress(cinfo, lines, width, height, format, quality_); | |
161 | |
162 // Everything went fine, "setjmp()" didn't get called | |
163 | |
164 fclose(fp); | |
165 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
166 #endif |
1602 | 167 |
168 | |
1916 | 169 void JpegWriter::WriteToMemoryInternal(std::string& jpeg, |
170 unsigned int width, | |
171 unsigned int height, | |
172 unsigned int pitch, | |
173 PixelFormat format, | |
174 const void* buffer) | |
1602 | 175 { |
176 std::vector<uint8_t*> lines; | |
177 GetLines(lines, height, pitch, format, buffer); | |
178 | |
179 struct jpeg_compress_struct cinfo; | |
180 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
181 | |
1604 | 182 Internals::JpegErrorManager jerr; |
1602 | 183 |
184 unsigned char* data = NULL; | |
185 unsigned long size; | |
186 | |
187 if (setjmp(jerr.GetJumpBuffer())) | |
188 { | |
189 jpeg_destroy_compress(&cinfo); | |
190 | |
191 if (data != NULL) | |
192 { | |
193 free(data); | |
194 } | |
195 | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
196 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
197 "Error during JPEG encoding: " + jerr.GetMessage()); |
1602 | 198 } |
199 | |
200 // Do not allocate data on the stack below this line! | |
201 | |
202 jpeg_create_compress(&cinfo); | |
203 cinfo.err = jerr.GetPublic(); | |
204 jpeg_mem_dest(&cinfo, &data, &size); | |
205 | |
206 Compress(cinfo, lines, width, height, format, quality_); | |
207 | |
208 // Everything went fine, "setjmp()" didn't get called | |
209 | |
210 jpeg.assign(reinterpret_cast<const char*>(data), size); | |
211 free(data); | |
212 } | |
213 } |