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