Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / jpeg / jdapimin.c
blob5b85f799d766f0d24cd3e96973860c1ceeb059c1
1 /*
2 * jdapimin.c
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
16 * case.
19 #define JPEG_INTERNALS
20 #include "jinclude.h"
21 #include "jpeglib.h"
23 #ifdef HAVE_MMX_INTEL_MNEMONICS
24 #if _MSC_VER >= 1400
25 #include "intrin.h"
26 #else
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;
31 __asm {
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 */
40 pop eax
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 */
46 cpuid
47 cmp eax, InfoType
48 jl NOT_SUPPORTED /* the requested cpuid type is not supported */
50 /* actually make the cpuid call */
51 mov eax, InfoType
52 cpuid
53 mov my_eax, eax
54 mov my_ebx, ebx
55 mov my_ecx, ecx
56 mov my_edx, edx
57 NOT_SUPPORTED:
59 CPUInfo[0] = my_eax;
60 CPUInfo[1] = my_ebx;
61 CPUInfo[2] = my_ecx;
62 CPUInfo[3] = my_edx;
64 #endif /* _MSC_VER >= 1400 */
66 int MMXAvailable;
67 static int mmxsupport();
68 #endif
70 #ifdef HAVE_SSE2_INTRINSICS
71 int SSE2Available = 0;
72 #ifdef HAVE_SSE2_INTEL_MNEMONICS
73 static int sse2support();
74 #else
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).
85 GLOBAL(void)
86 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
88 int i;
90 #ifdef HAVE_MMX_INTEL_MNEMONICS
91 static int cpuidDetected = 0;
93 if(!cpuidDetected)
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) */
100 if (MMXAvailable)
101 SSE2Available = sse2support();
102 #endif
104 cpuidDetected = 1;
106 #else
107 #ifdef HAVE_SSE2_INTRINSICS
108 static int cpuidDetected = 0;
110 if(!cpuidDetected) {
111 SSE2Available = sse2supportGCC();
112 cpuidDetected = 1;
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
134 * complain here.
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));
140 cinfo->err = err;
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;
150 cinfo->src = 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);
169 /* OK, I'm ready */
170 cinfo->global_state = DSTATE_START;
175 * Destruction of a JPEG decompression object
178 GLOBAL(void)
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.
190 GLOBAL(void)
191 jpeg_abort_decompress (j_decompress_ptr cinfo)
193 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
197 * Set default decompression parameters.
200 LOCAL(void)
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) {
207 case 1:
208 cinfo->jpeg_color_space = JCS_GRAYSCALE;
209 cinfo->out_color_space = JCS_GRAYSCALE;
210 break;
212 case 3:
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) {
217 case 0:
218 cinfo->jpeg_color_space = JCS_RGB;
219 break;
220 case 1:
221 cinfo->jpeg_color_space = JCS_YCbCr;
222 break;
223 default:
224 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
225 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
226 break;
228 } else {
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' */
238 else {
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;
245 break;
247 case 4:
248 if (cinfo->saw_Adobe_marker) {
249 switch (cinfo->Adobe_transform) {
250 case 0:
251 cinfo->jpeg_color_space = JCS_CMYK;
252 break;
253 case 2:
254 cinfo->jpeg_color_space = JCS_YCCK;
255 break;
256 default:
257 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
258 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
259 break;
261 } else {
262 /* No special markers, assume straight CMYK. */
263 cinfo->jpeg_color_space = JCS_CMYK;
265 cinfo->out_color_space = JCS_CMYK;
266 break;
268 default:
269 cinfo->jpeg_color_space = JCS_UNKNOWN;
270 cinfo->out_color_space = JCS_UNKNOWN;
271 break;
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;
288 #else
289 cinfo->two_pass_quantize = FALSE;
290 #endif
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
319 * processing.
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.
327 GLOBAL(int)
328 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
330 int retcode;
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);
338 switch (retcode) {
339 case JPEG_REACHED_SOS:
340 retcode = JPEG_HEADER_OK;
341 break;
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;
351 break;
352 case JPEG_SUSPENDED:
353 /* no work */
354 break;
357 return retcode;
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
370 * method.
373 GLOBAL(int)
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) {
380 case DSTATE_START:
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;
386 /*FALLTHROUGH*/
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;
395 break;
396 case DSTATE_READY:
397 /* Can't advance past first SOS until start_decompress is called */
398 retcode = JPEG_REACHED_SOS;
399 break;
400 case DSTATE_PRELOAD:
401 case DSTATE_PRESCAN:
402 case DSTATE_SCANNING:
403 case DSTATE_RAW_OK:
404 case DSTATE_BUFIMAGE:
405 case DSTATE_BUFPOST:
406 case DSTATE_STOPPING:
407 retcode = (*cinfo->inputctl->consume_input) (cinfo);
408 break;
409 default:
410 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
412 return retcode;
417 * Have we finished reading the input file?
420 GLOBAL(boolean)
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?
435 GLOBAL(boolean)
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.
455 GLOBAL(boolean)
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);
472 /* Read until EOI */
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);
481 return TRUE;
485 #ifdef HAVE_MMX_INTEL_MNEMONICS
486 static int mmxsupport()
488 int CPUInfo[4];
490 __cpuid(CPUInfo, 1);
491 if (CPUInfo[3] & (0x1 << 23))
492 return 1;
493 else
494 return 0;
496 #endif
498 #ifdef HAVE_SSE2_INTEL_MNEMONICS
499 static int sse2support()
501 int CPUInfo[4];
503 __cpuid(CPUInfo, 1);
504 if (CPUInfo[3] & (0x1 << 26))
505 return 1;
506 else
507 return 2;
509 #else
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)
518 return 1;
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 */
526 return 2;
528 #endif /* HAVE_SSE2_INTRINSICS */
529 #endif /* HAVE_SSE2_INTEL_MNEMONICS */