home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / quicktime / jdatadst.c < prev    next >
C/C++ Source or Header  |  2000-11-29  |  4KB  |  133 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "jpeg.h"
  4. #include "jpeglib.h"
  5. #include "jerror.h"
  6.  
  7.  
  8. /* Expanded data destination object for stdio output */
  9.  
  10. typedef struct {
  11.   struct jpeg_destination_mgr pub; /* public fields */
  12.  
  13.   JOCTET *buffer;        /* Pointer to buffer */
  14.   mjpa_compress_engine *engine;
  15. } my_destination_mgr;
  16.  
  17. typedef my_destination_mgr *my_dest_ptr;
  18.  
  19.  
  20. /*
  21.  * Initialize destination --- called by jpeg_start_compress
  22.  * before any data is actually written.
  23.  */
  24.  
  25. METHODDEF(void)
  26. init_destination (j_compress_ptr cinfo)
  27. {
  28.   my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  29.  
  30. /* Set the pointer to the preallocated buffer */
  31.   dest->buffer = dest->engine->output_buffer;
  32.  
  33.   dest->pub.next_output_byte = dest->buffer;
  34.   dest->pub.free_in_buffer = dest->engine->output_allocated;
  35. }
  36.  
  37.  
  38. /*
  39.  * Empty the output buffer --- called whenever buffer fills up.
  40.  *
  41.  * In typical applications, this should write the entire output buffer
  42.  * (ignoring the current state of next_output_byte & free_in_buffer),
  43.  * reset the pointer & count to the start of the buffer, and return TRUE
  44.  * indicating that the buffer has been dumped.
  45.  *
  46.  * In applications that need to be able to suspend compression due to output
  47.  * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  48.  * In this situation, the compressor will return to its caller (possibly with
  49.  * an indication that it has not accepted all the supplied scanlines).  The
  50.  * application should resume compression after it has made more room in the
  51.  * output buffer.  Note that there are substantial restrictions on the use of
  52.  * suspension --- see the documentation.
  53.  *
  54.  * When suspending, the compressor will back up to a convenient restart point
  55.  * (typically the start of the current MCU). next_output_byte & free_in_buffer
  56.  * indicate where the restart point will be if the current call returns FALSE.
  57.  * Data beyond this point will be regenerated after resumption, so do not
  58.  * write it out when emptying the buffer externally.
  59.  */
  60.  
  61. METHODDEF(boolean)
  62. empty_output_buffer (j_compress_ptr cinfo)
  63. {
  64. /* Allocate a bigger buffer. */
  65.     my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  66.     char *new_buffer;
  67.     int new_size, i;
  68.     int offset = dest->engine->output_allocated;
  69.  
  70.     new_size = dest->engine->output_allocated * 2;
  71.     new_buffer = malloc(new_size);
  72.     for(i = 0; i < offset; i++)
  73.         new_buffer[i] = dest->engine->output_buffer[i];
  74.     free(dest->engine->output_buffer);
  75.     dest->engine->output_buffer = new_buffer;
  76.     dest->engine->output_allocated = new_size;
  77.     dest->buffer = dest->engine->output_buffer;
  78.  
  79.     dest->pub.next_output_byte = dest->buffer + offset;
  80.     dest->pub.free_in_buffer = dest->engine->output_allocated - offset;
  81.  
  82.     return TRUE;
  83. }
  84.  
  85.  
  86. /*
  87.  * Terminate destination --- called by jpeg_finish_compress
  88.  * after all data has been written.  Usually needs to flush buffer.
  89.  *
  90.  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  91.  * application must deal with any cleanup that should happen even
  92.  * for error exit.
  93.  */
  94.  
  95. METHODDEF(void)
  96. term_destination (j_compress_ptr cinfo)
  97. {
  98. /* Just get the length */
  99.     my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  100.     dest->engine->output_size = dest->engine->output_allocated - dest->pub.free_in_buffer;
  101. }
  102.  
  103.  
  104. /*
  105.  * Prepare for output to a stdio stream.
  106.  * The caller must have already opened the stream, and is responsible
  107.  * for closing it after finishing compression.
  108.  */
  109.  
  110. GLOBAL(void)
  111. jpeg_buffer_dest (j_compress_ptr cinfo, mjpa_compress_engine *engine)
  112. {
  113.   my_dest_ptr dest;
  114.  
  115.   /* The destination object is made permanent so that multiple JPEG images
  116.    * can be written to the same file without re-executing jpeg_stdio_dest.
  117.    * This makes it dangerous to use this manager and a different destination
  118.    * manager serially with the same JPEG object, because their private object
  119.    * sizes may be different.  Caveat programmer.
  120.    */
  121.   if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
  122.     cinfo->dest = (struct jpeg_destination_mgr *)
  123.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  124.                   sizeof(my_destination_mgr));
  125.   }
  126.  
  127.   dest = (my_dest_ptr) cinfo->dest;
  128.   dest->pub.init_destination = init_destination;
  129.   dest->pub.empty_output_buffer = empty_output_buffer;
  130.   dest->pub.term_destination = term_destination;
  131.   dest->engine = engine;
  132. }
  133.