tif_color.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /*
  25. * CIE L*a*b* to CIE XYZ and CIE XYZ to RGB conversion routines are taken
  26. * from the VIPS library (http://www.vips.ecs.soton.ac.uk) with
  27. * the permission of John Cupitt, the VIPS author.
  28. */
  29. /*
  30. * TIFF Library.
  31. *
  32. * Color space conversion routines.
  33. */
  34. #include "tiffiop.h"
  35. #include <math.h>
  36. /*
  37. * Convert color value from the CIE L*a*b* 1976 space to CIE XYZ.
  38. */
  39. void
  40. TIFFCIELabToXYZ(TIFFCIELabToRGB *cielab, uint32 l, int32 a, int32 b,
  41. float *X, float *Y, float *Z)
  42. {
  43. float L = (float)l * 100.0F / 255.0F;
  44. float cby, tmp;
  45. if( L < 8.856F ) {
  46. *Y = (L * cielab->Y0) / 903.292F;
  47. cby = 7.787F * (*Y / cielab->Y0) + 16.0F / 116.0F;
  48. } else {
  49. cby = (L + 16.0F) / 116.0F;
  50. *Y = cielab->Y0 * cby * cby * cby;
  51. }
  52. tmp = (float)a / 500.0F + cby;
  53. if( tmp < 0.2069F )
  54. *X = cielab->X0 * (tmp - 0.13793F) / 7.787F;
  55. else
  56. *X = cielab->X0 * tmp * tmp * tmp;
  57. tmp = cby - (float)b / 200.0F;
  58. if( tmp < 0.2069F )
  59. *Z = cielab->Z0 * (tmp - 0.13793F) / 7.787F;
  60. else
  61. *Z = cielab->Z0 * tmp * tmp * tmp;
  62. }
  63. #define RINT(R) ((uint32)((R)>0?((R)+0.5):((R)-0.5)))
  64. /*
  65. * Convert color value from the XYZ space to RGB.
  66. */
  67. void
  68. TIFFXYZToRGB(TIFFCIELabToRGB *cielab, float X, float Y, float Z,
  69. uint32 *r, uint32 *g, uint32 *b)
  70. {
  71. int i;
  72. float Yr, Yg, Yb;
  73. float *matrix = &cielab->display.d_mat[0][0];
  74. /* Multiply through the matrix to get luminosity values. */
  75. Yr = matrix[0] * X + matrix[1] * Y + matrix[2] * Z;
  76. Yg = matrix[3] * X + matrix[4] * Y + matrix[5] * Z;
  77. Yb = matrix[6] * X + matrix[7] * Y + matrix[8] * Z;
  78. /* Clip input */
  79. Yr = TIFFmax(Yr, cielab->display.d_Y0R);
  80. Yg = TIFFmax(Yg, cielab->display.d_Y0G);
  81. Yb = TIFFmax(Yb, cielab->display.d_Y0B);
  82. /* Avoid overflow in case of wrong input values */
  83. Yr = TIFFmin(Yr, cielab->display.d_YCR);
  84. Yg = TIFFmin(Yg, cielab->display.d_YCG);
  85. Yb = TIFFmin(Yb, cielab->display.d_YCB);
  86. /* Turn luminosity to colour value. */
  87. i = (int)((Yr - cielab->display.d_Y0R) / cielab->rstep);
  88. i = TIFFmin(cielab->range, i);
  89. *r = RINT(cielab->Yr2r[i]);
  90. i = (int)((Yg - cielab->display.d_Y0G) / cielab->gstep);
  91. i = TIFFmin(cielab->range, i);
  92. *g = RINT(cielab->Yg2g[i]);
  93. i = (int)((Yb - cielab->display.d_Y0B) / cielab->bstep);
  94. i = TIFFmin(cielab->range, i);
  95. *b = RINT(cielab->Yb2b[i]);
  96. /* Clip output. */
  97. *r = TIFFmin(*r, cielab->display.d_Vrwr);
  98. *g = TIFFmin(*g, cielab->display.d_Vrwg);
  99. *b = TIFFmin(*b, cielab->display.d_Vrwb);
  100. }
  101. #undef RINT
  102. /*
  103. * Allocate conversion state structures and make look_up tables for
  104. * the Yr,Yb,Yg <=> r,g,b conversions.
  105. */
  106. int
  107. TIFFCIELabToRGBInit(TIFFCIELabToRGB* cielab,
  108. const TIFFDisplay *display, float *refWhite)
  109. {
  110. int i;
  111. double dfGamma;
  112. cielab->range = CIELABTORGB_TABLE_RANGE;
  113. _TIFFmemcpy(&cielab->display, display, sizeof(TIFFDisplay));
  114. /* Red */
  115. dfGamma = 1.0 / cielab->display.d_gammaR ;
  116. cielab->rstep =
  117. (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range;
  118. for(i = 0; i <= cielab->range; i++) {
  119. cielab->Yr2r[i] = cielab->display.d_Vrwr
  120. * ((float)pow((double)i / cielab->range, dfGamma));
  121. }
  122. /* Green */
  123. dfGamma = 1.0 / cielab->display.d_gammaG ;
  124. cielab->gstep =
  125. (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range;
  126. for(i = 0; i <= cielab->range; i++) {
  127. cielab->Yg2g[i] = cielab->display.d_Vrwg
  128. * ((float)pow((double)i / cielab->range, dfGamma));
  129. }
  130. /* Blue */
  131. dfGamma = 1.0 / cielab->display.d_gammaB ;
  132. cielab->bstep =
  133. (cielab->display.d_YCR - cielab->display.d_Y0R) / cielab->range;
  134. for(i = 0; i <= cielab->range; i++) {
  135. cielab->Yb2b[i] = cielab->display.d_Vrwb
  136. * ((float)pow((double)i / cielab->range, dfGamma));
  137. }
  138. /* Init reference white point */
  139. cielab->X0 = refWhite[0];
  140. cielab->Y0 = refWhite[1];
  141. cielab->Z0 = refWhite[2];
  142. return 0;
  143. }
  144. /*
  145. * Convert color value from the YCbCr space to RGB.
  146. * The colorspace conversion algorithm comes from the IJG v5a code;
  147. * see below for more information on how it works.
  148. */
  149. #define SHIFT 16
  150. #define FIX(x) ((int32)((x) * (1L<<SHIFT) + 0.5))
  151. #define ONE_HALF ((int32)(1<<(SHIFT-1)))
  152. #define Code2V(c, RB, RW, CR) ((((c)-(int32)(RB))*(float)(CR))/(float)(((RW)-(RB)!=0) ? ((RW)-(RB)) : 1))
  153. #define CLAMP(f,min,max) ((f)<(min)?(min):(f)>(max)?(max):(f))
  154. #define HICLAMP(f,max) ((f)>(max)?(max):(f))
  155. void
  156. TIFFYCbCrtoRGB(TIFFYCbCrToRGB *ycbcr, uint32 Y, int32 Cb, int32 Cr,
  157. uint32 *r, uint32 *g, uint32 *b)
  158. {
  159. int32 i;
  160. /* XXX: Only 8-bit YCbCr input supported for now */
  161. Y = HICLAMP(Y, 255);
  162. Cb = CLAMP(Cb, 0, 255);
  163. Cr = CLAMP(Cr, 0, 255);
  164. i = ycbcr->Y_tab[Y] + ycbcr->Cr_r_tab[Cr];
  165. *r = CLAMP(i, 0, 255);
  166. i = ycbcr->Y_tab[Y]
  167. + (int)((ycbcr->Cb_g_tab[Cb] + ycbcr->Cr_g_tab[Cr]) >> SHIFT);
  168. *g = CLAMP(i, 0, 255);
  169. i = ycbcr->Y_tab[Y] + ycbcr->Cb_b_tab[Cb];
  170. *b = CLAMP(i, 0, 255);
  171. }
  172. /* Clamp function for sanitization purposes. Normally clamping should not */
  173. /* occur for well behaved chroma and refBlackWhite coefficients */
  174. static float CLAMPw(float v, float vmin, float vmax)
  175. {
  176. if( v < vmin )
  177. {
  178. /* printf("%f clamped to %f\n", v, vmin); */
  179. return vmin;
  180. }
  181. if( v > vmax )
  182. {
  183. /* printf("%f clamped to %f\n", v, vmax); */
  184. return vmax;
  185. }
  186. return v;
  187. }
  188. /*
  189. * Initialize the YCbCr->RGB conversion tables. The conversion
  190. * is done according to the 6.0 spec:
  191. *
  192. * R = Y + Cr*(2 - 2*LumaRed)
  193. * B = Y + Cb*(2 - 2*LumaBlue)
  194. * G = Y
  195. * - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen
  196. * - LumaRed*Cr*(2-2*LumaRed)/LumaGreen
  197. *
  198. * To avoid floating point arithmetic the fractional constants that
  199. * come out of the equations are represented as fixed point values
  200. * in the range 0...2^16. We also eliminate multiplications by
  201. * pre-calculating possible values indexed by Cb and Cr (this code
  202. * assumes conversion is being done for 8-bit samples).
  203. */
  204. int
  205. TIFFYCbCrToRGBInit(TIFFYCbCrToRGB* ycbcr, float *luma, float *refBlackWhite)
  206. {
  207. TIFFRGBValue* clamptab;
  208. int i;
  209. #define LumaRed luma[0]
  210. #define LumaGreen luma[1]
  211. #define LumaBlue luma[2]
  212. clamptab = (TIFFRGBValue*)(
  213. (uint8*) ycbcr+TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long)));
  214. _TIFFmemset(clamptab, 0, 256); /* v < 0 => 0 */
  215. ycbcr->clamptab = (clamptab += 256);
  216. for (i = 0; i < 256; i++)
  217. clamptab[i] = (TIFFRGBValue) i;
  218. _TIFFmemset(clamptab+256, 255, 2*256); /* v > 255 => 255 */
  219. ycbcr->Cr_r_tab = (int*) (clamptab + 3*256);
  220. ycbcr->Cb_b_tab = ycbcr->Cr_r_tab + 256;
  221. ycbcr->Cr_g_tab = (int32*) (ycbcr->Cb_b_tab + 256);
  222. ycbcr->Cb_g_tab = ycbcr->Cr_g_tab + 256;
  223. ycbcr->Y_tab = ycbcr->Cb_g_tab + 256;
  224. { float f1 = 2-2*LumaRed; int32 D1 = FIX(CLAMP(f1,0.0F,2.0F));
  225. float f2 = LumaRed*f1/LumaGreen; int32 D2 = -FIX(CLAMP(f2,0.0F,2.0F));
  226. float f3 = 2-2*LumaBlue; int32 D3 = FIX(CLAMP(f3,0.0F,2.0F));
  227. float f4 = LumaBlue*f3/LumaGreen; int32 D4 = -FIX(CLAMP(f4,0.0F,2.0F));
  228. int x;
  229. #undef LumaBlue
  230. #undef LumaGreen
  231. #undef LumaRed
  232. /*
  233. * i is the actual input pixel value in the range 0..255
  234. * Cb and Cr values are in the range -128..127 (actually
  235. * they are in a range defined by the ReferenceBlackWhite
  236. * tag) so there is some range shifting to do here when
  237. * constructing tables indexed by the raw pixel data.
  238. */
  239. for (i = 0, x = -128; i < 256; i++, x++) {
  240. int32 Cr = (int32)CLAMPw(Code2V(x, refBlackWhite[4] - 128.0F,
  241. refBlackWhite[5] - 128.0F, 127),
  242. -128.0F * 32, 128.0F * 32);
  243. int32 Cb = (int32)CLAMPw(Code2V(x, refBlackWhite[2] - 128.0F,
  244. refBlackWhite[3] - 128.0F, 127),
  245. -128.0F * 32, 128.0F * 32);
  246. ycbcr->Cr_r_tab[i] = (int32)((D1*Cr + ONE_HALF)>>SHIFT);
  247. ycbcr->Cb_b_tab[i] = (int32)((D3*Cb + ONE_HALF)>>SHIFT);
  248. ycbcr->Cr_g_tab[i] = D2*Cr;
  249. ycbcr->Cb_g_tab[i] = D4*Cb + ONE_HALF;
  250. ycbcr->Y_tab[i] =
  251. (int32)CLAMPw(Code2V(x + 128, refBlackWhite[0], refBlackWhite[1], 255),
  252. -128.0F * 32, 128.0F * 32);
  253. }
  254. }
  255. return 0;
  256. }
  257. #undef HICLAMP
  258. #undef CLAMP
  259. #undef Code2V
  260. #undef SHIFT
  261. #undef ONE_HALF
  262. #undef FIX
  263. /* vim: set ts=8 sts=8 sw=8 noet: */
  264. /*
  265. * Local Variables:
  266. * mode: c
  267. * c-basic-offset: 8
  268. * fill-column: 78
  269. * End:
  270. */