2
0

TIFFcolor.3tiff 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. .\" $Id: TIFFcolor.3tiff,v 1.4 2009-11-30 12:22:26 fwarmerdam Exp $
  2. .\"
  3. .\" Copyright (c) 2003, Andrey Kiselev <dron@ak4719.spb.edu>
  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. .if n .po 0
  25. .TH COLOR 3TIFF "December 21, 2003" "libtiff"
  26. .SH NAME
  27. TIFFYCbCrToRGBInit, TIFFYCbCrtoRGB, TIFFCIELabToRGBInit, TIFFCIELabToXYZ,
  28. TIFFXYZToRGB \- color conversion routines.
  29. .SH SYNOPSIS
  30. .B "#include <tiffio.h>"
  31. .sp
  32. .BI "int TIFFYCbCrToRGBInit(TIFFYCbCrToRGB *" ycbcr ", float *" luma ", float *"refBlackWhite" );"
  33. .br
  34. .BI "void TIFFYCbCrtoRGB(TIFFYCbCrToRGB *" ycbcr ", uint32 " Y ", int32 " Cb ", int32 " Cr ", uint32 *" R ", uint32 *" G ", uint32 *" B " );"
  35. .sp
  36. .BI "int TIFFCIELabToRGBInit(TIFFCIELabToRGB *" cielab ", const TIFFDisplay *" display ", float *" refWhite ");"
  37. .br
  38. .BI "void TIFFCIELabToXYZ(TIFFCIELabToRGB *" cielab ", uint32 " L ", int32 " a ", int32 " b ", float *" X ", float *" Y ", float *" Z ");"
  39. .br
  40. .BI "void TIFFXYZToRGB(TIFFCIELabToRGB *" cielab ", float " X ", float " Y ", float " Z" , uint32 *" R ", uint32 *" G ", uint32 *" B ");"
  41. .SH DESCRIPTION
  42. TIFF supports several color spaces for images stored in that format. There is
  43. usually a problem of application to handle the data properly and convert
  44. between different colorspaces for displaying and printing purposes. To
  45. simplify this task libtiff implements several color conversion routines
  46. itself. In particular, these routines used in
  47. .B TIFFRGBAImage(3TIFF)
  48. interface.
  49. .PP
  50. .B TIFFYCbCrToRGBInit()
  51. used to initialize
  52. .I YCbCr
  53. to
  54. .I RGB
  55. conversion state. Allocating and freeing of the
  56. .I ycbcr
  57. structure belongs to programmer.
  58. .I TIFFYCbCrToRGB
  59. defined in
  60. .B tiffio.h
  61. as
  62. .PP
  63. .RS
  64. .nf
  65. typedef struct { /* YCbCr->RGB support */
  66. TIFFRGBValue* clamptab; /* range clamping table */
  67. int* Cr_r_tab;
  68. int* Cb_b_tab;
  69. int32* Cr_g_tab;
  70. int32* Cb_g_tab;
  71. int32* Y_tab;
  72. } TIFFYCbCrToRGB;
  73. .fi
  74. .RE
  75. .PP
  76. .I luma
  77. is a float array of three values representing proportions of the red, green
  78. and blue in luminance, Y (see section 21 of the TIFF 6.0 specification, where
  79. the YCbCr images discussed).
  80. .I TIFFTAG_YCBCRCOEFFICIENTS
  81. holds that values in TIFF file.
  82. .I refBlackWhite
  83. is a float array of 6 values which specifies a pair of headroom and footroom
  84. image data values (codes) for each image component (see section 20 of the
  85. TIFF 6.0 specification where the colorinmetry fields discussed).
  86. .I TIFFTAG_REFERENCEBLACKWHITE
  87. is responsible for storing these values in TIFF file. Following code snippet
  88. should helps to understand the the technique:
  89. .PP
  90. .RS
  91. .nf
  92. float *luma, *refBlackWhite;
  93. uint16 hs, vs;
  94. /* Initialize structures */
  95. ycbcr = (TIFFYCbCrToRGB*)
  96. _TIFFmalloc(TIFFroundup(sizeof(TIFFYCbCrToRGB), sizeof(long))
  97. + 4*256*sizeof(TIFFRGBValue)
  98. + 2*256*sizeof(int)
  99. + 3*256*sizeof(int32));
  100. if (ycbcr == NULL) {
  101. TIFFError("YCbCr->RGB",
  102. "No space for YCbCr->RGB conversion state");
  103. exit(0);
  104. }
  105. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
  106. TIFFGetFieldDefaulted(tif, TIFFTAG_REFERENCEBLACKWHITE, &refBlackWhite);
  107. if (TIFFYCbCrToRGBInit(ycbcr, luma, refBlackWhite) < 0)
  108. exit(0);
  109. /* Start conversion */
  110. uint32 r, g, b;
  111. uint32 Y;
  112. int32 Cb, Cr;
  113. for each pixel in image
  114. TIFFYCbCrtoRGB(img->ycbcr, Y, Cb, Cr, &r, &g, &b);
  115. /* Free state structure */
  116. _TIFFfree(ycbcr);
  117. .fi
  118. .RE
  119. .PP
  120. .PP
  121. .B TIFFCIELabToRGBInit()
  122. initializes the
  123. .I CIE L*a*b* 1976
  124. to
  125. .I RGB
  126. conversion state.
  127. .B TIFFCIELabToRGB
  128. defined as
  129. .PP
  130. .RS
  131. .nf
  132. #define CIELABTORGB_TABLE_RANGE 1500
  133. typedef struct { /* CIE Lab 1976->RGB support */
  134. int range; /* Size of conversion table */
  135. float rstep, gstep, bstep;
  136. float X0, Y0, Z0; /* Reference white point */
  137. TIFFDisplay display;
  138. float Yr2r[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yr to r */
  139. float Yg2g[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yg to g */
  140. float Yb2b[CIELABTORGB_TABLE_RANGE + 1]; /* Conversion of Yb to b */
  141. } TIFFCIELabToRGB;
  142. .fi
  143. .RE
  144. .PP
  145. .I display
  146. is a display device description, declared as
  147. .PP
  148. .RS
  149. .nf
  150. typedef struct {
  151. float d_mat[3][3]; /* XYZ -> luminance matrix */
  152. float d_YCR; /* Light o/p for reference white */
  153. float d_YCG;
  154. float d_YCB;
  155. uint32 d_Vrwr; /* Pixel values for ref. white */
  156. uint32 d_Vrwg;
  157. uint32 d_Vrwb;
  158. float d_Y0R; /* Residual light for black pixel */
  159. float d_Y0G;
  160. float d_Y0B;
  161. float d_gammaR; /* Gamma values for the three guns */
  162. float d_gammaG;
  163. float d_gammaB;
  164. } TIFFDisplay;
  165. .fi
  166. .RE
  167. .PP
  168. For example, the one can use sRGB device, which has the following parameters:
  169. .PP
  170. .RS
  171. .nf
  172. TIFFDisplay display_sRGB = {
  173. { /* XYZ -> luminance matrix */
  174. { 3.2410F, -1.5374F, -0.4986F },
  175. { -0.9692F, 1.8760F, 0.0416F },
  176. { 0.0556F, -0.2040F, 1.0570F }
  177. },
  178. 100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
  179. 255, 255, 255, /* Pixel values for ref. white */
  180. 1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
  181. 2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
  182. };
  183. .fi
  184. .RE
  185. .PP
  186. .I refWhite
  187. is a color temperature of the reference white. The
  188. .I TIFFTAG_WHITEPOINT
  189. contains the chromaticity of the white point of the image from where the
  190. reference white can be calculated using following formulae:
  191. .PP
  192. .RS
  193. refWhite_Y = 100.0
  194. .br
  195. refWhite_X = whitePoint_x / whitePoint_y * refWhite_Y
  196. .br
  197. refWhite_Z = (1.0 - whitePoint_x - whitePoint_y) / whitePoint_y * refWhite_X
  198. .br
  199. .RE
  200. .PP
  201. The conversion itself performed in two steps: at the first one we will convert
  202. .I CIE L*a*b* 1976
  203. to
  204. .I CIE XYZ
  205. using
  206. .B TIFFCIELabToXYZ()
  207. routine, and at the second step we will convert
  208. .I CIE XYZ
  209. to
  210. .I RGB
  211. using
  212. .B TIFFXYZToRGB().
  213. Look at the code sample below:
  214. .PP
  215. .RS
  216. .nf
  217. float *whitePoint;
  218. float refWhite[3];
  219. /* Initialize structures */
  220. img->cielab = (TIFFCIELabToRGB *)
  221. _TIFFmalloc(sizeof(TIFFCIELabToRGB));
  222. if (!cielab) {
  223. TIFFError("CIE L*a*b*->RGB",
  224. "No space for CIE L*a*b*->RGB conversion state.");
  225. exit(0);
  226. }
  227. TIFFGetFieldDefaulted(tif, TIFFTAG_WHITEPOINT, &whitePoint);
  228. refWhite[1] = 100.0F;
  229. refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
  230. refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
  231. / whitePoint[1] * refWhite[1];
  232. if (TIFFCIELabToRGBInit(cielab, &display_sRGB, refWhite) < 0) {
  233. TIFFError("CIE L*a*b*->RGB",
  234. "Failed to initialize CIE L*a*b*->RGB conversion state.");
  235. _TIFFfree(cielab);
  236. exit(0);
  237. }
  238. /* Now we can start to convert */
  239. uint32 r, g, b;
  240. uint32 L;
  241. int32 a, b;
  242. float X, Y, Z;
  243. for each pixel in image
  244. TIFFCIELabToXYZ(cielab, L, a, b, &X, &Y, &Z);
  245. TIFFXYZToRGB(cielab, X, Y, Z, &r, &g, &b);
  246. /* Don't forget to free the state structure */
  247. _TIFFfree(cielab);
  248. .fi
  249. .RE
  250. .PP
  251. .SH "SEE ALSO"
  252. .BR TIFFRGBAImage (3TIFF)
  253. .BR libtiff (3TIFF),
  254. .PP
  255. Libtiff library home page:
  256. .BR http://www.remotesensing.org/libtiff/