jcomapi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * jcomapi.c
  3. *
  4. * Copyright (C) 1994-1997, Thomas G. Lane.
  5. * Modified 2019 by Guido Vollbeding.
  6. * This file is part of the Independent JPEG Group's software.
  7. * For conditions of distribution and use, see the accompanying README file.
  8. *
  9. * This file contains application interface routines that are used for both
  10. * compression and decompression.
  11. */
  12. #define JPEG_INTERNALS
  13. #include "jinclude.h"
  14. #include "jpeglib.h"
  15. /*
  16. * Abort processing of a JPEG compression or decompression operation,
  17. * but don't destroy the object itself.
  18. *
  19. * For this, we merely clean up all the nonpermanent memory pools.
  20. * Note that temp files (virtual arrays) are not allowed to belong to
  21. * the permanent pool, so we will be able to close all temp files here.
  22. * Closing a data source or destination, if necessary, is the application's
  23. * responsibility.
  24. */
  25. GLOBAL(void)
  26. jpeg_abort (j_common_ptr cinfo)
  27. {
  28. int pool;
  29. /* Do nothing if called on a not-initialized or destroyed JPEG object. */
  30. if (cinfo->mem == NULL)
  31. return;
  32. /* Releasing pools in reverse order might help avoid fragmentation
  33. * with some (brain-damaged) malloc libraries.
  34. */
  35. for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
  36. (*cinfo->mem->free_pool) (cinfo, pool);
  37. }
  38. /* Reset overall state for possible reuse of object */
  39. if (cinfo->is_decompressor) {
  40. cinfo->global_state = DSTATE_START;
  41. /* Try to keep application from accessing now-deleted marker list.
  42. * A bit kludgy to do it here, but this is the most central place.
  43. */
  44. ((j_decompress_ptr) cinfo)->marker_list = NULL;
  45. } else {
  46. cinfo->global_state = CSTATE_START;
  47. }
  48. }
  49. /*
  50. * Destruction of a JPEG object.
  51. *
  52. * Everything gets deallocated except the master jpeg_compress_struct itself
  53. * and the error manager struct. Both of these are supplied by the application
  54. * and must be freed, if necessary, by the application. (Often they are on
  55. * the stack and so don't need to be freed anyway.)
  56. * Closing a data source or destination, if necessary, is the application's
  57. * responsibility.
  58. */
  59. GLOBAL(void)
  60. jpeg_destroy (j_common_ptr cinfo)
  61. {
  62. /* We need only tell the memory manager to release everything. */
  63. /* NB: mem pointer is NULL if memory mgr failed to initialize. */
  64. if (cinfo->mem != NULL)
  65. (*cinfo->mem->self_destruct) (cinfo);
  66. cinfo->mem = NULL; /* be safe if jpeg_destroy is called twice */
  67. cinfo->global_state = 0; /* mark it destroyed */
  68. }
  69. /*
  70. * Convenience routines for allocating quantization and Huffman tables.
  71. * (Would jutils.c be a more reasonable place to put these?)
  72. */
  73. GLOBAL(JQUANT_TBL *)
  74. jpeg_alloc_quant_table (j_common_ptr cinfo)
  75. {
  76. JQUANT_TBL *tbl;
  77. tbl = (JQUANT_TBL *)
  78. (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
  79. tbl->sent_table = FALSE; /* make sure this is false in any new table */
  80. return tbl;
  81. }
  82. GLOBAL(JHUFF_TBL *)
  83. jpeg_alloc_huff_table (j_common_ptr cinfo)
  84. {
  85. JHUFF_TBL *tbl;
  86. tbl = (JHUFF_TBL *)
  87. (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
  88. tbl->sent_table = FALSE; /* make sure this is false in any new table */
  89. return tbl;
  90. }
  91. /*
  92. * Set up the standard Huffman tables (cf. JPEG standard section K.3).
  93. * IMPORTANT: these are only valid for 8-bit data precision!
  94. * (Would jutils.c be a more reasonable place to put this?)
  95. */
  96. GLOBAL(JHUFF_TBL *)
  97. jpeg_std_huff_table (j_common_ptr cinfo, boolean isDC, int tblno)
  98. {
  99. JHUFF_TBL **htblptr, *htbl;
  100. const UINT8 *bits, *val;
  101. int nsymbols, len;
  102. static const UINT8 bits_dc_luminance[17] =
  103. { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  104. static const UINT8 val_dc_luminance[] =
  105. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  106. static const UINT8 bits_dc_chrominance[17] =
  107. { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  108. static const UINT8 val_dc_chrominance[] =
  109. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  110. static const UINT8 bits_ac_luminance[17] =
  111. { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  112. static const UINT8 val_ac_luminance[] =
  113. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  114. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  115. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  116. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  117. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  118. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  119. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  120. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  121. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  122. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  123. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  124. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  125. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  126. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  127. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  128. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  129. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  130. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  131. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  132. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  133. 0xf9, 0xfa };
  134. static const UINT8 bits_ac_chrominance[17] =
  135. { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  136. static const UINT8 val_ac_chrominance[] =
  137. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  138. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  139. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  140. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  141. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  142. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  143. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  144. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  145. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  146. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  147. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  148. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  149. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  150. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  151. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  152. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  153. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  154. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  155. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  156. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  157. 0xf9, 0xfa };
  158. if (cinfo->is_decompressor) {
  159. if (isDC)
  160. htblptr = ((j_decompress_ptr) cinfo)->dc_huff_tbl_ptrs;
  161. else
  162. htblptr = ((j_decompress_ptr) cinfo)->ac_huff_tbl_ptrs;
  163. } else {
  164. if (isDC)
  165. htblptr = ((j_compress_ptr) cinfo)->dc_huff_tbl_ptrs;
  166. else
  167. htblptr = ((j_compress_ptr) cinfo)->ac_huff_tbl_ptrs;
  168. }
  169. switch (tblno) {
  170. case 0:
  171. if (isDC) {
  172. bits = bits_dc_luminance;
  173. val = val_dc_luminance;
  174. } else {
  175. bits = bits_ac_luminance;
  176. val = val_ac_luminance;
  177. }
  178. break;
  179. case 1:
  180. if (isDC) {
  181. bits = bits_dc_chrominance;
  182. val = val_dc_chrominance;
  183. } else {
  184. bits = bits_ac_chrominance;
  185. val = val_ac_chrominance;
  186. }
  187. break;
  188. default:
  189. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  190. return NULL; /* avoid compiler warnings for uninitialized variables */
  191. }
  192. if (htblptr[tblno] == NULL)
  193. htblptr[tblno] = jpeg_alloc_huff_table(cinfo);
  194. htbl = htblptr[tblno];
  195. /* Copy the number-of-symbols-of-each-code-length counts */
  196. MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
  197. /* Validate the counts. We do this here mainly so we can copy the right
  198. * number of symbols from the val[] array, without risking marching off
  199. * the end of memory. jxhuff.c will do a more thorough test later.
  200. */
  201. nsymbols = 0;
  202. for (len = 1; len <= 16; len++)
  203. nsymbols += bits[len];
  204. if (nsymbols > 256)
  205. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  206. if (nsymbols > 0)
  207. MEMCOPY(htbl->huffval, val, nsymbols * SIZEOF(UINT8));
  208. /* Initialize sent_table FALSE so table will be written to JPEG file. */
  209. htbl->sent_table = FALSE;
  210. return htbl;
  211. }