ilutiljpeg.c 10 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 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: ilutiljpeg.c /main/3 1995/10/23 16:02:31 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. #include <stdlib.h>
  40. #include "ilint.h"
  41. #include "ilerrors.h"
  42. #include "iljpgdecode.h"
  43. #include "iljpgencode.h"
  44. #include "ilutiljpeg.h"
  45. /* -------------------- _ilJPEGDataIn -------------------------- */
  46. /* Copy general data (but *not* table info) from IL data into
  47. IL JPG package format.
  48. */
  49. IL_PRIVATE void _ilJPEGDataIn (
  50. const ilImageDes *pDes,
  51. long width,
  52. long height,
  53. iljpgDataPtr pDst
  54. )
  55. {
  56. int maxSubsample;
  57. pDst->width = width;
  58. pDst->height = height;
  59. pDst->nComps = pDes->nSamplesPerPixel;
  60. /* Set IL subsample info into iljpg Hori/VertFactor info, which is essentially
  61. the inverse of IL subsample data: for each sample/comp, hori/vert:
  62. iljpgFactor[comp] = maxILSubsample/ILSubsample[comp]
  63. NOTE: currently only YCbCr data in IL is subsampled!
  64. */
  65. if (pDes->type == IL_YCBCR) {
  66. const ilYCbCrSampleInfo *pSample = pDes->typeInfo.YCbCr.sample;
  67. maxSubsample = pSample[0].subsampleHoriz;
  68. if (pSample[1].subsampleHoriz > maxSubsample)
  69. maxSubsample = pSample[1].subsampleHoriz;
  70. if (pSample[2].subsampleHoriz > maxSubsample)
  71. maxSubsample = pSample[2].subsampleHoriz;
  72. pDst->maxHoriFactor = maxSubsample;
  73. pDst->comp[0].horiFactor = maxSubsample / pSample[0].subsampleHoriz;
  74. pDst->comp[1].horiFactor = maxSubsample / pSample[1].subsampleHoriz;
  75. pDst->comp[2].horiFactor = maxSubsample / pSample[2].subsampleHoriz;
  76. maxSubsample = pSample[0].subsampleVert;
  77. if (pSample[1].subsampleVert > maxSubsample)
  78. maxSubsample = pSample[1].subsampleVert;
  79. if (pSample[2].subsampleVert > maxSubsample)
  80. maxSubsample = pSample[2].subsampleVert;
  81. pDst->maxVertFactor = maxSubsample;
  82. pDst->comp[0].vertFactor = maxSubsample / pSample[0].subsampleVert;
  83. pDst->comp[1].vertFactor = maxSubsample / pSample[1].subsampleVert;
  84. pDst->comp[2].vertFactor = maxSubsample / pSample[2].subsampleVert;
  85. }
  86. else {
  87. pDst->maxHoriFactor = 1;
  88. pDst->maxVertFactor = 1;
  89. pDst->comp[0].horiFactor = 1;
  90. pDst->comp[0].vertFactor = 1;
  91. pDst->comp[1].horiFactor = 1;
  92. pDst->comp[1].vertFactor = 1;
  93. pDst->comp[2].horiFactor = 1;
  94. pDst->comp[2].vertFactor = 1;
  95. pDst->comp[3].horiFactor = 1;
  96. pDst->comp[3].vertFactor = 1;
  97. }
  98. }
  99. /* -------------------- _ilJPEGTablesIn -------------------------- */
  100. /* Copy table info from IL format into IL JPG package format.
  101. Note: the restartInterval is also copied.
  102. */
  103. IL_PRIVATE void _ilJPEGTablesIn (
  104. ilJPEGData *pSrc,
  105. iljpgDataPtr pDst
  106. )
  107. {
  108. /* Copy the restartInterval; just convenient to do so in this function */
  109. pDst->restartInterval = pSrc->restartInterval;
  110. /* Tables are independent of samples; copy all 4 */
  111. pDst->QTables[0] = pSrc->QTables[0];
  112. pDst->QTables[1] = pSrc->QTables[1];
  113. pDst->QTables[2] = pSrc->QTables[2];
  114. pDst->QTables[3] = pSrc->QTables[3];
  115. pDst->DCTables[0] = pSrc->DCTables[0];
  116. pDst->DCTables[1] = pSrc->DCTables[1];
  117. pDst->DCTables[2] = pSrc->DCTables[2];
  118. pDst->DCTables[3] = pSrc->DCTables[3];
  119. pDst->ACTables[0] = pSrc->ACTables[0];
  120. pDst->ACTables[1] = pSrc->ACTables[1];
  121. pDst->ACTables[2] = pSrc->ACTables[2];
  122. pDst->ACTables[3] = pSrc->ACTables[3];
  123. /* Copy 4 samples / components of indices into above tables */
  124. pDst->comp[0].QTableIndex = pSrc->sample[0].QTableIndex;
  125. pDst->comp[0].DCTableIndex = pSrc->sample[0].DCTableIndex;
  126. pDst->comp[0].ACTableIndex = pSrc->sample[0].ACTableIndex;
  127. pDst->comp[1].QTableIndex = pSrc->sample[1].QTableIndex;
  128. pDst->comp[1].DCTableIndex = pSrc->sample[1].DCTableIndex;
  129. pDst->comp[1].ACTableIndex = pSrc->sample[1].ACTableIndex;
  130. pDst->comp[2].QTableIndex = pSrc->sample[2].QTableIndex;
  131. pDst->comp[2].DCTableIndex = pSrc->sample[2].DCTableIndex;
  132. pDst->comp[2].ACTableIndex = pSrc->sample[2].ACTableIndex;
  133. pDst->comp[3].QTableIndex = pSrc->sample[3].QTableIndex;
  134. pDst->comp[3].DCTableIndex = pSrc->sample[3].DCTableIndex;
  135. pDst->comp[3].ACTableIndex = pSrc->sample[3].ACTableIndex;
  136. }
  137. /* -------------------- _ilJPEGFreeTables -------------------------- */
  138. /* Free the tables in the given IL JPEG data block, which is not freed.
  139. */
  140. IL_PRIVATE void _ilJPEGFreeTables (
  141. ilJPEGData *pData
  142. )
  143. {
  144. int i;
  145. for (i = 0; i < 4; i++) {
  146. if (pData->QTables[i]) {
  147. IL_FREE (pData->QTables[i]);
  148. pData->QTables[i] = (ilPtr)NULL;
  149. }
  150. if (pData->DCTables[i]) {
  151. IL_FREE (pData->DCTables[i]);
  152. pData->DCTables[i] = (ilPtr)NULL;
  153. }
  154. if (pData->ACTables[i]) {
  155. IL_FREE (pData->ACTables[i]);
  156. pData->ACTables[i] = (ilPtr)NULL;
  157. }
  158. }
  159. }
  160. /* -------------------- _ilJPEGCopyHuffmanTable -------------------------- */
  161. /* Copy the given JPEG Huffman table into a malloc'd space and return a ptr
  162. to it or null if malloc failed.
  163. */
  164. static ilPtr _ilJPEGCopyHuffmanTable (
  165. ilPtr pSrc
  166. )
  167. {
  168. int i, nBytes;
  169. ilPtr pDst;
  170. /* Huffman tables are: 16 bytes of # occurrences each # bits, followed by
  171. bytes for each of those # occurrences. Size of table = 16 + sum(0..15).
  172. */
  173. for (i = 0, nBytes = 16; i < 16; i++)
  174. nBytes += pSrc[i];
  175. if (!(pDst = (ilPtr)IL_MALLOC (nBytes)))
  176. return (ilPtr)NULL;
  177. bcopy ((char *)pSrc, (char *)pDst, nBytes);
  178. return pDst;
  179. }
  180. /* -------------------- _ilJPEGCopyData -------------------------- */
  181. /* Copy data from pSrc to pDst, mallocing and copying contents of tables.
  182. If an error, free all malloc'd tables in dest, null them and return false.
  183. */
  184. IL_PRIVATE ilBool _ilJPEGCopyData (
  185. ilJPEGData *pSrc,
  186. ilJPEGData *pDst
  187. )
  188. {
  189. int i;
  190. /* Zero table ptrs in *pDst in case of error; copy index info */
  191. bzero ((char *)pDst, sizeof (ilJPEGData));
  192. for (i = 0; i < IL_MAX_SAMPLES; i++)
  193. pDst->sample[i] = pSrc->sample[i];
  194. /* For each non-null table in *pSrc, malloc space, copy contents, set pDst */
  195. for (i = 0; i < 4; i++) {
  196. if (pSrc->QTables[i]) {
  197. if (!(pDst->QTables[i] = (ilPtr)IL_MALLOC (64)))
  198. goto cdMallocError;
  199. bcopy ((char *)pSrc->QTables[i], (char *)pDst->QTables[i], 64);
  200. }
  201. if (pSrc->DCTables[i])
  202. if (!(pDst->DCTables[i] = _ilJPEGCopyHuffmanTable (pSrc->DCTables[i])))
  203. goto cdMallocError;
  204. if (pSrc->ACTables[i])
  205. if (!(pDst->ACTables[i] = _ilJPEGCopyHuffmanTable (pSrc->ACTables[i])))
  206. goto cdMallocError;
  207. }
  208. /* Copy restart interval */
  209. pDst->restartInterval = pSrc->restartInterval;
  210. return TRUE;
  211. /* Goto point on malloc error: free all tables in pDst, return false */
  212. cdMallocError:
  213. _ilJPEGFreeTables (pDst);
  214. return FALSE;
  215. }
  216. /* -------------------- _ilReallocJPEGEncode -------------------------- */
  217. /* Called by ILJPG_ENCODE_PUT_BYTE() macro to reallocate the buffer
  218. defined by "pStream" so that there is space for *exactly* "nBytes" bytes
  219. to be written. Zero (0) is returned if success else an error code.
  220. */
  221. IL_PRIVATE iljpgError _ilReallocJPEGEncode (
  222. ilJPEGEncodeStreamPtr pStream,
  223. long nBytes
  224. )
  225. {
  226. long offset;
  227. /* Allocate space so exactly "nBytes" more bytes will fit in output buffer.
  228. There are "offset" bytes in the buffer now so allocate offset + nBytes.
  229. */
  230. if (pStream->pBuffer) {
  231. offset = pStream->pDst - pStream->pBuffer;
  232. pStream->pBuffer = (ilPtr)IL_REALLOC (pStream->pBuffer, offset + nBytes);
  233. }
  234. else {
  235. offset = 0; /* no buffer yet; allocate one */
  236. pStream->pBuffer = (ilPtr)IL_MALLOC (offset + nBytes);
  237. }
  238. if (!pStream->pBuffer) {
  239. pStream->pDst = pStream->pPastEndBuffer = (ilPtr)NULL;
  240. return ILJPG_ERROR_ENCODE_MALLOC;
  241. }
  242. /* Successful realloc: set end of buffer to pBuffer + new size; set pDst
  243. to pBuffer + offset and return success.
  244. */
  245. pStream->pPastEndBuffer = pStream->pBuffer + offset + nBytes;
  246. pStream->pDst = pStream->pBuffer + offset;
  247. return 0;
  248. }