File: jpeg.c

package info (click to toggle)
liboil 0.3.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,276 kB
  • ctags: 2,005
  • sloc: ansic: 14,199; sh: 8,527; xml: 7,375; makefile: 429
file content (971 lines) | stat: -rw-r--r-- 25,345 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <string.h>
#include <ctype.h>
#include <liboil/liboil.h>

#include "jpeg_internal.h"



#define JPEG_MARKER_STUFFED		0x00
#define JPEG_MARKER_TEM			0x01
#define JPEG_MARKER_RES			0x02

#define JPEG_MARKER_SOF_0		0xc0
#define JPEG_MARKER_SOF_1		0xc1
#define JPEG_MARKER_SOF_2		0xc2
#define JPEG_MARKER_SOF_3		0xc3
#define JPEG_MARKER_DHT			0xc4
#define JPEG_MARKER_SOF_5		0xc5
#define JPEG_MARKER_SOF_6		0xc6
#define JPEG_MARKER_SOF_7		0xc7
#define JPEG_MARKER_JPG			0xc8
#define JPEG_MARKER_SOF_9		0xc9
#define JPEG_MARKER_SOF_10		0xca
#define JPEG_MARKER_SOF_11		0xcb
#define JPEG_MARKER_DAC			0xcc
#define JPEG_MARKER_SOF_13		0xcd
#define JPEG_MARKER_SOF_14		0xce
#define JPEG_MARKER_SOF_15		0xcf

#define JPEG_MARKER_RST_0		0xd0
#define JPEG_MARKER_RST_1		0xd1
#define JPEG_MARKER_RST_2		0xd2
#define JPEG_MARKER_RST_3		0xd3
#define JPEG_MARKER_RST_4		0xd4
#define JPEG_MARKER_RST_5		0xd5
#define JPEG_MARKER_RST_6		0xd6
#define JPEG_MARKER_RST_7		0xd7
#define JPEG_MARKER_SOI			0xd8
#define JPEG_MARKER_EOI			0xd9
#define JPEG_MARKER_SOS			0xda
#define JPEG_MARKER_DQT			0xdb
#define JPEG_MARKER_DNL			0xdc
#define JPEG_MARKER_DRI			0xdd
#define JPEG_MARKER_DHP			0xde
#define JPEG_MARKER_EXP			0xdf
#define JPEG_MARKER_APP(x)		(0xe0 + (x))
#define JPEG_MARKER_JPG_(x)		(0xf0 + (x))
#define JPEG_MARKER_COM			0xfe

#define JPEG_MARKER_JFIF		JPEG_MARKER_APP(0)

struct jpeg_marker_struct
{
  int tag;
  int (*func) (JpegDecoder * dec, bits_t * bits);
  char *name;
  unsigned int flags;
};
static struct jpeg_marker_struct jpeg_markers[] = {
  {0xc0, jpeg_decoder_sof_baseline_dct,
      "baseline DCT"},
  {0xc4, jpeg_decoder_define_huffman_table,
      "define Huffman table(s)"},
  {0xd8, jpeg_decoder_soi,
      "start of image"},
  {0xd9, jpeg_decoder_eoi,
      "end of image"},
  {0xda, jpeg_decoder_sos,
      "start of scan", JPEG_ENTROPY_SEGMENT},
  {0xdb, jpeg_decoder_define_quant_table,
      "define quantization table"},
  {0xe0, jpeg_decoder_application0,
      "application segment 0"},

  {0x00, NULL, "illegal"},
  {0x01, NULL, "TEM"},
  {0x02, NULL, "RES"},

  {0xc1, NULL, "extended sequential DCT"},
  {0xc2, NULL, "progressive DCT"},
  {0xc3, NULL, "lossless (sequential)"},
  {0xc5, NULL, "differential sequential DCT"},
  {0xc6, NULL, "differential progressive DCT"},
  {0xc7, NULL, "differential lossless (sequential)"},
  {0xc8, NULL, "reserved"},
  {0xc9, NULL, "extended sequential DCT"},
  {0xca, NULL, "progressive DCT"},
  {0xcb, NULL, "lossless (sequential)"},
  {0xcc, NULL, "define arithmetic coding conditioning(s)"},
  {0xcd, NULL, "differential sequential DCT"},
  {0xce, NULL, "differential progressive DCT"},
  {0xcf, NULL, "differential lossless (sequential)"},

  {0xd0, jpeg_decoder_restart, "restart0", JPEG_ENTROPY_SEGMENT},
  {0xd1, jpeg_decoder_restart, "restart1", JPEG_ENTROPY_SEGMENT},
  {0xd2, jpeg_decoder_restart, "restart2", JPEG_ENTROPY_SEGMENT},
  {0xd3, jpeg_decoder_restart, "restart3", JPEG_ENTROPY_SEGMENT},
  {0xd4, jpeg_decoder_restart, "restart4", JPEG_ENTROPY_SEGMENT},
  {0xd5, jpeg_decoder_restart, "restart5", JPEG_ENTROPY_SEGMENT},
  {0xd6, jpeg_decoder_restart, "restart6", JPEG_ENTROPY_SEGMENT},
  {0xd7, jpeg_decoder_restart, "restart7", JPEG_ENTROPY_SEGMENT},

  {0xdc, NULL, "define number of lines"},
  {0xdd, jpeg_decoder_restart_interval, "define restart interval"},
  {0xde, NULL, "define hierarchical progression"},
  {0xdf, NULL, "expand reference component(s)"},

  {0xe1, jpeg_decoder_application_misc, "application segment 1"},
  {0xe2, jpeg_decoder_application_misc, "application segment 2"},
  {0xe3, jpeg_decoder_application_misc, "application segment 3"},
  {0xe4, jpeg_decoder_application_misc, "application segment 4"},
  {0xe5, jpeg_decoder_application_misc, "application segment 5"},
  {0xe6, jpeg_decoder_application_misc, "application segment 6"},
  {0xe7, jpeg_decoder_application_misc, "application segment 7"},
  {0xe8, jpeg_decoder_application_misc, "application segment 8"},
  {0xe9, jpeg_decoder_application_misc, "application segment 9"},
  {0xea, jpeg_decoder_application_misc, "application segment a"},
  {0xeb, jpeg_decoder_application_misc, "application segment b"},
  {0xec, jpeg_decoder_application_misc, "application segment c"},
  {0xed, jpeg_decoder_application_misc, "application segment d"},
  {0xee, jpeg_decoder_application_misc, "application segment e"},
  {0xef, jpeg_decoder_application_misc, "application segment f"},

  {0xf0, NULL, "JPEG extension 0"},
  {0xfe, jpeg_decoder_comment, "comment"},

  {0x00, NULL, "illegal"},
};
static const int n_jpeg_markers =
    sizeof (jpeg_markers) / sizeof (jpeg_markers[0]);

static unsigned char std_tables[] = {
  0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,

  0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
  0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31,
  0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32,
  0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52,
  0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
  0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
  0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57,
  0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83,
  0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,
  0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
  0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
  0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  0xf9, 0xfa,

  0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  0x0a, 0x0b,

  0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
  0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
  0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06,
  0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81,
  0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33,
  0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28,
  0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44,
  0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56,
  0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
  0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92,
  0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3,
  0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
  0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  0xf9, 0xfa,
};


/* misc helper function declarations */

static void dumpbits (bits_t * bits);
static char *sprintbits (char *str, unsigned int bits, int n);

static void huffman_table_load_std_jpeg (JpegDecoder * dec);


int
jpeg_decoder_soi (JpegDecoder * dec, bits_t * bits)
{
  return 0;
}

int
jpeg_decoder_eoi (JpegDecoder * dec, bits_t * bits)
{
  return 0;
}

int
jpeg_decoder_sof_baseline_dct (JpegDecoder * dec, bits_t * bits)
{
  int i;
  int length;
  int image_size;
  int rowstride;
  int max_h_oversample = 0, max_v_oversample = 0;

  JPEG_DEBUG (0, "start of frame (baseline DCT)\n");

  length = get_be_u16 (bits);
  bits->end = bits->ptr + length - 2;

  dec->depth = get_u8 (bits);
  dec->height = get_be_u16 (bits);
  dec->width = get_be_u16 (bits);
  dec->n_components = get_u8 (bits);

  JPEG_DEBUG (0,
      "frame_length=%d depth=%d height=%d width=%d n_components=%d\n", length,
      dec->depth, dec->height, dec->width, dec->n_components);

  for (i = 0; i < dec->n_components; i++) {
    dec->components[i].id = get_u8 (bits);
    dec->components[i].h_oversample = getbits (bits, 4);
    dec->components[i].v_oversample = getbits (bits, 4);
    dec->components[i].quant_table = get_u8 (bits);

    JPEG_DEBUG (0,
        "[%d] id=%d h_oversample=%d v_oversample=%d quant_table=%d\n", i,
        dec->components[i].id, dec->components[i].h_oversample,
        dec->components[i].v_oversample, dec->components[i].quant_table);

    max_h_oversample = MAX (max_h_oversample, dec->components[i].h_oversample);
    max_v_oversample = MAX (max_v_oversample, dec->components[i].v_oversample);
  }
  dec->width_blocks =
      (dec->width + 8 * max_h_oversample - 1) / (8 * max_h_oversample);
  dec->height_blocks =
      (dec->height + 8 * max_v_oversample - 1) / (8 * max_v_oversample);
  for (i = 0; i < dec->n_components; i++) {
    dec->components[i].h_subsample = max_h_oversample /
        dec->components[i].h_oversample;
    dec->components[i].v_subsample = max_v_oversample /
        dec->components[i].v_oversample;

    rowstride = dec->width_blocks * 8 * max_h_oversample /
        dec->components[i].h_subsample;
    image_size = rowstride *
        (dec->height_blocks * 8 * max_v_oversample /
        dec->components[i].v_subsample);
    dec->components[i].rowstride = rowstride;
    dec->components[i].image = g_malloc (image_size);
  }

  if (bits->end != bits->ptr)
    JPEG_DEBUG (0, "endptr != bits\n");

  return length;
}

int
jpeg_decoder_define_quant_table (JpegDecoder * dec, bits_t * bits)
{
  int length;
  int pq;
  int tq;
  int i;
  short *q;

  JPEG_DEBUG (0, "define quantization table\n");

  length = get_be_u16 (bits);
  bits->end = bits->ptr + length - 2;

  while (bits->ptr < bits->end) {
    pq = getbits (bits, 4);
    tq = getbits (bits, 4);

    q = dec->quant_table[tq];
    if (pq) {
      for (i = 0; i < 64; i++) {
        q[i] = get_be_u16 (bits);
      }
    } else {
      for (i = 0; i < 64; i++) {
        q[i] = get_u8 (bits);
      }
    }

    JPEG_DEBUG (0, "quant table index %d:\n", tq);
    for (i = 0; i < 8; i++) {
      JPEG_DEBUG (0, "%3d %3d %3d %3d %3d %3d %3d %3d\n",
          q[0], q[1], q[2], q[3], q[4], q[5], q[6], q[7]);
      q += 8;
    }
  }

  return length;
}

void
generate_code_table (int *huffsize)
{
  int code;
  int i;
  int j;
  int k;
  char str[33];

  //int l;

  code = 0;
  k = 0;
  for (i = 0; i < 16; i++) {
    for (j = 0; j < huffsize[i]; j++) {
      JPEG_DEBUG (0, "huffcode[%d] = %s\n", k,
          sprintbits (str, code >> (15 - i), i + 1));
      code++;
      k++;
    }
    code <<= 1;
  }

}

HuffmanTable *
huffman_table_new_jpeg (bits_t * bits)
{
  int n_symbols;
  int huffsize[16];
  int i, j, k;
  HuffmanTable *table;
  unsigned int symbol;

  table = huffman_table_new ();

  /* huffsize[i] is the number of symbols that have length
   * (i+1) bits.  Maximum bit length is 16 bits, so there are
   * 16 entries. */
  n_symbols = 0;
  for (i = 0; i < 16; i++) {
    huffsize[i] = get_u8 (bits);
    n_symbols += huffsize[i];
  }

  /* Build up the symbol table.  The first symbol is all 0's, with
   * the number of bits determined by the first non-zero entry in
   * huffsize[].  Subsequent symbols with the same bit length are
   * incremented by 1.  Increasing the bit length shifts the
   * symbol 1 bit to the left. */
  symbol = 0;
  k = 0;
  for (i = 0; i < 16; i++) {
    for (j = 0; j < huffsize[i]; j++) {
      huffman_table_add (table, symbol, i + 1, get_u8 (bits));
      symbol++;
      k++;
    }
    /* This checks that our symbol is actually less than the
     * number of bits we think it is.  This is only triggered
     * for bad huffsize[] arrays. */
    if (symbol >= (1 << (i + 1))) {
      JPEG_DEBUG (0, "bad huffsize[] array\n");
      return NULL;
    }

    symbol <<= 1;
  }

  huffman_table_dump (table);

  return table;
}

int
jpeg_decoder_define_huffman_table (JpegDecoder * dec, bits_t * bits)
{
  int length;
  int tc;
  int th;
  HuffmanTable *hufftab;

  JPEG_DEBUG (0, "define huffman table\n");

  length = get_be_u16 (bits);
  bits->end = bits->ptr + length - 2;

  while (bits->ptr < bits->end) {
    tc = getbits (bits, 4);
    th = getbits (bits, 4);

    JPEG_DEBUG (0, "huff table index %d:\n", th);
    JPEG_DEBUG (0, "type %d (%s)\n", tc, tc ? "ac" : "dc");

    hufftab = huffman_table_new_jpeg (bits);
    if (tc) {
      if (dec->ac_huff_table[th]) {
        huffman_table_free (dec->ac_huff_table[th]);
      }
      dec->ac_huff_table[th] = hufftab;
    } else {
      if (dec->dc_huff_table[th]) {
        huffman_table_free (dec->dc_huff_table[th]);
      }
      dec->dc_huff_table[th] = hufftab;
    }
  }

  return length;
}

static void
dumpbits (bits_t * bits)
{
  int i;
  int j;
  unsigned char *p;
  char s[40];

  p = bits->ptr;
  for (i = 0; i < 8; i++) {
    sprintf (s, "%02x %02x %02x %02x %02x %02x %02x %02x ........",
        p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
    for (j = 0; j < 8; j++) {
      s[j + 24] = (isprint (p[j])) ? p[j] : '.';
    }
    JPEG_DEBUG (0, "%s\n", s);
    p += 8;
  }

}

int
jpeg_decoder_find_component_by_id (JpegDecoder * dec, int id)
{
  int i;

  for (i = 0; i < dec->n_components; i++) {
    if (dec->components[i].id == id)
      return i;
  }
  JPEG_DEBUG (0, "undefined component id %d\n", id);
  return 0;
}

int
jpeg_decoder_sos (JpegDecoder * dec, bits_t * bits)
{
  int length;
  int i;

  int n_components;
  int spectral_start;
  int spectral_end;
  int approx_high;
  int approx_low;
  int n;

  JPEG_DEBUG (0, "start of scan\n");

  length = get_be_u16 (bits);
  bits->end = bits->ptr + length - 2;
  JPEG_DEBUG (0, "length=%d\n", length);

  n_components = get_u8 (bits);
  n = 0;
  dec->scan_h_subsample = 0;
  dec->scan_v_subsample = 0;
  for (i = 0; i < n_components; i++) {
    int component_id;
    int dc_table;
    int ac_table;
    int x;
    int y;
    int index;
    int h_subsample;
    int v_subsample;
    int quant_index;

    component_id = get_u8 (bits);
    dc_table = getbits (bits, 4);
    ac_table = getbits (bits, 4);
    index = jpeg_decoder_find_component_by_id (dec, component_id);

    h_subsample = dec->components[index].h_oversample;
    v_subsample = dec->components[index].v_oversample;
    quant_index = dec->components[index].quant_table;

    for (y = 0; y < v_subsample; y++) {
      for (x = 0; x < h_subsample; x++) {
        dec->scan_list[n].component_index = index;
        dec->scan_list[n].dc_table = dc_table;
        dec->scan_list[n].ac_table = ac_table;
        dec->scan_list[n].quant_table = quant_index;
        dec->scan_list[n].x = x;
        dec->scan_list[n].y = y;
        dec->scan_list[n].offset =
            y * 8 * dec->components[index].rowstride + x * 8;
        n++;
      }
    }

    dec->scan_h_subsample = MAX (dec->scan_h_subsample, h_subsample);
    dec->scan_v_subsample = MAX (dec->scan_v_subsample, v_subsample);

    syncbits (bits);

    JPEG_DEBUG (0, "component %d: index=%d dc_table=%d ac_table=%d n=%d\n",
        component_id, index, dc_table, ac_table, n);
  }
  dec->scan_list_length = n;

  spectral_start = get_u8 (bits);
  spectral_end = get_u8 (bits);
  JPEG_DEBUG (0, "spectral range [%d,%d]\n", spectral_start, spectral_end);
  approx_high = getbits (bits, 4);
  approx_low = getbits (bits, 4);
  JPEG_DEBUG (0, "approx range [%d,%d]\n", approx_low, approx_high);
  syncbits (bits);

  dec->x = 0;
  dec->y = 0;
  dec->dc[0] = dec->dc[1] = dec->dc[2] = dec->dc[3] = 128 * 8;

  if (bits->end != bits->ptr)
    JPEG_DEBUG (0, "endptr != bits\n");

  return length;
}

int
jpeg_decoder_application0 (JpegDecoder * dec, bits_t * bits)
{
  int length;

  JPEG_DEBUG (0, "app0\n");

  length = get_be_u16 (bits);
  JPEG_DEBUG (0, "length=%d\n", length);

  if (memcmp (bits->ptr, "JFIF", 4) == 0 && bits->ptr[4] == 0) {
    int version;
    int units;
    int x_density;
    int y_density;
    int x_thumbnail;
    int y_thumbnail;

    JPEG_DEBUG (0, "JFIF\n");
    bits->ptr += 5;

    version = get_be_u16 (bits);
    units = get_u8 (bits);
    x_density = get_be_u16 (bits);
    y_density = get_be_u16 (bits);
    x_thumbnail = get_u8 (bits);
    y_thumbnail = get_u8 (bits);

    JPEG_DEBUG (0, "version = %04x\n", version);
    JPEG_DEBUG (0, "units = %d\n", units);
    JPEG_DEBUG (0, "x_density = %d\n", x_density);
    JPEG_DEBUG (0, "y_density = %d\n", y_density);
    JPEG_DEBUG (0, "x_thumbnail = %d\n", x_thumbnail);
    JPEG_DEBUG (0, "y_thumbnail = %d\n", y_thumbnail);

  }

  if (memcmp (bits->ptr, "JFXX", 4) == 0 && bits->ptr[4] == 0) {
    JPEG_DEBUG (0, "JFIF extension (not handled)\n");
    bits->ptr += length - 2;
  }

  return length;
}

int
jpeg_decoder_application_misc (JpegDecoder * dec, bits_t * bits)
{
  int length;

  JPEG_DEBUG (0, "appX\n");

  length = get_be_u16 (bits);
  JPEG_DEBUG (0, "length=%d\n", length);

  JPEG_DEBUG (0, "JPEG application tag X ignored\n");
  dumpbits (bits);

  bits->ptr += length - 2;

  return length;
}

int
jpeg_decoder_comment (JpegDecoder * dec, bits_t * bits)
{
  int length;

  JPEG_DEBUG (0, "comment\n");

  length = get_be_u16 (bits);
  JPEG_DEBUG (0, "length=%d\n", length);

  dumpbits (bits);

  bits->ptr += length - 2;

  return length;
}

int
jpeg_decoder_restart_interval (JpegDecoder * dec, bits_t * bits)
{
  int length;

  JPEG_DEBUG (0, "comment\n");

  length = get_be_u16 (bits);
  JPEG_DEBUG (0, "length=%d\n", length);

  dec->restart_interval = get_be_u16 (bits);
  JPEG_DEBUG (0, "restart_interval=%d\n", dec->restart_interval);

  return length;
}

int
jpeg_decoder_restart (JpegDecoder * dec, bits_t * bits)
{
  JPEG_DEBUG (0, "restart\n");

  return 0;
}

void
jpeg_decoder_decode_entropy_segment (JpegDecoder * dec, bits_t * bits)
{
  bits_t b2, *bits2 = &b2;
  short block[64];
  short block2[64];
  unsigned char *newptr;
  int len;
  int j;
  int i;
  int go;
  int x, y;
  int n;
  int ret;

  len = 0;
  j = 0;
  while (1) {
    if (bits->ptr[len] == 0xff && bits->ptr[len + 1] != 0x00) {
      break;
    }
    len++;
  }
  JPEG_DEBUG (0, "entropy length = %d\n", len);

  /* we allocate extra space, since the getbits() code can
   * potentially read past the end of the buffer */
  newptr = g_malloc (len + 2);
  for (i = 0; i < len; i++) {
    newptr[j] = bits->ptr[i];
    j++;
    if (bits->ptr[i] == 0xff)
      i++;
  }
  bits->ptr += len;

  bits2->ptr = newptr;
  bits2->idx = 0;
  bits2->end = newptr + j;
  newptr[j] = 0;
  newptr[j + 1] = 0;

  dec->dc[0] = dec->dc[1] = dec->dc[2] = dec->dc[3] = 128 * 8;
  go = 1;
  x = dec->x;
  y = dec->y;
  n = dec->restart_interval;
  if (n == 0)
    n = G_MAXINT;
  while (n-- > 0) {
    for (i = 0; i < dec->scan_list_length; i++) {
      int dc_table_index;
      int ac_table_index;
      int quant_index;
      unsigned char *ptr;
      int component_index;

      JPEG_DEBUG (3, "%d,%d: component=%d dc_table=%d ac_table=%d\n",
          x, y,
          dec->scan_list[i].component_index,
          dec->scan_list[i].dc_table, dec->scan_list[i].ac_table);

      component_index = dec->scan_list[i].component_index;
      dc_table_index = dec->scan_list[i].dc_table;
      ac_table_index = dec->scan_list[i].ac_table;
      quant_index = dec->scan_list[i].quant_table;

      ret = huffman_table_decode_macroblock (block,
          dec->dc_huff_table[dc_table_index],
          dec->ac_huff_table[ac_table_index], bits2);
      if (ret < 0) {
        JPEG_DEBUG (0, "%d,%d: component=%d dc_table=%d ac_table=%d\n",
            x, y,
            dec->scan_list[i].component_index,
            dec->scan_list[i].dc_table, dec->scan_list[i].ac_table);
        n = 0;
        break;
      }

      JPEG_DEBUG (3, "using quant table %d\n", quant_index);
      oil_mult8x8_s16 (block2, block, dec->quant_table[quant_index],
          sizeof (short) * 8, sizeof(short) * 8, sizeof (short) * 8);
      dec->dc[component_index] += block2[0];
      block2[0] = dec->dc[component_index];
      oil_unzigzag8x8_s16 (block, sizeof (short) * 8, block2,
          sizeof (short) * 8);
      oil_idct8x8_s16 (block2, sizeof (short) * 8, block, sizeof (short) * 8);
      oil_trans8x8_u16 (block, sizeof (short) * 8, block2, sizeof (short) * 8);

      ptr = dec->components[component_index].image +
          x * dec->components[component_index].h_oversample +
          dec->scan_list[i].offset +
          dec->components[component_index].rowstride * y *
          dec->components[component_index].v_oversample;

      oil_clipconv8x8_u8_s16 (ptr,
          dec->components[component_index].rowstride,
          block, sizeof (short) * 8);
    }
    x += 8;
    if (x * dec->scan_h_subsample >= dec->width) {
      x = 0;
      y += 8;
    }
    if (y * dec->scan_v_subsample >= dec->height) {
      go = 0;
    }
  }
  dec->x = x;
  dec->y = y;
  g_free (newptr);
}



JpegDecoder *
jpeg_decoder_new (void)
{
  JpegDecoder *dec;

  oil_init ();

  dec = g_new0 (JpegDecoder, 1);

  huffman_table_load_std_jpeg (dec);

  return dec;
}

void
jpeg_decoder_free (JpegDecoder * dec)
{
  int i;

  huffman_table_free (dec->dc_huff_table[0]);
  huffman_table_free (dec->ac_huff_table[0]);
  huffman_table_free (dec->dc_huff_table[1]);
  huffman_table_free (dec->ac_huff_table[1]);

  for (i = 0; i < JPEG_N_COMPONENTS; i++) {
    if (dec->components[i].image)
      g_free (dec->components[i].image);
  }

  if (dec->data)
    g_free (dec->data);

  g_free (dec);
}

int
jpeg_decoder_addbits (JpegDecoder * dec, unsigned char *data, unsigned int len)
{
  unsigned int offset;

  offset = dec->bits.ptr - dec->data;

  dec->data = realloc (dec->data, dec->data_len + len);
  memcpy (dec->data + dec->data_len, data, len);
  dec->data_len += len;

  dec->bits.ptr = dec->data + offset;
  dec->bits.end = dec->data + dec->data_len;

  return 0;
}

int
jpeg_decoder_get_image_size (JpegDecoder * dec, int *width, int *height)
{
  if (width)
    *width = dec->width;
  if (height)
    *height = dec->height;

  return 0;
}

int
jpeg_decoder_get_component_ptr (JpegDecoder * dec, int id,
    unsigned char **image, int *rowstride)
{
  int i;

  i = jpeg_decoder_find_component_by_id (dec, id);
  if (image)
    *image = dec->components[i].image;
  if (rowstride)
    *rowstride = dec->components[i].rowstride;

  return 0;
}

int
jpeg_decoder_get_component_size (JpegDecoder * dec, int id,
    int *width, int *height)
{
  int i;

  /* subsampling sizes are rounded up */

  i = jpeg_decoder_find_component_by_id (dec, id);
  if (width)
    *width = (dec->width - 1) / dec->components[i].h_subsample + 1;
  if (height)
    *height = (dec->height - 1) / dec->components[i].v_subsample + 1;

  return 0;
}

int
jpeg_decoder_get_component_subsampling (JpegDecoder * dec, int id,
    int *h_subsample, int *v_subsample)
{
  int i;

  i = jpeg_decoder_find_component_by_id (dec, id);
  if (h_subsample)
    *h_subsample = dec->components[i].h_subsample;
  if (v_subsample)
    *v_subsample = dec->components[i].v_subsample;

  return 0;
}

int
jpeg_decoder_parse (JpegDecoder * dec)
{
  bits_t *bits = &dec->bits;
  bits_t b2;
  unsigned int x;
  unsigned int tag;
  int i;

  while (bits->ptr < bits->end) {
    x = get_u8 (bits);
    if (x != 0xff) {
      int n = 0;

      while (x != 0xff) {
        x = get_u8 (bits);
        n++;
      }
      JPEG_DEBUG (0, "lost sync, skipped %d bytes\n", n);
    }
    while (x == 0xff) {
      x = get_u8 (bits);
    }
    tag = x;
    JPEG_DEBUG (0, "tag %02x\n", tag);

    b2 = *bits;

    for (i = 0; i < n_jpeg_markers - 1; i++) {
      if (tag == jpeg_markers[i].tag) {
        break;
      }
    }
    JPEG_DEBUG (0, "tag: %s\n", jpeg_markers[i].name);
    if (jpeg_markers[i].func) {
      jpeg_markers[i].func (dec, &b2);
    } else {
      JPEG_DEBUG (0, "unhandled or illegal JPEG marker (0x%02x)\n", tag);
      dumpbits (&b2);
    }
    if (jpeg_markers[i].flags & JPEG_ENTROPY_SEGMENT) {
      jpeg_decoder_decode_entropy_segment (dec, &b2);
    }
    syncbits (&b2);
    bits->ptr = b2.ptr;
  }

  return 0;
}


int jpeg_decoder_verbose_level = -1;

void
jpeg_debug (int n, const char *format, ...)
{
  va_list args;

  if (n > jpeg_decoder_verbose_level)
    return;

  fflush (stdout);
  fprintf (stderr, "JPEG_DEBUG: ");
  va_start (args, format);
  vfprintf (stderr, format, args);
  va_end (args);
}


/* misc helper functins */

static char *
sprintbits (char *str, unsigned int bits, int n)
{
  int i;
  int bit = 1 << (n - 1);

  for (i = 0; i < n; i++) {
    str[i] = (bits & bit) ? '1' : '0';
    bit >>= 1;
  }
  str[i] = 0;

  return str;
}

static void
huffman_table_load_std_jpeg (JpegDecoder * dec)
{
  bits_t b, *bits = &b;

  bits->ptr = std_tables;
  bits->idx = 0;
  bits->end = std_tables + sizeof (std_tables);

  dec->dc_huff_table[0] = huffman_table_new_jpeg (bits);
  dec->ac_huff_table[0] = huffman_table_new_jpeg (bits);
  dec->dc_huff_table[1] = huffman_table_new_jpeg (bits);
  dec->ac_huff_table[1] = huffman_table_new_jpeg (bits);
}