00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include "jpeg.h" 00004 #include "jpegwrapper.h" 00005 #include "jerror.h" 00006 #include "filemov.h" 00007 00008 /* Expanded data destination object for stdio output */ 00009 00010 typedef struct { 00011 struct jpeg_destination_mgr pub; /* public fields */ 00012 00013 JOCTET *buffer; // Pointer to buffer 00014 thread_struct *buffer_struct; 00015 } bcast_destination_mgr; 00016 00017 typedef bcast_destination_mgr *bcast_dest_ptr; 00018 00019 00020 /* 00021 * Initialize destination --- called by jpeg_start_compress 00022 * before any data is actually written. 00023 */ 00024 00025 METHODDEF(void) 00026 bcast_init_destination (j_compress_ptr cinfo) 00027 { 00028 bcast_dest_ptr dest = (bcast_dest_ptr) cinfo->dest; 00029 00030 // Set the pointer to the preallocated buffer 00031 dest->buffer = dest->buffer_struct->output_buffer; 00032 00033 dest->pub.next_output_byte = dest->buffer; 00034 dest->pub.free_in_buffer = dest->buffer_struct->output_allocated; 00035 } 00036 00037 00038 /* 00039 * Empty the output buffer --- called whenever buffer fills up. 00040 * 00041 * In typical applications, this should write the entire output buffer 00042 * (ignoring the current state of next_output_byte & free_in_buffer), 00043 * reset the pointer & count to the start of the buffer, and return TRUE 00044 * indicating that the buffer has been dumped. 00045 * 00046 * In applications that need to be able to suspend compression due to output 00047 * overrun, a FALSE return indicates that the buffer cannot be emptied now. 00048 * In this situation, the compressor will return to its caller (possibly with 00049 * an indication that it has not accepted all the supplied scanlines). The 00050 * application should resume compression after it has made more room in the 00051 * output buffer. Note that there are substantial restrictions on the use of 00052 * suspension --- see the documentation. 00053 * 00054 * When suspending, the compressor will back up to a convenient restart point 00055 * (typically the start of the current MCU). next_output_byte & free_in_buffer 00056 * indicate where the restart point will be if the current call returns FALSE. 00057 * Data beyond this point will be regenerated after resumption, so do not 00058 * write it out when emptying the buffer externally. 00059 */ 00060 00061 METHODDEF(boolean) 00062 bcast_empty_output_buffer (j_compress_ptr cinfo) 00063 { 00064 // Allocate a bigger buffer. 00065 bcast_dest_ptr dest = (bcast_dest_ptr) cinfo->dest; 00066 unsigned char *new_buffer; 00067 int new_size, i; 00068 int offset = dest->buffer_struct->output_allocated; 00069 00070 new_size = dest->buffer_struct->output_allocated * 2; 00071 new_buffer = (unsigned char*)malloc(new_size); 00072 for(i = 0; i < offset; i++) 00073 new_buffer[i] = dest->buffer_struct->output_buffer[i]; 00074 free(dest->buffer_struct->output_buffer); 00075 dest->buffer_struct->output_buffer = new_buffer; 00076 dest->buffer_struct->output_allocated = new_size; 00077 dest->buffer = dest->buffer_struct->output_buffer; 00078 00079 dest->pub.next_output_byte = dest->buffer + offset; 00080 dest->pub.free_in_buffer = dest->buffer_struct->output_allocated - offset; 00081 00082 return TRUE; 00083 } 00084 00085 00086 /* 00087 * Terminate destination --- called by jpeg_finish_compress 00088 * after all data has been written. Usually needs to flush buffer. 00089 * 00090 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding 00091 * application must deal with any cleanup that should happen even 00092 * for error exit. 00093 */ 00094 00095 METHODDEF(void) 00096 bcast_term_destination (j_compress_ptr cinfo) 00097 { 00098 // Just get the length 00099 bcast_dest_ptr dest = (bcast_dest_ptr) cinfo->dest; 00100 dest->buffer_struct->output_size = dest->buffer_struct->output_allocated - dest->pub.free_in_buffer; 00101 } 00102 00103 00104 /* 00105 * Prepare for output to a stdio stream. 00106 * The caller must have already opened the stream, and is responsible 00107 * for closing it after finishing compression. 00108 */ 00109 00110 GLOBAL(void) 00111 bcast_jpeg_buffer_dest (j_compress_ptr cinfo, thread_struct *buffer_struct) 00112 { 00113 bcast_dest_ptr dest; 00114 00115 /* The destination object is made permanent so that multiple JPEG images 00116 * can be written to the same file without re-executing jpeg_stdio_dest. 00117 * This makes it dangerous to use this manager and a different destination 00118 * manager serially with the same JPEG object, because their private object 00119 * sizes may be different. Caveat programmer. 00120 */ 00121 if (cinfo->dest == NULL) { /* first time for this JPEG object? */ 00122 cinfo->dest = (struct jpeg_destination_mgr *) 00123 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 00124 sizeof(bcast_destination_mgr)); 00125 } 00126 00127 dest = (bcast_dest_ptr) cinfo->dest; 00128 dest->pub.init_destination = bcast_init_destination; 00129 dest->pub.empty_output_buffer = bcast_empty_output_buffer; 00130 dest->pub.term_destination = bcast_term_destination; 00131 dest->buffer_struct = buffer_struct; 00132 }
1.4.4