annotate Resources/Orthanc/Core/Images/JpegWriter.cpp @ 200:03afbee0cc7b

integration of Orthanc core into Stone
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 23 Mar 2018 11:04:03 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
200
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "../PrecompiledHeaders.h"
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "JpegWriter.h"
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include "../OrthancException.h"
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include "../Logging.h"
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include "JpegErrorManager.h"
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #if ORTHANC_SANDBOXED == 0
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 # include "../SystemToolbox.h"
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #endif
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 #include <stdlib.h>
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 #include <vector>
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 namespace Orthanc
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 static void GetLines(std::vector<uint8_t*>& lines,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 unsigned int height,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 unsigned int pitch,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 PixelFormat format,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 const void* buffer)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 if (format != PixelFormat_Grayscale8 &&
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 format != PixelFormat_RGB24)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 throw OrthancException(ErrorCode_ParameterOutOfRange);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 lines.resize(height);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer));
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 for (unsigned int y = 0; y < height; y++)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 static void Compress(struct jpeg_compress_struct& cinfo,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 std::vector<uint8_t*>& lines,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 unsigned int width,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 unsigned int height,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 PixelFormat format,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 uint8_t quality)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 cinfo.image_width = width;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 cinfo.image_height = height;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 switch (format)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 case PixelFormat_Grayscale8:
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 cinfo.input_components = 1;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 cinfo.in_color_space = JCS_GRAYSCALE;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 break;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 case PixelFormat_RGB24:
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 cinfo.input_components = 3;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 cinfo.in_color_space = JCS_RGB;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 break;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 default:
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 throw OrthancException(ErrorCode_InternalError);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 jpeg_set_defaults(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 jpeg_set_quality(&cinfo, quality, TRUE);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 jpeg_start_compress(&cinfo, TRUE);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 jpeg_write_scanlines(&cinfo, &lines[0], height);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 jpeg_finish_compress(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 jpeg_destroy_compress(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 void JpegWriter::SetQuality(uint8_t quality)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 if (quality == 0 || quality > 100)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 throw OrthancException(ErrorCode_ParameterOutOfRange);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 quality_ = quality;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 #if ORTHANC_SANDBOXED == 0
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 void JpegWriter::WriteToFileInternal(const std::string& filename,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 unsigned int width,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 unsigned int height,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 unsigned int pitch,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 PixelFormat format,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 const void* buffer)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 if (fp == NULL)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 throw OrthancException(ErrorCode_CannotWriteFile);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 std::vector<uint8_t*> lines;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 GetLines(lines, height, pitch, format, buffer);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 struct jpeg_compress_struct cinfo;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 Internals::JpegErrorManager jerr;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 cinfo.err = jerr.GetPublic();
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 if (setjmp(jerr.GetJumpBuffer()))
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 /* If we get here, the JPEG code has signaled an error.
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 * We need to clean up the JPEG object, close the input file, and return.
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 */
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 jpeg_destroy_compress(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 fclose(fp);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 throw OrthancException(ErrorCode_InternalError);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 // Do not allocate data on the stack below this line!
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 jpeg_create_compress(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 jpeg_stdio_dest(&cinfo, fp);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 Compress(cinfo, lines, width, height, format, quality_);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 // Everything went fine, "setjmp()" didn't get called
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 fclose(fp);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 #endif
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 #if ORTHANC_SANDBOXED == 0
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 void JpegWriter::WriteToMemoryInternal(std::string& jpeg,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 unsigned int width,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 unsigned int height,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 unsigned int pitch,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 PixelFormat format,
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 const void* buffer)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 std::vector<uint8_t*> lines;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 GetLines(lines, height, pitch, format, buffer);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 struct jpeg_compress_struct cinfo;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 Internals::JpegErrorManager jerr;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 unsigned char* data = NULL;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 unsigned long size;
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 if (setjmp(jerr.GetJumpBuffer()))
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 jpeg_destroy_compress(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 if (data != NULL)
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 free(data);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 LOG(ERROR) << "Error during JPEG encoding: " << jerr.GetMessage();
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 throw OrthancException(ErrorCode_InternalError);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 // Do not allocate data on the stack below this line!
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 jpeg_create_compress(&cinfo);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 cinfo.err = jerr.GetPublic();
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 jpeg_mem_dest(&cinfo, &data, &size);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 Compress(cinfo, lines, width, height, format, quality_);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 // Everything went fine, "setjmp()" didn't get called
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 jpeg.assign(reinterpret_cast<const char*>(data), size);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 free(data);
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 }
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 #endif
03afbee0cc7b integration of Orthanc core into Stone
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }