1 | #ifndef GIMP_H |
2 | #define GIMP_H |
3 | /* -*- c++ -*- |
4 | * gimp.h: Header for a Qt 3 plug-in for reading GIMP XCF image files |
5 | * Copyright (C) 2001 lignum Computing, Inc. <allen@lignumcomputing.com> |
6 | * Copyright (C) 2004 Melchior FRANZ <mfranz@kde.org> |
7 | * |
8 | * This plug-in is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Lesser General Public |
10 | * License as published by the Free Software Foundation; either |
11 | * version 2.1 of the License, or (at your option) any later version. |
12 | * |
13 | * This library is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Lesser General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Lesser General Public |
19 | * License along with this library; if not, write to the Free Software |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | * |
22 | */ |
23 | |
24 | typedef unsigned char uchar; |
25 | |
26 | /* |
27 | * These are the constants and functions I extracted from The GIMP source |
28 | * code. If the reader fails to work, this is probably the place to start |
29 | * looking for discontinuities. |
30 | */ |
31 | |
32 | // From GIMP "tile.h" v1.2 |
33 | |
34 | const uint TILE_WIDTH = 64; //!< Width of a tile in the XCF file. |
35 | const uint TILE_HEIGHT = 64; //!< Height of a tile in the XCF file. |
36 | |
37 | // From GIMP "paint_funcs.c" v1.2 |
38 | |
39 | const int RANDOM_TABLE_SIZE = 4096; //!< Size of dissolve random number table. |
40 | const int RANDOM_SEED = 314159265; //!< Seed for dissolve random number table. |
41 | const double EPSILON = 0.0001; //!< Roundup in alpha blending. |
42 | |
43 | // From GIMP "paint_funcs.h" v1.2 |
44 | |
45 | const uchar OPAQUE_OPACITY = 255; //!< Opaque value for 8-bit alpha component. |
46 | |
47 | // From GIMP "apptypes.h" v1.2 |
48 | |
49 | //! Basic GIMP image type. QImage converter may produce a deeper image |
50 | //! than is specified here. For example, a grayscale image with an |
51 | //! alpha channel must (currently) use a 32-bit Qt image. |
52 | |
53 | typedef enum |
54 | { |
55 | RGB, |
56 | GRAY, |
57 | INDEXED |
58 | } GimpImageBaseType; |
59 | |
60 | //! Type of individual layers in an XCF file. |
61 | |
62 | typedef enum |
63 | { |
64 | RGB_GIMAGE, |
65 | RGBA_GIMAGE, |
66 | GRAY_GIMAGE, |
67 | GRAYA_GIMAGE, |
68 | INDEXED_GIMAGE, |
69 | INDEXEDA_GIMAGE |
70 | } GimpImageType; |
71 | |
72 | // From GIMP "libgimp/gimpenums.h" v2.4 |
73 | |
74 | //! Effect to apply when layers are merged together. |
75 | |
76 | typedef enum |
77 | { |
78 | NORMAL_MODE, |
79 | DISSOLVE_MODE, |
80 | BEHIND_MODE, |
81 | MULTIPLY_MODE, |
82 | SCREEN_MODE, |
83 | OVERLAY_MODE, |
84 | DIFFERENCE_MODE, |
85 | ADDITION_MODE, |
86 | SUBTRACT_MODE, |
87 | DARKEN_ONLY_MODE, |
88 | LIGHTEN_ONLY_MODE, |
89 | HUE_MODE, |
90 | SATURATION_MODE, |
91 | COLOR_MODE, |
92 | VALUE_MODE, |
93 | DIVIDE_MODE, |
94 | DODGE_MODE, |
95 | BURN_MODE, |
96 | HARDLIGHT_MODE, |
97 | SOFTLIGHT_MODE, |
98 | , |
99 | GRAIN_MERGE_MODE |
100 | } LayerModeEffects; |
101 | |
102 | // From GIMP "xcf.c" v1.2 |
103 | |
104 | //! Properties which can be stored in an XCF file. |
105 | |
106 | typedef enum |
107 | { |
108 | PROP_END = 0, |
109 | PROP_COLORMAP = 1, |
110 | PROP_ACTIVE_LAYER = 2, |
111 | PROP_ACTIVE_CHANNEL = 3, |
112 | PROP_SELECTION = 4, |
113 | PROP_FLOATING_SELECTION = 5, |
114 | PROP_OPACITY = 6, |
115 | PROP_MODE = 7, |
116 | PROP_VISIBLE = 8, |
117 | PROP_LINKED = 9, |
118 | PROP_PRESERVE_TRANSPARENCY = 10, |
119 | PROP_APPLY_MASK = 11, |
120 | PROP_EDIT_MASK = 12, |
121 | PROP_SHOW_MASK = 13, |
122 | PROP_SHOW_MASKED = 14, |
123 | PROP_OFFSETS = 15, |
124 | PROP_COLOR = 16, |
125 | PROP_COMPRESSION = 17, |
126 | PROP_GUIDES = 18, |
127 | PROP_RESOLUTION = 19, |
128 | PROP_TATTOO = 20, |
129 | PROP_PARASITES = 21, |
130 | PROP_UNIT = 22, |
131 | PROP_PATHS = 23, |
132 | PROP_USER_UNIT = 24 |
133 | } PropType; |
134 | |
135 | // From GIMP "xcf.c" v1.2 |
136 | |
137 | //! Compression type used in layer tiles. |
138 | |
139 | typedef enum |
140 | { |
141 | COMPRESS_NONE = 0, |
142 | COMPRESS_RLE = 1, |
143 | COMPRESS_ZLIB = 2, |
144 | COMPRESS_FRACTAL = 3 /* Unused. */ |
145 | } CompressionType; |
146 | |
147 | // From GIMP "paint_funcs.c" v1.2 |
148 | |
149 | /*! |
150 | * Multiply two color components. Really expects the arguments to be |
151 | * 8-bit quantities. |
152 | * \param a first minuend. |
153 | * \param b second minuend. |
154 | * \return product of arguments. |
155 | */ |
156 | inline int INT_MULT ( int a, int b ) |
157 | { |
158 | int c = a * b + 0x80; |
159 | return ( ( c >> 8 ) + c ) >> 8; |
160 | } |
161 | |
162 | /*! |
163 | * Blend the two color components in the proportion alpha: |
164 | * |
165 | * result = alpha a + ( 1 - alpha ) b |
166 | * |
167 | * \param a first component. |
168 | * \param b second component. |
169 | * \param alpha blend proportion. |
170 | * \return blended color components. |
171 | */ |
172 | |
173 | inline int INT_BLEND ( int a, int b, int alpha ) |
174 | { |
175 | return INT_MULT( a - b, alpha ) + b; |
176 | } |
177 | |
178 | // From GIMP "gimpcolorspace.c" v1.2 |
179 | |
180 | /*! |
181 | * Convert a color in RGB space to HSV space (Hue, Saturation, Value). |
182 | * \param red the red component (modified in place). |
183 | * \param green the green component (modified in place). |
184 | * \param blue the blue component (modified in place). |
185 | */ |
186 | static void RGBTOHSV ( uchar& red, uchar& green, uchar& blue ) |
187 | { |
188 | int r, g, b; |
189 | double h, s, v; |
190 | int min, max; |
191 | |
192 | h = 0.; |
193 | |
194 | r = red; |
195 | g = green; |
196 | b = blue; |
197 | |
198 | if ( r > g ) { |
199 | max = qMax( r, b ); |
200 | min = qMin( g, b ); |
201 | } |
202 | else { |
203 | max = qMax( g, b ); |
204 | min = qMin( r, b ); |
205 | } |
206 | |
207 | v = max; |
208 | |
209 | if ( max != 0 ) |
210 | s = ( ( max - min ) * 255 ) / (double)max; |
211 | else |
212 | s = 0; |
213 | |
214 | if ( s == 0 ) |
215 | h = 0; |
216 | else { |
217 | int delta = max - min; |
218 | if ( r == max ) |
219 | h = ( g - b ) / (double)delta; |
220 | else if ( g == max ) |
221 | h = 2 + ( b - r ) / (double)delta; |
222 | else if ( b == max ) |
223 | h = 4 + ( r - g ) / (double)delta; |
224 | h *= 42.5; |
225 | |
226 | if ( h < 0 ) |
227 | h += 255; |
228 | if ( h > 255 ) |
229 | h -= 255; |
230 | } |
231 | |
232 | red = (uchar)h; |
233 | green = (uchar)s; |
234 | blue = (uchar)v; |
235 | } |
236 | |
237 | /*! |
238 | * Convert a color in HSV space to RGB space. |
239 | * \param hue the hue component (modified in place). |
240 | * \param saturation the saturation component (modified in place). |
241 | * \param value the value component (modified in place). |
242 | */ |
243 | static void HSVTORGB ( uchar& hue, uchar& saturation, uchar& value ) |
244 | { |
245 | if ( saturation == 0 ) { |
246 | hue = value; |
247 | saturation = value; |
248 | //value = value; |
249 | } |
250 | else { |
251 | double h = hue * 6. / 255.; |
252 | double s = saturation / 255.; |
253 | double v = value / 255.; |
254 | |
255 | double f = h - (int)h; |
256 | double p = v * ( 1. - s ); |
257 | double q = v * ( 1. - ( s * f ) ); |
258 | double t = v * ( 1. - ( s * ( 1. - f ) ) ); |
259 | |
260 | // Worth a note here that gcc 2.96 will generate different results |
261 | // depending on optimization mode on i386. |
262 | |
263 | switch ((int)h) { |
264 | case 0: |
265 | hue = (uchar)( v * 255 ); |
266 | saturation = (uchar)( t * 255 ); |
267 | value = (uchar)( p * 255 ); |
268 | break; |
269 | case 1: |
270 | hue = (uchar)( q * 255 ); |
271 | saturation = (uchar)( v * 255 ); |
272 | value = (uchar)( p * 255 ); |
273 | break; |
274 | case 2: |
275 | hue = (uchar)( p * 255 ); |
276 | saturation = (uchar)( v * 255 ); |
277 | value = (uchar)( t * 255 ); |
278 | break; |
279 | case 3: |
280 | hue = (uchar)( p * 255 ); |
281 | saturation = (uchar)( q * 255 ); |
282 | value = (uchar)( v * 255 ); |
283 | break; |
284 | case 4: |
285 | hue = (uchar)( t * 255 ); |
286 | saturation = (uchar)( p * 255 ); |
287 | value = (uchar)( v * 255 ); |
288 | break; |
289 | case 5: |
290 | hue = (uchar)( v * 255 ); |
291 | saturation = (uchar)( p * 255 ); |
292 | value = (uchar)( q * 255 ); |
293 | } |
294 | } |
295 | } |
296 | |
297 | /*! |
298 | * Convert a color in RGB space to HLS space (Hue, Lightness, Saturation). |
299 | * \param red the red component (modified in place). |
300 | * \param green the green component (modified in place). |
301 | * \param blue the blue component (modified in place). |
302 | */ |
303 | static void RGBTOHLS ( uchar& red, uchar& green, uchar& blue ) |
304 | { |
305 | int r = red; |
306 | int g = green; |
307 | int b = blue; |
308 | |
309 | int min, max; |
310 | |
311 | if ( r > g ) { |
312 | max = qMax( r, b ); |
313 | min = qMin( g, b ); |
314 | } |
315 | else { |
316 | max = qMax( g, b ); |
317 | min = qMin( r, b ); |
318 | } |
319 | |
320 | double h; |
321 | double l = ( max + min ) / 2.; |
322 | double s; |
323 | |
324 | if ( max == min ) { |
325 | s = 0.; |
326 | h = 0.; |
327 | } |
328 | else { |
329 | int delta = max - min; |
330 | |
331 | if ( l < 128 ) |
332 | s = 255 * (double)delta / (double)( max + min ); |
333 | else |
334 | s = 255 * (double)delta / (double)( 511 - max - min ); |
335 | |
336 | if ( r == max ) |
337 | h = ( g - b ) / (double)delta; |
338 | else if ( g == max ) |
339 | h = 2 + ( b - r ) / (double)delta; |
340 | else |
341 | h = 4 + ( r - g ) / (double)delta; |
342 | |
343 | h *= 42.5; |
344 | |
345 | if ( h < 0 ) |
346 | h += 255; |
347 | else if ( h > 255 ) |
348 | h -= 255; |
349 | } |
350 | |
351 | red = (uchar)h; |
352 | green = (uchar)l; |
353 | blue = (uchar)s; |
354 | } |
355 | |
356 | /*! |
357 | * Implement the HLS "double hex-cone". |
358 | * \param n1 lightness fraction (?) |
359 | * \param n2 saturation fraction (?) |
360 | * \param hue hue "angle". |
361 | * \return HLS value. |
362 | */ |
363 | static int HLSVALUE ( double n1, double n2, double hue ) |
364 | { |
365 | double value; |
366 | |
367 | if ( hue > 255 ) |
368 | hue -= 255; |
369 | else if ( hue < 0 ) |
370 | hue += 255; |
371 | |
372 | if ( hue < 42.5 ) |
373 | value = n1 + ( n2 - n1 ) * ( hue / 42.5 ); |
374 | else if ( hue < 127.5 ) |
375 | value = n2; |
376 | else if ( hue < 170 ) |
377 | value = n1 + ( n2 - n1 ) * ( ( 170 - hue ) / 42.5 ); |
378 | else |
379 | value = n1; |
380 | |
381 | return (int)( value * 255 ); |
382 | } |
383 | |
384 | /*! |
385 | * Convert a color in HLS space to RGB space. |
386 | * \param hue the hue component (modified in place). |
387 | * \param lightness the lightness component (modified in place). |
388 | * \param saturation the saturation component (modified in place). |
389 | */ |
390 | static void HLSTORGB ( uchar& hue, uchar& lightness, uchar& saturation ) |
391 | { |
392 | double h = hue; |
393 | double l = lightness; |
394 | double s = saturation; |
395 | |
396 | if ( s == 0 ) { |
397 | hue = (uchar)l; |
398 | lightness = (uchar)l; |
399 | saturation = (uchar)l; |
400 | } |
401 | else { |
402 | double m1, m2; |
403 | |
404 | if ( l < 128 ) |
405 | m2 = ( l * ( 255 + s ) ) / 65025.; |
406 | else |
407 | m2 = ( l + s - ( l * s ) / 255. ) / 255.; |
408 | |
409 | m1 = ( l / 127.5 ) - m2; |
410 | |
411 | hue = HLSVALUE( m1, m2, h + 85 ); |
412 | lightness = HLSVALUE( m1, m2, h ); |
413 | saturation = HLSVALUE( m1, m2, h - 85 ); |
414 | } |
415 | } |
416 | #endif |
417 | |