1. validate image reader

2. open files in binary mode (win32)
This commit is contained in:
Artem Pavlenko 2006-04-05 08:27:45 +00:00
parent 61fd258fd1
commit cdc48edfd6
9 changed files with 296 additions and 278 deletions

View file

@ -100,11 +100,11 @@ namespace mapnik {
}
inline std::string to_string() const
{
{
std::stringstream ss;
ss << "rgb (" << red() << "," << green() << "," << blue() <<")";
return ss.str();
}
}
};
}

View file

@ -53,11 +53,9 @@ namespace mapnik
double maxZoom_;
bool active_;
bool selectable_;
std::vector<std::string> styles_;
std::string selection_style_;
mutable datasource_p ds_;
std::vector<std::string> styles_;
std::string selection_style_;
mutable std::vector<boost::shared_ptr<Feature> > selection_;
public:

View file

@ -78,8 +78,8 @@
namespace mapnik
{
void save_to_xml(Map const& map,const char* filename);
void load_from_xml(Map & map, const char * filename);
void MAPNIK_DECL save_to_xml(Map const& map,const char* filename);
void MAPNIK_DECL load_from_xml(Map & map, const char * filename);
}
#endif //MAPNIK_HPP

View file

@ -85,11 +85,11 @@ namespace mapnik
maxZoom_=rhs.maxZoom_;
active_=rhs.active_;
selectable_=rhs.selectable_;
//ds_=rhs.ds_;
styles_=rhs.styles_;
ds_=rhs.ds_;
selection_style_=rhs.selection_style_;
}
Layer::~Layer() {}
parameters const& Layer::params() const

View file

@ -37,7 +37,8 @@ namespace mapnik
try
{
std::auto_ptr<ImageReader> reader(get_image_reader(type,file));
reader->read(0,0,*pattern_);
if (reader.get())
reader->read(0,0,*pattern_);
}
catch (...)
{

View file

@ -31,170 +31,185 @@ namespace mapnik
class PngReader : public ImageReader
{
private:
std::string fileName_;
unsigned width_;
unsigned height_;
int bit_depth_;
int color_type_;
std::string fileName_;
unsigned width_;
unsigned height_;
int bit_depth_;
int color_type_;
public:
explicit PngReader(const std::string& fileName);
~PngReader();
unsigned width() const;
unsigned height() const;
void read(unsigned x,unsigned y,ImageData32& image);
explicit PngReader(const std::string& fileName);
~PngReader();
unsigned width() const;
unsigned height() const;
void read(unsigned x,unsigned y,ImageData32& image);
private:
PngReader(const PngReader&);
PngReader& operator=(const PngReader&);
void init();
PngReader(const PngReader&);
PngReader& operator=(const PngReader&);
void init();
};
namespace
{
ImageReader* createPngReader(const std::string& file)
{
return new PngReader(file);
}
const bool registered = register_image_reader("png",createPngReader);
ImageReader* createPngReader(const std::string& file)
{
return new PngReader(file);
}
const bool registered = register_image_reader("png",createPngReader);
}
PngReader::PngReader(const std::string& fileName)
: fileName_(fileName),
: fileName_(fileName),
width_(0),
height_(0),
bit_depth_(0),
color_type_(0)
{
try
{
init();
}
catch (const ImageReaderException& e)
{
std::clog<<e.what()<<std::endl;
throw;
}
try
{
init();
}
catch (const ImageReaderException& e)
{
std::clog<<e.what()<<std::endl;
throw;
}
}
PngReader::~PngReader() {}
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,
(FILE *)png_ptr->io_ptr);
if (check != length)
{
png_error(png_ptr, "Read Error");
}
}
void PngReader::init()
{
FILE *fp=fopen(fileName_.c_str(),"r");
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");
}
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");
}
png_structp png_ptr = png_create_read_struct
(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_LIBPNG_VER_STRING,0,0,0);
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr, info_ptr);
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_uint_32 width, height;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth_, &color_type_,0,0,0);
width_=width;
height_=height;
std::clog<<"bit_depth="<<bit_depth_<<" color_type="<<color_type_<<std::endl;
png_destroy_read_struct(&png_ptr,&info_ptr,0);
fclose(fp);
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;
std::clog<<"bit_depth="<<bit_depth_<<" color_type="<<color_type_<<std::endl;
png_destroy_read_struct(&png_ptr,&info_ptr,0);
fclose(fp);
}
unsigned PngReader::width() const
{
return width_;
return width_;
}
unsigned PngReader::height() const
{
return height_;
return height_;
}
void PngReader::read(unsigned x0, unsigned y0,ImageData32& image)
{
FILE *fp=fopen(fileName_.c_str(),"r");
if (!fp) throw ImageReaderException("cannot open image file "+fileName_);
FILE *fp=fopen(fileName_.c_str(),"rb");
if (!fp) throw ImageReaderException("cannot open image file "+fileName_);
png_structp png_ptr = png_create_read_struct
(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_LIBPNG_VER_STRING,0,0,0);
png_init_io(png_ptr, fp);
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
png_set_add_alpha(png_ptr,1,1);
double gamma;
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, 2.2, gamma);
if (!png_ptr)
{
fclose(fp);
throw ImageReaderException("failed to allocate png_ptr");
}
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);
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
png_set_add_alpha(png_ptr,1,1);
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);
}
}

View file

@ -36,7 +36,10 @@ namespace mapnik
try
{
std::auto_ptr<ImageReader> reader(get_image_reader(type,file));
reader->read(0,0,*symbol_);
if (reader.get())
{
reader->read(0,0,*symbol_);
}
}
catch (...)
{

View file

@ -36,7 +36,8 @@ namespace mapnik
try
{
std::auto_ptr<ImageReader> reader(get_image_reader(type,file));
reader->read(0,0,*pattern_);
if (reader.get())
reader->read(0,0,*pattern_);
}
catch (...)
{

View file

@ -29,9 +29,9 @@
namespace mapnik
{
using std::min;
using std::max;
using std::min;
using std::max;
class TiffReader : public ImageReader
{
private:
@ -44,11 +44,11 @@ namespace mapnik
int tile_height_;
public:
enum
{
generic=1,
stripped,
tiled
};
{
generic=1,
stripped,
tiled
};
explicit TiffReader(const std::string& file_name);
virtual ~TiffReader();
unsigned width() const;
@ -65,16 +65,16 @@ namespace mapnik
namespace
{
ImageReader* createTiffReader(const std::string& file)
{
return new TiffReader(file);
}
ImageReader* createTiffReader(const std::string& file)
{
return new TiffReader(file);
}
const bool registered = register_image_reader("tiff",createTiffReader);
const bool registered = register_image_reader("tiff",createTiffReader);
}
TiffReader::TiffReader(const std::string& file_name)
: file_name_(file_name),
: file_name_(file_name),
read_method_(generic),
width_(0),
height_(0),
@ -82,177 +82,177 @@ namespace mapnik
tile_width_(0),
tile_height_(0)
{
try
{
init();
}
catch (ImageReaderException& ex)
{
std::clog<<ex.what()<<std::endl;
throw;
}
try
{
init();
}
catch (ImageReaderException& ex)
{
std::clog<<ex.what()<<std::endl;
throw;
}
}
void TiffReader::init()
{
TIFF* tif = TIFFOpen(file_name_.c_str(), "r");
if (!tif) throw ImageReaderException("cannot open "+file_name_);
char msg[1024];
TIFF* tif = TIFFOpen(file_name_.c_str(), "rb");
if (!tif) throw ImageReaderException("cannot open "+file_name_);
char msg[1024];
if (TIFFRGBAImageOK(tif,msg))
{
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width_);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height_);
if (TIFFIsTiled(tif))
{
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width_);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height_);
read_method_=tiled;
}
else if (TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rows_per_strip_)!=0)
{
read_method_=stripped;
}
TIFFClose(tif);
}
else
{
TIFFClose(tif);
throw ImageReaderException(msg);
}
if (TIFFRGBAImageOK(tif,msg))
{
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width_);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height_);
if (TIFFIsTiled(tif))
{
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width_);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height_);
read_method_=tiled;
}
else if (TIFFGetField(tif,TIFFTAG_ROWSPERSTRIP,&rows_per_strip_)!=0)
{
read_method_=stripped;
}
TIFFClose(tif);
}
else
{
TIFFClose(tif);
throw ImageReaderException(msg);
}
}
TiffReader::~TiffReader()
{
//
//
}
unsigned TiffReader::width() const
{
return width_;
return width_;
}
unsigned TiffReader::height() const
{
return height_;
return height_;
}
void TiffReader::read(unsigned x,unsigned y,ImageData32& image)
{
if (read_method_==stripped)
{
read_stripped(x,y,image);
}
else if (read_method_==tiled)
{
read_tiled(x,y,image);
}
else
{
read_generic(x,y,image);
}
if (read_method_==stripped)
{
read_stripped(x,y,image);
}
else if (read_method_==tiled)
{
read_tiled(x,y,image);
}
else
{
read_generic(x,y,image);
}
}
void TiffReader::read_generic(unsigned x,unsigned y,ImageData32& image)
{
TIFF* tif = TIFFOpen(file_name_.c_str(), "r");
if (tif)
{
std::clog<<"TODO:tiff is not stripped or tiled\n";
TIFFClose(tif);
}
TIFF* tif = TIFFOpen(file_name_.c_str(), "rb");
if (tif)
{
std::clog<<"TODO:tiff is not stripped or tiled\n";
TIFFClose(tif);
}
}
void TiffReader::read_tiled(unsigned x0,unsigned y0,ImageData32& image)
{
TIFF* tif=TIFFOpen(file_name_.c_str(), "r");
if (tif)
{
uint32* buf = (uint32*)_TIFFmalloc(tile_width_*tile_height_*sizeof(uint32));
int width=image.width();
int height=image.height();
TIFF* tif=TIFFOpen(file_name_.c_str(), "rb");
if (tif)
{
uint32* buf = (uint32*)_TIFFmalloc(tile_width_*tile_height_*sizeof(uint32));
int width=image.width();
int height=image.height();
int start_y=(y0/tile_height_)*tile_height_;
int end_y=((y0+height)/tile_height_+1)*tile_height_;
int start_y=(y0/tile_height_)*tile_height_;
int end_y=((y0+height)/tile_height_+1)*tile_height_;
int start_x=(x0/tile_width_)*tile_width_;
int end_x=((x0+width)/tile_width_+1)*tile_width_;
int row,tx0,tx1,ty0,ty1;
int start_x=(x0/tile_width_)*tile_width_;
int end_x=((x0+width)/tile_width_+1)*tile_width_;
int row,tx0,tx1,ty0,ty1;
for (int y=start_y;y<end_y;y+=tile_height_)
{
ty0 = max(y0,(unsigned)y) - y;
ty1 = min(height+y0,(unsigned)(y+tile_height_)) - y;
for (int y=start_y;y<end_y;y+=tile_height_)
{
ty0 = max(y0,(unsigned)y) - y;
ty1 = min(height+y0,(unsigned)(y+tile_height_)) - y;
int n0=tile_height_-ty1;
int n1=tile_height_-ty0-1;
for (int x=start_x;x<end_x;x+=tile_width_)
{
int n0=tile_height_-ty1;
int n1=tile_height_-ty0-1;
if (!TIFFReadRGBATile(tif,x,y,buf)) break;
for (int x=start_x;x<end_x;x+=tile_width_)
{
tx0=max(x0,(unsigned)x);
tx1=min(width+x0,(unsigned)(x+tile_width_));
row=y+ty0-y0;
for (int n=n1;n>=n0;--n)
{
image.setRow(row,tx0-x0,tx1-x0,(const unsigned*)&buf[n*tile_width_+tx0-x]);
++row;
}
}
}
_TIFFfree(buf);
TIFFClose(tif);
}
if (!TIFFReadRGBATile(tif,x,y,buf)) break;
tx0=max(x0,(unsigned)x);
tx1=min(width+x0,(unsigned)(x+tile_width_));
row=y+ty0-y0;
for (int n=n1;n>=n0;--n)
{
image.setRow(row,tx0-x0,tx1-x0,(const unsigned*)&buf[n*tile_width_+tx0-x]);
++row;
}
}
}
_TIFFfree(buf);
TIFFClose(tif);
}
}
void TiffReader::read_stripped(unsigned x0,unsigned y0,ImageData32& image)
{
TIFF* tif = TIFFOpen(file_name_.c_str(), "r");
if (tif)
{
uint32* buf = (uint32*)_TIFFmalloc(width_*rows_per_strip_*sizeof(uint32));
TIFF* tif = TIFFOpen(file_name_.c_str(), "rb");
if (tif)
{
uint32* buf = (uint32*)_TIFFmalloc(width_*rows_per_strip_*sizeof(uint32));
int width=image.width();
int height=image.height();
unsigned start_y=(y0/rows_per_strip_)*rows_per_strip_;
unsigned end_y=((y0+height)/rows_per_strip_+1)*rows_per_strip_;
bool laststrip=((unsigned)end_y > height_)?true:false;
int row,tx0,tx1,ty0,ty1;
int width=image.width();
int height=image.height();
tx0=x0;
tx1=min(width+x0,(unsigned)width_);
unsigned start_y=(y0/rows_per_strip_)*rows_per_strip_;
unsigned end_y=((y0+height)/rows_per_strip_+1)*rows_per_strip_;
bool laststrip=((unsigned)end_y > height_)?true:false;
int row,tx0,tx1,ty0,ty1;
for (unsigned y=start_y; y < end_y; y+=rows_per_strip_)
{
ty0 = max(y0,y)-y;
ty1 = min(height+y0,y+rows_per_strip_)-y;
tx0=x0;
tx1=min(width+x0,(unsigned)width_);
if (!TIFFReadRGBAStrip(tif,y,buf)) break;
row=y+ty0-y0;
int n0=laststrip ? 0:(rows_per_strip_-ty1);
int n1=laststrip ? (ty1-ty0-1):(rows_per_strip_-ty0-1);
for (int n=n1;n>=n0;--n)
{
image.setRow(row,tx0-x0,tx1-x0,(const unsigned*)&buf[n*width_+tx0]);
++row;
}
}
_TIFFfree(buf);
TIFFClose(tif);
}
for (unsigned y=start_y; y < end_y; y+=rows_per_strip_)
{
ty0 = max(y0,y)-y;
ty1 = min(height+y0,y+rows_per_strip_)-y;
if (!TIFFReadRGBAStrip(tif,y,buf)) break;
row=y+ty0-y0;
int n0=laststrip ? 0:(rows_per_strip_-ty1);
int n1=laststrip ? (ty1-ty0-1):(rows_per_strip_-ty0-1);
for (int n=n1;n>=n0;--n)
{
image.setRow(row,tx0-x0,tx1-x0,(const unsigned*)&buf[n*width_+tx0]);
++row;
}
}
_TIFFfree(buf);
TIFFClose(tif);
}
}
}