Go to the documentation of this file.00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "vil_jpeg_destination_mgr.h"
00014 #include <vcl_cassert.h>
00015 #include <vcl_cstddef.h>
00016 #include <vil/vil_stream.h>
00017
00018 #define STATIC
00019
00020
00021
00022
00023
00024
00025
00026 #define SIZEOF(object) ((vcl_size_t) sizeof(object))
00027
00028
00029
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
00036
00037 STATIC
00038 void
00039 vil_jpeg_init_destination (j_compress_ptr cinfo)
00040 {
00041 vil_jpeg_dstptr dest = (vil_jpeg_dstptr) cinfo->dest;
00042
00043
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
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 jpeg_boolean
00075 vil_jpeg_empty_output_buffer (j_compress_ptr cinfo)
00076 {
00077 vil_jpeg_dstptr dest = (vil_jpeg_dstptr) cinfo->dest;
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
00090
00091
00092
00093
00094 void
00095 vil_jpeg_term_destination (j_compress_ptr cinfo)
00096 {
00097 vil_jpeg_dstptr dest = (vil_jpeg_dstptr) cinfo->dest;
00098 vcl_size_t datacount = vil_jpeg_OUTPUT_BUF_SIZE - dest->base.free_in_buffer;
00099
00100
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
00109
00110
00111 void
00112 vil_jpeg_stream_dst_set (j_compress_ptr cinfo, vil_stream *vs)
00113 {
00114
00115
00116
00117
00118
00119
00120 assert(! cinfo->dest);
00121
00122
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
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 {
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 }