1 //------------------------------------------------------------------------------
2 // jpegdecoder.cpp
3 // Small JPEG Decoder Library v0.93b
4 // Last updated: Dec. 28, 2001
5 // Copyright (C) 1994-2000 Rich Geldreich
6 // richgel@voicenet.com
7 //
8 // Dec. 19, 2001 - fixed dumb bug in the decode_next_row functions that
9 // could cause bogus non-zero coefficients from leaking through in rare cases.
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 //------------------------------------------------------------------------------
25 #include "jpegdecoder.h"
26 //------------------------------------------------------------------------------
27 // Coefficients are stored in this sequence in the data stream.
28 static int ZAG[64] =
29 {
30   0,  1,  8, 16,  9,  2,  3, 10,
31  17, 24, 32, 25, 18, 11,  4,  5,
32  12, 19, 26, 33, 40, 48, 41, 34,
33  27, 20, 13,  6,  7, 14, 21, 28,
34  35, 42, 49, 56, 57, 50, 43, 36,
35  29, 22, 15, 23, 30, 37, 44, 51,
36  58, 59, 52, 45, 38, 31, 39, 46,
37  53, 60, 61, 54, 47, 55, 62, 63,
38 };
39 //------------------------------------------------------------------------------
40 const int AAN_SCALE_BITS = 14;
41 const int IFAST_SCALE_BITS = 2; /* fractional bits in scale factors */
42 //------------------------------------------------------------------------------
43 static int16 aan_scales[64] =
44 {
45   16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
46   22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
47   21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
48   19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
49   16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
50   12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
51    8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
52    4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
53 };
54 //------------------------------------------------------------------------------
55 // Unconditionally frees all allocated blocks.
free_all_blocks(void)56 void jpeg_decoder::free_all_blocks(void)
57 {
58   if (Pstream)
59   {
60     Pstream->detach();
61     Pstream = NULL;
62   }
63 
64   for (int i = 0; i < JPGD_MAXBLOCKS; i++)
65   {
66     free(blocks[i]);
67     blocks[i] = NULL;
68   }
69 }
70 //------------------------------------------------------------------------------
71 // This method handles all errors.
72 // It could easily be changed to use C++ exceptions.
terminate(int status)73 void jpeg_decoder::terminate(int status)
74 {
75   error_code = status;
76 
77   free_all_blocks();
78 
79   longjmp(jmp_state, status);
80 }
81 //------------------------------------------------------------------------------
82 // Allocate a block of memory-- store block's address in list for
83 // later deallocation by free_all_blocks().
alloc(int n)84 void *jpeg_decoder::alloc(int n)
85 {
86   int i;
87 
88   // Find a free slot. The number of allocated slots will
89   // always be very low, so a linear search is good enough.
90   for (i = 0; i < JPGD_MAXBLOCKS; i++)
91   {
92     if (blocks[i] == NULL)
93       break;
94   }
95 
96   if (i == JPGD_MAXBLOCKS)
97     terminate(JPGD_TOO_MANY_BLOCKS);
98 
99   void *q = malloc(n + 8);
100 
101   if (q == NULL)
102     terminate(JPGD_NOTENOUGHMEM);
103 
104   memset(q, 0, n + 8);
105 
106   blocks[i] = q;
107 
108   // Round to qword boundry, to avoid misaligned accesses with MMX code
109   return ((void *)(((ulong)q + 7) & ~7));
110 }
111 //------------------------------------------------------------------------------
112 // Clear buffer to word values.
word_clear(void * p,ushort c,uint n)113 void jpeg_decoder::word_clear(void *p, ushort c, uint n)
114 {
115   ushort *ps = (ushort *)p;
116   while (n)
117   {
118     *ps++ = c;
119     n--;
120   }
121 }
122 //------------------------------------------------------------------------------
123 // Refill the input buffer.
124 // This method will sit in a loop until (A) the buffer is full or (B)
125 // the stream's read() method reports and end of file condition.
prep_in_buffer(void)126 void jpeg_decoder::prep_in_buffer(void)
127 {
128   in_buf_left = 0;
129   Pin_buf_ofs = in_buf;
130 
131   if (eof_flag)
132     return;
133 
134 #ifdef SUPPORT_MMX
135   bool was_mmx_active = mmx_active;
136   if (mmx_active)
137     get_bits_2_mmx_deinit();
138 #endif
139 
140   do
141   {
142     int bytes_read = Pstream->read(in_buf + in_buf_left,
143                                    JPGD_INBUFSIZE - in_buf_left,
144                                    &eof_flag);
145 
146     if (bytes_read == -1)
147       terminate(JPGD_STREAM_READ);
148 
149     in_buf_left += bytes_read;
150 
151   } while ((in_buf_left < JPGD_INBUFSIZE) && (!eof_flag));
152 
153   total_bytes_read += in_buf_left;
154 
155   word_clear(Pin_buf_ofs + in_buf_left, 0xD9FF, 64);
156 
157 #ifdef SUPPORT_MMX
158   if (was_mmx_active)
159     get_bits_2_mmx_init();
160 #endif
161 }
162 //------------------------------------------------------------------------------
163 // Read a Huffman code table.
read_dht_marker(void)164 void jpeg_decoder::read_dht_marker(void)
165 {
166   int i, index, count;
167   uint left;
168   uchar huff_num[17];
169   uchar huff_val[256];
170 
171   left = get_bits_1(16);
172 
173   if (left < 2)
174     terminate(JPGD_BAD_DHT_MARKER);
175 
176   left -= 2;
177 
178   while (left)
179   {
180     index = get_bits_1(8);
181 
182     huff_num[0] = 0;
183 
184     count = 0;
185 
186     for (i = 1; i <= 16; i++)
187     {
188       huff_num[i] = get_bits_1(8);
189       count += huff_num[i];
190     }
191 
192     if (count > 255)
193       terminate(JPGD_BAD_DHT_COUNTS);
194 
195     for (i = 0; i < count; i++)
196       huff_val[i] = get_bits_1(8);
197 
198     i = 1 + 16 + count;
199 
200     if (left < (uint)i)
201       terminate(JPGD_BAD_DHT_MARKER);
202 
203     left -= i;
204 
205     if ((index & 0x10) > 0x10)
206       terminate(JPGD_BAD_DHT_INDEX);
207 
208     index = (index & 0x0F) + ((index & 0x10) >> 4) * (JPGD_MAXHUFFTABLES >> 1);
209 
210     if (index >= JPGD_MAXHUFFTABLES)
211       terminate(JPGD_BAD_DHT_INDEX);
212 
213     if (!this->huff_num[index])
214       this->huff_num[index] = (uchar *)alloc(17);
215 
216     if (!this->huff_val[index])
217       this->huff_val[index] = (uchar *)alloc(256);
218 
219     memcpy(this->huff_num[index], huff_num, 17);
220     memcpy(this->huff_val[index], huff_val, 256);
221   }
222 }
223 //------------------------------------------------------------------------------
224 // Read a quantization table.
read_dqt_marker(void)225 void jpeg_decoder::read_dqt_marker(void)
226 {
227   int n, i, prec;
228   uint left;
229   uint temp;
230 
231   left = get_bits_1(16);
232 
233   if (left < 2)
234     terminate(JPGD_BAD_DQT_MARKER);
235 
236   left -= 2;
237 
238   while (left)
239   {
240     n = get_bits_1(8);
241     prec = n >> 4;
242     n &= 0x0F;
243 
244     if (n >= JPGD_MAXQUANTTABLES)
245       terminate(JPGD_BAD_DQT_TABLE);
246 
247     if (!quant[n])
248       quant[n] = (QUANT_TYPE *)alloc(64 * sizeof(QUANT_TYPE));
249 
250     // read quantization entries, in zag order
251     for (i = 0; i < 64; i++)
252     {
253       temp = get_bits_1(8);
254 
255       if (prec)
256         temp = (temp << 8) + get_bits_1(8);
257 
258       if (use_mmx_idct)
259         quant[n][ZAG[i]] = (temp * aan_scales[ZAG[i]] + (1 << (AAN_SCALE_BITS - IFAST_SCALE_BITS - 1))) >> (AAN_SCALE_BITS - IFAST_SCALE_BITS);
260       else
261         quant[n][i] = temp;
262     }
263 
264     i = 64 + 1;
265 
266     if (prec)
267       i += 64;
268 
269     if (left < (uint)i)
270       terminate(JPGD_BAD_DQT_LENGTH);
271 
272     left -= i;
273   }
274 }
275 //------------------------------------------------------------------------------
276 // Read the start of frame (SOF) marker.
read_sof_marker(void)277 void jpeg_decoder::read_sof_marker(void)
278 {
279   int i;
280   uint left;
281 
282   left = get_bits_1(16);
283 
284   if (get_bits_1(8) != 8)   /* precision: sorry, only 8-bit precision is supported right now */
285     terminate(JPGD_BAD_PRECISION);
286 
287   image_y_size = get_bits_1(16);
288 
289   if ((image_y_size < 1) || (image_y_size > JPGD_MAX_HEIGHT))
290     terminate(JPGD_BAD_HEIGHT);
291 
292   image_x_size = get_bits_1(16);
293 
294   if ((image_x_size < 1) || (image_x_size > JPGD_MAX_WIDTH))
295     terminate(JPGD_BAD_WIDTH);
296 
297   comps_in_frame = get_bits_1(8);
298 
299   if (comps_in_frame > JPGD_MAXCOMPONENTS)
300     terminate(JPGD_TOO_MANY_COMPONENTS);
301 
302   if (left != (uint)(comps_in_frame * 3 + 8))
303     terminate(JPGD_BAD_SOF_LENGTH);
304 
305   for (i = 0; i < comps_in_frame; i++)
306   {
307     comp_ident[i]  = get_bits_1(8);
308     comp_h_samp[i] = get_bits_1(4);
309     comp_v_samp[i] = get_bits_1(4);
310     comp_quant[i]  = get_bits_1(8);
311   }
312 }
313 //------------------------------------------------------------------------------
314 // Used to skip unrecognized markers.
skip_variable_marker(void)315 void jpeg_decoder::skip_variable_marker(void)
316 {
317   uint left;
318 
319   left = get_bits_1(16);
320 
321   if (left < 2)
322     terminate(JPGD_BAD_VARIABLE_MARKER);
323 
324   left -= 2;
325 
326   while (left)
327   {
328     get_bits_1(8);
329     left--;
330   }
331 }
332 //------------------------------------------------------------------------------
333 // Read a define restart interval (DRI) marker.
read_dri_marker(void)334 void jpeg_decoder::read_dri_marker(void)
335 {
336   if (get_bits_1(16) != 4)
337     terminate(JPGD_BAD_DRI_LENGTH);
338 
339   restart_interval = get_bits_1(16);
340 }
341 //------------------------------------------------------------------------------
342 // Read a start of scan (SOS) marker.
read_sos_marker(void)343 void jpeg_decoder::read_sos_marker(void)
344 {
345   uint left;
346   int i, ci, n, c, cc;
347 
348   left = get_bits_1(16);
349 
350   n = get_bits_1(8);
351 
352   comps_in_scan = n;
353 
354   left -= 3;
355 
356   if ( (left != (uint)(n * 2 + 3)) || (n < 1) || (n > JPGD_MAXCOMPSINSCAN) )
357     terminate(JPGD_BAD_SOS_LENGTH);
358 
359   for (i = 0; i < n; i++)
360   {
361     cc = get_bits_1(8);
362     c = get_bits_1(8);
363     left -= 2;
364 
365     for (ci = 0; ci < comps_in_frame; ci++)
366       if (cc == comp_ident[ci])
367         break;
368 
369     if (ci >= comps_in_frame)
370       terminate(JPGD_BAD_SOS_COMP_ID);
371 
372     comp_list[i]    = ci;
373     comp_dc_tab[ci] = (c >> 4) & 15;
374     comp_ac_tab[ci] = (c & 15) + (JPGD_MAXHUFFTABLES >> 1);
375   }
376 
377   spectral_start  = get_bits_1(8);
378   spectral_end    = get_bits_1(8);
379   successive_high = get_bits_1(4);
380   successive_low  = get_bits_1(4);
381 
382   if (!progressive_flag)
383   {
384     spectral_start = 0;
385     spectral_end = 63;
386   }
387 
388   left -= 3;
389 
390   while (left)                  /* read past whatever is left */
391   {
392     get_bits_1(8);
393     left--;
394   }
395 }
396 //------------------------------------------------------------------------------
397 // Finds the next marker.
next_marker(void)398 int jpeg_decoder::next_marker(void)
399 {
400   uint c, bytes;
401 
402   bytes = 0;
403 
404   do
405   {
406     do
407     {
408       bytes++;
409 
410       c = get_bits_1(8);
411 
412     } while (c != 0xFF);
413 
414     do
415     {
416       c = get_bits_1(8);
417 
418     } while (c == 0xFF);
419 
420   } while (c == 0);
421 
422   // If bytes > 0 here, there where extra bytes before the marker (not good).
423 
424   return c;
425 }
426 //------------------------------------------------------------------------------
427 // Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
428 // encountered.
process_markers(void)429 int jpeg_decoder::process_markers(void)
430 {
431   int c;
432 
433   for ( ; ; )
434   {
435     c = next_marker();
436 
437     switch (c)
438     {
439       case M_SOF0:
440       case M_SOF1:
441       case M_SOF2:
442       case M_SOF3:
443       case M_SOF5:
444       case M_SOF6:
445       case M_SOF7:
446 //      case M_JPG:
447       case M_SOF9:
448       case M_SOF10:
449       case M_SOF11:
450       case M_SOF13:
451       case M_SOF14:
452       case M_SOF15:
453       case M_SOI:
454       case M_EOI:
455       case M_SOS:
456       {
457         return c;
458       }
459       case M_DHT:
460       {
461         read_dht_marker();
462         break;
463       }
464       // Sorry, no arithmitic support at this time. Dumb patents!
465       case M_DAC:
466       {
467         terminate(JPGD_NO_ARITHMITIC_SUPPORT);
468         break;
469       }
470       case M_DQT:
471       {
472         read_dqt_marker();
473         break;
474       }
475       case M_DRI:
476       {
477         read_dri_marker();
478         break;
479       }
480       //case M_APP0:  /* no need to read the JFIF marker */
481 
482       case M_JPG:
483       case M_RST0:    /* no parameters */
484       case M_RST1:
485       case M_RST2:
486       case M_RST3:
487       case M_RST4:
488       case M_RST5:
489       case M_RST6:
490       case M_RST7:
491       case M_TEM:
492       {
493         terminate(JPGD_UNEXPECTED_MARKER);
494         break;
495       }
496       default:    /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
497       {
498         skip_variable_marker();
499         break;
500       }
501 
502     }
503   }
504 }
505 //------------------------------------------------------------------------------
506 // Finds the start of image (SOI) marker.
507 // This code is rather defensive: it only checks the first 512 bytes to avoid
508 // false positives.
locate_soi_marker(void)509 void jpeg_decoder::locate_soi_marker(void)
510 {
511   uint lastchar, thischar;
512   ulong bytesleft;
513 
514   lastchar = get_bits_1(8);
515 
516   thischar = get_bits_1(8);
517 
518   /* ok if it's a normal JPEG file without a special header */
519 
520   if ((lastchar == 0xFF) && (thischar == M_SOI))
521     return;
522 
523   bytesleft = 512;
524 
525   for ( ; ; )
526   {
527     if (--bytesleft == 0)
528       terminate(JPGD_NOT_JPEG);
529 
530     lastchar = thischar;
531 
532     thischar = get_bits_1(8);
533 
534     if ((lastchar == 0xFF) && (thischar == M_SOI))
535       break;
536   }
537 
538   /* Check the next character after marker: if it's not 0xFF, it can't
539      be the start of the next marker, so it probably isn't a JPEG */
540 
541   thischar = (bit_buf >> 8) & 0xFF;
542 
543   if (thischar != 0xFF)
544     terminate(JPGD_NOT_JPEG);
545 }
546 //------------------------------------------------------------------------------
547 // Find a start of frame (SOF) marker.
locate_sof_marker(void)548 void jpeg_decoder::locate_sof_marker(void)
549 {
550   int c;
551 
552   locate_soi_marker();
553 
554   c = process_markers();
555 
556   switch (c)
557   {
558     case M_SOF2:
559       progressive_flag = TRUE;
560     case M_SOF0:  /* baseline DCT */
561     case M_SOF1:  /* extended sequential DCT */
562     {
563       read_sof_marker();
564       break;
565     }
566     case M_SOF9:  /* Arithmitic coding */
567     {
568       terminate(JPGD_NO_ARITHMITIC_SUPPORT);
569       break;
570     }
571 
572     default:
573     {
574       terminate(JPGD_UNSUPPORTED_MARKER);
575       break;
576     }
577   }
578 }
579 //------------------------------------------------------------------------------
580 // Find a start of scan (SOS) marker.
locate_sos_marker(void)581 int jpeg_decoder::locate_sos_marker(void)
582 {
583   int c;
584 
585   c = process_markers();
586 
587   if (c == M_EOI)
588     return FALSE;
589   else if (c != M_SOS)
590     terminate(JPGD_UNEXPECTED_MARKER);
591 
592   read_sos_marker();
593 
594   return TRUE;
595 }
596 //------------------------------------------------------------------------------
597 // Reset everything to default/uninitialized state.
init(Pjpeg_decoder_stream Pstream,bool use_mmx)598 void jpeg_decoder::init(Pjpeg_decoder_stream Pstream, bool use_mmx)
599 {
600   error_code = 0;
601 
602   ready_flag = false;
603 
604   image_x_size = image_y_size = 0;
605 
606   this->Pstream = Pstream;
607 
608 #ifdef SUPPORT_MMX
609   this->use_mmx = use_mmx;
610   use_mmx_idct = (use_mmx) && (jpeg_idct_ifast_avail());
611 #else
612   this->use_mmx = false;
613   use_mmx_idct = false;
614 #endif
615 
616   progressive_flag = FALSE;
617 
618   memset(huff_num, 0, sizeof(huff_num));
619   memset(huff_val, 0, sizeof(huff_val));
620   memset(quant, 0, sizeof(quant));
621 
622   scan_type = 0;
623 
624   comps_in_frame = 0;
625 
626   memset(comp_h_samp, 0, sizeof(comp_h_samp));
627   memset(comp_v_samp, 0, sizeof(comp_v_samp));
628   memset(comp_quant, 0, sizeof(comp_quant));
629   memset(comp_ident, 0, sizeof(comp_ident));
630   memset(comp_h_blocks, 0, sizeof(comp_h_blocks));
631   memset(comp_v_blocks, 0, sizeof(comp_v_blocks));
632 
633   comps_in_scan = 0;
634   memset(comp_list, 0, sizeof(comp_list));
635   memset(comp_dc_tab, 0, sizeof(comp_dc_tab));
636   memset(comp_ac_tab, 0, sizeof(comp_ac_tab));
637 
638   spectral_start = 0;
639   spectral_end = 0;
640   successive_low = 0;
641   successive_high = 0;
642 
643   max_mcu_x_size = 0;
644   max_mcu_y_size = 0;
645 
646   blocks_per_mcu = 0;
647   max_blocks_per_row = 0;
648   mcus_per_row = 0;
649   mcus_per_col = 0;
650 
651   memset(mcu_org, 0, sizeof(mcu_org));
652 
653   total_lines_left = 0;
654   mcu_lines_left = 0;
655 
656   real_dest_bytes_per_scan_line = 0;
657   dest_bytes_per_scan_line = 0;
658   dest_bytes_per_pixel = 0;
659 
660   memset(blocks, 0, sizeof(blocks));
661 
662   memset(h, 0, sizeof(h));
663 
664   memset(dc_coeffs, 0, sizeof(dc_coeffs));
665   memset(ac_coeffs, 0, sizeof(ac_coeffs));
666   memset(block_y_mcu, 0, sizeof(block_y_mcu));
667 
668   eob_run = 0;
669 
670   memset(block_y_mcu, 0, sizeof(block_y_mcu));
671 
672   Pin_buf_ofs = in_buf;
673   in_buf_left = 0;
674   eof_flag = false;
675   tem_flag = 0;
676 
677   memset(padd_1, 0, sizeof(padd_1));
678   memset(in_buf, 0, sizeof(in_buf));
679   memset(padd_2, 0, sizeof(padd_2));
680 
681   restart_interval = 0;
682   restarts_left    = 0;
683   next_restart_num = 0;
684 
685   max_mcus_per_row = 0;
686   max_blocks_per_mcu = 0;
687   max_mcus_per_col = 0;
688 
689   memset(component, 0, sizeof(component));
690   memset(last_dc_val, 0, sizeof(last_dc_val));
691   memset(dc_huff_seg, 0, sizeof(dc_huff_seg));
692   memset(ac_huff_seg, 0, sizeof(ac_huff_seg));
693   memset(block_seg, 0, sizeof(block_seg));
694   Psample_buf = NULL;
695 
696   total_bytes_read = 0;
697 
698   // Tell the stream we're going to use it.
699   Pstream->attach();
700 
701   use_mmx_getbits = false;
702   mmx_active = false;
703 
704   // Ready the input buffer.
705   prep_in_buffer();
706 
707   // Prime the bit buffer.
708   bits_left = 16;
709   bit_buf_64[0] = 0;
710   bit_buf_64[1] = 0;
711 
712   get_bits_1(16);
713   get_bits_1(16);
714 
715   for (int i = 0; i < JPGD_MAXBLOCKSPERROW; i++)
716     block_max_zag_set[i] = 64;
717 }
718 //------------------------------------------------------------------------------
719 #define SCALEBITS 16
720 #define ONE_HALF ((long) 1 << (SCALEBITS-1))
721 #define FIX(x) ((long) ((x) * (1L<<SCALEBITS) + 0.5))
722 //------------------------------------------------------------------------------
723 // Create a few tables that allow us to quickly convert YCbCr to RGB.
create_look_ups(void)724 void jpeg_decoder::create_look_ups(void)
725 {
726   int i, k;
727 
728   for (i = 0; i <= 255; i++)
729   {
730     //k = (i * 2) - 255;
731 		k = (i * 2) - 256; // Dec. 28 2001- change so table[128] == 0
732 
733     crr[i] = ( FIX(1.40200/2)  * k + ONE_HALF) >> SCALEBITS;
734     cbb[i] = ( FIX(1.77200/2)  * k + ONE_HALF) >> SCALEBITS;
735 
736     crg[i] = (-FIX(0.71414/2)) * k;
737     cbg[i] = (-FIX(0.34414/2)) * k + ONE_HALF;
738   }
739 }
740 //------------------------------------------------------------------------------
741 // This method throws back into the stream any bytes that where read
742 // into the bit buffer during initial marker scanning.
fix_in_buffer(void)743 void jpeg_decoder::fix_in_buffer(void)
744 {
745   /* In case any 0xFF's where pulled into the buffer during marker scanning */
746 
747   if (bits_left == 16)
748     stuff_char( (uchar)((bit_buf >> 16) & 0xFF));
749 
750   if (bits_left >= 8)
751     stuff_char( (uchar)((bit_buf >> 24) & 0xFF));
752 
753   stuff_char( (uchar)(bit_buf & 0xFF) );
754 
755   stuff_char( (uchar)((bit_buf >> 8) & 0xFF) );
756 
757 #ifdef SUPPORT_MMX
758   if (use_mmx_getbits)
759   {
760     bits_left = 48;
761     get_bits_2_mmx_init();
762     get_bits_2_mmx(16);
763     get_bits_2_mmx(16);
764     get_bits_2_mmx(16);
765     get_bits_2_mmx(16);
766     get_bits_2_mmx_deinit();
767   }
768   else
769 #endif
770   {
771     bits_left = 16;
772     //bit_buf = 0;
773     get_bits_2(16);
774     get_bits_2(16);
775   }
776 }
777 //------------------------------------------------------------------------------
778 // Performs a 2D IDCT over the entire row's coefficient buffer.
transform_row(void)779 void jpeg_decoder::transform_row(void)
780 {
781 #ifdef SUPPORT_MMX
782   if (use_mmx_idct)
783   {
784     BLOCK_TYPE *Psrc_ptr = block_seg[0];
785     uchar *Pdst_ptr = Psample_buf - 8;
786 
787     for (int mcu_row = 0; mcu_row < mcus_per_row; mcu_row++)
788     {
789       for (int mcu_block = 0; mcu_block < blocks_per_mcu; mcu_block++)
790       {
791         int component_id = mcu_org[mcu_block];
792         QUANT_TYPE *Pquant_ptr = quant[comp_quant[component_id]];
793 
794         uchar * outptr[8];
795         outptr[0] = Pdst_ptr;
796         outptr[1] = Pdst_ptr+8*1;
797         outptr[2] = Pdst_ptr+8*2;
798         outptr[3] = Pdst_ptr+8*3;
799         outptr[4] = Pdst_ptr+8*4;
800         outptr[5] = Pdst_ptr+8*5;
801         outptr[6] = Pdst_ptr+8*6;
802         outptr[7] = Pdst_ptr+8*7;
803 
804         jpeg_idct_ifast(
805           Psrc_ptr,
806           Pquant_ptr,
807           outptr,
808           8);
809 
810         Psrc_ptr += 64;
811         Pdst_ptr += 64;
812       }
813     }
814 
815     jpeg_idct_ifast_deinit();
816   }
817   else
818 #endif
819   {
820     BLOCK_TYPE *Psrc_ptr = block_seg[0];
821     uchar *Pdst_ptr = Psample_buf;
822 
823     for (int i = max_blocks_per_row; i > 0; i--)
824     {
825       // Copy the block to a temp. buffer to prevent the IDCT
826       // from modifying the entire block.
827       memcpy(temp_block, Psrc_ptr, 64 * sizeof(BLOCK_TYPE));
828       idct(temp_block, Pdst_ptr);
829       Psrc_ptr += 64;
830       Pdst_ptr += 64;
831     }
832   }
833 }
834 //------------------------------------------------------------------------------
835 // The coeff_buf series of methods originally stored the coefficients
836 // into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
837 // was used to make this process more efficient. Now, we can store the entire
838 // thing in RAM.
coeff_buf_open(int block_num_x,int block_num_y,int block_len_x,int block_len_y)839 Pcoeff_buf_t jpeg_decoder::coeff_buf_open(
840   int block_num_x, int block_num_y,
841   int block_len_x, int block_len_y)
842 {
843   Pcoeff_buf_t cb = (Pcoeff_buf_t)alloc(sizeof(coeff_buf_t));
844 
845   cb->block_num_x = block_num_x;
846   cb->block_num_y = block_num_y;
847 
848   cb->block_len_x = block_len_x;
849   cb->block_len_y = block_len_y;
850 
851   cb->block_size = (block_len_x * block_len_y) * sizeof(BLOCK_TYPE);
852 
853   cb->Pdata = (uchar *)alloc(cb->block_size * block_num_x * block_num_y);
854 
855   return cb;
856 }
857 //------------------------------------------------------------------------------
coeff_buf_read(Pcoeff_buf_t cb,int block_x,int block_y,BLOCK_TYPE * buffer)858 void jpeg_decoder::coeff_buf_read(
859   Pcoeff_buf_t cb,
860   int block_x, int block_y,
861   BLOCK_TYPE *buffer)
862 {
863   if (block_x >= cb->block_num_x)
864     terminate(JPGD_ASSERTION_ERROR);
865 
866   if (block_y >= cb->block_num_y)
867     terminate(JPGD_ASSERTION_ERROR);
868 
869   memcpy(buffer,
870          cb->Pdata + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x),
871          cb->block_size);
872 }
873 //------------------------------------------------------------------------------
coeff_buf_write(Pcoeff_buf_t cb,int block_x,int block_y,BLOCK_TYPE * buffer)874 void jpeg_decoder::coeff_buf_write(
875   Pcoeff_buf_t cb,
876   int block_x, int block_y,
877   BLOCK_TYPE *buffer)
878 {
879   if (block_x >= cb->block_num_x)
880     terminate(JPGD_ASSERTION_ERROR);
881 
882   if (block_y >= cb->block_num_y)
883     terminate(JPGD_ASSERTION_ERROR);
884 
885   memcpy(cb->Pdata + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x),
886          buffer,
887          cb->block_size);
888 }
889 //------------------------------------------------------------------------------
coeff_buf_getp(Pcoeff_buf_t cb,int block_x,int block_y)890 BLOCK_TYPE *jpeg_decoder::coeff_buf_getp(
891   Pcoeff_buf_t cb,
892   int block_x, int block_y)
893 {
894   if (block_x >= cb->block_num_x)
895     terminate(JPGD_ASSERTION_ERROR);
896 
897   if (block_y >= cb->block_num_y)
898     terminate(JPGD_ASSERTION_ERROR);
899 
900   return (BLOCK_TYPE *)(cb->Pdata + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x));
901 }
902 //------------------------------------------------------------------------------
903 // Loads and dequantizes the next row of (already decoded) coefficients.
904 // Progressive images only.
load_next_row(void)905 void jpeg_decoder::load_next_row(void)
906 {
907   int i;
908   BLOCK_TYPE *p;
909   QUANT_TYPE *q;
910   int mcu_row, mcu_block, row_block = 0;
911   int component_num, component_id;
912   int block_x_mcu[JPGD_MAXCOMPONENTS];
913 
914   memset(block_x_mcu, 0, JPGD_MAXCOMPONENTS * sizeof(int));
915 
916   for (mcu_row = 0; mcu_row < mcus_per_row; mcu_row++)
917   {
918     int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
919 
920     for (mcu_block = 0; mcu_block < blocks_per_mcu; mcu_block++)
921     {
922       component_id = mcu_org[mcu_block];
923 
924       p = block_seg[row_block];
925       q = quant[comp_quant[component_id]];
926 
927       BLOCK_TYPE *pAC = coeff_buf_getp(ac_coeffs[component_id],
928                                   block_x_mcu[component_id] + block_x_mcu_ofs,
929                                   block_y_mcu[component_id] + block_y_mcu_ofs);
930 
931       BLOCK_TYPE *pDC = coeff_buf_getp(dc_coeffs[component_id],
932                                   block_x_mcu[component_id] + block_x_mcu_ofs,
933                                   block_y_mcu[component_id] + block_y_mcu_ofs);
934       p[0] = pDC[0];
935       memcpy(&p[1], &pAC[1], 63 * sizeof(BLOCK_TYPE));
936 
937       if (!use_mmx_idct)
938       {
939         for (i = 63; i > 0; i--)
940           if (p[ZAG[i]])
941             break;
942 
943         //block_num[row_block++] = i + 1;
944 
945         for ( ; i >= 0; i--)
946           if (p[ZAG[i]])
947             p[ZAG[i]] *= q[i];
948       }
949 
950       row_block++;
951 
952       if (comps_in_scan == 1)
953         block_x_mcu[component_id]++;
954       else
955       {
956         if (++block_x_mcu_ofs == comp_h_samp[component_id])
957         {
958           block_x_mcu_ofs = 0;
959 
960           if (++block_y_mcu_ofs == comp_v_samp[component_id])
961           {
962             block_y_mcu_ofs = 0;
963 
964             block_x_mcu[component_id] += comp_h_samp[component_id];
965           }
966         }
967       }
968     }
969   }
970 
971   if (comps_in_scan == 1)
972     block_y_mcu[comp_list[0]]++;
973   else
974   {
975     for (component_num = 0; component_num < comps_in_scan; component_num++)
976     {
977       component_id = comp_list[component_num];
978 
979       block_y_mcu[component_id] += comp_v_samp[component_id];
980     }
981   }
982 }
983 //------------------------------------------------------------------------------
984 // Restart interval processing.
process_restart(void)985 void jpeg_decoder::process_restart(void)
986 {
987   int i, c;
988 
989   // Align to a byte boundry
990   // FIXME: Is this really necessary? get_bits_2() never reads in markers!
991   //get_bits_2(bits_left & 7);
992 
993   // Let's scan a little bit to find the marker, but not _too_ far.
994   // 1536 is a "fudge factor" that determines how much to scan.
995   for (i = 1536; i > 0; i--)
996     if (get_char() == 0xFF)
997       break;
998 
999   if (i == 0)
1000     terminate(JPGD_BAD_RESTART_MARKER);
1001 
1002   for ( ; i > 0; i--)
1003     if ((c = get_char()) != 0xFF)
1004       break;
1005 
1006   if (i == 0)
1007     terminate(JPGD_BAD_RESTART_MARKER);
1008 
1009   // Is it the expected marker? If not, something bad happened.
1010   if (c != (next_restart_num + M_RST0))
1011     terminate(JPGD_BAD_RESTART_MARKER);
1012 
1013   // Reset each component's DC prediction values.
1014   memset(&last_dc_val, 0, comps_in_frame * sizeof(uint));
1015 
1016   eob_run = 0;
1017 
1018   restarts_left = restart_interval;
1019 
1020   next_restart_num = (next_restart_num + 1) & 7;
1021 
1022   // Get the bit buffer going again...
1023 
1024 #ifdef SUPPORT_MMX
1025   if (use_mmx_getbits)
1026   {
1027     bits_left = 48;
1028     get_bits_2_mmx(16);
1029     get_bits_2_mmx(16);
1030     get_bits_2_mmx(16);
1031     get_bits_2_mmx(16);
1032   }
1033   else
1034 #endif
1035   {
1036     bits_left = 16;
1037     //bit_buf = 0;
1038     get_bits_2(16);
1039     get_bits_2(16);
1040   }
1041 }
1042 //------------------------------------------------------------------------------
1043 // Decodes and dequantizes the next row of coefficients.
decode_next_row(void)1044 void jpeg_decoder::decode_next_row(void)
1045 {
1046   int row_block = 0;
1047 
1048   // Clearing the entire row block buffer can take a lot of time!
1049   // Instead of clearing the entire buffer each row, keep track
1050   // of the number of nonzero entries written to each block and do
1051   // selective clears.
1052   //memset(block_seg[0], 0, mcus_per_row * blocks_per_mcu * 64 * sizeof(BLOCK_TYPE));
1053 
1054   for (int mcu_row = 0; mcu_row < mcus_per_row; mcu_row++)
1055   {
1056     if ((restart_interval) && (restarts_left == 0))
1057       process_restart();
1058 
1059     for (int mcu_block = 0; mcu_block < blocks_per_mcu; mcu_block++)
1060     {
1061       int component_id = mcu_org[mcu_block];
1062 
1063       BLOCK_TYPE *p = block_seg[row_block];
1064       QUANT_TYPE *q = quant[comp_quant[component_id]];
1065       int r, s, k;
1066 
1067       if ((s = huff_decode(h[comp_dc_tab[component_id]])) != 0)
1068       {
1069         r = get_bits_2(s);
1070         s = HUFF_EXTEND(r, s);
1071       }
1072 
1073       last_dc_val[component_id] = (s += last_dc_val[component_id]);
1074 
1075       if (use_mmx_idct)
1076         p[0] = s;
1077       else
1078         p[0] = s * q[0];
1079 
1080       int prev_num_set = block_max_zag_set[row_block];
1081 
1082       Phuff_tables_t Ph = h[comp_ac_tab[component_id]];
1083 
1084       for (k = 1; k < 64; k++)
1085       {
1086         s = huff_decode(Ph);
1087 
1088         r = s >> 4;
1089         s &= 15;
1090 
1091         if (s)
1092         {
1093           if (r)
1094           {
1095             if ((k + r) > 63)
1096               terminate(JPGD_DECODE_ERROR);
1097 
1098             if (k < prev_num_set)
1099             {
1100               int n = min(r, prev_num_set - k);
1101               int kt = k;
1102               while (n--)
1103                 p[ZAG[kt++]] = 0;
1104             }
1105 
1106             k += r;
1107           }
1108 
1109           r = get_bits_2(s);
1110           s = HUFF_EXTEND(r, s);
1111 
1112           //assert(k < 64);
1113 
1114           if (use_mmx_idct)
1115             p[ZAG[k]] = s;
1116           else
1117             p[ZAG[k]] = s * q[k];
1118         }
1119         else
1120         {
1121           if (r == 15)
1122           {
1123             if ((k + 15) > 63)
1124               terminate(JPGD_DECODE_ERROR);
1125 
1126             if (k < prev_num_set)
1127             {
1128               int n = min(16, prev_num_set - k);		//bugfix Dec. 19, 2001 - was 15!
1129               int kt = k;
1130               while (n--)
1131                 p[ZAG[kt++]] = 0;
1132             }
1133 
1134             k += 15;
1135           }
1136           else
1137           {
1138             //while (k < 64)
1139             //  p[ZAG[k++]] = 0;
1140 
1141             break;
1142           }
1143         }
1144       }
1145 
1146       if (k < prev_num_set)
1147       {
1148         int kt = k;
1149         while (kt < prev_num_set)
1150           p[ZAG[kt++]] = 0;
1151       }
1152 
1153       block_max_zag_set[row_block] = k;
1154 
1155       //block_num[row_block++] = k;
1156       row_block++;
1157     }
1158 
1159     restarts_left--;
1160   }
1161 }
1162 //------------------------------------------------------------------------------
1163 #ifdef SUPPORT_MMX
decode_next_row_mmx(void)1164 void jpeg_decoder::decode_next_row_mmx(void)
1165 {
1166   int row_block = 0;
1167 
1168   // Clearing the entire row block buffer can take a lot of time!
1169   // Instead of clearing the entire buffer each row, keep track
1170   // of the number of nonzero entries written to each block and do
1171   // selective clears.
1172   //memset(block_seg[0], 0, mcus_per_row * blocks_per_mcu * 64 * sizeof(BLOCK_TYPE));
1173 
1174   get_bits_2_mmx_init();
1175 
1176   for (int mcu_row = 0; mcu_row < mcus_per_row; mcu_row++)
1177   {
1178     if ((restart_interval) && (restarts_left == 0))
1179       process_restart();
1180 
1181     for (int mcu_block = 0; mcu_block < blocks_per_mcu; mcu_block++)
1182     {
1183       int component_id = mcu_org[mcu_block];
1184 
1185       BLOCK_TYPE *p = block_seg[row_block];
1186       QUANT_TYPE *q = quant[comp_quant[component_id]];
1187       int r, s, k;
1188 
1189       if ((s = huff_decode_mmx(h[comp_dc_tab[component_id]])) != 0)
1190       {
1191         r = get_bits_2_mmx(s);
1192         s = HUFF_EXTEND(r, s);
1193       }
1194 
1195       last_dc_val[component_id] = (s += last_dc_val[component_id]);
1196 
1197       if (use_mmx_idct)
1198         p[0] = s;
1199       else
1200         p[0] = s * q[0];
1201 
1202       int prev_num_set = block_max_zag_set[row_block];
1203 
1204       Phuff_tables_t Ph = h[comp_ac_tab[component_id]];
1205 
1206       for (k = 1; k < 64; k++)
1207       {
1208         s = huff_decode_mmx(Ph);
1209 
1210         r = s >> 4;
1211         s &= 15;
1212 
1213         if (s)
1214         {
1215           if (r)
1216           {
1217 			if ((k + r) <= 63) {
1218 
1219               if (k < prev_num_set)
1220 			  {
1221                 int n = min(r, prev_num_set - k);
1222                 int kt = k;
1223                 while (n--)
1224                   p[ZAG[kt++]] = 0;
1225 			  }
1226 
1227               k += r;
1228 
1229 			} else {
1230 #ifdef _DEBUG
1231 				terminate(JPGD_DECODE_ERROR);
1232 #endif
1233 			}
1234           }
1235 
1236           r = get_bits_2_mmx(s);
1237           s = HUFF_EXTEND(r, s);
1238 
1239           assert(k < 64);
1240 
1241           if (use_mmx_idct)
1242             p[ZAG[k]] = s;
1243           else
1244             p[ZAG[k]] = s * q[k];
1245         }
1246         else
1247         {
1248           if (r == 15)
1249           {
1250 			if ((k + 15) <= 63) {
1251 
1252               if (k < prev_num_set)
1253 			  {
1254                 int n = min(16, prev_num_set - k);		//bugfix Dec. 19, 2001 - was 15!
1255                 int kt = k;
1256                 while (n--)
1257                   p[ZAG[kt++]] = 0;
1258 			  }
1259 
1260               k += 15;
1261 
1262 			} else {
1263 #ifdef _DEBUG
1264 				terminate(JPGD_DECODE_ERROR);
1265 #endif
1266 			}
1267           }
1268           else
1269             break;
1270         }
1271       }
1272 
1273       if (k < prev_num_set)
1274       {
1275         int kt = k;
1276         while (kt < prev_num_set)
1277           p[ZAG[kt++]] = 0;
1278       }
1279 
1280       block_max_zag_set[row_block] = k;
1281 
1282       //block_num[row_block++] = k;
1283       row_block++;
1284     }
1285 
1286     restarts_left--;
1287   }
1288 
1289   get_bits_2_mmx_deinit();
1290 }
1291 #endif
1292 //------------------------------------------------------------------------------
1293 // YCbCr H1V1 (1x1:1:1, 3 blocks per MCU) to 24-bit RGB
H1V1Convert(void)1294 void jpeg_decoder::H1V1Convert(void)
1295 {
1296   int row = max_mcu_y_size - mcu_lines_left;
1297   uchar *d = scan_line_0;
1298   uchar *s = Psample_buf + row * 8;
1299 
1300   for (int i = max_mcus_per_row; i > 0; i--)
1301   {
1302     for (int j = 0; j < 8; j++)
1303     {
1304       int y = s[j];
1305       int cb = s[64+j];
1306       int cr = s[128+j];
1307 
1308       d[0] = clamp(y + crr[cr]);
1309       d[1] = clamp(y + ((crg[cr] + cbg[cb]) >> 16));
1310       d[2] = clamp(y + cbb[cb]);
1311       d += 4;
1312     }
1313 
1314     s += 64*3;
1315   }
1316 }
1317 //------------------------------------------------------------------------------
1318 // YCbCr H2V1 (2x1:1:1, 4 blocks per MCU) to 24-bit RGB
H2V1Convert(void)1319 void jpeg_decoder::H2V1Convert(void)
1320 {
1321   int row = max_mcu_y_size - mcu_lines_left;
1322   uchar *d0 = scan_line_0;
1323   uchar *y = Psample_buf + row * 8;
1324   uchar *c = Psample_buf + 2*64 + row * 8;
1325 
1326   for (int i = max_mcus_per_row; i > 0; i--)
1327   {
1328     for (int l = 0; l < 2; l++)
1329     {
1330       for (int j = 0; j < 4; j++)
1331       {
1332         int cb = c[0];
1333         int cr = c[64];
1334 
1335         int rc = crr[cr];
1336         int gc = ((crg[cr] + cbg[cb]) >> 16);
1337         int bc = cbb[cb];
1338 
1339         int yy = y[j<<1];
1340         d0[0] = clamp(yy+rc);
1341         d0[1] = clamp(yy+gc);
1342         d0[2] = clamp(yy+bc);
1343 
1344         yy = y[(j<<1)+1];
1345         d0[4] = clamp(yy+rc);
1346         d0[5] = clamp(yy+gc);
1347         d0[6] = clamp(yy+bc);
1348 
1349         d0 += 8;
1350 
1351         c++;
1352       }
1353       y += 64;
1354     }
1355 
1356     y += 64*4 - 64*2;
1357     c += 64*4 - 8;
1358   }
1359 }
1360 //------------------------------------------------------------------------------
1361 // YCbCr H2V1 (1x2:1:1, 4 blocks per MCU) to 24-bit RGB
H1V2Convert(void)1362 void jpeg_decoder::H1V2Convert(void)
1363 {
1364   int row = max_mcu_y_size - mcu_lines_left;
1365   uchar *d0 = scan_line_0;
1366   uchar *d1 = scan_line_1;
1367   uchar *y;
1368   uchar *c;
1369 
1370   if (row < 8)
1371     y = Psample_buf + row * 8;
1372   else
1373     y = Psample_buf + 64*1 + (row & 7) * 8;
1374 
1375   c = Psample_buf + 64*2 + (row >> 1) * 8;
1376 
1377   for (int i = max_mcus_per_row; i > 0; i--)
1378   {
1379     for (int j = 0; j < 8; j++)
1380     {
1381       int cb = c[0+j];
1382       int cr = c[64+j];
1383 
1384       int rc = crr[cr];
1385       int gc = ((crg[cr] + cbg[cb]) >> 16);
1386       int bc = cbb[cb];
1387 
1388       int yy = y[j];
1389       d0[0] = clamp(yy+rc);
1390       d0[1] = clamp(yy+gc);
1391       d0[2] = clamp(yy+bc);
1392 
1393       yy = y[8+j];
1394       d1[0] = clamp(yy+rc);
1395       d1[1] = clamp(yy+gc);
1396       d1[2] = clamp(yy+bc);
1397 
1398       d0 += 4;
1399       d1 += 4;
1400     }
1401 
1402     y += 64*4;
1403     c += 64*4;
1404   }
1405 }
1406 //------------------------------------------------------------------------------
1407 // Y (1 block per MCU) to 8-bit greyscale
GrayConvert(void)1408 void jpeg_decoder::GrayConvert(void)
1409 {
1410   int row = max_mcu_y_size - mcu_lines_left;
1411   uchar *d = scan_line_0;
1412   uchar *s = Psample_buf + row * 8;
1413 
1414   for (int i = max_mcus_per_row; i > 0; i--)
1415   {
1416 #if 0
1417     d[0] = s[0];
1418     d[1] = s[1];
1419     d[2] = s[2];
1420     d[3] = s[3];
1421     d[4] = s[4];
1422     d[5] = s[5];
1423     d[6] = s[6];
1424     d[7] = s[7];
1425 #endif
1426     *(uint *)d = *(uint *)s;
1427     *(uint *)(&d[4]) = *(uint *)(&s[4]);
1428 
1429     s += 64;
1430     d += 8;
1431   }
1432 }
1433 //------------------------------------------------------------------------------
1434 // Find end of image (EOI) marker, so we can return to the user the
1435 // exact size of the input stream.
find_eoi(void)1436 void jpeg_decoder::find_eoi(void)
1437 {
1438   if (!progressive_flag)
1439   {
1440     // Attempt to read the EOI marker.
1441     //get_bits_2(bits_left & 7);
1442 
1443     // Prime the bit buffer
1444     bits_left = 16;
1445     //bit_buf = 0;
1446     get_bits_1(16);
1447     get_bits_1(16);
1448 
1449     // The next marker _should_ be EOI
1450     process_markers();
1451   }
1452 
1453   total_bytes_read -= in_buf_left;
1454 }
1455 //------------------------------------------------------------------------------
1456 // Returns the next scan line.
1457 // Returns JPGD_DONE if all scan lines have been returned.
1458 // Returns JPGD_OKAY if a scan line has been returned.
1459 // Returns JPGD_FAILED if an error occured.
decode(void ** Pscan_line_ofs,uint * Pscan_line_len)1460 int jpeg_decoder::decode(
1461   void * *Pscan_line_ofs, uint *Pscan_line_len)
1462 {
1463   if ((error_code) || (!ready_flag))
1464     return (JPGD_FAILED);
1465 
1466   if (total_lines_left == 0)
1467     return (JPGD_DONE);
1468 
1469   if (mcu_lines_left == 0)
1470   {
1471     if (setjmp(jmp_state))
1472       return (JPGD_DECODE_ERROR);
1473 
1474     if (progressive_flag)
1475       load_next_row();
1476     else
1477     {
1478 #ifdef SUPPORT_MMX
1479       if (use_mmx_getbits)
1480         decode_next_row_mmx();
1481       else
1482 #endif
1483         decode_next_row();
1484     }
1485 
1486     // Find the EOI marker if that was the last row.
1487     if (total_lines_left <= max_mcu_y_size)
1488       find_eoi();
1489 
1490     transform_row();
1491 
1492     mcu_lines_left = max_mcu_y_size;
1493   }
1494 
1495   switch (scan_type)
1496   {
1497     case JPGD_YH2V2:
1498     {
1499       if ((mcu_lines_left & 1) == 0)
1500       {
1501         H2V2Convert();
1502         *Pscan_line_ofs = scan_line_0;
1503       }
1504       else
1505         *Pscan_line_ofs = scan_line_1;
1506 
1507       break;
1508     }
1509     case JPGD_YH2V1:
1510     {
1511       H2V1Convert();
1512       *Pscan_line_ofs = scan_line_0;
1513       break;
1514     }
1515     case JPGD_YH1V2:
1516     {
1517       if ((mcu_lines_left & 1) == 0)
1518       {
1519         H1V2Convert();
1520         *Pscan_line_ofs = scan_line_0;
1521       }
1522       else
1523         *Pscan_line_ofs = scan_line_1;
1524 
1525       break;
1526     }
1527     case JPGD_YH1V1:
1528     {
1529       H1V1Convert();
1530       *Pscan_line_ofs = scan_line_0;
1531       break;
1532     }
1533     case JPGD_GRAYSCALE:
1534     {
1535       GrayConvert();
1536       *Pscan_line_ofs = scan_line_0;
1537 
1538       break;
1539     }
1540   }
1541 
1542   *Pscan_line_len = real_dest_bytes_per_scan_line;
1543 
1544   mcu_lines_left--;
1545   total_lines_left--;
1546 
1547   return (JPGD_OKAY);
1548 }
1549 //------------------------------------------------------------------------------
1550 // Creates the tables needed for efficient Huffman decoding.
make_huff_table(int index,Phuff_tables_t hs)1551 void jpeg_decoder::make_huff_table(
1552   int index,
1553   Phuff_tables_t hs)
1554 {
1555   int p, i, l, si;
1556   uchar huffsize[257];
1557   uint huffcode[257];
1558   uint code;
1559   uint subtree;
1560   int code_size;
1561   int lastp;
1562   int nextfreeentry;
1563   int currententry;
1564 
1565   p = 0;
1566 
1567   for (l = 1; l <= 16; l++)
1568   {
1569     for (i = 1; i <= huff_num[index][l]; i++)
1570       huffsize[p++] = l;
1571   }
1572 
1573   huffsize[p] = 0;
1574 
1575   lastp = p;
1576 
1577   code = 0;
1578   si = huffsize[0];
1579   p = 0;
1580 
1581   while (huffsize[p])
1582   {
1583     while (huffsize[p] == si)
1584     {
1585       huffcode[p++] = code;
1586       code++;
1587     }
1588 
1589     code <<= 1;
1590     si++;
1591   }
1592 
1593   memset(hs->look_up, 0, sizeof(hs->look_up));
1594   memset(hs->tree, 0, sizeof(hs->tree));
1595   memset(hs->code_size, 0, sizeof(hs->code_size));
1596 
1597   nextfreeentry = -1;
1598 
1599   p = 0;
1600 
1601   while (p < lastp)
1602   {
1603     i = huff_val[index][p];
1604     code = huffcode[p];
1605     code_size = huffsize[p];
1606 
1607     hs->code_size[i] = code_size;
1608 
1609     if (code_size <= 8)
1610     {
1611       code <<= (8 - code_size);
1612 
1613       for (l = 1 << (8 - code_size); l > 0; l--)
1614       {
1615         hs->look_up[code] = i;
1616         code++;
1617       }
1618     }
1619     else
1620     {
1621       subtree = (code >> (code_size - 8)) & 0xFF;
1622 
1623       currententry = hs->look_up[subtree];
1624 
1625       if (currententry == 0)
1626       {
1627         hs->look_up[subtree] = currententry = nextfreeentry;
1628 
1629         nextfreeentry -= 2;
1630       }
1631 
1632       code <<= (16 - (code_size - 8));
1633 
1634       for (l = code_size; l > 9; l--)
1635       {
1636         if ((code & 0x8000) == 0)
1637           currententry--;
1638 
1639         if (hs->tree[-currententry - 1] == 0)
1640         {
1641           hs->tree[-currententry - 1] = nextfreeentry;
1642 
1643           currententry = nextfreeentry;
1644 
1645           nextfreeentry -= 2;
1646         }
1647         else
1648           currententry = hs->tree[-currententry - 1];
1649 
1650         code <<= 1;
1651       }
1652 
1653       if ((code & 0x8000) == 0)
1654         currententry--;
1655 
1656       hs->tree[-currententry - 1] = i;
1657     }
1658 
1659     p++;
1660   }
1661 }
1662 //------------------------------------------------------------------------------
1663 // Verifies the quantization tables needed for this scan are available.
check_quant_tables(void)1664 void jpeg_decoder::check_quant_tables(void)
1665 {
1666   int i;
1667 
1668   for (i = 0; i < comps_in_scan; i++)
1669     if (quant[comp_quant[comp_list[i]]] == NULL)
1670       terminate(JPGD_UNDEFINED_QUANT_TABLE);
1671 }
1672 //------------------------------------------------------------------------------
1673 // Verifies that all the Huffman tables needed for this scan are available.
check_huff_tables(void)1674 void jpeg_decoder::check_huff_tables(void)
1675 {
1676   int i;
1677 
1678   for (i = 0; i < comps_in_scan; i++)
1679   {
1680     if ((spectral_start == 0) && (huff_num[comp_dc_tab[comp_list[i]]] == NULL))
1681       terminate(JPGD_UNDEFINED_HUFF_TABLE);
1682 
1683     if ((spectral_end > 0) && (huff_num[comp_ac_tab[comp_list[i]]] == NULL))
1684       terminate(JPGD_UNDEFINED_HUFF_TABLE);
1685   }
1686 
1687   for (i = 0; i < JPGD_MAXHUFFTABLES; i++)
1688     if (huff_num[i])
1689     {
1690       if (!h[i])
1691         h[i] = (Phuff_tables_t)alloc(sizeof(huff_tables_t));
1692 
1693       make_huff_table(i, h[i]);
1694     }
1695 
1696   for (i = 0; i < blocks_per_mcu; i++)
1697   {
1698     dc_huff_seg[i] = h[comp_dc_tab[mcu_org[i]]];
1699     ac_huff_seg[i] = h[comp_ac_tab[mcu_org[i]]];
1700     component[i]   = &last_dc_val[mcu_org[i]];
1701   }
1702 }
1703 //------------------------------------------------------------------------------
1704 // Determines the component order inside each MCU.
1705 // Also calcs how many MCU's are on each row, etc.
calc_mcu_block_order(void)1706 void jpeg_decoder::calc_mcu_block_order(void)
1707 {
1708   int component_num, component_id;
1709   int max_h_samp = 0, max_v_samp = 0;
1710 
1711   for (component_id = 0; component_id < comps_in_frame; component_id++)
1712   {
1713     if (comp_h_samp[component_id] > max_h_samp)
1714       max_h_samp = comp_h_samp[component_id];
1715 
1716     if (comp_v_samp[component_id] > max_v_samp)
1717       max_v_samp = comp_v_samp[component_id];
1718   }
1719 
1720   for (component_id = 0; component_id < comps_in_frame; component_id++)
1721   {
1722     comp_h_blocks[component_id] = ((((image_x_size * comp_h_samp[component_id]) + (max_h_samp - 1)) / max_h_samp) + 7) / 8;
1723     comp_v_blocks[component_id] = ((((image_y_size * comp_v_samp[component_id]) + (max_v_samp - 1)) / max_v_samp) + 7) / 8;
1724   }
1725 
1726   if (comps_in_scan == 1)
1727   {
1728     mcus_per_row = comp_h_blocks[comp_list[0]];
1729     mcus_per_col = comp_v_blocks[comp_list[0]];
1730   }
1731   else
1732   {
1733     mcus_per_row = (((image_x_size + 7) / 8) + (max_h_samp - 1)) / max_h_samp;
1734     mcus_per_col = (((image_y_size + 7) / 8) + (max_v_samp - 1)) / max_v_samp;
1735   }
1736 
1737   if (comps_in_scan == 1)
1738   {
1739     mcu_org[0] = comp_list[0];
1740 
1741     blocks_per_mcu = 1;
1742   }
1743   else
1744   {
1745     blocks_per_mcu = 0;
1746 
1747     for (component_num = 0; component_num < comps_in_scan; component_num++)
1748     {
1749       int num_blocks;
1750 
1751       component_id = comp_list[component_num];
1752 
1753       num_blocks = comp_h_samp[component_id] * comp_v_samp[component_id];
1754 
1755       while (num_blocks--)
1756         mcu_org[blocks_per_mcu++] = component_id;
1757     }
1758   }
1759 }
1760 //------------------------------------------------------------------------------
1761 // Starts a new scan.
init_scan(void)1762 int jpeg_decoder::init_scan(void)
1763 {
1764   if (!locate_sos_marker())
1765     return FALSE;
1766 
1767   calc_mcu_block_order();
1768 
1769   check_huff_tables();
1770 
1771   check_quant_tables();
1772 
1773   memset(last_dc_val, 0, comps_in_frame * sizeof(uint));
1774 
1775   eob_run = 0;
1776 
1777   if (restart_interval)
1778   {
1779     restarts_left = restart_interval;
1780     next_restart_num = 0;
1781   }
1782 
1783   if ((!progressive_flag) && (use_mmx))
1784     use_mmx_getbits = true;
1785 
1786   fix_in_buffer();
1787 
1788   return TRUE;
1789 }
1790 //------------------------------------------------------------------------------
1791 // Starts a frame. Determines if the number of components or sampling factors
1792 // are supported.
init_frame(void)1793 void jpeg_decoder::init_frame(void)
1794 {
1795   int i;
1796   uchar *q;
1797 
1798   if (comps_in_frame == 1)
1799   {
1800     scan_type          = JPGD_GRAYSCALE;
1801 
1802     max_blocks_per_mcu = 1;
1803 
1804     max_mcu_x_size     = 8;
1805     max_mcu_y_size     = 8;
1806   }
1807   else if (comps_in_frame == 3)
1808   {
1809     if ( ((comp_h_samp[1] != 1) || (comp_v_samp[1] != 1)) ||
1810          ((comp_h_samp[2] != 1) || (comp_v_samp[2] != 1)) )
1811       terminate(JPGD_UNSUPPORTED_SAMP_FACTORS);
1812 
1813     if ((comp_h_samp[0] == 1) && (comp_v_samp[0] == 1))
1814     {
1815       scan_type          = JPGD_YH1V1;
1816 
1817       max_blocks_per_mcu = 3;
1818 
1819       max_mcu_x_size     = 8;
1820       max_mcu_y_size     = 8;
1821     }
1822     else if ((comp_h_samp[0] == 2) && (comp_v_samp[0] == 1))
1823     {
1824       scan_type          = JPGD_YH2V1;
1825 
1826       max_blocks_per_mcu = 4;
1827 
1828       max_mcu_x_size     = 16;
1829       max_mcu_y_size     = 8;
1830     }
1831     else if ((comp_h_samp[0] == 1) && (comp_v_samp[0] == 2))
1832     {
1833       scan_type          = JPGD_YH1V2;
1834 
1835       max_blocks_per_mcu = 4;
1836 
1837       max_mcu_x_size     = 8;
1838       max_mcu_y_size     = 16;
1839     }
1840     else if ((comp_h_samp[0] == 2) && (comp_v_samp[0] == 2))
1841     {
1842       scan_type          = JPGD_YH2V2;
1843 
1844       max_blocks_per_mcu = 6;
1845 
1846       max_mcu_x_size     = 16;
1847       max_mcu_y_size     = 16;
1848     }
1849     else
1850       terminate(JPGD_UNSUPPORTED_SAMP_FACTORS);
1851   }
1852   else
1853     terminate(JPGD_UNSUPPORTED_COLORSPACE);
1854 
1855   max_mcus_per_row = (image_x_size + (max_mcu_x_size - 1)) / max_mcu_x_size;
1856   max_mcus_per_col = (image_y_size + (max_mcu_y_size - 1)) / max_mcu_y_size;
1857 
1858   /* these values are for the *destination* pixels: after conversion */
1859 
1860   if (scan_type == JPGD_GRAYSCALE)
1861     dest_bytes_per_pixel = 1;
1862   else
1863     dest_bytes_per_pixel = 4;
1864 
1865   dest_bytes_per_scan_line = ((image_x_size + 15) & 0xFFF0) * dest_bytes_per_pixel;
1866 
1867   real_dest_bytes_per_scan_line = (image_x_size * dest_bytes_per_pixel);
1868 
1869   // Initialize two scan line buffers.
1870   // FIXME: Only the V2 sampling factors need two buffers.
1871   scan_line_0         = (uchar *)alloc(dest_bytes_per_scan_line + 8);
1872   memset(scan_line_0, 0, dest_bytes_per_scan_line);
1873 
1874   scan_line_1         = (uchar *)alloc(dest_bytes_per_scan_line + 8);
1875   memset(scan_line_1, 0, dest_bytes_per_scan_line);
1876 
1877   max_blocks_per_row = max_mcus_per_row * max_blocks_per_mcu;
1878 
1879   // Should never happen
1880   if (max_blocks_per_row > JPGD_MAXBLOCKSPERROW)
1881     terminate(JPGD_ASSERTION_ERROR);
1882 
1883   // Allocate the coefficient buffer, enough for one row's worth of MCU's
1884   q = (uchar *)alloc(max_blocks_per_row * 64 * sizeof(BLOCK_TYPE) + 8);
1885 
1886   // Align to 8-byte boundry, for MMX code
1887   q = (uchar *)(((ulong)q + 7) & ~7);
1888 
1889   // The block_seg[] array's name dates back to the
1890   // 16-bit assembler implementation. "seg" stood for "segment".
1891   for (i = 0; i < max_blocks_per_row; i++)
1892     block_seg[i] = (BLOCK_TYPE *)(q + i * 64 * sizeof(BLOCK_TYPE));
1893 
1894   for (i = 0; i < max_blocks_per_row; i++)
1895     block_max_zag_set[i] = 64;
1896 
1897   Psample_buf = (uchar *)(((ulong)alloc(max_blocks_per_row * 64 + 8) + 7) & ~7);
1898 
1899   total_lines_left = image_y_size;
1900 
1901   mcu_lines_left = 0;
1902 
1903   create_look_ups();
1904 }
1905 //------------------------------------------------------------------------------
1906 // The following methods decode the various types of blocks encountered
1907 // in progressively encoded images.
decode_block_dc_first(jpeg_decoder * Pd,int component_id,int block_x,int block_y)1908 void progressive_block_decoder::decode_block_dc_first(
1909   jpeg_decoder *Pd,
1910   int component_id, int block_x, int block_y)
1911 {
1912   int s, r;
1913   BLOCK_TYPE *p = Pd->coeff_buf_getp(Pd->dc_coeffs[component_id], block_x, block_y);
1914 
1915   if ((s = Pd->huff_decode(Pd->h[Pd->comp_dc_tab[component_id]])) != 0)
1916   {
1917     r = Pd->get_bits_2(s);
1918     s = HUFF_EXTEND_P(r, s);
1919   }
1920 
1921   Pd->last_dc_val[component_id] = (s += Pd->last_dc_val[component_id]);
1922 
1923   p[0] = s << Pd->successive_low;
1924 }
1925 //------------------------------------------------------------------------------
decode_block_dc_refine(jpeg_decoder * Pd,int component_id,int block_x,int block_y)1926 void progressive_block_decoder::decode_block_dc_refine(
1927   jpeg_decoder *Pd,
1928   int component_id, int block_x, int block_y)
1929 {
1930   if (Pd->get_bits_2(1))
1931   {
1932     BLOCK_TYPE *p = Pd->coeff_buf_getp(Pd->dc_coeffs[component_id], block_x, block_y);
1933 
1934     p[0] |= (1 << Pd->successive_low);
1935   }
1936 }
1937 //------------------------------------------------------------------------------
decode_block_ac_first(jpeg_decoder * Pd,int component_id,int block_x,int block_y)1938 void progressive_block_decoder::decode_block_ac_first(
1939   jpeg_decoder *Pd,
1940   int component_id, int block_x, int block_y)
1941 {
1942   int k, s, r;
1943 
1944   if (Pd->eob_run)
1945   {
1946     Pd->eob_run--;
1947     return;
1948   }
1949 
1950   BLOCK_TYPE *p = Pd->coeff_buf_getp(Pd->ac_coeffs[component_id], block_x, block_y);
1951 
1952   for (k = Pd->spectral_start; k <= Pd->spectral_end; k++)
1953   {
1954     s = Pd->huff_decode(Pd->h[Pd->comp_ac_tab[component_id]]);
1955 
1956     r = s >> 4;
1957     s &= 15;
1958 
1959     if (s)
1960     {
1961       if ((k += r) > 63)
1962         Pd->terminate(JPGD_DECODE_ERROR);
1963 
1964       r = Pd->get_bits_2(s);
1965       s = HUFF_EXTEND_P(r, s);
1966 
1967       p[ZAG[k]] = s << Pd->successive_low;
1968     }
1969     else
1970     {
1971       if (r == 15)
1972       {
1973         if ((k += 15) > 63)
1974           Pd->terminate(JPGD_DECODE_ERROR);
1975       }
1976       else
1977       {
1978         Pd->eob_run = 1 << r;
1979 
1980         if (r)
1981           Pd->eob_run += Pd->get_bits_2(r);
1982 
1983         Pd->eob_run--;
1984 
1985         break;
1986       }
1987     }
1988   }
1989 }
1990 //------------------------------------------------------------------------------
decode_block_ac_refine(jpeg_decoder * Pd,int component_id,int block_x,int block_y)1991 void progressive_block_decoder::decode_block_ac_refine(
1992   jpeg_decoder *Pd,
1993   int component_id, int block_x, int block_y)
1994 {
1995   int s, k, r;
1996   int p1 = 1 << Pd->successive_low;
1997   int m1 = (-1) << Pd->successive_low;
1998   BLOCK_TYPE *p = Pd->coeff_buf_getp(Pd->ac_coeffs[component_id], block_x, block_y);
1999 
2000   k = Pd->spectral_start;
2001 
2002   if (Pd->eob_run == 0)
2003   {
2004     for ( ; k <= Pd->spectral_end; k++)
2005     {
2006       s = Pd->huff_decode(Pd->h[Pd->comp_ac_tab[component_id]]);
2007 
2008       r = s >> 4;
2009       s &= 15;
2010 
2011       if (s)
2012       {
2013         if (s != 1)
2014           Pd->terminate(JPGD_DECODE_ERROR);
2015 
2016         if (Pd->get_bits_2(1))
2017           s = p1;
2018         else
2019           s = m1;
2020       }
2021       else
2022       {
2023         if (r != 15)
2024         {
2025           Pd->eob_run = 1 << r;
2026 
2027           if (r)
2028             Pd->eob_run += Pd->get_bits_2(r);
2029 
2030           break;
2031         }
2032       }
2033 
2034       do
2035       {
2036         BLOCK_TYPE *this_coef = p + ZAG[k];
2037 
2038         if (*this_coef != 0)
2039         {
2040           if (Pd->get_bits_2(1))
2041           {
2042             if ((*this_coef & p1) == 0)
2043             {
2044               if (*this_coef >= 0)
2045                 *this_coef += p1;
2046               else
2047                 *this_coef += m1;
2048             }
2049           }
2050         }
2051         else
2052         {
2053           if (--r < 0)
2054             break;
2055         }
2056 
2057         k++;
2058 
2059       } while (k <= Pd->spectral_end);
2060 
2061       if ((s) && (k < 64))
2062       {
2063         p[ZAG[k]] = s;
2064       }
2065     }
2066   }
2067 
2068   if (Pd->eob_run > 0)
2069   {
2070     for ( ; k <= Pd->spectral_end; k++)
2071     {
2072       BLOCK_TYPE *this_coef = p + ZAG[k];
2073 
2074       if (*this_coef != 0)
2075       {
2076         if (Pd->get_bits_2(1))
2077         {
2078           if ((*this_coef & p1) == 0)
2079           {
2080             if (*this_coef >= 0)
2081               *this_coef += p1;
2082             else
2083               *this_coef += m1;
2084           }
2085         }
2086       }
2087     }
2088 
2089     Pd->eob_run--;
2090   }
2091 }
2092 //------------------------------------------------------------------------------
2093 // Decode a scan in a progressively encoded image.
decode_scan(Pdecode_block_func decode_block_func)2094 void jpeg_decoder::decode_scan(
2095   Pdecode_block_func decode_block_func)
2096 {
2097   int mcu_row, mcu_col, mcu_block;
2098   int block_x_mcu[JPGD_MAXCOMPONENTS], block_y_mcu[JPGD_MAXCOMPONENTS];
2099 
2100   memset(block_y_mcu, 0, sizeof(block_y_mcu));
2101 
2102   for (mcu_col = 0; mcu_col < mcus_per_col; mcu_col++)
2103   {
2104     int component_num, component_id;
2105 
2106     memset(block_x_mcu, 0, sizeof(block_x_mcu));
2107 
2108     for (mcu_row = 0; mcu_row < mcus_per_row; mcu_row++)
2109     {
2110       int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
2111 
2112       if ((restart_interval) && (restarts_left == 0))
2113         process_restart();
2114 
2115       for (mcu_block = 0; mcu_block < blocks_per_mcu; mcu_block++)
2116       {
2117         component_id = mcu_org[mcu_block];
2118 
2119         decode_block_func(this, component_id,
2120                           block_x_mcu[component_id] + block_x_mcu_ofs,
2121                           block_y_mcu[component_id] + block_y_mcu_ofs);
2122 
2123         if (comps_in_scan == 1)
2124           block_x_mcu[component_id]++;
2125         else
2126         {
2127           if (++block_x_mcu_ofs == comp_h_samp[component_id])
2128           {
2129             block_x_mcu_ofs = 0;
2130 
2131             if (++block_y_mcu_ofs == comp_v_samp[component_id])
2132             {
2133               block_y_mcu_ofs = 0;
2134 
2135               block_x_mcu[component_id] += comp_h_samp[component_id];
2136             }
2137           }
2138         }
2139       }
2140 
2141       restarts_left--;
2142     }
2143 
2144     if (comps_in_scan == 1)
2145       block_y_mcu[comp_list[0]]++;
2146     else
2147     {
2148       for (component_num = 0; component_num < comps_in_scan; component_num++)
2149       {
2150         component_id = comp_list[component_num];
2151 
2152         block_y_mcu[component_id] += comp_v_samp[component_id];
2153       }
2154     }
2155   }
2156 }
2157 //------------------------------------------------------------------------------
2158 // Decode a progressively encoded image.
init_progressive(void)2159 void jpeg_decoder::init_progressive(void)
2160 {
2161   int i;
2162 
2163   if (comps_in_frame == 4)
2164     terminate(JPGD_UNSUPPORTED_COLORSPACE);
2165 
2166   // Allocate the coefficient buffers.
2167   for (i = 0; i < comps_in_frame; i++)
2168   {
2169     dc_coeffs[i] = coeff_buf_open(max_mcus_per_row * comp_h_samp[i],
2170                                   max_mcus_per_col * comp_v_samp[i], 1, 1);
2171     ac_coeffs[i] = coeff_buf_open(max_mcus_per_row * comp_h_samp[i],
2172                                   max_mcus_per_col * comp_v_samp[i], 8, 8);
2173   }
2174 
2175   for ( ; ; )
2176   {
2177     int dc_only_scan, refinement_scan;
2178     Pdecode_block_func decode_block_func;
2179 
2180     if (!init_scan())
2181       break;
2182 
2183     dc_only_scan    = (spectral_start == 0);
2184     refinement_scan = (successive_high != 0);
2185 
2186     if ((spectral_start > spectral_end) || (spectral_end > 63))
2187       terminate(JPGD_BAD_SOS_SPECTRAL);
2188 
2189     if (dc_only_scan)
2190     {
2191       if (spectral_end)
2192         terminate(JPGD_BAD_SOS_SPECTRAL);
2193     }
2194     else if (comps_in_scan != 1)  /* AC scans can only contain one component */
2195       terminate(JPGD_BAD_SOS_SPECTRAL);
2196 
2197     if ((refinement_scan) && (successive_low != successive_high - 1))
2198       terminate(JPGD_BAD_SOS_SUCCESSIVE);
2199 
2200     if (dc_only_scan)
2201     {
2202       if (refinement_scan)
2203         decode_block_func = progressive_block_decoder::decode_block_dc_refine;
2204       else
2205         decode_block_func = progressive_block_decoder::decode_block_dc_first;
2206     }
2207     else
2208     {
2209       if (refinement_scan)
2210         decode_block_func = progressive_block_decoder::decode_block_ac_refine;
2211       else
2212         decode_block_func = progressive_block_decoder::decode_block_ac_first;
2213     }
2214 
2215     decode_scan(decode_block_func);
2216 
2217     //get_bits_2(bits_left & 7);
2218 
2219     bits_left = 16;
2220     //bit_buf = 0;
2221     get_bits_1(16);
2222     get_bits_1(16);
2223   }
2224 
2225   comps_in_scan = comps_in_frame;
2226 
2227   for (i = 0; i < comps_in_frame; i++)
2228     comp_list[i] = i;
2229 
2230   calc_mcu_block_order();
2231 }
2232 //------------------------------------------------------------------------------
init_sequential(void)2233 void jpeg_decoder::init_sequential(void)
2234 {
2235   if (!init_scan())
2236     terminate(JPGD_UNEXPECTED_MARKER);
2237 }
2238 //------------------------------------------------------------------------------
decode_start(void)2239 void jpeg_decoder::decode_start(void)
2240 {
2241   init_frame();
2242 
2243   if (progressive_flag)
2244     init_progressive();
2245   else
2246     init_sequential();
2247 }
2248 //------------------------------------------------------------------------------
2249 // Find the start of the JPEG file and reads enough data to determine
2250 // its size, number of components, etc.
decode_init(Pjpeg_decoder_stream Pstream,bool use_mmx)2251 void jpeg_decoder::decode_init(Pjpeg_decoder_stream Pstream, bool use_mmx)
2252 {
2253   init(Pstream, use_mmx);
2254 
2255   locate_sof_marker();
2256 }
2257 //------------------------------------------------------------------------------
2258 // Call get_error_code() after constructing to determine if the stream
2259 // was valid or not. You may call the get_width(), get_height(), etc.
2260 // methods after the constructor is called.
2261 // You may then either destruct the object, or begin decoding the image
2262 // by calling begin(), then decode().
jpeg_decoder(Pjpeg_decoder_stream Pstream,bool use_mmx)2263 jpeg_decoder::jpeg_decoder(Pjpeg_decoder_stream Pstream, bool use_mmx)
2264 {
2265   if (setjmp(jmp_state))
2266     return;
2267 
2268   decode_init(Pstream, use_mmx);
2269 }
2270 //------------------------------------------------------------------------------
2271 // If you wish to decompress the image, call this method after constructing
2272 // the object. If JPGD_OKAY is returned you may then call decode() to
2273 // fetch the scan lines.
begin(void)2274 int jpeg_decoder::begin(void)
2275 {
2276   if (ready_flag)
2277     return (JPGD_OKAY);
2278 
2279   if (error_code)
2280     return (JPGD_FAILED);
2281 
2282   if (setjmp(jmp_state))
2283     return (JPGD_FAILED);
2284 
2285   decode_start();
2286 
2287   ready_flag = true;
2288 
2289   return (JPGD_OKAY);
2290 }
2291 //------------------------------------------------------------------------------
2292 // Completely destroys the decoder object. May be called at any time.
~jpeg_decoder()2293 jpeg_decoder::~jpeg_decoder()
2294 {
2295   free_all_blocks();
2296 }
2297 //------------------------------------------------------------------------------
2298 
2299