1 /*
2  * cdjpeg.h
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1994-1997, Thomas G. Lane.
6  * Modified 2019 by Guido Vollbeding.
7  * libjpeg-turbo Modifications:
8  * Copyright (C) 2017, 2019, 2021, D. R. Commander.
9  * For conditions of distribution and use, see the accompanying README.ijg
10  * file.
11  *
12  * This file contains common declarations for the sample applications
13  * cjpeg and djpeg.  It is NOT used by the core JPEG library.
14  */
15 
16 #define JPEG_CJPEG_DJPEG        /* define proper options in jconfig.h */
17 #define JPEG_INTERNAL_OPTIONS   /* cjpeg.c,djpeg.c need to see xxx_SUPPORTED */
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jerror.h"             /* get library error codes too */
21 #include "cderror.h"            /* get application-specific error codes */
22 
23 
24 /*
25  * Object interface for cjpeg's source file decoding modules
26  */
27 
28 typedef struct cjpeg_source_struct *cjpeg_source_ptr;
29 
30 struct cjpeg_source_struct {
31   void (*start_input) (j_compress_ptr cinfo, cjpeg_source_ptr sinfo);
32   JDIMENSION (*get_pixel_rows) (j_compress_ptr cinfo, cjpeg_source_ptr sinfo);
33   void (*finish_input) (j_compress_ptr cinfo, cjpeg_source_ptr sinfo);
34 
35   FILE *input_file;
36 
37   JSAMPARRAY buffer;
38   JDIMENSION buffer_height;
39 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
40   JDIMENSION max_pixels;
41 #endif
42 };
43 
44 
45 /*
46  * Object interface for djpeg's output file encoding modules
47  */
48 
49 typedef struct djpeg_dest_struct *djpeg_dest_ptr;
50 
51 struct djpeg_dest_struct {
52   /* start_output is called after jpeg_start_decompress finishes.
53    * The color map will be ready at this time, if one is needed.
54    */
55   void (*start_output) (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo);
56   /* Emit the specified number of pixel rows from the buffer. */
57   void (*put_pixel_rows) (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
58                           JDIMENSION rows_supplied);
59   /* Finish up at the end of the image. */
60   void (*finish_output) (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo);
61   /* Re-calculate buffer dimensions based on output dimensions (for use with
62      partial image decompression.)  If this is NULL, then the output format
63      does not support partial image decompression (BMP, in particular, cannot
64      support partial decompression because it uses an inversion buffer to write
65      the image in bottom-up order.) */
66   void (*calc_buffer_dimensions) (j_decompress_ptr cinfo,
67                                   djpeg_dest_ptr dinfo);
68 
69 
70   /* Target file spec; filled in by djpeg.c after object is created. */
71   FILE *output_file;
72 
73   /* Output pixel-row buffer.  Created by module init or start_output.
74    * Width is cinfo->output_width * cinfo->output_components;
75    * height is buffer_height.
76    */
77   JSAMPARRAY buffer;
78   JDIMENSION buffer_height;
79 };
80 
81 
82 /*
83  * cjpeg/djpeg may need to perform extra passes to convert to or from
84  * the source/destination file format.  The JPEG library does not know
85  * about these passes, but we'd like them to be counted by the progress
86  * monitor.  We use an expanded progress monitor object to hold the
87  * additional pass count.
88  */
89 
90 struct cdjpeg_progress_mgr {
91   struct jpeg_progress_mgr pub; /* fields known to JPEG library */
92   int completed_extra_passes;   /* extra passes completed */
93   int total_extra_passes;       /* total extra */
94   JDIMENSION max_scans;         /* abort if the number of scans exceeds this
95                                    value and the value is non-zero */
96   boolean report;               /* whether or not to report progress */
97   /* last printed percentage stored here to avoid multiple printouts */
98   int percent_done;
99 };
100 
101 typedef struct cdjpeg_progress_mgr *cd_progress_ptr;
102 
103 
104 /* Module selection routines for I/O modules. */
105 
106 EXTERN(cjpeg_source_ptr) jinit_read_bmp(j_compress_ptr cinfo,
107                                         boolean use_inversion_array);
108 EXTERN(djpeg_dest_ptr) jinit_write_bmp(j_decompress_ptr cinfo, boolean is_os2,
109                                        boolean use_inversion_array);
110 EXTERN(cjpeg_source_ptr) jinit_read_gif(j_compress_ptr cinfo);
111 EXTERN(djpeg_dest_ptr) jinit_write_gif(j_decompress_ptr cinfo, boolean is_lzw);
112 EXTERN(cjpeg_source_ptr) jinit_read_ppm(j_compress_ptr cinfo);
113 EXTERN(djpeg_dest_ptr) jinit_write_ppm(j_decompress_ptr cinfo);
114 EXTERN(cjpeg_source_ptr) jinit_read_targa(j_compress_ptr cinfo);
115 EXTERN(djpeg_dest_ptr) jinit_write_targa(j_decompress_ptr cinfo);
116 
117 /* cjpeg support routines (in rdswitch.c) */
118 
119 EXTERN(boolean) read_quant_tables(j_compress_ptr cinfo, char *filename,
120                                   boolean force_baseline);
121 EXTERN(boolean) read_scan_script(j_compress_ptr cinfo, char *filename);
122 EXTERN(boolean) set_quality_ratings(j_compress_ptr cinfo, char *arg,
123                                     boolean force_baseline);
124 EXTERN(boolean) set_quant_slots(j_compress_ptr cinfo, char *arg);
125 EXTERN(boolean) set_sample_factors(j_compress_ptr cinfo, char *arg);
126 
127 /* djpeg support routines (in rdcolmap.c) */
128 
129 EXTERN(void) read_color_map(j_decompress_ptr cinfo, FILE *infile);
130 
131 /* common support routines (in cdjpeg.c) */
132 
133 EXTERN(void) start_progress_monitor(j_common_ptr cinfo,
134                                     cd_progress_ptr progress);
135 EXTERN(void) end_progress_monitor(j_common_ptr cinfo);
136 EXTERN(boolean) keymatch(char *arg, const char *keyword, int minchars);
137 EXTERN(FILE *) read_stdin(void);
138 EXTERN(FILE *) write_stdout(void);
139 
140 /* miscellaneous useful macros */
141 
142 #ifdef DONT_USE_B_MODE          /* define mode parameters for fopen() */
143 #define READ_BINARY     "r"
144 #define WRITE_BINARY    "w"
145 #else
146 #define READ_BINARY     "rb"
147 #define WRITE_BINARY    "wb"
148 #endif
149 
150 #ifndef EXIT_FAILURE            /* define exit() codes if not provided */
151 #define EXIT_FAILURE  1
152 #endif
153 #ifndef EXIT_SUCCESS
154 #define EXIT_SUCCESS  0
155 #endif
156 #ifndef EXIT_WARNING
157 #define EXIT_WARNING  2
158 #endif
159 
160 #define IsExtRGB(cs) \
161   (cs == JCS_RGB || (cs >= JCS_EXT_RGB && cs <= JCS_EXT_ARGB))
162