home *** CD-ROM | disk | FTP | other *** search
/ PC World Plus! (NZ) 2001 June / HDC50.iso / Info / Extras / Jpeg / SRC / JUTILS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-11  |  5.4 KB  |  184 lines

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