summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/XIE/mixie/jpeg/jcxief.c
blob: e3e20a12624fb0fd20c0f0059286ff622dd2ab18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/* $XConsortium: jcxief.c,v 1.3 94/01/12 20:18:54 rws Exp $ */
/* Module jcxief.c */

/****************************************************************************

Copyright (c) 1993, 1994  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.


				NOTICE
                              
This software is being provided by AGE Logic, Inc. under the
following license.  By obtaining, using and/or copying this software,
you agree that you have read, understood, and will comply with these
terms and conditions:

     Permission to use, copy, modify, distribute and sell this
     software and its documentation for any purpose and without
     fee or royalty and to grant others any or all rights granted
     herein is hereby granted, provided that you agree to comply
     with the following copyright notice and statements, including
     the disclaimer, and that the same appears on all copies and
     derivative works of the software and documentation you make.
     
     "Copyright 1993, 1994 by AGE Logic, Inc."
     
     THIS SOFTWARE IS PROVIDED "AS IS".  AGE LOGIC MAKES NO
     REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way of
     example, but not limitation, AGE LOGIC MAKE NO
     REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS
     FOR ANY PARTICULAR PURPOSE OR THAT THE SOFTWARE DOES NOT
     INFRINGE THIRD-PARTY PROPRIETARY RIGHTS.  AGE LOGIC 
     SHALL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE.  IN NO
     EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT,
     INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS
     OF PROFITS, REVENUE, DATA OR USE, INCURRED BY EITHER PARTY OR
     ANY THIRD PARTY, WHETHER IN AN ACTION IN CONTRACT OR TORT OR
     BASED ON A WARRANTY, EVEN IF AGE LOGIC LICENSEES
     HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
     DAMAGES.
    
     The name of AGE Logic, Inc. may not be used in
     advertising or publicity pertaining to this software without
     specific, written prior permission from AGE Logic.

     Title to this software shall at all times remain with AGE
     Logic, Inc.
*****************************************************************************

	jcxief.c: Xie JPEG Compression Wrapper Routines 

	Gary Rogers, AGE Logic, Inc., October 1993
	Gary Rogers, AGE Logic, Inc., January 1994

****************************************************************************/

#include "jinclude.h"

#ifdef THINK_C
#include <console.h>		/* command-line reader for Macintosh */
#endif

#if NeedFunctionPrototypes
GLOBAL void
jselrXIE (compress_info_ptr);
GLOBAL void	
process_rgb_ycc_row (compress_info_ptr, JSAMPARRAY);
GLOBAL int
jcXIE_get (compress_info_ptr, int, JSAMPARRAY);
GLOBAL void
jcXIE_get_buffersize (compress_info_ptr);
GLOBAL int
j_add_quant_table (compress_info_ptr, int, const QUANT_VAL *, int, boolean);
GLOBAL int
add_huff_table (compress_info_ptr, HUFF_TBL **, const UINT8 *, const UINT8 *);
#endif	/* NeedFunctionPrototypes */

/******************************************************************************/

LOCAL int
#if NeedFunctionPrototypes
load_quant_tables (compress_info_ptr cinfo, 
			UINT8 * q_table, int nq_table, int scale_factor)
#else
load_quant_tables (cinfo, q_table, nq_table, scale_factor)
	compress_info_ptr cinfo;
	UINT8 * q_table;
	int nq_table;
	int scale_factor;
#endif	/* NeedFunctionPrototypes */
/* Read a set of quantization tables pointed at by q_table.
 * There may be one to NUM_QUANT_TBLS tables, each of 64 values.
 * The tables are implicitly numbered 0, 1, etc.
 */
{
  /* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
  /* read in natural order (left to right, top to bottom). */
  static const short ZIG[DCTSIZE2] = {
     0,  1,  5,  6, 14, 15, 27, 28,
     2,  4,  7, 13, 16, 26, 29, 42,
     3,  8, 12, 17, 25, 30, 41, 43,
     9, 11, 18, 24, 31, 40, 44, 53,
    10, 19, 23, 32, 39, 45, 52, 54,
    20, 22, 33, 38, 46, 51, 55, 60,
    21, 34, 37, 47, 50, 56, 59, 61,
    35, 36, 48, 49, 57, 58, 62, 63
    };
  int numtables;    
  int tblno, i;
  QUANT_TBL table;

  numtables = nq_table / 64;
  if (numtables < 1 || numtables > NUM_QUANT_TBLS)
  	return(XIE_ERR);
    
  for (tblno = 0; tblno < numtables; tblno++)
  	{
    for (i = 0; i < DCTSIZE2; i++)
      table[ZIG[i]] = *q_table++;
    j_add_quant_table(cinfo, tblno, table, scale_factor, FALSE);
  }
  return(0);
}

LOCAL int
#if NeedFunctionPrototypes
load_ac_tables (compress_info_ptr cinfo, 
			UINT8 * ac_table, int nac_table)
#else
load_ac_tables (cinfo, ac_table, nac_table)
	compress_info_ptr cinfo;
	UINT8 * ac_table;
	int nac_table;
#endif	/* NeedFunctionPrototypes */
/* Read a set of AC Huffman tables pointed at by ac_table.
 * There may be one or two tables.
 */
{
  int i;
  int count;
  UINT8 bits[17];
  UINT8 val[256];

  bits[0] = 0;
  /* luminance AC coefficients */  
  count = 0;
  nac_table -= 16;
  if (nac_table < 0)	/* bad length */
  	return(XIE_ERR);
  for(i = 1; i < 17; i++) {
    bits[i] = *ac_table++;
    count += bits[i];
  }
  nac_table -= count;
  if (nac_table < 0 || 256 < count)	/* bad length */
  	return(XIE_ERR);
  for(i = 0; i < count; i++) {
    val[i] = *ac_table++;
  }
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0], bits, val))
    return(XIE_ERR);
    
  /* chrominance AC coefficients */  
  count = 0;
  nac_table -= 16;
  if (nac_table < 0)	/* bad length */
  	return(XIE_ERR);
  for(i = 1; i < 17; i++) {
    bits[i] = *ac_table++;
    count += bits[i];
  }
  nac_table -= count;
  if (nac_table < 0 || 256 < count)	/* bad length */
  	return(XIE_ERR);
  for(i = 0; i < count; i++) {
    val[i] = *ac_table++;
  }
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1], bits, val))
    return(XIE_ERR);
    
  return(0);
}

LOCAL int
#if NeedFunctionPrototypes
load_dc_tables (compress_info_ptr cinfo, 
			UINT8 * dc_table, int ndc_table)
#else
load_dc_tables (cinfo, dc_table, ndc_table)
	compress_info_ptr cinfo;
	UINT8 * dc_table;
	int ndc_table;
#endif	/* NeedFunctionPrototypes */
/* Read a set of DC Huffman tables pointed at by dc_table.
 * There may be one or two tables.
 */
{
  int i;
  int count;
  UINT8 bits[17];
  UINT8 val[256];

  bits[0] = 0;
  /* luminance DC coefficients */  
  count = 0;
  ndc_table -= 16;
  if (ndc_table < 0)				/* bad length */
  	return(XIE_ERR);
  for(i = 1; i < 17; i++) {
    bits[i] = *dc_table++;
    count += bits[i];
  }
  ndc_table -= count;
  if (ndc_table < 0 || 256 < count)	/* bad length */
  	return(XIE_ERR);
  for(i = 0; i < count; i++) {
    val[i] = *dc_table++;
  }
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0], bits, val))
    return(XIE_ERR);
    
  /* chrominance DC coefficients */  
  count = 0;
  ndc_table -= 16;
  if (ndc_table < 0)				/* bad length */
  	return(XIE_ERR);
  for(i = 1; i < 17; i++) {
    bits[i] = *dc_table++;
    count += bits[i];
  }
  ndc_table -= count;
  if (ndc_table < 0 || 256 < count)	/* bad length */
  	return(XIE_ERR);
  for(i = 0; i < count; i++) {
    val[i] = *dc_table++;
  }
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1], bits, val))
    return(XIE_ERR);
    
  return(0);
}

/******************************************************************************/

GLOBAL int
#if NeedFunctionPrototypes
JC_INIT (compress_info_ptr cinfo,
	compress_methods_ptr cmethods, external_methods_ptr emethods)
#else
JC_INIT (cinfo, cmethods, emethods)
	compress_info_ptr cinfo;
	compress_methods_ptr cmethods;
	external_methods_ptr emethods;
#endif	/* NeedFunctionPrototypes */
{
  /* Set up links to method structures. */
  cinfo->methods = cmethods;
  cinfo->emethods = emethods;

  /* Set restart to NULL */
  cinfo->XIErestart = XIE_RNUL;

  jselrXIE (cinfo);

  return (XIE_NRML);
}

GLOBAL int
#if NeedFunctionPrototypes
JC_BEGINFRAME (compress_info_ptr cinfo,
	long components, long width, long height,
	UINT8 * q_table, int nq_table, 
	UINT8 * ac_table, int nac_table,
	UINT8 * dc_table, int ndc_table,
	short * h_sample, short * v_sample)
#else
JC_BEGINFRAME (cinfo, components, width, height,
		q_table, nq_table, ac_table, nac_table, dc_table, ndc_table,
		h_sample, v_sample)
	compress_info_ptr cinfo; 
	long components; 
	long width; 
	long height;
	UINT8 * q_table;
	int nq_table;
	UINT8 * ac_table;
	int nac_table;
	UINT8 * dc_table;
	int ndc_table;
      short * h_sample, * v_sample;
#endif	/* NeedFunctionPrototypes */
{
  short ci;
  short hsample, vsample, total;
  int status;
  int scale_factor = 100;
  	
  if (cinfo->XIErestart == XIE_RNUL) {
    cinfo->input_components = (short)components;
    cinfo->image_width = width;
    cinfo->image_height = height;
    if (components == 1)
      cinfo->in_color_space = CS_GRAYSCALE;
    else
      cinfo->in_color_space = CS_RGB;

    if (1 < components) {
      total = 0;
      for(ci = 0; ci < components; ci++) {
        hsample = h_sample[ci];
        if (hsample <= 0 || MAX_SAMP_FACTOR < hsample)
          return(XIE_ERR);  /* bad sub-sampling factor */
        vsample = v_sample[ci];
        if (vsample <= 0 || MAX_SAMP_FACTOR < vsample)
          return(XIE_ERR);  /* bad sub-sampling factor */
        total += (hsample*vsample);
        if (MAX_BLOCKS_IN_MCU < total)
          return(XIE_ERR);  /* bad sub-sampling factor(s) */
        cinfo->xie_h_samp_factor[ci] = hsample;            
        cinfo->xie_v_samp_factor[ci] = vsample;            
      }
    }

    if (((*cinfo->methods->input_init) (cinfo)) == XIE_ERR)	/* jcXIE_init (cinfo); */
      return(XIE_ERR);
  }
  
  if (0 < nq_table) {
  	if ((load_quant_tables (cinfo, q_table, nq_table, scale_factor)) == XIE_ERR)
		return(XIE_ERR);
  }
        
  if (0 < nac_table) {
  	if ((load_ac_tables (cinfo, ac_table, nac_table)) == XIE_ERR)
		return(XIE_ERR);
  }
        
  if (0 < ndc_table) {
  	if ((load_dc_tables (cinfo, dc_table, ndc_table)) == XIE_ERR)
		return(XIE_ERR);
  }
        
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWFH)) {
    if ((status = ((*cinfo->methods->write_file_header) (cinfo))) < 0) {
   	  if (status == XIE_ERR)
        return(XIE_ERR);
      cinfo->XIErestart = XIE_RWFH;	/* set restart status */
      return(XIE_OUT);	
    }
  }
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWSH)) {
	if ((status = ((*cinfo->methods->write_scan_header) (cinfo))) < 0) {
   	  if (status == XIE_ERR)
        return(XIE_ERR);
      cinfo->XIErestart = XIE_RWSH;	/* set restart status */
      return(XIE_OUT);	
    }
  }

  return (XIE_NRML);
}

GLOBAL int
#if NeedFunctionPrototypes
JC_ENDFRAME (compress_info_ptr cinfo)
#else
JC_ENDFRAME (cinfo)
	compress_info_ptr cinfo;
#endif	/* NeedFunctionPrototypes */
{
  int status;
  	
  /* jcXIE_term (cinfo); */
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
	if ((status = ((*cinfo->methods->input_term) (cinfo))) < 0) {
      if (status == XIE_ERR)
      	return(XIE_ERR);
      cinfo->XIErestart = XIE_RMCU;	/* set restart status */
      return(XIE_OUT);	
    }
  }
  
  (*cinfo->methods->write_scan_trailer) (cinfo); /* does nothing */
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWFT)) {
	if (((*cinfo->methods->write_file_trailer) (cinfo)) == XIE_OUT) {
	  cinfo->XIErestart = XIE_RWFT;	/* set restart status */
      return(XIE_OUT);	
    }
  }
  return (XIE_NRML);
}

GLOBAL int
#if NeedFunctionPrototypes
JC_SCANLINE_RGB (compress_info_ptr cinfo, 
	int * scanline, JSAMPROW ptr0, JSAMPROW ptr1, JSAMPROW ptr2)
#else
JC_SCANLINE_RGB (cinfo, scanline, ptr0, ptr1, ptr2)
	compress_info_ptr cinfo; 
	int * scanline; 
	JSAMPROW ptr0; 
	JSAMPROW ptr1; 
	JSAMPROW ptr2;
#endif	/* NeedFunctionPrototypes */
{
  JSAMPROW pixel_row[3];
  int row, rows_this_time;
  int status;

  row = *scanline;	
  if (cinfo->XIErestart == XIE_RNUL) {
    if (cinfo->cur_pixel_row <= 0)	{
      rows_this_time = (int) MIN((long) cinfo->rows_in_mem,
                    cinfo->image_height - row);
      if (rows_this_time <= 0)
        return (XIE_NRML);
      cinfo->rows_this_time = rows_this_time;
    }
    pixel_row[0] = ptr0;
    pixel_row[1] = ptr1;
    pixel_row[2] = ptr2;
  }
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
    if ((status = jcXIE_get (cinfo, row, pixel_row)) < 0) {
      if (status == XIE_ERR)
      	return(XIE_ERR);
      cinfo->XIErestart = XIE_RMCU;	/* set restart status */
      return(XIE_OUT);	
    }
  }
  
  return (XIE_NRML);
}

GLOBAL int
#if NeedFunctionPrototypes
JC_SCANLINE_GRAY (compress_info_ptr cinfo, int * scanline, JSAMPROW ptr0)
#else
JC_SCANLINE_GRAY (cinfo, scanline, ptr0)
	compress_info_ptr cinfo; 
	int * scanline; 
	JSAMPROW ptr0;
#endif	/* NeedFunctionPrototypes */
{
  JSAMPROW pixel_row[3];
  int row, rows_this_time;
  int status;

  row = *scanline;	
  if (cinfo->XIErestart == XIE_RNUL) {
    if (cinfo->cur_pixel_row <= 0)	{
      rows_this_time = (int) MIN((long) cinfo->rows_in_mem,
                    cinfo->image_height - row);
      if (rows_this_time <= 0)
        return (XIE_NRML);
      cinfo->rows_this_time = rows_this_time;
    }
    pixel_row[0] = ptr0;
    pixel_row[1] = 0;
    pixel_row[2] = 0;
  }
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
    if ((status = jcXIE_get (cinfo, row, pixel_row)) < 0) {
      if (status == XIE_ERR)
      	return(XIE_ERR);
      cinfo->XIErestart = XIE_RMCU;	/* set restart status */
      return(XIE_OUT);	
    }
  }
  
  return (XIE_NRML);
}