Imported Upstream version 3.0.1
[platform/upstream/libjpeg-turbo.git] / cdjpeg.h
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-2022, 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   J12SAMPARRAY buffer12;
39 #ifdef C_LOSSLESS_SUPPORTED
40   J16SAMPARRAY buffer16;
41 #endif
42   JDIMENSION buffer_height;
43 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
44   JDIMENSION max_pixels;
45 #endif
46 };
47
48
49 /*
50  * Object interface for djpeg's output file encoding modules
51  */
52
53 typedef struct djpeg_dest_struct *djpeg_dest_ptr;
54
55 struct djpeg_dest_struct {
56   /* start_output is called after jpeg_start_decompress finishes.
57    * The color map will be ready at this time, if one is needed.
58    */
59   void (*start_output) (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo);
60   /* Emit the specified number of pixel rows from the buffer. */
61   void (*put_pixel_rows) (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
62                           JDIMENSION rows_supplied);
63   /* Finish up at the end of the image. */
64   void (*finish_output) (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo);
65   /* Re-calculate buffer dimensions based on output dimensions (for use with
66      partial image decompression.)  If this is NULL, then the output format
67      does not support partial image decompression (BMP, in particular, cannot
68      support partial decompression because it uses an inversion buffer to write
69      the image in bottom-up order.) */
70   void (*calc_buffer_dimensions) (j_decompress_ptr cinfo,
71                                   djpeg_dest_ptr dinfo);
72
73
74   /* Target file spec; filled in by djpeg.c after object is created. */
75   FILE *output_file;
76
77   /* Output pixel-row buffer.  Created by module init or start_output.
78    * Width is cinfo->output_width * cinfo->output_components;
79    * height is buffer_height.
80    */
81   JSAMPARRAY buffer;
82   J12SAMPARRAY buffer12;
83 #ifdef D_LOSSLESS_SUPPORTED
84   J16SAMPARRAY buffer16;
85 #endif
86   JDIMENSION buffer_height;
87 };
88
89
90 /*
91  * cjpeg/djpeg may need to perform extra passes to convert to or from
92  * the source/destination file format.  The JPEG library does not know
93  * about these passes, but we'd like them to be counted by the progress
94  * monitor.  We use an expanded progress monitor object to hold the
95  * additional pass count.
96  */
97
98 struct cdjpeg_progress_mgr {
99   struct jpeg_progress_mgr pub; /* fields known to JPEG library */
100   int completed_extra_passes;   /* extra passes completed */
101   int total_extra_passes;       /* total extra */
102   JDIMENSION max_scans;         /* abort if the number of scans exceeds this
103                                    value and the value is non-zero */
104   boolean report;               /* whether or not to report progress */
105   /* last printed percentage stored here to avoid multiple printouts */
106   int percent_done;
107 };
108
109 typedef struct cdjpeg_progress_mgr *cd_progress_ptr;
110
111
112 /* Module selection routines for I/O modules. */
113
114 EXTERN(cjpeg_source_ptr) jinit_read_bmp(j_compress_ptr cinfo,
115                                         boolean use_inversion_array);
116 EXTERN(djpeg_dest_ptr) jinit_write_bmp(j_decompress_ptr cinfo, boolean is_os2,
117                                        boolean use_inversion_array);
118 EXTERN(cjpeg_source_ptr) jinit_read_gif(j_compress_ptr cinfo);
119 EXTERN(cjpeg_source_ptr) j12init_read_gif(j_compress_ptr cinfo);
120 #ifdef C_LOSSLESS_SUPPORTED
121 EXTERN(cjpeg_source_ptr) j16init_read_gif(j_compress_ptr cinfo);
122 #endif
123 EXTERN(djpeg_dest_ptr) jinit_write_gif(j_decompress_ptr cinfo, boolean is_lzw);
124 EXTERN(djpeg_dest_ptr) j12init_write_gif(j_decompress_ptr cinfo,
125                                          boolean is_lzw);
126 EXTERN(cjpeg_source_ptr) jinit_read_ppm(j_compress_ptr cinfo);
127 EXTERN(cjpeg_source_ptr) j12init_read_ppm(j_compress_ptr cinfo);
128 #ifdef C_LOSSLESS_SUPPORTED
129 EXTERN(cjpeg_source_ptr) j16init_read_ppm(j_compress_ptr cinfo);
130 #endif
131 EXTERN(djpeg_dest_ptr) jinit_write_ppm(j_decompress_ptr cinfo);
132 EXTERN(djpeg_dest_ptr) j12init_write_ppm(j_decompress_ptr cinfo);
133 #ifdef D_LOSSLESS_SUPPORTED
134 EXTERN(djpeg_dest_ptr) j16init_write_ppm(j_decompress_ptr cinfo);
135 #endif
136 EXTERN(cjpeg_source_ptr) jinit_read_targa(j_compress_ptr cinfo);
137 EXTERN(djpeg_dest_ptr) jinit_write_targa(j_decompress_ptr cinfo);
138
139 /* cjpeg support routines (in rdswitch.c) */
140
141 EXTERN(boolean) read_quant_tables(j_compress_ptr cinfo, char *filename,
142                                   boolean force_baseline);
143 EXTERN(boolean) read_scan_script(j_compress_ptr cinfo, char *filename);
144 EXTERN(boolean) set_quality_ratings(j_compress_ptr cinfo, char *arg,
145                                     boolean force_baseline);
146 EXTERN(boolean) set_quant_slots(j_compress_ptr cinfo, char *arg);
147 EXTERN(boolean) set_sample_factors(j_compress_ptr cinfo, char *arg);
148
149 /* djpeg support routines (in rdcolmap.c) */
150
151 EXTERN(void) read_color_map(j_decompress_ptr cinfo, FILE *infile);
152 EXTERN(void) read_color_map_12(j_decompress_ptr cinfo, FILE *infile);
153
154 /* common support routines (in cdjpeg.c) */
155
156 EXTERN(void) start_progress_monitor(j_common_ptr cinfo,
157                                     cd_progress_ptr progress);
158 EXTERN(void) end_progress_monitor(j_common_ptr cinfo);
159 EXTERN(boolean) keymatch(char *arg, const char *keyword, int minchars);
160 EXTERN(FILE *) read_stdin(void);
161 EXTERN(FILE *) write_stdout(void);
162
163 /* miscellaneous useful macros */
164
165 #ifdef DONT_USE_B_MODE          /* define mode parameters for fopen() */
166 #define READ_BINARY     "r"
167 #define WRITE_BINARY    "w"
168 #else
169 #define READ_BINARY     "rb"
170 #define WRITE_BINARY    "wb"
171 #endif
172
173 #ifndef EXIT_FAILURE            /* define exit() codes if not provided */
174 #define EXIT_FAILURE  1
175 #endif
176 #ifndef EXIT_SUCCESS
177 #define EXIT_SUCCESS  0
178 #endif
179 #ifndef EXIT_WARNING
180 #define EXIT_WARNING  2
181 #endif