annotate Core/Images/JpegWriter.cpp @ 2132:a43708636890

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