From 01ad965dc3b016dd1953d15208a7763c918f129f Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Mon, 28 Feb 2005 21:03:55 +0000 Subject: [PATCH] added basic jpeg write support todo: 1. check for libjpeg properly in configure.ac (at the moment it is part of TIFF check) 2. move all image writing stuff to the image_writer_factory --- src/image_util.cc | 50 +++++++++++++++++++++++++++++++++++++++++++++-- src/image_util.hh | 1 + 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/image_util.cc b/src/image_util.cc index 0f95b4ec8..9ff723ed0 100644 --- a/src/image_util.cc +++ b/src/image_util.cc @@ -20,6 +20,7 @@ #include #include +#include #include "graphics.hh" #include "image_util.hh" #include "memory.hh" @@ -39,10 +40,15 @@ namespace mapnik // void ImageUtils::save_to_file(const std::string& filename,const std::string& type,const Image32& image) { + //all that should go into image_writer factory if (type=="png") { save_as_png(filename,image); - } + } + else if (type=="jpeg") + { + save_as_jpeg(filename,85,image); + } } void ImageUtils::save_as_png(const std::string& filename,const Image32& image) @@ -55,7 +61,6 @@ namespace mapnik if (!png_ptr) return; png_set_mem_fn(png_ptr,mem_ptr,malloc_fn,free_fn); - //png_set_bgr(png_ptr); // switch on optimization #if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) @@ -98,4 +103,45 @@ namespace mapnik fclose(fp); } + void ImageUtils::save_as_jpeg(const std::string& filename,int quality, const Image32& image) + { + FILE *fp=fopen(filename.c_str(), "wb"); + if (!fp) return; + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + + int width=image.width(); + int height=image.height(); + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + jpeg_stdio_dest(&cinfo, fp); + cinfo.image_width = width; + cinfo.image_height = height; + cinfo.input_components = 3; + cinfo.in_color_space = JCS_RGB; + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo, quality,1); + jpeg_start_compress(&cinfo, 1); + JSAMPROW row_pointer[1]; + JSAMPLE* row=new JSAMPLE[width*3]; + const ImageData32& imageData=image.data(); + while (cinfo.next_scanline < cinfo.image_height) + { + const unsigned* imageRow=imageData.getRow(cinfo.next_scanline); + int index=0; + for (int i=0;i>8)&0xff; + row[index++]=(imageRow[i]>>16)&0xff; + } + row_pointer[0] = &row[0]; + (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + delete [] row; + jpeg_finish_compress(&cinfo); + fclose(fp); + jpeg_destroy_compress(&cinfo); + } } diff --git a/src/image_util.hh b/src/image_util.hh index 2712b8491..ed7d78484 100644 --- a/src/image_util.hh +++ b/src/image_util.hh @@ -29,6 +29,7 @@ namespace mapnik static void save_to_file(const std::string& filename,const std::string& type,const Image32& image); private: static void save_as_png(const std::string& filename,const Image32& image); + static void save_as_jpeg(const std::string& filename,int quality, const Image32& image); }; template