Debian Codegraph @

texk/xdvipsk/graphlib/conversion.cpp

raw
1 /* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki.
2
3 Copyright (C) 2016 by VTeX Ltd (www.vtex.lt),
4 the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis.
5
6 Program original code copyright by Floris van den Berg,
7 Hervé Drolon and Karl-Heinz Bussian, the FreeImage 3 project team.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24 // ==========================================================
25 // Bitmap conversion routines
26 //
27 // ==========================================================
28 #include <limits>
29
30 #include "bitmap.h"
31 #include "utilities.h"
32
33
34 // ==========================================================
35 // Utility functions declared in Utilities.h
36
37 BOOL SwapRedBlue32(FIBITMAP* dib) {
38 if(Bitmap_GetImageType(dib) != FIT_BITMAP) {
39 return FALSE;
40 }
41
42 const unsigned bytesperpixel = Bitmap_GetBPP(dib) / 8;
43 if(bytesperpixel > 4 || bytesperpixel < 3) {
44 return FALSE;
45 }
46
47 const unsigned height = Bitmap_GetHeight(dib);
48 const unsigned pitch = Bitmap_GetPitch(dib);
49 const unsigned lineSize = Bitmap_GetLine(dib);
50
51 BYTE* line = Bitmap_GetBits(dib);
52 for(unsigned y = 0; y < height; ++y, line += pitch) {
53 for(BYTE* pixel = line; pixel < line + lineSize ; pixel += bytesperpixel) {
54 BYTE tmp = pixel[0];
55 pixel[0] = pixel[2];
56 pixel[2] = tmp;
57 }
58 }
59
60 return TRUE;
61 }
62
63 // ----------------------------------------------------------
64
65 static inline void
66 assignRGB(WORD r, WORD g, WORD b, WORD* out) {
67 out[0] = r;
68 out[1] = g;
69 out[2] = b;
70 }
71
72 static inline void
73 assignRGB(BYTE r, BYTE g, BYTE b, BYTE* out) {
74 out[FI_RGBA_RED] = r;
75 out[FI_RGBA_GREEN] = g;
76 out[FI_RGBA_BLUE] = b;
77 }
78
79 /// Clamp function
80 template <class T>
81 static inline T
82 CLAMP(const T &value, const T &min_value, const T &max_value) {
83 return ((value < min_value) ? min_value : (value > max_value) ? max_value : value);
84 }
85
86 /**
87 CMYK -> CMY -> RGB conversion from http://www.easyrgb.com/
88
89 CMYK to CMY [0-1]: C,M,Y * (1 - K) + K
90 CMY to RGB [0-1]: (1 - C,M,Y)
91
92 => R,G,B = (1 - C,M,Y) * (1 - K)
93 mapped to [0-MAX_VAL]:
94 (MAX_VAL - C,M,Y) * (MAX_VAL - K) / MAX_VAL
95 */
96 template <class T>
97 static inline void
98 CMYKToRGB(T C, T M, T Y, T K, T* out) {
99 unsigned max_val = std::numeric_limits<T>::max();
100
101 unsigned r = (max_val - C) * (max_val - K) / max_val;
102 unsigned g = (max_val - M) * (max_val - K) / max_val;
103 unsigned b = (max_val - Y) * (max_val - K) / max_val;
104
105 // clamp values to [0..max_val]
106 T red = (T)CLAMP(r, (unsigned)0, max_val);
107 T green = (T)CLAMP(g, (unsigned)0, max_val);
108 T blue = (T)CLAMP(b, (unsigned)0, max_val);
109
110 assignRGB(red, green, blue, out);
111 }
112
113 template <class T>
114 static void
115 _convertCMYKtoRGBA(unsigned width, unsigned height, BYTE* line_start, unsigned pitch, unsigned samplesperpixel) {
116 const BOOL hasBlack = (samplesperpixel > 3) ? TRUE : FALSE;
117 const T MAX_VAL = std::numeric_limits<T>::max();
118
119 T K = 0;
120 for(unsigned y = 0; y < height; y++) {
121 T *line = (T*)line_start;
122
123 for(unsigned x = 0; x < width; x++) {
124 if(hasBlack) {
125 K = line[FI_RGBA_ALPHA];
126 line[FI_RGBA_ALPHA] = MAX_VAL; // TODO write the first extra channel as alpha!
127 }
128
129 CMYKToRGB<T>(line[0], line[1], line[2], K, line);
130
131 line += samplesperpixel;
132 }
133 line_start += pitch;
134 }
135 }
136
137 BOOL
138 ConvertCMYKtoRGBA(FIBITMAP* dib) {
139 if (!Bitmap_HasPixels(dib)) {
140 return FALSE;
141 }
142
143 const BITMAP_TYPE image_type = Bitmap_GetImageType(dib);
144 const unsigned bytesperpixel = Bitmap_GetBPP(dib) / 8;
145
146 unsigned channelSize = 1;
147 if (!(image_type == FIT_BITMAP && (bytesperpixel > 2))) {
148 return FALSE;
149 }
150
151 const unsigned width = Bitmap_GetWidth(dib);
152 const unsigned height = Bitmap_GetHeight(dib);
153 BYTE *line_start = Bitmap_GetScanLine(dib, 0);
154 const unsigned pitch = Bitmap_GetPitch(dib);
155
156 unsigned samplesperpixel = Bitmap_GetLine(dib) / width / channelSize;
157
158 if(channelSize == sizeof(WORD)) {
159 _convertCMYKtoRGBA<WORD>(width, height, line_start, pitch, samplesperpixel);
160 } else {
161 _convertCMYKtoRGBA<BYTE>(width, height, line_start, pitch, samplesperpixel);
162 }
163
164 return TRUE;
165 }
166
167 // ----------------------------------------------------------
168
169 /**
170 CIELab -> XYZ conversion from http://www.easyrgb.com/
171 */
172 static void
173 CIELabToXYZ(float L, float a, float b, float *X, float *Y, float *Z) {
174 float pow_3;
175
176 // CIELab -> XYZ conversion
177 // ------------------------
178 float var_Y = (L + 16.F ) / 116.F;
179 float var_X = a / 500.F + var_Y;
180 float var_Z = var_Y - b / 200.F;
181
182 pow_3 = powf(var_Y, 3);
183 if(pow_3 > 0.008856F) {
184 var_Y = pow_3;
185 } else {
186 var_Y = ( var_Y - 16.F / 116.F ) / 7.787F;
187 }
188 pow_3 = powf(var_X, 3);
189 if(pow_3 > 0.008856F) {
190 var_X = pow_3;
191 } else {
192 var_X = ( var_X - 16.F / 116.F ) / 7.787F;
193 }
194 pow_3 = powf(var_Z, 3);
195 if(pow_3 > 0.008856F) {
196 var_Z = pow_3;
197 } else {
198 var_Z = ( var_Z - 16.F / 116.F ) / 7.787F;
199 }
200
201 static const float ref_X = 95.047F;
202 static const float ref_Y = 100.000F;
203 static const float ref_Z = 108.883F;
204
205 *X = ref_X * var_X; // ref_X = 95.047 (Observer= 2°, Illuminant= D65)
206 *Y = ref_Y * var_Y; // ref_Y = 100.000
207 *Z = ref_Z * var_Z; // ref_Z = 108.883
208 }
209
210 /**
211 XYZ -> RGB conversion from http://www.easyrgb.com/
212 */
213 static void
214 XYZToRGB(float X, float Y, float Z, float *R, float *G, float *B) {
215 float var_X = X / 100; // X from 0 to 95.047 (Observer = 2°, Illuminant = D65)
216 float var_Y = Y / 100; // Y from 0 to 100.000
217 float var_Z = Z / 100; // Z from 0 to 108.883
218
219 float var_R = var_X * 3.2406F + var_Y * -1.5372F + var_Z * -0.4986F;
220 float var_G = var_X * -0.9689F + var_Y * 1.8758F + var_Z * 0.0415F;
221 float var_B = var_X * 0.0557F + var_Y * -0.2040F + var_Z * 1.0570F;
222
223 float exponent = 1.F / 2.4F;
224
225 if(var_R > 0.0031308F) {
226 var_R = 1.055F * powf(var_R, exponent) - 0.055F;
227 } else {
228 var_R = 12.92F * var_R;
229 }
230 if(var_G > 0.0031308F) {
231 var_G = 1.055F * powf(var_G, exponent) - 0.055F;
232 } else {
233 var_G = 12.92F * var_G;
234 }
235 if(var_B > 0.0031308F) {
236 var_B = 1.055F * powf(var_B, exponent) - 0.055F;
237 } else {
238 var_B = 12.92F * var_B;
239 }
240
241 *R = var_R;
242 *G = var_G;
243 *B = var_B;
244 }
245
246 template<class T>
247 static void
248 CIELabToRGB(float L, float a, float b, T *rgb) {
249 float X, Y, Z;
250 float R, G, B;
251 const float max_val = std::numeric_limits<T>::max();
252
253 CIELabToXYZ(L, a, b, &X, &Y, &Z);
254 XYZToRGB(X, Y, Z, &R, &G, &B);
255
256 // clamp values to [0..max_val]
257 T red = (T)CLAMP(R * max_val, 0.0F, max_val);
258 T green = (T)CLAMP(G * max_val, 0.0F, max_val);
259 T blue = (T)CLAMP(B * max_val, 0.0F, max_val);
260
261 assignRGB(red, green, blue, rgb);
262 }
263
264 template<class T>
265 static void
266 _convertLABtoRGB(unsigned width, unsigned height, BYTE* line_start, unsigned pitch, unsigned samplesperpixel) {
267 const unsigned max_val = std::numeric_limits<T>::max();
268 const float sL = 100.F / max_val;
269 const float sa = 256.F / max_val;
270 const float sb = 256.F / max_val;
271
272 for(unsigned y = 0; y < height; y++) {
273 T *line = (T*)line_start;
274
275 for(unsigned x = 0; x < width; x++) {
276 CIELabToRGB(line[0]* sL, line[1]* sa - 128.F, line[2]* sb - 128.F, line);
277
278 line += samplesperpixel;
279 }
280 line_start += pitch;
281 }
282 }
283
284 BOOL
285 ConvertLABtoRGB(FIBITMAP* dib) {
286 if (!Bitmap_HasPixels(dib)) {
287 return FALSE;
288 }
289
290 const BITMAP_TYPE image_type = Bitmap_GetImageType(dib);
291 const unsigned bytesperpixel = Bitmap_GetBPP(dib) / 8;
292
293 unsigned channelSize = 1;
294 if (!(image_type == FIT_BITMAP && (bytesperpixel > 2))) {
295 return FALSE;
296 }
297
298 const unsigned width = Bitmap_GetWidth(dib);
299 const unsigned height = Bitmap_GetHeight(dib);
300 BYTE *line_start = Bitmap_GetScanLine(dib, 0);
301 const unsigned pitch = Bitmap_GetPitch(dib);
302
303 unsigned samplesperpixel = Bitmap_GetLine(dib) / width / channelSize;
304
305 if(channelSize == 1) {
306 _convertLABtoRGB<BYTE>(width, height, line_start, pitch, samplesperpixel);
307 }
308 else {
309 _convertLABtoRGB<WORD>(width, height, line_start, pitch, samplesperpixel);
310 }
311
312 return TRUE;
313 }
314
315 // ----------------------------------------------------------
316
317 FIBITMAP*
318 RemoveAlphaChannel(FIBITMAP* src) {
319
320 if (!Bitmap_HasPixels(src)) {
321 return NULL;
322 }
323
324 const BITMAP_TYPE image_type = Bitmap_GetImageType(src);
325
326 switch(image_type) {
327 case FIT_BITMAP:
328 if (Bitmap_GetBPP(src) == 32) {
329 // convert to 24-bit
330 return Bitmap_ConvertTo24Bits(src);
331 }
332 break;
333 default:
334 // unsupported image type
335 return NULL;
336 }
337
338 return NULL;
339 }