1 
2 /*
3  * xa_qt_decs.c
4  *
5  * Copyright (C) 1996,1998,1999 by Mark Podlipec.
6  * All rights reserved.
7  *
8  * This software may be freely used, copied and redistributed without
9  * fee for non-commerical purposes provided that this copyright
10  * notice is preserved intact on all copies.
11  *
12  * There is no warranty or other guarantee of fitness of this software.
13  * It is provided solely "as is". The author disclaims all
14  * responsibility and liability with respect to this software's usage
15  * or its effect upon hardware or computer systems.
16  *
17  */
18 /* REVISIONS ***********
19  * 31Aug94  RPZA was using *iptr+=row_inc instead of iptr+=row_inc.
20  * 15Sep94  Added support for RAW32 format. straight RGB with 0x00 up front.
21  * 20Sep94  Added RAW4,RAW16,RAW24,RAW32,Gray CVID and Gray Other codecs.
22  * 07Nov94  Fixed bug in RLE,RLE16,RLE24,RLE32 and RLE1 code, where I
23  *          had improperly guessed what a header meant. Now it's a different
24  *	    hopeful more correct guess.
25  * 29Dec94  Above bug wasn't fixed in RLE(8bit), now it is.
26  * 11Feb95  Fixed Bug with RLE depth 1 codec.
27  * 04Mar95  Added Dithering(+F option) to Cinepak Video Codec.
28  * 17Mar95  Fixed bug that was causing quicktime to erroneously send
29  *          back a FULL_IM flag allowing serious skipping to occur. This
30  *          causes on screen corruption if the Video Codec doesn't
31  *          really support serious skipping.
32  * 11Apr95  Fixed bug in QT_Create_Gray_Cmap that caused last color of
33  *	    colormap not to be generated correctly. Only affected
34  *	    the Gray Quicktime codecs.
35  * 16Jun95  Removed Cinepak Codec per Radius request.
36  *  1Mar96  Separated Video Codecs from mainline qt code.
37  * 17Mar96  Added dithering to RPZA Codec(Apple Video).
38  * 08Jul98  Added support for 8BPS depth 8 and depth 24 video codecs.
39  * 18Nov98  konstantin Priblouda added support for RAW3(SGI grayscale)
40  * 18Nov98  Support for MJPA motion jpeg
41  * 18Nov98  Added support for 8BPS depth 32 video codec.
42  */
43 #include "xa_qt.h"
44 #include "xa_codecs.h"
45 #include "xa_color.h"
46 
47 xaLONG QT_Codec_Query();
48 xaLONG QT_UNK_Codec_Query();
49 
50 xaULONG QT_Decode_RAW1();
51 xaULONG QT_Decode_RAW4();
52 xaULONG QT_Decode_RAW8();
53 xaULONG QT_Decode_RAW16();
54 xaULONG QT_Decode_RAW24();
55 xaULONG QT_Decode_RAW32();
56 xaULONG QT_Decode_RLE1();
57 xaULONG QT_Decode_RLE2();
58 xaULONG QT_Decode_RLE4();
59 xaULONG QT_Decode_RLE8();
60 xaULONG QT_Decode_RLE16();
61 xaULONG QT_Decode_RLE24();
62 xaULONG QT_Decode_RLE32();
63 xaULONG QT_Decode_RPZA();
64 xaULONG QT_Decode_SMC();
65 xaULONG QT_Decode_YUV2();
66 xaULONG QT_Decode_8BPS8();
67 xaULONG QT_Decode_8BPS24();
68 extern xaULONG JFIF_Decode_JPEG();
69 extern xaULONG AVI_Decode_CRAM();
70 extern xaULONG AVI_Decode_CRAM16();
71 
72 xaULONG QT_RPZA_Dither();
73 
74 extern void XA_Gen_YUV_Tabs();
75 /* JPEG and other assist routines */
76 extern void JPG_Alloc_MCU_Bufs();
77 extern void JPG_Setup_Samp_Limit_Table();
78 
79 extern void yuv_to_rgb();
80 void	QT_Get_Dith1_Color24();
81 void	QT_Get_Dith4_Color24();
82 xaULONG	QT_Get_Color();
83 void	QT_Get_RGBColor();
84 void	QT_Get_RGBColorL();
85 void	QT_Get_AV_Colors();
86 void	QT_Get_AV_RGBColors();
87 void	QT_Get_AV_DITH_RGB();
88 xaULONG	QT_Get_DithColor24();
89 extern	xaULONG XA_RGB24_To_CLR32();
90 extern	xaUSHORT qt_gamma_adj[32];
91 
92 void *XA_YUV211111_Func();
93 
94 extern void CMAP_Cache_Clear();
95 extern void CMAP_Cache_Init();
96 extern xaULONG CMAP_Find_Closest();
97 
98 extern xaLONG xa_dither_flag;
99 extern xaUBYTE  *xa_byte_limit;
100 extern YUVBufs jpg_YUVBufs;
101 extern YUVTabs def_yuv_tabs;
102 
103 
104 #define	SMC_MAX_CNT 256
105 /* POD NOTE: eventually make these conditionally dynamic */
106 static xaULONG smc_8cnt,smc_Acnt,smc_Ccnt;
107 static xaULONG smc_8[ (2 * SMC_MAX_CNT) ];
108 static xaULONG smc_A[ (4 * SMC_MAX_CNT) ];
109 static xaULONG smc_C[ (8 * SMC_MAX_CNT) ];
110 
111 
112 /*****************************************************************************
113  *
114  ****************/
QT_Codec_Query(codec)115 xaLONG QT_Codec_Query(codec)
116 XA_CODEC_HDR *codec;
117 { xaLONG ret = CODEC_UNKNOWN;   /* default */
118   codec->extra = 0;
119   codec->xapi_rev = 0x0001;
120   codec->decoder = 0;
121   codec->description = 0;
122   codec->avi_read_ext = 0;
123 
124   switch(codec->compression)
125   {
126     case QT_rle:
127         codec->compression      = QT_rle;
128         codec->description      = "Apple Animation(RLE)";
129         ret = CODEC_SUPPORTED;
130 	switch(codec->depth)
131 	{
132 	  case 40:
133 	  case 8:	codec->decoder = QT_Decode_RLE8;
134 			codec->x = 4 * ((codec->x + 3)/4);
135 			codec->depth = 8;
136 			break;
137 	  case 16:	codec->decoder = QT_Decode_RLE16;
138 			break;
139 	  case 24:	codec->decoder = QT_Decode_RLE24;
140 			break;
141 	  case 32:	codec->decoder = QT_Decode_RLE32;
142 			break;
143 	  case 33:
144 	  case  1:	codec->decoder = QT_Decode_RLE1;
145 			codec->x = 16 * ((codec->x + 15)/16);
146 			codec->depth = 1;
147 			break;
148 	  case 34:
149 	  case  2:	codec->decoder = QT_Decode_RLE2;
150 			codec->x = 16 * ((codec->x + 15)/16);
151 			codec->depth = 4;
152 			break;
153 	  case 36:
154 	  case  4:	codec->decoder = QT_Decode_RLE4;
155 			codec->x = 8 * ((codec->x + 7)/8);
156 			codec->depth = 4;
157 			break;
158 	  default:	ret = CODEC_UNSUPPORTED;	break;
159 	}
160 	break;
161 
162     case QT_smc:
163         codec->compression      = QT_smc;
164         codec->description      = "Apple Graphics(SMC)";
165         ret = CODEC_SUPPORTED;
166 	if ((codec->depth==8) || (codec->depth==40))
167 	{ codec->decoder = QT_Decode_SMC;
168 	  codec->x = 4 * ((codec->x + 3)/4);
169 	  codec->y = 4 * ((codec->y + 3)/4);
170 	  codec->depth = 8;
171 	}
172 	else ret = CODEC_UNSUPPORTED;
173 	break;
174 
175     case QT_raw3: /* added by konstantin Priblouda to support
176 		   * sgi grayscale 8 bit QT files */
177     case QT_raw:
178         codec->compression      = QT_raw;
179         codec->description      = "Apple Uncompressed";
180         ret = CODEC_SUPPORTED;
181 	switch(codec->depth)
182 	{
183 	  case 33:
184 	  case 1:	codec->decoder = QT_Decode_RAW1;
185 			codec->depth = 1;
186 			break;
187 	  case 36:
188 	  case 4:	codec->decoder = QT_Decode_RAW4;
189 			codec->depth = 4;
190 			break;
191 	  case 40:
192 	  case 8:	codec->decoder = QT_Decode_RAW8;
193 			codec->depth = 8;
194 			break;
195 	  case 16:	codec->decoder = QT_Decode_RAW16;
196 			break;
197 	  case 24:	codec->decoder = QT_Decode_RAW24;
198 			break;
199 	  case 32:	codec->decoder = QT_Decode_RAW32;
200 			break;
201 	  default:	ret = CODEC_UNSUPPORTED;	break;
202 	}
203 	break;
204 
205     case QT_8BPS:
206         codec->compression      = QT_8BPS;
207         codec->description      = "Photoshop";
208         ret = CODEC_SUPPORTED;
209 	switch(codec->depth)
210 	{
211 /* NOT YET SUPPORTED
212 	  case 33:
213 	  case 1:	codec->decoder = QT_Decode_RAW1;
214 			codec->depth = 1;
215 			break;
216 	  case 36:
217 	  case 4:	codec->decoder = QT_Decode_RAW4;
218 			codec->depth = 4;
219 			break;
220 	  case 16:	codec->decoder = QT_Decode_RAW16;
221 			break;
222 	  case 32:	codec->decoder = QT_Decode_RAW32;
223 			break;
224 */
225 	  case 40:
226 	  case 8:	codec->decoder = QT_Decode_8BPS8;
227 			codec->depth = 8;
228 			break;
229 	  case 32:	/* TEST */
230 		codec->extra = (void *)(0x01);
231 	  case 24:	codec->decoder = QT_Decode_8BPS24;
232 			break;
233 	  default:	ret = CODEC_UNSUPPORTED;	break;
234 	}
235 	if (ret == CODEC_SUPPORTED)
236 		JPG_Alloc_MCU_Bufs(codec->anim_hdr,codec->x,0,xaFALSE);
237 	break;
238 
239 
240 
241     case QT_azpr:
242     case QT_rpza:
243         codec->compression      = QT_rpza;
244         codec->description      = "Apple Video(RPZA)";
245         ret = CODEC_SUPPORTED;
246 	if (codec->depth == 16)
247 	{ codec->decoder = QT_Decode_RPZA;
248 	  codec->x = 4 * ((codec->x + 3)/4);
249 	  codec->y = 4 * ((codec->y + 3)/4);
250 	  JPG_Setup_Samp_Limit_Table(codec->anim_hdr);
251 	}
252 	else ret = CODEC_UNSUPPORTED;
253 	break;
254 
255     case QT_jpeg:
256         codec->compression      = QT_jpeg;
257         codec->description      = "JPEG";
258 	codec->xapi_rev		= 0x0002;
259         ret = CODEC_SUPPORTED;
260 	JPG_Alloc_MCU_Bufs(codec->anim_hdr,codec->x,0,xaFALSE);
261 	JPG_Setup_Samp_Limit_Table(codec->anim_hdr);
262 	codec->x = 4 * ((codec->x + 3)/4);
263 	codec->y = 2 * ((codec->y + 1)/2);
264 	codec->decoder = JFIF_Decode_JPEG;
265 	if (codec->depth == 40) codec->depth = 8;
266         if (codec->depth > 8)   XA_Gen_YUV_Tabs(codec->anim_hdr);
267 	break;
268 
269 /* NOTE: image appears to shifted 25% to the right  qts/mjpg/08*.qt */
270     case QT_MJPG:
271     case QT_mjpg:
272         codec->compression      = QT_MJPG;
273         codec->description      = "Motion JPEG";
274         codec->xapi_rev = 0x0002;
275         ret = CODEC_SUPPORTED;
276         codec->extra    = (void *)(0x00);
277         codec->decoder = JFIF_Decode_JPEG;
278         codec->x = 4 * ((codec->x + 3)/4);
279         codec->y = 2 * ((codec->y + 1)/2);
280 	if (codec->depth == 40) codec->depth = 8;
281         if (codec->depth > 8)   XA_Gen_YUV_Tabs(codec->anim_hdr);
282         else    codec->avi_ctab_flag = xaFALSE;
283         JPG_Alloc_MCU_Bufs(codec->anim_hdr,codec->x,0,xaFALSE);
284         break;
285 
286     case QT_mjpa:
287         codec->compression      = QT_MJPG;
288         codec->description      = "Motion JPEG Type A";
289         codec->xapi_rev = 0x0002;
290         ret = CODEC_SUPPORTED;
291 	/* interleave is optional and detected automatically, but with mjpga
292 	 * first field is always top field
293 	 */
294         codec->extra    = (void *)(0x02);
295         codec->decoder = JFIF_Decode_JPEG;
296         codec->x = 4 * ((codec->x + 3)/4);
297         codec->y = 4 * ((codec->y + 3)/4);
298 	if (codec->depth == 40) codec->depth = 8;
299         if (codec->depth > 8)   XA_Gen_YUV_Tabs(codec->anim_hdr);
300         else    codec->avi_ctab_flag = xaFALSE;
301         JPG_Alloc_MCU_Bufs(codec->anim_hdr,codec->x,0,xaFALSE);
302         break;
303 
304 
305     case QT_yuv2:
306 	codec->xapi_rev = 0x0002;
307         codec->compression      = QT_yuv2;
308         codec->description      = "Component Video(YUV2)";
309         ret = CODEC_SUPPORTED;
310 	codec->decoder = QT_Decode_YUV2;
311 	codec->x = 2 * ((codec->x + 1)/2);
312 	codec->y = 2 * ((codec->y + 1)/2); /* POD ness? */
313 	JPG_Alloc_MCU_Bufs(codec->anim_hdr,codec->x,0,xaFALSE);
314 	JPG_Setup_Samp_Limit_Table(codec->anim_hdr);
315 	XA_Gen_YUV_Tabs(codec->anim_hdr);
316 	break;
317 
318     case QT_cram:
319     case QT_CRAM:
320     case QT_wham:
321     case QT_WHAM:
322     case QT_msvc:
323     case QT_MSVC:
324         codec->compression      = QT_cram;
325         codec->description      = "Microsoft Video 1";
326         ret = CODEC_SUPPORTED;
327 	if (codec->depth == 8)		codec->decoder = AVI_Decode_CRAM;
328 	else if (codec->depth == 16)	codec->decoder = AVI_Decode_CRAM16;
329 	else ret = CODEC_UNSUPPORTED;
330 	codec->x = 4 * ((codec->x + 3)/4);
331 	codec->y = 4 * ((codec->y + 3)/4);
332 	JPG_Setup_Samp_Limit_Table(codec->anim_hdr);
333 	break;
334 
335 
336 
337     default:
338         ret = CODEC_UNKNOWN;
339         break;
340   }
341   return(ret);
342 }
343 
344 /*****************************************************************************
345  *
346  ****************/
QT_UNK_Codec_Query(codec)347 xaLONG QT_UNK_Codec_Query(codec)
348 XA_CODEC_HDR *codec;
349 { xaLONG ret = CODEC_UNKNOWN;   /* default */
350   codec->extra = 0;
351   codec->xapi_rev = 0x0001;
352   codec->decoder = 0;
353   codec->description = 0;
354   codec->avi_read_ext = 0;
355 
356   switch(codec->compression)
357   {
358     case QT_PGVV:
359         codec->compression      = QT_PGVV;
360         codec->description      = "Radius (PGVV)";
361         ret = CODEC_UNSUPPORTED;
362 	break;
363 
364     case QT_cvid:
365     case QT_CVID:
366         codec->compression      = QT_CVID;
367         codec->description      = "Radius Cinepak";
368         ret = CODEC_UNSUPPORTED;
369 	break;
370 
371     case QT_SPIG:
372         codec->compression      = QT_SPIG;
373         codec->description      = "Radius Spigot";
374         ret = CODEC_UNSUPPORTED;
375 	break;
376 
377     case QT_rt21:
378     case QT_RT21:
379         codec->compression      = QT_RT21;
380         codec->description      = "Intel Indeo R2.1";
381         ret = CODEC_UNSUPPORTED;
382 	break;
383 
384     case QT_iv31:
385     case QT_IV31:
386         codec->compression      = QT_IV31;
387         codec->description      = "Intel Indeo R3.1";
388         ret = CODEC_UNSUPPORTED;
389 	break;
390 
391     case QT_iv32:
392     case QT_IV32:
393         codec->compression      = QT_IV32;
394         codec->description      = "Intel Indeo R3.2";
395         ret = CODEC_UNSUPPORTED;
396 	break;
397 
398     case QT_iv41:
399     case QT_IV41:
400         codec->compression      = QT_IV41;
401         codec->description      = "Intel Indeo R4.1";
402         ret = CODEC_UNSUPPORTED;
403 	break;
404 
405     case QT_YVU9:
406     case QT_YUV9:
407         codec->compression      = QT_YUV9;
408         codec->description      = "Intel Raw(YUV9)";
409         ret = CODEC_UNSUPPORTED;
410 	break;
411 
412     case QT_kpcd:
413     case QT_KPCD:
414         codec->compression      = QT_KPCD;
415         codec->description      = "Kodak Photo CD";
416         ret = CODEC_UNSUPPORTED;
417 	break;
418 
419     case QT_SVQ1:
420         codec->compression      = QT_SVQ1;
421         codec->description      = "Sorenson Video";
422         ret = CODEC_UNSUPPORTED;
423 	break;
424 
425     default:
426         ret = CODEC_UNKNOWN;
427         break;
428   }
429   return(ret);
430 }
431 
432 
433 
434 /********************* * * * *******************************************/
QT_Decode_RAW4(image,delta,dsize,dec_info)435 xaULONG QT_Decode_RAW4(image,delta,dsize,dec_info)
436 xaUBYTE *image;         /* Image Buffer. */
437 xaUBYTE *delta;         /* delta data. */
438 xaULONG dsize;          /* delta size */
439 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
440 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
441   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
442   xaUBYTE *dp = delta;
443   xaLONG i = (imagex * imagey) >> 1;
444 
445   dec_info->xs = dec_info->ys = 0;
446   dec_info->xe = imagex; dec_info->ye = imagey;
447 
448     /* Indicate we can drop these frames */
449   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
450 
451 /* POD QUESTION: is imagex a multiple of 2??? */
452   if (map_flag==xaFALSE) { xaUBYTE *iptr = (xaUBYTE *)image;
453     while(i--) { xaUBYTE d = *dp++; *iptr++ = (d>>4); *iptr++ = d & 0xf; }
454   }
455   else if (x11_bytes_pixel==1) { xaUBYTE *iptr = (xaUBYTE *)image;
456     while(i--) { xaULONG d = (xaULONG)*dp++;
457                  *iptr++ = (xaUBYTE)map[d>>4]; *iptr++ = (xaUBYTE)map[d&15]; }
458   }
459   else if (x11_bytes_pixel==4) { xaULONG *iptr = (xaULONG *)image;
460     while(i--) { xaULONG d = (xaULONG)*dp++;
461                  *iptr++ = (xaULONG)map[d>>4]; *iptr++ = (xaULONG)map[d&15]; }
462   }
463   else /*(x11_bytes_pixel==2)*/ { xaUSHORT *iptr = (xaUSHORT *)image;
464     while(i--) { xaULONG d = (xaULONG)*dp++;
465                  *iptr++ = (xaUSHORT)map[d>>4]; *iptr++ = (xaUSHORT)map[d&15]; }
466   }
467   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
468   else return(ACT_DLTA_NORM);
469 }
470 
471 /********************* * * * *******************************************/
QT_Decode_RAW1(image,delta,dsize,dec_info)472 xaULONG QT_Decode_RAW1(image,delta,dsize,dec_info)
473 xaUBYTE *image;         /* Image Buffer. */
474 xaUBYTE *delta;         /* delta data. */
475 xaULONG dsize;          /* delta size */
476 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
477 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
478   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
479   xaUBYTE *dp = delta;
480   xaLONG i = (imagex * imagey) >> 1;
481   xaULONG white,black;
482 
483   dec_info->xs = dec_info->ys = 0;
484   dec_info->xe = imagex; dec_info->ye = imagey;
485 
486   black = 0x00;
487   white = 0x01;
488 
489     /* Indicate we can drop these frames */
490   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
491 
492 /* POD QUESTION: is imagex a multiple of 8??? */
493   if (map_flag==xaFALSE) { xaUBYTE *iptr = (xaUBYTE *)image;
494     while(i--) { xaUBYTE d = *dp++;
495        *iptr++ = (xaUBYTE)( (d & 0x80)?(white):(black) );
496        *iptr++ = (xaUBYTE)( (d & 0x40)?(white):(black) );
497        *iptr++ = (xaUBYTE)( (d & 0x20)?(white):(black) );
498        *iptr++ = (xaUBYTE)( (d & 0x10)?(white):(black) );
499        *iptr++ = (xaUBYTE)( (d & 0x08)?(white):(black) );
500        *iptr++ = (xaUBYTE)( (d & 0x04)?(white):(black) );
501        *iptr++ = (xaUBYTE)( (d & 0x02)?(white):(black) );
502        *iptr++ = (xaUBYTE)( (d & 0x01)?(white):(black) ); }
503   }
504   else if (x11_bytes_pixel==1) { xaUBYTE *iptr = (xaUBYTE *)image;
505     while(i--) { xaUBYTE d = *dp++;
506        *iptr++ = (xaUBYTE)map[ (d & 0x80)?(white):(black) ];
507        *iptr++ = (xaUBYTE)map[ (d & 0x40)?(white):(black) ];
508        *iptr++ = (xaUBYTE)map[ (d & 0x20)?(white):(black) ];
509        *iptr++ = (xaUBYTE)map[ (d & 0x10)?(white):(black) ];
510        *iptr++ = (xaUBYTE)map[ (d & 0x08)?(white):(black) ];
511        *iptr++ = (xaUBYTE)map[ (d & 0x04)?(white):(black) ];
512        *iptr++ = (xaUBYTE)map[ (d & 0x02)?(white):(black) ];
513        *iptr++ = (xaUBYTE)map[ (d & 0x01)?(white):(black) ]; }
514   }
515   else if (x11_bytes_pixel==4) { xaULONG *iptr = (xaULONG *)image;
516     while(i--) { xaUBYTE d = *dp++;
517        *iptr++ = (xaULONG)map[ (d & 0x80)?(white):(black) ];
518        *iptr++ = (xaULONG)map[ (d & 0x40)?(white):(black) ];
519        *iptr++ = (xaULONG)map[ (d & 0x20)?(white):(black) ];
520        *iptr++ = (xaULONG)map[ (d & 0x10)?(white):(black) ];
521        *iptr++ = (xaULONG)map[ (d & 0x08)?(white):(black) ];
522        *iptr++ = (xaULONG)map[ (d & 0x04)?(white):(black) ];
523        *iptr++ = (xaULONG)map[ (d & 0x02)?(white):(black) ];
524        *iptr++ = (xaULONG)map[ (d & 0x01)?(white):(black) ]; }
525   }
526   else /*(x11_bytes_pixel==2)*/ { xaUSHORT *iptr = (xaUSHORT *)image;
527     while(i--) { xaUBYTE d = *dp++;
528        *iptr++ = (xaUSHORT)map[ (d & 0x80)?(white):(black) ];
529        *iptr++ = (xaUSHORT)map[ (d & 0x40)?(white):(black) ];
530        *iptr++ = (xaUSHORT)map[ (d & 0x20)?(white):(black) ];
531        *iptr++ = (xaUSHORT)map[ (d & 0x10)?(white):(black) ];
532        *iptr++ = (xaUSHORT)map[ (d & 0x08)?(white):(black) ];
533        *iptr++ = (xaUSHORT)map[ (d & 0x04)?(white):(black) ];
534        *iptr++ = (xaUSHORT)map[ (d & 0x02)?(white):(black) ];
535        *iptr++ = (xaUSHORT)map[ (d & 0x01)?(white):(black) ]; }
536   }
537   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
538   else return(ACT_DLTA_NORM);
539 }
540 
541 /********************* * * * *******************************************/
QT_Decode_RAW8(image,delta,dsize,dec_info)542 xaULONG QT_Decode_RAW8(image,delta,dsize,dec_info)
543 xaUBYTE *image;         /* Image Buffer. */
544 xaUBYTE *delta;         /* delta data. */
545 xaULONG dsize;          /* delta size */
546 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
547 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
548   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
549   xaUBYTE *dptr = delta;
550   xaLONG i = imagex * imagey;
551 
552   dec_info->xs = dec_info->ys = 0;
553   dec_info->xe = imagex; dec_info->ye = imagey;
554     /* Indicate we can drop these frames */
555   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
556 
557   if (map_flag==xaFALSE) { xaUBYTE *iptr = (xaUBYTE *)image;
558 				while(i--) *iptr++ = (xaUBYTE)*dptr++; }
559   else if (x11_bytes_pixel==1) { xaUBYTE *iptr = (xaUBYTE *)image;
560 				while(i--) *iptr++ = (xaUBYTE)map[*dptr++]; }
561   else if (x11_bytes_pixel==2) { xaUSHORT *iptr = (xaUSHORT *)image;
562 				while(i--) *iptr++ = (xaUSHORT)map[*dptr++]; }
563   else { xaULONG *iptr = (xaULONG *)image;
564 				while(i--) *iptr++ = (xaULONG)map[*dptr++]; }
565 
566   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
567   else return(ACT_DLTA_NORM);
568 }
569 
570 #define QT_RAW_READ16RGB(dp,r,g,b) { xaULONG _d = *dp++ << 8; _d |= *dp++; \
571   r = (_d >> 10) & 0x1f; g = (_d >> 5) & 0x1f; b = _d & 0x1f;	\
572   r = (r << 3) | (r >> 2); g = (g << 3) | (g >> 2); b = (b << 3) | (b >> 2); }
573 
574 /****************** RAW CODEC DEPTH 16 *********************************
575  *  1 unused bit. 5 bits R, G, B
576  *
577  *********************************************************************/
QT_Decode_RAW16(image,delta,dsize,dec_info)578 xaULONG QT_Decode_RAW16(image,delta,dsize,dec_info)
579 xaUBYTE *image;         /* Image Buffer. */
580 xaUBYTE *delta;         /* delta data. */
581 xaULONG dsize;          /* delta size */
582 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
583 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
584   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
585   xaULONG special = dec_info->special;
586   XA_CHDR *chdr = dec_info->chdr;
587   xaUBYTE *dp = delta;
588   xaULONG special_flag = special & 0x0001;
589   xaLONG i = imagex * imagey;
590 
591   dec_info->xs = dec_info->ys = 0;
592   dec_info->xe = imagex; dec_info->ye = imagey;
593     /* Indicate we can drop these frames */
594   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
595 
596   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
597 
598   if (special_flag)
599   { xaUBYTE *iptr = (xaUBYTE *)image;
600     while(i--)
601     { xaULONG r,g,b;  QT_RAW_READ16RGB(dp,r,g,b);
602       *iptr++ = r; *iptr++ = g; *iptr++ = b;}
603   }
604   else if ( (map_flag==xaFALSE) || (x11_bytes_pixel==1) )
605   { xaUBYTE *iptr = (xaUBYTE *)image;
606     while(i--)
607     { xaULONG r,g,b; QT_RAW_READ16RGB(dp,r,g,b);
608       *iptr++ = (xaUBYTE)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
609     }
610   }
611   else if (x11_bytes_pixel==4)
612   { xaULONG *iptr = (xaULONG *)image;
613     while(i--)
614     { xaULONG r,g,b;  QT_RAW_READ16RGB(dp,r,g,b);
615       *iptr++ = (xaULONG)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
616     }
617   }
618   else /* (x11_bytes_pixel==2) */
619   { xaUSHORT *iptr = (xaUSHORT *)image;
620     while(i--)
621     { xaULONG r,g,b;  QT_RAW_READ16RGB(dp,r,g,b);
622       *iptr++ = (xaUSHORT)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
623     }
624   }
625   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
626   else return(ACT_DLTA_NORM);
627 }
628 
629 
630 /****************** RAW CODEC DEPTH 24 *********************************
631  *  R G B
632  *********************************************************************/
QT_Decode_RAW24(image,delta,dsize,dec_info)633 xaULONG QT_Decode_RAW24(image,delta,dsize,dec_info)
634 xaUBYTE *image;         /* Image Buffer. */
635 xaUBYTE *delta;         /* delta data. */
636 xaULONG dsize;          /* delta size */
637 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
638 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
639   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
640   xaULONG special = dec_info->special;
641   XA_CHDR *chdr = dec_info->chdr;
642   xaUBYTE *dp = delta;
643   xaULONG special_flag = special & 0x0001;
644   xaLONG i = imagex * imagey;
645 
646   dec_info->xs = dec_info->ys = 0;
647   dec_info->xe = imagex; dec_info->ye = imagey;
648     /* Indicate we can drop these frames */
649   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
650 
651   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
652 
653   if (special_flag)
654   { xaUBYTE *iptr = (xaUBYTE *)image;
655     while(i--) { *iptr++ = *dp++; *iptr++ = *dp++; *iptr++ = *dp++;}
656   }
657   else if ( (map_flag==xaFALSE) || (x11_bytes_pixel==1) )
658   { xaUBYTE *iptr = (xaUBYTE *)image;
659     while(i--)
660     { xaULONG r,g,b;
661       r = (xaULONG)*dp++; g = (xaULONG)*dp++; b = (xaULONG)*dp++;
662       *iptr++ = (xaUBYTE)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
663     }
664   }
665   else if (x11_bytes_pixel==4)
666   { xaULONG *iptr = (xaULONG *)image;
667     while(i--)
668     { xaULONG r,g,b;
669       r = (xaULONG)*dp++; g = (xaULONG)*dp++; b = (xaULONG)*dp++;
670       *iptr++ = (xaULONG)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
671     }
672   }
673   else /* (x11_bytes_pixel==2) */
674   { xaUSHORT *iptr = (xaUSHORT *)image;
675     while(i--)
676     { xaULONG r,g,b;
677       r = (xaULONG)*dp++; g = (xaULONG)*dp++; b = (xaULONG)*dp++;
678       *iptr++ = (xaUSHORT)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
679     }
680   }
681   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
682   else return(ACT_DLTA_NORM);
683 }
684 
685 
686 /****************** RAW CODEC DEPTH 32 *********************************
687  *  0 R G B
688  *
689  *********************************************************************/
QT_Decode_RAW32(image,delta,dsize,dec_info)690 xaULONG QT_Decode_RAW32(image,delta,dsize,dec_info)
691 xaUBYTE *image;         /* Image Buffer. */
692 xaUBYTE *delta;         /* delta data. */
693 xaULONG dsize;          /* delta size */
694 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
695 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
696   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
697   xaULONG special = dec_info->special;
698   XA_CHDR *chdr = dec_info->chdr;
699   xaUBYTE *dp = delta;
700   xaULONG special_flag = special & 0x0001;
701   xaLONG i = imagex * imagey;
702 
703   dec_info->xs = dec_info->ys = 0;
704   dec_info->xe = imagex; dec_info->ye = imagey;
705     /* Indicate we can drop these frames */
706   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
707 
708   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
709   if (special_flag)
710   { xaUBYTE *iptr = (xaUBYTE *)image;
711     while(i--) {dp++; *iptr++ = *dp++; *iptr++ = *dp++; *iptr++ = *dp++;}
712   }
713   else if ( (map_flag==xaFALSE) || (x11_bytes_pixel==1) )
714   { xaUBYTE *iptr = (xaUBYTE *)image;
715     while(i--)
716     { xaULONG r,g,b;
717       dp++; r = (xaULONG)*dp++; g = (xaULONG)*dp++; b = (xaULONG)*dp++;
718       *iptr++ = (xaUBYTE)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
719     }
720   }
721   else if (x11_bytes_pixel==4)
722   { xaULONG *iptr = (xaULONG *)image;
723     while(i--)
724     { xaULONG r,g,b;
725       dp++; r = (xaULONG)*dp++; g = (xaULONG)*dp++; b = (xaULONG)*dp++;
726       *iptr++ = (xaULONG)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
727     }
728   }
729   else /* (x11_bytes_pixel==2) */
730   { xaUSHORT *iptr = (xaUSHORT *)image;
731     while(i--)
732     { xaULONG r,g,b;
733       dp++; r = (xaULONG)*dp++; g = (xaULONG)*dp++; b = (xaULONG)*dp++;
734       *iptr++ = (xaUSHORT)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
735     }
736   }
737   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
738   else return(ACT_DLTA_NORM);
739 }
740 
741 
742 /********************* * * * *******************************************/
QT_Decode_RLE1(image,delta,dsize,dec_info)743 xaULONG QT_Decode_RLE1(image,delta,dsize,dec_info)
744 xaUBYTE *image;         /* Image Buffer. */
745 xaUBYTE *delta;         /* delta data. */
746 xaULONG dsize;          /* delta size */
747 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
748 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
749   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
750   xaLONG x,y,d,lines; /* xaLONG min_x,max_x,min_y,max_y; */
751   xaUBYTE *dptr;
752 
753   dptr = delta;
754   dptr += 4;    /* skip codec size */
755   d = (*dptr++) << 8;  d |= *dptr++;   /* read code either 0008 or 0000 */
756   if (dsize < 8) /* NOP */
757   {
758     dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
759     return(ACT_DLTA_NOP);
760   }
761   if (d & 0x0008) /* Header present */
762   {
763     y = (*dptr++) << 8; y |= *dptr++;           /* start line */
764     dptr += 2;                                  /* unknown */
765     lines = (*dptr++) << 8; lines |= *dptr++;   /* number of lines */
766     dptr += 2;                                  /* unknown */
767   }
768   else { y = 0; lines = imagey; }
769   x = 0; y--; lines++;
770   while(lines)				/* loop thru lines */
771   {
772     xaULONG d,xskip,cnt;
773 
774     xskip = *dptr++;				/* skip x pixels */
775     cnt = *dptr++;				/* RLE code */
776     if (cnt == 0) break; 			/* exit */
777 
778 /* this can be removed */
779     if ((xskip == 0x80) && (cnt == 0x00))  /* end of codec */
780     {
781 	  lines = 0; y++; x = 0;
782     }
783     else if ((xskip == 0x80) && (cnt == 0xff)) /* skip line */
784 	{lines--; y++; x = 0; }
785     else
786     {
787       if (xskip & 0x80) {lines--; y++; x = xskip & 0x7f;}
788       else x += xskip;
789 
790       if (cnt < 0x80)				/* run of data */
791       {
792         xaUBYTE *bptr; xaUSHORT *sptr; xaULONG *lptr;
793 	if ((x11_bytes_pixel==1) || (map_flag==xaFALSE) )
794 		bptr = (xaUBYTE *)(image + (y * imagex) + (x << 4) );
795 	else if (x11_bytes_pixel==2)
796 		sptr = (xaUSHORT *)(image + 2*(y * imagex) + (x << 5) );
797         else lptr = (xaULONG *)(image + 4*(y * imagex) + (x << 6) );
798         x += cnt;
799         while(cnt--)
800         { xaULONG i,mask;
801           d = (*dptr++ << 8); d |= *dptr++;
802           mask = 0x8000;
803           for(i=0;i<16;i++)
804           {
805             if (map_flag==xaFALSE)
806 		{ if (d & mask) *bptr++ = 0;  else *bptr++ = 1; }
807             else if (x11_bytes_pixel==1) {if (d & mask) *bptr++=(xaUBYTE)map[0];
808 					else *bptr++=(xaUBYTE)map[1];}
809             else if (x11_bytes_pixel==2) {if (d & mask) *sptr++ =(xaUSHORT)map[0];
810 					else *sptr++ =(xaUSHORT)map[1]; }
811             else { if (d & mask) *lptr++ = (xaULONG)map[0];
812 					else *lptr++ = (xaULONG)map[1]; }
813             mask >>= 1;
814           }
815         }
816       } /* end run */
817       else				/* repeat data */
818       {
819         xaUBYTE *bptr; xaUSHORT *sptr; xaULONG *lptr;
820 	if ((x11_bytes_pixel==1) || (map_flag==xaFALSE) )
821 		bptr = (xaUBYTE *)(image + (y * imagex) + (x << 4) );
822 	else if (x11_bytes_pixel==2)
823 		sptr = (xaUSHORT *)(image + 2*(y * imagex) + (x << 5) );
824         else lptr = (xaULONG *)(image + 4*(y * imagex) + (x << 6) );
825         cnt = 0x100 - cnt;
826         x += cnt;
827         d = (*dptr++ << 8); d |= *dptr++;
828         while(cnt--)
829         { xaULONG i,mask;
830           mask = 0x8000;
831           for(i=0;i<16;i++)
832           {
833             if (map_flag==xaFALSE)
834 		{ if (d & mask) *bptr++ = 0;  else *bptr++ = 1; }
835             else if (x11_bytes_pixel==1) {if (d & mask) *bptr++=(xaUBYTE)map[0];
836 					else *bptr++=(xaUBYTE)map[1];}
837             else if (x11_bytes_pixel==2) {if (d & mask) *sptr++ =(xaUSHORT)map[0];
838 					else *sptr++ =(xaUSHORT)map[1]; }
839             else { if (d & mask) *lptr++ = (xaULONG)map[0];
840 					else *lptr++ = (xaULONG)map[1]; }
841             mask >>= 1;
842           }
843         }
844       } /* end repeat */
845     } /* end of code */
846   } /* end of lines */
847  dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
848  if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
849  else return(ACT_DLTA_NORM);
850 }
851 
852 /********************* * * * *******************************************/
QT_Decode_RLE2(image,delta,dsize,dec_info)853 xaULONG QT_Decode_RLE2(image,delta,dsize,dec_info)
854 xaUBYTE *image;         /* Image Buffer. */
855 xaUBYTE *delta;         /* delta data. */
856 xaULONG dsize;          /* delta size */
857 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
858 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
859   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
860   xaLONG x,y,lines; /* xaLONG min_x,max_x,min_y,max_y; */
861   xaULONG d;
862   xaUBYTE *dptr;
863 
864   dptr = delta;
865   dptr += 4;    /* skip codec size */
866   d = (*dptr++) << 8;  d |= *dptr++;   /* read code either 0008 or 0000 */
867   if (dsize < 8) /* NOP */
868   {
869     dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
870 DEBUG_LEVEL1 fprintf(stderr,"nop\n"); /* POD */
871     return(ACT_DLTA_NOP);
872   }
873   if (d & 0x0008) /* Header present */
874   {
875     y = (*dptr++) << 8; y |= *dptr++;           /* start line */
876     dptr += 2;                                  /* unknown */
877     lines = (*dptr++) << 8; lines |= *dptr++;   /* number of lines */
878     dptr += 2;                                  /* unknown */
879   }
880   else { y = 0; lines = imagey; }
881 
882 DEBUG_LEVEL1 fprintf(stderr,"d=%d y=%d lines %d\n",d,y,lines); /* POD */
883 
884   x = -1; lines++;
885   while(lines)				/* loop thru lines */
886   {
887     xaULONG xskip,cnt;
888 
889     if (x == -1)
890     {  xskip = *dptr++;  /* skip x pixels */
891        if (xskip == 0) break;  /* end of codec */
892     }
893     else xskip = 0;
894 
895 
896     cnt = *dptr++;				/* RLE code */
897 
898 DEBUG_LEVEL1 fprintf(stderr," xy <%d,%d> xskip %x cnt %x\n",x,y,xskip,cnt);
899 
900     if (cnt == 0) break; 			/* exit */
901 
902 /* this can be removed */
903     if (cnt == 0x00)  /* end of codec */
904     {
905 	  lines = 0; y++; x = -1;
906     }
907     else if (cnt == 0xff) /* skip line */
908     { lines--; y++; x = -1;
909 
910       DEBUG_LEVEL1 fprintf(stderr,"    skip line xy <%d,%d>\n",x,y);
911     }
912     else
913     {
914       if (xskip & 0x80) {lines--; y++; x = xskip & 0x7f;}
915       else x += xskip;
916 
917       DEBUG_LEVEL1 fprintf(stderr,"    cnt %x <%d,%d>\n",cnt,x,y);
918 
919       if (cnt < 0x80)				/* run of data */
920       {
921         xaUBYTE *bptr; xaUSHORT *sptr; xaULONG *lptr;
922 	if ((x11_bytes_pixel==1) || (map_flag==xaFALSE) )
923 		bptr = (xaUBYTE *)(image + (y * imagex) + (x << 4) );
924 	else if (x11_bytes_pixel==2)
925 		sptr = (xaUSHORT *)(image + 2*(y * imagex) + (x << 5) );
926         else lptr = (xaULONG *)(image + 4*(y * imagex) + (x << 6) );
927         x += cnt;
928         while(cnt--)
929         { xaULONG i,shift,d0;
930           d0 = (*dptr++ << 24);  d0 |= (*dptr++ << 16);
931 				d0 |= (*dptr++ << 8);  d0 |= *dptr++;
932           shift = 32;
933           for(i=0;i<16;i++)
934           { shift -= 2;
935             d = (d0 >> shift) & 0x03;
936             if (map_flag==xaFALSE)	 *bptr++ = (xaUBYTE)d;
937             else if (x11_bytes_pixel==1) *bptr++ = (xaUBYTE)map[d];
938             else if (x11_bytes_pixel==2) *sptr++ = (xaUSHORT)map[d];
939             else			 *lptr++ = (xaULONG)map[d];
940           }
941         }
942       } /* end run */
943       else				/* repeat data */
944       { xaULONG d0;
945         xaUBYTE *bptr; xaUSHORT *sptr; xaULONG *lptr;
946 	if ((x11_bytes_pixel==1) || (map_flag==xaFALSE) )
947 		bptr = (xaUBYTE *)(image + (y * imagex) + (x << 4) );
948 	else if (x11_bytes_pixel==2)
949 		sptr = (xaUSHORT *)(image + 2*(y * imagex) + (x << 5) );
950         else lptr = (xaULONG *)(image + 4*(y * imagex) + (x << 6) );
951         cnt = 0x100 - cnt;
952         x += cnt;
953         d0 = (*dptr++ << 24);  d0 |= (*dptr++ << 16);
954 				d0 |= (*dptr++ << 8);  d0 |= *dptr++;
955         while(cnt--)
956         { xaULONG i,shift;
957           shift = 32;
958           for(i=0;i<16;i++)
959           { shift -= 2;
960             d = (d0 >> shift) & 0x03;
961             if (map_flag==xaFALSE) 	 *bptr++ = (xaUBYTE)d;
962             else if (x11_bytes_pixel==1) *bptr++ = (xaUBYTE)map[d];
963             else if (x11_bytes_pixel==2) *sptr++ = (xaUSHORT)map[d];
964             else			 *lptr++ = (xaULONG)map[d];
965           }
966         }
967       } /* end repeat */
968     } /* end of code */
969   } /* end of lines */
970  dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
971  if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
972  else return(ACT_DLTA_NORM);
973 }
974 
975 /********************* * * * *******************************************/
QT_Decode_RLE4(image,delta,dsize,dec_info)976 xaULONG QT_Decode_RLE4(image,delta,dsize,dec_info)
977 xaUBYTE *image;         /* Image Buffer. */
978 xaUBYTE *delta;         /* delta data. */
979 xaULONG dsize;          /* delta size */
980 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
981 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
982   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
983   xaLONG x,y,lines; /* xaLONG min_x,max_x,min_y,max_y; */
984   xaULONG d;
985   xaUBYTE *dptr;
986 
987   dptr = delta;
988   dptr += 4;    /* skip codec size */
989   d = (*dptr++) << 8;  d |= *dptr++;   /* read code either 0008 or 0000 */
990   if (dsize < 8) /* NOP */
991   {
992     dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
993 DEBUG_LEVEL1 fprintf(stderr,"nop\n"); /* POD */
994     return(ACT_DLTA_NOP);
995   }
996   if (d & 0x0008) /* Header present */
997   {
998     y = (*dptr++) << 8; y |= *dptr++;           /* start line */
999     dptr += 2;                                  /* unknown */
1000     lines = (*dptr++) << 8; lines |= *dptr++;   /* number of lines */
1001     dptr += 2;                                  /* unknown */
1002   }
1003   else { y = 0; lines = imagey; }
1004 
1005 DEBUG_LEVEL1 fprintf(stderr,"d=%d y=%d lines %d\n",d,y,lines); /* POD */
1006 
1007   x = -1; lines++;
1008   while(lines)				/* loop thru lines */
1009   {
1010     xaULONG xskip,cnt;
1011 
1012     if (x == -1)
1013     {  xskip = *dptr++;  /* skip x pixels */
1014        if (xskip == 0) break;  /* end of codec */
1015     }
1016     else xskip = 0;
1017 
1018 
1019     cnt = *dptr++;				/* RLE code */
1020 
1021 DEBUG_LEVEL1 fprintf(stderr," xy <%d,%d> xskip %x cnt %x\n",x,y,xskip,cnt);
1022 
1023     if (cnt == 0) break; 			/* exit */
1024 
1025 /* this can be removed */
1026     if (cnt == 0x00)  /* end of codec */
1027     {
1028 	  lines = 0; y++; x = -1;
1029     }
1030     else if (cnt == 0xff) /* skip line */
1031     { lines--; y++; x = -1;
1032 
1033       DEBUG_LEVEL1 fprintf(stderr,"    skip line xy <%d,%d>\n",x,y);
1034     }
1035     else
1036     {
1037       if (xskip & 0x80) {lines--; y++; x = xskip & 0x7f;}
1038       else x += xskip;
1039 
1040       DEBUG_LEVEL1 fprintf(stderr,"    cnt %x <%d,%d>\n",cnt,x,y);
1041 
1042       if (cnt < 0x80)				/* run of data */
1043       {
1044         xaUBYTE *bptr; xaUSHORT *sptr; xaULONG *lptr;
1045 	if ((x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1046 		bptr = (xaUBYTE *)(image + (y * imagex) + (x << 3) );
1047 	else if (x11_bytes_pixel==2)
1048 		sptr = (xaUSHORT *)(image + 2*(y * imagex) + (x << 4) );
1049         else lptr = (xaULONG *)(image + 4*(y * imagex) + (x << 5) );
1050         x += cnt;
1051         while(cnt--)
1052         { xaULONG i,shift,d0;
1053           d0 = (*dptr++ << 24);  d0 |= (*dptr++ << 16);
1054 				d0 |= (*dptr++ << 8);  d0 |= *dptr++;
1055           shift = 32;
1056           for(i=0;i<8;i++)
1057           { shift -= 4;
1058             d = (d0 >> shift) & 0x0f;
1059             if (map_flag==xaFALSE)	 *bptr++ = (xaUBYTE)d;
1060             else if (x11_bytes_pixel==1) *bptr++ = (xaUBYTE)map[d];
1061             else if (x11_bytes_pixel==2) *sptr++ = (xaUSHORT)map[d];
1062             else			 *lptr++ = (xaULONG)map[d];
1063           }
1064         }
1065       } /* end run */
1066       else				/* repeat data */
1067       { xaULONG d0;
1068         xaUBYTE *bptr; xaUSHORT *sptr; xaULONG *lptr;
1069 	if ((x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1070 		bptr = (xaUBYTE *)(image + (y * imagex) + (x << 3) );
1071 	else if (x11_bytes_pixel==2)
1072 		sptr = (xaUSHORT *)(image + 2*(y * imagex) + (x << 4) );
1073         else lptr = (xaULONG *)(image + 4*(y * imagex) + (x << 5) );
1074         cnt = 0x100 - cnt;
1075         x += cnt;
1076         d0 = (*dptr++ << 24);  d0 |= (*dptr++ << 16);
1077 				d0 |= (*dptr++ << 8);  d0 |= *dptr++;
1078         while(cnt--)
1079         { xaULONG i,shift;
1080           shift = 32;
1081           for(i=0;i<8;i++)
1082           { shift -= 4;
1083             d = (d0 >> shift) & 0x0f;
1084             if (map_flag==xaFALSE) 	 *bptr++ = (xaUBYTE)d;
1085             else if (x11_bytes_pixel==1) *bptr++ = (xaUBYTE)map[d];
1086             else if (x11_bytes_pixel==2) *sptr++ = (xaUSHORT)map[d];
1087             else			 *lptr++ = (xaULONG)map[d];
1088           }
1089         }
1090       } /* end repeat */
1091     } /* end of code */
1092   } /* end of lines */
1093  dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
1094  if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
1095  else return(ACT_DLTA_NORM);
1096 }
1097 
1098 
1099 /********************* * * * *******************************************/
QT_Decode_RLE8(image,delta,dsize,dec_info)1100 xaULONG QT_Decode_RLE8(image,delta,dsize,dec_info)
1101 xaUBYTE *image;         /* Image Buffer. */
1102 xaUBYTE *delta;         /* delta data. */
1103 xaULONG dsize;          /* delta size */
1104 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
1105 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
1106   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
1107   xaLONG y,lines,d; /* xaLONG min_x,max_x,min_y,max_y;  */
1108   xaUBYTE *dptr;
1109 
1110   dptr = delta;
1111   dptr += 4;    /* skip codec size */
1112   d = (*dptr++) << 8;  d |= *dptr++;   /* read code either 0008 or 0000 */
1113   if (dsize < 8) /* NOP */
1114   { dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
1115     return(ACT_DLTA_NOP);
1116   }
1117   if (d & 0x0008) /* Header present */
1118   {
1119     y = (*dptr++) << 8; y |= *dptr++;           /* start line */
1120     dptr += 2;                                  /* unknown */
1121     lines = (*dptr++) << 8; lines |= *dptr++;   /* number of lines */
1122     dptr += 2;                                  /* unknown */
1123   }
1124   else { y = 0; lines = imagey; }
1125   while(lines--)
1126   {
1127     xaULONG xskip,cnt;
1128     xskip = *dptr++;				/* skip x pixels */
1129     if (xskip==0) break;			/* exit */
1130     cnt = *dptr++;				/* RLE code */
1131     if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1132     { xaUBYTE *iptr = (xaUBYTE *)(image + (y * imagex) + (4 * (xskip-1)) );
1133       while(cnt != 0xff)
1134       {
1135         if (cnt == 0x00) { xskip = *dptr++; iptr += 4 * (xskip-1); }
1136         else if (cnt < 0x80)			/* run of data */
1137         {
1138           cnt *= 4; if (map_flag==xaFALSE) while(cnt--) *iptr++ = (xaUBYTE)*dptr++;
1139           else while(cnt--) *iptr++ = (xaUBYTE)map[*dptr++];
1140         } else					/* repeat data */
1141         { xaUBYTE d1,d2,d3,d4;	cnt = 0x100 - cnt;
1142           if (map_flag==xaTRUE) { d1=(xaUBYTE)map[*dptr++]; d2=(xaUBYTE)map[*dptr++];
1143 			      d3=(xaUBYTE)map[*dptr++]; d4=(xaUBYTE)map[*dptr++]; }
1144 	  else	{ d1 = (xaUBYTE)*dptr++; d2 = (xaUBYTE)*dptr++;
1145 		  d3 = (xaUBYTE)*dptr++; d4 = (xaUBYTE)*dptr++; }
1146           while(cnt--) { *iptr++ =d1; *iptr++ =d2; *iptr++ =d3; *iptr++ =d4; }
1147         } /* end of  >= 0x80 */
1148         cnt = *dptr++;
1149       } /* end while cnt */
1150     } else if (x11_bytes_pixel==2)
1151     { xaUSHORT *iptr = (xaUSHORT *)(image + 2 *((y * imagex) + (4 * (xskip-1))) );
1152       while(cnt != 0xff)
1153       {
1154         if (cnt == 0x00) { xskip = *dptr++; iptr += 4 * (xskip-1); }
1155         else if (cnt < 0x80)			/* run of data */
1156         {
1157           cnt *= 4; while(cnt--) *iptr++ = (xaUSHORT)map[*dptr++];
1158         } else					/* repeat data */
1159         { xaUSHORT d1,d2,d3,d4;	cnt = 0x100 - cnt;
1160 	  { d1 = (xaUSHORT)map[*dptr++]; d2 = (xaUSHORT)map[*dptr++];
1161 	    d3 = (xaUSHORT)map[*dptr++]; d4 = (xaUSHORT)map[*dptr++]; }
1162           while(cnt--) { *iptr++ =d1; *iptr++ =d2; *iptr++ =d3; *iptr++ =d4; }
1163         } /* end of  >= 0x80 */
1164         cnt = *dptr++;
1165       } /* end while cnt */
1166     } else /* bytes == 4 */
1167     { xaULONG *iptr = (xaULONG *)(image + 4 * ((y * imagex) + (4 * (xskip-1))) );
1168       while(cnt != 0xff)
1169       {
1170         if (cnt == 0x00) { xskip = *dptr++; iptr += 4 * (xskip-1); }
1171         else if (cnt < 0x80)			/* run of data */
1172         {
1173           cnt *= 4; while(cnt--) *iptr++ = (xaULONG)map[*dptr++];
1174         } else					/* repeat data */
1175         { xaULONG d1,d2,d3,d4; cnt = 0x100 - cnt;
1176 	  { d1 = (xaULONG)map[*dptr++]; d2 = (xaULONG)map[*dptr++];
1177 	    d3 = (xaULONG)map[*dptr++]; d4 = (xaULONG)map[*dptr++]; }
1178           while(cnt--) { *iptr++ =d1; *iptr++ =d2; *iptr++ =d3; *iptr++ =d4; }
1179         } /* end of  >= 0x80 */
1180         cnt = *dptr++;
1181       } /* end while cnt */
1182     }
1183     y++;
1184   } /* end of lines */
1185  /* one more zero byte */
1186  dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
1187  if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
1188  else return(ACT_DLTA_NORM);
1189 }
1190 
1191 /********************* * * * *******************************************/
QT_Decode_RLE16(image,delta,dsize,dec_info)1192 xaULONG QT_Decode_RLE16(image,delta,dsize,dec_info)
1193 xaUBYTE *image;         /* Image Buffer. */
1194 xaUBYTE *delta;         /* delta data. */
1195 xaULONG dsize;          /* delta size */
1196 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
1197 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
1198   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
1199   xaULONG special = dec_info->special;
1200   XA_CHDR *chdr = dec_info->chdr;
1201   xaLONG y,d,lines; /* xaLONG min_x,max_x,min_y,max_y; */
1202   xaULONG special_flag;
1203   xaUBYTE r,g,b,*dptr;
1204 
1205   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
1206 
1207   special_flag = special & 0x0001;
1208 
1209   dptr = delta;
1210   dptr += 4;    /* skip codec size */
1211   d = (*dptr++) << 8;  d |= *dptr++;   /* read code either 0008 or 0000 */
1212   if (dsize < 8) /* NOP */
1213   {
1214     dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
1215     return(ACT_DLTA_NOP);
1216   }
1217   if (d & 0x0008) /* Header present */
1218   {
1219     y = (*dptr++) << 8; y |= *dptr++;           /* start line */
1220     dptr += 2;                                  /* unknown */
1221     lines = (*dptr++) << 8; lines |= *dptr++;   /* number of lines */
1222     dptr += 2;                                  /* unknown */
1223   }
1224   else { y = 0; lines = imagey; }
1225   while(lines--)				/* loop thru lines */
1226   {
1227     xaULONG d,xskip,cnt;
1228     xskip = *dptr++;				/* skip x pixels */
1229     if (xskip==0) break;			/* exit */
1230     cnt = *dptr++;				/* RLE code */
1231 
1232     if (special_flag)
1233     { xaUBYTE *iptr = (xaUBYTE *)(image + 3*((y * imagex) + (xskip-1)) );
1234       while(cnt != 0xff)				/* while not EOL */
1235       {
1236         if (cnt == 0x00) { xskip = *dptr++; iptr += 3*(xskip-1); }
1237         else if (cnt < 0x80)				/* run of data */
1238           while(cnt--) { d = (*dptr++ << 8); d |= *dptr++;
1239 			 QT_Get_RGBColor(&r,&g,&b,d);
1240 			*iptr++ = r; *iptr++ = g; *iptr++ = b; }
1241         else						/* repeat data */
1242         { cnt = 0x100 - cnt; d = (*dptr++ << 8); d |= *dptr++;
1243           QT_Get_RGBColor(&r,&g,&b,d);
1244           while(cnt--) { *iptr++ = r; *iptr++ = g; *iptr++ = b; }
1245         }
1246         cnt = *dptr++;				/* get new RLE code */
1247       } /* end of line */
1248     }
1249     else if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1250     { xaUBYTE *iptr = (xaUBYTE *)(image + (y * imagex) + (xskip-1) );
1251       while(cnt != 0xff)				/* while not EOL */
1252       {
1253         if (cnt == 0x00) { xskip = *dptr++; iptr += (xskip-1); }
1254         else if (cnt < 0x80)				/* run of data */
1255           while(cnt--) { d = (*dptr++ << 8); d |= *dptr++;
1256 		*iptr++ = (xaUBYTE)QT_Get_Color(d,map_flag,map,chdr); }
1257         else						/* repeat data */
1258         { cnt = 0x100 - cnt; d = (*dptr++ << 8); d |= *dptr++;
1259           d = QT_Get_Color(d,map_flag,map,chdr);
1260           while(cnt--) { *iptr++ = (xaUBYTE)d; }
1261         }
1262         cnt = *dptr++;				/* get new RLE code */
1263       } /* end of line */
1264     }
1265     else if (x11_bytes_pixel==4)
1266     { xaULONG *iptr = (xaULONG *)(image + 4*((y * imagex)+(xskip-1)) );
1267       while(cnt != 0xff)				/* while not EOL */
1268       {
1269         if (cnt == 0x00) { xskip = *dptr++; iptr += (xskip-1); }
1270         else if (cnt < 0x80)				/* run of data */
1271           while(cnt--) { d = (*dptr++ << 8); d |= *dptr++;
1272 		*iptr++ = (xaULONG)QT_Get_Color(d,map_flag,map,chdr); }
1273         else						/* repeat data */
1274         { cnt = 0x100 - cnt; d = (*dptr++ << 8); d |= *dptr++;
1275           d = QT_Get_Color(d,map_flag,map,chdr);
1276           while(cnt--) { *iptr++ = (xaULONG)d; }
1277         }
1278         cnt = *dptr++;				/* get new RLE code */
1279       } /* end of line */
1280     }
1281     else /* if (x11_bytes_pixel==2) */
1282     { xaUSHORT *iptr = (xaUSHORT *)(image + 2*((y * imagex)+(xskip-1)) );
1283       while(cnt != 0xff)				/* while not EOL */
1284       {
1285         if (cnt == 0x00) { xskip = *dptr++; iptr += (xskip-1); }
1286         else if (cnt < 0x80)				/* run of data */
1287           while(cnt--) { d = (*dptr++ << 8); d |= *dptr++;
1288 		*iptr++ = (xaUSHORT)QT_Get_Color(d,map_flag,map,chdr); }
1289         else						/* repeat data */
1290         { cnt = 0x100 - cnt; d = (*dptr++ << 8); d |= *dptr++;
1291           d = QT_Get_Color(d,map_flag,map,chdr);
1292           while(cnt--) { *iptr++ = (xaUSHORT)d; }
1293         }
1294         cnt = *dptr++;				/* get new RLE code */
1295       } /* end of line */
1296     }
1297     y++;
1298   } /* end of lines */
1299  /* one more zero byte */
1300  dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
1301  if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
1302  else return(ACT_DLTA_NORM);
1303 }
1304 
1305 /********************* * * * *******************************************/
QT_Decode_RLE24(image,delta,dsize,dec_info)1306 xaULONG QT_Decode_RLE24(image,delta,dsize,dec_info)
1307 xaUBYTE *image;         /* Image Buffer. */
1308 xaUBYTE *delta;         /* delta data. */
1309 xaULONG dsize;          /* delta size */
1310 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
1311 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
1312   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
1313   xaULONG special = dec_info->special;
1314   XA_CHDR *chdr = dec_info->chdr;
1315   xaLONG y,d,lines; /* xaULONG min_x,max_x,min_y,max_y; */
1316   xaULONG special_flag,r,g,b;
1317   xaUBYTE *dptr;
1318 
1319   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
1320 
1321   special_flag = special & 0x0001;
1322 
1323   dptr = delta;
1324   dptr += 4;    /* skip codec size */
1325   d = (*dptr++) << 8;  d |= *dptr++;   /* read code either 0008 or 0000 */
1326   if (dsize < 8) /* NOP */
1327   {  dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
1328     return(ACT_DLTA_NOP);
1329   }
1330   if (d & 0x0008) /* Header present */
1331   { y = (*dptr++) << 8; y |= *dptr++;		/* start line */
1332     dptr += 2;					/* unknown */
1333     lines = (*dptr++) << 8; lines |= *dptr++;	/* number of lines */
1334     dptr += 2;					/* unknown */
1335   }
1336   else { y = 0; lines = imagey; }
1337 
1338   while(lines--)				/* loop thru lines */
1339   { xaULONG d,xskip,cnt;
1340     xskip = *dptr++;				/* skip x pixels */
1341     if (xskip == 0) break; 			/* exit */
1342     cnt = *dptr++;				/* RLE code */
1343 
1344     if (special_flag)
1345     { xaUBYTE *iptr = (xaUBYTE *)(image + 3*((y * imagex) + (xskip-1)) );
1346       while(cnt != 0xff)				/* while not EOL */
1347       { if (cnt == 0x00) { xskip = *dptr++; iptr += 3*(xskip-1); }
1348         else if (cnt < 0x80)				/* run of data */
1349           while(cnt--) { r = *dptr++; g = *dptr++; b = *dptr++;
1350 			*iptr++ = (xaUBYTE)r; *iptr++ = (xaUBYTE)g;
1351 					    *iptr++ = (xaUBYTE)b; }
1352         else						/* repeat data */
1353         { cnt = 0x100 - cnt; r = *dptr++; g = *dptr++; b = *dptr++;
1354           while(cnt--) { *iptr++ = (xaUBYTE)r; *iptr++ = (xaUBYTE)g;
1355 					     *iptr++ = (xaUBYTE)b; }
1356         }
1357         cnt = *dptr++;				/* get new RLE code */
1358       } /* end of line */
1359     }
1360     else if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1361     { xaUBYTE *iptr = (xaUBYTE *)(image + (y * imagex) + (xskip-1) );
1362       while(cnt != 0xff)				/* while not EOL */
1363       { if (cnt == 0x00) { xskip = *dptr++; iptr += (xskip-1); }
1364         else if (cnt < 0x80)				/* run of data */
1365           while(cnt--) { r = *dptr++; g = *dptr++; b = *dptr++;
1366 		*iptr++ = (xaUBYTE)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr); }
1367         else						/* repeat data */
1368         { cnt = 0x100 - cnt; r = *dptr++; g = *dptr++; b = *dptr++;
1369           d = XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
1370           while(cnt--) { *iptr++ = (xaUBYTE)d; }
1371         }
1372         cnt = *dptr++;				/* get new RLE code */
1373       } /* end of line */
1374     }
1375     else if (x11_bytes_pixel==4)
1376     { xaULONG *iptr = (xaULONG *)(image + 4*((y * imagex)+(xskip-1)) );
1377       while(cnt != 0xff)				/* while not EOL */
1378       { if (cnt == 0x00) { xskip = *dptr++; iptr += (xskip-1); }
1379         else if (cnt < 0x80)				/* run of data */
1380           while(cnt--) { r = *dptr++; g = *dptr++; b = *dptr++;
1381 		*iptr++ = (xaULONG)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr); }
1382         else						/* repeat data */
1383         { cnt = 0x100 - cnt; r = *dptr++; g = *dptr++; b = *dptr++;
1384           d = XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
1385           while(cnt--) { *iptr++ = (xaULONG)d; }
1386         }
1387         cnt = *dptr++;				/* get new RLE code */
1388       } /* end of line */
1389     }
1390     else /* if (x11_bytes_pixel==2) */
1391     { xaUSHORT *iptr = (xaUSHORT *)(image + 2*((y * imagex)+(xskip-1)) );
1392       while(cnt != 0xff)				/* while not EOL */
1393       {
1394         if (cnt == 0x00) { xskip = *dptr++; iptr += (xskip-1); }
1395         else if (cnt < 0x80)				/* run of data */
1396           while(cnt--) { r = *dptr++; g = *dptr++; b = *dptr++;
1397 	       *iptr++ = (xaUSHORT)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr); }
1398         else						/* repeat data */
1399         { cnt = 0x100 - cnt; r = *dptr++; g = *dptr++; b = *dptr++;
1400           d = XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
1401           while(cnt--) { *iptr++ = (xaUSHORT)d; }
1402         }
1403         cnt = *dptr++;				/* get new RLE code */
1404       } /* end of line */
1405     }
1406     y++;
1407   } /* end of lines */
1408  /* one more zero byte */
1409   dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
1410   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
1411   else return(ACT_DLTA_NORM);
1412 }
1413 
1414 /********************* * * * *******************************************/
QT_Decode_RLE32(image,delta,dsize,dec_info)1415 xaULONG QT_Decode_RLE32(image,delta,dsize,dec_info)
1416 xaUBYTE *image;         /* Image Buffer. */
1417 xaUBYTE *delta;         /* delta data. */
1418 xaULONG dsize;          /* delta size */
1419 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
1420 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
1421   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
1422   xaULONG special = dec_info->special;
1423   XA_CHDR *chdr = dec_info->chdr;
1424   xaLONG y,d,lines; /* xaULONG min_x,max_x,min_y,max_y; */
1425   xaULONG special_flag,r,g,b;
1426   xaUBYTE *dp;
1427 
1428   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
1429 
1430   special_flag = special & 0x0001;
1431 
1432   dp = delta;
1433   dp += 4;    /* skip codec size */
1434   d = (*dp++) << 8;  d |= *dp++;   /* read code either 0008 or 0000 */
1435   if (dsize < 8) /* NOP */
1436   {
1437     dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
1438     return(ACT_DLTA_NOP);
1439   }
1440   if (d & 0x0008) /* Header present */
1441   {
1442     y = (*dp++) << 8; y |= *dp++;           /* start line */
1443     dp += 2;                                  /* unknown */
1444     lines = (*dp++) << 8; lines |= *dp++;   /* number of lines */
1445     dp += 2;                                  /* unknown */
1446   }
1447   else { y = 0; lines = imagey; }
1448   while(lines--)				/* loop thru lines */
1449   {
1450     xaULONG d,xskip,cnt;
1451     xskip = *dp++;				/* skip x pixels */
1452     if (xskip == 0) break;			/* exit */
1453     cnt = *dp++;				/* RLE code */
1454 
1455     if (special_flag)
1456     { xaUBYTE *iptr = (xaUBYTE *)(image + 3*((y * imagex) + (xskip-1)) );
1457       while(cnt != 0xff)				/* while not EOL */
1458       {
1459         if (cnt == 0x00) { xskip = *dp++; iptr += 3*(xskip-1); }
1460         else if (cnt < 0x80)				/* run of data */
1461           while(cnt--) { dp++; r = *dp++; g = *dp++; b = *dp++;
1462 			*iptr++ = (xaUBYTE)r; *iptr++ = (xaUBYTE)g;
1463 					    *iptr++ = (xaUBYTE)b; }
1464         else						/* repeat data */
1465         { cnt = 0x100 - cnt; dp++; r = *dp++; g = *dp++; b = *dp++;
1466           while(cnt--) { *iptr++ = (xaUBYTE)r; *iptr++ = (xaUBYTE)g;
1467 					     *iptr++ = (xaUBYTE)b; }
1468         }
1469         cnt = *dp++;				/* get new RLE code */
1470       } /* end of line */
1471     }
1472     else if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1473     { xaUBYTE *iptr = (xaUBYTE *)(image + (y * imagex) + (xskip-1) );
1474       while(cnt != 0xff)				/* while not EOL */
1475       {
1476         if (cnt == 0x00) { xskip = *dp++; iptr += (xskip-1); }
1477         else if (cnt < 0x80)				/* run of data */
1478           while(cnt--) { dp++; r = *dp++; g = *dp++; b = *dp++;
1479 		*iptr++ = (xaUBYTE)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr); }
1480         else						/* repeat data */
1481         { cnt = 0x100 - cnt; dp++; r = *dp++; g = *dp++; b = *dp++;
1482           d = XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
1483           while(cnt--) { *iptr++ = (xaUBYTE)d; }
1484         }
1485         cnt = *dp++;				/* get new RLE code */
1486       } /* end of line */
1487     }
1488     else if (x11_bytes_pixel==4)
1489     { xaULONG *iptr = (xaULONG *)(image + 4*((y * imagex)+(xskip-1)) );
1490       while(cnt != 0xff)				/* while not EOL */
1491       {
1492         if (cnt == 0x00) { xskip = *dp++; iptr += (xskip-1); }
1493         else if (cnt < 0x80)				/* run of data */
1494           while(cnt--) { dp++; r = *dp++; g = *dp++; b = *dp++;
1495 		*iptr++ = (xaULONG)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr); }
1496         else						/* repeat data */
1497         { cnt = 0x100 - cnt; dp++; r = *dp++; g = *dp++; b = *dp++;
1498           d = XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
1499           while(cnt--) { *iptr++ = (xaULONG)d; }
1500         }
1501         cnt = *dp++;				/* get new RLE code */
1502       } /* end of line */
1503     }
1504     else /* if (x11_bytes_pixel==2) */
1505     { xaUSHORT *iptr = (xaUSHORT *)(image + 2*((y * imagex)+(xskip-1)) );
1506       while(cnt != 0xff)				/* while not EOL */
1507       {
1508         if (cnt == 0x00) { xskip = *dp++; iptr += (xskip-1); }
1509         else if (cnt < 0x80)				/* run of data */
1510           while(cnt--) { dp++; r = *dp++; g = *dp++; b = *dp++;
1511 		*iptr++ = (xaUSHORT)XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr); }
1512         else						/* repeat data */
1513         { cnt = 0x100 - cnt; dp++; r = *dp++; g = *dp++; b = *dp++;
1514           d = XA_RGB24_To_CLR32(r,g,b,map_flag,map,chdr);
1515           while(cnt--) { *iptr++ = (xaUSHORT)d; }
1516         }
1517         cnt = *dp++;				/* get new RLE code */
1518       } /* end of line */
1519     }
1520     y++;
1521   } /* end of lines */
1522  /* one more zero byte */
1523   dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
1524  if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
1525  else return(ACT_DLTA_NORM);
1526 }
1527 
1528 /********************* * * * *******************************************/
1529 #define QT_BLOCK_INC(x,y,imagex) { x += 4; if (x>=imagex) { x=0; y += 4; }}
1530 
1531 #define QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,binc,rinc) \
1532 { x += 4; im0 += binc; im1 += binc; im2 += binc; im3 += binc;	\
1533   if (x>=imagex)					\
1534   { x=0; y += 4; im0 += rinc; im1 += rinc; im2 += rinc; im3 += rinc; } }
1535 
1536 #define QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y) {	\
1537   if (x > max_x) max_x=x; if (y > max_y) max_y=y;	\
1538   if (x < min_x) min_x=x; if (y < min_y) min_y=y;  }
1539 
1540 #define QT_RPZA_C1(ip0,ip1,ip2,ip3,c,CAST) { \
1541  ip0[0] = ip0[1] = ip0[2] = ip0[3] = (CAST)c; \
1542  ip1[0] = ip1[1] = ip1[2] = ip1[3] = (CAST)c; \
1543  ip2[0] = ip2[1] = ip2[2] = ip2[3] = (CAST)c; \
1544  ip3[0] = ip3[1] = ip3[2] = ip3[3] = (CAST)c; }
1545 
1546 #define QT_RPZA_C4(ip,c,mask,CAST); { \
1547  ip[0] =(CAST)(c[((mask>>6)&0x03)]); ip[1] =(CAST)(c[((mask>>4)&0x03)]); \
1548  ip[2] =(CAST)(c[((mask>>2)&0x03)]); ip[3] =(CAST)(c[ (mask & 0x03)]); }
1549 
1550 #define QT_RPZA_C16(ip0,ip1,ip2,ip3,c,CAST) { \
1551  ip0[0] = (CAST)(*c++); ip0[1] = (CAST)(*c++); \
1552  ip0[2] = (CAST)(*c++); ip0[3] = (CAST)(*c++); \
1553  ip1[0] = (CAST)(*c++); ip1[1] = (CAST)(*c++); \
1554  ip1[2] = (CAST)(*c++); ip1[3] = (CAST)(*c++); \
1555  ip2[0] = (CAST)(*c++); ip2[1] = (CAST)(*c++); \
1556  ip2[2] = (CAST)(*c++); ip2[3] = (CAST)(*c++); \
1557  ip3[0] = (CAST)(*c++); ip3[1] = (CAST)(*c++); \
1558  ip3[2] = (CAST)(*c++); ip3[3] = (CAST)(*c  ); }
1559 
1560 #define QT_RPZA_rgbC1(ip,r,g,b) { \
1561  ip[0] = ip[3] = ip[6] = ip[9]  = r; \
1562  ip[1] = ip[4] = ip[7] = ip[10] = g; \
1563  ip[2] = ip[5] = ip[8] = ip[11] = b; }
1564 
1565 #define QT_RPZA_rgbC4(ip,r,g,b,mask); { xaULONG _idx; \
1566  _idx = (mask>>6)&0x03; ip[0] = r[_idx]; ip[1] = g[_idx]; ip[2] = b[_idx]; \
1567  _idx = (mask>>4)&0x03; ip[3] = r[_idx]; ip[4] = g[_idx]; ip[5] = b[_idx]; \
1568  _idx = (mask>>2)&0x03; ip[6] = r[_idx]; ip[7] = g[_idx]; ip[8] = b[_idx]; \
1569  _idx =  mask    &0x03; ip[9] = r[_idx]; ip[10] = g[_idx]; ip[11] = b[_idx]; }
1570 
1571 #define QT_RPZA_rgbC16(ip,r,g,b) { \
1572  ip[0]= *r++; ip[1]= *g++; ip[2]= *b++; \
1573  ip[3]= *r++; ip[4]= *g++; ip[5]= *b++; \
1574  ip[6]= *r++; ip[7]= *g++; ip[8]= *b++; \
1575  ip[9]= *r++; ip[10]= *g++; ip[11]= *b++; }
1576 
1577 
1578 #define RPZA_DITH_COL2RGB(_r,_g,_b,_col) { \
1579 _r = (_col & 0x7c00); _g = (_col & 0x03e0); _b = (_col & 0x001f);	\
1580 _r = _r | (_r >> 5);  _g = (_g << 5) | _g; _b = (_b << 10) | (_b << 5); }
1581 
1582 #define RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col) { xaLONG r1,g1,b1; \
1583   r1 = (xaLONG)rnglimit[(r + re) >> 7];	\
1584   g1 = (xaLONG)rnglimit[(g + ge) >> 7];	\
1585   b1 = (xaLONG)rnglimit[(b + be) >> 7];	\
1586   col = (r1 & 0xe0) | ((g1 & 0xe0) >> 3) | ((b1 & 0xc0) >> 6); }
1587 
1588 #define RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap) { \
1589   re =  ((xaLONG)(r) - (xaLONG)(cmap[col].red   >> 1)) >> 1; \
1590   ge =  ((xaLONG)(g) - (xaLONG)(cmap[col].green >> 1)) >> 1; \
1591   be =  ((xaLONG)(b) - (xaLONG)(cmap[col].blue  >> 1)) >> 1; }
1592 
1593 
1594 /********************* * * * *******************************************/
QT_Decode_RPZA(image,delta,dsize,dec_info)1595 xaULONG QT_Decode_RPZA(image,delta,dsize,dec_info)
1596 xaUBYTE *image;         /* Image Buffer. */
1597 xaUBYTE *delta;         /* delta data. */
1598 xaULONG dsize;          /* delta size */
1599 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
1600 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
1601   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
1602   xaULONG special = dec_info->special;
1603   XA_CHDR *chdr = dec_info->chdr;
1604   xaLONG x,y,len,row_inc,blk_inc;
1605   xaLONG min_x,max_x,min_y,max_y;
1606   xaUBYTE *dptr = delta;
1607   xaULONG code,changed;
1608   xaUBYTE *im0,*im1,*im2,*im3;
1609 
1610   dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
1611 
1612   dptr++;			/* skip  0xe1 */
1613   len =(*dptr++)<<16; len |= (*dptr++)<< 8; len |= (*dptr++); /* Read Len */
1614   /* CHECK FOR CORRUPTION - FAIRLY COMMON */
1615   if (len != dsize)
1616   { if (xa_verbose==xaTRUE)
1617 	fprintf(stderr,"QT corruption-skipping this frame %x %x\n",dsize,len);
1618     return(ACT_DLTA_NOP);
1619   }
1620   len -= 4;				/* read 4 already */
1621 
1622   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
1623 
1624    /* special case dither routine - fill out a bit */
1625   if (   (!special) && (x11_bytes_pixel==1)
1626       && (chdr) && (x11_display_type == XA_PSEUDOCOLOR)
1627       && (cmap_color_func != 4)
1628       && (cmap_true_to_332 == xaTRUE) && (x11_cmap_size == 256)
1629       && (xa_dither_flag==xaTRUE)
1630      )
1631      return( QT_RPZA_Dither(image,dptr,len,dec_info) );
1632 
1633 
1634   max_x = max_y = 0; min_x = imagex; min_y = imagey; changed = 0;
1635   x = y = 0;
1636   if (special) blk_inc = 3;
1637   else if ( (x11_bytes_pixel==1) || (map_flag == xaFALSE) ) blk_inc = 1;
1638   else if (x11_bytes_pixel==2) blk_inc = 2;
1639   else blk_inc = 4;
1640   row_inc = blk_inc * imagex;
1641   blk_inc *= 4;
1642   im1 = im0 = image;	im1 += row_inc;
1643   im2 = im1;		im2 += row_inc;
1644   im3 = im2;		im3 += row_inc;
1645   row_inc *= 3; /* skip 3 rows at a time */
1646 
1647 
1648   while(len > 0)
1649   { code = *dptr++; len--;
1650 
1651     if ( (code >= 0xa0) && (code <= 0xbf) )			/* SINGLE */
1652     {
1653       xaULONG color,skip;
1654       changed = 1;
1655       color = (*dptr++) << 8; color |= *dptr++; len -= 2;
1656       skip = (code-0x9f);
1657       if (special)
1658       { xaUBYTE r,g,b;
1659         QT_Get_RGBColor(&r,&g,&b,color);
1660         while(skip--)
1661         { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1; xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
1662 	  QT_RPZA_rgbC1(ip0,r,g,b);
1663 	  QT_RPZA_rgbC1(ip1,r,g,b);
1664 	  QT_RPZA_rgbC1(ip2,r,g,b);
1665 	  QT_RPZA_rgbC1(ip3,r,g,b);
1666 	  QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
1667 	  QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
1668 	}
1669       }
1670       else /* not special */
1671       {
1672         color = QT_Get_Color(color,map_flag,map,chdr);
1673         while(skip--)
1674         {
1675           if ( (x11_bytes_pixel==1) || (map_flag == xaFALSE) )
1676           { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1; xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
1677 	    QT_RPZA_C1(ip0,ip1,ip2,ip3,color,xaUBYTE);
1678 	  }
1679           else if (x11_bytes_pixel==4)
1680           { xaULONG *ip0= (xaULONG *)im0; xaULONG *ip1= (xaULONG *)im1;
1681 	    xaULONG *ip2= (xaULONG *)im2; xaULONG *ip3= (xaULONG *)im3;
1682 	    QT_RPZA_C1(ip0,ip1,ip2,ip3,color,xaULONG);
1683 	  }
1684           else /* if (x11_bytes_pixel==2) */
1685           { xaUSHORT *ip0= (xaUSHORT *)im0; xaUSHORT *ip1= (xaUSHORT *)im1;
1686 	    xaUSHORT *ip2= (xaUSHORT *)im2; xaUSHORT *ip3= (xaUSHORT *)im3;
1687 	    QT_RPZA_C1(ip0,ip1,ip2,ip3,color,xaUSHORT);
1688 	  }
1689 	  QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
1690 	  QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
1691         } /* end of skip-- */
1692       } /* end not special */
1693     }
1694     else if ( (code >= 0x80) && (code <= 0x9f) )		/* SKIP */
1695     { xaULONG skip = (code-0x7f);
1696       while(skip--)
1697 		QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
1698     }
1699     else if ( (code < 0x80) 				/* FOUR/SIXTEEN */
1700 	     || ((code >= 0xc0) && (code <= 0xdf)) )
1701     { xaULONG cA,cB;
1702       changed = 1;
1703       /* Get 1st two colors */
1704       if (code >= 0xc0) { cA = (*dptr++) << 8; cA |= *dptr++; len -= 2; }
1705       else {cA = (code << 8) | *dptr++; len -= 1;}
1706       cB = (*dptr++) << 8; cB |= *dptr++; len -= 2;
1707 
1708       /****** SIXTEEN COLOR *******/
1709       if ( (code < 0x80) && ((cB & 0x8000)==0) ) /* 16 color */
1710       {
1711         xaULONG i,d,*clr,c[16];
1712         xaUBYTE r[16],g[16],b[16];
1713 	if (special)
1714 	{ QT_Get_RGBColor(&r[0],&g[0],&b[0],cA);
1715           QT_Get_RGBColor(&r[1],&g[1],&b[1],cB);
1716           for(i=2; i<16; i++)
1717           {
1718             d = (*dptr++) << 8; d |= *dptr++; len -= 2;
1719             QT_Get_RGBColor(&r[i],&g[i],&b[i],d);
1720           }
1721 	}
1722 	else
1723 	{
1724 	  clr = c;
1725           *clr++ = QT_Get_Color(cA,map_flag,map,chdr);
1726           *clr++ = QT_Get_Color(cB,map_flag,map,chdr);
1727           for(i=2; i<16; i++)
1728           {
1729             d = (*dptr++) << 8; d |= *dptr++; len -= 2;
1730             *clr++ = QT_Get_Color(d,map_flag,map,chdr);
1731           }
1732 	}
1733 	clr = c;
1734 	if (special)
1735         { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1; xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
1736 	  xaUBYTE *tr,*tg,*tb; tr=r; tg=g; tb=b;
1737 	  QT_RPZA_rgbC16(ip0,tr,tg,tb);
1738 	  QT_RPZA_rgbC16(ip1,tr,tg,tb);
1739 	  QT_RPZA_rgbC16(ip2,tr,tg,tb);
1740 	  QT_RPZA_rgbC16(ip3,tr,tg,tb);
1741 	}
1742 	else if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1743         { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1; xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
1744 	  QT_RPZA_C16(ip0,ip1,ip2,ip3,clr,xaUBYTE);
1745 	}
1746 	else if (x11_bytes_pixel==4)
1747         { xaULONG *ip0= (xaULONG *)im0; xaULONG *ip1= (xaULONG *)im1;
1748 	  xaULONG *ip2= (xaULONG *)im2; xaULONG *ip3= (xaULONG *)im3;
1749 	  QT_RPZA_C16(ip0,ip1,ip2,ip3,clr,xaULONG);
1750 	}
1751 	else /* if (x11_bytes_pixel==2) */
1752         { xaUSHORT *ip0= (xaUSHORT *)im0; xaUSHORT *ip1= (xaUSHORT *)im1;
1753 	  xaUSHORT *ip2= (xaUSHORT *)im2; xaUSHORT *ip3= (xaUSHORT *)im3;
1754 	  QT_RPZA_C16(ip0,ip1,ip2,ip3,clr,xaUSHORT);
1755 	}
1756 	QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
1757 	QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
1758       } /*** END of SIXTEEN COLOR */
1759       else					/****** FOUR COLOR *******/
1760       { xaULONG m_cnt,msk0,msk1,msk2,msk3,c[4];
1761 	xaUBYTE r[4],g[4],b[4];
1762 
1763         if (code < 0x80) m_cnt = 1;
1764 	else m_cnt = code - 0xbf;
1765 
1766 	if (special)		QT_Get_AV_RGBColors(c,r,g,b,cA,cB);
1767 	else			QT_Get_AV_Colors(c,cA,cB,map_flag,map,chdr);
1768 
1769         while(m_cnt--)
1770         { msk0 = *dptr++; msk1 = *dptr++;
1771 	  msk2 = *dptr++; msk3 = *dptr++; len -= 4;
1772 	  if (special)
1773           { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1; xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
1774 	    QT_RPZA_rgbC4(ip0,r,g,b,msk0);
1775 	    QT_RPZA_rgbC4(ip1,r,g,b,msk1);
1776 	    QT_RPZA_rgbC4(ip2,r,g,b,msk2);
1777 	    QT_RPZA_rgbC4(ip3,r,g,b,msk3);
1778 	  }
1779 	  else if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
1780           { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1; xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
1781 	    QT_RPZA_C4(ip0,c,msk0,xaUBYTE);
1782 	    QT_RPZA_C4(ip1,c,msk1,xaUBYTE);
1783 	    QT_RPZA_C4(ip2,c,msk2,xaUBYTE);
1784 	    QT_RPZA_C4(ip3,c,msk3,xaUBYTE);
1785 	  }
1786 	  else if (x11_bytes_pixel==4)
1787           { xaULONG *ip0= (xaULONG *)im0; xaULONG *ip1= (xaULONG *)im1;
1788 	    xaULONG *ip2= (xaULONG *)im2; xaULONG *ip3= (xaULONG *)im3;
1789 	    QT_RPZA_C4(ip0,c,msk0,xaULONG);
1790 	    QT_RPZA_C4(ip1,c,msk1,xaULONG);
1791 	    QT_RPZA_C4(ip2,c,msk2,xaULONG);
1792 	    QT_RPZA_C4(ip3,c,msk3,xaULONG);
1793 	  }
1794 	  else /* if (x11_bytes_pixel==2) */
1795           { xaUSHORT *ip0= (xaUSHORT *)im0; xaUSHORT *ip1= (xaUSHORT *)im1;
1796 	    xaUSHORT *ip2= (xaUSHORT *)im2; xaUSHORT *ip3= (xaUSHORT *)im3;
1797 	    QT_RPZA_C4(ip0,c,msk0,xaUSHORT);
1798 	    QT_RPZA_C4(ip1,c,msk1,xaUSHORT);
1799 	    QT_RPZA_C4(ip2,c,msk2,xaUSHORT);
1800 	    QT_RPZA_C4(ip3,c,msk3,xaUSHORT);
1801 	  }
1802 	  QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
1803 	  QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
1804         }
1805       } /*** END of FOUR COLOR *******/
1806     } /*** END of 4/16 COLOR BLOCKS ****/
1807     else /* UNKNOWN */
1808     {
1809       fprintf(stderr,"QT RPZA: Unknown %x\n",code);
1810       return(ACT_DLTA_NOP);
1811     }
1812   }
1813   if (xa_optimize_flag == xaTRUE)
1814   {
1815     if (changed) { dec_info->xs=min_x; dec_info->ys=min_y;
1816 		   dec_info->xe=max_x + 4; dec_info->ye=max_y + 4; }
1817     else  { dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
1818 	    return(ACT_DLTA_NOP); }
1819   }
1820   else { dec_info->xs = dec_info->ys = 0;
1821 	 dec_info->xe = imagex; dec_info->ye = imagey; }
1822   if (map_flag) return(ACT_DLTA_MAPD);
1823   else return(ACT_DLTA_NORM);
1824 }
1825 
QT_Get_RGBColor(r,g,b,color)1826 void QT_Get_RGBColor(r,g,b,color)
1827 xaUBYTE *r,*g,*b;
1828 xaULONG color;
1829 { xaULONG ra,ga,ba;
1830   ra = (color >> 10) & 0x1f;	ra = (ra << 3) | (ra >> 2);
1831   ga = (color >>  5) & 0x1f;	ga = (ga << 3) | (ga >> 2);
1832   ba =  color & 0x1f;		ba = (ba << 3) | (ba >> 2);
1833   *r = ra; *g = ga; *b = ba;
1834 }
1835 
QT_Get_RGBColorL(r,g,b,color)1836 void QT_Get_RGBColorL(r,g,b,color)
1837 xaLONG *r,*g,*b;
1838 xaULONG color;
1839 { xaULONG ra,ga,ba;
1840   ra = (color >> 10) & 0x1f;	ra = (ra << 3) | (ra >> 2);
1841   ga = (color >>  5) & 0x1f;	ga = (ga << 3) | (ga >> 2);
1842   ba =  color & 0x1f;		ba = (ba << 3) | (ba >> 2);
1843   *r = ra; *g = ga; *b = ba;
1844 }
1845 
QT_Get_Color(color,map_flag,map,chdr)1846 xaULONG QT_Get_Color(color,map_flag,map,chdr)
1847 xaULONG color,map_flag,*map;
1848 XA_CHDR *chdr;
1849 {
1850   register xaULONG clr,ra,ga,ba,ra5,ga5,ba5;
1851 
1852   ra5 = (color >> 10) & 0x1f;
1853   ga5 = (color >>  5) & 0x1f;
1854   ba5 =  color & 0x1f;
1855   ra = qt_gamma_adj[ra5]; ga = qt_gamma_adj[ga5]; ba = qt_gamma_adj[ba5];
1856 
1857   if (x11_display_type & XA_X11_TRUE) clr = X11_Get_True_Color(ra,ga,ba,16);
1858   else
1859   {
1860     if ((cmap_color_func == 4) && (chdr))
1861     { register xaULONG cache_i = color & 0x7fff;
1862       if (cmap_cache == 0) CMAP_Cache_Init(0);
1863       if (chdr != cmap_cache_chdr)
1864       {
1865         CMAP_Cache_Clear();
1866         cmap_cache_chdr = chdr;
1867       }
1868       if (cmap_cache[cache_i] == 0xffff)
1869       {
1870         clr = chdr->coff +
1871            CMAP_Find_Closest(chdr->cmap,chdr->csize,ra,ga,ba,16,16,16,xaTRUE);
1872         cmap_cache[cache_i] = (xaUSHORT)clr;
1873       }
1874       else clr = (xaULONG)cmap_cache[cache_i];
1875     }
1876     else
1877     {
1878       if (cmap_true_to_332 == xaTRUE) clr = CMAP_GET_332(ra5,ga5,ba5,CMAP_SCALE5);
1879       else			  clr = CMAP_GET_GRAY(ra5,ga5,ba5,CMAP_SCALE10);
1880       if (map_flag) clr = map[clr];
1881     }
1882   }
1883   return(clr);
1884 }
1885 
1886 
QT_Get_AV_RGBColors(c,r,g,b,cA,cB)1887 void QT_Get_AV_RGBColors(c,r,g,b,cA,cB)
1888 xaULONG *c;
1889 xaUBYTE *r,*g,*b;
1890 xaULONG cA,cB;
1891 { xaULONG rA,gA,bA,rB,gB,bB,ra,ga,ba;
1892 /**color 3 ***/
1893   rA = (cA >> 10) & 0x1f;	r[3] = (rA << 3) | (rA >> 2);
1894   gA = (cA >>  5) & 0x1f;	g[3] = (gA << 3) | (gA >> 2);
1895   bA =  cA & 0x1f;		b[3] = (bA << 3) | (bA >> 2);
1896 /**color 0 ***/
1897   rB = (cB >> 10) & 0x1f;	r[0] = (rB << 3) | (rB >> 2);
1898   gB = (cB >>  5) & 0x1f;	g[0] = (gB << 3) | (gB >> 2);
1899   bB =  cB & 0x1f;		b[0] = (bB << 3) | (bB >> 2);
1900 /**color 2 ***/
1901   ra = (21*rA + 11*rB) >> 5;	r[2] = (ra << 3) | (ra >> 2);
1902   ga = (21*gA + 11*gB) >> 5;	g[2] = (ga << 3) | (ga >> 2);
1903   ba = (21*bA + 11*bB) >> 5;	b[2] = (ba << 3) | (ba >> 2);
1904 /**color 1 ***/
1905   ra = (11*rA + 21*rB) >> 5;	r[1] = (ra << 3) | (ra >> 2);
1906   ga = (11*gA + 21*gB) >> 5;	g[1] = (ga << 3) | (ga >> 2);
1907   ba = (11*bA + 21*bB) >> 5;	b[1] = (ba << 3) | (ba >> 2);
1908 }
1909 
QT_Get_AV_DITH_RGB(r,g,b,cA,cB)1910 void QT_Get_AV_DITH_RGB(r,g,b,cA,cB)
1911 xaLONG *r,*g,*b;
1912 xaULONG cA,cB;
1913 { xaULONG rA,gA,bA,rB,gB,bB,ra,ga,ba;
1914 /**color 3 ***/
1915   rA = (cA >> 10) & 0x1f;	r[3] = (rA << 9) | (rA << 4);
1916   gA = (cA >>  5) & 0x1f;	g[3] = (gA << 9) | (gA << 4);
1917   bA =  cA & 0x1f;		b[3] = (bA << 9) | (bA << 4);
1918 /**color 0 ***/
1919   rB = (cB >> 10) & 0x1f;	r[0] = (rB << 9) | (rB << 4);
1920   gB = (cB >>  5) & 0x1f;	g[0] = (gB << 9) | (gB << 4);
1921   bB =  cB & 0x1f;		b[0] = (bB << 9) | (bB << 4);
1922 /**color 2 ***/
1923   ra = (21*rA + 11*rB) >> 5;	r[2] = (ra << 9) | (ra << 4);
1924   ga = (21*gA + 11*gB) >> 5;	g[2] = (ga << 9) | (ga << 4);
1925   ba = (21*bA + 11*bB) >> 5;	b[2] = (ba << 9) | (ba << 4);
1926 /**color 1 ***/
1927   ra = (11*rA + 21*rB) >> 5;	r[1] = (ra << 9) | (ra << 4);
1928   ga = (11*gA + 21*gB) >> 5;	g[1] = (ga << 9) | (ga << 4);
1929   ba = (11*bA + 21*bB) >> 5;	b[1] = (ba << 9) | (ba << 4);
1930 }
1931 
QT_Get_AV_Colors(c,cA,cB,map_flag,map,chdr)1932 void QT_Get_AV_Colors(c,cA,cB,map_flag,map,chdr)
1933 xaULONG *c;
1934 xaULONG cA,cB,map_flag,*map;
1935 XA_CHDR *chdr;
1936 {
1937   xaULONG clr,rA,gA,bA,rB,gB,bB,r0,g0,b0,r1,g1,b1;
1938   xaULONG rA5,gA5,bA5,rB5,gB5,bB5;
1939   xaULONG r05,g05,b05,r15,g15,b15;
1940 
1941 /*color 3*/
1942   rA5 = (cA >> 10) & 0x1f;
1943   gA5 = (cA >>  5) & 0x1f;
1944   bA5 =  cA & 0x1f;
1945 /*color 0*/
1946   rB5 = (cB >> 10) & 0x1f;
1947   gB5 = (cB >>  5) & 0x1f;
1948   bB5 =  cB & 0x1f;
1949 /*color 2*/
1950   r05 = (21*rA5 + 11*rB5) >> 5;
1951   g05 = (21*gA5 + 11*gB5) >> 5;
1952   b05 = (21*bA5 + 11*bB5) >> 5;
1953 /*color 1*/
1954   r15 = (11*rA5 + 21*rB5) >> 5;
1955   g15 = (11*gA5 + 21*gB5) >> 5;
1956   b15 = (11*bA5 + 21*bB5) >> 5;
1957 /*adj and scale to 16 bits */
1958   rA=qt_gamma_adj[rA5]; gA=qt_gamma_adj[gA5]; bA=qt_gamma_adj[bA5];
1959   rB=qt_gamma_adj[rB5]; gB=qt_gamma_adj[gB5]; bB=qt_gamma_adj[bB5];
1960   r0=qt_gamma_adj[r05]; g0=qt_gamma_adj[g05]; b0=qt_gamma_adj[b05];
1961   r1=qt_gamma_adj[r15]; g1=qt_gamma_adj[g15]; b1=qt_gamma_adj[b15];
1962 
1963   /*** 1st Color **/
1964   if (x11_display_type & XA_X11_TRUE) clr = X11_Get_True_Color(rA,gA,bA,16);
1965   else
1966   {
1967     if ((cmap_color_func == 4) && (chdr))
1968     { register xaULONG cache_i = cA & 0x7fff;
1969       if (cmap_cache == 0) CMAP_Cache_Init(0);
1970       if (chdr != cmap_cache_chdr)
1971       {
1972         CMAP_Cache_Clear();
1973         cmap_cache_chdr = chdr;
1974       }
1975       if (cmap_cache[cache_i] == 0xffff)
1976       {
1977         clr = chdr->coff +
1978            CMAP_Find_Closest(chdr->cmap,chdr->csize,rA,gA,bA,16,16,16,xaTRUE);
1979         cmap_cache[cache_i] = (xaUSHORT)clr;
1980       }
1981       else clr = (xaULONG)cmap_cache[cache_i];
1982     }
1983     else
1984     { if (cmap_true_to_332 == xaTRUE) clr = CMAP_GET_332(rA5,gA5,bA5,CMAP_SCALE5);
1985       else	clr = CMAP_GET_GRAY(rA5,gA5,bA5,CMAP_SCALE10);
1986       if (map_flag) clr = map[clr];
1987     }
1988   }
1989   c[3] = clr;
1990 
1991   /*** 2nd Color **/
1992   if (x11_display_type & XA_X11_TRUE) clr = X11_Get_True_Color(rB,gB,bB,16);
1993   else
1994   {
1995     if ((cmap_color_func == 4) && (chdr))
1996     { register xaULONG cache_i = cB & 0x7fff;
1997       if (cmap_cache[cache_i] == 0xffff)
1998       {
1999         clr = chdr->coff +
2000            CMAP_Find_Closest(chdr->cmap,chdr->csize,rB,gB,bB,16,16,16,xaTRUE);
2001         cmap_cache[cache_i] = (xaUSHORT)clr;
2002       }
2003       else clr = (xaULONG)cmap_cache[cache_i];
2004     }
2005     else
2006     { if (cmap_true_to_332 == xaTRUE) clr = CMAP_GET_332(rB5,gB5,bB5,CMAP_SCALE5);
2007       else	clr = CMAP_GET_GRAY(rB5,gB5,bB5,CMAP_SCALE10);
2008       if (map_flag) clr = map[clr];
2009     }
2010   }
2011   c[0] = clr;
2012 
2013   /*** 1st Av ****/
2014   if (x11_display_type & XA_X11_TRUE) clr = X11_Get_True_Color(r0,g0,b0,16);
2015   else
2016   {
2017     if ((cmap_color_func == 4) && (chdr))
2018     { register xaULONG cache_i;
2019       cache_i = (xaULONG)(r05 << 10) | (g05 << 5) | b05;
2020       if (cmap_cache[cache_i] == 0xffff)
2021       {
2022         clr = chdr->coff +
2023            CMAP_Find_Closest(chdr->cmap,chdr->csize,r0,g0,b0,16,16,16,xaTRUE);
2024         cmap_cache[cache_i] = (xaUSHORT)clr;
2025       }
2026       else clr = (xaULONG)cmap_cache[cache_i];
2027     }
2028     else
2029     { if (cmap_true_to_332 == xaTRUE) clr = CMAP_GET_332(r05,g05,b05,CMAP_SCALE5);
2030       else	clr = CMAP_GET_GRAY(r05,g05,b05,CMAP_SCALE10);
2031       if (map_flag) clr = map[clr];
2032     }
2033   }
2034   c[2] = clr;
2035 
2036   /*** 2nd Av ****/
2037   if (x11_display_type & XA_X11_TRUE) clr = X11_Get_True_Color(r1,g1,b1,16);
2038   else
2039   {
2040     if ((cmap_color_func == 4) && (chdr))
2041     { register xaULONG cache_i;
2042       cache_i = (xaULONG)(r15 << 10) | (g15 << 5) | b15;
2043       if (cmap_cache[cache_i] == 0xffff)
2044       {
2045         clr = chdr->coff +
2046            CMAP_Find_Closest(chdr->cmap,chdr->csize,r1,g1,b1,16,16,16,xaTRUE);
2047         cmap_cache[cache_i] = (xaUSHORT)clr;
2048       }
2049       else clr = (xaULONG)cmap_cache[cache_i];
2050     }
2051     else
2052     { if (cmap_true_to_332 == xaTRUE) clr = CMAP_GET_332(r15,g15,b15,CMAP_SCALE5);
2053       else	clr = CMAP_GET_GRAY(r15,g15,b15,CMAP_SCALE10);
2054       if (map_flag) clr = map[clr];
2055     }
2056   }
2057   c[1] = clr;
2058 }
2059 
2060 
2061 
2062 #define QT_SMC_O2I(i,o,rinc) { \
2063 *i++ = *o++; *i++ = *o++; *i++ = *o++; *i++ = *o++; i += rinc; o += rinc; \
2064 *i++ = *o++; *i++ = *o++; *i++ = *o++; *i++ = *o++; i += rinc; o += rinc; \
2065 *i++ = *o++; *i++ = *o++; *i++ = *o++; *i++ = *o++; i += rinc; o += rinc; \
2066 *i++ = *o++; *i++ = *o++; *i++ = *o++; *i++ = *o++;  }
2067 
2068 #define QT_SMC_C1(i,c,rinc) { \
2069 *i++ = c; *i++ = c; *i++ = c; *i++ = c;  i += rinc; \
2070 *i++ = c; *i++ = c; *i++ = c; *i++ = c;  i += rinc; \
2071 *i++ = c; *i++ = c; *i++ = c; *i++ = c;  i += rinc; \
2072 *i++ = c; *i++ = c; *i++ = c; *i++ = c;  i += rinc; }
2073 
2074 #define QT_SMC_C2(i,c0,c1,msk,rinc) { \
2075 *i++ =(msk&0x80)?c1:c0; *i++ =(msk&0x40)?c1:c0; \
2076 *i++ =(msk&0x20)?c1:c0; *i++ =(msk&0x10)?c1:c0; i += rinc; \
2077 *i++ =(msk&0x08)?c1:c0; *i++ =(msk&0x04)?c1:c0; \
2078 *i++ =(msk&0x02)?c1:c0; *i++ =(msk&0x01)?c1:c0; }
2079 
2080 #define QT_SMC_C4(i,CST,c,mska,mskb,rinc) { \
2081 *i++ = (CST)(c[(mska>>6) & 0x03]); *i++ = (CST)(c[(mska>>4) & 0x03]); \
2082 *i++ = (CST)(c[(mska>>2) & 0x03]); *i++ = (CST)(c[mska & 0x03]); i+=rinc; \
2083 *i++ = (CST)(c[(mskb>>6) & 0x03]); *i++ = (CST)(c[(mskb>>4) & 0x03]); \
2084 *i++ = (CST)(c[(mskb>>2) & 0x03]); *i++ = (CST)(c[mskb & 0x03]); }
2085 
2086 #define QT_SMC_C8(i,CST,c,msk,rinc) { \
2087 *i++ = (CST)(c[(msk>>21) & 0x07]); *i++ = (CST)(c[(msk>>18) & 0x07]); \
2088 *i++ = (CST)(c[(msk>>15) & 0x07]); *i++ = (CST)(c[(msk>>12) & 0x07]); i+=rinc; \
2089 *i++ = (CST)(c[(msk>> 9) & 0x07]); *i++ = (CST)(c[(msk>> 6) & 0x07]); \
2090 *i++ = (CST)(c[(msk>> 3) & 0x07]); *i++ = (CST)(c[msk & 0x07]); }
2091 
2092 #define QT_SMC_C16m(i,dp,CST,map,rinc) { \
2093 *i++ =(CST)map[*dp++]; *i++ =(CST)map[*dp++]; \
2094 *i++ =(CST)map[*dp++]; *i++ =(CST)map[*dp++]; i += rinc; \
2095 *i++ =(CST)map[*dp++]; *i++ =(CST)map[*dp++]; \
2096 *i++ =(CST)map[*dp++]; *i++ =(CST)map[*dp++]; }
2097 
2098 #define QT_SMC_C16(i,dp,CST) { \
2099 *i++ =(CST)(*dp++); *i++ =(CST)(*dp++); \
2100 *i++ =(CST)(*dp++); *i++ =(CST)(*dp++); }
2101 
2102 xaULONG
QT_Decode_SMC(image,delta,dsize,dec_info)2103 QT_Decode_SMC(image,delta,dsize,dec_info)
2104 xaUBYTE *image;         /* Image Buffer. */
2105 xaUBYTE *delta;         /* delta data. */
2106 xaULONG dsize;          /* delta size */
2107 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
2108 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
2109   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
2110   xaLONG x,y,len,row_inc; /* xaLONG min_x,max_x,min_y,max_y; */
2111   xaUBYTE *dptr;
2112   xaULONG i,cnt,hicode,code;
2113   xaULONG *c;
2114 
2115   smc_8cnt = smc_Acnt = smc_Ccnt = 0;
2116 
2117   dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
2118   dptr = delta;
2119   x = y = 0;
2120   row_inc = imagex - 4;
2121 
2122   dptr++;				/* skip over 0xe1 */
2123   len =(*dptr++)<<16; len |= (*dptr++)<< 8; len |= (*dptr++); /* Read Len */
2124   len -= 4;				/* read 4 already */
2125   while(len > 0)
2126   {
2127     code = *dptr++; len--; hicode = code & 0xf0;
2128     switch(hicode)
2129     {
2130       case 0x00: /* SKIPs */
2131       case 0x10:
2132 	if (hicode == 0x10) {cnt = 1 + *dptr++; len -= 1;}
2133 	else cnt = 1 + (code & 0x0f);
2134         while(cnt--) {x += 4; if (x >= imagex) { x = 0; y += 4; } }
2135 	break;
2136       case 0x20: /* Repeat Last Block */
2137       case 0x30:
2138 	{ xaLONG tx,ty;
2139 	  if (hicode == 0x30) {cnt = 1 + *dptr++; len--;}
2140 	  else cnt = 1 + (code & 0x0f);
2141 	  if (x==0) {ty = y-4; tx = imagex-4;} else {ty=y; tx = x-4;}
2142 
2143 	  while(cnt--)
2144 	  {
2145 	    if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
2146 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2147 	      xaUBYTE *o_ptr = (xaUBYTE *)(image + ty * imagex + tx);
2148 	      QT_SMC_O2I(i_ptr,o_ptr,row_inc);
2149 	    } else if (x11_bytes_pixel==2)
2150 	    { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x) );
2151 	      xaUSHORT *o_ptr = (xaUSHORT *)(image + 2*(ty * imagex + tx) );
2152 	      QT_SMC_O2I(i_ptr,o_ptr,row_inc);
2153 	    } else /* if (x11_bytes_pixel==4) */
2154 	    { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x) );
2155 	      xaULONG *o_ptr = (xaULONG *)(image + 4*(ty * imagex + tx) );
2156 	      QT_SMC_O2I(i_ptr,o_ptr,row_inc);
2157 	    }
2158 	    x += 4; if (x >= imagex) { x = 0; y += 4; }
2159 	  }
2160 	}
2161 	break;
2162       case 0x40: /* */
2163       case 0x50:
2164 	{ xaULONG cnt,cnt1;
2165 	  xaULONG m_cnt,m_cnt1;
2166 	  xaLONG m_tx,m_ty;
2167           xaLONG tx,ty;
2168 	  if (hicode == 0x50)
2169 	  {
2170 	     m_cnt1 = 1 + *dptr++; len--;
2171 	     m_cnt = 2;
2172 	  }
2173           else
2174 	  {
2175 	    m_cnt1 = (1 + (code & 0x0f));
2176 	    m_cnt = 2;
2177 	  }
2178           m_tx = x-(xaLONG)(4 * m_cnt); m_ty = y;
2179 	  if (m_tx < 0) {m_tx += imagex; m_ty -= 4;}
2180 	  cnt1 = m_cnt1;
2181 	  while(cnt1--)
2182 	  {
2183 	    cnt = m_cnt; tx = m_tx; ty = m_ty;
2184 	    while(cnt--)
2185 	    {
2186 	      if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
2187 	      { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2188 		xaUBYTE *o_ptr = (xaUBYTE *)(image + ty * imagex + tx);
2189 		QT_SMC_O2I(i_ptr,o_ptr,row_inc);
2190 	      } else if (x11_bytes_pixel==2)
2191 	      { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x));
2192 		xaUSHORT *o_ptr = (xaUSHORT *)(image + 2*(ty * imagex + tx));
2193 		QT_SMC_O2I(i_ptr,o_ptr,row_inc);
2194 	      } else
2195 	      { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x));
2196 		xaULONG *o_ptr = (xaULONG *)(image + 4*(ty * imagex + tx));
2197 		QT_SMC_O2I(i_ptr,o_ptr,row_inc);
2198 	      }
2199 	      x += 4; if (x >= imagex) { x = 0; y += 4; }
2200 	      tx += 4; if (tx >= imagex) { tx = 0; ty += 4; }
2201 	    } /* end of cnt */
2202 	  } /* end of cnt1 */
2203 	}
2204 	break;
2205 
2206       case 0x60: /* Repeat Data */
2207       case 0x70:
2208 	{ xaULONG ct,cnt;
2209 	  if (hicode == 0x70) {cnt = 1 + *dptr++; len--;}
2210 	  else cnt = 1 + (code & 0x0f);
2211 	  ct = (map_flag)?(map[*dptr++]):(xaULONG)(*dptr++); len--;
2212 	  while(cnt--)
2213 	  {
2214 	    if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
2215 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2216 	      xaUBYTE d = (xaUBYTE)(ct);
2217 	      QT_SMC_C1(i_ptr,d,row_inc);
2218 	    } else if (x11_bytes_pixel==2)
2219 	    { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x));
2220 	      xaUSHORT d = (xaUBYTE)(ct);
2221 	      QT_SMC_C1(i_ptr,d,row_inc);
2222 	    } else
2223 	    { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x));
2224 	      QT_SMC_C1(i_ptr,ct,row_inc);
2225 	    }
2226 	    x += 4; if (x >= imagex) { x = 0; y += 4; }
2227 	  }
2228 	}
2229 	break;
2230 
2231       case 0x80: /* 2 colors plus 16 mbits per */
2232       case 0x90:
2233         { xaULONG cnt = 1 + (code & 0x0f);
2234 	  if (hicode == 0x80)
2235 	  {
2236             c = (xaULONG *)&smc_8[ (smc_8cnt * 2) ];  len -= 2;
2237 	    smc_8cnt++; if (smc_8cnt >= SMC_MAX_CNT) smc_8cnt -= SMC_MAX_CNT;
2238 	    for(i=0;i<2;i++) {c[i]=(map_flag)?map[*dptr++]:(xaULONG)(*dptr++);}
2239 	  }
2240           else { c = (xaULONG *)&smc_8[ ((xaULONG)(*dptr++) << 1) ]; len--; }
2241 	  while(cnt--)
2242 	  { xaULONG msk1,msk0;
2243 	    msk0 = *dptr++; msk1 = *dptr++;  len-= 2;
2244 	    if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
2245 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2246 	      xaUBYTE c0=(xaUBYTE)c[0];	xaUBYTE c1=(xaUBYTE)c[1];
2247 	      QT_SMC_C2(i_ptr,c0,c1,msk0,row_inc); i_ptr += row_inc;
2248 	      QT_SMC_C2(i_ptr,c0,c1,msk1,row_inc);
2249 	    } else if (x11_bytes_pixel==2)
2250 	    { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x));
2251 	      xaUSHORT c0=(xaUSHORT)c[0];	xaUSHORT c1=(xaUSHORT)c[1];
2252 	      QT_SMC_C2(i_ptr,c0,c1,msk0,row_inc); i_ptr += row_inc;
2253 	      QT_SMC_C2(i_ptr,c0,c1,msk1,row_inc);
2254 	    } else
2255 	    { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x));
2256 	      xaULONG c0=(xaULONG)c[0];	xaULONG c1=(xaULONG)c[1];
2257 	      QT_SMC_C2(i_ptr,c0,c1,msk0,row_inc); i_ptr += row_inc;
2258 	      QT_SMC_C2(i_ptr,c0,c1,msk1,row_inc);
2259 	    }
2260 	    x += 4; if (x >= imagex) { x = 0; y += 4; }
2261           }
2262         }
2263 	break;
2264 
2265       case 0xA0: /* 4 color + 32 mbits */
2266       case 0xB0:
2267         { xaULONG cnt = 1 + (code & 0xf);
2268           if (hicode == 0xA0)
2269           {
2270             c = (xaULONG *)&smc_A[ (smc_Acnt << 2) ]; len -= 4;
2271             smc_Acnt++; if (smc_Acnt >= SMC_MAX_CNT) smc_Acnt -= SMC_MAX_CNT;
2272             for(i=0;i<4;i++) {c[i]=(map_flag)?map[*dptr++]:(xaULONG)(*dptr++);}
2273           }
2274           else { c = (xaULONG *)&smc_A[ ((xaULONG)(*dptr++) << 2) ]; len--; }
2275 	  while(cnt--)
2276 	  { xaUBYTE msk0,msk1,msk2,msk3;
2277 	    msk0 = *dptr++;	msk1 = *dptr++;
2278 	    msk2 = *dptr++;	msk3 = *dptr++;		len -= 4;
2279 	    if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
2280 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2281 	      QT_SMC_C4(i_ptr,xaUBYTE,c,msk0,msk1,row_inc); i_ptr += row_inc;
2282 	      QT_SMC_C4(i_ptr,xaUBYTE,c,msk2,msk3,row_inc);
2283 	    } else if (x11_bytes_pixel==2)
2284 	    { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x));
2285 	      QT_SMC_C4(i_ptr,xaUSHORT,c,msk0,msk1,row_inc); i_ptr += row_inc;
2286 	      QT_SMC_C4(i_ptr,xaUSHORT,c,msk2,msk3,row_inc);
2287 	    } else
2288 	    { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x));
2289 	      QT_SMC_C4(i_ptr,xaULONG,c,msk0,msk1,row_inc); i_ptr += row_inc;
2290 	      QT_SMC_C4(i_ptr,xaULONG,c,msk2,msk3,row_inc);
2291 	    }
2292 	    x += 4; if (x >= imagex) { x = 0; y += 4; }
2293           }
2294         }
2295 	break;
2296 
2297       case 0xC0: /* 8 colors + 48 mbits */
2298       case 0xD0:
2299         { xaULONG cnt = 1 + (code & 0xf);
2300           if (hicode == 0xC0)
2301           {
2302             c = (xaULONG *)&smc_C[ (smc_Ccnt << 3) ];   len -= 8;
2303             smc_Ccnt++; if (smc_Ccnt >= SMC_MAX_CNT) smc_Ccnt -= SMC_MAX_CNT;
2304             for(i=0;i<8;i++) {c[i]=(map_flag)?map[*dptr++]:(xaULONG)(*dptr++);}
2305           }
2306           else { c = (xaULONG *)&smc_C[ ((xaULONG)(*dptr++) << 3) ]; len--; }
2307 
2308 	  while(cnt--)
2309 	  { xaULONG t,mbits0,mbits1;
2310 	    t = (*dptr++) << 8; t |= *dptr++;
2311 	    mbits0  = (t & 0xfff0) << 8;  mbits1  = (t & 0x000f) << 8;
2312 	    t = (*dptr++) << 8; t |= *dptr++;
2313 	    mbits0 |= (t & 0xfff0) >> 4;  mbits1 |= (t & 0x000f) << 4;
2314 	    t = (*dptr++) << 8; t |= *dptr++;
2315 	    mbits1 |= (t & 0xfff0) << 8;  mbits1 |= (t & 0x000f);
2316 	    len -= 6;
2317 	    if ( (x11_bytes_pixel==1) || (map_flag==xaFALSE) )
2318 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2319 	      QT_SMC_C8(i_ptr,xaUBYTE,c,mbits0,row_inc); i_ptr += row_inc;
2320 	      QT_SMC_C8(i_ptr,xaUBYTE,c,mbits1,row_inc);
2321 	    } else if (x11_bytes_pixel==2)
2322 	    { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x));
2323 	      QT_SMC_C8(i_ptr,xaUSHORT,c,mbits0,row_inc); i_ptr += row_inc;
2324 	      QT_SMC_C8(i_ptr,xaUSHORT,c,mbits1,row_inc);
2325 	    } else
2326 	    { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x));
2327 	      QT_SMC_C8(i_ptr,xaULONG,c,mbits0,row_inc); i_ptr += row_inc;
2328 	      QT_SMC_C8(i_ptr,xaULONG,c,mbits1,row_inc);
2329 	    }
2330 	    x += 4; if (x >= imagex) { x = 0; y += 4; }
2331           }
2332         }
2333 	break;
2334 
2335       case 0xE0: /* 16 colors */
2336         { xaULONG cnt = 1 + (code & 0x0f);
2337 	  while(cnt--)
2338 	  {
2339 	    if (map_flag==xaFALSE)
2340 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2341 	      QT_SMC_C16(i_ptr,dptr,xaUBYTE); i_ptr += row_inc;
2342 	      QT_SMC_C16(i_ptr,dptr,xaUBYTE); i_ptr += row_inc;
2343 	      QT_SMC_C16(i_ptr,dptr,xaUBYTE); i_ptr += row_inc;
2344 	      QT_SMC_C16(i_ptr,dptr,xaUBYTE);
2345 	    } else if (x11_bytes_pixel==1)
2346 	    { xaUBYTE *i_ptr = (xaUBYTE *)(image + y * imagex + x);
2347 	      QT_SMC_C16m(i_ptr,dptr,xaUBYTE,map,row_inc); i_ptr += row_inc;
2348 	      QT_SMC_C16m(i_ptr,dptr,xaUBYTE,map,row_inc);
2349 	    } else if (x11_bytes_pixel==2)
2350 	    { xaUSHORT *i_ptr = (xaUSHORT *)(image + 2*(y * imagex + x));
2351 	      QT_SMC_C16m(i_ptr,dptr,xaUSHORT,map,row_inc); i_ptr += row_inc;
2352 	      QT_SMC_C16m(i_ptr,dptr,xaUSHORT,map,row_inc);
2353 	    } else
2354 	    { xaULONG *i_ptr = (xaULONG *)(image + 4*(y * imagex + x));
2355 	      QT_SMC_C16m(i_ptr,dptr,xaULONG,map,row_inc); i_ptr += row_inc;
2356 	      QT_SMC_C16m(i_ptr,dptr,xaULONG,map,row_inc);
2357 	    }
2358 	    len -= 16; x += 4; if (x >= imagex) { x = 0; y += 4; }
2359 	  }
2360 	}
2361 	break;
2362 
2363       default:   /* 0xF0 */
2364 	fprintf(stderr,"SMC opcode %x is unknown\n",code);
2365 	break;
2366     }
2367   }
2368   if (map_flag) return(ACT_DLTA_MAPD);
2369   else return(ACT_DLTA_NORM);
2370 }
2371 
QT_Decode_YUV2(image,delta,dsize,dec_info)2372 xaULONG QT_Decode_YUV2(image,delta,dsize,dec_info)
2373 xaUBYTE *image;         /* Image Buffer. */
2374 xaUBYTE *delta;         /* delta data. */
2375 xaULONG dsize;          /* delta size */
2376 XA_DEC2_INFO *dec_info;  /* Decoder Info Header */
2377 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
2378   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
2379   xaULONG special = dec_info->special & 0x01;
2380   XA_CHDR *chdr = dec_info->chdr;
2381   xaUBYTE *dptr = delta;
2382   xaULONG ycnt, row_inc = imagex << 1;
2383   xaULONG mx = imagex >> 1;
2384   void (*color_func)() = (void (*)())XA_YUV211111_Func(dec_info->image_type);
2385 
2386 
2387   dec_info->xs = dec_info->ys = 0;
2388   dec_info->xe = imagex; dec_info->ye = imagey;
2389   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
2390   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
2391 
2392   row_inc *= (special)?(3):(dec_info->bytes_pixel);
2393 
2394   ycnt = 0;
2395   while( (imagey > 0) && (dsize > 0) )
2396   { xaUBYTE *yp,*up,*vp;
2397     xaULONG x = mx;
2398     if (ycnt == 0)
2399       { yp = jpg_YUVBufs.Ybuf; up = jpg_YUVBufs.Ubuf; vp = jpg_YUVBufs.Vbuf; }
2400     while(x--)
2401     { *yp++ = *dptr++; *up++ = (*dptr++) ^ 0x80;
2402       *yp++ = *dptr++; *vp++ = (*dptr++) ^ 0x80;  dsize -= 4;
2403     }
2404     ycnt++;
2405     imagey--;
2406     if ((ycnt >= 2) || (imagey == 0))
2407     { color_func(image,imagex,ycnt,imagex,ycnt,
2408                 &jpg_YUVBufs, &def_yuv_tabs, map_flag, map, chdr);
2409       ycnt = 0;
2410       image += row_inc;
2411     }
2412   } /*end of y */
2413 
2414   if (map_flag) return(ACT_DLTA_MAPD);
2415   else return(ACT_DLTA_NORM);
2416 }
2417 
2418 /*********
2419  *
2420  *******/
2421 
QT_RPZA_Dither(image,delta,len,dec_info)2422 xaULONG QT_RPZA_Dither(image,delta,len,dec_info)
2423 xaUBYTE *image;		/* Image Buffer. */
2424 xaUBYTE *delta;		/* delta data. */
2425 xaULONG len;		/* delta size */
2426 XA_DEC_INFO *dec_info;	/* Decoder Info Header */
2427 {
2428   xaULONG imagex = dec_info->imagex;
2429   xaULONG imagey = dec_info->imagey;
2430   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
2431   XA_CHDR *chdr = dec_info->chdr;
2432   xaLONG x,y,row_inc,blk_inc;
2433   xaLONG min_x,max_x,min_y,max_y;
2434   xaUBYTE *dptr;
2435   xaULONG code,changed;
2436   xaUBYTE *im0,*im1,*im2,*im3;
2437   xaUBYTE *rnglimit = xa_byte_limit;
2438   ColorReg *cmap;
2439 
2440   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
2441   cmap = chdr->cmap;
2442   dec_info->xs = dec_info->ys = 0; dec_info->xe = imagex; dec_info->ye = imagey;
2443   max_x = max_y = 0; min_x = imagex; min_y = imagey; changed = 0;
2444   dptr = delta;
2445   x = y = 0;
2446 
2447   blk_inc = 1; /* possible optimization */
2448   row_inc = blk_inc * imagex;
2449   blk_inc *= 4;
2450   im1 = im0 = image;    im1 += row_inc;
2451   im2 = im1;            im2 += row_inc;
2452   im3 = im2;            im3 += row_inc;
2453   row_inc *= 3; /* skip 3 rows at a time */
2454 
2455   while(len > 0)
2456   { code = *dptr++; len--;
2457     if ( (code >= 0xa0) && (code <= 0xbf) )		/* SINGLE */
2458     { xaULONG color,skip;
2459       xaLONG re,ge,be,r,g,b,col;
2460 
2461       changed = 1;
2462       color = (*dptr++) << 8; color |= *dptr++; len -= 2;
2463       skip = (code - 0x9f);
2464 
2465       while(skip--)
2466       { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1;
2467 	xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
2468 	  /* get original r,g,b  and scale to 8 bits */
2469 	RPZA_DITH_COL2RGB(r,g,b,color);
2470 
2471       /**---0 0*/
2472 	  RPZA_DITH_GET_RGB(r,g,b,0,0,0,col);
2473 	  if (map_flag) col = map[col];			ip0[0] = (xaUBYTE)col;
2474 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2475       /*----0 1*/
2476 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2477 	  if (map_flag) col = map[col];			ip0[1] = (xaUBYTE)col;
2478 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2479       /*----1 1*/
2480 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2481 	  if (map_flag) col = map[col];			ip1[1] = (xaUBYTE)col;
2482 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2483       /*----1 0*/
2484 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2485 	  if (map_flag) col = map[col];			ip1[0] = (xaUBYTE)col;
2486 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2487       /**---2 0*/
2488 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2489 	  if (map_flag) col = map[col];			ip2[0] = (xaUBYTE)col;
2490 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2491       /*----3 0*/
2492 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2493 	  if (map_flag) col = map[col];			ip3[0] = (xaUBYTE)col;
2494 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2495       /*----3 1*/
2496 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2497 	  if (map_flag) col = map[col];			ip3[1] = (xaUBYTE)col;
2498 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2499       /*----2 1*/
2500 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2501 	  if (map_flag) col = map[col];			ip2[1] = (xaUBYTE)col;
2502 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2503       /**---2 2*/
2504 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2505 	  if (map_flag) col = map[col];			ip2[2] = (xaUBYTE)col;
2506 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2507       /*----3 2*/
2508 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2509 	  if (map_flag) col = map[col];			ip3[2] = (xaUBYTE)col;
2510 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2511       /*----3 3*/
2512 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2513 	  if (map_flag) col = map[col];			ip3[3] = (xaUBYTE)col;
2514 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2515       /*----2 3*/
2516 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2517 	  if (map_flag) col = map[col];			ip2[3] = (xaUBYTE)col;
2518 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2519       /**---1 3*/
2520 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2521 	  if (map_flag) col = map[col];			ip1[3] = (xaUBYTE)col;
2522 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2523       /*----1 2*/
2524 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2525 	  if (map_flag) col = map[col];			ip1[2] = (xaUBYTE)col;
2526 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2527       /*----0 2*/
2528 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2529 	  if (map_flag) col = map[col];			ip0[2] = (xaUBYTE)col;
2530 	  RPZA_DITH_GET_ERR(r,g,b,re,ge,be,col,cmap);
2531       /*----0 3*/
2532 	  RPZA_DITH_GET_RGB(r,g,b,re,ge,be,col);
2533 	  if (map_flag) col = map[col];			ip0[3] = (xaUBYTE)col;
2534 
2535 	  QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
2536 	  QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
2537         } /* end while */
2538     }
2539     else if ( (code >= 0x80) && (code <= 0x9f) )		/* SKIP */
2540     { xaULONG skip = (code - 0x7f);
2541       while(skip--)
2542 		QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
2543     }
2544 
2545  				/* FOUR/SIXTEEN */
2546     else if ( (code < 0x80) || ((code >= 0xc0) && (code <= 0xdf)) )
2547     { xaULONG cA,cB;
2548       changed = 1;
2549       /* Get 1st two colors */
2550       if (code >= 0xc0) { cA = (*dptr++) << 8; cA |= *dptr++; len -= 2; }
2551       else {cA = (code << 8) | *dptr++; len -= 1;}
2552       cB = (*dptr++) << 8; cB |= *dptr++; len -= 2;
2553 
2554       /****** SIXTEEN COLOR *******/
2555       if ( (code < 0x80) && ((cB & 0x8000)==0) ) /* 16 color */
2556       { xaUBYTE *cptr = dptr;
2557         xaUBYTE *ip0=im0; xaUBYTE *ip1=im1;
2558 	xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
2559         xaLONG re,ge,be,ra,ga,ba,col;
2560 	dptr += 28; len -= 28;  /* skip over colors */
2561 
2562 	/**---0 0*/
2563 	 RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2564 	 RPZA_DITH_GET_RGB(ra,ga,ba,0,0,0,col);
2565 	 if (map_flag) col = map[col];		ip0[0] = (xaUBYTE)col;
2566 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2567 	/*----0 1*/
2568 	 RPZA_DITH_COL2RGB(ra,ga,ba,cB);
2569 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2570 	 if (map_flag) col = map[col];		ip0[1] = (xaUBYTE)col;
2571 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2572 	/*----1 1*/
2573 	 cA = (cptr[ 6]) << 8; cA |= cptr[ 7]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2574 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2575 	 if (map_flag) col = map[col];		ip1[1] = (xaUBYTE)col;
2576 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2577 	/*----1 0*/
2578 	 cA = (cptr[ 4]) << 8; cA |= cptr[ 5]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2579 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2580 	 if (map_flag) col = map[col];		ip1[0] = (xaUBYTE)col;
2581 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2582 	/**---2 0*/
2583 	 cA = (cptr[12]) << 8; cA |= cptr[13]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2584 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2585 	 if (map_flag) col = map[col];		ip2[0] = (xaUBYTE)col;
2586 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2587 	/*----3 0*/
2588 	 cA = (cptr[20]) << 8; cA |= cptr[21]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2589 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2590 	 if (map_flag) col = map[col];		ip3[0] = (xaUBYTE)col;
2591 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2592 	/*----3 1*/
2593 	 cA = (cptr[22]) << 8; cA |= cptr[23]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2594 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2595 	 if (map_flag) col = map[col];		ip3[1] = (xaUBYTE)col;
2596 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2597 	/*----2 1*/
2598 	 cA = (cptr[14]) << 8; cA |= cptr[15]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2599 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2600 	 if (map_flag) col = map[col];		ip2[1] = (xaUBYTE)col;
2601 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2602 	/**---2 2*/
2603 	 cA = (cptr[16]) << 8; cA |= cptr[17]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2604 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2605 	 if (map_flag) col = map[col];		ip2[2] = (xaUBYTE)col;
2606 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2607 	/*----3 2*/
2608 	 cA = (cptr[24]) << 8; cA |= cptr[25]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2609 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2610 	 if (map_flag) col = map[col];		ip3[2] = (xaUBYTE)col;
2611 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2612 	/*----3 3*/
2613 	 cA = (cptr[26]) << 8; cA |= cptr[27]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2614 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2615 	 if (map_flag) col = map[col];		ip3[3] = (xaUBYTE)col;
2616 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2617 	/*----2 3*/
2618 	 cA = (cptr[18]) << 8; cA |= cptr[19]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2619 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2620 	 if (map_flag) col = map[col];		ip2[3] = (xaUBYTE)col;
2621 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2622 	/**---1 3*/
2623 	 cA = (cptr[10]) << 8; cA |= cptr[11]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2624 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2625 	 if (map_flag) col = map[col];		ip1[3] = (xaUBYTE)col;
2626 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2627 	/*----1 2*/
2628 	 cA = (cptr[ 8]) << 8; cA |= cptr[ 9]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2629 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2630 	 if (map_flag) col = map[col];		ip1[2] = (xaUBYTE)col;
2631 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2632 	/*----0 2*/
2633 	 cA = (cptr[ 0]) << 8; cA |= cptr[ 1]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2634 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2635 	 if (map_flag) col = map[col];		ip0[2] = (xaUBYTE)col;
2636 	 RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2637 	/*----0 3*/
2638 	 cA = (cptr[ 2]) << 8; cA |= cptr[ 3]; RPZA_DITH_COL2RGB(ra,ga,ba,cA);
2639 	 RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2640 	 if (map_flag) col = map[col];		ip0[3] = (xaUBYTE)col;
2641 
2642 	QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
2643 	QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
2644       } /*** END of SIXTEEN COLOR */
2645       else					/****** FOUR COLOR *******/
2646       { xaULONG m_cnt,msk0,msk1,msk2,msk3;
2647 	xaLONG r[4],g[4],b[4];
2648 
2649         if (code < 0x80) m_cnt = 1;
2650 	else m_cnt = code - 0xbf;
2651 
2652 	{ xaULONG rA,gA,bA, rB,gB,bB;
2653 	  RPZA_DITH_COL2RGB(rA,gA,bA,cA);  r[3] = rA; g[3] = gA; b[3] = bA;
2654 	  RPZA_DITH_COL2RGB(rB,gB,bB,cB);  r[0] = rB; g[0] = gB; b[0] = bB;
2655           r[2] = (21*rA + 11*rB) >> 5;	   r[1] = (11*rA + 21*rB) >> 5;
2656 	  g[2] = (21*gA + 11*gB) >> 5;	   g[1] = (11*gA + 21*gB) >> 5;
2657 	  b[2] = (21*bA + 11*bB) >> 5;	   b[1] = (11*bA + 21*bB) >> 5;
2658 	}
2659 
2660 
2661         while(m_cnt--)
2662         { msk0 = *dptr++; msk1 = *dptr++;
2663 	  msk2 = *dptr++; msk3 = *dptr++; len -= 4;
2664 
2665           { xaUBYTE *ip0=im0; xaUBYTE *ip1=im1;
2666 	    xaUBYTE *ip2=im2; xaUBYTE *ip3=im3;
2667             xaLONG idx,re,ge,be,ra,ga,ba,col;
2668       /**---0 0*/
2669 	    idx = (msk0>>6)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2670 	    RPZA_DITH_GET_RGB(ra,ga,ba,0,0,0,col);
2671 	    if (map_flag) col = map[col];		ip0[0] = (xaUBYTE)col;
2672 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2673       /*----0 1*/
2674 	    idx = (msk0>>4)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2675 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2676 	    if (map_flag) col = map[col];		ip0[1] = (xaUBYTE)col;
2677 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2678       /*----1 1*/
2679 	    idx = (msk1>>4)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2680 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2681 	    if (map_flag) col = map[col];		ip1[1] = (xaUBYTE)col;
2682 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2683       /*----1 0*/
2684 	    idx = (msk1>>6)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2685 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2686 	    if (map_flag) col = map[col];		ip1[0] = (xaUBYTE)col;
2687 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2688       /**---2 0*/
2689 	    idx = (msk2>>6)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2690 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2691 	    if (map_flag) col = map[col];		ip2[0] = (xaUBYTE)col;
2692 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2693       /*----3 0*/
2694 	    idx = (msk3>>6)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2695 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2696 	    if (map_flag) col = map[col];		ip3[0] = (xaUBYTE)col;
2697 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2698       /*----3 1*/
2699 	    idx = (msk3>>4)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2700 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2701 	    if (map_flag) col = map[col];		ip3[1] = (xaUBYTE)col;
2702 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2703       /*----2 1*/
2704 	    idx = (msk2>>4)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2705 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2706 	    if (map_flag) col = map[col];		ip2[1] = (xaUBYTE)col;
2707 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2708       /**---2 2*/
2709 	    idx = (msk2>>2)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2710 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2711 	    if (map_flag) col = map[col];		ip2[2] = (xaUBYTE)col;
2712 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2713       /*----3 2*/
2714 	    idx = (msk3>>2)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2715 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2716 	    if (map_flag) col = map[col];		ip3[2] = (xaUBYTE)col;
2717 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2718       /*----3 3*/
2719 	    idx = msk3 & 0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2720 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2721 	    if (map_flag) col = map[col];		ip3[3] = (xaUBYTE)col;
2722 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2723       /*----2 3*/
2724 	    idx = msk2 & 0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2725 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2726 	    if (map_flag) col = map[col];		ip2[3] = (xaUBYTE)col;
2727 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2728       /**---1 3*/
2729 	    idx = msk1 & 0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2730 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2731 	    if (map_flag) col = map[col];		ip1[3] = (xaUBYTE)col;
2732 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2733       /*----1 2*/
2734 	    idx = (msk1>>2)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2735 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2736 	    if (map_flag) col = map[col];		ip1[2] = (xaUBYTE)col;
2737 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2738       /*----0 2*/
2739 	    idx = (msk0>>2)&0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2740 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2741 	    if (map_flag) col = map[col];		ip0[2] = (xaUBYTE)col;
2742 	    RPZA_DITH_GET_ERR(ra,ga,ba,re,ge,be,col,cmap);
2743       /*----0 3*/
2744 	    idx = msk0 & 0x03; ra = r[idx]; ga = g[idx]; ba = b[idx];
2745 	    RPZA_DITH_GET_RGB(ra,ga,ba,re,ge,be,col);
2746 	    if (map_flag) col = map[col];		ip0[3] = (xaUBYTE)col;
2747 	  }
2748 	  QT_MIN_MAX_CHECK(x,y,min_x,min_y,max_x,max_y);
2749 	  QT_RPZA_BLOCK_INC(x,y,imagex,im0,im1,im2,im3,blk_inc,row_inc);
2750         }
2751       } /*** END of FOUR COLOR *******/
2752     } /*** END of 4/16 COLOR BLOCKS ****/
2753     else /* UNKNOWN */
2754     {
2755       fprintf(stderr,"QT RPZA: Unknown %x\n",code);
2756       return(ACT_DLTA_NOP);
2757     }
2758   }
2759   if (xa_optimize_flag == xaTRUE)
2760   { if (changed) { dec_info->xs=min_x; dec_info->ys=min_y;
2761 		   dec_info->xe=max_x + 4; dec_info->ye=max_y + 4; }
2762     else  { dec_info->xs = dec_info->ys = dec_info->xe = dec_info->ye = 0;
2763 	    return(ACT_DLTA_NOP); }
2764   }
2765   else { dec_info->xs = dec_info->ys = 0;
2766 	 dec_info->xe = imagex; dec_info->ye = imagey; }
2767   if (map_flag) return(ACT_DLTA_MAPD);
2768   else return(ACT_DLTA_NORM);
2769 }
2770 
2771 
2772 /****************** 8BPS CODEC DEPTH 24 ********************************
2773  *  R G B
2774  *********************************************************************/
QT_Decode_8BPS24(image,delta,dsize,dec_info)2775 xaULONG QT_Decode_8BPS24(image,delta,dsize,dec_info)
2776 xaUBYTE *image;         /* Image Buffer. */
2777 xaUBYTE *delta;         /* delta data. */
2778 xaULONG dsize;          /* delta size */
2779 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
2780 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
2781   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
2782   xaULONG special = dec_info->special;
2783   xaULONG extra = (xaULONG)(dec_info->extra);
2784   XA_CHDR *chdr = dec_info->chdr;
2785   xaULONG special_flag = special & 0x0001;
2786   xaLONG len, y;
2787   xaUBYTE *rp, *gp, *bp;
2788   xaUBYTE *r_lp, *g_lp, *b_lp, *p_lp;
2789 
2790   dec_info->xs = dec_info->ys = 0;
2791   dec_info->xe = imagex; dec_info->ye = imagey;
2792     /* Indicate we can drop these frames */
2793   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
2794 
2795   if (chdr) { if (chdr->new_chdr) chdr = chdr->new_chdr; }
2796 
2797 	/* Line Pointers for each Plane */
2798   r_lp = delta;
2799   g_lp = r_lp + (imagey << 1);
2800   b_lp = g_lp + (imagey << 1);
2801   p_lp = b_lp + (imagey << 1); /* pad/alpha?? */
2802 
2803 	/* Start of Red Rows */
2804   if (extra == 0x01)	rp = p_lp + (imagey << 1);
2805   else			rp = b_lp + (imagey << 1);
2806 	/* Calculate Size of Compressed Red Plane */
2807   gp = r_lp; len = 0;
2808   for(y = 0; y < imagey; y++) { len += ((gp[0] << 8) | gp[1]); gp += 2; }
2809 	/* Start of Green Rows */
2810   gp = rp + len;
2811 	/* Calculate Size of Compressed Green Plane */
2812   bp = g_lp; len = 0;
2813   for(y = 0; y < imagey; y++) { len += ((bp[0] << 8) | bp[1]); bp += 2; }
2814 	/* Start of Blue Rows */
2815   bp = gp + len;
2816 
2817   for(y=0; y<imagey; y++)
2818   { xaUBYTE *yp, *vp, *up;
2819     xaLONG x;
2820 
2821 	/* RED PLANE */
2822     yp = jpg_YUVBufs.Ybuf;
2823     len = r_lp[ (y*2) ] << 8;  len |= r_lp[ (y*2) + 1 ];
2824     x = 0;
2825     while((x < imagex) && (len > 0))
2826     { xaUBYTE code = *rp++; len--; /* code*/
2827       if (code <= 127) { code++; len -= code; x += code;
2828 				while(code--) { *yp++ = *rp++; } }
2829       else if (code > 128) { code = (xaULONG)(257) - code; x += code;
2830 				while(code--) { *yp++ = *rp; } rp++; len--; }
2831     }
2832 	/* Green PLANE */
2833     up = jpg_YUVBufs.Ubuf;
2834     len = g_lp[ (y*2) ] << 8;  len |= g_lp[ (y*2) + 1 ];
2835     x = 0;
2836     while((x < imagex) && (len > 0))
2837     { xaUBYTE code = *gp++; len--; /* code*/
2838       if (code <= 127) { code++; len -= code; x += code;
2839 				while(code--) { *up++ = *gp++; } }
2840       else if (code > 128) { code = (xaULONG)(257) - code; x += code;
2841 				while(code--) { *up++ = *gp; } gp++; len--; }
2842     }
2843 	/* Blue PLANE */
2844     vp = jpg_YUVBufs.Vbuf;
2845     len = b_lp[ (y*2) ] << 8;  len |= b_lp[ (y*2) + 1 ];
2846     x = 0;
2847     while((x < imagex) && (len > 0))
2848     { xaUBYTE code = *bp++; len--; /* code*/
2849       if (code <= 127) { code++; len -= code; x += code;
2850 				while(code--) { *vp++ = *bp++; } }
2851       else if (code > 128) { code = (xaULONG)(257) - code; x += code;
2852 				while(code--) { *vp++ = *bp; } bp++; len--; }
2853     }
2854 
2855     yp = jpg_YUVBufs.Ybuf; up = jpg_YUVBufs.Ubuf; vp = jpg_YUVBufs.Vbuf;
2856     x = imagex;
2857     if (special_flag)
2858     { xaUBYTE *ip = (xaUBYTE *)(image + (y * imagex * 3));
2859       while(x--) { *ip++ = *yp++; *ip++ = *up++; *ip++ = *vp++; }
2860     }
2861     else if ( (map_flag==xaFALSE) || (x11_bytes_pixel==1) )
2862     { xaUBYTE *ip = (xaUBYTE *)(image + y * imagex);
2863       while(x--) *ip++ =
2864 	(xaUBYTE)XA_RGB24_To_CLR32(*yp++,*up++,*vp++,map_flag,map,chdr);
2865     }
2866     else if (x11_bytes_pixel==4)
2867     { xaULONG *ip = (xaULONG *)(image + ((y * imagex) << 2));
2868       while(x--) *ip++ =
2869 	(xaULONG)XA_RGB24_To_CLR32(*yp++,*up++,*vp++,map_flag,map,chdr);
2870     }
2871     else /* (x11_bytes_pixel==2) */
2872     { xaUSHORT *ip = (xaUSHORT *)(image + ((y * imagex) << 1));
2873       while(x--) *ip++ =
2874 	(xaUSHORT)XA_RGB24_To_CLR32(*yp++,*up++,*vp++,map_flag,map,chdr);
2875     }
2876   } /* end of y */
2877 
2878   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
2879   else return(ACT_DLTA_NORM);
2880 }
2881 
2882 
2883 /********************* * * * *******************************************/
QT_Decode_8BPS8(image,delta,dsize,dec_info)2884 xaULONG QT_Decode_8BPS8(image,delta,dsize,dec_info)
2885 xaUBYTE *image;         /* Image Buffer. */
2886 xaUBYTE *delta;         /* delta data. */
2887 xaULONG dsize;          /* delta size */
2888 XA_DEC_INFO *dec_info;  /* Decoder Info Header */
2889 { xaULONG imagex = dec_info->imagex;    xaULONG imagey = dec_info->imagey;
2890   xaULONG map_flag = dec_info->map_flag;        xaULONG *map = dec_info->map;
2891   xaLONG len, y;
2892   xaUBYTE *rp, *lp;
2893 
2894   dec_info->xs = dec_info->ys = 0;
2895   dec_info->xe = imagex; dec_info->ye = imagey;
2896     /* Indicate we can drop these frames */
2897   if (dec_info->skip_flag > 0) return(ACT_DLTA_DROP);
2898 
2899 	/* Line Pointers for each Plane */
2900   lp = delta;
2901 
2902 	/* Start of Rows */
2903   rp = lp + (imagey << 1);
2904 
2905 	/* Loop Through Each Line */
2906   for(y=0; y<imagey; y++)
2907   { xaUBYTE *yp;
2908     xaLONG x;
2909 
2910 	/* Decode Row */
2911     yp = jpg_YUVBufs.Ybuf;
2912     len = lp[ (y*2) ] << 8;  len |= lp[ (y*2) + 1 ];
2913     x = 0;
2914     while((x < imagex) && (len > 0))
2915     { xaUBYTE code = *rp++; len--; /* code*/
2916       if (code <= 127) { code++; len -= code; x += code;
2917 				while(code--) { *yp++ = *rp++; } }
2918       else if (code > 128) { code = (xaULONG)(257) - code; x += code;
2919 				while(code--) { *yp++ = *rp; } rp++; len--; }
2920     }
2921 
2922     yp = jpg_YUVBufs.Ybuf;
2923     x = imagex;
2924     if (map_flag==xaFALSE)
2925     { xaUBYTE *ip = (xaUBYTE *)(image + y * imagex);
2926 	while(x--) { *ip++ = *yp++; }
2927     }
2928     else if (x11_bytes_pixel==1)
2929     { xaUBYTE *ip = (xaUBYTE *)(image + y * imagex);
2930 	while(x--) *ip++ = (xaUBYTE)map[*yp++];
2931     }
2932     else if (x11_bytes_pixel==4)
2933     { xaULONG *ip = (xaULONG *)(image + ((y * imagex) << 2));
2934 	while(x--) *ip++ = (xaULONG)map[*yp++];
2935     }
2936     else /* (x11_bytes_pixel==2) */
2937     { xaUSHORT *ip = (xaUSHORT *)(image + ((y * imagex) << 1));
2938 	while(x--) *ip++ = (xaUSHORT)map[*yp++];
2939     }
2940   } /* end of y */
2941 
2942   if (map_flag==xaTRUE) return(ACT_DLTA_MAPD);
2943   else return(ACT_DLTA_NORM);
2944 }
2945 
2946