Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/JpegWriter.cpp @ 4831:7053502fbf97
added copyright UCLouvain
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 25 Nov 2021 19:01:11 +0100 |
parents | d9473bd5ed43 |
children | 43e613a7756b |
rev | line source |
---|---|
1602 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1602 | 4 * Department, University Hospital of Liege, Belgium |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4300
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
4831
7053502fbf97
added copyright UCLouvain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
6 * Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1602 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
1602 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
1602 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
1602 | 21 **/ |
22 | |
23 | |
1604 | 24 #include "../PrecompiledHeaders.h" |
1602 | 25 #include "JpegWriter.h" |
26 | |
27 #include "../OrthancException.h" | |
28 #include "../Logging.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
29 #include "JpegErrorManager.h" |
1602 | 30 |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
31 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
32 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
33 #endif |
1604 | 34 |
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
|
35 #include <stdlib.h> |
1602 | 36 #include <vector> |
37 | |
38 namespace Orthanc | |
39 { | |
40 static void GetLines(std::vector<uint8_t*>& lines, | |
41 unsigned int height, | |
42 unsigned int pitch, | |
43 PixelFormat format, | |
44 const void* buffer) | |
45 { | |
46 if (format != PixelFormat_Grayscale8 && | |
47 format != PixelFormat_RGB24) | |
48 { | |
49 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
50 } | |
51 | |
52 lines.resize(height); | |
53 | |
54 uint8_t* base = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)); | |
55 for (unsigned int y = 0; y < height; y++) | |
56 { | |
57 lines[y] = base + static_cast<intptr_t>(y) * static_cast<intptr_t>(pitch); | |
58 } | |
59 } | |
60 | |
61 | |
62 static void Compress(struct jpeg_compress_struct& cinfo, | |
63 std::vector<uint8_t*>& lines, | |
64 unsigned int width, | |
65 unsigned int height, | |
66 PixelFormat format, | |
1654
3727a09e7b53
fix some icc warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1612
diff
changeset
|
67 uint8_t quality) |
1602 | 68 { |
69 cinfo.image_width = width; | |
70 cinfo.image_height = height; | |
71 | |
72 switch (format) | |
73 { | |
74 case PixelFormat_Grayscale8: | |
75 cinfo.input_components = 1; | |
76 cinfo.in_color_space = JCS_GRAYSCALE; | |
77 break; | |
78 | |
79 case PixelFormat_RGB24: | |
80 cinfo.input_components = 3; | |
81 cinfo.in_color_space = JCS_RGB; | |
82 break; | |
83 | |
84 default: | |
85 throw OrthancException(ErrorCode_InternalError); | |
86 } | |
87 | |
88 jpeg_set_defaults(&cinfo); | |
2683 | 89 |
90 // The "static_cast" is necessary on OS X: | |
91 // https://github.com/simonfuhrmann/mve/issues/371 | |
92 jpeg_set_quality(&cinfo, quality, static_cast<boolean>(true)); | |
93 jpeg_start_compress(&cinfo, static_cast<boolean>(true)); | |
94 | |
1602 | 95 jpeg_write_scanlines(&cinfo, &lines[0], height); |
96 jpeg_finish_compress(&cinfo); | |
97 jpeg_destroy_compress(&cinfo); | |
98 } | |
99 | |
100 | |
4300 | 101 JpegWriter::JpegWriter() : quality_(90) |
102 { | |
103 } | |
104 | |
105 | |
1602 | 106 void JpegWriter::SetQuality(uint8_t quality) |
107 { | |
2223 | 108 if (quality == 0 || quality > 100) |
1602 | 109 { |
110 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
111 } | |
112 | |
113 quality_ = quality; | |
114 } | |
115 | |
116 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
117 #if ORTHANC_SANDBOXED == 0 |
1916 | 118 void JpegWriter::WriteToFileInternal(const std::string& filename, |
119 unsigned int width, | |
120 unsigned int height, | |
121 unsigned int pitch, | |
122 PixelFormat format, | |
123 const void* buffer) | |
1602 | 124 { |
2140 | 125 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
1602 | 126 if (fp == NULL) |
127 { | |
2017 | 128 throw OrthancException(ErrorCode_CannotWriteFile); |
1602 | 129 } |
130 | |
131 std::vector<uint8_t*> lines; | |
132 GetLines(lines, height, pitch, format, buffer); | |
133 | |
134 struct jpeg_compress_struct cinfo; | |
135 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
136 | |
1604 | 137 Internals::JpegErrorManager jerr; |
1602 | 138 cinfo.err = jerr.GetPublic(); |
139 | |
140 if (setjmp(jerr.GetJumpBuffer())) | |
141 { | |
142 /* If we get here, the JPEG code has signaled an error. | |
143 * We need to clean up the JPEG object, close the input file, and return. | |
144 */ | |
145 jpeg_destroy_compress(&cinfo); | |
146 fclose(fp); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
147 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
148 "Error during JPEG encoding: " + jerr.GetMessage()); |
1602 | 149 } |
150 | |
151 // Do not allocate data on the stack below this line! | |
152 | |
153 jpeg_create_compress(&cinfo); | |
154 jpeg_stdio_dest(&cinfo, fp); | |
155 Compress(cinfo, lines, width, height, format, quality_); | |
156 | |
157 // Everything went fine, "setjmp()" didn't get called | |
158 | |
159 fclose(fp); | |
160 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
161 #endif |
1602 | 162 |
163 | |
1916 | 164 void JpegWriter::WriteToMemoryInternal(std::string& jpeg, |
165 unsigned int width, | |
166 unsigned int height, | |
167 unsigned int pitch, | |
168 PixelFormat format, | |
169 const void* buffer) | |
1602 | 170 { |
171 std::vector<uint8_t*> lines; | |
172 GetLines(lines, height, pitch, format, buffer); | |
173 | |
174 struct jpeg_compress_struct cinfo; | |
175 memset(&cinfo, 0, sizeof(struct jpeg_compress_struct)); | |
176 | |
1604 | 177 Internals::JpegErrorManager jerr; |
1602 | 178 |
179 unsigned char* data = NULL; | |
180 unsigned long size; | |
181 | |
182 if (setjmp(jerr.GetJumpBuffer())) | |
183 { | |
184 jpeg_destroy_compress(&cinfo); | |
185 | |
186 if (data != NULL) | |
187 { | |
188 free(data); | |
189 } | |
190 | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
191 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2699
diff
changeset
|
192 "Error during JPEG encoding: " + jerr.GetMessage()); |
1602 | 193 } |
194 | |
195 // Do not allocate data on the stack below this line! | |
196 | |
197 jpeg_create_compress(&cinfo); | |
198 cinfo.err = jerr.GetPublic(); | |
199 jpeg_mem_dest(&cinfo, &data, &size); | |
200 | |
201 Compress(cinfo, lines, width, height, format, quality_); | |
202 | |
203 // Everything went fine, "setjmp()" didn't get called | |
204 | |
205 jpeg.assign(reinterpret_cast<const char*>(data), size); | |
206 free(data); | |
207 } | |
4300 | 208 |
209 uint8_t JpegWriter::GetQuality() const | |
210 { | |
211 return quality_; | |
212 } | |
1602 | 213 } |