iljpgencode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 librararies 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: iljpgencode.c /main/3 1995/10/23 15:55:46 rswiston $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1992 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. #include "iljpgencodeint.h"
  40. /* -------------------- iljpgEncodeInit -------------------------- */
  41. /* Init for JPEG encoding and return ptr to private block.
  42. */
  43. ILJPG_PUBLIC
  44. iljpgError iljpgEncodeInit (
  45. register iljpgDataPtr pData,
  46. iljpgPtr *pPrivate /* RETURNED */
  47. )
  48. {
  49. register iljpgEncodePrivPtr pPriv;
  50. register iljpgECompPtr pComp;
  51. iljpgCompDataPtr pCompData;
  52. int comp;
  53. iljpgError error;
  54. register unsigned int index, i;
  55. /* Validate *pData: valid hori/vertFactor, tables present, etc. */
  56. if (!_iljpgValidPars (pData))
  57. return ILJPG_ERROR_ENCODE_PARS;
  58. /* Allocate and return private block */
  59. pPriv = (iljpgEncodePrivPtr)ILJPG_MALLOC_ZERO (sizeof (iljpgEncodePrivRec));
  60. if (!pPriv)
  61. return ILJPG_ERROR_ENCODE_MALLOC;
  62. pPriv->pData = pData;
  63. *pPrivate = (iljpgPtr)pPriv;
  64. /* For DCT encoding, get and store into private a
  65. scaled version of each Q table.
  66. */
  67. for (i = 0; i < 4; i++)
  68. pPriv->DCTScaleTables[i] = (float *)NULL;
  69. for (i = 0; i < 4; i++) {
  70. if (pData->QTables[i]) {
  71. if (!(pPriv->DCTScaleTables[i] = (float *)ILJPG_MALLOC(sizeof(float) * 64)))
  72. return ILJPG_ERROR_ENCODE_MALLOC;
  73. if (error = _iljpgEnDCTScale (pData->QTables[i], pPriv->DCTScaleTables[i]))
  74. return error;
  75. }
  76. }
  77. /* Init Huffman encoding */
  78. if (error = _iljpgEnhuffInit (pPriv))
  79. return error;
  80. /* Setup static part of per-comp data; copy from other places */
  81. for (comp = 0, pCompData = pData->comp, pComp = pPriv->compData;
  82. comp < pData->nComps; comp++, pCompData++, pComp++) {
  83. pComp->pScale = pPriv->DCTScaleTables[pCompData->QTableIndex];
  84. pComp->horiFactor = pCompData->horiFactor;
  85. pComp->vertFactor = pCompData->vertFactor;
  86. pComp->width = pData->width * pCompData->horiFactor / pData->maxHoriFactor;
  87. pComp->mcuXInc = 8 * pCompData->horiFactor;
  88. pComp->mcuYInc = 8 * pCompData->vertFactor;
  89. }
  90. return 0;
  91. }
  92. /* -------------------- iljpgEncodeCleanup -------------------------- */
  93. /* Cleanup after JPEG encoding.
  94. */
  95. ILJPG_PUBLIC
  96. iljpgError iljpgEncodeCleanup (
  97. iljpgPtr pPrivate
  98. )
  99. {
  100. register int i;
  101. register iljpgEncodePrivPtr pPriv;
  102. iljpgError error;
  103. /* Free any scaled Q tables created by Init() */
  104. pPriv = (iljpgEncodePrivPtr)pPrivate;
  105. for (i = 0; i < 4; i++)
  106. if (pPriv->DCTScaleTables[i])
  107. ILJPG_FREE (pPriv->DCTScaleTables[i]);
  108. /* Cleanup Huffman */
  109. error = _iljpgEnhuffCleanup(pPriv);
  110. /* Free the given private data. Note that pPrivate->pData is not
  111. freed; that is done when the caller calls iljpgFreeData().
  112. */
  113. ILJPG_FREE (pPrivate);
  114. return error;
  115. }
  116. /* -------------------- iljpgEncodeExecute -------------------------- */
  117. /* Encode (compress) the pixels from the per-plane buffers pointed to
  118. by "pSrcPixels": one ptr per component (# of components = "nComps" in
  119. the iljpgData passed to iljpgEncodeInit()). "nSrcBytesPerRow" is length
  120. of each row in the corresponding buffers in "pSrcPixels".
  121. "pPrivate" is the ptr returned by iljpgEncodeInit().
  122. "nSrcLines" is the # of lines to read; encoding stops when nSrcLines
  123. lines have been encoded.
  124. */
  125. ILJPG_PUBLIC
  126. iljpgError iljpgEncodeExecute (
  127. iljpgPtr pPrivate,
  128. ILJPG_ENCODE_STREAM stream,
  129. long nSrcLines,
  130. iljpgPtr pSrcPixels[],
  131. long nSrcBytesPerRow[]
  132. )
  133. {
  134. iljpgEncodePrivPtr pPriv;
  135. iljpgDataPtr pData;
  136. register iljpgECompPtr pComp;
  137. iljpgPtr pPixels;
  138. iljpgError error;
  139. long nBytesPerRow, mcuMaxX, mcuMaxY, bX, bY;
  140. int comp, v, h, mcuWidth, mcuHeight;
  141. int pixels[64];
  142. pPriv = (iljpgEncodePrivPtr)pPrivate;
  143. pData = pPriv->pData;
  144. /* Encode "interleaved" JPEG data, where all components are grouped together
  145. in Minimum Coded Unit (MCU) size, = 8 * max hori(width) or vert(height)
  146. factor. The "factors" are the inverse of IL subsample factors. For
  147. example, if component 0 is not subsampled and 1 and 2 are subsampled by 2,
  148. then hori/vertFactor for comp 0 is 2, for comps 1..2 is 1, and max hori/vert
  149. factor is 2 (there are 4x as many comp 0 pixels as comps 1 or 2).
  150. So: loop over y, and over x within y, and decode one MCU (all components),
  151. advancing x by mcuWidth and y by mcuHeight.
  152. */
  153. mcuWidth = 8 * pData->maxHoriFactor;
  154. mcuHeight = 8 * pData->maxVertFactor;
  155. /* Reset temp vars in comp data in private; "height" is size of one strip */
  156. for (comp = 0, pComp = pPriv->compData; comp < pData->nComps; comp++, pComp++) {
  157. pComp->height = nSrcLines * pComp->vertFactor / pData->maxVertFactor;
  158. pComp->x = 0;
  159. pComp->y = 0;
  160. pComp->lastDC = 0; /* new strip; clear as if a reset/restart */
  161. }
  162. /* Loop over y, and over x within y, and encode one MCU (all components),
  163. advancing x by mcuWidth and y by mcuHeight.
  164. */
  165. for (mcuMaxY = 0; mcuMaxY < nSrcLines; mcuMaxY += mcuHeight) {
  166. for (mcuMaxX = 0; mcuMaxX < pData->width; mcuMaxX += mcuWidth) {
  167. /* Encode one MCU, all components, to (mcuX, mcuY). For each component
  168. there are horiFactor * vertFactor 8x8 blocks that go across then down.
  169. */
  170. for (comp = 0, pComp = pPriv->compData; comp < pData->nComps; comp++, pComp++) {
  171. nBytesPerRow = nSrcBytesPerRow[comp];
  172. pPixels = pSrcPixels[comp];
  173. for (v = 0, bY = pComp->y; v < pComp->vertFactor; v++, bY += 8) {
  174. for (h = 0, bX = pComp->x; h < pComp->horiFactor; h++, bX += 8) {
  175. /* Extract one 8x8 block from position (bX, bY). If
  176. clipped replicate pixel or above scan line out to 8x8.
  177. */
  178. { int nBytesInit;
  179. register int nLines, nBytesM1, *pDst, *pDstLine;
  180. register iljpgPtr pSrc, pSrcLine;
  181. nLines = pComp->height - bY;
  182. if (nLines > 8)
  183. nLines = 8;
  184. nBytesInit = pComp->width - bX;
  185. if (nBytesInit > 8)
  186. nBytesInit = 8;
  187. if ((nLines > 0) && (nBytesInit > 0)) {
  188. pSrcLine = pPixels + (bY * nBytesPerRow) + bX;
  189. pDstLine = pixels;
  190. /* If clipped; do nLines, replicate at right edge */
  191. if ((nLines < 8) || (nBytesInit < 8)) {
  192. int i, j, pixel;
  193. for (i = 0; i < nLines; i++) {
  194. pSrc = pSrcLine;
  195. pSrcLine += nBytesPerRow;
  196. pDst = pDstLine;
  197. pDstLine += 8;
  198. for (j = 0; j < nBytesInit; j++)
  199. *pDst++ = pixel = *pSrc++;
  200. for (j = nBytesInit; j < 8; j++)
  201. *pDst++ = pixel; /* replicate right */
  202. }
  203. /* Replicate last dst line out to 8 lines */
  204. pDst = pDstLine;
  205. pDstLine -= 8; /* back up one line */
  206. for (i = nLines; i < 8; i++) {
  207. for (j = 0; j < 8; j++)
  208. *pDst++ = pDstLine[j];
  209. }
  210. } /* END clipped */
  211. else { /* all 8x8 available; do simple case */
  212. nLines--; /* make # lines/bytes - 1 */
  213. nBytesInit--;
  214. do {
  215. nBytesM1 = nBytesInit;
  216. pSrc = pSrcLine;
  217. pSrcLine += nBytesPerRow;
  218. pDst = pDstLine;
  219. pDstLine += 8;
  220. do {
  221. *pDst++ = *pSrc++;
  222. } while (--nBytesM1 >= 0);
  223. } while (--nLines >= 0);
  224. }
  225. }
  226. }
  227. /* Do DCT encoding, from "pixels" back into "pixels" */
  228. _iljpgEnDCT (pixels, pComp->pScale);
  229. /* Subtract previous DC from this one, save this one for next */
  230. { int dc;
  231. dc = pixels[0];
  232. pixels[0] = dc - pComp->lastDC;
  233. pComp->lastDC = dc;
  234. }
  235. /* Do Huffman encoding of block */
  236. if (error = _iljpgEnhuffExecute (pPriv, comp, pixels, stream))
  237. return error;
  238. } /* END hori, one 8x8 block */
  239. } /* END vert */
  240. pComp->x += pComp->mcuXInc; /* move component one MCU to right */
  241. } /* END one component */
  242. } /* END one hori MCU */
  243. /* Move each component one MCU down, reset to left edge */
  244. for (comp = 0, pComp = pPriv->compData; comp < pData->nComps; comp++, pComp++) {
  245. pComp->y += pComp->mcuYInc;
  246. pComp->x = 0;
  247. }
  248. } /* END one vert MCU */
  249. /* Flush any bits not yet output from Huffman encoding and exit */
  250. return _iljpgEnhuffFlush (pPriv, stream);
  251. }