core/vil/file_formats/vil_jpeg_destination_mgr.cxx
Go to the documentation of this file.
00001 // This is core/vil/file_formats/vil_jpeg_destination_mgr.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 // \verbatim
00009 //  Modifications
00010 //     11 Oct 2002 Ian Scott - converted to vil
00011 //\endverbatim
00012 
00013 #include "vil_jpeg_destination_mgr.h"
00014 #include <vcl_cassert.h>
00015 #include <vcl_cstddef.h> // for vcl_size_t
00016 #include <vil/vil_stream.h>
00017 
00018 #define STATIC /*static*/
00019 
00020 // In ANSI C, and indeed any rational implementation, vcl_size_t is also the
00021 // type returned by sizeof().  However, it seems there are some irrational
00022 // implementations out there, in which sizeof() returns an int even though
00023 // vcl_size_t is defined as long or unsigned long.  To ensure consistent results
00024 // we always use this SIZEOF() macro in place of using sizeof() directly.
00025 
00026 #define SIZEOF(object) ((vcl_size_t) sizeof(object))
00027 
00028 // Implement a jpeg_destination_manager for vil_stream *.
00029 // Adapted by fsm from the FILE * version in jdatadst.c
00030 
00031 #define vil_jpeg_OUTPUT_BUF_SIZE  4096 // choose an efficiently fwrite'able size
00032 typedef vil_jpeg_stream_destination_mgr *vil_jpeg_dstptr;
00033 
00034 
00035 //  * Initialize destination --- called by jpeg_start_compress
00036 //  * before any data is actually written.
00037 STATIC
00038 void
00039 vil_jpeg_init_destination (j_compress_ptr cinfo)
00040 {
00041   vil_jpeg_dstptr dest = (vil_jpeg_dstptr) cinfo->dest; // cast to derived class
00042 
00043   // Allocate the output buffer --- it will be released when done with image
00044   dest->buffer = (JOCTET *)
00045     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
00046                                 JPOOL_IMAGE,
00047                                 vil_jpeg_OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
00048 
00049   dest->base.next_output_byte = dest->buffer;
00050   dest->base.free_in_buffer = vil_jpeg_OUTPUT_BUF_SIZE;
00051 }
00052 
00053 
00054 //: Empty the output buffer --- called whenever buffer fills up.
00055 // 
00056 //  In typical applications, this should write the entire output buffer
00057 //  (ignoring the current state of next_output_byte & free_in_buffer),
00058 //  reset the pointer & count to the start of the buffer, and return TRUE
00059 //  indicating that the buffer has been dumped.
00060 // 
00061 //  In applications that need to be able to suspend compression due to output
00062 //  overrun, a FALSE return indicates that the buffer cannot be emptied now.
00063 //  In this situation, the compressor will return to its caller (possibly with
00064 //  an indication that it has not accepted all the supplied scanlines).  The
00065 //  application should resume compression after it has made more room in the
00066 //  output buffer.  Note that there are substantial restrictions on the use of
00067 //  suspension --- see the documentation.
00068 // 
00069 //  When suspending, the compressor will back up to a convenient restart point
00070 //  (typically the start of the current MCU). next_output_byte & free_in_buffer
00071 //  indicate where the restart point will be if the current call returns FALSE.
00072 //  Data beyond this point will be regenerated after resumption, so do not
00073 //  write it out when emptying the buffer externally.
00074 jpeg_boolean
00075 vil_jpeg_empty_output_buffer (j_compress_ptr cinfo)
00076 {
00077   vil_jpeg_dstptr dest = (vil_jpeg_dstptr) cinfo->dest; // cast to derived class
00078 
00079   if (dest->stream->write(dest->buffer, vil_jpeg_OUTPUT_BUF_SIZE) != (vcl_size_t) vil_jpeg_OUTPUT_BUF_SIZE)
00080     ERREXIT(cinfo, JERR_FILE_WRITE);
00081 
00082   dest->base.next_output_byte = dest->buffer;
00083   dest->base.free_in_buffer = vil_jpeg_OUTPUT_BUF_SIZE;
00084 
00085   return TRUE;
00086 }
00087 
00088 
00089 //: Terminate destination --- called by jpeg_finish_compress after all data has been written.  Usually needs to flush buffer.
00090 // 
00091 //  \note \e not called by jpeg_abort or jpeg_destroy; surrounding
00092 //  application must deal with any cleanup that should happen even
00093 //  for error exit.
00094 void
00095 vil_jpeg_term_destination (j_compress_ptr cinfo)
00096 {
00097   vil_jpeg_dstptr dest = (vil_jpeg_dstptr) cinfo->dest; // cast to derived class
00098   vcl_size_t datacount = vil_jpeg_OUTPUT_BUF_SIZE - dest->base.free_in_buffer;
00099 
00100   // Write any data remaining in the buffer
00101   if (datacount > 0) {
00102     if (dest->stream->write(dest->buffer, datacount) != (vil_streampos)datacount)
00103       ERREXIT(cinfo, JERR_FILE_WRITE);
00104   }
00105 }
00106 
00107 
00108 //: Prepare for output to a vil_stream.
00109 //  The caller must have already opened the stream, and is responsible
00110 //  for closing it after finishing compression.
00111 void
00112 vil_jpeg_stream_dst_set (j_compress_ptr cinfo, vil_stream *vs)
00113 {
00114   // The destination object is made permanent so that multiple JPEG images
00115   // can be written to the same file without re-executing jpeg_stdio_dest.
00116   // This makes it dangerous to use this manager and a different destination
00117   // manager serially with the same JPEG object, because their private object
00118   // sizes may be different.  Caveat programmer.
00119   //
00120   assert(! cinfo->dest); // call this routine only once.
00121 
00122   // allocate
00123   vil_jpeg_dstptr dest = (vil_jpeg_dstptr)
00124     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
00125                                 JPOOL_PERMANENT,
00126                                 SIZEOF(vil_jpeg_stream_destination_mgr));
00127   cinfo->dest = reinterpret_cast<jpeg_destination_mgr *>(dest);
00128 
00129   // fill in methods in base
00130   dest->base.init_destination    = vil_jpeg_init_destination;
00131   dest->base.empty_output_buffer = vil_jpeg_empty_output_buffer;
00132   dest->base.term_destination    = vil_jpeg_term_destination;
00133 
00134   dest->stream = vs;
00135 }
00136 
00137 void
00138 vil_jpeg_stream_dst_rewind(j_compress_ptr cinfo, vil_stream *vs)
00139 {
00140   vil_jpeg_dstptr dst = ( vil_jpeg_dstptr )( cinfo->dest );
00141   { // verify
00142     assert(dst != 0);
00143     assert(dst->stream == vs);
00144   }
00145 
00146   cinfo->dest->next_output_byte = dst->buffer;
00147   cinfo->dest->free_in_buffer = vil_jpeg_OUTPUT_BUF_SIZE;
00148 
00149   vs->seek(0L);
00150 }