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.

182 lines
5.2 KiB

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