123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include "jinclude.h"
- #include "jpeglib.h"
- #include "jerror.h"
- typedef struct {
- struct jpeg_destination_mgr pub;
- FILE * outfile;
- JOCTET * buffer;
- } my_destination_mgr;
- typedef my_destination_mgr * my_dest_ptr;
- #define OUTPUT_BUF_SIZE 4096
- METHODDEF void
- init_destination (j_compress_ptr cinfo)
- {
- my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
-
- dest->buffer = (JOCTET *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
- dest->pub.next_output_byte = dest->buffer;
- dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
- }
- METHODDEF boolean
- empty_output_buffer (j_compress_ptr cinfo)
- {
- my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
- if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
- (size_t) OUTPUT_BUF_SIZE)
- ERREXIT(cinfo, JERR_FILE_WRITE);
- dest->pub.next_output_byte = dest->buffer;
- dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
- return TRUE;
- }
- METHODDEF void
- term_destination (j_compress_ptr cinfo)
- {
- my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
- size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
-
- if (datacount > 0) {
- if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
- ERREXIT(cinfo, JERR_FILE_WRITE);
- }
- fflush(dest->outfile);
-
- if (ferror(dest->outfile))
- ERREXIT(cinfo, JERR_FILE_WRITE);
- }
- GLOBAL void
- jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
- {
- my_dest_ptr dest;
-
- if (cinfo->dest == NULL) {
- cinfo->dest = (struct jpeg_destination_mgr *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
- SIZEOF(my_destination_mgr));
- }
- dest = (my_dest_ptr) cinfo->dest;
- dest->pub.init_destination = init_destination;
- dest->pub.empty_output_buffer = empty_output_buffer;
- dest->pub.term_destination = term_destination;
- dest->outfile = outfile;
- }
|