1
/* $Xorg: jcxief.c,v 1.4 2001/02/09 02:04:28 xorgcvs Exp $ */
4
/****************************************************************************
6
Copyright 1993, 1994, 1998 The Open Group
8
Permission to use, copy, modify, distribute, and sell this software and its
9
documentation for any purpose is hereby granted without fee, provided that
10
the above copyright notice appear in all copies and that both that
11
copyright notice and this permission notice appear in supporting
14
The above copyright notice and this permission notice shall be included in
15
all copies or substantial portions of the Software.
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
Except as contained in this notice, the name of The Open Group shall not be
25
used in advertising or otherwise to promote the sale, use or other dealings
26
in this Software without prior written authorization from The Open Group.
31
This software is being provided by AGE Logic, Inc. under the
32
following license. By obtaining, using and/or copying this software,
33
you agree that you have read, understood, and will comply with these
36
Permission to use, copy, modify, distribute and sell this
37
software and its documentation for any purpose and without
38
fee or royalty and to grant others any or all rights granted
39
herein is hereby granted, provided that you agree to comply
40
with the following copyright notice and statements, including
41
the disclaimer, and that the same appears on all copies and
42
derivative works of the software and documentation you make.
44
"Copyright 1993, 1994 by AGE Logic, Inc."
46
THIS SOFTWARE IS PROVIDED "AS IS". AGE LOGIC MAKES NO
47
REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. By way of
48
example, but not limitation, AGE LOGIC MAKE NO
49
REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS
50
FOR ANY PARTICULAR PURPOSE OR THAT THE SOFTWARE DOES NOT
51
INFRINGE THIRD-PARTY PROPRIETARY RIGHTS. AGE LOGIC
52
SHALL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE. IN NO
53
EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT,
54
INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS
55
OF PROFITS, REVENUE, DATA OR USE, INCURRED BY EITHER PARTY OR
56
ANY THIRD PARTY, WHETHER IN AN ACTION IN CONTRACT OR TORT OR
57
BASED ON A WARRANTY, EVEN IF AGE LOGIC LICENSEES
58
HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
61
The name of AGE Logic, Inc. may not be used in
62
advertising or publicity pertaining to this software without
63
specific, written prior permission from AGE Logic.
65
Title to this software shall at all times remain with AGE
67
*****************************************************************************
69
jcxief.c: Xie JPEG Compression Wrapper Routines
71
Gary Rogers, AGE Logic, Inc., October 1993
72
Gary Rogers, AGE Logic, Inc., January 1994
74
****************************************************************************/
79
#include <console.h> /* command-line reader for Macintosh */
82
#if NeedFunctionPrototypes
84
jselrXIE (compress_info_ptr);
86
process_rgb_ycc_row (compress_info_ptr, JSAMPARRAY);
88
jcXIE_get (compress_info_ptr, int, JSAMPARRAY);
90
jcXIE_get_buffersize (compress_info_ptr);
92
j_add_quant_table (compress_info_ptr, int, const QUANT_VAL *, int, boolean);
94
add_huff_table (compress_info_ptr, HUFF_TBL **, const UINT8 *, const UINT8 *);
95
#endif /* NeedFunctionPrototypes */
97
/******************************************************************************/
100
#if NeedFunctionPrototypes
101
load_quant_tables (compress_info_ptr cinfo,
102
UINT8 * q_table, int nq_table, int scale_factor)
104
load_quant_tables (cinfo, q_table, nq_table, scale_factor)
105
compress_info_ptr cinfo;
109
#endif /* NeedFunctionPrototypes */
110
/* Read a set of quantization tables pointed at by q_table.
111
* There may be one to NUM_QUANT_TBLS tables, each of 64 values.
112
* The tables are implicitly numbered 0, 1, etc.
115
/* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
116
/* read in natural order (left to right, top to bottom). */
117
static const short ZIG[DCTSIZE2] = {
118
0, 1, 5, 6, 14, 15, 27, 28,
119
2, 4, 7, 13, 16, 26, 29, 42,
120
3, 8, 12, 17, 25, 30, 41, 43,
121
9, 11, 18, 24, 31, 40, 44, 53,
122
10, 19, 23, 32, 39, 45, 52, 54,
123
20, 22, 33, 38, 46, 51, 55, 60,
124
21, 34, 37, 47, 50, 56, 59, 61,
125
35, 36, 48, 49, 57, 58, 62, 63
131
numtables = nq_table / 64;
132
if (numtables < 1 || numtables > NUM_QUANT_TBLS)
135
for (tblno = 0; tblno < numtables; tblno++)
137
for (i = 0; i < DCTSIZE2; i++)
138
table[ZIG[i]] = *q_table++;
139
j_add_quant_table(cinfo, tblno, table, scale_factor, FALSE);
145
#if NeedFunctionPrototypes
146
load_ac_tables (compress_info_ptr cinfo,
147
UINT8 * ac_table, int nac_table)
149
load_ac_tables (cinfo, ac_table, nac_table)
150
compress_info_ptr cinfo;
153
#endif /* NeedFunctionPrototypes */
154
/* Read a set of AC Huffman tables pointed at by ac_table.
155
* There may be one or two tables.
164
/* luminance AC coefficients */
167
if (nac_table < 0) /* bad length */
169
for(i = 1; i < 17; i++) {
170
bits[i] = *ac_table++;
174
if (nac_table < 0 || 256 < count) /* bad length */
176
for(i = 0; i < count; i++) {
177
val[i] = *ac_table++;
179
if(XIE_ERR == add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0], bits, val))
182
/* chrominance AC coefficients */
185
if (nac_table < 0) /* bad length */
187
for(i = 1; i < 17; i++) {
188
bits[i] = *ac_table++;
192
if (nac_table < 0 || 256 < count) /* bad length */
194
for(i = 0; i < count; i++) {
195
val[i] = *ac_table++;
197
if(XIE_ERR == add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1], bits, val))
204
#if NeedFunctionPrototypes
205
load_dc_tables (compress_info_ptr cinfo,
206
UINT8 * dc_table, int ndc_table)
208
load_dc_tables (cinfo, dc_table, ndc_table)
209
compress_info_ptr cinfo;
212
#endif /* NeedFunctionPrototypes */
213
/* Read a set of DC Huffman tables pointed at by dc_table.
214
* There may be one or two tables.
223
/* luminance DC coefficients */
226
if (ndc_table < 0) /* bad length */
228
for(i = 1; i < 17; i++) {
229
bits[i] = *dc_table++;
233
if (ndc_table < 0 || 256 < count) /* bad length */
235
for(i = 0; i < count; i++) {
236
val[i] = *dc_table++;
238
if(XIE_ERR == add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0], bits, val))
241
/* chrominance DC coefficients */
244
if (ndc_table < 0) /* bad length */
246
for(i = 1; i < 17; i++) {
247
bits[i] = *dc_table++;
251
if (ndc_table < 0 || 256 < count) /* bad length */
253
for(i = 0; i < count; i++) {
254
val[i] = *dc_table++;
256
if(XIE_ERR == add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1], bits, val))
262
/******************************************************************************/
265
#if NeedFunctionPrototypes
266
JC_INIT (compress_info_ptr cinfo,
267
compress_methods_ptr cmethods, external_methods_ptr emethods)
269
JC_INIT (cinfo, cmethods, emethods)
270
compress_info_ptr cinfo;
271
compress_methods_ptr cmethods;
272
external_methods_ptr emethods;
273
#endif /* NeedFunctionPrototypes */
275
/* Set up links to method structures. */
276
cinfo->methods = cmethods;
277
cinfo->emethods = emethods;
279
/* Set restart to NULL */
280
cinfo->XIErestart = XIE_RNUL;
288
#if NeedFunctionPrototypes
289
JC_BEGINFRAME (compress_info_ptr cinfo,
290
long components, long width, long height,
291
UINT8 * q_table, int nq_table,
292
UINT8 * ac_table, int nac_table,
293
UINT8 * dc_table, int ndc_table,
294
short * h_sample, short * v_sample)
296
JC_BEGINFRAME (cinfo, components, width, height,
297
q_table, nq_table, ac_table, nac_table, dc_table, ndc_table,
299
compress_info_ptr cinfo;
309
short * h_sample, * v_sample;
310
#endif /* NeedFunctionPrototypes */
313
short hsample, vsample, total;
315
int scale_factor = 100;
317
if (cinfo->XIErestart == XIE_RNUL) {
318
cinfo->input_components = (short)components;
319
cinfo->image_width = width;
320
cinfo->image_height = height;
322
cinfo->in_color_space = CS_GRAYSCALE;
324
cinfo->in_color_space = CS_RGB;
326
if (1 < components) {
328
for(ci = 0; ci < components; ci++) {
329
hsample = h_sample[ci];
330
if (hsample <= 0 || MAX_SAMP_FACTOR < hsample)
331
return(XIE_ERR); /* bad sub-sampling factor */
332
vsample = v_sample[ci];
333
if (vsample <= 0 || MAX_SAMP_FACTOR < vsample)
334
return(XIE_ERR); /* bad sub-sampling factor */
335
total += (hsample*vsample);
336
if (MAX_BLOCKS_IN_MCU < total)
337
return(XIE_ERR); /* bad sub-sampling factor(s) */
338
cinfo->xie_h_samp_factor[ci] = hsample;
339
cinfo->xie_v_samp_factor[ci] = vsample;
343
if (((*cinfo->methods->input_init) (cinfo)) == XIE_ERR) /* jcXIE_init (cinfo); */
348
if ((load_quant_tables (cinfo, q_table, nq_table, scale_factor)) == XIE_ERR)
353
if ((load_ac_tables (cinfo, ac_table, nac_table)) == XIE_ERR)
358
if ((load_dc_tables (cinfo, dc_table, ndc_table)) == XIE_ERR)
362
if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWFH)) {
363
if ((status = ((*cinfo->methods->write_file_header) (cinfo))) < 0) {
364
if (status == XIE_ERR)
366
cinfo->XIErestart = XIE_RWFH; /* set restart status */
370
if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWSH)) {
371
if ((status = ((*cinfo->methods->write_scan_header) (cinfo))) < 0) {
372
if (status == XIE_ERR)
374
cinfo->XIErestart = XIE_RWSH; /* set restart status */
383
#if NeedFunctionPrototypes
384
JC_ENDFRAME (compress_info_ptr cinfo)
387
compress_info_ptr cinfo;
388
#endif /* NeedFunctionPrototypes */
392
/* jcXIE_term (cinfo); */
393
if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
394
if ((status = ((*cinfo->methods->input_term) (cinfo))) < 0) {
395
if (status == XIE_ERR)
397
cinfo->XIErestart = XIE_RMCU; /* set restart status */
402
(*cinfo->methods->write_scan_trailer) (cinfo); /* does nothing */
403
if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWFT)) {
404
if (((*cinfo->methods->write_file_trailer) (cinfo)) == XIE_OUT) {
405
cinfo->XIErestart = XIE_RWFT; /* set restart status */
413
#if NeedFunctionPrototypes
414
JC_SCANLINE_RGB (compress_info_ptr cinfo,
415
int * scanline, JSAMPROW ptr0, JSAMPROW ptr1, JSAMPROW ptr2)
417
JC_SCANLINE_RGB (cinfo, scanline, ptr0, ptr1, ptr2)
418
compress_info_ptr cinfo;
423
#endif /* NeedFunctionPrototypes */
425
JSAMPROW pixel_row[3];
426
int row, rows_this_time;
430
if (cinfo->XIErestart == XIE_RNUL) {
431
if (cinfo->cur_pixel_row <= 0) {
432
rows_this_time = (int) MIN((long) cinfo->rows_in_mem,
433
cinfo->image_height - row);
434
if (rows_this_time <= 0)
436
cinfo->rows_this_time = rows_this_time;
442
if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
443
if ((status = jcXIE_get (cinfo, row, pixel_row)) < 0) {
444
if (status == XIE_ERR)
446
cinfo->XIErestart = XIE_RMCU; /* set restart status */
455
#if NeedFunctionPrototypes
456
JC_SCANLINE_GRAY (compress_info_ptr cinfo, int * scanline, JSAMPROW ptr0)
458
JC_SCANLINE_GRAY (cinfo, scanline, ptr0)
459
compress_info_ptr cinfo;
462
#endif /* NeedFunctionPrototypes */
464
JSAMPROW pixel_row[3];
465
int row, rows_this_time;
469
if (cinfo->XIErestart == XIE_RNUL) {
470
if (cinfo->cur_pixel_row <= 0) {
471
rows_this_time = (int) MIN((long) cinfo->rows_in_mem,
472
cinfo->image_height - row);
473
if (rows_this_time <= 0)
475
cinfo->rows_this_time = rows_this_time;
481
if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
482
if ((status = jcXIE_get (cinfo, row, pixel_row)) < 0) {
483
if (status == XIE_ERR)
485
cinfo->XIErestart = XIE_RMCU; /* set restart status */