Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/JpegWriter.cpp @ 4564:5a0adc1c19a9 db-changes
avoid copy of objects in ServerIndex
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 08 Mar 2021 11:08:03 +0100 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4300
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
1602 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
1602 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
1602 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
1602 | 20 **/ |
21 | |
22 | |
1604 | 23 #include "../PrecompiledHeaders.h" |
1602 | 24 #include "JpegWriter.h" |
25 | |
26 #include "../OrthancException.h" | |
27 #include "../Logging.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
28 #include "JpegErrorManager.h" |
1602 | 29 |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
30 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
31 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
32 #endif |
1604 | 33 |
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
|
34 #include <stdlib.h> |
1602 | 35 #include <vector> |
36 | |
37 namespace Orthanc | |
38 { | |
39 static void GetLines(std::vector<uint8_t*>& lines, | |
40 unsigned int height, | |
41 unsigned int pitch, | |
42 PixelFormat format, | |
43 const void* buffer) | |
44 { | |
45 if (format != PixelFormat_Grayscale8 && | |
46 format != PixelFormat_RGB24) | |
47 { | |
48 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
49 } | |
50 | |
51 lines.resize(height); | |
52 | |
53 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)); | |
54 for (unsigned int y = 0; y < height; y++) | |
55 { | |
56 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch); | |
57 } | |
58 } | |
59 | |
60 | |
61 static void Compress(struct jpeg_compress_struct& cinfo, | |
62 std::vector<uint8_t*>& lines, | |
63 unsigned int width, | |
64 unsigned int height, | |
65 PixelFormat format, | |
1654
3727a09e7b53
fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1612
diff
changeset
|
66 uint8_t quality) |
1602 | 67 { |
68 cinfo.image_width = width; | |
69 cinfo.image_height = height; | |
70 | |
71 switch (format) | |
72 { | |
73 case PixelFormat_Grayscale8: | |
74 cinfo.input_components = 1; | |
75 cinfo.in_color_space = JCS_GRAYSCALE; | |
76 break; | |
77 | |
78 case PixelFormat_RGB24: | |
79 cinfo.input_components = 3; | |
80 cinfo.in_color_space = JCS_RGB; | |
81 break; | |
82 | |
83 default: | |
84 throw OrthancException(ErrorCode_InternalError); | |
85 } | |
86 | |
87 jpeg_set_defaults(&cinfo); | |
2683 | 88 |
89 // The "static_cast" is necessary on OS X: | |
90 // https://github.com/simonfuhrmann/mve/issues/371 | |
91 jpeg_set_quality(&cinfo, quality, static_cast<boolean>(true)); | |
92 jpeg_start_compress(&cinfo, static_cast<boolean>(true)); | |
93 | |
1602 | 94 jpeg_write_scanlines(&cinfo, &lines[0], height); |
95 jpeg_finish_compress(&cinfo); | |
96 jpeg_destroy_compress(&cinfo); | |
97 } | |
98 | |
99 | |
4300 | 100 JpegWriter::JpegWriter() : quality_(90) |
101 { | |
102 } | |
103 | |
104 | |
1602 | 105 void JpegWriter::SetQuality(uint8_t quality) |
106 { | |
2223 | 107 if (quality == 0 || quality > 100) |
1602 | 108 { |
109 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
110 } | |
111 | |
112 quality_ = quality; | |
113 } | |
114 | |
115 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
116 #if ORTHANC_SANDBOXED == 0 |
1916 | 117 void JpegWriter::WriteToFileInternal(const std::string& filename, |
118 unsigned int width, | |
119 unsigned int height, | |
120 unsigned int pitch, | |
121 PixelFormat format, | |
122 const void* buffer) | |
1602 | 123 { |
2140 | 124 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
1602 | 125 if (fp == NULL) |
126 { | |
2017 | 127 throw OrthancException(ErrorCode_CannotWriteFile); |
1602 | 128 } |
129 | |
130 std::vector<uint8_t*> lines; | |
131 GetLines(lines, height, pitch, format, buffer); | |
132 | |
133 struct jpeg_compress_struct cinfo; | |
134 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
135 | |
1604 | 136 Internals::JpegErrorManager jerr; |
1602 | 137 cinfo.err = jerr.GetPublic(); |
138 | |
139 if (setjmp(jerr.GetJumpBuffer())) | |
140 { | |
141 /* If we get here, the JPEG code has signaled an error. | |
142 * We need to clean up the JPEG object, close the input file, and return. | |
143 */ | |
144 jpeg_destroy_compress(&cinfo); | |
145 fclose(fp); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
146 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
147 "Error during JPEG encoding: " + jerr.GetMessage()); |
1602 | 148 } |
149 | |
150 // Do not allocate data on the stack below this line! | |
151 | |
152 jpeg_create_compress(&cinfo); | |
153 jpeg_stdio_dest(&cinfo, fp); | |
154 Compress(cinfo, lines, width, height, format, quality_); | |
155 | |
156 // Everything went fine, "setjmp()" didn't get called | |
157 | |
158 fclose(fp); | |
159 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
160 #endif |
1602 | 161 |
162 | |
1916 | 163 void JpegWriter::WriteToMemoryInternal(std::string& jpeg, |
164 unsigned int width, | |
165 unsigned int height, | |
166 unsigned int pitch, | |
167 PixelFormat format, | |
168 const void* buffer) | |
1602 | 169 { |
170 std::vector<uint8_t*> lines; | |
171 GetLines(lines, height, pitch, format, buffer); | |
172 | |
173 struct jpeg_compress_struct cinfo; | |
174 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
175 | |
1604 | 176 Internals::JpegErrorManager jerr; |
1602 | 177 |
178 unsigned char* data = NULL; | |
179 unsigned long size; | |
180 | |
181 if (setjmp(jerr.GetJumpBuffer())) | |
182 { | |
183 jpeg_destroy_compress(&cinfo); | |
184 | |
185 if (data != NULL) | |
186 { | |
187 free(data); | |
188 } | |
189 | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
190 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
191 "Error during JPEG encoding: " + jerr.GetMessage()); |
1602 | 192 } |
193 | |
194 // Do not allocate data on the stack below this line! | |
195 | |
196 jpeg_create_compress(&cinfo); | |
197 cinfo.err = jerr.GetPublic(); | |
198 jpeg_mem_dest(&cinfo, &data, &size); | |
199 | |
200 Compress(cinfo, lines, width, height, format, quality_); | |
201 | |
202 // Everything went fine, "setjmp()" didn't get called | |
203 | |
204 jpeg.assign(reinterpret_cast<const char*>(data), size); | |
205 free(data); | |
206 } | |
4300 | 207 |
208 uint8_t JpegWriter::GetQuality() const | |
209 { | |
210 return quality_; | |
211 } | |
1602 | 212 } |