Mercurial > hg > orthanc
annotate Core/Images/JpegReader.cpp @ 3886:b808958dff2a transcoding
transcoding to jpeg-ls
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 05 May 2020 16:03:46 +0200 |
parents | 94f4a18a79cc |
children |
rev | line source |
---|---|
1604 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1604 | 4 * Department, University Hospital of Liege, Belgium |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3378
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
1604 | 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 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "JpegReader.h" | |
36 | |
37 #include "JpegErrorManager.h" | |
38 #include "../OrthancException.h" | |
39 #include "../Logging.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
40 |
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 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
44 |
1604 | 45 |
46 namespace Orthanc | |
47 { | |
48 static void Uncompress(struct jpeg_decompress_struct& cinfo, | |
49 std::string& content, | |
50 ImageAccessor& accessor) | |
51 { | |
2683 | 52 // The "static_cast" is necessary on OS X: |
53 // https://github.com/simonfuhrmann/mve/issues/371 | |
54 jpeg_read_header(&cinfo, static_cast<boolean>(true)); | |
55 | |
1604 | 56 jpeg_start_decompress(&cinfo); |
57 | |
58 PixelFormat format; | |
59 if (cinfo.output_components == 1 && | |
60 cinfo.out_color_space == JCS_GRAYSCALE) | |
61 { | |
62 format = PixelFormat_Grayscale8; | |
63 } | |
64 else if (cinfo.output_components == 3 && | |
65 cinfo.out_color_space == JCS_RGB) | |
66 { | |
67 format = PixelFormat_RGB24; | |
68 } | |
69 else | |
70 { | |
71 throw OrthancException(ErrorCode_NotImplemented); | |
72 } | |
73 | |
74 unsigned int pitch = cinfo.output_width * cinfo.output_components; | |
75 | |
76 /* Make a one-row-high sample array that will go away when done with image */ | |
77 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, pitch, 1); | |
78 | |
79 try | |
80 { | |
81 content.resize(pitch * cinfo.output_height); | |
82 } | |
83 catch (...) | |
84 { | |
85 throw OrthancException(ErrorCode_NotEnoughMemory); | |
86 } | |
87 | |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1605
diff
changeset
|
88 accessor.AssignWritable(format, cinfo.output_width, cinfo.output_height, pitch, |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1605
diff
changeset
|
89 content.empty() ? NULL : &content[0]); |
1604 | 90 |
91 uint8_t* target = reinterpret_cast<uint8_t*>(&content[0]); | |
92 while (cinfo.output_scanline < cinfo.output_height) | |
93 { | |
94 jpeg_read_scanlines(&cinfo, buffer, 1); | |
95 memcpy(target, buffer[0], pitch); | |
96 target += pitch; | |
97 } | |
98 | |
99 // Everything went fine, "setjmp()" didn't get called | |
100 | |
101 jpeg_finish_decompress(&cinfo); | |
102 } | |
103 | |
104 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
105 #if ORTHANC_SANDBOXED == 0 |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
106 void JpegReader::ReadFromFile(const std::string& filename) |
1604 | 107 { |
2140 | 108 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_ReadBinary); |
1604 | 109 if (!fp) |
110 { | |
111 throw OrthancException(ErrorCode_InexistentFile); | |
112 } | |
113 | |
114 struct jpeg_decompress_struct cinfo; | |
115 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct)); | |
116 | |
117 Internals::JpegErrorManager jerr; | |
118 cinfo.err = jerr.GetPublic(); | |
119 | |
120 if (setjmp(jerr.GetJumpBuffer())) | |
121 { | |
122 jpeg_destroy_decompress(&cinfo); | |
123 fclose(fp); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2683
diff
changeset
|
124 |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2683
diff
changeset
|
125 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2683
diff
changeset
|
126 "Error during JPEG decoding: " + jerr.GetMessage()); |
1604 | 127 } |
128 | |
129 // Below this line, we are under the scope of a "setjmp" | |
130 | |
131 jpeg_create_decompress(&cinfo); | |
132 jpeg_stdio_src(&cinfo, fp); | |
133 | |
134 try | |
135 { | |
136 Uncompress(cinfo, content_, *this); | |
137 } | |
138 catch (OrthancException&) | |
139 { | |
140 jpeg_destroy_decompress(&cinfo); | |
141 fclose(fp); | |
142 throw; | |
143 } | |
144 | |
145 jpeg_destroy_decompress(&cinfo); | |
146 fclose(fp); | |
147 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
148 #endif |
1604 | 149 |
150 | |
151 void JpegReader::ReadFromMemory(const void* buffer, | |
152 size_t size) | |
153 { | |
154 struct jpeg_decompress_struct cinfo; | |
155 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct)); | |
156 | |
157 Internals::JpegErrorManager jerr; | |
158 cinfo.err = jerr.GetPublic(); | |
159 | |
160 if (setjmp(jerr.GetJumpBuffer())) | |
161 { | |
162 jpeg_destroy_decompress(&cinfo); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2683
diff
changeset
|
163 throw OrthancException(ErrorCode_InternalError, |
3378
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
164 "Error during JPEG decoding: " + jerr.GetMessage()); |
1604 | 165 } |
166 | |
167 // Below this line, we are under the scope of a "setjmp" | |
168 jpeg_create_decompress(&cinfo); | |
3378
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
169 jpeg_mem_src(&cinfo, |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
170 const_cast<unsigned char*>( |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
171 reinterpret_cast<const unsigned char*>(buffer)), |
596cfabd72c5
Fixed a couple of truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents:
3060
diff
changeset
|
172 static_cast<unsigned long>(size)); |
1604 | 173 |
174 try | |
175 { | |
176 Uncompress(cinfo, content_, *this); | |
177 } | |
178 catch (OrthancException&) | |
179 { | |
180 jpeg_destroy_decompress(&cinfo); | |
181 throw; | |
182 } | |
183 | |
184 jpeg_destroy_decompress(&cinfo); | |
185 } | |
186 | |
187 | |
188 void JpegReader::ReadFromMemory(const std::string& buffer) | |
189 { | |
190 if (buffer.empty()) | |
191 { | |
192 ReadFromMemory(NULL, 0); | |
193 } | |
194 else | |
195 { | |
196 ReadFromMemory(buffer.c_str(), buffer.size()); | |
197 } | |
198 } | |
199 } |