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,
29 { Nomssi }
32 { Initialization of a JPEG decompression object.
33 The error manager must already be set up (in case memory manager fails). }
35 {GLOBAL}
40 { Destruction of a JPEG decompression object }
42 {GLOBAL}
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}
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}
88 { Have we finished reading the input file? }
90 {GLOBAL}
93 { Is there more than one scan? }
95 {GLOBAL}
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}
109 implementation
112 begin
117 { Initialization of a JPEG decompression object.
118 The error manager must already be set up (in case memory manager fails). }
120 {GLOBAL}
124 var
126 var
129 begin
130 { Guard against version mismatches between library and caller. }
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
152 { Initialize a memory manager instance for this object }
155 { Zero out pointers to permanent structures. }
163 begin
168 { Initialize marker processor so application can override methods
169 for COM, APPn markers before calling jpeg_read_header. }
173 { And initialize the overall input controller. }
176 { OK, I'm ready }
181 { Destruction of a JPEG decompression object }
183 {GLOBAL}
185 begin
190 { Abort processing of a JPEG decompression operation,
191 but don't destroy the object itself. }
193 {GLOBAL}
195 begin
200 { Set default decompression parameters. }
202 {LOCAL}
204 var
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. }
220 begin
222 end
223 else
225 begin
229 else
230 begin
235 end
236 else
237 begin
238 { Saw no special markers, try to guess from the component IDs }
245 else
248 else
249 begin
250 {$IFDEF DEBUG}
252 {$ENDIF}
256 { Always guess RGB is proper output colorspace. }
262 begin
266 else
267 begin
272 end
273 else
274 begin
275 { No special markers, assume straight CMYK. }
281 else
282 begin
288 { Set defaults for other decompression parameters. }
298 { We set these in case application only sets quantize_colors. }
300 {$ifdef QUANT_2PASS_SUPPORTED}
302 {$else}
304 {$endif}
307 { Initialize for no mode change in buffered-image mode. }
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}
342 var
344 begin
352 JPEG_REACHED_SOS:
354 JPEG_REACHED_EOI:
355 begin
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). }
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}
384 var
386 begin
389 { NB: every possible DSTATE value should be listed in this switch }
393 { Start-of-datastream actions: reset appropriate modules }
395 { Initialize application's data source module }
401 DSTATE_START,
402 DSTATE_INHEADER:
403 begin
407 { Set up default parameters based on header data }
409 { Set global state: ready for start_decompress }
413 DSTATE_READY:
414 { Can't advance past first SOS until start_decompress is called }
417 DSTATE_PRELOAD,
418 DSTATE_PRESCAN,
419 DSTATE_SCANNING,
420 DSTATE_RAW_OK,
421 DSTATE_BUFIMAGE,
422 DSTATE_BUFPOST,
423 DSTATE_STOPPING:
425 else
432 { Have we finished reading the input file? }
434 {GLOBAL}
436 begin
437 { Check for valid jpeg object }
445 { Is there more than one scan? }
447 {GLOBAL}
449 begin
450 { Only valid after jpeg_read_header completes }
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}
467 begin
470 begin
471 { Terminate final pass of non-buffered mode }
476 end
477 else
479 begin
480 { Finishing after a buffered-image operation }
482 end
483 else
485 begin
486 { STOPPING := repeat call after a suspension, anything else is error }
489 { Read until EOI }
491 begin
493 begin
495 exit;
498 { Do final cleanup }
500 { We can use jpeg_abort to release memory and reset global_state }