2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
//$Id: image_util.cpp 36 2005-04-05 14:32:18Z pavlenko $
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <mapnik/image_util.hpp>
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/graphics.hpp>
|
|
|
|
#include <mapnik/memory.hpp>
|
1. hit_test implementation for geometry objects:
bool hit_test(double x, double y, double tol);
2. added image_view(unsigned x, unsigned y, unsigned width, unsigned height)
allowing to select region from image data e.g (in Python):
im = Image(2048,2048)
view = im.view(0,0,256,256)
save_to_file(filename,type, view)
3. changed envelope method to return vy value in datasource classes
4. features_at_point impl for shape and postgis plug-ins
2006-11-25 12:02:59 +01:00
|
|
|
#include <mapnik/image_view.hpp>
|
2007-12-10 20:59:17 +01:00
|
|
|
#include <mapnik/octree.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
// jpeg png
|
2006-08-31 23:32:07 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <png.h>
|
|
|
|
#include <jpeglib.h>
|
2006-08-31 23:32:07 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-08 19:42:41 +02:00
|
|
|
// stl
|
|
|
|
#include <string>
|
2007-10-17 16:47:56 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2007-10-08 19:42:41 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
2007-10-17 16:47:56 +02:00
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
void save_to_file(std::string const& filename,
|
|
|
|
std::string const& type,
|
|
|
|
T const& image)
|
|
|
|
{
|
|
|
|
std::ofstream file (filename.c_str(), std::ios::out| std::ios::trunc|std::ios::binary);
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
//all this should go into image_writer factory
|
|
|
|
if (type=="png") save_as_png(file,image);
|
2007-12-10 20:59:17 +01:00
|
|
|
else if (type == "png256") save_as_png256(file,image);
|
2007-10-17 16:47:56 +02:00
|
|
|
else if (type=="jpeg") save_as_jpeg(file,85,image);
|
2007-12-19 17:34:36 +01:00
|
|
|
else throw ImageWriterException("unknown file type: " + type);
|
2007-10-17 16:47:56 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-06 13:14:29 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void save_to_file(std::string const& filename,
|
|
|
|
T const& image)
|
|
|
|
{
|
|
|
|
std::string type = type_from_filename(filename);
|
|
|
|
save_to_file<T>(filename,type,image);
|
|
|
|
}
|
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
template <typename T>
|
|
|
|
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);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
png_voidp error_ptr=0;
|
|
|
|
png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
|
|
|
error_ptr,0, 0);
|
|
|
|
|
|
|
|
if (!png_ptr) return;
|
|
|
|
|
|
|
|
// switch on optimization only if supported
|
2006-12-18 11:27:09 +01:00
|
|
|
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) && defined(PNG_MMX_CODE_SUPPORTED)
|
2007-10-17 16:47:56 +02:00
|
|
|
png_uint_32 mask, flags;
|
|
|
|
flags = png_get_asm_flags(png_ptr);
|
|
|
|
mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
|
|
|
|
png_set_asm_flags(png_ptr, flags | mask);
|
2006-10-04 13:22:18 +02:00
|
|
|
#endif
|
2007-10-17 16:47:56 +02:00
|
|
|
png_set_filter (png_ptr, 0, PNG_FILTER_NONE);
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (!info_ptr)
|
|
|
|
{
|
|
|
|
png_destroy_write_struct(&png_ptr,(png_infopp)0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
{
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-06 11:59:28 +01:00
|
|
|
png_set_write_fn (png_ptr, &file, &write_data<T1>, &flush_data<T1>);
|
2007-10-17 16:47:56 +02:00
|
|
|
|
|
|
|
//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,
|
|
|
|
PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
for (unsigned i=0;i<image.height();i++)
|
|
|
|
{
|
|
|
|
png_write_row(png_ptr,(png_bytep)image.getRow(i));
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
}
|
|
|
|
|
2007-12-10 20:59:17 +01:00
|
|
|
template <typename T>
|
|
|
|
void reduce_8 (T const& in, ImageData8 & out, octree<rgb> & tree)
|
|
|
|
{
|
|
|
|
unsigned width = in.width();
|
|
|
|
unsigned height = in.height();
|
|
|
|
for (unsigned y = 0; y < height; ++y)
|
|
|
|
{
|
|
|
|
mapnik::ImageData32::pixel_type const * row = in.getRow(y);
|
|
|
|
mapnik::ImageData8::pixel_type * row_out = out.getRow(y);
|
|
|
|
for (unsigned x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
unsigned val = row[x];
|
|
|
|
mapnik::rgb c((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff);
|
|
|
|
uint8_t index = tree.quantize(c);
|
|
|
|
row_out[x] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void reduce_4 (T const& in, ImageData8 & out, octree<rgb> & tree)
|
|
|
|
{
|
|
|
|
unsigned width = in.width();
|
|
|
|
unsigned height = in.height();
|
|
|
|
|
|
|
|
for (unsigned y = 0; y < height; ++y)
|
|
|
|
{
|
|
|
|
mapnik::ImageData32::pixel_type const * row = in.getRow(y);
|
|
|
|
mapnik::ImageData8::pixel_type * row_out = out.getRow(y);
|
|
|
|
|
|
|
|
for (unsigned x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
unsigned val = row[x];
|
|
|
|
mapnik::rgb c((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff);
|
|
|
|
uint8_t index = tree.quantize(c);
|
|
|
|
if (x%2 > 0) index = index<<4;
|
|
|
|
row_out[x>>1] |= index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1-bit but only one color.
|
|
|
|
template <typename T>
|
|
|
|
void reduce_1(T const&, ImageData8 & out, octree<rgb> &)
|
|
|
|
{
|
|
|
|
out.set(0); // only one color!!!
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void save_as_png(T & file, std::vector<mapnik::rgb> & palette,
|
|
|
|
mapnik::ImageData8 const& image,
|
|
|
|
unsigned width,
|
|
|
|
unsigned height,
|
|
|
|
unsigned color_depth)
|
|
|
|
{
|
|
|
|
png_voidp error_ptr=0;
|
|
|
|
png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
|
|
|
error_ptr,0, 0);
|
|
|
|
|
|
|
|
if (!png_ptr) return;
|
|
|
|
|
|
|
|
// switch on optimization only if supported
|
|
|
|
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) && defined(PNG_MMX_CODE_SUPPORTED)
|
|
|
|
png_uint_32 mask, flags;
|
|
|
|
flags = png_get_asm_flags(png_ptr);
|
|
|
|
mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
|
|
|
|
png_set_asm_flags(png_ptr, flags | mask);
|
|
|
|
#endif
|
|
|
|
png_set_filter (png_ptr, 0, PNG_FILTER_NONE);
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (!info_ptr)
|
|
|
|
{
|
|
|
|
png_destroy_write_struct(&png_ptr,(png_infopp)0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
{
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
png_set_write_fn (png_ptr, &file, &write_data<T>, &flush_data<T>);
|
|
|
|
|
|
|
|
png_set_IHDR(png_ptr, info_ptr,width,height,color_depth,
|
|
|
|
PNG_COLOR_TYPE_PALETTE,PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
png_set_PLTE(png_ptr,info_ptr,reinterpret_cast<png_color*>(&palette[0]),palette.size());
|
|
|
|
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
for (unsigned i=0;i<height;i++)
|
|
|
|
{
|
|
|
|
png_write_row(png_ptr,(png_bytep)image.getRow(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T1,typename T2>
|
|
|
|
void save_as_png256(T1 & file, T2 const& image)
|
|
|
|
{
|
|
|
|
octree<rgb> tree(256);
|
|
|
|
unsigned width = image.width();
|
|
|
|
unsigned height = image.height();
|
|
|
|
for (unsigned y = 0; y < height; ++y)
|
|
|
|
{
|
|
|
|
typename T2::pixel_type const * row = image.getRow(y);
|
|
|
|
for (unsigned x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
unsigned val = row[x];
|
|
|
|
tree.insert(mapnik::rgb((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<rgb> palette;
|
|
|
|
tree.create_palette(palette);
|
|
|
|
assert(palette.size() <= 256);
|
|
|
|
|
|
|
|
if (palette.size() > 16 )
|
|
|
|
{
|
|
|
|
// >16 && <=256 colors -> write 8-bit color depth
|
|
|
|
ImageData8 reduced_image(width,height);
|
|
|
|
reduce_8(image,reduced_image,tree);
|
|
|
|
save_as_png(file,palette,reduced_image,width,height,8);
|
|
|
|
}
|
|
|
|
else if (palette.size() == 1)
|
|
|
|
{
|
|
|
|
// 1 color image -> write 1-bit color depth PNG
|
|
|
|
unsigned image_width = (int(0.125*width) + 7)&~7;
|
|
|
|
unsigned image_height = height;
|
|
|
|
ImageData8 reduced_image(image_width,image_height);
|
|
|
|
reduce_1(image,reduced_image,tree);
|
|
|
|
save_as_png(file,palette,reduced_image,width,height,1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// <=16 colors -> write 4-bit color depth PNG
|
|
|
|
unsigned image_width = (int(0.5*width) + 3)&~3;
|
|
|
|
unsigned image_height = height;
|
|
|
|
ImageData8 reduced_image(image_width,image_height);
|
|
|
|
reduce_4(image,reduced_image,tree);
|
|
|
|
save_as_png(file,palette,reduced_image,width,height,4);
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 11:59:28 +01:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
#define BUFFER_SIZE 4096
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
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;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct jpeg_compress_struct cinfo;
|
|
|
|
struct jpeg_error_mgr jerr;
|
|
|
|
|
|
|
|
int width=image.width();
|
|
|
|
int height=image.height();
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
|
|
|
|
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;
|
|
|
|
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=reinterpret_cast<JSAMPLE*>( ::operator new (sizeof(JSAMPLE) * width*3));
|
|
|
|
while (cinfo.next_scanline < cinfo.image_height)
|
|
|
|
{
|
|
|
|
const unsigned* imageRow=image.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);
|
|
|
|
}
|
|
|
|
::operator delete(row);
|
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
}
|
2007-12-06 11:59:28 +01:00
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
template void save_to_file<ImageData32>(std::string const&,
|
|
|
|
std::string const&,
|
|
|
|
ImageData32 const&);
|
1. hit_test implementation for geometry objects:
bool hit_test(double x, double y, double tol);
2. added image_view(unsigned x, unsigned y, unsigned width, unsigned height)
allowing to select region from image data e.g (in Python):
im = Image(2048,2048)
view = im.view(0,0,256,256)
save_to_file(filename,type, view)
3. changed envelope method to return vy value in datasource classes
4. features_at_point impl for shape and postgis plug-ins
2006-11-25 12:02:59 +01:00
|
|
|
|
2007-12-06 13:14:29 +01:00
|
|
|
template void save_to_file<ImageData32>(std::string const&,
|
|
|
|
ImageData32 const&);
|
|
|
|
|
2007-10-17 16:47:56 +02:00
|
|
|
template void save_to_file<image_view<ImageData32> > (std::string const&,
|
|
|
|
std::string const&,
|
|
|
|
image_view<ImageData32> const&);
|
2007-12-10 20:59:17 +01:00
|
|
|
|
2007-12-06 13:14:29 +01:00
|
|
|
template void save_to_file<image_view<ImageData32> > (std::string const&,
|
|
|
|
image_view<ImageData32> const&);
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|