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: png_reader.cpp 33 2005-04-04 13:01:03Z pavlenko $
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/image_reader.hpp>
|
2006-08-31 23:32:07 +02:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <png.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
|
|
|
#include <iostream>
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
class PngReader : public ImageReader
|
|
|
|
{
|
|
|
|
private:
|
2006-04-05 10:27:45 +02:00
|
|
|
std::string fileName_;
|
|
|
|
unsigned width_;
|
|
|
|
unsigned height_;
|
|
|
|
int bit_depth_;
|
|
|
|
int color_type_;
|
2005-06-14 17:06:59 +02:00
|
|
|
public:
|
2006-04-05 10:27:45 +02:00
|
|
|
explicit PngReader(const std::string& fileName);
|
|
|
|
~PngReader();
|
|
|
|
unsigned width() const;
|
|
|
|
unsigned height() const;
|
|
|
|
void read(unsigned x,unsigned y,ImageData32& image);
|
2005-06-14 17:06:59 +02:00
|
|
|
private:
|
2006-04-05 10:27:45 +02:00
|
|
|
PngReader(const PngReader&);
|
|
|
|
PngReader& operator=(const PngReader&);
|
|
|
|
void init();
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
ImageReader* createPngReader(const std::string& file)
|
|
|
|
{
|
|
|
|
return new PngReader(file);
|
|
|
|
}
|
|
|
|
const bool registered = register_image_reader("png",createPngReader);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PngReader::PngReader(const std::string& fileName)
|
2006-04-05 10:27:45 +02:00
|
|
|
: fileName_(fileName),
|
2006-10-04 13:22:18 +02:00
|
|
|
width_(0),
|
|
|
|
height_(0),
|
|
|
|
bit_depth_(0),
|
|
|
|
color_type_(0)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2007-09-25 20:47:12 +02:00
|
|
|
init();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PngReader::~PngReader() {}
|
2006-04-05 10:27:45 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
|
|
|
{
|
|
|
|
png_size_t check;
|
|
|
|
check = (png_size_t)fread(data, (png_size_t)1, length,
|
2006-10-04 13:22:18 +02:00
|
|
|
(FILE *)png_ptr->io_ptr);
|
2006-04-05 10:27:45 +02:00
|
|
|
|
|
|
|
if (check != length)
|
|
|
|
{
|
|
|
|
png_error(png_ptr, "Read Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
void PngReader::init()
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
FILE *fp=fopen(fileName_.c_str(),"rb");
|
|
|
|
if (!fp) throw ImageReaderException("cannot open image file "+fileName_);
|
|
|
|
png_byte header[8];
|
|
|
|
memset(header,0,8);
|
|
|
|
fread(header,1,8,fp);
|
|
|
|
int is_png=!png_sig_cmp(header,0,8);
|
|
|
|
if (!is_png)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
throw ImageReaderException(fileName_ + " is not a png file");
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
png_structp png_ptr = png_create_read_struct
|
2006-04-05 10:27:45 +02:00
|
|
|
(PNG_LIBPNG_VER_STRING,0,0,0);
|
|
|
|
|
|
|
|
if (!png_ptr)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
throw ImageReaderException("failed to allocate png_ptr");
|
|
|
|
}
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (!info_ptr)
|
|
|
|
{
|
|
|
|
png_destroy_read_struct(&png_ptr,0,0);
|
|
|
|
fclose(fp);
|
|
|
|
throw ImageReaderException("failed to create info_ptr");
|
|
|
|
}
|
|
|
|
|
|
|
|
png_set_read_fn(png_ptr, (png_voidp)fp, png_read_data);
|
|
|
|
|
|
|
|
png_set_sig_bytes(png_ptr,8);
|
|
|
|
png_read_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
png_uint_32 width, height;
|
|
|
|
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0);
|
|
|
|
|
|
|
|
width_=width;
|
|
|
|
height_=height;
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2006-04-05 10:27:45 +02:00
|
|
|
std::clog<<"bit_depth="<<bit_depth_<<" color_type="<<color_type_<<std::endl;
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
2006-04-05 10:27:45 +02:00
|
|
|
png_destroy_read_struct(&png_ptr,&info_ptr,0);
|
|
|
|
fclose(fp);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
unsigned PngReader::width() const
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
return width_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-04-05 10:27:45 +02:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
unsigned PngReader::height() const
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
return height_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PngReader::read(unsigned x0, unsigned y0,ImageData32& image)
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
FILE *fp=fopen(fileName_.c_str(),"rb");
|
|
|
|
if (!fp) throw ImageReaderException("cannot open image file "+fileName_);
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
png_structp png_ptr = png_create_read_struct
|
2006-04-05 10:27:45 +02:00
|
|
|
(PNG_LIBPNG_VER_STRING,0,0,0);
|
|
|
|
|
|
|
|
if (!png_ptr)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
throw ImageReaderException("failed to allocate png_ptr");
|
|
|
|
}
|
|
|
|
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (!info_ptr)
|
|
|
|
{
|
|
|
|
png_destroy_read_struct(&png_ptr,0,0);
|
|
|
|
fclose(fp);
|
|
|
|
throw ImageReaderException("failed to create info_ptr");
|
|
|
|
}
|
|
|
|
|
|
|
|
png_set_read_fn(png_ptr, (png_voidp)fp, png_read_data);
|
|
|
|
png_read_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
if (color_type_ == PNG_COLOR_TYPE_PALETTE)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
if (bit_depth_ == 16)
|
|
|
|
png_set_strip_16(png_ptr);
|
|
|
|
if (color_type_ == PNG_COLOR_TYPE_GRAY ||
|
|
|
|
color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
|
|
|
|
// quick hack -- only work in >=libpng 1.2.7
|
2006-12-01 16:23:19 +01:00
|
|
|
png_set_add_alpha(png_ptr,0xff,PNG_FILLER_AFTER); //rgba
|
|
|
|
|
2006-04-05 10:27:45 +02:00
|
|
|
double gamma;
|
|
|
|
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
|
|
|
|
png_set_gamma(png_ptr, 2.2, gamma);
|
|
|
|
|
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
//START read image rows
|
|
|
|
unsigned w=std::min((unsigned)image.width(),width_);
|
|
|
|
unsigned h=std::min((unsigned)image.height(),height_);
|
|
|
|
|
|
|
|
unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
|
|
|
|
unsigned char* row= new unsigned char[rowbytes];
|
|
|
|
for (unsigned i=0;i<height_;++i)
|
|
|
|
{
|
|
|
|
png_read_row(png_ptr,row,0);
|
|
|
|
if (i>=y0 && i<h)
|
|
|
|
{
|
|
|
|
image.setRow(i-y0,(unsigned*) &row[x0],w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//END
|
|
|
|
delete [] row;
|
|
|
|
png_read_end(png_ptr,0);
|
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr,0);
|
|
|
|
fclose(fp);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
}
|