Delete all Trailing spaces in code.
[reactos.git] / rosapps / lib / libjpeg / jdapimin.c
1 /*
2 * jdapimin.c
3 *
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.
7 *
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.
12 *
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.
17 */
18
19 #define JPEG_INTERNALS
20 #include "jinclude.h"
21 #include "jpeglib.h"
22
23 #ifdef HAVE_MMX_INTEL_MNEMONICS
24 int MMXAvailable;
25 static int mmxsupport();
26 #endif
27
28 #ifdef HAVE_SSE2_INTEL_MNEMONICS
29 int SSE2Available = 0;
30 static int sse2support();
31 #endif
32
33
34 /*
35 * Initialization of a JPEG decompression object.
36 * The error manager must already be set up (in case memory manager fails).
37 */
38
39 GLOBAL(void)
40 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
41 {
42 int i;
43
44 #ifdef HAVE_MMX_INTEL_MNEMONICS
45 static int cpuidDetected = 0;
46
47 if(!cpuidDetected)
48 {
49 MMXAvailable = mmxsupport();
50
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) */
54 if (MMXAvailable)
55 SSE2Available = sse2support();
56 #endif
57
58 cpuidDetected = 1;
59 }
60 #endif
61
62 /* For debugging purposes, zero the whole master structure.
63 * But error manager pointer is already there, so save and restore it.
64 */
65
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);
73
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
78 * complain here.
79 */
80 {
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));
84 cinfo->err = err;
85 cinfo->client_data = client_data;
86 }
87 cinfo->is_decompressor = TRUE;
88
89 /* Initialize a memory manager instance for this object */
90 jinit_memory_mgr((j_common_ptr) cinfo);
91
92 /* Zero out pointers to permanent structures. */
93 cinfo->progress = NULL;
94 cinfo->src = NULL;
95
96 for (i = 0; i < NUM_QUANT_TBLS; i++)
97 cinfo->quant_tbl_ptrs[i] = NULL;
98
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;
102 }
103
104 /* Initialize marker processor so application can override methods
105 * for COM, APPn markers before calling jpeg_read_header.
106 */
107 cinfo->marker_list = NULL;
108 jinit_marker_reader(cinfo);
109
110 /* And initialize the overall input controller. */
111 jinit_input_controller(cinfo);
112
113 /* OK, I'm ready */
114 cinfo->global_state = DSTATE_START;
115 }
116
117
118 /*
119 * Destruction of a JPEG decompression object
120 */
121
122 GLOBAL(void)
123 jpeg_destroy_decompress (j_decompress_ptr cinfo)
124 {
125 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
126 }
127
128
129 /*
130 * Abort processing of a JPEG decompression operation,
131 * but don't destroy the object itself.
132 */
133
134 GLOBAL(void)
135 jpeg_abort_decompress (j_decompress_ptr cinfo)
136 {
137 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
138 }
139
140 /*
141 * Set default decompression parameters.
142 */
143
144 LOCAL(void)
145 default_decompress_parms (j_decompress_ptr cinfo)
146 {
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) {
151 case 1:
152 cinfo->jpeg_color_space = JCS_GRAYSCALE;
153 cinfo->out_color_space = JCS_GRAYSCALE;
154 break;
155
156 case 3:
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) {
161 case 0:
162 cinfo->jpeg_color_space = JCS_RGB;
163 break;
164 case 1:
165 cinfo->jpeg_color_space = JCS_YCbCr;
166 break;
167 default:
168 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
169 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
170 break;
171 }
172 } else {
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;
177
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' */
182 else {
183 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
184 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
185 }
186 }
187 /* Always guess RGB is proper output colorspace. */
188 cinfo->out_color_space = JCS_RGB;
189 break;
190
191 case 4:
192 if (cinfo->saw_Adobe_marker) {
193 switch (cinfo->Adobe_transform) {
194 case 0:
195 cinfo->jpeg_color_space = JCS_CMYK;
196 break;
197 case 2:
198 cinfo->jpeg_color_space = JCS_YCCK;
199 break;
200 default:
201 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
202 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
203 break;
204 }
205 } else {
206 /* No special markers, assume straight CMYK. */
207 cinfo->jpeg_color_space = JCS_CMYK;
208 }
209 cinfo->out_color_space = JCS_CMYK;
210 break;
211
212 default:
213 cinfo->jpeg_color_space = JCS_UNKNOWN;
214 cinfo->out_color_space = JCS_UNKNOWN;
215 break;
216 }
217
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;
232 #else
233 cinfo->two_pass_quantize = FALSE;
234 #endif
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;
241 }
242
243
244 /*
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.
247 *
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
263 * processing.
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.
266 *
267 * This routine is now just a front end to jpeg_consume_input, with some
268 * extra error checking.
269 */
270
271 GLOBAL(int)
272 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
273 {
274 int retcode;
275
276 if (cinfo->global_state != DSTATE_START &&
277 cinfo->global_state != DSTATE_INHEADER)
278 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
279
280 retcode = jpeg_consume_input(cinfo);
281
282 switch (retcode) {
283 case JPEG_REACHED_SOS:
284 retcode = JPEG_HEADER_OK;
285 break;
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).
292 */
293 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
294 retcode = JPEG_HEADER_TABLES_ONLY;
295 break;
296 case JPEG_SUSPENDED:
297 /* no work */
298 break;
299 }
300
301 return retcode;
302 }
303
304
305 /*
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.
309 *
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
314 * method.
315 */
316
317 GLOBAL(int)
318 jpeg_consume_input (j_decompress_ptr cinfo)
319 {
320 int retcode = JPEG_SUSPENDED;
321
322 /* NB: every possible DSTATE value should be listed in this switch */
323 switch (cinfo->global_state) {
324 case DSTATE_START:
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;
330 /*FALLTHROUGH*/
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;
338 }
339 break;
340 case DSTATE_READY:
341 /* Can't advance past first SOS until start_decompress is called */
342 retcode = JPEG_REACHED_SOS;
343 break;
344 case DSTATE_PRELOAD:
345 case DSTATE_PRESCAN:
346 case DSTATE_SCANNING:
347 case DSTATE_RAW_OK:
348 case DSTATE_BUFIMAGE:
349 case DSTATE_BUFPOST:
350 case DSTATE_STOPPING:
351 retcode = (*cinfo->inputctl->consume_input) (cinfo);
352 break;
353 default:
354 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
355 }
356 return retcode;
357 }
358
359
360 /*
361 * Have we finished reading the input file?
362 */
363
364 GLOBAL(boolean)
365 jpeg_input_complete (j_decompress_ptr cinfo)
366 {
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;
372 }
373
374
375 /*
376 * Is there more than one scan?
377 */
378
379 GLOBAL(boolean)
380 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
381 {
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;
387 }
388
389
390 /*
391 * Finish JPEG decompression.
392 *
393 * This will normally just verify the file trailer and release temp storage.
394 *
395 * Returns FALSE if suspended. The return value need be inspected only if
396 * a suspending data source is used.
397 */
398
399 GLOBAL(boolean)
400 jpeg_finish_decompress (j_decompress_ptr cinfo)
401 {
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);
415 }
416 /* Read until EOI */
417 while (! cinfo->inputctl->eoi_reached) {
418 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
419 return FALSE; /* Suspend, come back later */
420 }
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);
425 return TRUE;
426 }
427
428
429 #ifdef HAVE_MMX_INTEL_MNEMONICS
430
431
432 static int mmxsupport()
433 {
434 int mmx_supported = 0;
435
436 _asm {
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
442
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
450
451 xor eax, eax //Set eax to zero
452
453 cpuid
454
455 cmp eax, 1 //make sure eax return non-zero value
456 jl NOT_SUPPORTED //If eax is zero, mmx not supported
457
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"
461
462 cpuid
463
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
467
468 mov mmx_supported, 1 //set return value to 1
469
470 NOT_SUPPORTED:
471 mov eax, mmx_supported //move return value to eax
472
473 }
474
475 return mmx_supported;
476 }
477 #endif
478
479 #ifdef HAVE_SSE2_INTEL_MNEMONICS
480
481 static int sse2support()
482 {
483 int sse2available = 0;
484 int my_edx;
485 _asm
486 {
487 mov eax, 01
488 cpuid
489 mov my_edx, edx
490 }
491 if (my_edx & (0x1 << 26))
492 sse2available = 1;
493 else sse2available = 2;
494
495 return sse2available;
496 }
497
498 #endif
499