1. templated save_as_png and save_as_jpeg
2. use std::iostream for I/O in save_to_file TODO: allow writing an output image to memory buffer (i.e Python StringIO)
This commit is contained in:
parent
1c5bbea587
commit
b40706c295
1 changed files with 166 additions and 112 deletions
|
@ -37,6 +37,8 @@ extern "C"
|
|||
|
||||
// stl
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace mapnik
|
||||
{
|
||||
|
@ -45,22 +47,32 @@ namespace mapnik
|
|||
std::string const& type,
|
||||
T const& image)
|
||||
{
|
||||
//all that should go into image_writer factory
|
||||
if (type=="png")
|
||||
std::ofstream file (filename.c_str(), std::ios::out| std::ios::trunc|std::ios::binary);
|
||||
if (file)
|
||||
{
|
||||
save_as_png(filename,image);
|
||||
}
|
||||
else if (type=="jpeg")
|
||||
{
|
||||
save_as_jpeg(filename,85,image);
|
||||
//all this should go into image_writer factory
|
||||
if (type=="png") save_as_png(file,image);
|
||||
else if (type=="jpeg") save_as_jpeg(file,85,image);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void save_as_png(std::string const& filename, T const& image)
|
||||
void write_data (png_structp png_ptr, png_bytep data, png_size_t length)
|
||||
{
|
||||
T * out = static_cast<T*>(png_get_io_ptr(png_ptr));
|
||||
out->write(reinterpret_cast<char*>(data), length);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void flush_data (png_structp png_ptr)
|
||||
{
|
||||
T * out = static_cast<T*>(png_get_io_ptr(png_ptr));
|
||||
out->flush();
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
void save_as_png(T1 & file , T2 const& image)
|
||||
{
|
||||
FILE *fp=fopen(filename.c_str(), "wb");
|
||||
if (!fp) return;
|
||||
png_voidp error_ptr=0;
|
||||
png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
||||
error_ptr,0, 0);
|
||||
|
@ -80,17 +92,15 @@ namespace mapnik
|
|||
if (!info_ptr)
|
||||
{
|
||||
png_destroy_write_struct(&png_ptr,(png_infopp)0);
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
png_set_write_fn (png_ptr, &file, &write_data<std::ofstream>, &flush_data<std::ofstream>);
|
||||
|
||||
png_init_io(png_ptr, fp);
|
||||
//png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
|
||||
//png_set_compression_strategy(png_ptr, Z_FILTERED);
|
||||
png_set_IHDR(png_ptr, info_ptr,image.width(),image.height(),8,
|
||||
|
@ -104,14 +114,50 @@ namespace mapnik
|
|||
|
||||
png_write_end(png_ptr, info_ptr);
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void save_as_jpeg(std::string const& filename,int quality, T const& image)
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct jpeg_destination_mgr pub;
|
||||
std::ostream * out;
|
||||
JOCTET * buffer;
|
||||
} dest_mgr;
|
||||
|
||||
void init_destination( j_compress_ptr cinfo)
|
||||
{
|
||||
dest_mgr * dest = reinterpret_cast<dest_mgr*>(cinfo->dest);
|
||||
dest->buffer = (JOCTET*) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
||||
BUFFER_SIZE * sizeof(JOCTET));
|
||||
dest->pub.next_output_byte = dest->buffer;
|
||||
dest->pub.free_in_buffer = BUFFER_SIZE;
|
||||
}
|
||||
|
||||
boolean empty_output_buffer (j_compress_ptr cinfo)
|
||||
{
|
||||
dest_mgr * dest = reinterpret_cast<dest_mgr*>(cinfo->dest);
|
||||
dest->out->write((char*)dest->buffer, BUFFER_SIZE);
|
||||
if (!*(dest->out)) return false;
|
||||
dest->pub.next_output_byte = dest->buffer;
|
||||
dest->pub.free_in_buffer = BUFFER_SIZE;
|
||||
return true;
|
||||
}
|
||||
|
||||
void term_destination( j_compress_ptr cinfo)
|
||||
{
|
||||
dest_mgr * dest = reinterpret_cast<dest_mgr*>(cinfo->dest);
|
||||
size_t size = BUFFER_SIZE - dest->pub.free_in_buffer;
|
||||
if (size > 0)
|
||||
{
|
||||
dest->out->write((char*)dest->buffer, size);
|
||||
}
|
||||
dest->out->flush();
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
void save_as_jpeg(T1 & file,int quality, T2 const& image)
|
||||
{
|
||||
FILE *fp=fopen(filename.c_str(), "wb");
|
||||
if (!fp) return;
|
||||
struct jpeg_compress_struct cinfo;
|
||||
struct jpeg_error_mgr jerr;
|
||||
|
||||
|
@ -120,7 +166,16 @@ namespace mapnik
|
|||
|
||||
cinfo.err = jpeg_std_error(&jerr);
|
||||
jpeg_create_compress(&cinfo);
|
||||
jpeg_stdio_dest(&cinfo, fp);
|
||||
|
||||
cinfo.dest = (struct jpeg_destination_mgr *)(*cinfo.mem->alloc_small)
|
||||
((j_common_ptr) &cinfo, JPOOL_PERMANENT, sizeof(dest_mgr));
|
||||
dest_mgr * dest = (dest_mgr*) cinfo.dest;
|
||||
dest->pub.init_destination = init_destination;
|
||||
dest->pub.empty_output_buffer = empty_output_buffer;
|
||||
dest->pub.term_destination = term_destination;
|
||||
dest->out = &file;
|
||||
|
||||
//jpeg_stdio_dest(&cinfo, fp);
|
||||
cinfo.image_width = width;
|
||||
cinfo.image_height = height;
|
||||
cinfo.input_components = 3;
|
||||
|
@ -129,8 +184,7 @@ namespace mapnik
|
|||
jpeg_set_quality(&cinfo, quality,1);
|
||||
jpeg_start_compress(&cinfo, 1);
|
||||
JSAMPROW row_pointer[1];
|
||||
JSAMPLE* row=new JSAMPLE[width*3];
|
||||
|
||||
JSAMPLE* row=reinterpret_cast<JSAMPLE*>( ::operator new (sizeof(JSAMPLE) * width*3));
|
||||
while (cinfo.next_scanline < cinfo.image_height)
|
||||
{
|
||||
const unsigned* imageRow=image.getRow(cinfo.next_scanline);
|
||||
|
@ -144,9 +198,9 @@ namespace mapnik
|
|||
row_pointer[0] = &row[0];
|
||||
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
||||
}
|
||||
delete [] row;
|
||||
::operator delete(row);
|
||||
|
||||
jpeg_finish_compress(&cinfo);
|
||||
fclose(fp);
|
||||
jpeg_destroy_compress(&cinfo);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue