home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jdapimin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  14.2 KB  |  472 lines

  1. /*
  2.  * jdapimin.c
  3.  *
  4.  * Copyright (C) 1994-1995, 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. #include "xp_core.h"
  23.  
  24. #ifdef XP_WIN32
  25. int MMXAvailable;
  26. int mmxsupport();
  27. #endif
  28.  
  29. /*
  30.  * Initialization of a JPEG decompression object.
  31.  * The error manager must already be set up (in case memory manager fails).
  32.  */
  33.  
  34. GLOBAL JRI_PUBLIC_API(void)
  35. jpeg_create_decompress (j_decompress_ptr cinfo)
  36. {
  37.   int i;
  38.  
  39. #ifdef XP_WIN32
  40.   static int cpuidDetected = 0;
  41.  
  42.   if(!cpuidDetected)
  43.   {
  44.     MMXAvailable = mmxsupport();
  45.     cpuidDetected = 1;
  46.   }
  47. #endif
  48.  
  49.   /* For debugging purposes, zero the whole master structure.
  50.    * But error manager pointer is already there, so save and restore it.
  51.    */
  52.   {
  53.     struct jpeg_error_mgr * err = cinfo->err;
  54.     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
  55.     cinfo->err = err;
  56.   }
  57.   cinfo->is_decompressor = TRUE;
  58.  
  59.   /* Initialize a memory manager instance for this object */
  60.   jinit_memory_mgr((j_common_ptr) cinfo);
  61.  
  62.   /* Zero out pointers to permanent structures. */
  63.   cinfo->progress = NULL;
  64.   cinfo->src = NULL;
  65.  
  66.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  67.     cinfo->quant_tbl_ptrs[i] = NULL;
  68.  
  69.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  70.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  71.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  72.   }
  73.  
  74.   /* Initialize marker processor so application can override methods
  75.    * for COM, APPn markers before calling jpeg_read_header.
  76.    */
  77.   jinit_marker_reader(cinfo);
  78.  
  79.   /* And initialize the overall input controller. */
  80.   jinit_input_controller(cinfo);
  81.  
  82.   /* OK, I'm ready */
  83.   cinfo->global_state = DSTATE_START;
  84. }
  85.  
  86.  
  87. /*
  88.  * Destruction of a JPEG decompression object
  89.  */
  90.  
  91. GLOBAL JRI_PUBLIC_API(void)
  92. jpeg_destroy_decompress (j_decompress_ptr cinfo)
  93. {
  94.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  95. }
  96.  
  97.  
  98. /*
  99.  * Abort processing of a JPEG decompression operation,
  100.  * but don't destroy the object itself.
  101.  */
  102.  
  103. GLOBAL void
  104. jpeg_abort_decompress (j_decompress_ptr cinfo)
  105. {
  106.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  107. }
  108.  
  109.  
  110. /*
  111.  * Install a special processing method for COM or APPn markers.
  112.  */
  113.  
  114. GLOBAL JRI_PUBLIC_API(void)
  115. jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  116.                jpeg_marker_parser_method routine)
  117. {
  118.   if (marker_code == JPEG_COM)
  119.     cinfo->marker->process_COM = routine;
  120.   else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
  121.     cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
  122.   else
  123.     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  124. }
  125.  
  126.  
  127. /*
  128.  * Set default decompression parameters.
  129.  */
  130.  
  131. LOCAL void
  132. default_decompress_parms (j_decompress_ptr cinfo)
  133. {
  134.   /* Guess the input colorspace, and set output colorspace accordingly. */
  135.   /* (Wish JPEG committee had provided a real way to specify this...) */
  136.   /* Note application may override our guesses. */
  137.   switch (cinfo->num_components) {
  138.   case 1:
  139.     cinfo->jpeg_color_space = JCS_GRAYSCALE;
  140.     cinfo->out_color_space = JCS_GRAYSCALE;
  141.     break;
  142.     
  143.   case 3:
  144.     if (cinfo->saw_JFIF_marker) {
  145.       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  146.     } else if (cinfo->saw_Adobe_marker) {
  147.       switch (cinfo->Adobe_transform) {
  148.       case 0:
  149.     cinfo->jpeg_color_space = JCS_RGB;
  150.     break;
  151.       case 1:
  152.     cinfo->jpeg_color_space = JCS_YCbCr;
  153.     break;
  154.       default:
  155.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  156.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  157.     break;
  158.       }
  159.     } else {
  160.       /* Saw no special markers, try to guess from the component IDs */
  161.       int cid0 = cinfo->comp_info[0].component_id;
  162.       int cid1 = cinfo->comp_info[1].component_id;
  163.       int cid2 = cinfo->comp_info[2].component_id;
  164.  
  165.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  166.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  167.       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  168.     cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  169.       else {
  170.     TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  171.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  172.       }
  173.     }
  174.     /* Always guess RGB is proper output colorspace. */
  175.     cinfo->out_color_space = JCS_RGB;
  176.     break;
  177.     
  178.   case 4:
  179.     if (cinfo->saw_Adobe_marker) {
  180.       switch (cinfo->Adobe_transform) {
  181.       case 0:
  182.     cinfo->jpeg_color_space = JCS_CMYK;
  183.     break;
  184.       case 2:
  185.     cinfo->jpeg_color_space = JCS_YCCK;
  186.     break;
  187.       default:
  188.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  189.     cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  190.     break;
  191.       }
  192.     } else {
  193.       /* No special markers, assume straight CMYK. */
  194.       cinfo->jpeg_color_space = JCS_CMYK;
  195.     }
  196.     cinfo->out_color_space = JCS_CMYK;
  197.     break;
  198.     
  199.   default:
  200.     cinfo->jpeg_color_space = JCS_UNKNOWN;
  201.     cinfo->out_color_space = JCS_UNKNOWN;
  202.     break;
  203.   }
  204.  
  205.   /* Set defaults for other decompression parameters. */
  206.   cinfo->scale_num = 1;        /* 1:1 scaling */
  207.   cinfo->scale_denom = 1;
  208.   cinfo->output_gamma = 1.0;
  209.   cinfo->buffered_image = FALSE;
  210.   cinfo->raw_data_out = FALSE;
  211.   cinfo->dct_method = JDCT_DEFAULT;
  212.   cinfo->do_fancy_upsampling = TRUE;
  213.   cinfo->do_block_smoothing = TRUE;
  214.   cinfo->quantize_colors = FALSE;
  215.   /* We set these in case application only sets quantize_colors. */
  216.   cinfo->dither_mode = JDITHER_FS;
  217. #ifdef QUANT_2PASS_SUPPORTED
  218.   cinfo->two_pass_quantize = TRUE;
  219. #else
  220.   cinfo->two_pass_quantize = FALSE;
  221. #endif
  222.   cinfo->desired_number_of_colors = 256;
  223.   cinfo->colormap = NULL;
  224.   /* Initialize for no mode change in buffered-image mode. */
  225.   cinfo->enable_1pass_quant = FALSE;
  226.   cinfo->enable_external_quant = FALSE;
  227.   cinfo->enable_2pass_quant = FALSE;
  228. }
  229.  
  230.  
  231. /*
  232.  * Decompression startup: read start of JPEG datastream to see what's there.
  233.  * Need only initialize JPEG object and supply a data source before calling.
  234.  *
  235.  * This routine will read as far as the first SOS marker (ie, actual start of
  236.  * compressed data), and will save all tables and parameters in the JPEG
  237.  * object.  It will also initialize the decompression parameters to default
  238.  * values, and finally return JPEG_HEADER_OK.  On return, the application may
  239.  * adjust the decompression parameters and then call jpeg_start_decompress.
  240.  * (Or, if the application only wanted to determine the image parameters,
  241.  * the data need not be decompressed.  In that case, call jpeg_abort or
  242.  * jpeg_destroy to release any temporary space.)
  243.  * If an abbreviated (tables only) datastream is presented, the routine will
  244.  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  245.  * re-use the JPEG object to read the abbreviated image datastream(s).
  246.  * It is unnecessary (but OK) to call jpeg_abort in this case.
  247.  * The JPEG_SUSPENDED return code only occurs if the data source module
  248.  * requests suspension of the decompressor.  In this case the application
  249.  * should load more source data and then re-call jpeg_read_header to resume
  250.  * processing.
  251.  * If a non-suspending data source is used and require_image is TRUE, then the
  252.  * return code need not be inspected since only JPEG_HEADER_OK is possible.
  253.  *
  254.  * This routine is now just a front end to jpeg_consume_input, with some
  255.  * extra error checking.
  256.  */
  257.  
  258. GLOBAL JRI_PUBLIC_API(int)
  259. jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  260. {
  261.   int retcode;
  262.  
  263.   if (cinfo->global_state != DSTATE_START &&
  264.       cinfo->global_state != DSTATE_INHEADER)
  265.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  266.  
  267.   retcode = jpeg_consume_input(cinfo);
  268.  
  269.   switch (retcode) {
  270.   case JPEG_REACHED_SOS:
  271.     retcode = JPEG_HEADER_OK;
  272.     break;
  273.   case JPEG_REACHED_EOI:
  274.     if (require_image)        /* Complain if application wanted an image */
  275.       ERREXIT(cinfo, JERR_NO_IMAGE);
  276.     /* Reset to start state; it would be safer to require the application to
  277.      * call jpeg_abort, but we can't change it now for compatibility reasons.
  278.      * A side effect is to free any temporary memory (there shouldn't be any).
  279.      */
  280.     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  281.     retcode = JPEG_HEADER_TABLES_ONLY;
  282.     break;
  283.   case JPEG_SUSPENDED:
  284.     /* no work */
  285.     break;
  286.   }
  287.  
  288.   return retcode;
  289. }
  290.  
  291.  
  292. /*
  293.  * Consume data in advance of what the decompressor requires.
  294.  * This can be called at any time once the decompressor object has
  295.  * been created and a data source has been set up.
  296.  *
  297.  * This routine is essentially a state machine that handles a couple
  298.  * of critical state-transition actions, namely initial setup and
  299.  * transition from header scanning to ready-for-start_decompress.
  300.  * All the actual input is done via the input controller's consume_input
  301.  * method.
  302.  */
  303.  
  304. GLOBAL JRI_PUBLIC_API(int)
  305. jpeg_consume_input (j_decompress_ptr cinfo)
  306. {
  307.   int retcode = JPEG_SUSPENDED;
  308.  
  309.   /* NB: every possible DSTATE value should be listed in this switch */
  310.   switch (cinfo->global_state) {
  311.   case DSTATE_START:
  312.     /* Start-of-datastream actions: reset appropriate modules */
  313.     (*cinfo->inputctl->reset_input_controller) (cinfo);
  314.     /* Initialize application's data source module */
  315.     (*cinfo->src->init_source) (cinfo);
  316.     cinfo->global_state = DSTATE_INHEADER;
  317.     /*FALLTHROUGH*/
  318.   case DSTATE_INHEADER:
  319.     retcode = (*cinfo->inputctl->consume_input) (cinfo);
  320.     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  321.       /* Set up default parameters based on header data */
  322.       default_decompress_parms(cinfo);
  323.       /* Set global state: ready for start_decompress */
  324.       cinfo->global_state = DSTATE_READY;
  325.     }
  326.     break;
  327.   case DSTATE_READY:
  328.     /* Can't advance past first SOS until start_decompress is called */
  329.     retcode = JPEG_REACHED_SOS;
  330.     break;
  331.   case DSTATE_PRELOAD:
  332.   case DSTATE_PRESCAN:
  333.   case DSTATE_SCANNING:
  334.   case DSTATE_RAW_OK:
  335.   case DSTATE_BUFIMAGE:
  336.   case DSTATE_BUFPOST:
  337.   case DSTATE_STOPPING:
  338.     retcode = (*cinfo->inputctl->consume_input) (cinfo);
  339.     break;
  340.   default:
  341.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  342.   }
  343.   return retcode;
  344. }
  345.  
  346.  
  347. /*
  348.  * Have we finished reading the input file?
  349.  */
  350.  
  351. GLOBAL boolean
  352. jpeg_input_complete (j_decompress_ptr cinfo)
  353. {
  354.   /* Check for valid jpeg object */
  355.   if (cinfo->global_state < DSTATE_START ||
  356.       cinfo->global_state > DSTATE_STOPPING)
  357.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  358.   return cinfo->inputctl->eoi_reached;
  359. }
  360.  
  361.  
  362. /*
  363.  * Is there more than one scan?
  364.  */
  365.  
  366. GLOBAL JRI_PUBLIC_API(boolean)
  367. jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  368. {
  369.   /* Only valid after jpeg_read_header completes */
  370.   if (cinfo->global_state < DSTATE_READY ||
  371.       cinfo->global_state > DSTATE_STOPPING)
  372.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  373.   return cinfo->inputctl->has_multiple_scans;
  374. }
  375.  
  376.  
  377. /*
  378.  * Finish JPEG decompression.
  379.  *
  380.  * This will normally just verify the file trailer and release temp storage.
  381.  *
  382.  * Returns FALSE if suspended.  The return value need be inspected only if
  383.  * a suspending data source is used.
  384.  */
  385.  
  386. GLOBAL JRI_PUBLIC_API(boolean)
  387. jpeg_finish_decompress (j_decompress_ptr cinfo)
  388. {
  389.   if ((cinfo->global_state == DSTATE_SCANNING ||
  390.        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  391.     /* Terminate final pass of non-buffered mode */
  392.     if (cinfo->output_scanline < cinfo->output_height)
  393.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  394.     (*cinfo->master->finish_output_pass) (cinfo);
  395.     cinfo->global_state = DSTATE_STOPPING;
  396.   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  397.     /* Finishing after a buffered-image operation */
  398.     cinfo->global_state = DSTATE_STOPPING;
  399.   } else if (cinfo->global_state != DSTATE_STOPPING) {
  400.     /* STOPPING = repeat call after a suspension, anything else is error */
  401.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  402.   }
  403.   /* Read until EOI */
  404.   while (! cinfo->inputctl->eoi_reached) {
  405.     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  406.       return FALSE;        /* Suspend, come back later */
  407.   }
  408.   /* Do final cleanup */
  409.   (*cinfo->src->term_source) (cinfo);
  410.   /* We can use jpeg_abort to release memory and reset global_state */
  411.   jpeg_abort((j_common_ptr) cinfo);
  412.   return TRUE;
  413. }
  414.  
  415. #ifdef XP_WIN32
  416.  
  417.  
  418. int mmxsupport()
  419. {
  420.     int mmx_supported = 0;
  421.  
  422.     _asm {
  423.         pushfd                    //Save Eflag to stack
  424.         pop eax                    //Get Eflag from stack into eax
  425.         mov ecx, eax            //Make another copy of Eflag in ecx
  426.         xor eax, 0x200000        //Toggle ID bit in Eflag [i.e. bit(21)] 
  427.         push eax                //Save modified Eflag back to stack
  428.  
  429.         popfd                    //Restored modified value back to Eflag reg 
  430.         pushfd                    //Save Eflag to stack
  431.         pop eax                    //Get Eflag from stack
  432.         xor eax, ecx            //Compare the new Eflag with the original Eflag
  433.         jz NOT_SUPPORTED        //If the same, CPUID instruction is not supported,
  434.                                 //skip following instructions and jump to
  435.                                 //NOT_SUPPORTED label
  436.  
  437.         xor eax, eax            //Set eax to zero
  438.                     
  439.         _asm _emit 0x0f            //CPUID instruction  (two bytes opcode)
  440.         _asm _emit 0xa2 
  441.         
  442.         cmp eax, 1                //make sure eax return non-zero value
  443.         jl NOT_SUPPORTED        //If eax is zero, mmx not supported
  444.  
  445.         xor eax, eax            //set eax to zero
  446.         inc eax                    //Now increment eax to 1.  This instruction is 
  447.                                 //faster than the instruction "mov eax, 1"
  448.         
  449.         _asm _emit 0x0f            //CPUID instruction
  450.         _asm _emit 0xa2 
  451.  
  452.         and edx, 0x00800000        //mask out all bits but mmx bit(24)
  453.         cmp edx, 0                // 0 = mmx not supported
  454.         jz    NOT_SUPPORTED        // non-zero = Yes, mmx IS supported
  455.  
  456.         mov    mmx_supported, 1    //set return value to 1
  457.  
  458. NOT_SUPPORTED:
  459.         mov    eax, mmx_supported    //move return value to eax    
  460.  
  461.     }
  462.  
  463.     return mmx_supported;        
  464. }
  465.  
  466.  
  467.  
  468.  
  469.  
  470. #endif
  471.  
  472.