jcapi.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 13k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2.  * jcapimin.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 compression half
  9.  * of the JPEG library.  These are the "minimum" API routines that may be
  10.  * needed in either the normal full-compression case or the transcoding-only
  11.  * case.
  12.  *
  13.  * Most of the routines intended to be called directly by an application
  14.  * are in this file or in jcapistd.c.  But also see jcparam.c for
  15.  * parameter-setup helper routines, jcomapi.c for routines shared by
  16.  * compression and decompression, and jctrans.c for the transcoding case.
  17.  */
  18. #define JPEG_INTERNALS
  19. #include "jinclude.h"
  20. #include "jpeglib.h"
  21. /*
  22.  * Compression initialization.
  23.  * Before calling this, all parameters and a data destination must be set up.
  24.  *
  25.  * We require a write_all_tables parameter as a failsafe check when writing
  26.  * multiple datastreams from the same compression object.  Since prior runs
  27.  * will have left all the tables marked sent_table=TRUE, a subsequent run
  28.  * would emit an abbreviated stream (no tables) by default.  This may be what
  29.  * is wanted, but for safety's sake it should not be the default behavior:
  30.  * programmers should have to make a deliberate choice to emit abbreviated
  31.  * images.  Therefore the documentation and examples should encourage people
  32.  * to pass write_all_tables=TRUE; then it will take active thought to do the
  33.  * wrong thing.
  34.  */
  35. GLOBAL (void)
  36. jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
  37. {
  38.   if (cinfo->global_state != CSTATE_START)
  39.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  40.   if (write_all_tables)
  41.     jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
  42.   /* (Re)initialize error mgr and destination modules */
  43.   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  44.   (*cinfo->dest->init_destination) (cinfo);
  45.   /* Perform master selection of active modules */
  46.   jinit_compress_master(cinfo);
  47.   /* Set up for the first pass */
  48.   (*cinfo->master->prepare_for_pass) (cinfo);
  49.   /* Ready for application to drive first pass through jpeg_write_scanlines
  50.    * or jpeg_write_raw_data.
  51.    */
  52.   cinfo->next_scanline = 0;
  53.   cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
  54. }
  55. /*
  56.  * Initialization of a JPEG compression object.
  57.  * The error manager must already be set up (in case memory manager fails).
  58.  */
  59. GLOBAL(void)
  60. jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
  61. {
  62.   int i;
  63.   /* Guard against version mismatches between library and caller. */
  64.   cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
  65.   if (version != JPEG_LIB_VERSION)
  66.     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
  67.   if (structsize != SIZEOF(struct jpeg_compress_struct))
  68.     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
  69.      (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
  70.   /* For debugging purposes, zero the whole master structure.
  71.    * But error manager pointer is already there, so save and restore it.
  72.    */
  73.   {
  74.     struct jpeg_error_mgr * err = cinfo->err;
  75.     MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
  76.     cinfo->err = err;
  77.   }
  78.   cinfo->is_decompressor = FALSE;
  79.   /* Initialize a memory manager instance for this object */
  80.   jinit_memory_mgr((j_common_ptr) cinfo);
  81.   /* Zero out pointers to permanent structures. */
  82.   cinfo->progress = NULL;
  83.   cinfo->dest = NULL;
  84.   cinfo->comp_info = NULL;
  85.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  86.     cinfo->quant_tbl_ptrs[i] = NULL;
  87.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  88.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  89.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  90.   }
  91.   cinfo->input_gamma = 1.0; /* in case application forgets */
  92.   /* OK, I'm ready */
  93.   cinfo->global_state = CSTATE_START;
  94. }
  95. /*
  96.  * Destruction of a JPEG compression object
  97.  */
  98. GLOBAL (void)
  99. jpeg_destroy_compress (j_compress_ptr cinfo)
  100. {
  101.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  102. }
  103. /*
  104.  * Abort processing of a JPEG compression operation,
  105.  * but don't destroy the object itself.
  106.  */
  107. GLOBAL (void)
  108. jpeg_abort_compress (j_compress_ptr cinfo)
  109. {
  110.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  111. }
  112. /*
  113.  * Forcibly suppress or un-suppress all quantization and Huffman tables.
  114.  * Marks all currently defined tables as already written (if suppress)
  115.  * or not written (if !suppress).  This will control whether they get emitted
  116.  * by a subsequent jpeg_start_compress call.
  117.  *
  118.  * This routine is exported for use by applications that want to produce
  119.  * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
  120.  * since it is called by jpeg_start_compress, we put it here --- otherwise
  121.  * jcparam.o would be linked whether the application used it or not.
  122.  */
  123. GLOBAL (void)
  124. jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
  125. {
  126.   int i;
  127.   JQUANT_TBL * qtbl;
  128.   JHUFF_TBL * htbl;
  129.   for (i = 0; i < NUM_QUANT_TBLS; i++) {
  130.     if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
  131.       qtbl->sent_table = suppress;
  132.   }
  133.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  134.     if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
  135.       htbl->sent_table = suppress;
  136.     if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
  137.       htbl->sent_table = suppress;
  138.   }
  139. }
  140. /*
  141.  * Write some scanlines of data to the JPEG compressor.
  142.  *
  143.  * The return value will be the number of lines actually written.
  144.  * This should be less than the supplied num_lines only in case that
  145.  * the data destination module has requested suspension of the compressor,
  146.  * or if more than image_height scanlines are passed in.
  147.  *
  148.  * Note: we warn about excess calls to jpeg_write_scanlines() since
  149.  * this likely signals an application programmer error.  However,
  150.  * excess scanlines passed in the last valid call are *silently* ignored,
  151.  * so that the application need not adjust num_lines for end-of-image
  152.  * when using a multiple-scanline buffer.
  153.  */
  154. GLOBAL (JDIMENSION)
  155. jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
  156.       JDIMENSION num_lines)
  157. {
  158.   JDIMENSION row_ctr, rows_left;
  159.   if (cinfo->global_state != CSTATE_SCANNING)
  160.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  161.   if (cinfo->next_scanline >= cinfo->image_height)
  162.     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  163.   /* Call progress monitor hook if present */
  164.   if (cinfo->progress != NULL) {
  165.     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  166.     cinfo->progress->pass_limit = (long) cinfo->image_height;
  167.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  168.   }
  169.   /* Give master control module another chance if this is first call to
  170.    * jpeg_write_scanlines.  This lets output of the frame/scan headers be
  171.    * delayed so that application can write COM, etc, markers between
  172.    * jpeg_start_compress and jpeg_write_scanlines.
  173.    */
  174.   if (cinfo->master->call_pass_startup)
  175.     (*cinfo->master->pass_startup) (cinfo);
  176.   /* Ignore any extra scanlines at bottom of image. */
  177.   rows_left = cinfo->image_height - cinfo->next_scanline;
  178.   if (num_lines > rows_left)
  179.     num_lines = rows_left;
  180.   row_ctr = 0;
  181.   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
  182.   cinfo->next_scanline += row_ctr;
  183.   return row_ctr;
  184. }
  185. /*
  186.  * Alternate entry point to write raw data.
  187.  * Processes exactly one iMCU row per call, unless suspended.
  188.  */
  189. GLOBAL (JDIMENSION)
  190. jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
  191.      JDIMENSION num_lines)
  192. {
  193.   JDIMENSION lines_per_iMCU_row;
  194.   if (cinfo->global_state != CSTATE_RAW_OK)
  195.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  196.   if (cinfo->next_scanline >= cinfo->image_height) {
  197.     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
  198.     return 0;
  199.   }
  200.   /* Call progress monitor hook if present */
  201.   if (cinfo->progress != NULL) {
  202.     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
  203.     cinfo->progress->pass_limit = (long) cinfo->image_height;
  204.     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  205.   }
  206.   /* Give master control module another chance if this is first call to
  207.    * jpeg_write_raw_data.  This lets output of the frame/scan headers be
  208.    * delayed so that application can write COM, etc, markers between
  209.    * jpeg_start_compress and jpeg_write_raw_data.
  210.    */
  211.   if (cinfo->master->call_pass_startup)
  212.     (*cinfo->master->pass_startup) (cinfo);
  213.   /* Verify that at least one iMCU row has been passed. */
  214.   lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
  215.   if (num_lines < lines_per_iMCU_row)
  216.     ERREXIT(cinfo, JERR_BUFFER_SIZE);
  217.   /* Directly compress the row. */
  218.   if (! (*cinfo->coef->compress_data) (cinfo, data)) {
  219.     /* If compressor did not consume the whole row, suspend processing. */
  220.     return 0;
  221.   }
  222.   /* OK, we processed one iMCU row. */
  223.   cinfo->next_scanline += lines_per_iMCU_row;
  224.   return lines_per_iMCU_row;
  225. }
  226. /*
  227.  * Finish JPEG compression.
  228.  *
  229.  * If a multipass operating mode was selected, this may do a great deal of
  230.  * work including most of the actual output.
  231.  */
  232. GLOBAL (void)
  233. jpeg_finish_compress (j_compress_ptr cinfo)
  234. {
  235.   JDIMENSION iMCU_row;
  236.   if (cinfo->global_state == CSTATE_SCANNING ||
  237.       cinfo->global_state == CSTATE_RAW_OK) {
  238.     /* Terminate first pass */
  239.     if (cinfo->next_scanline < cinfo->image_height)
  240.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  241.     (*cinfo->master->finish_pass) (cinfo);
  242.   } else if (cinfo->global_state != CSTATE_WRCOEFS)
  243.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  244.   /* Perform any remaining passes */
  245.   while (! cinfo->master->is_last_pass) {
  246.     (*cinfo->master->prepare_for_pass) (cinfo);
  247.     for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
  248.       if (cinfo->progress != NULL) {
  249. cinfo->progress->pass_counter = (long) iMCU_row;
  250. cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
  251. (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
  252.       }
  253.       /* We bypass the main controller and invoke coef controller directly;
  254.        * all work is being done from the coefficient buffer.
  255.        */
  256.       if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
  257. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  258.     }
  259.     (*cinfo->master->finish_pass) (cinfo);
  260.   }
  261.   /* Write EOI, do final cleanup */
  262.   (*cinfo->marker->write_file_trailer) (cinfo);
  263.   (*cinfo->dest->term_destination) (cinfo);
  264.   /* We can use jpeg_abort to release memory and reset global_state */
  265.   jpeg_abort((j_common_ptr) cinfo);
  266. }
  267. /*
  268.  * Write a special marker.
  269.  * This is only recommended for writing COM or APPn markers.
  270.  * Must be called after jpeg_start_compress() and before
  271.  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
  272.  */
  273. GLOBAL (void)
  274. jpeg_write_marker (j_compress_ptr cinfo, int marker,
  275.    const JOCTET *dataptr, unsigned int datalen)
  276. {
  277.   if (cinfo->next_scanline != 0 ||
  278.       (cinfo->global_state != CSTATE_SCANNING &&
  279.        cinfo->global_state != CSTATE_RAW_OK &&
  280.        cinfo->global_state != CSTATE_WRCOEFS))
  281.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  282.   (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
  283. }
  284. /*
  285.  * Alternate compression function: just write an abbreviated table file.
  286.  * Before calling this, all parameters and a data destination must be set up.
  287.  *
  288.  * To produce a pair of files containing abbreviated tables and abbreviated
  289.  * image data, one would proceed as follows:
  290.  *
  291.  * initialize JPEG object
  292.  * set JPEG parameters
  293.  * set destination to table file
  294.  * jpeg_write_tables(cinfo);
  295.  * set destination to image file
  296.  * jpeg_start_compress(cinfo, FALSE);
  297.  * write data...
  298.  * jpeg_finish_compress(cinfo);
  299.  *
  300.  * jpeg_write_tables has the side effect of marking all tables written
  301.  * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
  302.  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
  303.  */
  304. GLOBAL (void)
  305. jpeg_write_tables (j_compress_ptr cinfo)
  306. {
  307.   if (cinfo->global_state != CSTATE_START)
  308.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  309.   /* (Re)initialize error mgr and destination modules */
  310.   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
  311.   (*cinfo->dest->init_destination) (cinfo);
  312.   /* Initialize the marker writer ... bit of a crock to do it here. */
  313.   jinit_marker_writer(cinfo);
  314.   /* Write them tables! */
  315.   (*cinfo->marker->write_tables_only) (cinfo);
  316.   /* And clean up. */
  317.   (*cinfo->dest->term_destination) (cinfo);
  318.   /* We can use jpeg_abort to release memory. */
  319. #ifndef NIFTY
  320.   jpeg_abort((j_common_ptr) cinfo);
  321. #endif
  322. }