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