jutils.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * jutils.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * Modified 2009-2011 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 tables and miscellaneous utility routines needed
  10. * for both compression and decompression.
  11. * Note we prefix all global names with "j" to minimize conflicts with
  12. * a surrounding application.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. /*
  18. * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
  19. * of a DCT block read in natural order (left to right, top to bottom).
  20. */
  21. #if 0 /* This table is not actually needed in v6a */
  22. const int jpeg_zigzag_order[DCTSIZE2] = {
  23. 0, 1, 5, 6, 14, 15, 27, 28,
  24. 2, 4, 7, 13, 16, 26, 29, 42,
  25. 3, 8, 12, 17, 25, 30, 41, 43,
  26. 9, 11, 18, 24, 31, 40, 44, 53,
  27. 10, 19, 23, 32, 39, 45, 52, 54,
  28. 20, 22, 33, 38, 46, 51, 55, 60,
  29. 21, 34, 37, 47, 50, 56, 59, 61,
  30. 35, 36, 48, 49, 57, 58, 62, 63
  31. };
  32. #endif
  33. /*
  34. * jpeg_natural_order[i] is the natural-order position of the i'th element
  35. * of zigzag order.
  36. *
  37. * When reading corrupted data, the Huffman decoders could attempt
  38. * to reference an entry beyond the end of this array (if the decoded
  39. * zero run length reaches past the end of the block). To prevent
  40. * wild stores without adding an inner-loop test, we put some extra
  41. * "63"s after the real entries. This will cause the extra coefficient
  42. * to be stored in location 63 of the block, not somewhere random.
  43. * The worst case would be a run-length of 15, which means we need 16
  44. * fake entries.
  45. */
  46. const int jpeg_natural_order[DCTSIZE2+16] = {
  47. 0, 1, 8, 16, 9, 2, 3, 10,
  48. 17, 24, 32, 25, 18, 11, 4, 5,
  49. 12, 19, 26, 33, 40, 48, 41, 34,
  50. 27, 20, 13, 6, 7, 14, 21, 28,
  51. 35, 42, 49, 56, 57, 50, 43, 36,
  52. 29, 22, 15, 23, 30, 37, 44, 51,
  53. 58, 59, 52, 45, 38, 31, 39, 46,
  54. 53, 60, 61, 54, 47, 55, 62, 63,
  55. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  56. 63, 63, 63, 63, 63, 63, 63, 63
  57. };
  58. const int jpeg_natural_order7[7*7+16] = {
  59. 0, 1, 8, 16, 9, 2, 3, 10,
  60. 17, 24, 32, 25, 18, 11, 4, 5,
  61. 12, 19, 26, 33, 40, 48, 41, 34,
  62. 27, 20, 13, 6, 14, 21, 28, 35,
  63. 42, 49, 50, 43, 36, 29, 22, 30,
  64. 37, 44, 51, 52, 45, 38, 46, 53,
  65. 54,
  66. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  67. 63, 63, 63, 63, 63, 63, 63, 63
  68. };
  69. const int jpeg_natural_order6[6*6+16] = {
  70. 0, 1, 8, 16, 9, 2, 3, 10,
  71. 17, 24, 32, 25, 18, 11, 4, 5,
  72. 12, 19, 26, 33, 40, 41, 34, 27,
  73. 20, 13, 21, 28, 35, 42, 43, 36,
  74. 29, 37, 44, 45,
  75. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  76. 63, 63, 63, 63, 63, 63, 63, 63
  77. };
  78. const int jpeg_natural_order5[5*5+16] = {
  79. 0, 1, 8, 16, 9, 2, 3, 10,
  80. 17, 24, 32, 25, 18, 11, 4, 12,
  81. 19, 26, 33, 34, 27, 20, 28, 35,
  82. 36,
  83. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  84. 63, 63, 63, 63, 63, 63, 63, 63
  85. };
  86. const int jpeg_natural_order4[4*4+16] = {
  87. 0, 1, 8, 16, 9, 2, 3, 10,
  88. 17, 24, 25, 18, 11, 19, 26, 27,
  89. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  90. 63, 63, 63, 63, 63, 63, 63, 63
  91. };
  92. const int jpeg_natural_order3[3*3+16] = {
  93. 0, 1, 8, 16, 9, 2, 10, 17,
  94. 18,
  95. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  96. 63, 63, 63, 63, 63, 63, 63, 63
  97. };
  98. const int jpeg_natural_order2[2*2+16] = {
  99. 0, 1, 8, 9,
  100. 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
  101. 63, 63, 63, 63, 63, 63, 63, 63
  102. };
  103. /*
  104. * Arithmetic utilities
  105. */
  106. GLOBAL(long)
  107. jdiv_round_up (long a, long b)
  108. /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
  109. /* Assumes a >= 0, b > 0 */
  110. {
  111. return (a + b - 1L) / b;
  112. }
  113. GLOBAL(long)
  114. jround_up (long a, long b)
  115. /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
  116. /* Assumes a >= 0, b > 0 */
  117. {
  118. a += b - 1L;
  119. return a - (a % b);
  120. }
  121. /* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
  122. * and coefficient-block arrays. This won't work on 80x86 because the arrays
  123. * are FAR and we're assuming a small-pointer memory model. However, some
  124. * DOS compilers provide far-pointer versions of memcpy() and memset() even
  125. * in the small-model libraries. These will be used if USE_FMEM is defined.
  126. * Otherwise, the routines below do it the hard way. (The performance cost
  127. * is not all that great, because these routines aren't very heavily used.)
  128. */
  129. #ifndef NEED_FAR_POINTERS /* normal case, same as regular macro */
  130. #define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
  131. #else /* 80x86 case, define if we can */
  132. #ifdef USE_FMEM
  133. #define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
  134. #else
  135. /* This function is for use by the FMEMZERO macro defined in jpegint.h.
  136. * Do not call this function directly, use the FMEMZERO macro instead.
  137. */
  138. GLOBAL(void)
  139. jzero_far (void FAR * target, size_t bytestozero)
  140. /* Zero out a chunk of FAR memory. */
  141. /* This might be sample-array data, block-array data, or alloc_large data. */
  142. {
  143. register char FAR * ptr = (char FAR *) target;
  144. register size_t count;
  145. for (count = bytestozero; count > 0; count--) {
  146. *ptr++ = 0;
  147. }
  148. }
  149. #endif
  150. #endif
  151. GLOBAL(void)
  152. jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
  153. JSAMPARRAY output_array, int dest_row,
  154. int num_rows, JDIMENSION num_cols)
  155. /* Copy some rows of samples from one place to another.
  156. * num_rows rows are copied from input_array[source_row++]
  157. * to output_array[dest_row++]; these areas may overlap for duplication.
  158. * The source and destination arrays must be at least as wide as num_cols.
  159. */
  160. {
  161. register JSAMPROW inptr, outptr;
  162. #ifdef FMEMCOPY
  163. register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
  164. #else
  165. register JDIMENSION count;
  166. #endif
  167. register int row;
  168. input_array += source_row;
  169. output_array += dest_row;
  170. for (row = num_rows; row > 0; row--) {
  171. inptr = *input_array++;
  172. outptr = *output_array++;
  173. #ifdef FMEMCOPY
  174. FMEMCOPY(outptr, inptr, count);
  175. #else
  176. for (count = num_cols; count > 0; count--)
  177. *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
  178. #endif
  179. }
  180. }
  181. GLOBAL(void)
  182. jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
  183. JDIMENSION num_blocks)
  184. /* Copy a row of coefficient blocks from one place to another. */
  185. {
  186. #ifdef FMEMCOPY
  187. FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
  188. #else
  189. register JCOEFPTR inptr, outptr;
  190. register long count;
  191. inptr = (JCOEFPTR) input_row;
  192. outptr = (JCOEFPTR) output_row;
  193. for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
  194. *outptr++ = *inptr++;
  195. }
  196. #endif
  197. }