TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gimp.h 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #include <tdeglobal.h>
  24. /*
  25. * These are the constants and functions I extracted from The GIMP source
  26. * code. If the reader fails to work, this is probably the place to start
  27. * looking for discontinuities.
  28. */
  29. // From GIMP "tile.h" v1.2
  30. const uint TILE_WIDTH = 64; //!< Width of a tile in the XCF file.
  31. const uint TILE_HEIGHT = 64; //!< Height of a tile in the XCF file.
  32. // From GIMP "paint_funcs.c" v1.2
  33. const int RANDOM_TABLE_SIZE = 4096; //!< Size of dissolve random number table.
  34. const int RANDOM_SEED = 314159265; //!< Seed for dissolve random number table.
  35. const double EPSILON = 0.0001; //!< Roundup in alpha blending.
  36. // From GIMP "paint_funcs.h" v1.2
  37. const uchar OPAQUE_OPACITY = 255; //!< Opaque value for 8-bit alpha component.
  38. // From GIMP "apptypes.h" v1.2
  39. //! Basic GIMP image type. TQImage converter may produce a deeper image
  40. //! than is specified here. For example, a grayscale image with an
  41. //! alpha channel must (currently) use a 32-bit Qt image.
  42. typedef enum
  43. {
  44. RGB,
  45. GRAY,
  46. INDEXED
  47. } GimpImageBaseType;
  48. //! Type of individual layers in an XCF file.
  49. typedef enum
  50. {
  51. RGB_GIMAGE,
  52. RGBA_GIMAGE,
  53. GRAY_GIMAGE,
  54. GRAYA_GIMAGE,
  55. INDEXED_GIMAGE,
  56. INDEXEDA_GIMAGE
  57. } GimpImageType;
  58. //! Effect to apply when layers are merged together.
  59. typedef enum
  60. {
  61. NORMAL_MODE,
  62. DISSOLVE_MODE,
  63. BEHIND_MODE,
  64. MULTIPLY_MODE,
  65. SCREEN_MODE,
  66. OVERLAY_MODE,
  67. DIFFERENCE_MODE,
  68. ADDITION_MODE,
  69. SUBTRACT_MODE,
  70. DARKEN_ONLY_MODE,
  71. LIGHTEN_ONLY_MODE,
  72. HUE_MODE,
  73. SATURATION_MODE,
  74. COLOR_MODE,
  75. VALUE_MODE,
  76. DIVIDE_MODE,
  77. ERASE_MODE,
  78. REPLACE_MODE,
  79. ANTI_ERASE_MODE
  80. } LayerModeEffects;
  81. // From GIMP "xcf.c" v1.2
  82. //! Properties which can be stored in an XCF file.
  83. typedef enum
  84. {
  85. PROP_END = 0,
  86. PROP_COLORMAP = 1,
  87. PROP_ACTIVE_LAYER = 2,
  88. PROP_ACTIVE_CHANNEL = 3,
  89. PROP_SELECTION = 4,
  90. PROP_FLOATING_SELECTION = 5,
  91. PROP_OPACITY = 6,
  92. PROP_MODE = 7,
  93. PROP_VISIBLE = 8,
  94. PROP_LINKED = 9,
  95. PROP_PRESERVE_TRANSPARENCY = 10,
  96. PROP_APPLY_MASK = 11,
  97. PROP_EDIT_MASK = 12,
  98. PROP_SHOW_MASK = 13,
  99. PROP_SHOW_MASKED = 14,
  100. PROP_OFFSETS = 15,
  101. PROP_COLOR = 16,
  102. PROP_COMPRESSION = 17,
  103. PROP_GUIDES = 18,
  104. PROP_RESOLUTION = 19,
  105. PROP_TATTOO = 20,
  106. PROP_PARASITES = 21,
  107. PROP_UNIT = 22,
  108. PROP_PATHS = 23,
  109. PROP_USER_UNIT = 24
  110. } PropType;
  111. // From GIMP "xcf.c" v1.2
  112. //! Compression type used in layer tiles.
  113. typedef enum
  114. {
  115. COMPRESS_NONE = 0,
  116. COMPRESS_RLE = 1,
  117. COMPRESS_ZLIB = 2,
  118. COMPRESS_FRACTAL = 3 /* Unused. */
  119. } CompressionType;
  120. // From GIMP "paint_funcs.c" v1.2
  121. /*!
  122. * Multiply two color components. Really expects the arguments to be
  123. * 8-bit quantities.
  124. * \param a first minuend.
  125. * \param b second minuend.
  126. * \return product of arguments.
  127. */
  128. inline int INT_MULT ( int a, int b )
  129. {
  130. int c = a * b + 0x80;
  131. return ( ( c >> 8 ) + c ) >> 8;
  132. }
  133. /*!
  134. * Blend the two color components in the proportion alpha:
  135. *
  136. * result = alpha a + ( 1 - alpha b)
  137. *
  138. * \param a first component.
  139. * \param b second component.
  140. * \param alpha blend proportion.
  141. * \return blended color components.
  142. */
  143. inline int INT_BLEND ( int a, int b, int alpha )
  144. {
  145. return INT_MULT( a - b, alpha ) + b;
  146. }
  147. // From GIMP "gimpcolorspace.c" v1.2
  148. /*!
  149. * Convert a color in RGB space to HSV space (Hue, Saturation, Value).
  150. * \param red the red component (modified in place).
  151. * \param green the green component (modified in place).
  152. * \param blue the blue component (modified in place).
  153. */
  154. void RGBTOHSV ( uchar& red, uchar& green, uchar& blue )
  155. {
  156. int r, g, b;
  157. double h, s, v;
  158. int min, max;
  159. h = 0.;
  160. r = red;
  161. g = green;
  162. b = blue;
  163. if ( r > g ) {
  164. max = KMAX( r, b );
  165. min = KMIN( g, b );
  166. }
  167. else {
  168. max = KMAX( g, b );
  169. min = KMIN( r, b );
  170. }
  171. v = max;
  172. if ( max != 0 )
  173. s = ( ( max - min ) * 255 ) / (double)max;
  174. else
  175. s = 0;
  176. if ( s == 0 )
  177. h = 0;
  178. else {
  179. int delta = max - min;
  180. if ( r == max )
  181. h = ( g - b ) / (double)delta;
  182. else if ( g == max )
  183. h = 2 + ( b - r ) / (double)delta;
  184. else if ( b == max )
  185. h = 4 + ( r - g ) / (double)delta;
  186. h *= 42.5;
  187. if ( h < 0 )
  188. h += 255;
  189. if ( h > 255 )
  190. h -= 255;
  191. }
  192. red = (uchar)h;
  193. green = (uchar)s;
  194. blue = (uchar)v;
  195. }
  196. /*!
  197. * Convert a color in HSV space to RGB space.
  198. * \param hue the hue component (modified in place).
  199. * \param saturation the saturation component (modified in place).
  200. * \param value the value component (modified in place).
  201. */
  202. void HSVTORGB ( uchar& hue, uchar& saturation, uchar& value )
  203. {
  204. if ( saturation == 0 ) {
  205. hue = value;
  206. saturation = value;
  207. value = value;
  208. }
  209. else {
  210. double h = hue * 6. / 255.;
  211. double s = saturation / 255.;
  212. double v = value / 255.;
  213. double f = h - (int)h;
  214. double p = v * ( 1. - s );
  215. double q = v * ( 1. - ( s * f ) );
  216. double t = v * ( 1. - ( s * ( 1. - f ) ) );
  217. // Worth a note here that gcc 2.96 will generate different results
  218. // depending on optimization mode on i386.
  219. switch ((int)h) {
  220. case 0:
  221. hue = (uchar)( v * 255 );
  222. saturation = (uchar)( t * 255 );
  223. value = (uchar)( p * 255 );
  224. break;
  225. case 1:
  226. hue = (uchar)( q * 255 );
  227. saturation = (uchar)( v * 255 );
  228. value = (uchar)( p * 255 );
  229. break;
  230. case 2:
  231. hue = (uchar)( p * 255 );
  232. saturation = (uchar)( v * 255 );
  233. value = (uchar)( t * 255 );
  234. break;
  235. case 3:
  236. hue = (uchar)( p * 255 );
  237. saturation = (uchar)( q * 255 );
  238. value = (uchar)( v * 255 );
  239. break;
  240. case 4:
  241. hue = (uchar)( t * 255 );
  242. saturation = (uchar)( p * 255 );
  243. value = (uchar)( v * 255 );
  244. break;
  245. case 5:
  246. hue = (uchar)( v * 255 );
  247. saturation = (uchar)( p * 255 );
  248. value = (uchar)( q * 255 );
  249. }
  250. }
  251. }
  252. /*!
  253. * Convert a color in RGB space to HLS space (Hue, Lightness, Saturation).
  254. * \param red the red component (modified in place).
  255. * \param green the green component (modified in place).
  256. * \param blue the blue component (modified in place).
  257. */
  258. void RGBTOHLS ( uchar& red, uchar& green, uchar& blue )
  259. {
  260. int r = red;
  261. int g = green;
  262. int b = blue;
  263. int min, max;
  264. if ( r > g ) {
  265. max = KMAX( r, b );
  266. min = KMIN( g, b );
  267. }
  268. else {
  269. max = KMAX( g, b );
  270. min = KMIN( r, b );
  271. }
  272. double h;
  273. double l = ( max + min ) / 2.;
  274. double s;
  275. if ( max == min ) {
  276. s = 0.;
  277. h = 0.;
  278. }
  279. else {
  280. int delta = max - min;
  281. if ( l < 128 )
  282. s = 255 * (double)delta / (double)( max + min );
  283. else
  284. s = 255 * (double)delta / (double)( 511 - max - min );
  285. if ( r == max )
  286. h = ( g - b ) / (double)delta;
  287. else if ( g == max )
  288. h = 2 + ( b - r ) / (double)delta;
  289. else
  290. h = 4 + ( r - g ) / (double)delta;
  291. h *= 42.5;
  292. if ( h < 0 )
  293. h += 255;
  294. else if ( h > 255 )
  295. h -= 255;
  296. }
  297. red = (uchar)h;
  298. green = (uchar)l;
  299. blue = (uchar)s;
  300. }
  301. /*!
  302. * Implement the HLS "double hex-cone".
  303. * \param n1 lightness fraction (?)
  304. * \param n2 saturation fraction (?)
  305. * \param hue hue "angle".
  306. * \return HLS value.
  307. */
  308. int HLSVALUE ( double n1, double n2, double hue )
  309. {
  310. double value;
  311. if ( hue > 255 )
  312. hue -= 255;
  313. else if ( hue < 0 )
  314. hue += 255;
  315. if ( hue < 42.5 )
  316. value = n1 + ( n2 - n1 ) * ( hue / 42.5 );
  317. else if ( hue < 127.5 )
  318. value = n2;
  319. else if ( hue < 170 )
  320. value = n1 + ( n2 - n1 ) * ( ( 170 - hue ) / 42.5 );
  321. else
  322. value = n1;
  323. return (int)( value * 255 );
  324. }
  325. /*!
  326. * Convert a color in HLS space to RGB space.
  327. * \param hue the hue component (modified in place).
  328. * \param lightness the lightness component (modified in place).
  329. * \param saturation the saturation component (modified in place).
  330. */
  331. void HLSTORGB ( uchar& hue, uchar& lightness, uchar& saturation )
  332. {
  333. double h = hue;
  334. double l = lightness;
  335. double s = saturation;
  336. if ( s == 0 ) {
  337. hue = (uchar)l;
  338. lightness = (uchar)l;
  339. saturation = (uchar)l;
  340. }
  341. else {
  342. double m1, m2;
  343. if ( l < 128 )
  344. m2 = ( l * ( 255 + s ) ) / 65025.;
  345. else
  346. m2 = ( l + s - ( l * s ) / 255. ) / 255.;
  347. m1 = ( l / 127.5 ) - m2;
  348. hue = HLSVALUE( m1, m2, h + 85 );
  349. lightness = HLSVALUE( m1, m2, h );
  350. saturation = HLSVALUE( m1, m2, h - 85 );
  351. }
  352. }
  353. #endif