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