ilpalette.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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: ilpalette.c /main/3 1995/10/23 15:58:32 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. /* /ilc/ilpalette.c : Code for handling IL_PALETTE images.
  40. Mostly converters to other types (e.g. to RGB), referenced by
  41. /ilc/ilconvert.c
  42. */
  43. #include "ilint.h"
  44. #include "ilpipelem.h"
  45. #include "ilconvert.h"
  46. /* ----------------------- ilPaletteToRGBByte ------------------------- */
  47. /* Converts byte palette image to RGB, each a byte
  48. Input image: uncompressed IL_PALETTE, any # of levels, IL_FORMAT_BYTE.
  49. Output image: IL_DES_RGB, IL_FORMAT_3BYTE_PIXEL.
  50. */
  51. /* Private data, inited by Init() function.
  52. "palette" is inited when the element is added. It is a copy of the palette,
  53. as longwords: <8 unused> <8 blue> <8 green> <8 red> for each value 0..255
  54. for faster lookup of each palette pixel. Each 8 is the upper 8 bits of
  55. the palette entry.
  56. */
  57. typedef struct {
  58. long nPixelsM1; /* width - 1 of src/dst images */
  59. long srcRowBytes; /* bytes/row of src image */
  60. ilPtr pSrcPixels; /* ptr to start of src pixels */
  61. long dstRowBytes; /* bytes/row of dst image */
  62. ilPtr pDstPixels; /* ptr to start of dst pixels */
  63. unsigned short *pPalette; /* ptr to palette, set by AddElement() */
  64. unsigned long palette [256]; /* copy of palette: see above */
  65. ilBool firstStrip; /* true if first strip to Execute() */
  66. } ilPaletteToRGBPrivRec, *ilPaletteToRGBPrivPtr;
  67. /* AddElement() function, called by ilConvert() after the pipe element is added.
  68. Merely copy the given pPalette into private.
  69. */
  70. static ilError ilAddElementPaletteToRGBByte (
  71. ilPaletteToRGBPrivPtr pPriv,
  72. unsigned short *pPalette
  73. )
  74. {
  75. pPriv->pPalette = pPalette;
  76. return IL_OK;
  77. }
  78. /* Init() function: init values in private */
  79. static ilError ilInitPaletteToRGBByte (
  80. ilPaletteToRGBPrivPtr pPriv,
  81. ilImageInfo *pSrcImage,
  82. ilImageInfo *pDstImage
  83. )
  84. {
  85. pPriv->nPixelsM1 = pSrcImage->width - 1;
  86. pPriv->srcRowBytes = pSrcImage->plane[0].nBytesPerRow;
  87. pPriv->pSrcPixels = pSrcImage->plane[0].pPixels;
  88. pPriv->dstRowBytes = pDstImage->plane[0].nBytesPerRow;
  89. pPriv->pDstPixels = pDstImage->plane[0].pPixels;
  90. pPriv->firstStrip = TRUE;
  91. return IL_OK;
  92. }
  93. /* Called by Execute() to setup "palette" in private */
  94. static void ilInitPackedPalette (
  95. ilPaletteToRGBPrivPtr pPriv
  96. )
  97. {
  98. int i;
  99. unsigned long *pPacked, packed;
  100. unsigned short *pRed, *pGreen, *pBlue, temp;
  101. /* Pack the upper 8 bits of the rgb value, with blue in the high order bits. */
  102. pPacked = pPriv->palette;
  103. pRed = &pPriv->pPalette [0 * 256];
  104. pGreen = &pPriv->pPalette [1 * 256];
  105. pBlue = &pPriv->pPalette [2 * 256];
  106. for (i = 0; i < 256; i++) {
  107. temp = *pBlue++;
  108. packed = temp >> 8;
  109. packed <<= 8;
  110. temp = *pGreen++;
  111. packed |= temp >> 8;
  112. packed <<= 8;
  113. temp = *pRed++;
  114. packed |= temp >> 8;
  115. *pPacked++ = packed;
  116. }
  117. }
  118. /* Execute() function: convert the given # of src lines.
  119. Lookup each palette byte (0..255) and get a long which looks like:
  120. <8 unused> <8 blue> <8 green> <8 red>
  121. output the lower 8 bits, then shift to get RGB.
  122. */
  123. static ilError ilExecutePaletteToRGBByte (
  124. ilExecuteData *pData,
  125. long dstLine,
  126. long *pNLines /* ignored on input */
  127. )
  128. {
  129. ilPaletteToRGBPrivPtr pPriv;
  130. long srcRowBytes, dstRowBytes, nPixelsM1;
  131. ilPtr pSrcLine, pDstLine;
  132. ilPtr pSrc, pDst;
  133. unsigned long rgb, *pPackedPalette;
  134. long nLinesM1, nPixelsM1Temp;
  135. /* If firstStrip, init the packed palette in private */
  136. pPriv = (ilPaletteToRGBPrivPtr)pData->pPrivate;
  137. if (pPriv->firstStrip) {
  138. ilInitPackedPalette (pPriv);
  139. pPriv->firstStrip = FALSE;
  140. }
  141. /* Setup src,dst line ptrs and rowBytes, and nLines/PixelsM1 ("minus 1");
  142. exit if no lines or pixels.
  143. */
  144. srcRowBytes = pPriv->srcRowBytes;
  145. pSrcLine = pPriv->pSrcPixels + pData->srcLine * srcRowBytes;
  146. dstRowBytes = pPriv->dstRowBytes;
  147. pDstLine = pPriv->pDstPixels + dstLine * dstRowBytes;
  148. nPixelsM1 = pPriv->nPixelsM1;
  149. if (nPixelsM1 < 0)
  150. return 0;
  151. nLinesM1 = *pNLines;
  152. if (nLinesM1 <= 0)
  153. return 0;
  154. nLinesM1--;
  155. /* For each src byte: get a long from the cvt'd palette table, then take
  156. the low eight bits to get r,g,b, shifting down by 8 to get next component.
  157. */
  158. pPackedPalette = pPriv->palette;
  159. do {
  160. pSrc = pSrcLine;
  161. pSrcLine += srcRowBytes;
  162. pDst = pDstLine;
  163. pDstLine += dstRowBytes;
  164. nPixelsM1Temp = nPixelsM1;
  165. do {
  166. rgb = pPackedPalette [*pSrc++];
  167. *pDst++ = (ilByte)rgb; /* red */
  168. rgb >>= 8;
  169. *pDst++ = (ilByte)rgb; /* green */
  170. rgb >>= 8;
  171. *pDst++ = (ilByte)rgb; /* blue */
  172. } while (--nPixelsM1Temp >= 0);
  173. } while (--nLinesM1 >= 0);
  174. return IL_OK;
  175. }
  176. /* Table exported to ilConvert(), declared in /ilc/ilconvert.h .
  177. */
  178. IL_PRIVATE ilConvertRec _ilPaletteToRGBByte = {
  179. IL_NPF, /* CheckFormat() */
  180. IL_STD_FORMAT_BYTE, /* srcFormatCode */
  181. ilAddElementPaletteToRGBByte, /* AddElement() */
  182. IL_DES_RGB, /* pDstDes */
  183. IL_FORMAT_3BYTE_PIXEL, /* pDstFormat */
  184. sizeof (ilPaletteToRGBPrivRec), /* nBytesPrivate */
  185. ilInitPaletteToRGBByte, /* Init() */
  186. IL_NPF, /* Cleanup() */
  187. IL_NPF, /* Destroy() */
  188. ilExecutePaletteToRGBByte /* Execute() */
  189. };