]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jdapimin.cpp
more eol-style
[xonotic/netradiant.git] / libs / jpeg6 / jdapimin.cpp
1 /*
2
3  * jdapimin.c
4
5  *
6
7  * Copyright (C) 1994-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 application interface code for the decompression half
16
17  * of the JPEG library.  These are the "minimum" API routines that may be
18
19  * needed in either the normal full-decompression case or the
20
21  * transcoding-only case.
22
23  *
24
25  * Most of the routines intended to be called directly by an application
26
27  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
28
29  * shared by compression and decompression, and jdtrans.c for the transcoding
30
31  * case.
32
33  */
34
35
36
37 #define JPEG_INTERNALS
38
39 #include "jinclude.h"
40
41 #include "radiant_jpeglib.h"
42
43
44
45
46
47 /*
48
49  * Initialization of a JPEG decompression object.
50
51  * The error manager must already be set up (in case memory manager fails).
52
53  */
54
55
56
57 GLOBAL void
58
59 jpeg_create_decompress (j_decompress_ptr cinfo)
60
61 {
62
63   int i;
64
65
66
67   /* For debugging purposes, zero the whole master structure.
68
69    * But error manager pointer is already there, so save and restore it.
70
71    */
72
73   {
74
75     struct jpeg_error_mgr * err = cinfo->err;
76
77     i = sizeof(struct jpeg_decompress_struct);
78
79     i = SIZEOF(struct jpeg_decompress_struct);
80
81     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
82
83     cinfo->err = err;
84
85   }
86
87   cinfo->is_decompressor = TRUE;
88
89
90
91   /* Initialize a memory manager instance for this object */
92
93   jinit_memory_mgr((j_common_ptr) cinfo);
94
95
96
97   /* Zero out pointers to permanent structures. */
98
99   cinfo->progress = NULL;
100
101   cinfo->src = NULL;
102
103
104
105   for (i = 0; i < NUM_QUANT_TBLS; i++)
106
107     cinfo->quant_tbl_ptrs[i] = NULL;
108
109
110
111   for (i = 0; i < NUM_HUFF_TBLS; i++) {
112
113     cinfo->dc_huff_tbl_ptrs[i] = NULL;
114
115     cinfo->ac_huff_tbl_ptrs[i] = NULL;
116
117   }
118
119
120
121   /* Initialize marker processor so application can override methods
122
123    * for COM, APPn markers before calling jpeg_read_header.
124
125    */
126
127   jinit_marker_reader(cinfo);
128
129
130
131   /* And initialize the overall input controller. */
132
133   jinit_input_controller(cinfo);
134
135
136
137   /* OK, I'm ready */
138
139   cinfo->global_state = DSTATE_START;
140
141 }
142
143
144
145
146
147 /*
148
149  * Destruction of a JPEG decompression object
150
151  */
152
153
154
155 GLOBAL void
156
157 jpeg_destroy_decompress (j_decompress_ptr cinfo)
158
159 {
160
161   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
162
163 }
164
165
166
167
168
169 /*
170
171  * Abort processing of a JPEG decompression operation,
172
173  * but don't destroy the object itself.
174
175  */
176
177
178
179 GLOBAL void
180
181 jpeg_abort_decompress (j_decompress_ptr cinfo)
182
183 {
184
185   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
186
187 }
188
189
190
191
192
193 /*
194
195  * Install a special processing method for COM or APPn markers.
196
197  */
198
199
200
201 GLOBAL void
202
203 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
204
205                            jpeg_marker_parser_method routine)
206
207 {
208
209   if (marker_code == JPEG_COM)
210
211     cinfo->marker->process_COM = routine;
212
213   else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
214
215     cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
216
217   else
218
219     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
220
221 }
222
223
224
225
226
227 /*
228
229  * Set default decompression parameters.
230
231  */
232
233
234
235 LOCAL void
236
237 default_decompress_parms (j_decompress_ptr cinfo)
238
239 {
240
241   /* Guess the input colorspace, and set output colorspace accordingly. */
242
243   /* (Wish JPEG committee had provided a real way to specify this...) */
244
245   /* Note application may override our guesses. */
246
247   switch (cinfo->num_components) {
248
249   case 1:
250
251     cinfo->jpeg_color_space = JCS_GRAYSCALE;
252
253     cinfo->out_color_space = JCS_GRAYSCALE;
254
255     break;
256
257     
258
259   case 3:
260
261     if (cinfo->saw_JFIF_marker) {
262
263       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
264
265     } else if (cinfo->saw_Adobe_marker) {
266
267       switch (cinfo->Adobe_transform) {
268
269       case 0:
270
271         cinfo->jpeg_color_space = JCS_RGB;
272
273         break;
274
275       case 1:
276
277         cinfo->jpeg_color_space = JCS_YCbCr;
278
279         break;
280
281       default:
282
283         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
284
285         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
286
287         break;
288
289       }
290
291     } else {
292
293       /* Saw no special markers, try to guess from the component IDs */
294
295       int cid0 = cinfo->comp_info[0].component_id;
296
297       int cid1 = cinfo->comp_info[1].component_id;
298
299       int cid2 = cinfo->comp_info[2].component_id;
300
301
302
303       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
304
305         cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
306
307       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
308
309         cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
310
311       else {
312
313         TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
314
315         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
316
317       }
318
319     }
320
321     /* Always guess RGB is proper output colorspace. */
322
323     cinfo->out_color_space = JCS_RGB;
324
325     break;
326
327     
328
329   case 4:
330
331     if (cinfo->saw_Adobe_marker) {
332
333       switch (cinfo->Adobe_transform) {
334
335       case 0:
336
337         cinfo->jpeg_color_space = JCS_CMYK;
338
339         break;
340
341       case 2:
342
343         cinfo->jpeg_color_space = JCS_YCCK;
344
345         break;
346
347       default:
348
349         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
350
351         cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
352
353         break;
354
355       }
356
357     } else {
358
359       /* No special markers, assume straight CMYK. */
360
361       cinfo->jpeg_color_space = JCS_CMYK;
362
363     }
364
365     cinfo->out_color_space = JCS_CMYK;
366
367     break;
368
369     
370
371   default:
372
373     cinfo->jpeg_color_space = JCS_UNKNOWN;
374
375     cinfo->out_color_space = JCS_UNKNOWN;
376
377     break;
378
379   }
380
381
382
383   /* Set defaults for other decompression parameters. */
384
385   cinfo->scale_num = 1;         /* 1:1 scaling */
386
387   cinfo->scale_denom = 1;
388
389   cinfo->output_gamma = 1.0;
390
391   cinfo->buffered_image = FALSE;
392
393   cinfo->raw_data_out = FALSE;
394
395   cinfo->dct_method = JDCT_DEFAULT;
396
397   cinfo->do_fancy_upsampling = TRUE;
398
399   cinfo->do_block_smoothing = TRUE;
400
401   cinfo->quantize_colors = FALSE;
402
403   /* We set these in case application only sets quantize_colors. */
404
405   cinfo->dither_mode = JDITHER_FS;
406
407 #ifdef QUANT_2PASS_SUPPORTED
408
409   cinfo->two_pass_quantize = TRUE;
410
411 #else
412
413   cinfo->two_pass_quantize = FALSE;
414
415 #endif
416
417   cinfo->desired_number_of_colors = 256;
418
419   cinfo->colormap = NULL;
420
421   /* Initialize for no mode change in buffered-image mode. */
422
423   cinfo->enable_1pass_quant = FALSE;
424
425   cinfo->enable_external_quant = FALSE;
426
427   cinfo->enable_2pass_quant = FALSE;
428
429 }
430
431
432
433
434
435 /*
436
437  * Decompression startup: read start of JPEG datastream to see what's there.
438
439  * Need only initialize JPEG object and supply a data source before calling.
440
441  *
442
443  * This routine will read as far as the first SOS marker (ie, actual start of
444
445  * compressed data), and will save all tables and parameters in the JPEG
446
447  * object.  It will also initialize the decompression parameters to default
448
449  * values, and finally return JPEG_HEADER_OK.  On return, the application may
450
451  * adjust the decompression parameters and then call jpeg_start_decompress.
452
453  * (Or, if the application only wanted to determine the image parameters,
454
455  * the data need not be decompressed.  In that case, call jpeg_abort or
456
457  * jpeg_destroy to release any temporary space.)
458
459  * If an abbreviated (tables only) datastream is presented, the routine will
460
461  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
462
463  * re-use the JPEG object to read the abbreviated image datastream(s).
464
465  * It is unnecessary (but OK) to call jpeg_abort in this case.
466
467  * The JPEG_SUSPENDED return code only occurs if the data source module
468
469  * requests suspension of the decompressor.  In this case the application
470
471  * should load more source data and then re-call jpeg_read_header to resume
472
473  * processing.
474
475  * If a non-suspending data source is used and require_image is TRUE, then the
476
477  * return code need not be inspected since only JPEG_HEADER_OK is possible.
478
479  *
480
481  * This routine is now just a front end to jpeg_consume_input, with some
482
483  * extra error checking.
484
485  */
486
487
488
489 GLOBAL int
490
491 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
492
493 {
494
495   int retcode;
496
497
498
499   if (cinfo->global_state != DSTATE_START &&
500
501       cinfo->global_state != DSTATE_INHEADER)
502
503     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
504
505
506
507   retcode = jpeg_consume_input(cinfo);
508
509
510
511   switch (retcode) {
512
513   case JPEG_REACHED_SOS:
514
515     retcode = JPEG_HEADER_OK;
516
517     break;
518
519   case JPEG_REACHED_EOI:
520
521     if (require_image)          /* Complain if application wanted an image */
522
523       ERREXIT(cinfo, JERR_NO_IMAGE);
524
525     /* Reset to start state; it would be safer to require the application to
526
527      * call jpeg_abort, but we can't change it now for compatibility reasons.
528
529      * A side effect is to free any temporary memory (there shouldn't be any).
530
531      */
532
533     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
534
535     retcode = JPEG_HEADER_TABLES_ONLY;
536
537     break;
538
539   case JPEG_SUSPENDED:
540
541     /* no work */
542
543     break;
544
545   }
546
547
548
549   return retcode;
550
551 }
552
553
554
555
556
557 /*
558
559  * Consume data in advance of what the decompressor requires.
560
561  * This can be called at any time once the decompressor object has
562
563  * been created and a data source has been set up.
564
565  *
566
567  * This routine is essentially a state machine that handles a couple
568
569  * of critical state-transition actions, namely initial setup and
570
571  * transition from header scanning to ready-for-start_decompress.
572
573  * All the actual input is done via the input controller's consume_input
574
575  * method.
576
577  */
578
579
580
581 GLOBAL int
582
583 jpeg_consume_input (j_decompress_ptr cinfo)
584
585 {
586
587   int retcode = JPEG_SUSPENDED;
588
589
590
591   /* NB: every possible DSTATE value should be listed in this switch */
592
593   switch (cinfo->global_state) {
594
595   case DSTATE_START:
596
597     /* Start-of-datastream actions: reset appropriate modules */
598
599     (*cinfo->inputctl->reset_input_controller) (cinfo);
600
601     /* Initialize application's data source module */
602
603     (*cinfo->src->init_source) (cinfo);
604
605     cinfo->global_state = DSTATE_INHEADER;
606
607     /*FALLTHROUGH*/
608
609   case DSTATE_INHEADER:
610
611     retcode = (*cinfo->inputctl->consume_input) (cinfo);
612
613     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
614
615       /* Set up default parameters based on header data */
616
617       default_decompress_parms(cinfo);
618
619       /* Set global state: ready for start_decompress */
620
621       cinfo->global_state = DSTATE_READY;
622
623     }
624
625     break;
626
627   case DSTATE_READY:
628
629     /* Can't advance past first SOS until start_decompress is called */
630
631     retcode = JPEG_REACHED_SOS;
632
633     break;
634
635   case DSTATE_PRELOAD:
636
637   case DSTATE_PRESCAN:
638
639   case DSTATE_SCANNING:
640
641   case DSTATE_RAW_OK:
642
643   case DSTATE_BUFIMAGE:
644
645   case DSTATE_BUFPOST:
646
647   case DSTATE_STOPPING:
648
649     retcode = (*cinfo->inputctl->consume_input) (cinfo);
650
651     break;
652
653   default:
654
655     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
656
657   }
658
659   return retcode;
660
661 }
662
663
664
665
666
667 /*
668
669  * Have we finished reading the input file?
670
671  */
672
673
674
675 GLOBAL boolean
676
677 jpeg_input_complete (j_decompress_ptr cinfo)
678
679 {
680
681   /* Check for valid jpeg object */
682
683   if (cinfo->global_state < DSTATE_START ||
684
685       cinfo->global_state > DSTATE_STOPPING)
686
687     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
688
689   return cinfo->inputctl->eoi_reached;
690
691 }
692
693
694
695
696
697 /*
698
699  * Is there more than one scan?
700
701  */
702
703
704
705 GLOBAL boolean
706
707 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
708
709 {
710
711   /* Only valid after jpeg_read_header completes */
712
713   if (cinfo->global_state < DSTATE_READY ||
714
715       cinfo->global_state > DSTATE_STOPPING)
716
717     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
718
719   return cinfo->inputctl->has_multiple_scans;
720
721 }
722
723
724
725
726
727 /*
728
729  * Finish JPEG decompression.
730
731  *
732
733  * This will normally just verify the file trailer and release temp storage.
734
735  *
736
737  * Returns FALSE if suspended.  The return value need be inspected only if
738
739  * a suspending data source is used.
740
741  */
742
743
744
745 GLOBAL boolean
746
747 jpeg_finish_decompress (j_decompress_ptr cinfo)
748
749 {
750
751   if ((cinfo->global_state == DSTATE_SCANNING ||
752
753        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
754
755     /* Terminate final pass of non-buffered mode */
756
757     if (cinfo->output_scanline < cinfo->output_height)
758
759       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
760
761     (*cinfo->master->finish_output_pass) (cinfo);
762
763     cinfo->global_state = DSTATE_STOPPING;
764
765   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
766
767     /* Finishing after a buffered-image operation */
768
769     cinfo->global_state = DSTATE_STOPPING;
770
771   } else if (cinfo->global_state != DSTATE_STOPPING) {
772
773     /* STOPPING = repeat call after a suspension, anything else is error */
774
775     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
776
777   }
778
779   /* Read until EOI */
780
781   while (! cinfo->inputctl->eoi_reached) {
782
783     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
784
785       return FALSE;             /* Suspend, come back later */
786
787   }
788
789   /* Do final cleanup */
790
791   (*cinfo->src->term_source) (cinfo);
792
793   /* We can use jpeg_abort to release memory and reset global_state */
794
795   jpeg_abort((j_common_ptr) cinfo);
796
797   return TRUE;
798
799 }
800