summaryrefslogtreecommitdiffstats
path: root/kimgio/gimp.h
blob: ac7685f8d1a875125f51a736a8005ed1b0d68222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#ifndef GIMP_H
#define GIMP_H
/*
 * gimp.h: Header for a Qt 3 plug-in for reading GIMP XCF image files
 * Copyright (C) 2001 lignum Computing, Inc. <[email protected]>
 * Copyright (C) 2004 Melchior FRANZ <[email protected]>
 *
 * This plug-in is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <tdeglobal.h>

/*
 * These are the constants and functions I extracted from The GIMP source
 * code. If the reader fails to work, this is probably the place to start
 * looking for discontinuities.
 */

// From GIMP "tile.h" v1.2

const uint TILE_WIDTH = 64;	//!< Width of a tile in the XCF file.
const uint TILE_HEIGHT = 64;	//!< Height of a tile in the XCF file.

// From GIMP "paint_funcs.c" v1.2

const int RANDOM_TABLE_SIZE = 4096; //!< Size of dissolve random number table.
const int RANDOM_SEED = 314159265; //!< Seed for dissolve random number table.
const double EPSILON = 0.0001;	//!< Roundup in alpha blending.

// From GIMP "paint_funcs.h" v1.2

const uchar OPAQUE_OPACITY = 255; //!< Opaque value for 8-bit alpha component.

// From GIMP "apptypes.h" v1.2

//! Basic GIMP image type. TQImage converter may produce a deeper image
//! than is specified here. For example, a grayscale image with an
//! alpha channel must (currently) use a 32-bit Qt image.

typedef enum
{
  RGB,
  GRAY,
  INDEXED
} GimpImageBaseType;

//! Type of individual layers in an XCF file.

typedef enum
{
  RGB_GIMAGE,
  RGBA_GIMAGE,
  GRAY_GIMAGE,
  GRAYA_GIMAGE,
  INDEXED_GIMAGE,
  INDEXEDA_GIMAGE
} GimpImageType;

//! Effect to apply when layers are merged together.

typedef enum
{
  NORMAL_MODE,
  DISSOLVE_MODE,
  BEHIND_MODE,
  MULTIPLY_MODE,
  SCREEN_MODE,
  OVERLAY_MODE,
  DIFFERENCE_MODE,
  ADDITION_MODE,
  SUBTRACT_MODE,
  DARKEN_ONLY_MODE,
  LIGHTEN_ONLY_MODE,
  HUE_MODE,
  SATURATION_MODE,
  COLOR_MODE,
  VALUE_MODE,
  DIVIDE_MODE,
  ERASE_MODE,
  REPLACE_MODE,
  ANTI_ERASE_MODE
} LayerModeEffects;

// From GIMP "xcf.c" v1.2

//! Properties which can be stored in an XCF file.

typedef enum
{
  PROP_END = 0,
  PROP_COLORMAP = 1,
  PROP_ACTIVE_LAYER = 2,
  PROP_ACTIVE_CHANNEL = 3,
  PROP_SELECTION = 4,
  PROP_FLOATING_SELECTION = 5,
  PROP_OPACITY = 6,
  PROP_MODE = 7,
  PROP_VISIBLE = 8,
  PROP_LINKED = 9,
  PROP_PRESERVE_TRANSPARENCY = 10,
  PROP_APPLY_MASK = 11,
  PROP_EDIT_MASK = 12,
  PROP_SHOW_MASK = 13,
  PROP_SHOW_MASKED = 14,
  PROP_OFFSETS = 15,
  PROP_COLOR = 16,
  PROP_COMPRESSION = 17,
  PROP_GUIDES = 18,
  PROP_RESOLUTION = 19,
  PROP_TATTOO = 20,
  PROP_PARASITES = 21,
  PROP_UNIT = 22,
  PROP_PATHS = 23,
  PROP_USER_UNIT = 24
} PropType;

// From GIMP "xcf.c" v1.2

//! Compression type used in layer tiles.

typedef enum
{
  COMPRESS_NONE = 0,
  COMPRESS_RLE = 1,
  COMPRESS_ZLIB = 2,
  COMPRESS_FRACTAL = 3  /* Unused. */
} CompressionType;

// From GIMP "paint_funcs.c" v1.2

/*!
 * Multiply two color components. Really expects the arguments to be
 * 8-bit quantities.
 * \param a first minuend.
 * \param b second minuend.
 * \return product of arguments.
 */
inline int INT_MULT ( int a, int b )
{
  int c = a * b + 0x80;
  return ( ( c >> 8 ) + c ) >> 8;
}

/*!
 * Blend the two color components in the proportion alpha:
 *
 * result = alpha a + ( 1 - alpha b)
 *
 * \param a first component.
 * \param b second component.
 * \param alpha blend proportion.
 * \return blended color components.
 */

inline int INT_BLEND ( int a, int b, int alpha )
{
  return INT_MULT( a - b, alpha ) + b;
}

// From GIMP "gimpcolorspace.c" v1.2

/*!
 * Convert a color in RGB space to HSV space (Hue, Saturation, Value).
 * \param red the red component (modified in place).
 * \param green the green component (modified in place).
 * \param blue the blue component (modified in place).
 */
void RGBTOHSV ( uchar& red, uchar& green, uchar& blue )
{
  int r, g, b;
  double h, s, v;
  int min, max;

  h = 0.;

  r = red;
  g = green;
  b = blue;

  if ( r > g ) {
    max = KMAX( r, b );
    min = KMIN( g, b );
  }
  else {
    max = KMAX( g, b );
    min = KMIN( r, b );
  }

  v = max;

  if ( max != 0 )
    s = ( ( max - min ) * 255 ) / (double)max;
  else
    s = 0;

  if ( s == 0 )
    h = 0;
  else {
    int delta = max - min;
    if ( r == max )
      h = ( g - b ) / (double)delta;
    else if ( g == max )
      h = 2 + ( b - r ) / (double)delta;
    else if ( b == max )
      h = 4 + ( r - g ) / (double)delta;
    h *= 42.5;

    if ( h < 0 )
      h += 255;
    if ( h > 255 )
      h -= 255;
  }

  red   = (uchar)h;
  green = (uchar)s;
  blue  = (uchar)v;
}

/*!
 * Convert a color in HSV space to RGB space.
 * \param hue the hue component (modified in place).
 * \param saturation the saturation component (modified in place).
 * \param value the value component (modified in place).
 */
void HSVTORGB ( uchar& hue, uchar& saturation, uchar& value )
{
  if ( saturation == 0 ) {
    hue        = value;
    saturation = value;
    value      = value;
  }
  else {
    double h = hue * 6. / 255.;
    double s = saturation / 255.;
    double v = value / 255.;

    double f = h - (int)h;
    double p = v * ( 1. - s );
    double q = v * ( 1. - ( s * f ) );
    double t = v * ( 1. - ( s * ( 1. - f ) ) );

    // Worth a note here that gcc 2.96 will generate different results
    // depending on optimization mode on i386.

    switch ((int)h) {
    case 0:
      hue        = (uchar)( v * 255 );
      saturation = (uchar)( t * 255 );
      value      = (uchar)( p * 255 );
      break;
    case 1:
      hue        = (uchar)( q * 255 );
      saturation = (uchar)( v * 255 );
      value      = (uchar)( p * 255 );
      break;
    case 2:
      hue        = (uchar)( p * 255 );
      saturation = (uchar)( v * 255 );
      value      = (uchar)( t * 255 );
      break;
    case 3:
      hue        = (uchar)( p * 255 );
      saturation = (uchar)( q * 255 );
      value      = (uchar)( v * 255 );
      break;
    case 4:
      hue        = (uchar)( t * 255 );
      saturation = (uchar)( p * 255 );
      value      = (uchar)( v * 255 );
      break;
    case 5:
      hue        = (uchar)( v * 255 );
      saturation = (uchar)( p * 255 );
      value      = (uchar)( q * 255 );
    }
  }
}

/*!
 * Convert a color in RGB space to HLS space (Hue, Lightness, Saturation).
 * \param red the red component (modified in place).
 * \param green the green component (modified in place).
 * \param blue the blue component (modified in place).
 */
void RGBTOHLS ( uchar& red, uchar& green, uchar& blue )
{
  int r = red;
  int g = green;
  int b = blue;

  int min, max;

  if ( r > g ) {
    max = KMAX( r, b );
    min = KMIN( g, b );
  }
  else {
    max = KMAX( g, b );
    min = KMIN( r, b );
  }

  double h;
  double l = ( max + min ) / 2.;
  double s;

  if ( max == min ) {
    s = 0.;
    h = 0.;
  }
  else {
    int delta = max - min;

    if ( l < 128 )
      s = 255 * (double)delta / (double)( max + min );
    else
      s = 255 * (double)delta / (double)( 511 - max - min );

    if ( r == max )
      h = ( g - b ) / (double)delta;
    else if ( g == max )
      h = 2 + ( b - r ) / (double)delta;
    else
      h = 4 + ( r - g ) / (double)delta;

    h *= 42.5;

    if ( h < 0 )
      h += 255;
    else if ( h > 255 )
      h -= 255;
  }

  red   = (uchar)h;
  green = (uchar)l;
  blue  = (uchar)s;
}

/*!
 * Implement the HLS "double hex-cone".
 * \param n1 lightness fraction (?)
 * \param n2 saturation fraction (?)
 * \param hue hue "angle".
 * \return HLS value.
 */
int HLSVALUE ( double n1, double n2, double hue )
{
  double value;

  if ( hue > 255 )
    hue -= 255;
  else if ( hue < 0 )
    hue += 255;

  if ( hue < 42.5 )
    value = n1 + ( n2 - n1 ) * ( hue / 42.5 );
  else if ( hue < 127.5 )
    value = n2;
  else if ( hue < 170 )
    value = n1 + ( n2 - n1 ) * ( ( 170 - hue ) / 42.5 );
  else
    value = n1;

  return (int)( value * 255 );
}

/*!
 * Convert a color in HLS space to RGB space.
 * \param hue the hue component (modified in place).
 * \param lightness the lightness component (modified in place).
 * \param saturation the saturation component (modified in place).
 */
void HLSTORGB ( uchar& hue, uchar& lightness, uchar& saturation )
{
  double h = hue;
  double l = lightness;
  double s = saturation;

  if ( s == 0 ) {
    hue        = (uchar)l;
    lightness  = (uchar)l;
    saturation = (uchar)l;
  }
  else {
    double m1, m2;

    if ( l < 128 )
      m2 = ( l * ( 255 + s ) ) / 65025.;
    else
      m2 = ( l + s - ( l * s ) / 255. ) / 255.;

    m1 = ( l / 127.5 ) - m2;

    hue        = HLSVALUE( m1, m2, h + 85 );
    lightness  = HLSVALUE( m1, m2, h );
    saturation = HLSVALUE( m1, m2, h - 85 );
  }
}
#endif