annotate OrthancFramework/Sources/Images/JpegWriter.cpp @ 4188:a2f1c97002fe

removed bad include for win32
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 14 Sep 2020 20:34:47 +0200
parents bf7b9edf6b81
children b30a8de92ad9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1654
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * 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
8 * 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
9 * 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
10 * the License, or (at your option) any later version.
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * 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
15 * Lesser General Public License for more details.
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
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 * 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
18 * 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
19 * <http://www.gnu.org/licenses/>.
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1602
diff changeset
23 #include "../PrecompiledHeaders.h"
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "JpegWriter.h"
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../OrthancException.h"
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "../Logging.h"
2171
35febe19e874 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2170
diff changeset
28 #include "JpegErrorManager.h"
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
2171
35febe19e874 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2170
diff changeset
30 #if ORTHANC_SANDBOXED == 0
35febe19e874 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2170
diff changeset
31 # include "../SystemToolbox.h"
35febe19e874 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2170
diff changeset
32 #endif
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1602
diff changeset
33
1914
501769757bf9 flag to remove network support in dcmtk, removal of unneeded sources in static builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
34 #include <stdlib.h>
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <vector>
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 namespace Orthanc
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 static void GetLines(std::vector<uint8_t*>& lines,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 unsigned int height,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 unsigned int pitch,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 PixelFormat format,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 const void* buffer)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 if (format != PixelFormat_Grayscale8 &&
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 format != PixelFormat_RGB24)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 throw OrthancException(ErrorCode_ParameterOutOfRange);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 lines.resize(height);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer));
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 for (unsigned int y = 0; y < height; y++)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 static void Compress(struct jpeg_compress_struct& cinfo,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 std::vector<uint8_t*>& lines,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 unsigned int width,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 unsigned int height,
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 PixelFormat format,
1654
3727a09e7b53 fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1612
diff changeset
66 uint8_t quality)
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 cinfo.image_width = width;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 cinfo.image_height = height;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 switch (format)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 case PixelFormat_Grayscale8:
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 cinfo.input_components = 1;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 cinfo.in_color_space = JCS_GRAYSCALE;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 break;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 case PixelFormat_RGB24:
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 cinfo.input_components = 3;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 cinfo.in_color_space = JCS_RGB;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 break;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 default:
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 throw OrthancException(ErrorCode_InternalError);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 jpeg_set_defaults(&cinfo);
2683
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
88
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
89 // The "static_cast" is necessary on OS X:
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
90 // https://github.com/simonfuhrmann/mve/issues/371
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
91 jpeg_set_quality(&cinfo, quality, static_cast<boolean>(true));
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
92 jpeg_start_compress(&cinfo, static_cast<boolean>(true));
964fb9a5786d fix build on OS X
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
93
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 jpeg_write_scanlines(&cinfo, &lines[0], height);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 jpeg_finish_compress(&cinfo);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 jpeg_destroy_compress(&cinfo);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 void JpegWriter::SetQuality(uint8_t quality)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
2223
29689b30f9ae cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2171
diff changeset
102 if (quality == 0 || quality > 100)
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 throw OrthancException(ErrorCode_ParameterOutOfRange);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 quality_ = quality;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
111 #if ORTHANC_SANDBOXED == 0
1916
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
112 void JpegWriter::WriteToFileInternal(const std::string& filename,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
113 unsigned int width,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
114 unsigned int height,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
115 unsigned int pitch,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
116 PixelFormat format,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
117 const void* buffer)
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 {
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2083
diff changeset
119 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary);
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 if (fp == NULL)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
2017
08ce34cfacad refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1916
diff changeset
122 throw OrthancException(ErrorCode_CannotWriteFile);
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 std::vector<uint8_t*> lines;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 GetLines(lines, height, pitch, format, buffer);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 struct jpeg_compress_struct cinfo;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1602
diff changeset
131 Internals::JpegErrorManager jerr;
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 cinfo.err = jerr.GetPublic();
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 if (setjmp(jerr.GetJumpBuffer()))
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 /* If we get here, the JPEG code has signaled an error.
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 * We need to clean up the JPEG object, close the input file, and return.
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 */
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 jpeg_destroy_compress(&cinfo);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 fclose(fp);
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2699
diff changeset
141 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2699
diff changeset
142 "Error during JPEG encoding: " + jerr.GetMessage());
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 // Do not allocate data on the stack below this line!
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 jpeg_create_compress(&cinfo);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 jpeg_stdio_dest(&cinfo, fp);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 Compress(cinfo, lines, width, height, format, quality_);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 // Everything went fine, "setjmp()" didn't get called
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 fclose(fp);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
155 #endif
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
1916
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
158 void JpegWriter::WriteToMemoryInternal(std::string& jpeg,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
159 unsigned int width,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
160 unsigned int height,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
161 unsigned int pitch,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
162 PixelFormat format,
5bcf721bde4f IImageWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1914
diff changeset
163 const void* buffer)
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 std::vector<uint8_t*> lines;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 GetLines(lines, height, pitch, format, buffer);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 struct jpeg_compress_struct cinfo;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
1604
1f5d6a2f9638 JpegReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1602
diff changeset
171 Internals::JpegErrorManager jerr;
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 unsigned char* data = NULL;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 unsigned long size;
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 if (setjmp(jerr.GetJumpBuffer()))
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 jpeg_destroy_compress(&cinfo);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 if (data != NULL)
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 {
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 free(data);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2699
diff changeset
185 throw OrthancException(ErrorCode_InternalError,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2699
diff changeset
186 "Error during JPEG encoding: " + jerr.GetMessage());
1602
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 // Do not allocate data on the stack below this line!
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 jpeg_create_compress(&cinfo);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 cinfo.err = jerr.GetPublic();
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 jpeg_mem_dest(&cinfo, &data, &size);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 Compress(cinfo, lines, width, height, format, quality_);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 // Everything went fine, "setjmp()" didn't get called
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 jpeg.assign(reinterpret_cast<const char*>(data), size);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 free(data);
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
292bce3f54ed JpegWriter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }