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