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
|
|
|
// stl
|
2005-06-14 17:06:59 +02:00
|
|
|
#include <string>
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/graphics.hpp>
|
|
|
|
#include <mapnik/memory.hpp>
|
|
|
|
#include <mapnik/image_util.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>
|
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
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
//use memory manager for mem allocation in libpng
|
|
|
|
png_voidp malloc_fn(png_structp png_ptr,png_size_t size)
|
|
|
|
{
|
|
|
|
return Object::operator new(size);
|
|
|
|
}
|
|
|
|
void free_fn(png_structp png_ptr, png_voidp ptr)
|
|
|
|
{
|
|
|
|
Object::operator delete(ptr);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void save_to_file(std::string const& filename,
|
|
|
|
std::string const& type,
|
|
|
|
T const& image)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-05-19 18:09:32 +02:00
|
|
|
//all that should go into image_writer factory
|
2005-06-14 17:06:59 +02:00
|
|
|
if (type=="png")
|
|
|
|
{
|
|
|
|
save_as_png(filename,image);
|
|
|
|
}
|
2006-05-19 18:09:32 +02:00
|
|
|
else if (type=="jpeg")
|
|
|
|
{
|
|
|
|
save_as_jpeg(filename,85,image);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void save_as_png(std::string const& filename, T const& image)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
FILE *fp=fopen(filename.c_str(), "wb");
|
|
|
|
if (!fp) return;
|
|
|
|
png_voidp mem_ptr=0;
|
|
|
|
png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
2006-10-04 13:22:18 +02:00
|
|
|
(png_voidp)mem_ptr,0, 0);
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
if (!png_ptr) return;
|
|
|
|
png_set_mem_fn(png_ptr,mem_ptr,malloc_fn,free_fn);
|
|
|
|
|
2006-10-26 22:23:12 +02:00
|
|
|
// switch on optimization only if supported
|
|
|
|
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
|
2006-01-26 17:44:46 +01:00
|
|
|
png_uint_32 mask, flags;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-01-26 17:44:46 +01:00
|
|
|
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
|
|
|
|
png_set_filter (png_ptr, 0, PNG_FILTER_NONE);
|
2005-06-14 17:06:59 +02:00
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
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_init_io(png_ptr, fp);
|
2006-10-04 13:22:18 +02:00
|
|
|
//png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
|
|
|
|
//png_set_compression_strategy(png_ptr, Z_FILTERED);
|
2005-06-14 17:06:59 +02:00
|
|
|
png_set_IHDR(png_ptr, info_ptr,image.width(),image.height(),8,
|
2006-10-04 13:22:18 +02:00
|
|
|
PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
|
2005-06-14 17:06:59 +02:00
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
for (unsigned i=0;i<image.height();i++)
|
|
|
|
{
|
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
|
|
|
png_write_row(png_ptr,(png_bytep)image.getRow(i));
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void save_as_jpeg(std::string const& filename,int quality, T const& image)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
FILE *fp=fopen(filename.c_str(), "wb");
|
2005-06-14 17:06:59 +02:00
|
|
|
if (!fp) return;
|
2006-10-04 13:22:18 +02:00
|
|
|
struct jpeg_compress_struct cinfo;
|
|
|
|
struct jpeg_error_mgr jerr;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
int width=image.width();
|
|
|
|
int height=image.height();
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
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];
|
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
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
while (cinfo.next_scanline < cinfo.image_height)
|
|
|
|
{
|
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
|
|
|
const unsigned* imageRow=image.getRow(cinfo.next_scanline);
|
2006-10-04 13:22:18 +02:00
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
template void save_to_file<ImageData32>(std::string const&,
|
|
|
|
std::string const& ,
|
|
|
|
ImageData32 const&);
|
|
|
|
|
|
|
|
template void save_to_file<image_view<ImageData32> > (std::string const&,
|
|
|
|
std::string const& ,
|
|
|
|
image_view<ImageData32> const&);
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|