From d258151391bead3a167f54068096a8d727ba273a Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Mon, 31 Jul 2006 17:09:25 +0200 Subject: add jpeg write support to DataImage --- src/DataImage.cxx | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/DataImage.cxx') diff --git a/src/DataImage.cxx b/src/DataImage.cxx index efc05e3..f7968b2 100644 --- a/src/DataImage.cxx +++ b/src/DataImage.cxx @@ -23,6 +23,9 @@ #include #include #include +extern "C" { +#include +} #include @@ -31,7 +34,7 @@ DataImage::DataImage(int X, int Y, int W, int H): Fl_Widget(X, Y, W, H) { d = 3; data = (uchar*) malloc(W * H * d); - memset(data, 0, W * H * d); + memset(data, 200, W * H * d); } DataImage::~DataImage() { @@ -117,10 +120,44 @@ DataImage::get_pixel(Fl_Image *img, int x, int y, return 0; } - +int +DataImage::write_jpeg(const char *file, int quality) { + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + FILE *outfile; + JSAMPROW row_pointer[1]; + int row_stride; + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + + if ((outfile = fopen(file, "wb")) == NULL) { + fprintf(stderr, "can't open %s\n", file); + return 1; + } + jpeg_stdio_dest(&cinfo, outfile); + cinfo.image_width = w(); + cinfo.image_height = h(); + cinfo.input_components = 3; /* # of color components per pixel */ + cinfo.in_color_space = JCS_RGB; + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo, quality, TRUE); + jpeg_start_compress(&cinfo, TRUE); + row_stride = w() * 3; /* JSAMPLEs per row in image_buffer */ + while (cinfo.next_scanline < cinfo.image_height) { + row_pointer[0] = & data[cinfo.next_scanline * row_stride]; + jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + + jpeg_finish_compress(&cinfo); + fclose(outfile); + jpeg_destroy_compress(&cinfo); + + return 0; +} -- cgit v1.2.3