jutils.c 5.1 KB

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