]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jdinput.cpp
more eol-style
[xonotic/netradiant.git] / libs / jpeg6 / jdinput.cpp
1 /*
2
3  * jdinput.c
4
5  *
6
7  * Copyright (C) 1991-1995, Thomas G. Lane.
8
9  * This file is part of the Independent JPEG Group's software.
10
11  * For conditions of distribution and use, see the accompanying README file.
12
13  *
14
15  * This file contains input control logic for the JPEG decompressor.
16
17  * These routines are concerned with controlling the decompressor's input
18
19  * processing (marker reading and coefficient decoding).  The actual input
20
21  * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
22
23  */
24
25
26
27 #define JPEG_INTERNALS
28
29 #include "jinclude.h"
30
31 #include "radiant_jpeglib.h"
32
33
34
35
36
37 /* Private state */
38
39
40
41 typedef struct {
42
43   struct jpeg_input_controller pub; /* public fields */
44
45
46
47   boolean inheaders;            /* TRUE until first SOS is reached */
48
49 } my_input_controller;
50
51
52
53 typedef my_input_controller * my_inputctl_ptr;
54
55
56
57
58
59 /* Forward declarations */
60
61 METHODDEF int consume_markers JPP((j_decompress_ptr cinfo));
62
63
64
65
66
67 /*
68
69  * Routines to calculate various quantities related to the size of the image.
70
71  */
72
73
74
75 LOCAL void
76
77 initial_setup (j_decompress_ptr cinfo)
78
79 /* Called once, when first SOS marker is reached */
80
81 {
82
83   int ci;
84
85   jpeg_component_info *compptr;
86
87
88
89   /* Make sure image isn't bigger than I can handle */
90
91   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
92
93       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
94
95     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
96
97
98
99   /* For now, precision must match compiled-in value... */
100
101   if (cinfo->data_precision != BITS_IN_JSAMPLE)
102
103     ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
104
105
106
107   /* Check that number of components won't exceed internal array sizes */
108
109   if (cinfo->num_components > MAX_COMPONENTS)
110
111     ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
112
113              MAX_COMPONENTS);
114
115
116
117   /* Compute maximum sampling factors; check factor validity */
118
119   cinfo->max_h_samp_factor = 1;
120
121   cinfo->max_v_samp_factor = 1;
122
123   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
124
125        ci++, compptr++) {
126
127     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
128
129         compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
130
131       ERREXIT(cinfo, JERR_BAD_SAMPLING);
132
133     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
134
135                                    compptr->h_samp_factor);
136
137     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
138
139                                    compptr->v_samp_factor);
140
141   }
142
143
144
145   /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
146
147    * In the full decompressor, this will be overridden by jdmaster.c;
148
149    * but in the transcoder, jdmaster.c is not used, so we must do it here.
150
151    */
152
153   cinfo->min_DCT_scaled_size = DCTSIZE;
154
155
156
157   /* Compute dimensions of components */
158
159   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
160
161        ci++, compptr++) {
162
163     compptr->DCT_scaled_size = DCTSIZE;
164
165     /* Size in DCT blocks */
166
167     compptr->width_in_blocks = (JDIMENSION)
168
169       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
170
171                     (long) (cinfo->max_h_samp_factor * DCTSIZE));
172
173     compptr->height_in_blocks = (JDIMENSION)
174
175       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
176
177                     (long) (cinfo->max_v_samp_factor * DCTSIZE));
178
179     /* downsampled_width and downsampled_height will also be overridden by
180
181      * jdmaster.c if we are doing full decompression.  The transcoder library
182
183      * doesn't use these values, but the calling application might.
184
185      */
186
187     /* Size in samples */
188
189     compptr->downsampled_width = (JDIMENSION)
190
191       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
192
193                     (long) cinfo->max_h_samp_factor);
194
195     compptr->downsampled_height = (JDIMENSION)
196
197       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
198
199                     (long) cinfo->max_v_samp_factor);
200
201     /* Mark component needed, until color conversion says otherwise */
202
203     compptr->component_needed = TRUE;
204
205     /* Mark no quantization table yet saved for component */
206
207     compptr->quant_table = NULL;
208
209   }
210
211
212
213   /* Compute number of fully interleaved MCU rows. */
214
215   cinfo->total_iMCU_rows = (JDIMENSION)
216
217     jdiv_round_up((long) cinfo->image_height,
218
219                   (long) (cinfo->max_v_samp_factor*DCTSIZE));
220
221
222
223   /* Decide whether file contains multiple scans */
224
225   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
226
227     cinfo->inputctl->has_multiple_scans = TRUE;
228
229   else
230
231     cinfo->inputctl->has_multiple_scans = FALSE;
232
233 }
234
235
236
237
238
239 LOCAL void
240
241 per_scan_setup (j_decompress_ptr cinfo)
242
243 /* Do computations that are needed before processing a JPEG scan */
244
245 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
246
247 {
248
249   int ci, mcublks, tmp;
250
251   jpeg_component_info *compptr;
252
253   
254
255   if (cinfo->comps_in_scan == 1) {
256
257     
258
259     /* Noninterleaved (single-component) scan */
260
261     compptr = cinfo->cur_comp_info[0];
262
263     
264
265     /* Overall image size in MCUs */
266
267     cinfo->MCUs_per_row = compptr->width_in_blocks;
268
269     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
270
271     
272
273     /* For noninterleaved scan, always one block per MCU */
274
275     compptr->MCU_width = 1;
276
277     compptr->MCU_height = 1;
278
279     compptr->MCU_blocks = 1;
280
281     compptr->MCU_sample_width = compptr->DCT_scaled_size;
282
283     compptr->last_col_width = 1;
284
285     /* For noninterleaved scans, it is convenient to define last_row_height
286
287      * as the number of block rows present in the last iMCU row.
288
289      */
290
291     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
292
293     if (tmp == 0) tmp = compptr->v_samp_factor;
294
295     compptr->last_row_height = tmp;
296
297     
298
299     /* Prepare array describing MCU composition */
300
301     cinfo->blocks_in_MCU = 1;
302
303     cinfo->MCU_membership[0] = 0;
304
305     
306
307   } else {
308
309     
310
311     /* Interleaved (multi-component) scan */
312
313     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
314
315       ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
316
317                MAX_COMPS_IN_SCAN);
318
319     
320
321     /* Overall image size in MCUs */
322
323     cinfo->MCUs_per_row = (JDIMENSION)
324
325       jdiv_round_up((long) cinfo->image_width,
326
327                     (long) (cinfo->max_h_samp_factor*DCTSIZE));
328
329     cinfo->MCU_rows_in_scan = (JDIMENSION)
330
331       jdiv_round_up((long) cinfo->image_height,
332
333                     (long) (cinfo->max_v_samp_factor*DCTSIZE));
334
335     
336
337     cinfo->blocks_in_MCU = 0;
338
339     
340
341     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
342
343       compptr = cinfo->cur_comp_info[ci];
344
345       /* Sampling factors give # of blocks of component in each MCU */
346
347       compptr->MCU_width = compptr->h_samp_factor;
348
349       compptr->MCU_height = compptr->v_samp_factor;
350
351       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
352
353       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
354
355       /* Figure number of non-dummy blocks in last MCU column & row */
356
357       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
358
359       if (tmp == 0) tmp = compptr->MCU_width;
360
361       compptr->last_col_width = tmp;
362
363       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
364
365       if (tmp == 0) tmp = compptr->MCU_height;
366
367       compptr->last_row_height = tmp;
368
369       /* Prepare array describing MCU composition */
370
371       mcublks = compptr->MCU_blocks;
372
373       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
374
375         ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
376
377       while (mcublks-- > 0) {
378
379         cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
380
381       }
382
383     }
384
385     
386
387   }
388
389 }
390
391
392
393
394
395 /*
396
397  * Save away a copy of the Q-table referenced by each component present
398
399  * in the current scan, unless already saved during a prior scan.
400
401  *
402
403  * In a multiple-scan JPEG file, the encoder could assign different components
404
405  * the same Q-table slot number, but change table definitions between scans
406
407  * so that each component uses a different Q-table.  (The IJG encoder is not
408
409  * currently capable of doing this, but other encoders might.)  Since we want
410
411  * to be able to dequantize all the components at the end of the file, this
412
413  * means that we have to save away the table actually used for each component.
414
415  * We do this by copying the table at the start of the first scan containing
416
417  * the component.
418
419  * The JPEG spec prohibits the encoder from changing the contents of a Q-table
420
421  * slot between scans of a component using that slot.  If the encoder does so
422
423  * anyway, this decoder will simply use the Q-table values that were current
424
425  * at the start of the first scan for the component.
426
427  *
428
429  * The decompressor output side looks only at the saved quant tables,
430
431  * not at the current Q-table slots.
432
433  */
434
435
436
437 LOCAL void
438
439 latch_quant_tables (j_decompress_ptr cinfo)
440
441 {
442
443   int ci, qtblno;
444
445   jpeg_component_info *compptr;
446
447   JQUANT_TBL * qtbl;
448
449
450
451   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
452
453     compptr = cinfo->cur_comp_info[ci];
454
455     /* No work if we already saved Q-table for this component */
456
457     if (compptr->quant_table != NULL)
458
459       continue;
460
461     /* Make sure specified quantization table is present */
462
463     qtblno = compptr->quant_tbl_no;
464
465     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
466
467         cinfo->quant_tbl_ptrs[qtblno] == NULL)
468
469       ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
470
471     /* OK, save away the quantization table */
472
473     qtbl = (JQUANT_TBL *)
474
475       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
476
477                                   SIZEOF(JQUANT_TBL));
478
479     MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
480
481     compptr->quant_table = qtbl;
482
483   }
484
485 }
486
487
488
489
490
491 /*
492
493  * Initialize the input modules to read a scan of compressed data.
494
495  * The first call to this is done by jdmaster.c after initializing
496
497  * the entire decompressor (during jpeg_start_decompress).
498
499  * Subsequent calls come from consume_markers, below.
500
501  */
502
503
504
505 METHODDEF void
506
507 start_input_pass (j_decompress_ptr cinfo)
508
509 {
510
511   per_scan_setup(cinfo);
512
513   latch_quant_tables(cinfo);
514
515   (*cinfo->entropy->start_pass) (cinfo);
516
517   (*cinfo->coef->start_input_pass) (cinfo);
518
519   cinfo->inputctl->consume_input = cinfo->coef->consume_data;
520
521 }
522
523
524
525
526
527 /*
528
529  * Finish up after inputting a compressed-data scan.
530
531  * This is called by the coefficient controller after it's read all
532
533  * the expected data of the scan.
534
535  */
536
537
538
539 METHODDEF void
540
541 finish_input_pass (j_decompress_ptr cinfo)
542
543 {
544
545   cinfo->inputctl->consume_input = consume_markers;
546
547 }
548
549
550
551
552
553 /*
554
555  * Read JPEG markers before, between, or after compressed-data scans.
556
557  * Change state as necessary when a new scan is reached.
558
559  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
560
561  *
562
563  * The consume_input method pointer points either here or to the
564
565  * coefficient controller's consume_data routine, depending on whether
566
567  * we are reading a compressed data segment or inter-segment markers.
568
569  */
570
571
572
573 METHODDEF int
574
575 consume_markers (j_decompress_ptr cinfo)
576
577 {
578
579   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
580
581   int val;
582
583
584
585   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
586
587     return JPEG_REACHED_EOI;
588
589
590
591   val = (*cinfo->marker->read_markers) (cinfo);
592
593
594
595   switch (val) {
596
597   case JPEG_REACHED_SOS:        /* Found SOS */
598
599     if (inputctl->inheaders) {  /* 1st SOS */
600
601       initial_setup(cinfo);
602
603       inputctl->inheaders = FALSE;
604
605       /* Note: start_input_pass must be called by jdmaster.c
606
607        * before any more input can be consumed.  jdapi.c is
608
609        * responsible for enforcing this sequencing.
610
611        */
612
613     } else {                    /* 2nd or later SOS marker */
614
615       if (! inputctl->pub.has_multiple_scans)
616
617         ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
618
619       start_input_pass(cinfo);
620
621     }
622
623     break;
624
625   case JPEG_REACHED_EOI:        /* Found EOI */
626
627     inputctl->pub.eoi_reached = TRUE;
628
629     if (inputctl->inheaders) {  /* Tables-only datastream, apparently */
630
631       if (cinfo->marker->saw_SOF)
632
633         ERREXIT(cinfo, JERR_SOF_NO_SOS);
634
635     } else {
636
637       /* Prevent infinite loop in coef ctlr's decompress_data routine
638
639        * if user set output_scan_number larger than number of scans.
640
641        */
642
643       if (cinfo->output_scan_number > cinfo->input_scan_number)
644
645         cinfo->output_scan_number = cinfo->input_scan_number;
646
647     }
648
649     break;
650
651   case JPEG_SUSPENDED:
652
653     break;
654
655   }
656
657
658
659   return val;
660
661 }
662
663
664
665
666
667 /*
668
669  * Reset state to begin a fresh datastream.
670
671  */
672
673
674
675 METHODDEF void
676
677 reset_input_controller (j_decompress_ptr cinfo)
678
679 {
680
681   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
682
683
684
685   inputctl->pub.consume_input = consume_markers;
686
687   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
688
689   inputctl->pub.eoi_reached = FALSE;
690
691   inputctl->inheaders = TRUE;
692
693   /* Reset other modules */
694
695   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
696
697   (*cinfo->marker->reset_marker_reader) (cinfo);
698
699   /* Reset progression state -- would be cleaner if entropy decoder did this */
700
701   cinfo->coef_bits = NULL;
702
703 }
704
705
706
707
708
709 /*
710
711  * Initialize the input controller module.
712
713  * This is called only once, when the decompression object is created.
714
715  */
716
717
718
719 GLOBAL void
720
721 jinit_input_controller (j_decompress_ptr cinfo)
722
723 {
724
725   my_inputctl_ptr inputctl;
726
727
728
729   /* Create subobject in permanent pool */
730
731   inputctl = (my_inputctl_ptr)
732
733     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
734
735                                 SIZEOF(my_input_controller));
736
737   cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
738
739   /* Initialize method pointers */
740
741   inputctl->pub.consume_input = consume_markers;
742
743   inputctl->pub.reset_input_controller = reset_input_controller;
744
745   inputctl->pub.start_input_pass = start_input_pass;
746
747   inputctl->pub.finish_input_pass = finish_input_pass;
748
749   /* Initialize state: can't use reset_input_controller since we don't
750
751    * want to try to reset other modules yet.
752
753    */
754
755   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
756
757   inputctl->pub.eoi_reached = FALSE;
758
759   inputctl->inheaders = TRUE;
760
761 }
762