DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / JpegLib / imjdapimin.pas
1 unit imjdapimin;
3 {$N+} { Nomssi: cinfo^.output_gamma }
5 { This file contains application interface code for the decompression half
6 of the JPEG library. These are the "minimum" API routines that may be
7 needed in either the normal full-decompression case or the
8 transcoding-only case.
10 Most of the routines intended to be called directly by an application
11 are in this file or in jdapistd.c. But also see jcomapi.c for routines
12 shared by compression and decompression, and jdtrans.c for the transcoding
13 case. }
15 { Original : jdapimin.c ; Copyright (C) 1994-1998, Thomas G. Lane. }
17 interface
19 {$I imjconfig.inc}
21 uses
22 imjmorecfg,
23 imjinclude,
24 imjdeferr,
25 imjerror,
26 imjpeglib,
27 imjmemmgr, imjdmarker, imjdinput, imjcomapi;
29 { Nomssi }
30 procedure jpeg_create_decompress(cinfo : j_decompress_ptr);
32 { Initialization of a JPEG decompression object.
33 The error manager must already be set up (in case memory manager fails). }
35 {GLOBAL}
36 procedure jpeg_CreateDecompress (cinfo : j_decompress_ptr;
37 version : int;
38 structsize : size_t);
40 { Destruction of a JPEG decompression object }
42 {GLOBAL}
43 procedure jpeg_destroy_decompress (cinfo : j_decompress_ptr);
46 { Decompression startup: read start of JPEG datastream to see what's there.
47 Need only initialize JPEG object and supply a data source before calling.
49 This routine will read as far as the first SOS marker (ie, actual start of
50 compressed data), and will save all tables and parameters in the JPEG
51 object. It will also initialize the decompression parameters to default
52 values, and finally return JPEG_HEADER_OK. On return, the application may
53 adjust the decompression parameters and then call jpeg_start_decompress.
54 (Or, if the application only wanted to determine the image parameters,
55 the data need not be decompressed. In that case, call jpeg_abort or
56 jpeg_destroy to release any temporary space.)
57 If an abbreviated (tables only) datastream is presented, the routine will
58 return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
59 re-use the JPEG object to read the abbreviated image datastream(s).
60 It is unnecessary (but OK) to call jpeg_abort in this case.
61 The JPEG_SUSPENDED return code only occurs if the data source module
62 requests suspension of the decompressor. In this case the application
63 should load more source data and then re-call jpeg_read_header to resume
64 processing.
65 If a non-suspending data source is used and require_image is TRUE, then the
66 return code need not be inspected since only JPEG_HEADER_OK is possible.
68 This routine is now just a front end to jpeg_consume_input, with some
69 extra error checking. }
71 {GLOBAL}
72 function jpeg_read_header (cinfo : j_decompress_ptr;
73 require_image : boolean) : int;
75 { Consume data in advance of what the decompressor requires.
76 This can be called at any time once the decompressor object has
77 been created and a data source has been set up.
79 This routine is essentially a state machine that handles a couple
80 of critical state-transition actions, namely initial setup and
81 transition from header scanning to ready-for-start_decompress.
82 All the actual input is done via the input controller's consume_input
83 method. }
85 {GLOBAL}
86 function jpeg_consume_input (cinfo : j_decompress_ptr) : int;
88 { Have we finished reading the input file? }
90 {GLOBAL}
91 function jpeg_input_complete (cinfo : j_decompress_ptr) : boolean;
93 { Is there more than one scan? }
95 {GLOBAL}
96 function jpeg_has_multiple_scans (cinfo : j_decompress_ptr) : boolean;
99 { Finish JPEG decompression.
101 This will normally just verify the file trailer and release temp storage.
103 Returns FALSE if suspended. The return value need be inspected only if
104 a suspending data source is used. }
106 {GLOBAL}
107 function jpeg_finish_decompress (cinfo : j_decompress_ptr) : boolean;
109 implementation
111 procedure jpeg_create_decompress(cinfo : j_decompress_ptr);
112 begin
113 jpeg_CreateDecompress(cinfo, JPEG_LIB_VERSION,
114 size_t(sizeof(jpeg_decompress_struct)));
115 end;
117 { Initialization of a JPEG decompression object.
118 The error manager must already be set up (in case memory manager fails). }
120 {GLOBAL}
121 procedure jpeg_CreateDecompress (cinfo : j_decompress_ptr;
122 version : int;
123 structsize : size_t);
124 var
125 i : int;
126 var
127 err : jpeg_error_mgr_ptr;
128 client_data : voidp;
129 begin
130 { Guard against version mismatches between library and caller. }
131 cinfo^.mem := NIL; { so jpeg_destroy knows mem mgr not called }
132 if (version <> JPEG_LIB_VERSION) then
133 ERREXIT2(j_common_ptr(cinfo), JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
134 if (structsize <> SIZEOF(jpeg_decompress_struct)) then
135 ERREXIT2(j_common_ptr(cinfo), JERR_BAD_STRUCT_SIZE,
136 int(SIZEOF(jpeg_decompress_struct)), int(structsize));
138 { For debugging purposes, we zero the whole master structure.
139 But the application has already set the err pointer, and may have set
140 client_data, so we have to save and restore those fields.
141 Note: if application hasn't set client_data, tools like Purify may
142 complain here. }
143 begin
144 err := cinfo^.err;
145 client_data := cinfo^.client_data; { ignore Purify complaint here }
146 MEMZERO(j_common_ptr(cinfo), SIZEOF(jpeg_decompress_struct));
147 cinfo^.err := err;
148 cinfo^.client_data := client_data;
149 end;
150 cinfo^.is_decompressor := TRUE;
152 { Initialize a memory manager instance for this object }
153 jinit_memory_mgr(j_common_ptr(cinfo));
155 { Zero out pointers to permanent structures. }
156 cinfo^.progress := NIL;
157 cinfo^.src := NIL;
159 for i := 0 to pred(NUM_QUANT_TBLS) do
160 cinfo^.quant_tbl_ptrs[i] := NIL;
162 for i := 0 to pred(NUM_HUFF_TBLS) do
163 begin
164 cinfo^.dc_huff_tbl_ptrs[i] := NIL;
165 cinfo^.ac_huff_tbl_ptrs[i] := NIL;
166 end;
168 { Initialize marker processor so application can override methods
169 for COM, APPn markers before calling jpeg_read_header. }
170 cinfo^.marker_list := NIL;
171 jinit_marker_reader(cinfo);
173 { And initialize the overall input controller. }
174 jinit_input_controller(cinfo);
176 { OK, I'm ready }
177 cinfo^.global_state := DSTATE_START;
178 end;
181 { Destruction of a JPEG decompression object }
183 {GLOBAL}
184 procedure jpeg_destroy_decompress (cinfo : j_decompress_ptr);
185 begin
186 jpeg_destroy(j_common_ptr(cinfo)); { use common routine }
187 end;
190 { Abort processing of a JPEG decompression operation,
191 but don't destroy the object itself. }
193 {GLOBAL}
194 procedure jpeg_abort_decompress (cinfo : j_decompress_ptr);
195 begin
196 jpeg_abort(j_common_ptr(cinfo)); { use common routine }
197 end;
200 { Set default decompression parameters. }
202 {LOCAL}
203 procedure default_decompress_parms (cinfo : j_decompress_ptr);
204 var
205 cid0 : int;
206 cid1 : int;
207 cid2 : int;
208 begin
209 { Guess the input colorspace, and set output colorspace accordingly. }
210 { (Wish JPEG committee had provided a real way to specify this...) }
211 { Note application may override our guesses. }
212 case (cinfo^.num_components) of
213 1: begin
214 cinfo^.jpeg_color_space := JCS_GRAYSCALE;
215 cinfo^.out_color_space := JCS_GRAYSCALE;
216 end;
218 3: begin
219 if (cinfo^.saw_JFIF_marker) then
220 begin
221 cinfo^.jpeg_color_space := JCS_YCbCr; { JFIF implies YCbCr }
222 end
223 else
224 if (cinfo^.saw_Adobe_marker) then
225 begin
226 case (cinfo^.Adobe_transform) of
227 0: cinfo^.jpeg_color_space := JCS_RGB;
228 1: cinfo^.jpeg_color_space := JCS_YCbCr;
229 else
230 begin
231 WARNMS1(j_common_ptr(cinfo), JWRN_ADOBE_XFORM, cinfo^.Adobe_transform);
232 cinfo^.jpeg_color_space := JCS_YCbCr; { assume it's YCbCr }
233 end;
234 end;
235 end
236 else
237 begin
238 { Saw no special markers, try to guess from the component IDs }
239 cid0 := cinfo^.comp_info^[0].component_id;
240 cid1 := cinfo^.comp_info^[1].component_id;
241 cid2 := cinfo^.comp_info^[2].component_id;
243 if (cid0 = 1) and (cid1 = 2) and (cid2 = 3) then
244 cinfo^.jpeg_color_space := JCS_YCbCr { assume JFIF w/out marker }
245 else
246 if (cid0 = 82) and (cid1 = 71) and (cid2 = 66) then
247 cinfo^.jpeg_color_space := JCS_RGB { ASCII 'R', 'G', 'B' }
248 else
249 begin
250 {$IFDEF DEBUG}
251 TRACEMS3(j_common_ptr(cinfo), 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
252 {$ENDIF}
253 cinfo^.jpeg_color_space := JCS_YCbCr; { assume it's YCbCr }
254 end;
255 end;
256 { Always guess RGB is proper output colorspace. }
257 cinfo^.out_color_space := JCS_RGB;
258 end;
260 4: begin
261 if (cinfo^.saw_Adobe_marker) then
262 begin
263 case (cinfo^.Adobe_transform) of
264 0: cinfo^.jpeg_color_space := JCS_CMYK;
265 2: cinfo^.jpeg_color_space := JCS_YCCK;
266 else
267 begin
268 WARNMS1(j_common_ptr(cinfo), JWRN_ADOBE_XFORM, cinfo^.Adobe_transform);
269 cinfo^.jpeg_color_space := JCS_YCCK; { assume it's YCCK }
270 end;
271 end;
272 end
273 else
274 begin
275 { No special markers, assume straight CMYK. }
276 cinfo^.jpeg_color_space := JCS_CMYK;
277 end;
278 cinfo^.out_color_space := JCS_CMYK;
279 end;
281 else
282 begin
283 cinfo^.jpeg_color_space := JCS_UNKNOWN;
284 cinfo^.out_color_space := JCS_UNKNOWN;
285 end;
286 end;
288 { Set defaults for other decompression parameters. }
289 cinfo^.scale_num := 1; { 1:1 scaling }
290 cinfo^.scale_denom := 1;
291 cinfo^.output_gamma := 1.0;
292 cinfo^.buffered_image := FALSE;
293 cinfo^.raw_data_out := FALSE;
294 cinfo^.dct_method := JDCT_DEFAULT;
295 cinfo^.do_fancy_upsampling := TRUE;
296 cinfo^.do_block_smoothing := TRUE;
297 cinfo^.quantize_colors := FALSE;
298 { We set these in case application only sets quantize_colors. }
299 cinfo^.dither_mode := JDITHER_FS;
300 {$ifdef QUANT_2PASS_SUPPORTED}
301 cinfo^.two_pass_quantize := TRUE;
302 {$else}
303 cinfo^.two_pass_quantize := FALSE;
304 {$endif}
305 cinfo^.desired_number_of_colors := 256;
306 cinfo^.colormap := NIL;
307 { Initialize for no mode change in buffered-image mode. }
308 cinfo^.enable_1pass_quant := FALSE;
309 cinfo^.enable_external_quant := FALSE;
310 cinfo^.enable_2pass_quant := FALSE;
311 end;
314 { Decompression startup: read start of JPEG datastream to see what's there.
315 Need only initialize JPEG object and supply a data source before calling.
317 This routine will read as far as the first SOS marker (ie, actual start of
318 compressed data), and will save all tables and parameters in the JPEG
319 object. It will also initialize the decompression parameters to default
320 values, and finally return JPEG_HEADER_OK. On return, the application may
321 adjust the decompression parameters and then call jpeg_start_decompress.
322 (Or, if the application only wanted to determine the image parameters,
323 the data need not be decompressed. In that case, call jpeg_abort or
324 jpeg_destroy to release any temporary space.)
325 If an abbreviated (tables only) datastream is presented, the routine will
326 return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
327 re-use the JPEG object to read the abbreviated image datastream(s).
328 It is unnecessary (but OK) to call jpeg_abort in this case.
329 The JPEG_SUSPENDED return code only occurs if the data source module
330 requests suspension of the decompressor. In this case the application
331 should load more source data and then re-call jpeg_read_header to resume
332 processing.
333 If a non-suspending data source is used and require_image is TRUE, then the
334 return code need not be inspected since only JPEG_HEADER_OK is possible.
336 This routine is now just a front end to jpeg_consume_input, with some
337 extra error checking. }
339 {GLOBAL}
340 function jpeg_read_header (cinfo : j_decompress_ptr;
341 require_image : boolean) : int;
342 var
343 retcode : int;
344 begin
345 if (cinfo^.global_state <> DSTATE_START) and
346 (cinfo^.global_state <> DSTATE_INHEADER) then
347 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
349 retcode := jpeg_consume_input(cinfo);
351 case (retcode) of
352 JPEG_REACHED_SOS:
353 retcode := JPEG_HEADER_OK;
354 JPEG_REACHED_EOI:
355 begin
356 if (require_image) then { Complain if application wanted an image }
357 ERREXIT(j_common_ptr(cinfo), JERR_NO_IMAGE);
358 { Reset to start state; it would be safer to require the application to
359 call jpeg_abort, but we can't change it now for compatibility reasons.
360 A side effect is to free any temporary memory (there shouldn't be any). }
362 jpeg_abort(j_common_ptr(cinfo)); { sets state := DSTATE_START }
363 retcode := JPEG_HEADER_TABLES_ONLY;
364 end;
365 JPEG_SUSPENDED: ; { no work }
366 end;
368 jpeg_read_header := retcode;
369 end;
372 { Consume data in advance of what the decompressor requires.
373 This can be called at any time once the decompressor object has
374 been created and a data source has been set up.
376 This routine is essentially a state machine that handles a couple
377 of critical state-transition actions, namely initial setup and
378 transition from header scanning to ready-for-start_decompress.
379 All the actual input is done via the input controller's consume_input
380 method. }
382 {GLOBAL}
383 function jpeg_consume_input (cinfo : j_decompress_ptr) : int;
384 var
385 retcode : int;
386 begin
387 retcode := JPEG_SUSPENDED;
389 { NB: every possible DSTATE value should be listed in this switch }
391 if (cinfo^.global_state) = DSTATE_START then
392 begin {work around the FALLTHROUGH}
393 { Start-of-datastream actions: reset appropriate modules }
394 cinfo^.inputctl^.reset_input_controller (cinfo);
395 { Initialize application's data source module }
396 cinfo^.src^.init_source (cinfo);
397 cinfo^.global_state := DSTATE_INHEADER;
398 end;
400 case (cinfo^.global_state) of
401 DSTATE_START,
402 DSTATE_INHEADER:
403 begin
404 retcode := cinfo^.inputctl^.consume_input (cinfo);
405 if (retcode = JPEG_REACHED_SOS) then
406 begin { Found SOS, prepare to decompress }
407 { Set up default parameters based on header data }
408 default_decompress_parms(cinfo);
409 { Set global state: ready for start_decompress }
410 cinfo^.global_state := DSTATE_READY;
411 end;
412 end;
413 DSTATE_READY:
414 { Can't advance past first SOS until start_decompress is called }
415 retcode := JPEG_REACHED_SOS;
417 DSTATE_PRELOAD,
418 DSTATE_PRESCAN,
419 DSTATE_SCANNING,
420 DSTATE_RAW_OK,
421 DSTATE_BUFIMAGE,
422 DSTATE_BUFPOST,
423 DSTATE_STOPPING:
424 retcode := cinfo^.inputctl^.consume_input (cinfo);
425 else
426 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
427 end;
428 jpeg_consume_input := retcode;
429 end;
432 { Have we finished reading the input file? }
434 {GLOBAL}
435 function jpeg_input_complete (cinfo : j_decompress_ptr) : boolean;
436 begin
437 { Check for valid jpeg object }
438 if (cinfo^.global_state < DSTATE_START) or
439 (cinfo^.global_state > DSTATE_STOPPING) then
440 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
441 jpeg_input_complete := cinfo^.inputctl^.eoi_reached;
442 end;
445 { Is there more than one scan? }
447 {GLOBAL}
448 function jpeg_has_multiple_scans (cinfo : j_decompress_ptr) : boolean;
449 begin
450 { Only valid after jpeg_read_header completes }
451 if (cinfo^.global_state < DSTATE_READY) or
452 (cinfo^.global_state > DSTATE_STOPPING) then
453 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
454 jpeg_has_multiple_scans := cinfo^.inputctl^.has_multiple_scans;
455 end;
458 { Finish JPEG decompression.
460 This will normally just verify the file trailer and release temp storage.
462 Returns FALSE if suspended. The return value need be inspected only if
463 a suspending data source is used. }
465 {GLOBAL}
466 function jpeg_finish_decompress (cinfo : j_decompress_ptr) : boolean;
467 begin
468 if ((cinfo^.global_state = DSTATE_SCANNING) or
469 (cinfo^.global_state = DSTATE_RAW_OK) and (not cinfo^.buffered_image)) then
470 begin
471 { Terminate final pass of non-buffered mode }
472 if (cinfo^.output_scanline < cinfo^.output_height) then
473 ERREXIT(j_common_ptr(cinfo), JERR_TOO_LITTLE_DATA);
474 cinfo^.master^.finish_output_pass (cinfo);
475 cinfo^.global_state := DSTATE_STOPPING;
476 end
477 else
478 if (cinfo^.global_state = DSTATE_BUFIMAGE) then
479 begin
480 { Finishing after a buffered-image operation }
481 cinfo^.global_state := DSTATE_STOPPING;
482 end
483 else
484 if (cinfo^.global_state <> DSTATE_STOPPING) then
485 begin
486 { STOPPING := repeat call after a suspension, anything else is error }
487 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);
488 end;
489 { Read until EOI }
490 while (not cinfo^.inputctl^.eoi_reached) do
491 begin
492 if (cinfo^.inputctl^.consume_input (cinfo) = JPEG_SUSPENDED) then
493 begin
494 jpeg_finish_decompress := FALSE; { Suspend, come back later }
495 exit;
496 end;
497 end;
498 { Do final cleanup }
499 cinfo^.src^.term_source (cinfo);
500 { We can use jpeg_abort to release memory and reset global_state }
501 jpeg_abort(j_common_ptr(cinfo));
502 jpeg_finish_decompress := TRUE;
503 end;
505 end.