iltruegray.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: iltruegray.c /main/3 1995/10/23 16:02:03 rswiston $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1991 Hewlett-Packard Co.
  27. ***
  28. *** RESTRICTED RIGHTS LEGEND
  29. *** Use, duplication, or disclosure by the U.S. Government is subject to
  30. *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
  31. *** Technical Data and Computer Software clause in DFARS 252.227-7013.
  32. *** Hewlett-Packard Company
  33. *** 3000 Hanover Street
  34. *** Palo Alto, CA 94304 U.S.A.
  35. *** Rights for non-DOD U.S. Government Departments and Agencies are as set
  36. *** forth in FAR 52.227-19(c)(1,2).
  37. ***
  38. ***-------------------------------------------------------------------*/
  39. /* ======================================================================================
  40. /ilc/iltruegray.c : Image Library conversion routines fron RGB to grayscale
  41. =================================================================================== */
  42. #include "ilint.h"
  43. #include "ilpipelem.h"
  44. #include "ilconvert.h"
  45. typedef struct {
  46. unsigned long R[256], G[256], B[256]; /* RGB->gray multiply lookup table */
  47. long nPixels; /* width of src/dst images */
  48. long srcRowBytes; /* bytes/row of src image */
  49. ilPtr pSrcPixels; /* ptr to start of src pixels */
  50. long dstRowBytes; /* bytes/row of dst image */
  51. ilPtr pDstPixels; /* ptr to start of dst pixels */
  52. } ilRGBToGrayRec, *ilRGBToGrayRecPtr;
  53. /* =============================================================================================================================
  54. ilInitRGBToGray - Init() function.
  55. NOTE: also used for Gray to RGB conversion below !
  56. ============================================================================================================================= */
  57. static ilError ilInitRGBGrayConversions (
  58. ilRGBToGrayRecPtr pPriv,
  59. ilImageInfo *pSrcImage,
  60. ilImageInfo *pDstImage
  61. )
  62. {
  63. pPriv->nPixels = pSrcImage->width;
  64. pPriv->srcRowBytes = pSrcImage->plane[0].nBytesPerRow;
  65. pPriv->pSrcPixels = pSrcImage->plane[0].pPixels;
  66. pPriv->dstRowBytes = pDstImage->plane[0].nBytesPerRow;
  67. pPriv->pDstPixels = pDstImage->plane[0].pPixels;
  68. return IL_OK;
  69. }
  70. /* =============================================================================================================================
  71. ilAddElementRGBGrayConversions - AddElement() function
  72. Called only for RGB->gray conversion: init RGB lookup/multiply tables in pPriv
  73. ============================================================================================================================= */
  74. static ilError ilAddElementRGBGrayConversions (
  75. ilRGBToGrayRecPtr pPriv,
  76. unsigned short *pPalette, /* not used */
  77. void *pOptionData /* not used */
  78. )
  79. {
  80. int i;
  81. /* Init tables, index by R/G/B, which effectively multiply each component
  82. (0..255) by the NTSC conversion formula: 0.30 * R + 0.587 * G + 0.114 * B,
  83. except that each value is * 65536 for extra accuracy: the result is
  84. shifted right by 16 before storing.
  85. Note that "19660" would probably be more accurate at "19661", but the
  86. old code used 19660 and for that little difference it is silly to
  87. invalidate the old checksum tests. Note also that if R/G/B are all = 255
  88. the result is still <= 255 after the shift right.
  89. */
  90. for (i = 0; i < 256; i++) {
  91. pPriv->R[i] = 19660 * i;
  92. pPriv->G[i] = 38469 * i;
  93. pPriv->B[i] = 7471 * i;
  94. }
  95. return IL_OK;
  96. }
  97. /* =============================================================================================================================
  98. ilExecuteRGBToGray - Execute() function: convert the given # of src lines.
  99. Converts RGB images to a grayscale with format byte per pixel.
  100. Input image: uncompressed IL_DES_RGB, IL_FORMAT_3BYTE_PIXEL
  101. Output image: uncompressed IL_GRAY, 255 levels, IL_FORMAT_BYTE.
  102. ============================================================================================================================= */
  103. static ilError ilExecuteRGBToGray (
  104. ilExecuteData *pData,
  105. long dstLine,
  106. long *pNLines
  107. )
  108. {
  109. ilRGBToGrayRecPtr pPriv;
  110. long srcnbytes, dstnbytes;
  111. ilPtr psrcline, pdstline;
  112. ilPtr psrc, pdst;
  113. long nLines, nPixels;
  114. INT32 i;
  115. pPriv = (ilRGBToGrayRecPtr)pData->pPrivate;
  116. srcnbytes = pPriv->srcRowBytes;
  117. psrcline = pPriv->pSrcPixels + pData->srcLine * srcnbytes;
  118. dstnbytes = pPriv->dstRowBytes;
  119. pdstline = pPriv->pDstPixels + dstLine * dstnbytes;
  120. if (pPriv->nPixels < 0) return 0;
  121. nLines = *pNLines;
  122. if (nLines <= 0) return 0;
  123. while ( nLines-- > 0 ) {
  124. psrc = psrcline;
  125. pdst = pdstline;
  126. nPixels = pPriv->nPixels;
  127. /* -- grayscale from: Y = 0.30red + 0.587green + 0.114blue */
  128. while ( nPixels-- > 0 ) {
  129. i = pPriv->R[*psrc++];
  130. i += pPriv->G[*psrc++];
  131. i += pPriv->B[*psrc++];
  132. *pdst++ = i >> 16;
  133. }
  134. psrcline += srcnbytes;
  135. pdstline += dstnbytes;
  136. }
  137. return IL_OK;
  138. }
  139. /* =============================================================================================================================
  140. ilRGBToGray - Table exported to ilConvert(), declared in /ilc/ilconvert.h
  141. ============================================================================================================================= */
  142. IL_PRIVATE ilConvertRec _ilRGBToGray = {
  143. IL_NPF, /* CheckFormat() */
  144. IL_STD_FORMAT_3BYTE_PIXEL, /* srcFormatCode */
  145. ilAddElementRGBGrayConversions, /* AddElement() */
  146. IL_DES_GRAY, /* pDstDes */
  147. IL_FORMAT_BYTE, /* pDstFormat */
  148. sizeof (ilRGBToGrayRec), /* nBytesPrivate */
  149. ilInitRGBGrayConversions, /* Init() */
  150. IL_NPF, /* Cleanup() */
  151. IL_NPF, /* Destroy() */
  152. ilExecuteRGBToGray /* Execute() */
  153. };
  154. /* =====================================================================================
  155. GRAY TO RGB
  156. Convert byte/pixel gray to 3byte/pixel rgb.
  157. ==================================================================================== */
  158. static ilError ilExecuteGrayToRGB (
  159. ilExecuteData *pData,
  160. long dstLine,
  161. long *pNLines
  162. )
  163. {
  164. ilRGBToGrayRecPtr pPriv;
  165. long nLinesM1, nPixelsM1;
  166. long srcRowBytes, dstRowBytes, nPixelsM1Init;
  167. ilPtr pSrcLine, pDstLine;
  168. ilPtr pSrc, pDst;
  169. ilByte byte;
  170. pPriv = (ilRGBToGrayRecPtr)pData->pPrivate;
  171. nPixelsM1Init = pPriv->nPixels - 1;
  172. nLinesM1 = *pNLines - 1;
  173. if ((nLinesM1 < 0) || (nPixelsM1Init < 0))
  174. return IL_OK;
  175. srcRowBytes = pPriv->srcRowBytes;
  176. pSrcLine = pPriv->pSrcPixels + pData->srcLine * srcRowBytes;
  177. dstRowBytes = pPriv->dstRowBytes;
  178. pDstLine = pPriv->pDstPixels + dstLine * dstRowBytes;
  179. /* Simply replicate each gray byte 3 times to form RGB - tough, eh?
  180. However if black not zero must invert the gray byte.
  181. */
  182. if (pData->pSrcImage->pDes->blackIsZero) {
  183. do {
  184. pSrc = pSrcLine;
  185. pSrcLine += srcRowBytes;
  186. pDst = pDstLine;
  187. pDstLine += dstRowBytes;
  188. nPixelsM1 = nPixelsM1Init;
  189. do {
  190. byte = *pSrc++;
  191. *pDst++ = byte;
  192. *pDst++ = byte;
  193. *pDst++ = byte;
  194. } while (--nPixelsM1 >= 0);
  195. } while (--nLinesM1 >= 0);
  196. }
  197. else {
  198. do {
  199. pSrc = pSrcLine;
  200. pSrcLine += srcRowBytes;
  201. pDst = pDstLine;
  202. pDstLine += dstRowBytes;
  203. nPixelsM1 = nPixelsM1Init;
  204. do {
  205. byte = ~*pSrc++;
  206. *pDst++ = byte;
  207. *pDst++ = byte;
  208. *pDst++ = byte;
  209. } while (--nPixelsM1 >= 0);
  210. } while (--nLinesM1 >= 0);
  211. }
  212. return IL_OK;
  213. }
  214. /* =====================================================================================
  215. ilGrayToRGB - Table exported to ilConvert(), declared in /ilc/ilconvert.h
  216. ==================================================================================== */
  217. IL_PRIVATE ilConvertRec _ilGrayToRGB = {
  218. IL_NPF, /* CheckFormat() */
  219. IL_STD_FORMAT_BYTE, /* srcFormatCode */
  220. IL_NPF, /* AddElement() */
  221. IL_DES_RGB, /* pDstDes */
  222. IL_FORMAT_3BYTE_PIXEL, /* pDstFormat */
  223. sizeof (ilRGBToGrayRec), /* nBytesPrivate */
  224. ilInitRGBGrayConversions, /* Init() */
  225. IL_NPF, /* Cleanup() */
  226. IL_NPF, /* Destroy() */
  227. ilExecuteGrayToRGB /* Execute() */
  228. };