home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jdapimin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  12.4 KB  |  404 lines

  1. //-------------------------------------------------------------------------//
  2. //          Windows Graphics Programming: Win32 GDI and DirectDraw         //
  3. //                        ISBN  0-13-086985-6                              //
  4. //                                                                         //
  5. //  Modified by: Yuan, Feng                             www.fengyuan.com   //
  6. //  Changes    : C++, exception, in-memory source, BGR byte order          //
  7. //  Version    : 1.00.000, May 31, 2000                                    //
  8. //-------------------------------------------------------------------------//
  9.  
  10. /*
  11.  * jdapimin.c
  12.  *
  13.  * Copyright (C) 1994-1998, Thomas G. Lane.
  14.  * This file is part of the Independent JPEG Group's software.
  15.  * For conditions of distribution and use, see the accompanying README file.
  16.  *
  17.  * This file contains application interface code for the decompression half
  18.  * of the JPEG library.  These are the "minimum" API routines that may be
  19.  * needed in either the normal full-decompression case or the
  20.  * transcoding-only case.
  21.  *
  22.  * Most of the routines intended to be called directly by an application
  23.  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
  24.  * shared by compression and decompression, and jdtrans.c for the transcoding
  25.  * case.
  26.  */
  27.  
  28. #define JPEG_INTERNALS
  29. #include "jinclude.h"
  30. #include "jpeglib.h"
  31.  
  32.  
  33. /*
  34.  * Initialization of a JPEG decompression object.
  35.  * The error manager must already be set up (in case memory manager fails).
  36.  */
  37.  
  38. void jpeg_decompress_struct::jpeg_CreateDecompress(int version, size_t structsize)
  39. {
  40.     int i;
  41.  
  42.     /* Guard against version mismatches between library and caller. */
  43.     mem = NULL;        /* so jpeg_destroy knows mem mgr not called */
  44.   
  45.     if (version != JPEG_LIB_VERSION)
  46.         ERREXIT2(JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  47.   
  48.     if (structsize != sizeof(jpeg_decompress_struct))
  49.         ERREXIT2(JERR_BAD_STRUCT_SIZE, 
  50.          (int) sizeof(jpeg_decompress_struct), (int) structsize);
  51.  
  52.     /* For debugging purposes, we zero the whole master structure.
  53.     * But the application has already set the err pointer, and may have set
  54.     * client_data, so we have to save and restore those fields.
  55.     * Note: if application hasn't set client_data, tools like Purify may
  56.     * complain here.
  57.     */
  58.     /*
  59.     {
  60.         struct jpeg_error_mgr * e = err;
  61.         void * c_data = client_data; // ignore Purify complaint here 
  62.         memset(this, 0, sizeof(jpeg_decompress_struct));
  63.         
  64.         err = e
  65.         client_data = c_data;
  66.     }
  67.     */
  68.     is_decompressor = TRUE;
  69.  
  70.     /* Initialize a memory manager instance for this object */
  71.     jinit_memory_mgr(this);
  72.  
  73.     /* Zero out pointers to permanent structures. */
  74.     progress = NULL;
  75.     src = NULL;
  76.  
  77.     for (i = 0; i < NUM_QUANT_TBLS; i++)
  78.         quant_tbl_ptrs[i] = NULL;
  79.  
  80.     for (i = 0; i < NUM_HUFF_TBLS; i++) 
  81.     {
  82.         dc_huff_tbl_ptrs[i] = NULL;
  83.         ac_huff_tbl_ptrs[i] = NULL;
  84.     }
  85.  
  86.     /* Initialize marker processor so application can override methods
  87.     * for COM, APPn markers before calling jpeg_read_header.
  88.     */
  89.     marker_list = NULL;
  90.     jinit_marker_reader(this);
  91.  
  92.     /* And initialize the overall input controller. */
  93.     jinit_input_controller(this);
  94.  
  95.     /* OK, I'm ready */
  96.     global_state = DSTATE_START;
  97. }
  98.  
  99.  
  100. // Destruction of a JPEG decompression object
  101. void jpeg_decompress_struct::jpeg_destroy_decompress(void)
  102. {
  103.     jpeg_destroy(); /* use common routine */
  104. }
  105.  
  106.  
  107. // Abort processing of a JPEG decompression operation,
  108. // but don't destroy the object itself.
  109. void jpeg_decompress_struct::jpeg_abort_decompress(void)
  110. {
  111.     jpeg_abort((j_common_ptr) this); /* use common routine */
  112. }
  113.  
  114.  
  115. // Set default decompression parameters.
  116. void jpeg_decompress_struct::default_decompress_parms(void)
  117. {
  118.     /* Guess the input colorspace, and set output colorspace accordingly. */
  119.     /* (Wish JPEG committee had provided a real way to specify this...) */
  120.     /* Note application may override our guesses. */
  121.     switch (num_components) 
  122.     {
  123.         case 1:
  124.             jpeg_color_space = JCS_GRAYSCALE;
  125.             out_color_space = JCS_GRAYSCALE;
  126.             break;
  127.     
  128.         case 3:
  129.             if (saw_JFIF_marker) 
  130.             {
  131.                 jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  132.             } 
  133.             else if (saw_Adobe_marker) 
  134.             {
  135.                 switch (Adobe_transform) 
  136.                 {
  137.                     case 0: jpeg_color_space = JCS_RGB;
  138.                             break;
  139.                     case 1: jpeg_color_space = JCS_YCbCr;
  140.                             break;
  141.                     default:
  142.                             WARNMS1(JWRN_ADOBE_XFORM, Adobe_transform);
  143.                             jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  144.                             break;
  145.                 }
  146.             } 
  147.             else 
  148.             {
  149.                 /* Saw no special markers, try to guess from the component IDs */
  150.                 int cid0 = comp_info[0].component_id;
  151.                 int cid1 = comp_info[1].component_id;
  152.                 int cid2 = comp_info[2].component_id;
  153.  
  154.                 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  155.                     jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  156.                 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  157.                     jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  158.                 else 
  159.                 {
  160.                     TRACEMS3(this, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  161.                     jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  162.                 }
  163.             }
  164.             /* Always guess RGB is proper output colorspace. */
  165.             out_color_space = JCS_RGB;
  166.             break;
  167.     
  168.         case 4:
  169.             if (saw_Adobe_marker) 
  170.             {
  171.                 switch (Adobe_transform) 
  172.                 {
  173.                     case 0: jpeg_color_space = JCS_CMYK;
  174.                             break;
  175.                     case 2: jpeg_color_space = JCS_YCCK;
  176.                             break;
  177.                     default:WARNMS1(JWRN_ADOBE_XFORM, Adobe_transform);
  178.                             jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  179.                             break;
  180.                 }
  181.             } 
  182.             else 
  183.             {
  184.                 /* No special markers, assume straight CMYK. */
  185.                 jpeg_color_space = JCS_CMYK;
  186.             }
  187.             out_color_space = JCS_CMYK;
  188.             break;
  189.     
  190.         default:
  191.             jpeg_color_space = JCS_UNKNOWN;
  192.             out_color_space = JCS_UNKNOWN;
  193.             break;
  194.     }
  195.  
  196.     /* Set defaults for other decompression parameters. */
  197.     scale_num = 1;        /* 1:1 scaling */
  198.     scale_denom = 1;
  199.     output_gamma = 1.0;
  200.     buffered_image = FALSE;
  201.     raw_data_out = FALSE;
  202.     dct_method = JDCT_DEFAULT;
  203.     do_fancy_upsampling = TRUE;
  204.     do_block_smoothing = TRUE;
  205.     quantize_colors = FALSE;
  206.     /* We set these in case application only sets quantize_colors. */
  207.     dither_mode = JDITHER_FS;
  208. #ifdef QUANT_2PASS_SUPPORTED
  209.     two_pass_quantize = TRUE;
  210. #else
  211.     two_pass_quantize = FALSE;
  212. #endif
  213.     desired_number_of_colors = 256;
  214.     colormap = NULL;
  215.     /* Initialize for no mode change in buffered-image mode. */
  216.     enable_1pass_quant = FALSE;
  217.     enable_external_quant = FALSE;
  218.     enable_2pass_quant = FALSE;
  219. }
  220.  
  221.  
  222. /*
  223.  * Decompression startup: read start of JPEG datastream to see what's there.
  224.  * Need only initialize JPEG object and supply a data source before calling.
  225.  *
  226.  * This routine will read as far as the first SOS marker (ie, actual start of
  227.  * compressed data), and will save all tables and parameters in the JPEG
  228.  * object.  It will also initialize the decompression parameters to default
  229.  * values, and finally return JPEG_HEADER_OK.  On return, the application may
  230.  * adjust the decompression parameters and then call jpeg_start_decompress.
  231.  * (Or, if the application only wanted to determine the image parameters,
  232.  * the data need not be decompressed.  In that case, call jpeg_abort or
  233.  * jpeg_destroy to release any temporary space.)
  234.  * If an abbreviated (tables only) datastream is presented, the routine will
  235.  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  236.  * re-use the JPEG object to read the abbreviated image datastream(s).
  237.  * It is unnecessary (but OK) to call jpeg_abort in this case.
  238.  * The JPEG_SUSPENDED return code only occurs if the data source module
  239.  * requests suspension of the decompressor.  In this case the application
  240.  * should load more source data and then re-call jpeg_read_header to resume
  241.  * processing.
  242.  * If a non-suspending data source is used and require_image is TRUE, then the
  243.  * return code need not be inspected since only JPEG_HEADER_OK is possible.
  244.  *
  245.  * This routine is now just a front end to jpeg_consume_input, with some
  246.  * extra error checking.
  247.  */
  248.  
  249. int jpeg_decompress_struct::jpeg_read_header (boolean require_image)
  250. {
  251.   int retcode;
  252.  
  253.   if (global_state != DSTATE_START &&
  254.       global_state != DSTATE_INHEADER)
  255.     ERREXIT1(JERR_BAD_STATE, global_state);
  256.  
  257.   retcode = jpeg_consume_input();
  258.  
  259.   switch (retcode) {
  260.   case JPEG_REACHED_SOS:
  261.     retcode = JPEG_HEADER_OK;
  262.     break;
  263.   case JPEG_REACHED_EOI:
  264.     if (require_image)        /* Complain if application wanted an image */
  265.         ERREXIT(JERR_NO_IMAGE);
  266.     /* Reset to start state; it would be safer to require the application to
  267.      * call jpeg_abort, but we can't change it now for compatibility reasons.
  268.      * A side effect is to free any temporary memory (there shouldn't be any).
  269.      */
  270.     jpeg_abort((j_common_ptr) this); /* sets state = DSTATE_START */
  271.     retcode = JPEG_HEADER_TABLES_ONLY;
  272.     break;
  273.   case JPEG_SUSPENDED:
  274.     /* no work */
  275.     break;
  276.   }
  277.  
  278.   return retcode;
  279. }
  280.  
  281.  
  282. /*
  283.  * Consume data in advance of what the decompressor requires.
  284.  * This can be called at any time once the decompressor object has
  285.  * been created and a data source has been set up.
  286.  *
  287.  * This routine is essentially a state machine that handles a couple
  288.  * of critical state-transition actions, namely initial setup and
  289.  * transition from header scanning to ready-for-start_decompress.
  290.  * All the actual input is done via the input controller's consume_input
  291.  * method.
  292.  */
  293.  
  294. int jpeg_decompress_struct::jpeg_consume_input(void)
  295. {
  296.   int retcode = JPEG_SUSPENDED;
  297.  
  298.   /* NB: every possible DSTATE value should be listed in this switch */
  299.   switch (global_state) {
  300.   case DSTATE_START:
  301.     /* Start-of-datastream actions: reset appropriate modules */
  302.     (*inputctl->reset_input_controller) (this);
  303.     /* Initialize application's data source module */
  304.     src->init_source(this);
  305.     global_state = DSTATE_INHEADER;
  306.     /*FALLTHROUGH*/
  307.   case DSTATE_INHEADER:
  308.     retcode = (*inputctl->consume_input) (this);
  309.     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  310.       /* Set up default parameters based on header data */
  311.       default_decompress_parms();
  312.       /* Set global state: ready for start_decompress */
  313.       global_state = DSTATE_READY;
  314.     }
  315.     break;
  316.   case DSTATE_READY:
  317.     /* Can't advance past first SOS until start_decompress is called */
  318.     retcode = JPEG_REACHED_SOS;
  319.     break;
  320.   case DSTATE_PRELOAD:
  321.   case DSTATE_PRESCAN:
  322.   case DSTATE_SCANNING:
  323.   case DSTATE_RAW_OK:
  324.   case DSTATE_BUFIMAGE:
  325.   case DSTATE_BUFPOST:
  326.   case DSTATE_STOPPING:
  327.     retcode = (*inputctl->consume_input) (this);
  328.     break;
  329.   default:
  330.     ERREXIT1(JERR_BAD_STATE, global_state);
  331.   }
  332.   return retcode;
  333. }
  334.  
  335.  
  336. // Have we finished reading the input file?
  337. boolean jpeg_decompress_struct::jpeg_input_complete(void)
  338. {
  339.     /* Check for valid jpeg object */
  340.     if ( global_state < DSTATE_START || global_state > DSTATE_STOPPING )
  341.         ERREXIT1(JERR_BAD_STATE, global_state);
  342.   
  343.     return inputctl->eoi_reached;
  344. }
  345.  
  346.  
  347. // Is there more than one scan?
  348. boolean jpeg_decompress_struct::jpeg_has_multiple_scans (void)
  349. {
  350.     /* Only valid after jpeg_read_header completes */
  351.     if ( global_state < DSTATE_READY || global_state > DSTATE_STOPPING    )
  352.         ERREXIT1(JERR_BAD_STATE, global_state);
  353.   
  354.     return inputctl->has_multiple_scans;
  355. }
  356.  
  357.  
  358. /*
  359.  * Finish JPEG decompression.
  360.  *
  361.  * This will normally just verify the file trailer and release temp storage.
  362.  *
  363.  * Returns FALSE if suspended.  The return value need be inspected only if
  364.  * a suspending data source is used.
  365.  */
  366.  
  367. boolean jpeg_decompress_struct::jpeg_finish_decompress(void)
  368. {
  369.     if ((global_state == DSTATE_SCANNING ||
  370.        global_state == DSTATE_RAW_OK) && ! buffered_image) 
  371.     {
  372.         /* Terminate final pass of non-buffered mode */
  373.         if (output_scanline < output_height)
  374.             ERREXIT(JERR_TOO_LITTLE_DATA);
  375.     
  376.         (*master->finish_output_pass) (this);
  377.         global_state = DSTATE_STOPPING;
  378.     } 
  379.     else if (global_state == DSTATE_BUFIMAGE) 
  380.     {
  381.         /* Finishing after a buffered-image operation */
  382.         global_state = DSTATE_STOPPING;
  383.     } 
  384.     else if (global_state != DSTATE_STOPPING) 
  385.     {
  386.         /* STOPPING = repeat call after a suspension, anything else is error */
  387.         ERREXIT1(JERR_BAD_STATE, global_state);
  388.     }
  389.     
  390.     /* Read until EOI */
  391.     while (! inputctl->eoi_reached) 
  392.     {
  393.         if ((*inputctl->consume_input) (this) == JPEG_SUSPENDED)
  394.             return FALSE;        /* Suspend, come back later */
  395.     }
  396.   
  397.     /* Do final cleanup */
  398.     src->term_source(this);
  399.   
  400.     /* We can use jpeg_abort to release memory and reset global_state */
  401.     jpeg_abort(this);
  402.     return TRUE;
  403. }
  404.