15 #include "pnmFileTypeJPG.h"
19 #include "config_pnmimagetypes.h"
22 #include "pnmWriter.h"
58 struct jpeg_destination_mgr pub;
64 typedef my_destination_mgr * my_dest_ptr;
66 #define OUTPUT_BUF_SIZE 4096
75 init_destination (j_compress_ptr cinfo)
77 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
80 dest->buffer = (JOCTET *)
81 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
82 OUTPUT_BUF_SIZE *
sizeof(JOCTET));
84 dest->pub.next_output_byte = dest->buffer;
85 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
113 empty_output_buffer (j_compress_ptr cinfo)
115 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
117 if (!dest->outfile->write((
const char *)dest->buffer, OUTPUT_BUF_SIZE))
118 ERREXIT(cinfo, JERR_FILE_WRITE);
120 dest->pub.next_output_byte = dest->buffer;
121 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
138 term_destination (j_compress_ptr cinfo)
140 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
141 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
145 if (!dest->outfile->write((
const char *)dest->buffer, datacount))
146 ERREXIT(cinfo, JERR_FILE_WRITE);
148 dest->outfile->flush();
151 if (dest->outfile->fail())
152 ERREXIT(cinfo, JERR_FILE_WRITE);
163 jpeg_ostream_dest (j_compress_ptr cinfo, ostream * outfile)
173 if (cinfo->dest == NULL) {
174 cinfo->dest = (
struct jpeg_destination_mgr *)
175 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
176 sizeof(my_destination_mgr));
179 dest = (my_dest_ptr) cinfo->dest;
180 dest->pub.init_destination = init_destination;
181 dest->pub.empty_output_buffer = empty_output_buffer;
182 dest->pub.term_destination = term_destination;
183 dest->outfile = outfile;
197 PNMFileTypeJPG::Writer::
198 Writer(
PNMFileType *type, ostream *file,
bool owns_file) :
225 int PNMFileTypeJPG::Writer::
226 write_data(
xel *array, xelval *) {
227 if (_y_size<=0 || _x_size<=0) {
237 struct jpeg_compress_struct cinfo;
246 struct jpeg_error_mgr jerr;
248 JSAMPROW row_pointer[1];
258 cinfo.err = jpeg_std_error(&jerr);
261 jpeg_create_compress(&cinfo);
265 jpeg_ostream_dest(&cinfo, _file);
272 cinfo.image_width = _x_size;
273 cinfo.image_height = _y_size;
274 if (is_grayscale()) {
275 cinfo.input_components = 1;
276 cinfo.in_color_space = JCS_GRAYSCALE;
278 cinfo.input_components = 3;
279 cinfo.in_color_space = JCS_RGB;
285 jpeg_set_defaults(&cinfo);
289 jpeg_set_quality(&cinfo, jpeg_quality, TRUE );
296 jpeg_start_compress(&cinfo, TRUE);
299 if (_comment.size()) {
301 &cinfo, JPEG_COM, (JOCTET *)_comment.c_str(), strlen(_comment.c_str()));
312 row_stride = _x_size * cinfo.input_components;
315 JSAMPROW row =
new JSAMPLE[row_stride];
316 while (cinfo.next_scanline < cinfo.image_height) {
321 for (
int i = 0; i < row_stride; i += cinfo.input_components) {
322 if (cinfo.input_components == 1) {
323 row[i] = (JSAMPLE)(MAXJSAMPLE * PPM_GETB(array[x])/_maxval);
325 row[i] = (JSAMPLE)(MAXJSAMPLE * PPM_GETR(array[x])/_maxval);
326 row[i+1] = (JSAMPLE)(MAXJSAMPLE * PPM_GETG(array[x])/_maxval);
327 row[i+2] = (JSAMPLE)(MAXJSAMPLE * PPM_GETB(array[x])/_maxval);
333 row_pointer[0] = row;
334 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
340 jpeg_finish_compress(&cinfo);
345 jpeg_destroy_compress(&cinfo);
This is the base class of a family of classes that represent particular image file types that PNMImag...
static void consider_yield()
Possibly suspends the current thread for the rest of the current epoch, if it has run for enough this...
This is an abstract base class that defines the interface for writing image files of various types...