4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains compression data destination routines for the case of
9 * emitting JPEG data to a file (or any stdio stream). While these routines
10 * are sufficient for most applications, some will want to use a different
11 * destination manager.
12 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
13 * JOCTETs into 8-bit-wide elements on external storage. If char is wider
14 * than 8 bits on your machine, you may need to do some tweaking.
17 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
20 /* Expanded data destination object for stdio output */
23 struct jpeg_destination_mgr pub; /* public fields */
25 std::ofstream * outfile; /* target stream */
26 JOCTET * buffer; /* start of buffer */
29 typedef my_destination_mgr * my_dest_ptr;
31 #define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
35 * Initialize destination --- called by jpeg_start_compress
36 * before any data is actually written.
40 init_destination (j_compress_ptr cinfo)
42 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
44 /* Allocate the output buffer --- it will be released when done with image */
45 dest->buffer = (JOCTET *)
46 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
47 OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
49 dest->pub.next_output_byte = dest->buffer;
50 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
55 * Empty the output buffer --- called whenever buffer fills up.
57 * In typical applications, this should write the entire output buffer
58 * (ignoring the current state of next_output_byte & free_in_buffer),
59 * reset the pointer & count to the start of the buffer, and return TRUE
60 * indicating that the buffer has been dumped.
62 * In applications that need to be able to suspend compression due to output
63 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
64 * In this situation, the compressor will return to its caller (possibly with
65 * an indication that it has not accepted all the supplied scanlines). The
66 * application should resume compression after it has made more room in the
67 * output buffer. Note that there are substantial restrictions on the use of
68 * suspension --- see the documentation.
70 * When suspending, the compressor will back up to a convenient restart point
71 * (typically the start of the current MCU). next_output_byte & free_in_buffer
72 * indicate where the restart point will be if the current call returns FALSE.
73 * Data beyond this point will be regenerated after resumption, so do not
74 * write it out when emptying the buffer externally.
78 empty_output_buffer (j_compress_ptr cinfo)
80 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
83 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
84 (size_t) OUTPUT_BUF_SIZE)
85 ERREXIT(cinfo, JERR_FILE_WRITE);
87 dest->outfile->write((char*)dest->buffer, OUTPUT_BUF_SIZE);
88 if( dest->outfile->fail() )
89 ERREXIT(cinfo, JERR_FILE_WRITE);
92 dest->pub.next_output_byte = dest->buffer;
93 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
100 * Terminate destination --- called by jpeg_finish_compress
101 * after all data has been written. Usually needs to flush buffer.
103 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
104 * application must deal with any cleanup that should happen even
109 term_destination (j_compress_ptr cinfo)
111 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
112 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
114 /* Write any data remaining in the buffer */
117 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
118 ERREXIT(cinfo, JERR_FILE_WRITE);
120 fflush(dest->outfile);
121 /* Make sure we wrote the output file OK */
122 if (ferror(dest->outfile))
123 ERREXIT(cinfo, JERR_FILE_WRITE);
126 dest->outfile->write((char*)dest->buffer, datacount);
127 if (dest->outfile->fail())
128 ERREXIT(cinfo, JERR_FILE_WRITE);
129 dest->outfile->flush();
130 /* Make sure we wrote the output file OK */
131 if (dest->outfile->fail())
132 ERREXIT(cinfo, JERR_FILE_WRITE);
139 * Prepare for output to a stdio stream.
140 * The caller must have already opened the stream, and is responsible
141 * for closing it after finishing compression.
145 jpeg_stdio_dest (j_compress_ptr cinfo, std::ofstream * outfile)
149 /* The destination object is made permanent so that multiple JPEG images
150 * can be written to the same file without re-executing jpeg_stdio_dest.
151 * This makes it dangerous to use this manager and a different destination
152 * manager serially with the same JPEG object, because their private object
153 * sizes may be different. Caveat programmer.
155 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
156 cinfo->dest = (struct jpeg_destination_mgr *)
157 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
158 SIZEOF(my_destination_mgr));
161 dest = (my_dest_ptr) cinfo->dest;
162 dest->pub.init_destination = init_destination;
163 dest->pub.empty_output_buffer = empty_output_buffer;
164 dest->pub.term_destination = term_destination;
165 dest->outfile = outfile;