00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdio.h>
00013 #include <jpeglib.h>
00014 #include <jerror.h>
00015
00016 typedef struct {
00017 struct jpeg_destination_mgr pub;
00018 JOCTET *buf;
00019 size_t bufsize;
00020 size_t jpegsize;
00021 } mem_destination_mgr;
00022
00023 typedef mem_destination_mgr *mem_dest_ptr;
00024
00025 METHODDEF(void) init_destination (j_compress_ptr cinfo)
00026 {
00027 mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
00028
00029 dest->pub.next_output_byte = dest->buf;
00030 dest->pub.free_in_buffer = dest->bufsize;
00031 dest->jpegsize = 0;
00032 }
00033
00034 METHODDEF(boolean) empty_output_buffer (j_compress_ptr cinfo)
00035 {
00036 mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
00037
00038 dest->pub.next_output_byte = dest->buf;
00039 dest->pub.free_in_buffer = dest->bufsize;
00040
00041 return FALSE;
00042 ERREXIT(cinfo, JERR_BUFFER_SIZE);
00043 }
00044
00045 METHODDEF(void) term_destination(j_compress_ptr cinfo)
00046 {
00047 mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
00048 dest->jpegsize = dest->bufsize - dest->pub.free_in_buffer;
00049 }
00050
00051 GLOBAL(void) jpeg_mem_dest (j_compress_ptr cinfo, JOCTET* buf, size_t bufsize)
00052 {
00053 mem_dest_ptr dest;
00054
00055 if (cinfo->dest == NULL) {
00056 cinfo->dest = (struct jpeg_destination_mgr *)
00057 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
00058 sizeof(mem_destination_mgr));
00059 }
00060
00061 dest = (mem_dest_ptr) cinfo->dest;
00062
00063 dest->pub.init_destination = init_destination;
00064 dest->pub.empty_output_buffer = empty_output_buffer;
00065 dest->pub.term_destination = term_destination;
00066
00067 dest->buf = buf;
00068 dest->bufsize = bufsize;
00069 dest->jpegsize = 0;
00070 }
00071
00072 GLOBAL(int) jpeg_mem_size(j_compress_ptr cinfo)
00073 {
00074 mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
00075 return dest->jpegsize;
00076 }