blob: ef1017406956c9f15b708f92c27c35a660618f58 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jutils.c
3 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00004 * Copyright (C) 1991-1994, 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
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000015#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016#include "jpeglib.h"
17
18
19/*
20 * Arithmetic utilities
21 */
22
23GLOBAL long
24jdiv_round_up (long a, long b)
25/* Compute a/b rounded up to next integer, ie, ceil(a/b) */
26/* Assumes a >= 0, b > 0 */
27{
28 return (a + b - 1L) / b;
29}
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000030
31
32GLOBAL long
33jround_up (long a, long b)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034/* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
35/* Assumes a >= 0, b > 0 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000036{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000037 a += b - 1L;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000038 return a - (a % b);
39}
40
41
Thomas G. Lane88aeed41992-12-10 00:00:00 +000042/* On normal machines we can apply MEMCOPY() and MEMZERO() to sample arrays
43 * and coefficient-block arrays. This won't work on 80x86 because the arrays
44 * are FAR and we're assuming a small-pointer memory model. However, some
45 * DOS compilers provide far-pointer versions of memcpy() and memset() even
46 * in the small-model libraries. These will be used if USE_FMEM is defined.
47 * Otherwise, the routines below do it the hard way. (The performance cost
48 * is not all that great, because these routines aren't very heavily used.)
49 */
50
51#ifndef NEED_FAR_POINTERS /* normal case, same as regular macros */
52#define FMEMCOPY(dest,src,size) MEMCOPY(dest,src,size)
53#define FMEMZERO(target,size) MEMZERO(target,size)
54#else /* 80x86 case, define if we can */
55#ifdef USE_FMEM
56#define FMEMCOPY(dest,src,size) _fmemcpy((void FAR *)(dest), (const void FAR *)(src), (size_t)(size))
57#define FMEMZERO(target,size) _fmemset((void FAR *)(target), 0, (size_t)(size))
58#endif
59#endif
60
61
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000062GLOBAL void
63jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
64 JSAMPARRAY output_array, int dest_row,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000065 int num_rows, JDIMENSION num_cols)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000066/* Copy some rows of samples from one place to another.
67 * num_rows rows are copied from input_array[source_row++]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000068 * to output_array[dest_row++]; these areas may overlap for duplication.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000069 * The source and destination arrays must be at least as wide as num_cols.
70 */
71{
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000072 register JSAMPROW inptr, outptr;
Thomas G. Lane88aeed41992-12-10 00:00:00 +000073#ifdef FMEMCOPY
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000074 register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE));
Thomas G. Lane88aeed41992-12-10 00:00:00 +000075#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000076 register JDIMENSION count;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000077#endif
78 register int row;
79
80 input_array += source_row;
81 output_array += dest_row;
82
83 for (row = num_rows; row > 0; row--) {
84 inptr = *input_array++;
85 outptr = *output_array++;
Thomas G. Lane88aeed41992-12-10 00:00:00 +000086#ifdef FMEMCOPY
87 FMEMCOPY(outptr, inptr, count);
88#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000089 for (count = num_cols; count > 0; count--)
90 *outptr++ = *inptr++; /* needn't bother with GETJSAMPLE() here */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000091#endif
92 }
93}
94
95
96GLOBAL void
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
98 JDIMENSION num_blocks)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000099/* Copy a row of coefficient blocks from one place to another. */
100{
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000101#ifdef FMEMCOPY
102 FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
103#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000104 register JCOEFPTR inptr, outptr;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000105 register long count;
106
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000107 inptr = (JCOEFPTR) input_row;
108 outptr = (JCOEFPTR) output_row;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 for (count = (long) num_blocks * DCTSIZE2; count > 0; count--) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000110 *outptr++ = *inptr++;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000111 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000112#endif
113}
114
115
116GLOBAL void
117jzero_far (void FAR * target, size_t bytestozero)
118/* Zero out a chunk of FAR memory. */
119/* This might be sample-array data, block-array data, or alloc_medium data. */
120{
Thomas G. Lane88aeed41992-12-10 00:00:00 +0000121#ifdef FMEMZERO
122 FMEMZERO(target, bytestozero);
123#else
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000124 register char FAR * ptr = (char FAR *) target;
125 register size_t count;
126
127 for (count = bytestozero; count > 0; count--) {
128 *ptr++ = 0;
129 }
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000130#endif
131}