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
This commit is contained in:
Artem Pavlenko 2005-02-28 21:03:55 +00:00
parent d9c4988b9e
commit 01ad965dc3
2 changed files with 49 additions and 2 deletions

View file

@ -20,6 +20,7 @@
#include <string>
#include <png.h>
#include <jpeglib.h>
#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<width;++i)
{
row[index++]=(imageRow[i])&0xff;
row[index++]=(imageRow[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);
}
}

View file

@ -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 <typename T>