annotate Core/Images/JpegReader.cpp @ 1723:40a8445fc81d db-changes

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 20 Oct 2015 10:45:21 +0200
parents 96582230ddcb
children b1291df2f780
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "../PrecompiledHeaders.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "JpegReader.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "JpegErrorManager.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include "../OrthancException.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include "../Logging.h"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 namespace Orthanc
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 static void Uncompress(struct jpeg_decompress_struct& cinfo,
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 std::string& content,
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 ImageAccessor& accessor)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 jpeg_read_header(&cinfo, TRUE);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 jpeg_start_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 PixelFormat format;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 if (cinfo.output_components == 1 &&
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 cinfo.out_color_space == JCS_GRAYSCALE)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 format = PixelFormat_Grayscale8;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 else if (cinfo.output_components == 3 &&
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 cinfo.out_color_space == JCS_RGB)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 format = PixelFormat_RGB24;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 else
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 throw OrthancException(ErrorCode_NotImplemented);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 unsigned int pitch = cinfo.output_width * cinfo.output_components;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 /* 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
68 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
69
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 try
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 content.resize(pitch * cinfo.output_height);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 catch (...)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 throw OrthancException(ErrorCode_NotEnoughMemory);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 uint8_t* target = reinterpret_cast<uint8_t*>(&content[0]);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 while (cinfo.output_scanline < cinfo.output_height)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 jpeg_read_scanlines(&cinfo, buffer, 1);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 memcpy(target, buffer[0], pitch);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 target += pitch;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 // Everything went fine, "setjmp()" didn't get called
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 jpeg_finish_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 void JpegReader::ReadFromFile(const char* filename)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 FILE* fp = fopen(filename, "rb");
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);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 throw OrthancException(ErrorCode_InternalError);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 // Below this line, we are under the scope of a "setjmp"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 jpeg_create_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 jpeg_stdio_src(&cinfo, fp);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 try
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 Uncompress(cinfo, content_, *this);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 catch (OrthancException&)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 fclose(fp);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 throw;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 fclose(fp);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 void JpegReader::ReadFromMemory(const void* buffer,
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 size_t size)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 struct jpeg_decompress_struct cinfo;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 Internals::JpegErrorManager jerr;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 cinfo.err = jerr.GetPublic();
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 if (setjmp(jerr.GetJumpBuffer()))
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 throw OrthancException(ErrorCode_InternalError);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 // Below this line, we are under the scope of a "setjmp"
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 jpeg_create_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 jpeg_mem_src(&cinfo, const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(buffer)), size);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 try
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 Uncompress(cinfo, content_, *this);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 catch (OrthancException&)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 throw;
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 jpeg_destroy_decompress(&cinfo);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 void JpegReader::ReadFromMemory(const std::string& buffer)
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 if (buffer.empty())
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 ReadFromMemory(NULL, 0);
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 else
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 {
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 ReadFromMemory(buffer.c_str(), buffer.size());
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 }
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }