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