annotate OrthancFramework/Sources/Images/JpegReader.cpp @ 5447:dacdce5e5c22

now accepting GET requests on the /tools/create-archive route and siblings
author Alain Mazy <am@osimis.io>
date Fri, 24 Nov 2023 18:14:52 +0100
parents 0ea402b4d901
children 48b8dae6dc77
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1612
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../PrecompiledHeaders.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "JpegReader.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "JpegErrorManager.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "../OrthancException.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 namespace Orthanc
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 static void Uncompress(struct jpeg_decompress_struct& cinfo,
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 std::string& content,
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 ImageAccessor& accessor)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
2683
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
42 // The "static_cast" is necessary on OS X:
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
43 // https://github.com/simonfuhrmann/mve/issues/371
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
44 jpeg_read_header(&cinfo, static_cast<boolean>(true));
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
45
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 jpeg_start_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 PixelFormat format;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 if (cinfo.output_components == 1 &&
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 cinfo.out_color_space == JCS_GRAYSCALE)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 format = PixelFormat_Grayscale8;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 else if (cinfo.output_components == 3 &&
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 cinfo.out_color_space == JCS_RGB)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 format = PixelFormat_RGB24;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 else
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 throw OrthancException(ErrorCode_NotImplemented);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 unsigned int pitch = cinfo.output_width * cinfo.output_components;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 /* Make a one-row-high sample array that will go away when done with image */
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, pitch, 1);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 try
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 content.resize(pitch * cinfo.output_height);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 catch (...)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 throw OrthancException(ErrorCode_NotEnoughMemory);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 uint8_t* target = reinterpret_cast<uint8_t*>(&content[0]);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 while (cinfo.output_scanline < cinfo.output_height)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 jpeg_read_scanlines(&cinfo, buffer, 1);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 memcpy(target, buffer[0], pitch);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 target += pitch;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 // Everything went fine, "setjmp()" didn't get called
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 jpeg_finish_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2085
diff changeset
98 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_ReadBinary);
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if (!fp)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 throw OrthancException(ErrorCode_InexistentFile);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 struct jpeg_decompress_struct cinfo;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 Internals::JpegErrorManager jerr;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 cinfo.err = jerr.GetPublic();
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 if (setjmp(jerr.GetJumpBuffer()))
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 // Below this line, we are under the scope of a "setjmp"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 jpeg_create_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 jpeg_stdio_src(&cinfo, fp);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 try
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 Uncompress(cinfo, content_, *this);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 catch (OrthancException&)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 fclose(fp);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 throw;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 fclose(fp);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 }
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
138 #endif
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 void JpegReader::ReadFromMemory(const void* buffer,
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 size_t size)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 struct jpeg_decompress_struct cinfo;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 Internals::JpegErrorManager jerr;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 cinfo.err = jerr.GetPublic();
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 if (setjmp(jerr.GetJumpBuffer()))
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 // Below this line, we are under the scope of a "setjmp"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 try
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 Uncompress(cinfo, content_, *this);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 catch (OrthancException&)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 throw;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 void JpegReader::ReadFromMemory(const std::string& buffer)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 if (buffer.empty())
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 ReadFromMemory(NULL, 0);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 else
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 ReadFromMemory(buffer.c_str(), buffer.size());
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 }