4 * Copyright (C) 1994-1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains application interface code for the decompression half
9 * of the JPEG library. These are the "minimum" API routines that may be
10 * needed in either the normal full-decompression case or the
11 * transcoding-only case.
13 * Most of the routines intended to be called directly by an application
14 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
15 * shared by compression and decompression, and jdtrans.c for the transcoding
19 #define JPEG_INTERNALS
23 #ifdef HAVE_MMX_INTEL_MNEMONICS
25 static int mmxsupport();
28 #ifdef HAVE_SSE2_INTEL_MNEMONICS
29 int SSE2Available
= 0;
30 static int sse2support();
35 * Initialization of a JPEG decompression object.
36 * The error manager must already be set up (in case memory manager fails).
40 jpeg_CreateDecompress (j_decompress_ptr cinfo
, int version
, size_t structsize
)
44 #ifdef HAVE_MMX_INTEL_MNEMONICS
45 static int cpuidDetected
= 0;
49 MMXAvailable
= mmxsupport();
51 #ifdef HAVE_SSE2_INTEL_MNEMONICS
52 /* only do the sse2 support check if mmx is supported (so
53 we know the processor supports cpuid) */
55 SSE2Available
= sse2support();
62 /* For debugging purposes, zero the whole master structure.
63 * But error manager pointer is already there, so save and restore it.
66 /* Guard against version mismatches between library and caller. */
67 cinfo
->mem
= NULL
; /* so jpeg_destroy knows mem mgr not called */
68 if (version
!= JPEG_LIB_VERSION
)
69 ERREXIT2(cinfo
, JERR_BAD_LIB_VERSION
, JPEG_LIB_VERSION
, version
);
70 if (structsize
!= SIZEOF(struct jpeg_decompress_struct
))
71 ERREXIT2(cinfo
, JERR_BAD_STRUCT_SIZE
,
72 (int) SIZEOF(struct jpeg_decompress_struct
), (int) structsize
);
74 /* For debugging purposes, we zero the whole master structure.
75 * But the application has already set the err pointer, and may have set
76 * client_data, so we have to save and restore those fields.
77 * Note: if application hasn't set client_data, tools like Purify may
81 struct jpeg_error_mgr
* err
= cinfo
->err
;
82 void * client_data
= cinfo
->client_data
; /* ignore Purify complaint here */
83 MEMZERO(cinfo
, SIZEOF(struct jpeg_decompress_struct
));
85 cinfo
->client_data
= client_data
;
87 cinfo
->is_decompressor
= TRUE
;
89 /* Initialize a memory manager instance for this object */
90 jinit_memory_mgr((j_common_ptr
) cinfo
);
92 /* Zero out pointers to permanent structures. */
93 cinfo
->progress
= NULL
;
96 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++)
97 cinfo
->quant_tbl_ptrs
[i
] = NULL
;
99 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
100 cinfo
->dc_huff_tbl_ptrs
[i
] = NULL
;
101 cinfo
->ac_huff_tbl_ptrs
[i
] = NULL
;
104 /* Initialize marker processor so application can override methods
105 * for COM, APPn markers before calling jpeg_read_header.
107 cinfo
->marker_list
= NULL
;
108 jinit_marker_reader(cinfo
);
110 /* And initialize the overall input controller. */
111 jinit_input_controller(cinfo
);
114 cinfo
->global_state
= DSTATE_START
;
119 * Destruction of a JPEG decompression object
123 jpeg_destroy_decompress (j_decompress_ptr cinfo
)
125 jpeg_destroy((j_common_ptr
) cinfo
); /* use common routine */
130 * Abort processing of a JPEG decompression operation,
131 * but don't destroy the object itself.
135 jpeg_abort_decompress (j_decompress_ptr cinfo
)
137 jpeg_abort((j_common_ptr
) cinfo
); /* use common routine */
141 * Set default decompression parameters.
145 default_decompress_parms (j_decompress_ptr cinfo
)
147 /* Guess the input colorspace, and set output colorspace accordingly. */
148 /* (Wish JPEG committee had provided a real way to specify this...) */
149 /* Note application may override our guesses. */
150 switch (cinfo
->num_components
) {
152 cinfo
->jpeg_color_space
= JCS_GRAYSCALE
;
153 cinfo
->out_color_space
= JCS_GRAYSCALE
;
157 if (cinfo
->saw_JFIF_marker
) {
158 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* JFIF implies YCbCr */
159 } else if (cinfo
->saw_Adobe_marker
) {
160 switch (cinfo
->Adobe_transform
) {
162 cinfo
->jpeg_color_space
= JCS_RGB
;
165 cinfo
->jpeg_color_space
= JCS_YCbCr
;
168 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
169 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
173 /* Saw no special markers, try to guess from the component IDs */
174 int cid0
= cinfo
->comp_info
[0].component_id
;
175 int cid1
= cinfo
->comp_info
[1].component_id
;
176 int cid2
= cinfo
->comp_info
[2].component_id
;
178 if (cid0
== 1 && cid1
== 2 && cid2
== 3)
179 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume JFIF w/out marker */
180 else if (cid0
== 82 && cid1
== 71 && cid2
== 66)
181 cinfo
->jpeg_color_space
= JCS_RGB
; /* ASCII 'R', 'G', 'B' */
183 TRACEMS3(cinfo
, 1, JTRC_UNKNOWN_IDS
, cid0
, cid1
, cid2
);
184 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
187 /* Always guess RGB is proper output colorspace. */
188 cinfo
->out_color_space
= JCS_RGB
;
192 if (cinfo
->saw_Adobe_marker
) {
193 switch (cinfo
->Adobe_transform
) {
195 cinfo
->jpeg_color_space
= JCS_CMYK
;
198 cinfo
->jpeg_color_space
= JCS_YCCK
;
201 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
202 cinfo
->jpeg_color_space
= JCS_YCCK
; /* assume it's YCCK */
206 /* No special markers, assume straight CMYK. */
207 cinfo
->jpeg_color_space
= JCS_CMYK
;
209 cinfo
->out_color_space
= JCS_CMYK
;
213 cinfo
->jpeg_color_space
= JCS_UNKNOWN
;
214 cinfo
->out_color_space
= JCS_UNKNOWN
;
218 /* Set defaults for other decompression parameters. */
219 cinfo
->scale_num
= 1; /* 1:1 scaling */
220 cinfo
->scale_denom
= 1;
221 cinfo
->output_gamma
= 1.0;
222 cinfo
->buffered_image
= FALSE
;
223 cinfo
->raw_data_out
= FALSE
;
224 cinfo
->dct_method
= JDCT_DEFAULT
;
225 cinfo
->do_fancy_upsampling
= TRUE
;
226 cinfo
->do_block_smoothing
= TRUE
;
227 cinfo
->quantize_colors
= FALSE
;
228 /* We set these in case application only sets quantize_colors. */
229 cinfo
->dither_mode
= JDITHER_FS
;
230 #ifdef QUANT_2PASS_SUPPORTED
231 cinfo
->two_pass_quantize
= TRUE
;
233 cinfo
->two_pass_quantize
= FALSE
;
235 cinfo
->desired_number_of_colors
= 256;
236 cinfo
->colormap
= NULL
;
237 /* Initialize for no mode change in buffered-image mode. */
238 cinfo
->enable_1pass_quant
= FALSE
;
239 cinfo
->enable_external_quant
= FALSE
;
240 cinfo
->enable_2pass_quant
= FALSE
;
245 * Decompression startup: read start of JPEG datastream to see what's there.
246 * Need only initialize JPEG object and supply a data source before calling.
248 * This routine will read as far as the first SOS marker (ie, actual start of
249 * compressed data), and will save all tables and parameters in the JPEG
250 * object. It will also initialize the decompression parameters to default
251 * values, and finally return JPEG_HEADER_OK. On return, the application may
252 * adjust the decompression parameters and then call jpeg_start_decompress.
253 * (Or, if the application only wanted to determine the image parameters,
254 * the data need not be decompressed. In that case, call jpeg_abort or
255 * jpeg_destroy to release any temporary space.)
256 * If an abbreviated (tables only) datastream is presented, the routine will
257 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
258 * re-use the JPEG object to read the abbreviated image datastream(s).
259 * It is unnecessary (but OK) to call jpeg_abort in this case.
260 * The JPEG_SUSPENDED return code only occurs if the data source module
261 * requests suspension of the decompressor. In this case the application
262 * should load more source data and then re-call jpeg_read_header to resume
264 * If a non-suspending data source is used and require_image is TRUE, then the
265 * return code need not be inspected since only JPEG_HEADER_OK is possible.
267 * This routine is now just a front end to jpeg_consume_input, with some
268 * extra error checking.
272 jpeg_read_header (j_decompress_ptr cinfo
, boolean require_image
)
276 if (cinfo
->global_state
!= DSTATE_START
&&
277 cinfo
->global_state
!= DSTATE_INHEADER
)
278 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
280 retcode
= jpeg_consume_input(cinfo
);
283 case JPEG_REACHED_SOS
:
284 retcode
= JPEG_HEADER_OK
;
286 case JPEG_REACHED_EOI
:
287 if (require_image
) /* Complain if application wanted an image */
288 ERREXIT(cinfo
, JERR_NO_IMAGE
);
289 /* Reset to start state; it would be safer to require the application to
290 * call jpeg_abort, but we can't change it now for compatibility reasons.
291 * A side effect is to free any temporary memory (there shouldn't be any).
293 jpeg_abort((j_common_ptr
) cinfo
); /* sets state = DSTATE_START */
294 retcode
= JPEG_HEADER_TABLES_ONLY
;
306 * Consume data in advance of what the decompressor requires.
307 * This can be called at any time once the decompressor object has
308 * been created and a data source has been set up.
310 * This routine is essentially a state machine that handles a couple
311 * of critical state-transition actions, namely initial setup and
312 * transition from header scanning to ready-for-start_decompress.
313 * All the actual input is done via the input controller's consume_input
318 jpeg_consume_input (j_decompress_ptr cinfo
)
320 int retcode
= JPEG_SUSPENDED
;
322 /* NB: every possible DSTATE value should be listed in this switch */
323 switch (cinfo
->global_state
) {
325 /* Start-of-datastream actions: reset appropriate modules */
326 (*cinfo
->inputctl
->reset_input_controller
) (cinfo
);
327 /* Initialize application's data source module */
328 (*cinfo
->src
->init_source
) (cinfo
);
329 cinfo
->global_state
= DSTATE_INHEADER
;
331 case DSTATE_INHEADER
:
332 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
333 if (retcode
== JPEG_REACHED_SOS
) { /* Found SOS, prepare to decompress */
334 /* Set up default parameters based on header data */
335 default_decompress_parms(cinfo
);
336 /* Set global state: ready for start_decompress */
337 cinfo
->global_state
= DSTATE_READY
;
341 /* Can't advance past first SOS until start_decompress is called */
342 retcode
= JPEG_REACHED_SOS
;
346 case DSTATE_SCANNING
:
348 case DSTATE_BUFIMAGE
:
350 case DSTATE_STOPPING
:
351 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
354 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
361 * Have we finished reading the input file?
365 jpeg_input_complete (j_decompress_ptr cinfo
)
367 /* Check for valid jpeg object */
368 if (cinfo
->global_state
< DSTATE_START
||
369 cinfo
->global_state
> DSTATE_STOPPING
)
370 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
371 return cinfo
->inputctl
->eoi_reached
;
376 * Is there more than one scan?
380 jpeg_has_multiple_scans (j_decompress_ptr cinfo
)
382 /* Only valid after jpeg_read_header completes */
383 if (cinfo
->global_state
< DSTATE_READY
||
384 cinfo
->global_state
> DSTATE_STOPPING
)
385 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
386 return cinfo
->inputctl
->has_multiple_scans
;
391 * Finish JPEG decompression.
393 * This will normally just verify the file trailer and release temp storage.
395 * Returns FALSE if suspended. The return value need be inspected only if
396 * a suspending data source is used.
400 jpeg_finish_decompress (j_decompress_ptr cinfo
)
402 if ((cinfo
->global_state
== DSTATE_SCANNING
||
403 cinfo
->global_state
== DSTATE_RAW_OK
) && ! cinfo
->buffered_image
) {
404 /* Terminate final pass of non-buffered mode */
405 if (cinfo
->output_scanline
< cinfo
->output_height
)
406 ERREXIT(cinfo
, JERR_TOO_LITTLE_DATA
);
407 (*cinfo
->master
->finish_output_pass
) (cinfo
);
408 cinfo
->global_state
= DSTATE_STOPPING
;
409 } else if (cinfo
->global_state
== DSTATE_BUFIMAGE
) {
410 /* Finishing after a buffered-image operation */
411 cinfo
->global_state
= DSTATE_STOPPING
;
412 } else if (cinfo
->global_state
!= DSTATE_STOPPING
) {
413 /* STOPPING = repeat call after a suspension, anything else is error */
414 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
417 while (! cinfo
->inputctl
->eoi_reached
) {
418 if ((*cinfo
->inputctl
->consume_input
) (cinfo
) == JPEG_SUSPENDED
)
419 return FALSE
; /* Suspend, come back later */
421 /* Do final cleanup */
422 (*cinfo
->src
->term_source
) (cinfo
);
423 /* We can use jpeg_abort to release memory and reset global_state */
424 jpeg_abort((j_common_ptr
) cinfo
);
429 #ifdef HAVE_MMX_INTEL_MNEMONICS
432 static int mmxsupport()
434 int mmx_supported
= 0;
437 pushfd
//Save Eflag to stack
438 pop eax
//Get Eflag from stack into eax
439 mov ecx
, eax
//Make another copy of Eflag in ecx
440 xor eax
, 0x200000 //Toggle ID bit in Eflag [i.e. bit(21)]
441 push eax
//Save modified Eflag back to stack
443 popfd
//Restored modified value back to Eflag reg
444 pushfd
//Save Eflag to stack
445 pop eax
//Get Eflag from stack
446 xor eax
, ecx
//Compare the new Eflag with the original Eflag
447 jz NOT_SUPPORTED
//If the same, CPUID instruction is not supported,
448 //skip following instructions and jump to
449 //NOT_SUPPORTED label
451 xor eax
, eax
//Set eax to zero
455 cmp eax
, 1 //make sure eax return non-zero value
456 jl NOT_SUPPORTED
//If eax is zero, mmx not supported
458 xor eax
, eax
//set eax to zero
459 inc eax
//Now increment eax to 1. This instruction is
460 //faster than the instruction "mov eax, 1"
464 and edx
, 0x00800000 //mask out all bits but mmx bit(24)
465 cmp edx
, 0 // 0 = mmx not supported
466 jz NOT_SUPPORTED
// non-zero = Yes, mmx IS supported
468 mov mmx_supported
, 1 //set return value to 1
471 mov eax
, mmx_supported
//move return value to eax
475 return mmx_supported
;
479 #ifdef HAVE_SSE2_INTEL_MNEMONICS
481 static int sse2support()
483 int sse2available
= 0;
491 if (my_edx
& (0x1 << 26))
493 else sse2available
= 2;
495 return sse2available
;