1 /* -*-C-*-
2 ******************************************************************************
3 *
4 * File: enctile.c
5 * RCS: $Header: /ImageMagick/delegates/fpx/jpeg/enctile.c,v 1.4 2002/03/02 22:59:59 bfriesen Exp $
6 * Description: JPEG encodes a tile of data
7 * Author: Gregory S. Yovanof
8 * Created: Tue Jun 13 11:04:22 1995
9 * Initial Source Release: Wed Jan 17 1996
10 * Language: C
11 * Package: Hewlett-Packard JPEG Encoder/Decoder
12 *
13 * Copyright (c) 1999 Digital Imaging Group, Inc.
14 * For conditions of distribution and use, see copyright notice
15 * in Flashpix.h
16 *
17 ******************************************************************************
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "jpegconf.h"
23 #include "enctile.h"
24 #include "encoder.h"
25
26 #include "ejpeg.h"
27 #include "ebuffer.h"
28 #include "eparser.h"
29 #include "win_dct.h"
30 #include "huffman.h"
31 #include "fpxmem.h"
32
33 #define SHIFT 128
34 #define BOUND 1023
35 #define Q_PRECISION 50
36 #define MAX_Q 255
37
38 int izigzag_index[] =
39 {
40 0, 1, 8, 16, 9, 2, 3, 10,
41 17, 24, 32, 25, 18, 11, 4, 5,
42 12, 19, 26, 33, 40, 48, 41, 34,
43 27, 20, 13, 6, 7, 14, 21, 28,
44 35, 42, 49, 56, 57, 50, 43, 36,
45 29, 22, 15, 23, 30, 37, 44, 51,
46 58, 59, 52, 45, 38, 31, 39, 46,
47 53, 60, 61, 54, 47, 55, 62, 63};
48
49 static int csize[] = {
50 0,
51 1,
52 2, 2,
53 3, 3, 3, 3,
54 4, 4, 4, 4, 4, 4, 4, 4,
55 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
56 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
57 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
58 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
59 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
60 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
61 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
62 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
63 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
64 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
65 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
66
67
68 #define ENCODE_HUFFMAN(val, huffman) \
69 EB_Write_Bits(huffman->ehufcode[val], huffman->ehufsize[val])
70
71 #ifdef DEBUG
72 /*
73 * Programming Error (i.e. something wrong with the program).
74 */
debug(message)75 void debug(message)
76 char *message;
77 {
78 fprintf(stderr,"**** SYSTEM ERROR: %s\n", message);
79 exit(1);
80 };
81 #endif /* DEBUG */
82
83 static void
Scale_Char_Matrix(int numerator,int denominator,unsigned char * in,int * out)84 Scale_Char_Matrix(int numerator, int denominator, unsigned char *in, int *out)
85 {
86 int i, val, inp;
87 for (i = 0; i < 64; i++) {
88 inp = (int) *in++;
89 val = (inp * numerator) / denominator;
90 if (val < 1) {
91 *out++ = 1;
92 } else if (val > 255) {
93 *out++ = 255;
94 } else {
95 *out++ = val;
96 }
97 }
98 }
99
Clear_Last_DC(JPEG_STRUCT * jpeg_struct)100 void Clear_Last_DC(JPEG_STRUCT *jpeg_struct)
101 {
102 jpeg_struct->en_last_dc[0] = jpeg_struct->en_last_dc[1] =
103 jpeg_struct->en_last_dc[2] = jpeg_struct->en_last_dc[3] = 0;
104 }
105
EN_Encode_DC(int dc_val,int comp,HUFFMAN_TABLE * huffman,JPEG_STRUCT * jpeg_struct)106 void EN_Encode_DC(int dc_val, int comp, HUFFMAN_TABLE *huffman,
107 JPEG_STRUCT *jpeg_struct)
108 {
109 int diff, s, cofac;
110
111 cofac = ((diff = dc_val - jpeg_struct->en_last_dc[comp]) < 0) ? -diff : diff;
112 s = (cofac < 256) ? csize[cofac] : csize[cofac >> 8] + 8;
113 jpeg_struct->en_last_dc[comp] = dc_val;
114
115 /* Encode size */
116 ENCODE_HUFFMAN(s, huffman);
117
118 /* Encode difference */
119 if (diff < 0)
120 EB_Write_Bits(diff-1, s);
121 else
122 EB_Write_Bits(diff , s);
123 }
124
125
EN_Encode_Block(int * block,int comp,HUFFMAN_TABLE * dcHuffman,HUFFMAN_TABLE * acHuffman,int * quant,JPEG_STRUCT * jpeg_struct)126 void EN_Encode_Block(int *block,
127 int comp, /* image component number */
128 HUFFMAN_TABLE *dcHuffman,
129 HUFFMAN_TABLE *acHuffman,
130 int *quant,
131 JPEG_STRUCT *jpeg_struct)
132 {
133 int r, data, ssss, *zig;
134 #define i comp
135
136 Dct(block);
137
138 /* Quantize and Encode DC component. No need to check Bound, since
139 data must be 11-bit signed integer, i.e. [-1023, 1023] */
140
141 data = SCALEM((long)block[0]*(*quant++));
142 EN_Encode_DC(data, comp, dcHuffman, jpeg_struct);
143
144 /* Quantize, Zigzag, then Encode AC coefficients. No Bound - see above */
145 for (zig = izigzag_index + 1, r = 0, i = 64; --i > 0;) {
146 if ((data = SCALEM((long)block[*zig++]*(*quant++))) == 0) {
147 if (i == 1) {
148 ENCODE_HUFFMAN(0, acHuffman);
149 break;
150 }
151 r++; /* Increment run-length of zeroes */
152 } else if (data > 0) {
153 while (r > 15) { /* If run-length > 15 -> runlen extension */
154 ENCODE_HUFFMAN(240, acHuffman);
155 r -= 16;
156 }
157 if (data < 256) { /* Find code byte */
158 r = (r << 4) + (ssss = csize[data]);
159 } else {
160 r = (r << 4) + (ssss = csize[data >> 8] + 8);
161 }
162 ENCODE_HUFFMAN(r, acHuffman); /* Encode RLE code */
163 EB_Write_Bits (data, ssss); /* Followed by sig. bits */
164 r = 0;
165 } else {
166 while (r > 15) { /* If run-length > 15 -> runlen extension */
167 ENCODE_HUFFMAN(240, acHuffman);
168 r -= 16;
169 }
170 if (data > -256) { /* Find code byte */
171 r = (r << 4) + (ssss = csize[-data]);
172 } else {
173 r = (r << 4) + (ssss = csize[(-data) >> 8] + 8);
174 }
175 ENCODE_HUFFMAN(r, acHuffman); /* Encode RLE code */
176 EB_Write_Bits (data-1, ssss); /* Followed by sig. bits */
177 r = 0;
178 }
179 }
180 #undef i
181 }
182
183
184 /*
185 * Return 0 if successful, -1 if EOF
186 */
EN_Encode_Scan_Gray(unsigned char * buf,int width,int height,JPEG_STRUCT * jpeg_struct)187 int EN_Encode_Scan_Gray(unsigned char *buf, int width, int height,
188 JPEG_STRUCT *jpeg_struct )
189 {
190 /* Routine to create macroblocks for Grayscale images
191
192 buf: points to the input data
193 width and heigh are already in multiples of 8; i.e. data
194 is already padded.
195 Some error checking should be done at the higher level.
196 */
197 int j, i, k, nhblocks;
198 int t, skip;
199 /* HANDLE lBlock; */
200 int block[64];
201 int *iptr;
202 unsigned char *buf_ptr;
203
204 Clear_Last_DC(jpeg_struct);
205
206 nhblocks = width/8;
207 skip = width -8;
208
209 for (i=0; i < nhblocks; i++) {
210 for(j=0; j < nhblocks; j++) {
211 iptr = block;
212 buf_ptr = buf + i*8*width + j*8;
213 for(k = 8; k > 0; k--) {
214 for(t = 8; t > 0; t--) {
215 *iptr++ = *buf_ptr++ - SHIFT;
216 }
217 buf_ptr = buf_ptr + skip;
218 }
219 EN_Encode_Block(block,0, &jpeg_struct->huff[0],&jpeg_struct->huff[1],
220 jpeg_struct->luminanceQuantization, jpeg_struct);
221 } /* end on j */
222 } /* end of vertical blocks (i) */
223 return(0);
224 }
225
226
227 /*
228 * Return 0 if successful, -1 if EOF
229 */
EN_Encode_Scan_Color11(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)230 int EN_Encode_Scan_Color11(unsigned char *buf,
231 int width,
232 int height,
233 int interleaved,
234 JPEG_STRUCT *jpeg_struct)
235 /*
236 * Routine to generate macroblocks for NON-subsampled data (1:1)
237 * buf: a pointer to the data (2 color components)
238 * if interleaved=1, data is interleaved (C1C2 C1C2 ...)
239 * else it is not (C1C1 ... C1C2C2 ... C2).
240 *
241 * width and heigh are already in multiples of 8; i.e. data
242 * is already padded.
243 * Some error checking should be done at the higher level.
244 */
245 {
246 unsigned char *buf_ptr;
247 int *block1, *block2;
248 int *p1, *p2, i, j, k, t, skip, block_row_skip;
249 int nvMCU, nhMCU; /* number of FULL MCU vert. and hori. */
250 unsigned char *c1, *c2;
251
252 Clear_Last_DC(jpeg_struct);
253
254 nvMCU = (height)/8;
255 nhMCU = (width)/8;
256
257 block1 = block2 = NULL; /* CHG_JPEG_MEM_FIX - used for allocation checking */
258 block1 = (int *) FPX_malloc(64 * sizeof(int));
259 block2 = (int *) FPX_malloc(64 * sizeof(int));
260
261 if(interleaved == 1) {
262 skip = (2*width) - 16;
263 block_row_skip = 2*width*8;
264 for (i = 0; i < nvMCU; i++) {
265 for (j = 0; j < nhMCU; j++) {
266 /* Construct 2 blocks within each MCU */
267
268 p1 = block1;
269 p2 = block2;
270 buf_ptr = buf + i*block_row_skip + j*16;
271 for (k = 8; k > 0; k--) {
272 for (t = 8; t > 0; t--) {
273 *p1++ = *buf_ptr++ - SHIFT;
274 *p2++ = *buf_ptr++ - SHIFT;
275 }
276 buf_ptr = buf_ptr + skip;
277 }
278
279 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
280 jpeg_struct->luminanceQuantization,jpeg_struct);
281
282 EN_Encode_Block(block2, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
283 jpeg_struct->chrominanceQuantization,jpeg_struct);
284 } /* end of j */
285 } /* end of i */
286 } /* end of interleaved data case */
287 else {
288 /************************/
289 /* Non interleaved data */
290 /************************/
291 skip = width - 8;
292 block_row_skip = width*8;
293 for (i = 0; i < nvMCU; i++) {
294 for (j = 0; j < nhMCU; j++) {
295 /* Construct 2 blocks within each MCU */
296
297 p1 = block1;
298 p2 = block2;
299 c1 = buf + i*block_row_skip + j*8;
300 c2 = c1 + width * height;
301 for (k = 8; k > 0; k--) {
302 for (t = 8; t > 0; t--) {
303 *p1++ = *c1++ - SHIFT;
304 *p2++ = *c2++ - SHIFT;
305 }
306 c1 = c1 + skip;
307 c2 = c2 + skip;
308 }
309 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
310 jpeg_struct->luminanceQuantization,jpeg_struct);
311 EN_Encode_Block(block2, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
312 jpeg_struct->chrominanceQuantization,jpeg_struct);
313 } /* end of j */
314 } /* end of i */
315 } /* end of else loop */
316
317 FPX_free(block1);
318 FPX_free(block2);
319
320 return(0);
321 }
322
323
324 /*
325 * Return 0 if successful, -1 if EOF
326 */
EN_Encode_Scan_Color111(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)327 int EN_Encode_Scan_Color111(unsigned char *buf,
328 int width,
329 int height,
330 int interleaved,
331 JPEG_STRUCT *jpeg_struct)
332 /*
333 * Routine to generate macroblocks for NON-subsampled data (1:1:1)
334 * buf: a pointer to the data (3 color components)
335 * if interleaved=1, data is interleaved (RGB RGB ..)
336 * else it is not.
337 *
338 * width and heigh are already in multiples of 8; i.e. data
339 * is already padded.
340 * Some error checking should be done at the higher level.
341 */
342 {
343 unsigned char *buf_ptr;
344 int *block1, *block2, *block3;
345 int *p1, *p2, *p3, i, j, k, t, skip, block_row_skip;
346 int nvMCU, nhMCU; /* number of FULL MCU vert. and hori. */
347 unsigned char *c1, *c2, *c3;
348
349 block1 = block2 = block3 = NULL; /* CHG_JPEG_MEM_FIX - used for allocation checking */
350 block1 = (int *) FPX_malloc(64 * sizeof(int));
351 block2 = (int *) FPX_malloc(64 * sizeof(int));
352 block3 = (int *) FPX_malloc(64 * sizeof(int));
353
354 /* CHG_JPEG_MEM_FIX - added the following allocation check */
355 if ((block1 == 0) || (block2 == 0) || (block3 == 0)) {
356 if (block1) FPX_free(block1);
357 if (block2) FPX_free(block2);
358 return (EJPEG_ERROR_MEM);
359 }
360
361 Clear_Last_DC(jpeg_struct);
362
363 nvMCU = (height)/8;
364 nhMCU = (width)/8;
365
366 if(interleaved == 1) {
367 skip = (3*width) - 24;
368 block_row_skip = 3*width*8;
369 for (i = 0; i < nvMCU; i++) {
370 for (j = 0; j < nhMCU; j++) {
371 /* Construct 3 blocks within each MCU */
372
373 p1 = block1;
374 p2 = block2;
375 p3 = block3;
376 buf_ptr = buf + i*block_row_skip + j*24;
377 for (k = 8; k > 0; k--) {
378 for (t = 8; t > 0; t--) {
379 *p1++ = *buf_ptr++ - SHIFT;
380 *p2++ = *buf_ptr++ - SHIFT;
381 *p3++ = *buf_ptr++ - SHIFT;
382 }
383 buf_ptr = buf_ptr + skip;
384 }
385 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
386 jpeg_struct->luminanceQuantization,jpeg_struct);
387 EN_Encode_Block(block2, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
388 jpeg_struct->chrominanceQuantization,jpeg_struct);
389 EN_Encode_Block(block3, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
390 jpeg_struct->tbl3_Quantization,jpeg_struct);
391 } /* end of j */
392 } /* end of i */
393 } /* end of interleaved data case */
394 else {
395 /************************/
396 /* Non interleaved data */
397 /************************/
398 skip = width - 8;
399 block_row_skip = width*8;
400 for (i = 0; i < nvMCU; i++) {
401 for (j = 0; j < nhMCU; j++) {
402 /* Construct 3 blocks within each MCU */
403
404 p1 = block1;
405 p2 = block2;
406 p3 = block3;
407 c1 = buf + i*block_row_skip + j*8;
408 c2 = c1 + width * height;
409 c3 = c2 + width * height;
410 for (k = 8; k > 0; k--) {
411 for (t = 8; t > 0; t--) {
412 *p1++ = *c1++ - SHIFT;
413 *p2++ = *c2++ - SHIFT;
414 *p3++ = *c3++ - SHIFT;
415 }
416 c1 = c1 + skip;
417 c2 = c2 + skip;
418 c3 = c3 + skip;
419 }
420 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
421 jpeg_struct->luminanceQuantization,jpeg_struct);
422 EN_Encode_Block(block2, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
423 jpeg_struct->chrominanceQuantization,jpeg_struct);
424 EN_Encode_Block(block3, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
425 jpeg_struct->tbl3_Quantization,jpeg_struct);
426 } /* end of j */
427 } /* end of i */
428 } /* end of else loop */
429
430 FPX_free(block1);
431 FPX_free(block2);
432 FPX_free(block3);
433
434 return(0);
435 }
436
437
438 /*
439 * Return 0 if successful, -1 if EOF
440 */
EN_Encode_Scan_Color1111(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)441 int EN_Encode_Scan_Color1111(unsigned char *buf,
442 int width, int height,
443 int interleaved,
444 JPEG_STRUCT *jpeg_struct)
445 /*
446 * Routine to generate macroblocks for NON-subsampled data (1:1:1:1)
447 * buf: a pointer to the data (4 color components)
448 * if interleaved=1, data is interleaved (c1c2c3c4 c1c2c3c4 ...)
449 * else it is not (c1c1...c1c2c2...c2c3c3...c3c4c4...c4).
450 *
451 * width and heigh are already in multiples of 8; i.e. data
452 * is already padded.
453 * Some error checking should be done at the higher level.
454 */
455 {
456 unsigned char *buf_ptr;
457 int *block1, *block2, *block3, *block4;
458 int *p1, *p2, *p3, *p4, i, j, k, t, skip, block_row_skip;
459 int nvMCU, nhMCU; /* number of FULL MCU vert. and hori. */
460 unsigned char *c1, *c2, *c3, *c4;
461
462 block1 = block2 = block3 = block4 = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
463 block1 = (int *) FPX_malloc(64 * sizeof(int));
464 block2 = (int *) FPX_malloc(64 * sizeof(int));
465 block3 = (int *) FPX_malloc(64 * sizeof(int));
466 block4 = (int *) FPX_malloc(64 * sizeof(int));
467
468 /* CHG_JPEG_MEM_FIX - added the following allocation check */
469 if ((block1 == 0) || (block2 == 0) || (block3 == 0) || (block4 == 0)) {
470 if (block1) FPX_free(block1);
471 if (block2) FPX_free(block2);
472 if (block3) FPX_free(block3);
473 return (EJPEG_ERROR_MEM);
474 }
475
476 Clear_Last_DC(jpeg_struct);
477
478 nvMCU = (height)/8;
479 nhMCU = (width)/8;
480
481 if(interleaved == 1) {
482 skip = (4*width) - 32;
483 block_row_skip = 4*width*8;
484 for (i = 0; i < nvMCU; i++) {
485 for (j = 0; j < nhMCU; j++) {
486 /* Construct 4 blocks within each MCU */
487
488 p1 = block1;
489 p2 = block2;
490 p3 = block3;
491 p4 = block4;
492 buf_ptr = buf + i*block_row_skip + j*32;
493
494 /* Should this run from seven to zero? Will this cause a whammy? KAW */
495 for (k = 8; k > 0; k--) {
496 for (t = 8; t > 0; t--) {
497 *p1++ = *buf_ptr++ - SHIFT;
498 *p2++ = *buf_ptr++ - SHIFT;
499 *p3++ = *buf_ptr++ - SHIFT;
500 *p4++ = *buf_ptr++ - SHIFT;
501 }
502 buf_ptr = buf_ptr + skip;
503 }
504 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
505 jpeg_struct->luminanceQuantization,jpeg_struct);
506 EN_Encode_Block(block2, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
507 jpeg_struct->chrominanceQuantization,jpeg_struct);
508 EN_Encode_Block(block3, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
509 jpeg_struct->tbl3_Quantization,jpeg_struct);
510 EN_Encode_Block(block4, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
511 jpeg_struct->tbl4_Quantization,jpeg_struct);
512 } /* end of j */
513 } /* end of i */
514 } /* end of interleaved data case */
515 else {
516 /************************/
517 /* Non interleaved data */
518 /************************/
519 skip = width - 8;
520 block_row_skip = width*8;
521 for (i = 0; i < nvMCU; i++) {
522 for (j = 0; j < nhMCU; j++) {
523 /* Construct 4 blocks within each MCU */
524
525 p1 = block1;
526 p2 = block2;
527 p3 = block3;
528 p4 = block4;
529 c1 = buf + i*block_row_skip + j*8;
530 c2 = c1 + width * height;
531 c3 = c2 + width * height;
532 c4 = c3 + width * height;
533 for (k = 8; k > 0; k--) {
534 for (t = 8; t > 0; t--) {
535 *p1++ = *c1++ - SHIFT;
536 *p2++ = *c2++ - SHIFT;
537 *p3++ = *c3++ - SHIFT;
538 *p4++ = *c4++ - SHIFT;
539 }
540 c1 = c1 + skip;
541 c2 = c2 + skip;
542 c3 = c3 + skip;
543 c4 = c4 + skip;
544 }
545 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
546 jpeg_struct->luminanceQuantization,jpeg_struct);
547 EN_Encode_Block(block2, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
548 jpeg_struct->chrominanceQuantization,jpeg_struct);
549 EN_Encode_Block(block3, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
550 jpeg_struct->tbl3_Quantization,jpeg_struct);
551 EN_Encode_Block(block4, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
552 jpeg_struct->tbl4_Quantization,jpeg_struct);
553 } /* end of j */
554 } /* end of i */
555 } /* end of else loop */
556
557 FPX_free(block1);
558 FPX_free(block2);
559 FPX_free(block3);
560 FPX_free(block4);
561 return(0);
562 }
563
564
565 /*
566 * Return 0 if successful, -1 if EOF
567 */
EN_Encode_Scan_Color4114(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)568 int EN_Encode_Scan_Color4114(unsigned char *buf,
569 int width, int height,
570 int interleaved,
571 JPEG_STRUCT *jpeg_struct)
572 {
573 /*
574 * Routine to generate macroblocks for subsampled data in 4:1:1:4 format
575 * i.e., 4-channel data with the two chroma channels subsampled by 2x
576 * in both dimensions. An example of such an image is a YCrCb-A image
577 * with the Cr-Cb channels subsampled by 2x.
578 *
579 * <buf>: a pointer to the data (4 color components)
580 * if interleaved=1, data is interleaved
581 * (c1c1c1c1 c2 c3 c4c4c4c4, c1c1c1c1 c2 c3 c4c4c4c4, ...)
582 * else it is not ( data is in scan-line by scan-line format).
583 *
584 * width and heigh are already in multiples of 16; i.e. data
585 * is already padded and subsampled as necessary.
586 *
587 * Some error checking should be done at the higher level
588 * for the width height dimensions.
589 */
590 unsigned char *buf_ptr, *c1_ptr, *c2_ptr, *c3_ptr;
591 int *block1, *block2, *block3, *block4;
592 int *p1, *p2, *p3, *p4, i, j, k, l, t, skip, skip2,skip_row_data;
593 int *p1_2, *p2_2;
594 int nvMCU, nhMCU; /* number of FULL MCU vert. and hor. */
595 int *block5, *block6;
596 int *block7, *block8, *block9, *block10;
597 int *p5, *p6, *p5_2, *p6_2;
598 unsigned char *c4_ptr;
599
600 block1 = block2 = block3 = block4 = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
601 block5 = block6 = NULL;
602 block7 = block8 = block9 = block10 = NULL;
603
604 block1 = (int *) FPX_malloc(64 * sizeof(int)); /* Luma block */
605 block2 = (int *) FPX_malloc(64 * sizeof(int)); /* Luma block */
606 block3 = (int *) FPX_malloc(64 * sizeof(int)); /* Luma block */
607 block4 = (int *) FPX_malloc(64 * sizeof(int)); /* Luma block */
608
609 if ((block1 == 0) || (block2 == 0) || (block3 == 0) || (block4 == 0))
610 goto Exit; /* CHG_JPEG_MEM_FIX - Added allocation test */
611
612 block5 = (int *) FPX_malloc(64 * sizeof(int)); /* 1rst-chroma block */
613 block6 = (int *) FPX_malloc(64 * sizeof(int)); /* 2nd-chroma block */
614
615 if ((block5 == 0) || (block6 == 0))
616 goto Exit; /* CHG_JPEG_MEM_FIX - Added allocation test */
617
618 block7 = (int *) FPX_malloc(64 * sizeof(int)); /* Alpha channel */
619 block8 = (int *) FPX_malloc(64 * sizeof(int)); /* Alpha channel */
620 block9 = (int *) FPX_malloc(64 * sizeof(int)); /* Alpha channel */
621 block10 = (int *) FPX_malloc(64 * sizeof(int)); /* Alpha channel */
622
623 if ((block7 == 0) || (block8 == 0) || (block9 == 0) || (block10 == 0))
624 goto Exit; /* CHG_JPEG_MEM_FIX - Added allocation test */
625
626 Clear_Last_DC(jpeg_struct);
627
628 nvMCU = height/16;
629 nhMCU = width/16;
630
631 /* Interleaved Data */
632 if (interleaved == 1) {
633 /* if interleaved, a line has (width/2 * 10) pixels */
634 skip = width*5 - 80; /* 80 = 8 subsampled blocks * 10 bytes/block */
635 skip_row_data = 8*5*width; /* One 16x16 block-row bytes */
636 for (i=0; i < nvMCU; i++) {
637 for (j=0; j < nhMCU; j++) {
638 buf_ptr = buf + i*skip_row_data + j*80;
639
640 p3 = block5;
641 p4 = block6 ; /* chroma blocks */
642 for (l=2; l>0; l--) { /* Top/Bottom row of Luma-blocks */
643 if (l==2) {
644 p1 = block1;
645 p2 = block2; /* left-right luma blocks, odd rows */
646 p1_2 = p1+8;
647 p2_2 = p2+8; /* even rows */
648 p5 = block7;
649 p6 = block8; /* left-right luma blocks, odd rows */
650 p5_2 = p5+8;
651 p6_2 = p6+8; /* even rows */
652 } else {
653 p1 = block3;
654 p2 = block4; /* left-right luma blocks, odd rows */
655 p1_2 = p1+8;
656 p2_2 = p2+8; /* even rows */
657 p5 = block9;
658 p6 = block10; /* left-right luma blocks, odd rows */
659 p5_2 = p5+8;
660 p6_2 = p6+8; /* even rows */
661 }
662
663 for(k=4; k > 0; k--) { /* vertical subsampled grid */
664 for(t=4; t > 0; t--) { /* hor. subsampled grid Left-Block */
665 *p1++ = *buf_ptr++ - SHIFT;
666 *p1++ = *buf_ptr++ - SHIFT;
667 *p1_2++ = *buf_ptr++ - SHIFT;
668 *p1_2++ = *buf_ptr++ - SHIFT;
669 *p3++ = *buf_ptr++ - SHIFT;
670 *p4++ = *buf_ptr++ - SHIFT;
671 *p5++ = *buf_ptr++ - SHIFT;
672 *p5++ = *buf_ptr++ - SHIFT;
673 *p5_2++ = *buf_ptr++ - SHIFT;
674 *p5_2++ = *buf_ptr++ - SHIFT;
675 } /* t-loop */
676 for(t=4; t > 0; t--) { /* hor. subsampled grid Right-Block */
677 *p2++ = *buf_ptr++ - SHIFT;
678 *p2++ = *buf_ptr++ - SHIFT;
679 *p2_2++ = *buf_ptr++ - SHIFT;
680 *p2_2++ = *buf_ptr++ - SHIFT;
681 *p3++ = *buf_ptr++ - SHIFT;
682 *p4++ = *buf_ptr++ - SHIFT;
683 *p6++ = *buf_ptr++ - SHIFT;
684 *p6++ = *buf_ptr++ - SHIFT;
685 *p6_2++ = *buf_ptr++ - SHIFT;
686 *p6_2++ = *buf_ptr++ - SHIFT;
687 } /* t-loop */
688 p1 = p1 + 8;
689 p2 = p2 +8;
690 p1_2 = p1_2 + 8;
691 p2_2 = p2_2 +8;
692 p5 = p5 + 8;
693 p6 = p6 +8;
694 p5_2 = p5_2 + 8;
695 p6_2 = p6_2 +8;
696 buf_ptr = buf_ptr + skip;
697 } /* k-loop */
698 } /* end on l */
699
700 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
701 jpeg_struct->luminanceQuantization,jpeg_struct);
702 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
703 jpeg_struct->luminanceQuantization,jpeg_struct);
704 EN_Encode_Block(block3, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
705 jpeg_struct->luminanceQuantization,jpeg_struct);
706 EN_Encode_Block(block4, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
707 jpeg_struct->luminanceQuantization,jpeg_struct);
708
709 EN_Encode_Block(block5, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
710 jpeg_struct->chrominanceQuantization,jpeg_struct);
711 EN_Encode_Block(block6, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
712 jpeg_struct->tbl3_Quantization,jpeg_struct);
713
714 EN_Encode_Block(block7, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
715 jpeg_struct->tbl4_Quantization,jpeg_struct);
716 EN_Encode_Block(block8, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
717 jpeg_struct->tbl4_Quantization,jpeg_struct);
718 EN_Encode_Block(block9, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
719 jpeg_struct->tbl4_Quantization,jpeg_struct);
720 EN_Encode_Block(block10, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
721 jpeg_struct->tbl4_Quantization,jpeg_struct);
722
723 } /* end on j */
724 } /* end on i */
725 } /* end of interleaved */
726 /************************/
727 /* Non-Interleaved Data */
728 /************************/
729 else {
730 skip = width - 16;
731 skip2 = (width/2) - 8;
732 for(i = 0; i < nvMCU ; i++) {
733 for(j = 0; j < nhMCU ; j++) {
734
735 p3 = block5;
736 p4 = block6 ; /* chroma blocks */
737 c1_ptr = buf + j*16;
738 c2_ptr = buf + width*height + j*8;
739 c3_ptr = c2_ptr + (width*height)/4;
740 c4_ptr = c3_ptr + (width*height)/4;
741
742 for (l=2; l>0; l--) {
743 if (l==2) {
744 p1 = block1;
745 p2 = block2; /* left-right luma blocks */
746 p5 = block7;
747 p6 = block8; /* left-right alpha blocks */
748 } else {
749 p1 = block3;
750 p2 = block4; /* left-right luma blocks */
751 p5 = block9;
752 p6 = block10; /* left-right alpha blocks */
753 }
754 for(k=8; k > 0; k--) {
755 /* Top */
756 for(t=8; t > 0; t--) {
757 *p1++ = *c1_ptr++ - SHIFT;
758 *p5++ = *c4_ptr++ - SHIFT;
759 } /* t-loop */
760 /* bottom */
761 for(t=8; t > 0; t--) {
762 *p2++ = *c1_ptr++ - SHIFT;
763 *p6++ = *c4_ptr++ - SHIFT;
764 } /* t-loop */
765 /* buf_ptr = buf_ptr + skip; */
766 } /* k-loop */
767 } /* end on l */
768 /* Fill-up the chroma 8x8 blocks */
769
770 for(k=8; k> 0; k--){
771 for(t=8; t > 0; t--) {
772 *p3++ = *c2_ptr++ - SHIFT;
773 *p4++ = *c3_ptr++ - SHIFT;
774 }
775 c2_ptr = c2_ptr + skip2;
776 c3_ptr = c3_ptr + skip2;
777 } /* end on k*/
778
779 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
780 jpeg_struct->luminanceQuantization,jpeg_struct);
781 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
782 jpeg_struct->luminanceQuantization,jpeg_struct);
783 EN_Encode_Block(block3, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
784 jpeg_struct->luminanceQuantization,jpeg_struct);
785 EN_Encode_Block(block4, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
786 jpeg_struct->luminanceQuantization,jpeg_struct);
787
788 EN_Encode_Block(block5, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
789 jpeg_struct->chrominanceQuantization,jpeg_struct);
790 EN_Encode_Block(block6, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
791 jpeg_struct->tbl3_Quantization,jpeg_struct);
792
793 EN_Encode_Block(block7, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
794 jpeg_struct->tbl4_Quantization,jpeg_struct);
795 EN_Encode_Block(block8, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
796 jpeg_struct->tbl4_Quantization,jpeg_struct);
797 EN_Encode_Block(block9, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
798 jpeg_struct->tbl4_Quantization,jpeg_struct);
799 EN_Encode_Block(block10, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
800 jpeg_struct->tbl4_Quantization,jpeg_struct);
801
802 } /* end on j */
803 } /* end on i */
804 } /* end of non-interleaved data. */
805
806 Exit:
807 FPX_free(block1);
808 FPX_free(block2);
809 FPX_free(block3);
810 FPX_free(block4);
811 FPX_free(block5);
812 FPX_free(block6);
813 FPX_free(block7);
814 FPX_free(block8);
815 FPX_free(block9);
816 FPX_free(block10);
817
818 return(0);
819 }
820
821
EN_Encode_Scan_Color422(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)822 int EN_Encode_Scan_Color422(unsigned char *buf,
823 int width, int height,
824 int interleaved,
825 JPEG_STRUCT *jpeg_struct)
826 {
827 /*
828 * Routine to generate macroblocks for subsampled data in 4:2:2
829 *
830 * <buf>: a pointer to the data (3 color components)
831 * if interleaved=1, data is interleaved (c1c1 c2 c3, c1c1 c2 c3)
832 * else it is not( data is in scan-line by scan -line format).
833 *
834 * width and height are already in multiples of 8/16; i.e. data
835 * is already padded and subsampled as necessary.
836 *
837 * Some error checking should be done at the higher level
838 * for the width height dimensions.
839 */
840 unsigned char *buf_ptr;
841 unsigned char *c1_ptr, *c2_ptr, *c3_ptr;
842 int *block1, *block2, *block3, *block4;
843 int *p1, *p2, *p3, *p4, i, j, k, t;
844 int skip,skip2,skip_row_data, skip_row_data1,skip_row_data2;
845 int nvMCU, nhMCU; /* number of FULL MCU vert. and hor. */
846
847 block1 = block2 = block3 = block4 = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
848 block1 = (int *) FPX_malloc(64 * sizeof(int));
849 block2 = (int *) FPX_malloc(64 * sizeof(int));
850 block3 = (int *) FPX_malloc(64 * sizeof(int));
851 block4 = (int *) FPX_malloc(64 * sizeof(int));
852
853 /* CHG_JPEG_MEM_FIX - added the following allocation check */
854 if ((block1 == 0) || (block2 == 0) || (block3 == 0) || (block4 == 0)) {
855 if (block1) FPX_free(block1);
856 if (block2) FPX_free(block2);
857 if (block3) FPX_free(block3);
858 return (EJPEG_ERROR_MEM);
859 }
860
861 Clear_Last_DC(jpeg_struct);
862
863 nhMCU = width/16;
864 nvMCU = height/8;
865
866 /* Interleaved Data */
867 if (interleaved == 1) {
868 /* if interleaved, a line has (width/2 * 4) pixels (1byte/pixel) */
869 /* A subsampled symbol has 4 bytes. */
870 skip = width*2 - 32; /* 32 = 8 subsampled symbols x 4bytes/ss-symbol */
871 skip_row_data = 8*4*width/2; /* One 8x16 block-row bytes */
872 for (i=0; i < nvMCU; i++) {
873 for (j=0; j < nhMCU; j++) {
874 p3 = block3;
875 p4 = block4 ; /* chroma blocks */
876 buf_ptr = buf + i*skip_row_data + j*32;
877
878 p1 = block1;
879 p2 = block2; /* left-right blocks */
880 for(k=8; k > 0; k--) {
881 for(t=4; t > 0; t--) {
882 *p1++ = *buf_ptr++ - SHIFT;
883 *p1++ = *buf_ptr++ - SHIFT;
884 *p3++ = *buf_ptr++ - SHIFT;
885 *p4++ = *buf_ptr++ - SHIFT;
886 } /* t-loop */
887 for(t=4; t > 0; t--) {
888 *p2++ = *buf_ptr++ - SHIFT;
889 *p2++ = *buf_ptr++ - SHIFT;
890 *p3++ = *buf_ptr++ - SHIFT;
891 *p4++ = *buf_ptr++ - SHIFT;
892 } /* t-loop */
893 buf_ptr = buf_ptr + skip;
894 } /* k-loop */
895 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
896 jpeg_struct->luminanceQuantization,jpeg_struct);
897 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
898 jpeg_struct->luminanceQuantization,jpeg_struct);
899 EN_Encode_Block(block3, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
900 jpeg_struct->chrominanceQuantization,jpeg_struct);
901 EN_Encode_Block(block4, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
902 jpeg_struct->tbl3_Quantization,jpeg_struct);
903
904 } /* end on j */
905 } /* end on i */
906 } /* end of interleaved */
907 /************************/
908 /* Non-Interleaved Data */
909 /************************/
910 else {
911 skip = width - 16;
912 skip2 = (width/2) - 8;
913 skip_row_data1 = 8*width; /* a row of luma-blocks */
914 skip_row_data2 = 8*width/2; /* a row of chroma-blocks */
915 for(i = 0; i < nvMCU ; i++) {
916 for(j = 0; j < nhMCU ; j++) {
917 p3 = block3;
918 p4 = block4 ; /* chroma blocks */
919 c1_ptr = buf + i*skip_row_data1 + j*16;
920 c2_ptr = buf + width*height + i*skip_row_data2 + j*8;
921 c3_ptr = c2_ptr + (width*height)/4;
922
923 p1 = block1;
924 p2 = block2; /* left-right blocks */
925 for(k=8; k > 0; k--) {
926 for(t=8; t > 0; t--) { /* left block */
927 *p1++ = *c1_ptr++ - SHIFT;
928 } /* t-loop */
929
930 for(t=8; t > 0; t--) { /* tight block */
931 *p2++ = *c1_ptr++ - SHIFT;
932 } /* t-loop */
933 c1_ptr += skip;
934 } /* k-loop */
935 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
936 jpeg_struct->luminanceQuantization,jpeg_struct);
937 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
938 jpeg_struct->luminanceQuantization,jpeg_struct);
939
940 /* Fill-up the chroma 8x8 blocks */
941 for(k=8; k> 0; k--){
942 for(t=8; t > 0; t--) {
943 *p3++ = *c2_ptr++ - SHIFT;
944 *p4++ = *c3_ptr++ - SHIFT;
945 }
946 c2_ptr = c2_ptr + skip2;
947 c3_ptr = c3_ptr + skip2;
948 } /* end on k*/
949 EN_Encode_Block(block3, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
950 jpeg_struct->chrominanceQuantization,jpeg_struct);
951 EN_Encode_Block(block4, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
952 jpeg_struct->tbl3_Quantization,jpeg_struct);
953 } /* end on j */
954 } /* end on i */
955 } /* end of non-interleaved data. */
956 FPX_free(block1);
957 FPX_free(block2);
958 FPX_free(block3);
959 FPX_free(block4);
960
961 return(0);
962 }
963
964
EN_Encode_Scan_Color4224(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)965 int EN_Encode_Scan_Color4224(unsigned char *buf,
966 int width, int height,
967 int interleaved,
968 JPEG_STRUCT *jpeg_struct)
969 {
970 /*
971 * Routine to generate macroblocks for subsampled data in 4:2:2:4 format
972 * 4-channel data, the middle 2 channels subsampled by 2x in hor. dimension only
973 *
974 * <buf>: a pointer to the data (4 color components)
975 * if interleaved=1, data is interleaved (c1c1 c2 c3 c4c4, c1c1 c2 c3 c4c4)
976 * else it is not( data is in scan-line by scan -line format).
977 *
978 * width and height are already in multiples of 8/16; i.e. data
979 * is already padded and subsampled as necessary.
980 *
981 * Some error checking should be done at the higher level
982 * for the width height dimensions.
983 */
984 unsigned char *buf_ptr;
985 unsigned char *c1_ptr, *c2_ptr, *c3_ptr, *c4_ptr;
986 int *block1, *block2, *block3, *block4, *block5, *block6;
987 int *p1, *p2, *p3, *p4, *p5, *p6, i, j, k, t;
988 int skip,skip2,skip_row_data, skip_row_data1,skip_row_data2;
989 int nvMCU, nhMCU; /* number of FULL MCU vert. and hor. */
990
991 block1 = block2 = block3 = block4= block5 = block6 = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
992 block1 = (int *) FPX_malloc(64 * sizeof(int));
993 block2 = (int *) FPX_malloc(64 * sizeof(int));
994 block3 = (int *) FPX_malloc(64 * sizeof(int));
995 block4 = (int *) FPX_malloc(64 * sizeof(int));
996 block5 = (int *) FPX_malloc(64 * sizeof(int));
997 block6 = (int *) FPX_malloc(64 * sizeof(int));
998
999 if ((block1 == 0) || (block2 == 0) || (block3 == 0) ||
1000 (block4 == 0) || (block5 == 0) || (block6 == 0))
1001 goto Exit; /* CHG_JPEG_MEM_FIX - Added allocation test */
1002
1003 Clear_Last_DC(jpeg_struct);
1004
1005 nhMCU = width/16;
1006 nvMCU = height/8;
1007
1008 /* Interleaved Data */
1009 if (interleaved == 1) {
1010 /* if interleaved, a line has (width/2 * 6) pixels (1byte/pixel) */
1011 /* A subsampled symbol has 6 bytes. */
1012 skip = width*3 - 48; /* 48 = 8 subsampled symbols x 6bytes/ss-symbol */
1013 skip_row_data = 24*width; /* One 8x16 block-row bytes */
1014 /* 8 lines * (6*width/2)bytes/line */
1015 for (i=0; i < nvMCU; i++) {
1016 for (j=0; j < nhMCU; j++) {
1017 p3 = block3;
1018 p4 = block4 ; /* chroma blocks */
1019 buf_ptr = buf + i*skip_row_data + j*48;
1020
1021 p1 = block1;
1022 p2 = block2; /* left-right luma blocks */
1023 p5 = block5;
1024 p6 = block6; /* left-right alpha blocks */
1025 for(k=8; k > 0; k--) {
1026 for(t=4; t > 0; t--) {
1027 *p1++ = *buf_ptr++ - SHIFT;
1028 *p1++ = *buf_ptr++ - SHIFT;
1029 *p3++ = *buf_ptr++ - SHIFT;
1030 *p4++ = *buf_ptr++ - SHIFT;
1031 *p5++ = *buf_ptr++ - SHIFT;
1032 *p5++ = *buf_ptr++ - SHIFT;
1033 } /* t-loop */
1034 for(t=4; t > 0; t--) {
1035 *p2++ = *buf_ptr++ - SHIFT;
1036 *p2++ = *buf_ptr++ - SHIFT;
1037 *p3++ = *buf_ptr++ - SHIFT;
1038 *p4++ = *buf_ptr++ - SHIFT;
1039 *p6++ = *buf_ptr++ - SHIFT;
1040 *p6++ = *buf_ptr++ - SHIFT;
1041 } /* t-loop */
1042 buf_ptr = buf_ptr + skip;
1043 } /* k-loop */
1044 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1045 jpeg_struct->luminanceQuantization,jpeg_struct);
1046 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1047 jpeg_struct->luminanceQuantization,jpeg_struct);
1048 EN_Encode_Block(block3, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
1049 jpeg_struct->chrominanceQuantization,jpeg_struct);
1050 EN_Encode_Block(block4, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
1051 jpeg_struct->tbl3_Quantization,jpeg_struct);
1052 EN_Encode_Block(block5, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
1053 jpeg_struct->tbl4_Quantization,jpeg_struct);
1054 EN_Encode_Block(block6, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
1055 jpeg_struct->tbl4_Quantization,jpeg_struct);
1056
1057 } /* end on j */
1058 } /* end on i */
1059 } /* end of interleaved */
1060 /************************/
1061 /* Non-Interleaved Data */
1062 /************************/
1063 else {
1064 skip = width - 16;
1065 skip2 = (width/2) - 8;
1066 skip_row_data1 = 8*width; /* a row of luma/alpha -blocks */
1067 skip_row_data2 = 8*width/2; /* a row of chroma-blocks */
1068 for(i = 0; i < nvMCU ; i++) {
1069 for(j = 0; j < nhMCU ; j++) {
1070 p3 = block3;
1071 p4 = block4 ; /* chroma blocks */
1072 c1_ptr = buf + i*skip_row_data1 + j*16;
1073 c2_ptr = buf + width*height + i*skip_row_data2 + j*8;
1074 c3_ptr = c2_ptr + (width*height)/4;
1075 c4_ptr = buf + 3*width*height/2 + i*skip_row_data1 + j*16;
1076
1077 p1 = block1;
1078 p2 = block2; /* left-right luma blocks */
1079 p5 = block5;
1080 p6 = block6; /* left-right alpha blocks */
1081 for(k=8; k > 0; k--) {
1082 for(t=8; t > 0; t--) { /* left block */
1083 *p1++ = *c1_ptr++ - SHIFT;
1084 *p5++ = *c4_ptr++ - SHIFT;
1085 } /* t-loop */
1086
1087 for(t=8; t > 0; t--) { /* tight block */
1088 *p2++ = *c1_ptr++ - SHIFT;
1089 *p6++ = *c4_ptr++ - SHIFT;
1090 } /* t-loop */
1091 c1_ptr += skip;
1092 c4_ptr += skip;
1093 } /* k-loop */
1094 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1095 jpeg_struct->luminanceQuantization,jpeg_struct);
1096 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1097 jpeg_struct->luminanceQuantization,jpeg_struct);
1098
1099 /* Fill-up the chroma 8x8 blocks */
1100 for(k=8; k> 0; k--){
1101 for(t=8; t > 0; t--) {
1102 *p3++ = *c2_ptr++ - SHIFT;
1103 *p4++ = *c3_ptr++ - SHIFT;
1104 }
1105 c2_ptr = c2_ptr + skip2;
1106 c3_ptr = c3_ptr + skip2;
1107 } /* end on k*/
1108 EN_Encode_Block(block3, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
1109 jpeg_struct->chrominanceQuantization,jpeg_struct);
1110 EN_Encode_Block(block4, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
1111 jpeg_struct->tbl3_Quantization,jpeg_struct);
1112
1113 /* Encode Alpha Blocks */
1114 EN_Encode_Block(block5, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
1115 jpeg_struct->tbl4_Quantization,jpeg_struct);
1116 EN_Encode_Block(block6, 3, &jpeg_struct->huff[6], &jpeg_struct->huff[7],
1117 jpeg_struct->tbl4_Quantization,jpeg_struct);
1118
1119 } /* end on j */
1120 } /* end on i */
1121 } /* end of non-interleaved data. */
1122 Exit: /* CHG_JPEG_MEM_FIX - branch to here if mem alloc fails */
1123 FPX_free(block1);
1124 FPX_free(block2);
1125 FPX_free(block3);
1126 FPX_free(block4);
1127 FPX_free(block5);
1128 FPX_free(block6);
1129
1130 return(0);
1131 }
1132
1133
EN_Encode_Scan_Color411(unsigned char * buf,int width,int height,int interleaved,JPEG_STRUCT * jpeg_struct)1134 int EN_Encode_Scan_Color411(unsigned char *buf,
1135 int width, int height,
1136 int interleaved,
1137 JPEG_STRUCT *jpeg_struct)
1138 {
1139 /*
1140 * Routine to generate macroblocks for subsampled data in 4:1:1
1141 * or 4:2:0 format.
1142 *
1143 * <buf>: a pointer to the data (3 color components)
1144 * if interleaved=1, data is interleaved (c1c1c1c1 c2 c3, c1c1c1c1 c2 c3)
1145 * else it is not( data is in scan-line by scan -line format).
1146 *
1147 * width and heigh are already in multiples of 16; i.e. data
1148 * is already padded and subsampled as necessary.
1149 *
1150 * Some error checking should be done at the higher level
1151 * for the width height dimensions.
1152 */
1153 unsigned char *buf_ptr, *c1_ptr, *c2_ptr, *c3_ptr;
1154 int *block1, *block2, *block3, *block4;
1155 int *p1, *p2, *p3, *p4, i, j, k, l, t, skip, skip2,skip_row_data;
1156 int *p1_2, *p2_2;
1157 int nvMCU, nhMCU; /* number of FULL MCU vert. and hor. */
1158
1159 block1 = block2 = block3 = block4 = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
1160 block1 = (int *) FPX_malloc(64 * sizeof(int));
1161 block2 = (int *) FPX_malloc(64 * sizeof(int));
1162 block3 = (int *) FPX_malloc(64 * sizeof(int));
1163 block4 = (int *) FPX_malloc(64 * sizeof(int));
1164
1165 /* CHG_JPEG_MEM_FIX - added the following allocation check */
1166 if ((block1 == 0) || (block2 == 0) || (block3 == 0) || (block4 == 0)) {
1167 if (block1) FPX_free(block1);
1168 if (block2) FPX_free(block2);
1169 if (block3) FPX_free(block3);
1170 return (EJPEG_ERROR_MEM);
1171 }
1172
1173 Clear_Last_DC(jpeg_struct);
1174
1175 nvMCU = height/16;
1176 nhMCU = width/16;
1177
1178 /* Interleaved Data */
1179 if (interleaved == 1) {
1180 /* if interleaved, a line has (width/2 * 6) pixels */
1181 skip = width*3 - 48; /* 48 = 16 x 3 */
1182 skip_row_data = 16*3*width/2; /* One 16x16 block-row bytes */
1183 for (i=0; i < nvMCU; i++) {
1184 for (j=0; j < nhMCU; j++) {
1185 p3 = block3;
1186 p4 = block4 ; /* chroma blocks */
1187 buf_ptr = buf + i*skip_row_data + j*48;
1188 for (l=2; l>0; l--) {
1189 p1 = block1;
1190 p2 = block2; /* left-right blocks */
1191 p1_2 = p1 +8;
1192 p2_2 = p2+8;
1193 for(k=4; k > 0; k--) {
1194 for(t=4; t > 0; t--) {
1195 *p1++ = *buf_ptr++ - SHIFT;
1196 *p1++ = *buf_ptr++ - SHIFT;
1197 *p1_2++ = *buf_ptr++ - SHIFT;
1198 *p1_2++ = *buf_ptr++ - SHIFT;
1199 *p3++ = *buf_ptr++ - SHIFT;
1200 *p4++ = *buf_ptr++ - SHIFT;
1201 } /* t-loop */
1202 for(t=4; t > 0; t--) {
1203 *p2++ = *buf_ptr++ - SHIFT;
1204 *p2++ = *buf_ptr++ - SHIFT;
1205 *p2_2++ = *buf_ptr++ - SHIFT;
1206 *p2_2++ = *buf_ptr++ - SHIFT;
1207 *p3++ = *buf_ptr++ - SHIFT;
1208 *p4++ = *buf_ptr++ - SHIFT;
1209 } /* t-loop */
1210 p1 = p1 + 8;
1211 p2 = p2 +8;
1212 p1_2 = p1_2 + 8;
1213 p2_2 = p2_2 +8;
1214 buf_ptr = buf_ptr + skip;
1215 } /* k-loop */
1216 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1217 jpeg_struct->luminanceQuantization,jpeg_struct);
1218 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1219 jpeg_struct->luminanceQuantization,jpeg_struct);
1220 } /* end on l */
1221 EN_Encode_Block(block3, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
1222 jpeg_struct->chrominanceQuantization,jpeg_struct);
1223 EN_Encode_Block(block4, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
1224 jpeg_struct->tbl3_Quantization,jpeg_struct);
1225 } /* end on j */
1226 } /* end on i */
1227 } /* end of interleaved */
1228 /************************/
1229 /* Non-Interleaved Data */
1230 /************************/
1231 else {
1232 skip = width - 16;
1233 skip2 = (width/2) - 8;
1234 for(i = 0; i < nvMCU ; i++) {
1235 for(j = 0; j < nhMCU ; j++) {
1236 p3 = block3;
1237 p4 = block4 ; /* chroma blocks */
1238 c1_ptr = buf + j*16;
1239 c2_ptr = buf + width*height + j*8;
1240 c3_ptr = c2_ptr + (width*height)/4;
1241 for (l=2; l>0; l--) {
1242 p1 = block1;
1243 p2 = block2; /* left-right blocks */
1244 for(k=8; k > 0; k--) {
1245 /* Top */
1246 for(t=8; t > 0; t--) {
1247 *p1++ = *c1_ptr++ - SHIFT;
1248 } /* t-loop */
1249 /* bottom */
1250 for(t=8; t > 0; t--) {
1251 *p2++ = *c1_ptr++ - SHIFT;
1252 } /* t-loop */
1253 c1_ptr = c1_ptr + skip;
1254 } /* k-loop */
1255 EN_Encode_Block(block1, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1256 jpeg_struct->luminanceQuantization,jpeg_struct);
1257 EN_Encode_Block(block2, 0, &jpeg_struct->huff[0], &jpeg_struct->huff[1],
1258 jpeg_struct->luminanceQuantization,jpeg_struct);
1259 } /* end on l */
1260 /* Fill-up the chroma 8x8 blocks */
1261 for(k=8; k> 0; k--){
1262 for(t=8; t > 0; t--) {
1263 *p3++ = *c2_ptr++ - SHIFT;
1264 *p4++ = *c3_ptr++ - SHIFT;
1265 }
1266 c2_ptr = c2_ptr + skip2;
1267 c3_ptr = c3_ptr + skip2;
1268 } /* end on k*/
1269 EN_Encode_Block(block3, 1, &jpeg_struct->huff[2], &jpeg_struct->huff[3],
1270 jpeg_struct->chrominanceQuantization,jpeg_struct);
1271 EN_Encode_Block(block4, 2, &jpeg_struct->huff[4], &jpeg_struct->huff[5],
1272 jpeg_struct->tbl3_Quantization,jpeg_struct);
1273 } /* end on j */
1274 } /* end on i */
1275 } /* end of non-interleaved data. */
1276 FPX_free(block1);
1277 FPX_free(block2);
1278 FPX_free(block3);
1279 FPX_free(block4);
1280
1281 return(0);
1282 }
1283
1284 /*
1285 * Return 0 if successful, ERROR_FORMAT if unsupported format
1286 */
EN_Encode_Scan(TILE_DATA * tile_data,JPEG_STRUCT * jpeg_struct)1287 int EN_Encode_Scan(TILE_DATA *tile_data,
1288 JPEG_STRUCT *jpeg_struct)
1289 {
1290 int width, height,components;
1291 int hdim[4],vdim[4],i;
1292 int interleave_flag;
1293
1294 components = tile_data->components;
1295 width = tile_data->width;
1296 height = tile_data->height;
1297 interleave_flag = tile_data->iflag;
1298 for (i=0; i < 4 ;i++) {
1299 hdim[i] = tile_data->Hsamp[i];
1300 vdim[i] = tile_data->Vsamp[i];
1301 }
1302
1303 if (components == 1) {
1304 return(EN_Encode_Scan_Gray
1305 (tile_data->data,width,height,jpeg_struct));
1306 } else if ( (components == 2) &&
1307 (hdim[0] == 1) && (hdim[1] == 1) &&
1308 (vdim[0] == 1) && (vdim[1] == 1) ) {
1309 return(EN_Encode_Scan_Color11(tile_data->data, width, height,
1310 interleave_flag,
1311 jpeg_struct));
1312 } else if ( (components == 3) &&
1313 (hdim[0] == 2) && (hdim[1] == 1) && (hdim[2] == 1) &&
1314 (vdim[0] == 2) && (vdim[1] == 1) && (vdim[2] == 1) ) {
1315 return(EN_Encode_Scan_Color411(tile_data->data,
1316 width, height,
1317 interleave_flag,
1318 jpeg_struct));
1319 } else if ( (components == 3) &&
1320 (hdim[0] == 2) && (hdim[1] == 1) && (hdim[2] == 1) &&
1321 (vdim[0] == 1) && (vdim[1] == 1) && (vdim[2] == 1) ) {
1322 return(EN_Encode_Scan_Color422(tile_data->data,
1323 width, height,
1324 interleave_flag,
1325 jpeg_struct));
1326 } else if ( (components == 3) &&
1327 (hdim[0] == 1) && (hdim[1] == 1) && (hdim[2] == 1) &&
1328 (vdim[0] == 1) && (vdim[1] == 1) && (vdim[2] == 1) ) {
1329 return(EN_Encode_Scan_Color111(tile_data->data, width, height,
1330 interleave_flag,
1331 jpeg_struct));
1332 } else if ( (components == 4) &&
1333 (hdim[0] == 1) && (hdim[1] == 1) && (hdim[2] == 1)
1334 && (hdim[3] == 1) &&
1335 (vdim[0] == 1) && (vdim[1] == 1) && (vdim[2] == 1)
1336 && (vdim[3] == 1) ) {
1337 return(EN_Encode_Scan_Color1111(tile_data->data, width, height,
1338 interleave_flag,
1339 jpeg_struct));
1340 } else if ( (components == 4) &&
1341 (hdim[0] == 2) && (hdim[1] == 1) && (hdim[2] == 1)
1342 && (hdim[3] == 2) &&
1343 (vdim[0] == 2) && (vdim[1] == 1) && (vdim[2] == 1)
1344 && (vdim[3] == 2) ) {
1345 return(EN_Encode_Scan_Color4114(tile_data->data, width, height,
1346 interleave_flag,
1347 jpeg_struct));
1348 } else if ( (components == 4) &&
1349 (hdim[0] == 2) && (hdim[1] == 1) && (hdim[2] == 1)
1350 && (hdim[3] == 2) &&
1351 (vdim[0] == 1) && (vdim[1] == 1) && (vdim[2] == 1)
1352 && (vdim[3] == 1) ) {
1353 return(EN_Encode_Scan_Color4224(tile_data->data, width, height,
1354 interleave_flag,
1355 jpeg_struct));
1356 } else {
1357 return(EJPEG_ERROR_FORMAT);
1358 }
1359 }
1360
1361
1362 /*
1363 * Return 0 if successful, -1 otherwise.
1364 */
JPEGEncodeTileInit(unsigned char * data,int width,int height,int bytes,int quality_factor,int * Hsamp,int * Vsamp,int iflag,TILE_DATA * tile_data,int nu_huff,JPEGHuffTable * huffman_table,unsigned char * CompDCHuffIdent,unsigned char * CompACHuffIdent,int nu_qtables,JPEGQuantTable * q_table,unsigned char * CompQuantIdent,JPEG_STRUCT * jpeg_struct,unsigned char * header_buffer,long header_buffer_size,long * header_bytes)1365 int JPEGEncodeTileInit(
1366 unsigned char *data, /* points to the input tile */
1367 int width,
1368 int height,
1369 int bytes, /* number of bytes per pixel */
1370 int quality_factor,
1371 int *Hsamp,int *Vsamp, /* Hor. & Vert. subsampling factors */
1372 int iflag, /* 1/0 = Interleaved/Non-interleaved data */
1373 TILE_DATA *tile_data,
1374 int nu_huff, /* # of distinct Huffman Tables (a max of 8 tables: four DC-AC pairs) */
1375 JPEGHuffTable *huffman_table,
1376 unsigned char *CompDCHuffIdent,
1377 unsigned char *CompACHuffIdent,
1378 int nu_qtables, /* # of distinct Q-tables (a max of 4 tables) */
1379 JPEGQuantTable *q_table,
1380 unsigned char *CompQuantIdent,
1381 JPEG_STRUCT *jpeg_struct,
1382 unsigned char *header_buffer,
1383 long header_buffer_size,
1384 long *header_bytes)
1385 {
1386 int Iflag;
1387
1388 EB_Init(header_buffer,header_buffer_size);
1389
1390 if (EP_Begin()) {
1391 return(EJPEG_ERROR_MEM);
1392 } else {
1393 EP_Write_SOI();
1394 {
1395 unsigned char *hclass, *ident, **bits, **huffval;
1396 int i,j;
1397 int q[4][64], *intptr, *intdstptr = 0, whereIndex[4];
1398 int whereDCIndex[4],whereACIndex[4];
1399
1400 /* Scale input Quantization matrices by the Q-factor */
1401 if (quality_factor < 1)
1402 quality_factor = 1;
1403 else if (quality_factor > MAX_Q)
1404 quality_factor = 255;
1405
1406 for (i=0; i < nu_qtables ;i++) {
1407 whereIndex[q_table[i].ident]=i;
1408 Scale_Char_Matrix(quality_factor, Q_PRECISION,
1409 q_table[i].quantizer,q[i]);
1410 EP_Write_DQT(0, q_table[i].ident, q[i]);
1411 Fill_Winograd_Quant_Table(q[i],q[i]);
1412 }
1413 for (i=0; i < bytes ;i++) {
1414 if (i==0) {
1415 intdstptr = jpeg_struct->luminanceQuantization;
1416 }
1417 else if (i==1) {
1418 intdstptr = jpeg_struct->chrominanceQuantization;
1419 }
1420 else if (i==2) {
1421 intdstptr = jpeg_struct->tbl3_Quantization;
1422 }
1423 else if (i==3) {
1424 intdstptr = jpeg_struct->tbl4_Quantization;
1425 }
1426 intptr = q[whereIndex[CompQuantIdent[i]]];
1427 for (j=0; j < 64 ; j++) {
1428 *intdstptr++ = *intptr++;
1429 }
1430 }
1431
1432 /* Read in Huffman Tables */
1433 bits = huffval = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
1434 hclass = ident = NULL; /* CHG_JPEG_MEM_FIX - for allocation checking */
1435 bits =(unsigned char **) FPX_malloc(8 * sizeof(unsigned char *));
1436 huffval=(unsigned char **) FPX_malloc(8 * sizeof(unsigned char *));
1437 hclass =(unsigned char *) FPX_malloc(8 * sizeof(unsigned char));
1438 ident =(unsigned char *) FPX_malloc(8 * sizeof(unsigned char));
1439
1440 /* CHG_JPEG_MEM_FIX - added the following allocation check */
1441 if ((bits == 0) || (huffval == 0) || (hclass == 0) || (ident == 0)) {
1442 if (bits) FPX_free(bits);
1443 if (huffval) FPX_free(huffval);
1444 if (hclass) FPX_free(hclass);
1445 if (ident) FPX_free(ident);
1446 return (EJPEG_ERROR_MEM);
1447 }
1448
1449 for (i=0; i < nu_huff ;i++) {
1450 bits[i] = huffman_table[i].bits;
1451 huffval[i] = huffman_table[i].vals;
1452 hclass[i] = huffman_table[i].hclass;
1453 ident[i] = huffman_table[i].ident;
1454 }
1455 EP_Write_DHTs(nu_huff, hclass, ident, bits, huffval);
1456
1457 /* The Huffman Tables alternate as: DC-AC-DC-AC-... */
1458 for (i=0; i < nu_huff ;i +=2) {
1459 whereDCIndex[huffman_table[i].ident]=i;
1460 whereACIndex[huffman_table[i+1].ident]=i+1;
1461 }
1462
1463 /* Set up internal structures */
1464 for (i=0; i < bytes ;i++) {
1465 BuildHuffmanTable(huffman_table[whereDCIndex[CompDCHuffIdent[i]]].bits,
1466 huffman_table[whereDCIndex[CompDCHuffIdent[i]]].vals,
1467 &jpeg_struct->huff[i*2]);
1468 BuildHuffmanTable(huffman_table[whereACIndex[CompACHuffIdent[i]]].bits,
1469 huffman_table[whereACIndex[CompACHuffIdent[i]]].vals,
1470 &jpeg_struct->huff[i*2+1]);
1471 }
1472
1473 FPX_free(hclass);
1474 FPX_free(ident);
1475 FPX_free(bits);
1476 FPX_free(huffval);
1477 }
1478 EP_Write_EOI(); /* Create an Abbreviated-Format for table-specification data */
1479 EP_End();
1480 }
1481
1482 /* Set up internal structures */
1483 tile_data->width = width;
1484 tile_data->height = height;
1485 tile_data->components = bytes;
1486 Iflag = (iflag==0) ? 1 : 0;
1487 tile_data->iflag = Iflag; /* interleaved data */
1488 tile_data->data = data;
1489 tile_data->Hsamp=Hsamp;
1490 tile_data->Vsamp=Vsamp;
1491
1492 EB_End(header_bytes);
1493
1494 return(0);
1495 }
1496
1497 /*
1498 * Return 0 if successful, -1 otherwise.
1499 */
JPEGEncodeTile(TILE_DATA * tile_data,JPEG_STRUCT * jpeg_struct,unsigned char * CompDCHuffIdent,unsigned char * CompACHuffIdent,unsigned char * CompQuantIdent,unsigned char * out_buf,long int out_buf_size,long int * compr_size)1500 int JPEGEncodeTile(
1501 TILE_DATA *tile_data,
1502 JPEG_STRUCT *jpeg_struct,
1503 unsigned char *CompDCHuffIdent,
1504 unsigned char *CompACHuffIdent,
1505 unsigned char *CompQuantIdent,
1506 unsigned char *out_buf,
1507 long int out_buf_size,
1508 long int *compr_size)
1509 {
1510 int width,height,components;
1511
1512 width = tile_data->width;
1513 height = tile_data->height;
1514 components = tile_data->components;
1515
1516 EB_Init(out_buf, out_buf_size);
1517 EP_Begin();
1518 EP_Write_SOI(); /* Abbreviated format for compressed image data */
1519 if (EP_Write_SOF((int)width,(int)height,
1520 tile_data->Hsamp,tile_data->Vsamp,(int)(components),
1521 CompQuantIdent)){
1522 EP_End();
1523 return(EJPEG_ERROR_MEM);
1524 }
1525 EP_Write_SOS(components,CompDCHuffIdent,CompACHuffIdent);
1526 if (EN_Encode_Scan(tile_data, jpeg_struct)) {
1527 EP_End();
1528 return(EJPEG_ERROR_EOF);
1529 }
1530 EP_Write_EOI();
1531 EP_End();
1532 EB_End(compr_size);
1533 return(0);
1534 }
1535