home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jdtrans.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  5.7 KB  |  153 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.  * jdtrans.c
  12.  *
  13.  * Copyright (C) 1995-1997, 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 library routines for transcoding decompression,
  18.  * that is, reading raw DCT coefficient arrays from an input JPEG file.
  19.  * The routines in jdapimin.c will also be needed by a transcoder.
  20.  */
  21.  
  22. #define JPEG_INTERNALS
  23. #include "jinclude.h"
  24. #include "jpeglib.h"
  25.  
  26.  
  27. /* Forward declarations */
  28. LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo);
  29.  
  30.  
  31. /*
  32.  * Read the coefficient arrays from a JPEG file.
  33.  * jpeg_read_header must be completed before calling this.
  34.  *
  35.  * The entire image is read into a set of virtual coefficient-block arrays,
  36.  * one per component.  The return value is a pointer to the array of
  37.  * virtual-array descriptors.  These can be manipulated directly via the
  38.  * JPEG memory manager, or handed off to jpeg_write_coefficients().
  39.  * To release the memory occupied by the virtual arrays, call
  40.  * jpeg_finish_decompress() when done with the data.
  41.  *
  42.  * An alternative usage is to simply obtain access to the coefficient arrays
  43.  * during a buffered-image-mode decompression operation.  This is allowed
  44.  * after any jpeg_finish_output() call.  The arrays can be accessed until
  45.  * jpeg_finish_decompress() is called.  (Note that any call to the library
  46.  * may reposition the arrays, so don't rely on access_virt_barray() results
  47.  * to stay valid across library calls.)
  48.  *
  49.  * Returns NULL if suspended.  This case need be checked only if
  50.  * a suspending data source is used.
  51.  */
  52.  
  53. GLOBAL(jvirt_barray_ptr *)
  54. jpeg_read_coefficients (j_decompress_ptr cinfo)
  55. {
  56.   if (cinfo->global_state == DSTATE_READY) {
  57.     /* First call: initialize active modules */
  58.     transdecode_master_selection(cinfo);
  59.     cinfo->global_state = DSTATE_RDCOEFS;
  60.   }
  61.   if (cinfo->global_state == DSTATE_RDCOEFS) {
  62.     /* Absorb whole file into the coef buffer */
  63.     for (;;) {
  64.       int retcode;
  65.       /* Call progress monitor hook if present */
  66.       if (cinfo->progress != NULL)
  67.     cinfo->progress->progress_monitor(cinfo);
  68.       /* Absorb some more input */
  69.       retcode = (*cinfo->inputctl->consume_input) (cinfo);
  70.       if (retcode == JPEG_SUSPENDED)
  71.     return NULL;
  72.       if (retcode == JPEG_REACHED_EOI)
  73.     break;
  74.       /* Advance progress counter if appropriate */
  75.       if (cinfo->progress != NULL &&
  76.       (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
  77.     if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
  78.       /* startup underestimated number of scans; ratchet up one scan */
  79.       cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
  80.     }
  81.       }
  82.     }
  83.     /* Set state so that jpeg_finish_decompress does the right thing */
  84.     cinfo->global_state = DSTATE_STOPPING;
  85.   }
  86.   /* At this point we should be in state DSTATE_STOPPING if being used
  87.    * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
  88.    * to the coefficients during a full buffered-image-mode decompression.
  89.    */
  90.   if ((cinfo->global_state == DSTATE_STOPPING ||
  91.        cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
  92.     return cinfo->coef->coef_arrays;
  93.   }
  94.   /* Oops, improper usage */
  95.   cinfo->ERREXIT1(JERR_BAD_STATE, cinfo->global_state);
  96.   return NULL;            /* keep compiler happy */
  97. }
  98.  
  99.  
  100. /*
  101.  * Master selection of decompression modules for transcoding.
  102.  * This substitutes for jdmaster.c's initialization of the full decompressor.
  103.  */
  104.  
  105. LOCAL(void)
  106. transdecode_master_selection (j_decompress_ptr cinfo)
  107. {
  108.   /* This is effectively a buffered-image operation. */
  109.   cinfo->buffered_image = TRUE;
  110.  
  111.   /* Entropy decoding: either Huffman or arithmetic coding. */
  112.   if (cinfo->arith_code) {
  113.     cinfo->ERREXIT(JERR_ARITH_NOTIMPL);
  114.   } else {
  115.     if (cinfo->progressive_mode) {
  116. #ifdef D_PROGRESSIVE_SUPPORTED
  117.       jinit_phuff_decoder(cinfo);
  118. #else
  119.       ERREXIT(cinfo, JERR_NOT_COMPILED);
  120. #endif
  121.     } else
  122.       jinit_huff_decoder(cinfo);
  123.   }
  124.  
  125.   /* Always get a full-image coefficient buffer. */
  126.   jinit_d_coef_controller(cinfo, TRUE);
  127.  
  128.   /* We can now tell the memory manager to allocate virtual arrays. */
  129.   cinfo->mem->realize_virt_arrays();
  130.  
  131.   /* Initialize input side of decompressor to consume first scan. */
  132.   (*cinfo->inputctl->start_input_pass) (cinfo);
  133.  
  134.   /* Initialize progress monitoring. */
  135.   if (cinfo->progress != NULL) {
  136.     int nscans;
  137.     /* Estimate number of scans to set pass_limit. */
  138.     if (cinfo->progressive_mode) {
  139.       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
  140.       nscans = 2 + 3 * cinfo->num_components;
  141.     } else if (cinfo->inputctl->has_multiple_scans) {
  142.       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
  143.       nscans = cinfo->num_components;
  144.     } else {
  145.       nscans = 1;
  146.     }
  147.     cinfo->progress->pass_counter = 0L;
  148.     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
  149.     cinfo->progress->completed_passes = 0;
  150.     cinfo->progress->total_passes = 1;
  151.   }
  152. }
  153.