blob: 7433e277f12a261e5f09f641a39f3d7afd23b644 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jutils.c
3 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00004 * Copyright (C) 1991, 1992, Thomas G. Lane.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00005 * 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 miscellaneous utility routines needed for both
9 * compression and decompression.
10 * Note we prefix all global names with "j" to minimize conflicts with
11 * a surrounding application.
12 */
13
14#include "jinclude.h"
15
16
17GLOBAL long
18jround_up (long a, long b)
19/* Compute a rounded up to next multiple of b; a >= 0, b > 0 */
20{
21 a += b-1;
22 return a - (a % b);
23}
24
25
Thomas G. Lane88aeed41992-12-10 00:00:00 +000026/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
27 * and coefficient-block arrays. This won't work on 80x86 because the arrays
28 * are FAR and we're assuming a small-pointer memory model. However, some
29 * DOS compilers provide far-pointer versions of memcpy() and memset() even
30 * in the small-model libraries. These will be used if USE_FMEM is defined.
31 * Otherwise, the routines below do it the hard way. (The performance cost
32 * is not all that great, because these routines aren't very heavily used.)
33 */
34
35#ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */
36#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
37#define FMEMZERO(target,size) MEMZERO(target,size)
38#else /* 80x86 case, define if we can */
39#ifdef USE_FMEM
40#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
41#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
42#endif
43#endif
44
45
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000046GLOBAL void
47jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
48 JSAMPARRAY output_array, int dest_row,
49 int num_rows, long num_cols)
50/* Copy some rows of samples from one place to another.
51 * num_rows rows are copied from input_array[source_row++]
52 * to output_array[dest_row++]; these areas should not overlap.
53 * The source and destination arrays must be at least as wide as num_cols.
54 */
55{
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000056 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +000057#ifdef FMEMCOPY
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000058 register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
Thomas G. Lane88aeed41992-12-10 00:00:00 +000059#else
60 register long count;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000061#endif
62 register int row;
63
64 input_array += source_row;
65 output_array += dest_row;
66
67 for (row = num_rows; row > 0; row--) {
68 inptr = *input_array++;
69 outptr = *output_array++;
Thomas G. Lane88aeed41992-12-10 00:00:00 +000070#ifdef FMEMCOPY
71 FMEMCOPY(outptr, inptr, count);
72#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000073 for (count = num_cols; count > 0; count--)
74 *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000075#endif
76 }
77}
78
79
80GLOBAL void
81jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row, long num_blocks)
82/* Copy a row of coefficient blocks from one place to another. */
83{
Thomas G. Lane88aeed41992-12-10 00:00:00 +000084#ifdef FMEMCOPY
85 FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
86#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000087 register JCOEFPTR inptr, outptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000088 register long count;
89
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000090 inptr = (JCOEFPTR) input_row;
91 outptr = (JCOEFPTR) output_row;
92 for (count = num_blocks * DCTSIZE2; count > 0; count--) {
93 *outptr++ = *inptr++;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000094 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000095#endif
96}
97
98
99GLOBAL void
100jzero_far (void FAR * target, size_t bytestozero)
101/* Zero out a chunk of FAR memory. */
102/* This might be sample-array data, block-array data, or alloc_medium data. */
103{
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000104#ifdef FMEMZERO
105 FMEMZERO(target, bytestozero);
106#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000107 register char FAR * ptr = (char FAR *) target;
108 register size_t count;
109
110 for (count = bytestozero; count > 0; count--) {
111 *ptr++ = 0;
112 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000113#endif
114}