Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.0 KiB

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