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
27 /* no __cpuid intrinsic, use a manually rewritten replacement */
28 void __stdcall
__cpuid( int CPUInfo
[4], int InfoType
)
30 int my_eax
= 0, my_ebx
= 0, my_ecx
= 0, my_edx
= 0;
32 /* check eflags bit 21 to see if cpuid is supported */
33 pushfd
/* save eflags to stack */
34 pop eax
/* and put it in eax */
35 mov ecx
, eax
/* save a copy in ecx to compare against */
36 xor eax
, 0x200000 /* toggle ID bit (bit 21) in eflags */
37 push eax
/* save modified eflags to stack */
38 popfd
/* set eflags register with modified value */
39 pushfd
/* read eflags back out */
41 xor eax
, ecx
/* check for modified eflags */
42 jz NOT_SUPPORTED
/* cpuid not supported */
44 /* check to see if the requested cpuid type is supported */
45 xor eax
, eax
/* set eax to zero */
48 jl NOT_SUPPORTED
/* the requested cpuid type is not supported */
50 /* actually make the cpuid call */
64 #endif /* _MSC_VER >= 1400 */
67 static int mmxsupport();
70 #ifdef HAVE_SSE2_INTRINSICS
71 int SSE2Available
= 0;
72 #ifdef HAVE_SSE2_INTEL_MNEMONICS
73 static int sse2support();
75 static int sse2supportGCC();
76 #endif /* HAVE_SSE2_INTEL_MNEMONICS */
77 #endif /* HAVE_SSE2_INTRINSICS */
81 * Initialization of a JPEG decompression object.
82 * The error manager must already be set up (in case memory manager fails).
86 jpeg_CreateDecompress (j_decompress_ptr cinfo
, int version
, size_t structsize
)
90 #ifdef HAVE_MMX_INTEL_MNEMONICS
91 static int cpuidDetected
= 0;
95 MMXAvailable
= mmxsupport();
97 #ifdef HAVE_SSE2_INTEL_MNEMONICS
98 /* only do the sse2 support check if mmx is supported (so
99 we know the processor supports cpuid) */
101 SSE2Available
= sse2support();
107 #ifdef HAVE_SSE2_INTRINSICS
108 static int cpuidDetected
= 0;
111 SSE2Available
= sse2supportGCC();
115 #endif /* HAVE_SSE2_INTRINSICS */
116 #endif /* HAVE_MMX_INTEL_MNEMONICS */
118 /* For debugging purposes, zero the whole master structure.
119 * But error manager pointer is already there, so save and restore it.
122 /* Guard against version mismatches between library and caller. */
123 cinfo
->mem
= NULL
; /* so jpeg_destroy knows mem mgr not called */
124 if (version
!= JPEG_LIB_VERSION
)
125 ERREXIT2(cinfo
, JERR_BAD_LIB_VERSION
, JPEG_LIB_VERSION
, version
);
126 if (structsize
!= SIZEOF(struct jpeg_decompress_struct
))
127 ERREXIT2(cinfo
, JERR_BAD_STRUCT_SIZE
,
128 (int) SIZEOF(struct jpeg_decompress_struct
), (int) structsize
);
130 /* For debugging purposes, we zero the whole master structure.
131 * But the application has already set the err pointer, and may have set
132 * client_data, so we have to save and restore those fields.
133 * Note: if application hasn't set client_data, tools like Purify may
137 struct jpeg_error_mgr
* err
= cinfo
->err
;
138 void * client_data
= cinfo
->client_data
; /* ignore Purify complaint here */
139 MEMZERO(cinfo
, SIZEOF(struct jpeg_decompress_struct
));
141 cinfo
->client_data
= client_data
;
143 cinfo
->is_decompressor
= TRUE
;
145 /* Initialize a memory manager instance for this object */
146 jinit_memory_mgr((j_common_ptr
) cinfo
);
148 /* Zero out pointers to permanent structures. */
149 cinfo
->progress
= NULL
;
152 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++)
153 cinfo
->quant_tbl_ptrs
[i
] = NULL
;
155 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
156 cinfo
->dc_huff_tbl_ptrs
[i
] = NULL
;
157 cinfo
->ac_huff_tbl_ptrs
[i
] = NULL
;
160 /* Initialize marker processor so application can override methods
161 * for COM, APPn markers before calling jpeg_read_header.
163 cinfo
->marker_list
= NULL
;
164 jinit_marker_reader(cinfo
);
166 /* And initialize the overall input controller. */
167 jinit_input_controller(cinfo
);
170 cinfo
->global_state
= DSTATE_START
;
175 * Destruction of a JPEG decompression object
179 jpeg_destroy_decompress (j_decompress_ptr cinfo
)
181 jpeg_destroy((j_common_ptr
) cinfo
); /* use common routine */
186 * Abort processing of a JPEG decompression operation,
187 * but don't destroy the object itself.
191 jpeg_abort_decompress (j_decompress_ptr cinfo
)
193 jpeg_abort((j_common_ptr
) cinfo
); /* use common routine */
197 * Set default decompression parameters.
201 default_decompress_parms (j_decompress_ptr cinfo
)
203 /* Guess the input colorspace, and set output colorspace accordingly. */
204 /* (Wish JPEG committee had provided a real way to specify this...) */
205 /* Note application may override our guesses. */
206 switch (cinfo
->num_components
) {
208 cinfo
->jpeg_color_space
= JCS_GRAYSCALE
;
209 cinfo
->out_color_space
= JCS_GRAYSCALE
;
213 if (cinfo
->saw_JFIF_marker
) {
214 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* JFIF implies YCbCr */
215 } else if (cinfo
->saw_Adobe_marker
) {
216 switch (cinfo
->Adobe_transform
) {
218 cinfo
->jpeg_color_space
= JCS_RGB
;
221 cinfo
->jpeg_color_space
= JCS_YCbCr
;
224 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
225 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
229 /* Saw no special markers, try to guess from the component IDs */
230 int cid0
= cinfo
->comp_info
[0].component_id
;
231 int cid1
= cinfo
->comp_info
[1].component_id
;
232 int cid2
= cinfo
->comp_info
[2].component_id
;
234 if (cid0
== 1 && cid1
== 2 && cid2
== 3)
235 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume JFIF w/out marker */
236 else if (cid0
== 82 && cid1
== 71 && cid2
== 66)
237 cinfo
->jpeg_color_space
= JCS_RGB
; /* ASCII 'R', 'G', 'B' */
239 TRACEMS3(cinfo
, 1, JTRC_UNKNOWN_IDS
, cid0
, cid1
, cid2
);
240 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
243 /* Always guess RGB is proper output colorspace. */
244 cinfo
->out_color_space
= JCS_RGB
;
248 if (cinfo
->saw_Adobe_marker
) {
249 switch (cinfo
->Adobe_transform
) {
251 cinfo
->jpeg_color_space
= JCS_CMYK
;
254 cinfo
->jpeg_color_space
= JCS_YCCK
;
257 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
258 cinfo
->jpeg_color_space
= JCS_YCCK
; /* assume it's YCCK */
262 /* No special markers, assume straight CMYK. */
263 cinfo
->jpeg_color_space
= JCS_CMYK
;
265 cinfo
->out_color_space
= JCS_CMYK
;
269 cinfo
->jpeg_color_space
= JCS_UNKNOWN
;
270 cinfo
->out_color_space
= JCS_UNKNOWN
;
274 /* Set defaults for other decompression parameters. */
275 cinfo
->scale_num
= 1; /* 1:1 scaling */
276 cinfo
->scale_denom
= 1;
277 cinfo
->output_gamma
= 1.0;
278 cinfo
->buffered_image
= FALSE
;
279 cinfo
->raw_data_out
= FALSE
;
280 cinfo
->dct_method
= JDCT_DEFAULT
;
281 cinfo
->do_fancy_upsampling
= TRUE
;
282 cinfo
->do_block_smoothing
= TRUE
;
283 cinfo
->quantize_colors
= FALSE
;
284 /* We set these in case application only sets quantize_colors. */
285 cinfo
->dither_mode
= JDITHER_FS
;
286 #ifdef QUANT_2PASS_SUPPORTED
287 cinfo
->two_pass_quantize
= TRUE
;
289 cinfo
->two_pass_quantize
= FALSE
;
291 cinfo
->desired_number_of_colors
= 256;
292 cinfo
->colormap
= NULL
;
293 /* Initialize for no mode change in buffered-image mode. */
294 cinfo
->enable_1pass_quant
= FALSE
;
295 cinfo
->enable_external_quant
= FALSE
;
296 cinfo
->enable_2pass_quant
= FALSE
;
301 * Decompression startup: read start of JPEG datastream to see what's there.
302 * Need only initialize JPEG object and supply a data source before calling.
304 * This routine will read as far as the first SOS marker (ie, actual start of
305 * compressed data), and will save all tables and parameters in the JPEG
306 * object. It will also initialize the decompression parameters to default
307 * values, and finally return JPEG_HEADER_OK. On return, the application may
308 * adjust the decompression parameters and then call jpeg_start_decompress.
309 * (Or, if the application only wanted to determine the image parameters,
310 * the data need not be decompressed. In that case, call jpeg_abort or
311 * jpeg_destroy to release any temporary space.)
312 * If an abbreviated (tables only) datastream is presented, the routine will
313 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
314 * re-use the JPEG object to read the abbreviated image datastream(s).
315 * It is unnecessary (but OK) to call jpeg_abort in this case.
316 * The JPEG_SUSPENDED return code only occurs if the data source module
317 * requests suspension of the decompressor. In this case the application
318 * should load more source data and then re-call jpeg_read_header to resume
320 * If a non-suspending data source is used and require_image is TRUE, then the
321 * return code need not be inspected since only JPEG_HEADER_OK is possible.
323 * This routine is now just a front end to jpeg_consume_input, with some
324 * extra error checking.
328 jpeg_read_header (j_decompress_ptr cinfo
, boolean require_image
)
332 if (cinfo
->global_state
!= DSTATE_START
&&
333 cinfo
->global_state
!= DSTATE_INHEADER
)
334 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
336 retcode
= jpeg_consume_input(cinfo
);
339 case JPEG_REACHED_SOS
:
340 retcode
= JPEG_HEADER_OK
;
342 case JPEG_REACHED_EOI
:
343 if (require_image
) /* Complain if application wanted an image */
344 ERREXIT(cinfo
, JERR_NO_IMAGE
);
345 /* Reset to start state; it would be safer to require the application to
346 * call jpeg_abort, but we can't change it now for compatibility reasons.
347 * A side effect is to free any temporary memory (there shouldn't be any).
349 jpeg_abort((j_common_ptr
) cinfo
); /* sets state = DSTATE_START */
350 retcode
= JPEG_HEADER_TABLES_ONLY
;
362 * Consume data in advance of what the decompressor requires.
363 * This can be called at any time once the decompressor object has
364 * been created and a data source has been set up.
366 * This routine is essentially a state machine that handles a couple
367 * of critical state-transition actions, namely initial setup and
368 * transition from header scanning to ready-for-start_decompress.
369 * All the actual input is done via the input controller's consume_input
374 jpeg_consume_input (j_decompress_ptr cinfo
)
376 int retcode
= JPEG_SUSPENDED
;
378 /* NB: every possible DSTATE value should be listed in this switch */
379 switch (cinfo
->global_state
) {
381 /* Start-of-datastream actions: reset appropriate modules */
382 (*cinfo
->inputctl
->reset_input_controller
) (cinfo
);
383 /* Initialize application's data source module */
384 (*cinfo
->src
->init_source
) (cinfo
);
385 cinfo
->global_state
= DSTATE_INHEADER
;
387 case DSTATE_INHEADER
:
388 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
389 if (retcode
== JPEG_REACHED_SOS
) { /* Found SOS, prepare to decompress */
390 /* Set up default parameters based on header data */
391 default_decompress_parms(cinfo
);
392 /* Set global state: ready for start_decompress */
393 cinfo
->global_state
= DSTATE_READY
;
397 /* Can't advance past first SOS until start_decompress is called */
398 retcode
= JPEG_REACHED_SOS
;
402 case DSTATE_SCANNING
:
404 case DSTATE_BUFIMAGE
:
406 case DSTATE_STOPPING
:
407 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
410 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
417 * Have we finished reading the input file?
421 jpeg_input_complete (j_decompress_ptr cinfo
)
423 /* Check for valid jpeg object */
424 if (cinfo
->global_state
< DSTATE_START
||
425 cinfo
->global_state
> DSTATE_STOPPING
)
426 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
427 return cinfo
->inputctl
->eoi_reached
;
432 * Is there more than one scan?
436 jpeg_has_multiple_scans (j_decompress_ptr cinfo
)
438 /* Only valid after jpeg_read_header completes */
439 if (cinfo
->global_state
< DSTATE_READY
||
440 cinfo
->global_state
> DSTATE_STOPPING
)
441 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
442 return cinfo
->inputctl
->has_multiple_scans
;
447 * Finish JPEG decompression.
449 * This will normally just verify the file trailer and release temp storage.
451 * Returns FALSE if suspended. The return value need be inspected only if
452 * a suspending data source is used.
456 jpeg_finish_decompress (j_decompress_ptr cinfo
)
458 if ((cinfo
->global_state
== DSTATE_SCANNING
||
459 cinfo
->global_state
== DSTATE_RAW_OK
) && ! cinfo
->buffered_image
) {
460 /* Terminate final pass of non-buffered mode */
461 if (cinfo
->output_scanline
< cinfo
->output_height
)
462 ERREXIT(cinfo
, JERR_TOO_LITTLE_DATA
);
463 (*cinfo
->master
->finish_output_pass
) (cinfo
);
464 cinfo
->global_state
= DSTATE_STOPPING
;
465 } else if (cinfo
->global_state
== DSTATE_BUFIMAGE
) {
466 /* Finishing after a buffered-image operation */
467 cinfo
->global_state
= DSTATE_STOPPING
;
468 } else if (cinfo
->global_state
!= DSTATE_STOPPING
) {
469 /* STOPPING = repeat call after a suspension, anything else is error */
470 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
473 while (! cinfo
->inputctl
->eoi_reached
) {
474 if ((*cinfo
->inputctl
->consume_input
) (cinfo
) == JPEG_SUSPENDED
)
475 return FALSE
; /* Suspend, come back later */
477 /* Do final cleanup */
478 (*cinfo
->src
->term_source
) (cinfo
);
479 /* We can use jpeg_abort to release memory and reset global_state */
480 jpeg_abort((j_common_ptr
) cinfo
);
485 #ifdef HAVE_MMX_INTEL_MNEMONICS
486 static int mmxsupport()
491 if (CPUInfo
[3] & (0x1 << 23))
498 #ifdef HAVE_SSE2_INTEL_MNEMONICS
499 static int sse2support()
504 if (CPUInfo
[3] & (0x1 << 26))
510 #ifdef HAVE_SSE2_INTRINSICS
511 static int sse2supportGCC()
514 /* Mac Intel started with Core Duo chips which have SSE2 Support */
516 #if defined(__GNUC__) && defined(__i386__)
517 #if defined(XP_MACOSX)
519 #endif /* XP_MACOSX */
520 #endif /* GNUC && i386 */
522 /* Add checking for SSE2 support for other platforms here */
524 /* We don't have SSE2 intrinsics support */
528 #endif /* HAVE_SSE2_INTRINSICS */
529 #endif /* HAVE_SSE2_INTEL_MNEMONICS */