1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker * jutils.c
3*dfc6aa5cSAndroid Build Coastguard Worker *
4*dfc6aa5cSAndroid Build Coastguard Worker * This file was part of the Independent JPEG Group's software:
5*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 1991-1996, Thomas G. Lane.
6*dfc6aa5cSAndroid Build Coastguard Worker * libjpeg-turbo Modifications:
7*dfc6aa5cSAndroid Build Coastguard Worker * Copyright (C) 2022, D. R. Commander.
8*dfc6aa5cSAndroid Build Coastguard Worker * For conditions of distribution and use, see the accompanying README.ijg
9*dfc6aa5cSAndroid Build Coastguard Worker * file.
10*dfc6aa5cSAndroid Build Coastguard Worker *
11*dfc6aa5cSAndroid Build Coastguard Worker * This file contains tables and miscellaneous utility routines needed
12*dfc6aa5cSAndroid Build Coastguard Worker * for both compression and decompression.
13*dfc6aa5cSAndroid Build Coastguard Worker * Note we prefix all global names with "j" to minimize conflicts with
14*dfc6aa5cSAndroid Build Coastguard Worker * a surrounding application.
15*dfc6aa5cSAndroid Build Coastguard Worker */
16*dfc6aa5cSAndroid Build Coastguard Worker
17*dfc6aa5cSAndroid Build Coastguard Worker #define JPEG_INTERNALS
18*dfc6aa5cSAndroid Build Coastguard Worker #include "jinclude.h"
19*dfc6aa5cSAndroid Build Coastguard Worker #include "jpeglib.h"
20*dfc6aa5cSAndroid Build Coastguard Worker
21*dfc6aa5cSAndroid Build Coastguard Worker
22*dfc6aa5cSAndroid Build Coastguard Worker /*
23*dfc6aa5cSAndroid Build Coastguard Worker * jpeg_zigzag_order[i] is the zigzag-order position of the i'th element
24*dfc6aa5cSAndroid Build Coastguard Worker * of a DCT block read in natural order (left to right, top to bottom).
25*dfc6aa5cSAndroid Build Coastguard Worker */
26*dfc6aa5cSAndroid Build Coastguard Worker
27*dfc6aa5cSAndroid Build Coastguard Worker #if 0 /* This table is not actually needed in v6a */
28*dfc6aa5cSAndroid Build Coastguard Worker
29*dfc6aa5cSAndroid Build Coastguard Worker const int jpeg_zigzag_order[DCTSIZE2] = {
30*dfc6aa5cSAndroid Build Coastguard Worker 0, 1, 5, 6, 14, 15, 27, 28,
31*dfc6aa5cSAndroid Build Coastguard Worker 2, 4, 7, 13, 16, 26, 29, 42,
32*dfc6aa5cSAndroid Build Coastguard Worker 3, 8, 12, 17, 25, 30, 41, 43,
33*dfc6aa5cSAndroid Build Coastguard Worker 9, 11, 18, 24, 31, 40, 44, 53,
34*dfc6aa5cSAndroid Build Coastguard Worker 10, 19, 23, 32, 39, 45, 52, 54,
35*dfc6aa5cSAndroid Build Coastguard Worker 20, 22, 33, 38, 46, 51, 55, 60,
36*dfc6aa5cSAndroid Build Coastguard Worker 21, 34, 37, 47, 50, 56, 59, 61,
37*dfc6aa5cSAndroid Build Coastguard Worker 35, 36, 48, 49, 57, 58, 62, 63
38*dfc6aa5cSAndroid Build Coastguard Worker };
39*dfc6aa5cSAndroid Build Coastguard Worker
40*dfc6aa5cSAndroid Build Coastguard Worker #endif
41*dfc6aa5cSAndroid Build Coastguard Worker
42*dfc6aa5cSAndroid Build Coastguard Worker /*
43*dfc6aa5cSAndroid Build Coastguard Worker * jpeg_natural_order[i] is the natural-order position of the i'th element
44*dfc6aa5cSAndroid Build Coastguard Worker * of zigzag order.
45*dfc6aa5cSAndroid Build Coastguard Worker *
46*dfc6aa5cSAndroid Build Coastguard Worker * When reading corrupted data, the Huffman decoders could attempt
47*dfc6aa5cSAndroid Build Coastguard Worker * to reference an entry beyond the end of this array (if the decoded
48*dfc6aa5cSAndroid Build Coastguard Worker * zero run length reaches past the end of the block). To prevent
49*dfc6aa5cSAndroid Build Coastguard Worker * wild stores without adding an inner-loop test, we put some extra
50*dfc6aa5cSAndroid Build Coastguard Worker * "63"s after the real entries. This will cause the extra coefficient
51*dfc6aa5cSAndroid Build Coastguard Worker * to be stored in location 63 of the block, not somewhere random.
52*dfc6aa5cSAndroid Build Coastguard Worker * The worst case would be a run-length of 15, which means we need 16
53*dfc6aa5cSAndroid Build Coastguard Worker * fake entries.
54*dfc6aa5cSAndroid Build Coastguard Worker */
55*dfc6aa5cSAndroid Build Coastguard Worker
56*dfc6aa5cSAndroid Build Coastguard Worker const int jpeg_natural_order[DCTSIZE2 + 16] = {
57*dfc6aa5cSAndroid Build Coastguard Worker 0, 1, 8, 16, 9, 2, 3, 10,
58*dfc6aa5cSAndroid Build Coastguard Worker 17, 24, 32, 25, 18, 11, 4, 5,
59*dfc6aa5cSAndroid Build Coastguard Worker 12, 19, 26, 33, 40, 48, 41, 34,
60*dfc6aa5cSAndroid Build Coastguard Worker 27, 20, 13, 6, 7, 14, 21, 28,
61*dfc6aa5cSAndroid Build Coastguard Worker 35, 42, 49, 56, 57, 50, 43, 36,
62*dfc6aa5cSAndroid Build Coastguard Worker 29, 22, 15, 23, 30, 37, 44, 51,
63*dfc6aa5cSAndroid Build Coastguard Worker 58, 59, 52, 45, 38, 31, 39, 46,
64*dfc6aa5cSAndroid Build Coastguard Worker 53, 60, 61, 54, 47, 55, 62, 63,
65*dfc6aa5cSAndroid Build Coastguard Worker 63, 63, 63, 63, 63, 63, 63, 63, /* extra entries for safety in decoder */
66*dfc6aa5cSAndroid Build Coastguard Worker 63, 63, 63, 63, 63, 63, 63, 63
67*dfc6aa5cSAndroid Build Coastguard Worker };
68*dfc6aa5cSAndroid Build Coastguard Worker
69*dfc6aa5cSAndroid Build Coastguard Worker
70*dfc6aa5cSAndroid Build Coastguard Worker /*
71*dfc6aa5cSAndroid Build Coastguard Worker * Arithmetic utilities
72*dfc6aa5cSAndroid Build Coastguard Worker */
73*dfc6aa5cSAndroid Build Coastguard Worker
74*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(long)
jdiv_round_up(long a,long b)75*dfc6aa5cSAndroid Build Coastguard Worker jdiv_round_up(long a, long b)
76*dfc6aa5cSAndroid Build Coastguard Worker /* Compute a/b rounded up to next integer, ie, ceil(a/b) */
77*dfc6aa5cSAndroid Build Coastguard Worker /* Assumes a >= 0, b > 0 */
78*dfc6aa5cSAndroid Build Coastguard Worker {
79*dfc6aa5cSAndroid Build Coastguard Worker return (a + b - 1L) / b;
80*dfc6aa5cSAndroid Build Coastguard Worker }
81*dfc6aa5cSAndroid Build Coastguard Worker
82*dfc6aa5cSAndroid Build Coastguard Worker
83*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(long)
jround_up(long a,long b)84*dfc6aa5cSAndroid Build Coastguard Worker jround_up(long a, long b)
85*dfc6aa5cSAndroid Build Coastguard Worker /* Compute a rounded up to next multiple of b, ie, ceil(a/b)*b */
86*dfc6aa5cSAndroid Build Coastguard Worker /* Assumes a >= 0, b > 0 */
87*dfc6aa5cSAndroid Build Coastguard Worker {
88*dfc6aa5cSAndroid Build Coastguard Worker a += b - 1L;
89*dfc6aa5cSAndroid Build Coastguard Worker return a - (a % b);
90*dfc6aa5cSAndroid Build Coastguard Worker }
91*dfc6aa5cSAndroid Build Coastguard Worker
92*dfc6aa5cSAndroid Build Coastguard Worker
93*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jcopy_sample_rows(JSAMPARRAY input_array,int source_row,JSAMPARRAY output_array,int dest_row,int num_rows,JDIMENSION num_cols)94*dfc6aa5cSAndroid Build Coastguard Worker jcopy_sample_rows(JSAMPARRAY input_array, int source_row,
95*dfc6aa5cSAndroid Build Coastguard Worker JSAMPARRAY output_array, int dest_row, int num_rows,
96*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION num_cols)
97*dfc6aa5cSAndroid Build Coastguard Worker /* Copy some rows of samples from one place to another.
98*dfc6aa5cSAndroid Build Coastguard Worker * num_rows rows are copied from input_array[source_row++]
99*dfc6aa5cSAndroid Build Coastguard Worker * to output_array[dest_row++]; these areas may overlap for duplication.
100*dfc6aa5cSAndroid Build Coastguard Worker * The source and destination arrays must be at least as wide as num_cols.
101*dfc6aa5cSAndroid Build Coastguard Worker */
102*dfc6aa5cSAndroid Build Coastguard Worker {
103*dfc6aa5cSAndroid Build Coastguard Worker register JSAMPROW inptr, outptr;
104*dfc6aa5cSAndroid Build Coastguard Worker register size_t count = (size_t)(num_cols * sizeof(JSAMPLE));
105*dfc6aa5cSAndroid Build Coastguard Worker register int row;
106*dfc6aa5cSAndroid Build Coastguard Worker
107*dfc6aa5cSAndroid Build Coastguard Worker input_array += source_row;
108*dfc6aa5cSAndroid Build Coastguard Worker output_array += dest_row;
109*dfc6aa5cSAndroid Build Coastguard Worker
110*dfc6aa5cSAndroid Build Coastguard Worker for (row = num_rows; row > 0; row--) {
111*dfc6aa5cSAndroid Build Coastguard Worker inptr = *input_array++;
112*dfc6aa5cSAndroid Build Coastguard Worker outptr = *output_array++;
113*dfc6aa5cSAndroid Build Coastguard Worker memcpy(outptr, inptr, count);
114*dfc6aa5cSAndroid Build Coastguard Worker }
115*dfc6aa5cSAndroid Build Coastguard Worker }
116*dfc6aa5cSAndroid Build Coastguard Worker
117*dfc6aa5cSAndroid Build Coastguard Worker
118*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jcopy_block_row(JBLOCKROW input_row,JBLOCKROW output_row,JDIMENSION num_blocks)119*dfc6aa5cSAndroid Build Coastguard Worker jcopy_block_row(JBLOCKROW input_row, JBLOCKROW output_row,
120*dfc6aa5cSAndroid Build Coastguard Worker JDIMENSION num_blocks)
121*dfc6aa5cSAndroid Build Coastguard Worker /* Copy a row of coefficient blocks from one place to another. */
122*dfc6aa5cSAndroid Build Coastguard Worker {
123*dfc6aa5cSAndroid Build Coastguard Worker memcpy(output_row, input_row, num_blocks * (DCTSIZE2 * sizeof(JCOEF)));
124*dfc6aa5cSAndroid Build Coastguard Worker }
125*dfc6aa5cSAndroid Build Coastguard Worker
126*dfc6aa5cSAndroid Build Coastguard Worker
127*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jzero_far(void * target,size_t bytestozero)128*dfc6aa5cSAndroid Build Coastguard Worker jzero_far(void *target, size_t bytestozero)
129*dfc6aa5cSAndroid Build Coastguard Worker /* Zero out a chunk of memory. */
130*dfc6aa5cSAndroid Build Coastguard Worker /* This might be sample-array data, block-array data, or alloc_large data. */
131*dfc6aa5cSAndroid Build Coastguard Worker {
132*dfc6aa5cSAndroid Build Coastguard Worker memset(target, 0, bytestozero);
133*dfc6aa5cSAndroid Build Coastguard Worker }
134