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