Back to index

lightning-sunbird  0.9+nobinonly
jdapimin.c
Go to the documentation of this file.
00001 /*
00002  * jdapimin.c
00003  *
00004  * Copyright (C) 1994-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains application interface code for the decompression half
00009  * of the JPEG library.  These are the "minimum" API routines that may be
00010  * needed in either the normal full-decompression case or the
00011  * transcoding-only case.
00012  *
00013  * Most of the routines intended to be called directly by an application
00014  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
00015  * shared by compression and decompression, and jdtrans.c for the transcoding
00016  * case.
00017  */
00018 
00019 #define JPEG_INTERNALS
00020 #include "jinclude.h"
00021 #include "jpeglib.h"
00022 
00023 #ifdef HAVE_MMX_INTEL_MNEMONICS
00024 int MMXAvailable;
00025 static int mmxsupport();
00026 #endif
00027 
00028 #ifdef HAVE_SSE2_INTEL_MNEMONICS
00029 int SSE2Available = 0;
00030 static int sse2support();
00031 #endif
00032 
00033 
00034 /*
00035  * Initialization of a JPEG decompression object.
00036  * The error manager must already be set up (in case memory manager fails).
00037  */
00038 
00039 GLOBAL(void)
00040 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
00041 {
00042   int i;
00043 
00044 #ifdef HAVE_MMX_INTEL_MNEMONICS
00045   static int cpuidDetected = 0;
00046 
00047   if(!cpuidDetected)
00048   {
00049        MMXAvailable = mmxsupport();
00050 
00051 #ifdef HAVE_SSE2_INTEL_MNEMONICS
00052        /* only do the sse2 support check if mmx is supported (so
00053           we know the processor supports cpuid) */
00054        if (MMXAvailable)
00055            SSE2Available = sse2support();
00056 #endif
00057 
00058        cpuidDetected = 1;
00059   }
00060 #endif
00061 
00062   /* For debugging purposes, zero the whole master structure.
00063    * But error manager pointer is already there, so save and restore it.
00064    */
00065 
00066   /* Guard against version mismatches between library and caller. */
00067   cinfo->mem = NULL;        /* so jpeg_destroy knows mem mgr not called */
00068   if (version != JPEG_LIB_VERSION)
00069     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
00070   if (structsize != SIZEOF(struct jpeg_decompress_struct))
00071     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
00072             (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
00073 
00074   /* For debugging purposes, we zero the whole master structure.
00075    * But the application has already set the err pointer, and may have set
00076    * client_data, so we have to save and restore those fields.
00077    * Note: if application hasn't set client_data, tools like Purify may
00078    * complain here.
00079    */
00080   {
00081     struct jpeg_error_mgr * err = cinfo->err;
00082     void * client_data = cinfo->client_data; /* ignore Purify complaint here */
00083     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
00084     cinfo->err = err;
00085     cinfo->client_data = client_data;
00086   }
00087   cinfo->is_decompressor = TRUE;
00088 
00089   /* Initialize a memory manager instance for this object */
00090   jinit_memory_mgr((j_common_ptr) cinfo);
00091 
00092   /* Zero out pointers to permanent structures. */
00093   cinfo->progress = NULL;
00094   cinfo->src = NULL;
00095 
00096   for (i = 0; i < NUM_QUANT_TBLS; i++)
00097     cinfo->quant_tbl_ptrs[i] = NULL;
00098 
00099   for (i = 0; i < NUM_HUFF_TBLS; i++) {
00100     cinfo->dc_huff_tbl_ptrs[i] = NULL;
00101     cinfo->ac_huff_tbl_ptrs[i] = NULL;
00102   }
00103 
00104   /* Initialize marker processor so application can override methods
00105    * for COM, APPn markers before calling jpeg_read_header.
00106    */
00107   cinfo->marker_list = NULL;
00108   jinit_marker_reader(cinfo);
00109 
00110   /* And initialize the overall input controller. */
00111   jinit_input_controller(cinfo);
00112 
00113   /* OK, I'm ready */
00114   cinfo->global_state = DSTATE_START;
00115 }
00116 
00117 
00118 /*
00119  * Destruction of a JPEG decompression object
00120  */
00121 
00122 GLOBAL(void)
00123 jpeg_destroy_decompress (j_decompress_ptr cinfo)
00124 {
00125   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
00126 }
00127 
00128 
00129 /*
00130  * Abort processing of a JPEG decompression operation,
00131  * but don't destroy the object itself.
00132  */
00133 
00134 GLOBAL(void)
00135 jpeg_abort_decompress (j_decompress_ptr cinfo)
00136 {
00137   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
00138 }
00139 
00140 /*
00141  * Set default decompression parameters.
00142  */
00143 
00144 LOCAL(void)
00145 default_decompress_parms (j_decompress_ptr cinfo)
00146 {
00147   /* Guess the input colorspace, and set output colorspace accordingly. */
00148   /* (Wish JPEG committee had provided a real way to specify this...) */
00149   /* Note application may override our guesses. */
00150   switch (cinfo->num_components) {
00151   case 1:
00152     cinfo->jpeg_color_space = JCS_GRAYSCALE;
00153     cinfo->out_color_space = JCS_GRAYSCALE;
00154     break;
00155     
00156   case 3:
00157     if (cinfo->saw_JFIF_marker) {
00158       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
00159     } else if (cinfo->saw_Adobe_marker) {
00160       switch (cinfo->Adobe_transform) {
00161       case 0:
00162        cinfo->jpeg_color_space = JCS_RGB;
00163        break;
00164       case 1:
00165        cinfo->jpeg_color_space = JCS_YCbCr;
00166        break;
00167       default:
00168        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
00169        cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
00170        break;
00171       }
00172     } else {
00173       /* Saw no special markers, try to guess from the component IDs */
00174       int cid0 = cinfo->comp_info[0].component_id;
00175       int cid1 = cinfo->comp_info[1].component_id;
00176       int cid2 = cinfo->comp_info[2].component_id;
00177 
00178       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
00179        cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
00180       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
00181        cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
00182       else {
00183        TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
00184        cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
00185       }
00186     }
00187     /* Always guess RGB is proper output colorspace. */
00188     cinfo->out_color_space = JCS_RGB;
00189     break;
00190     
00191   case 4:
00192     if (cinfo->saw_Adobe_marker) {
00193       switch (cinfo->Adobe_transform) {
00194       case 0:
00195        cinfo->jpeg_color_space = JCS_CMYK;
00196        break;
00197       case 2:
00198        cinfo->jpeg_color_space = JCS_YCCK;
00199        break;
00200       default:
00201        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
00202        cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
00203        break;
00204       }
00205     } else {
00206       /* No special markers, assume straight CMYK. */
00207       cinfo->jpeg_color_space = JCS_CMYK;
00208     }
00209     cinfo->out_color_space = JCS_CMYK;
00210     break;
00211     
00212   default:
00213     cinfo->jpeg_color_space = JCS_UNKNOWN;
00214     cinfo->out_color_space = JCS_UNKNOWN;
00215     break;
00216   }
00217 
00218   /* Set defaults for other decompression parameters. */
00219   cinfo->scale_num = 1;            /* 1:1 scaling */
00220   cinfo->scale_denom = 1;
00221   cinfo->output_gamma = 1.0;
00222   cinfo->buffered_image = FALSE;
00223   cinfo->raw_data_out = FALSE;
00224   cinfo->dct_method = JDCT_DEFAULT;
00225   cinfo->do_fancy_upsampling = TRUE;
00226   cinfo->do_block_smoothing = TRUE;
00227   cinfo->quantize_colors = FALSE;
00228   /* We set these in case application only sets quantize_colors. */
00229   cinfo->dither_mode = JDITHER_FS;
00230 #ifdef QUANT_2PASS_SUPPORTED
00231   cinfo->two_pass_quantize = TRUE;
00232 #else
00233   cinfo->two_pass_quantize = FALSE;
00234 #endif
00235   cinfo->desired_number_of_colors = 256;
00236   cinfo->colormap = NULL;
00237   /* Initialize for no mode change in buffered-image mode. */
00238   cinfo->enable_1pass_quant = FALSE;
00239   cinfo->enable_external_quant = FALSE;
00240   cinfo->enable_2pass_quant = FALSE;
00241 }
00242 
00243 
00244 /*
00245  * Decompression startup: read start of JPEG datastream to see what's there.
00246  * Need only initialize JPEG object and supply a data source before calling.
00247  *
00248  * This routine will read as far as the first SOS marker (ie, actual start of
00249  * compressed data), and will save all tables and parameters in the JPEG
00250  * object.  It will also initialize the decompression parameters to default
00251  * values, and finally return JPEG_HEADER_OK.  On return, the application may
00252  * adjust the decompression parameters and then call jpeg_start_decompress.
00253  * (Or, if the application only wanted to determine the image parameters,
00254  * the data need not be decompressed.  In that case, call jpeg_abort or
00255  * jpeg_destroy to release any temporary space.)
00256  * If an abbreviated (tables only) datastream is presented, the routine will
00257  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
00258  * re-use the JPEG object to read the abbreviated image datastream(s).
00259  * It is unnecessary (but OK) to call jpeg_abort in this case.
00260  * The JPEG_SUSPENDED return code only occurs if the data source module
00261  * requests suspension of the decompressor.  In this case the application
00262  * should load more source data and then re-call jpeg_read_header to resume
00263  * processing.
00264  * If a non-suspending data source is used and require_image is TRUE, then the
00265  * return code need not be inspected since only JPEG_HEADER_OK is possible.
00266  *
00267  * This routine is now just a front end to jpeg_consume_input, with some
00268  * extra error checking.
00269  */
00270 
00271 GLOBAL(int)
00272 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
00273 {
00274   int retcode;
00275 
00276   if (cinfo->global_state != DSTATE_START &&
00277       cinfo->global_state != DSTATE_INHEADER)
00278     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00279 
00280   retcode = jpeg_consume_input(cinfo);
00281 
00282   switch (retcode) {
00283   case JPEG_REACHED_SOS:
00284     retcode = JPEG_HEADER_OK;
00285     break;
00286   case JPEG_REACHED_EOI:
00287     if (require_image)             /* Complain if application wanted an image */
00288       ERREXIT(cinfo, JERR_NO_IMAGE);
00289     /* Reset to start state; it would be safer to require the application to
00290      * call jpeg_abort, but we can't change it now for compatibility reasons.
00291      * A side effect is to free any temporary memory (there shouldn't be any).
00292      */
00293     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
00294     retcode = JPEG_HEADER_TABLES_ONLY;
00295     break;
00296   case JPEG_SUSPENDED:
00297     /* no work */
00298     break;
00299   }
00300 
00301   return retcode;
00302 }
00303 
00304 
00305 /*
00306  * Consume data in advance of what the decompressor requires.
00307  * This can be called at any time once the decompressor object has
00308  * been created and a data source has been set up.
00309  *
00310  * This routine is essentially a state machine that handles a couple
00311  * of critical state-transition actions, namely initial setup and
00312  * transition from header scanning to ready-for-start_decompress.
00313  * All the actual input is done via the input controller's consume_input
00314  * method.
00315  */
00316 
00317 GLOBAL(int)
00318 jpeg_consume_input (j_decompress_ptr cinfo)
00319 {
00320   int retcode = JPEG_SUSPENDED;
00321 
00322   /* NB: every possible DSTATE value should be listed in this switch */
00323   switch (cinfo->global_state) {
00324   case DSTATE_START:
00325     /* Start-of-datastream actions: reset appropriate modules */
00326     (*cinfo->inputctl->reset_input_controller) (cinfo);
00327     /* Initialize application's data source module */
00328     (*cinfo->src->init_source) (cinfo);
00329     cinfo->global_state = DSTATE_INHEADER;
00330     /*FALLTHROUGH*/
00331   case DSTATE_INHEADER:
00332     retcode = (*cinfo->inputctl->consume_input) (cinfo);
00333     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
00334       /* Set up default parameters based on header data */
00335       default_decompress_parms(cinfo);
00336       /* Set global state: ready for start_decompress */
00337       cinfo->global_state = DSTATE_READY;
00338     }
00339     break;
00340   case DSTATE_READY:
00341     /* Can't advance past first SOS until start_decompress is called */
00342     retcode = JPEG_REACHED_SOS;
00343     break;
00344   case DSTATE_PRELOAD:
00345   case DSTATE_PRESCAN:
00346   case DSTATE_SCANNING:
00347   case DSTATE_RAW_OK:
00348   case DSTATE_BUFIMAGE:
00349   case DSTATE_BUFPOST:
00350   case DSTATE_STOPPING:
00351     retcode = (*cinfo->inputctl->consume_input) (cinfo);
00352     break;
00353   default:
00354     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00355   }
00356   return retcode;
00357 }
00358 
00359 
00360 /*
00361  * Have we finished reading the input file?
00362  */
00363 
00364 GLOBAL(boolean)
00365 jpeg_input_complete (j_decompress_ptr cinfo)
00366 {
00367   /* Check for valid jpeg object */
00368   if (cinfo->global_state < DSTATE_START ||
00369       cinfo->global_state > DSTATE_STOPPING)
00370     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00371   return cinfo->inputctl->eoi_reached;
00372 }
00373 
00374 
00375 /*
00376  * Is there more than one scan?
00377  */
00378 
00379 GLOBAL(boolean)
00380 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
00381 {
00382   /* Only valid after jpeg_read_header completes */
00383   if (cinfo->global_state < DSTATE_READY ||
00384       cinfo->global_state > DSTATE_STOPPING)
00385     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00386   return cinfo->inputctl->has_multiple_scans;
00387 }
00388 
00389 
00390 /*
00391  * Finish JPEG decompression.
00392  *
00393  * This will normally just verify the file trailer and release temp storage.
00394  *
00395  * Returns FALSE if suspended.  The return value need be inspected only if
00396  * a suspending data source is used.
00397  */
00398 
00399 GLOBAL(boolean)
00400 jpeg_finish_decompress (j_decompress_ptr cinfo)
00401 {
00402   if ((cinfo->global_state == DSTATE_SCANNING ||
00403        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
00404     /* Terminate final pass of non-buffered mode */
00405     if (cinfo->output_scanline < cinfo->output_height)
00406       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
00407     (*cinfo->master->finish_output_pass) (cinfo);
00408     cinfo->global_state = DSTATE_STOPPING;
00409   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
00410     /* Finishing after a buffered-image operation */
00411     cinfo->global_state = DSTATE_STOPPING;
00412   } else if (cinfo->global_state != DSTATE_STOPPING) {
00413     /* STOPPING = repeat call after a suspension, anything else is error */
00414     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00415   }
00416   /* Read until EOI */
00417   while (! cinfo->inputctl->eoi_reached) {
00418     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
00419       return FALSE;         /* Suspend, come back later */
00420   }
00421   /* Do final cleanup */
00422   (*cinfo->src->term_source) (cinfo);
00423   /* We can use jpeg_abort to release memory and reset global_state */
00424   jpeg_abort((j_common_ptr) cinfo);
00425   return TRUE;
00426 }
00427 
00428 
00429 #ifdef HAVE_MMX_INTEL_MNEMONICS
00430 
00431 
00432 static int mmxsupport()
00433 {
00434        int mmx_supported = 0;
00435 
00436        _asm {
00437               pushfd                             //Save Eflag to stack
00438               pop eax                                   //Get Eflag from stack into eax
00439               mov ecx, eax                //Make another copy of Eflag in ecx
00440               xor eax, 0x200000           //Toggle ID bit in Eflag [i.e. bit(21)] 
00441               push eax                           //Save modified Eflag back to stack
00442 
00443               popfd                              //Restored modified value back to Eflag reg 
00444               pushfd                             //Save Eflag to stack
00445               pop eax                                   //Get Eflag from stack
00446               xor eax, ecx                //Compare the new Eflag with the original Eflag
00447               jz NOT_SUPPORTED            //If the same, CPUID instruction is not supported,
00448                                                         //skip following instructions and jump to
00449                                                         //NOT_SUPPORTED label
00450 
00451               xor eax, eax                //Set eax to zero
00452                                    
00453               cpuid
00454               
00455               cmp eax, 1                         //make sure eax return non-zero value
00456               jl NOT_SUPPORTED            //If eax is zero, mmx not supported
00457 
00458               xor eax, eax                //set eax to zero
00459               inc eax                                   //Now increment eax to 1.  This instruction is 
00460                                                         //faster than the instruction "mov eax, 1"
00461               
00462               cpuid
00463 
00464               and edx, 0x00800000         //mask out all bits but mmx bit(24)
00465               cmp edx, 0                         // 0 = mmx not supported
00466               jz     NOT_SUPPORTED        // non-zero = Yes, mmx IS supported
00467 
00468               mov    mmx_supported, 1     //set return value to 1
00469 
00470 NOT_SUPPORTED:
00471               mov    eax, mmx_supported   //move return value to eax  
00472 
00473        }
00474 
00475        return mmx_supported;              
00476 }
00477 #endif
00478 
00479 #ifdef HAVE_SSE2_INTEL_MNEMONICS
00480 
00481 static int sse2support()
00482 {
00483        int sse2available = 0;
00484        int my_edx;
00485        _asm
00486        {
00487               mov eax, 01                       
00488               cpuid                                    
00489               mov my_edx, edx    
00490        }
00491        if (my_edx & (0x1 << 26)) 
00492               sse2available = 1; 
00493        else sse2available = 2;
00494 
00495        return sse2available;
00496 }
00497 
00498 #endif
00499