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: tiff_reader.cpp 17 2005-03-08 23:58:43Z pavlenko $
|
2006-10-04 13:22:18 +02:00
|
|
|
// stl
|
2005-06-14 17:06:59 +02:00
|
|
|
#include <iostream>
|
2006-12-30 22:48:15 +01:00
|
|
|
#include "boost/filesystem/operations.hpp"
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/image_reader.hpp>
|
|
|
|
|
2006-08-31 23:32:07 +02:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <tiffio.h>
|
|
|
|
}
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
2006-04-05 10:27:45 +02:00
|
|
|
using std::min;
|
|
|
|
using std::max;
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
class TiffReader : public ImageReader
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string file_name_;
|
|
|
|
int read_method_;
|
|
|
|
unsigned width_;
|
|
|
|
unsigned height_;
|
|
|
|
int rows_per_strip_;
|
|
|
|
int tile_width_;
|
|
|
|
int tile_height_;
|
|
|
|
public:
|
2007-03-16 11:11:37 +01:00
|
|
|
enum TiffType {
|
2006-08-31 23:32:07 +02:00
|
|
|
generic=1,
|
|
|
|
stripped,
|
|
|
|
tiled
|
|
|
|
};
|
2005-06-14 17:06:59 +02:00
|
|
|
explicit TiffReader(const std::string& file_name);
|
|
|
|
virtual ~TiffReader();
|
|
|
|
unsigned width() const;
|
|
|
|
unsigned height() const;
|
|
|
|
void read(unsigned x,unsigned y,ImageData32& image);
|
|
|
|
private:
|
|
|
|
TiffReader(const TiffReader&);
|
|
|
|
TiffReader& operator=(const TiffReader&);
|
|
|
|
void init();
|
|
|
|
void read_generic(unsigned x,unsigned y,ImageData32& image);
|
|
|
|
void read_stripped(unsigned x,unsigned y,ImageData32& image);
|
|
|
|
void read_tiled(unsigned x,unsigned y,ImageData32& image);
|
2006-12-20 01:22:45 +01:00
|
|
|
TIFF* load_if_exists(const std::string& filename);
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
ImageReader* createTiffReader(const std::string& file)
|
|
|
|
{
|
|
|
|
return new TiffReader(file);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-04-05 10:27:45 +02:00
|
|
|
const bool registered = register_image_reader("tiff",createTiffReader);
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TiffReader::TiffReader(const std::string& file_name)
|
2006-04-05 10:27:45 +02:00
|
|
|
: file_name_(file_name),
|
2006-08-31 23:32:07 +02:00
|
|
|
read_method_(generic),
|
|
|
|
width_(0),
|
|
|
|
height_(0),
|
|
|
|
rows_per_strip_(0),
|
|
|
|
tile_width_(0),
|
|
|
|
tile_height_(0)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
catch (ImageReaderException& ex)
|
|
|
|
{
|
|
|
|
std::clog<<ex.what()<<std::endl;
|
|
|
|
throw;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TiffReader::init()
|
|
|
|
{
|
2006-12-20 01:22:45 +01:00
|
|
|
TIFF* tif = load_if_exists(file_name_);
|
|
|
|
if (!tif) return;
|
|
|
|
|
2006-04-05 10:27:45 +02:00
|
|
|
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);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TiffReader::~TiffReader()
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
//
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned TiffReader::width() const
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
return width_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned TiffReader::height() const
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
return height_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TiffReader::read(unsigned x,unsigned y,ImageData32& image)
|
|
|
|
{
|
2006-04-05 10:27:45 +02:00
|
|
|
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);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TiffReader::read_generic(unsigned x,unsigned y,ImageData32& image)
|
|
|
|
{
|
2006-12-20 01:22:45 +01:00
|
|
|
TIFF* tif = load_if_exists(file_name_);
|
2006-04-05 10:27:45 +02:00
|
|
|
if (tif)
|
|
|
|
{
|
|
|
|
std::clog<<"TODO:tiff is not stripped or tiled\n";
|
|
|
|
TIFFClose(tif);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TiffReader::read_tiled(unsigned x0,unsigned y0,ImageData32& image)
|
|
|
|
{
|
2006-12-20 01:22:45 +01:00
|
|
|
TIFF* tif = load_if_exists(file_name_);
|
2006-04-05 10:27:45 +02:00
|
|
|
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_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;
|
|
|
|
|
|
|
|
int n0=tile_height_-ty1;
|
|
|
|
int n1=tile_height_-ty0-1;
|
|
|
|
|
|
|
|
for (int x=start_x;x<end_x;x+=tile_width_)
|
|
|
|
{
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TiffReader::read_stripped(unsigned x0,unsigned y0,ImageData32& image)
|
|
|
|
{
|
2006-12-20 01:22:45 +01:00
|
|
|
TIFF* tif = load_if_exists(file_name_);
|
2006-04-05 10:27:45 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
tx0=x0;
|
|
|
|
tx1=min(width+x0,(unsigned)width_);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2006-12-20 01:22:45 +01:00
|
|
|
|
2006-12-21 23:51:53 +01:00
|
|
|
TIFF* TiffReader::load_if_exists(std::string const& filename)
|
2006-12-20 01:22:45 +01:00
|
|
|
{
|
2006-12-21 23:51:53 +01:00
|
|
|
TIFF * tif = 0;
|
2006-12-20 01:22:45 +01:00
|
|
|
boost::filesystem::path path(file_name_);
|
2006-12-21 23:51:53 +01:00
|
|
|
if (exists(path)) // && is_regular(path)) { -- not supported in boost-1.33.*
|
|
|
|
{
|
2006-12-20 01:22:45 +01:00
|
|
|
// File path is a full file path and does exist
|
|
|
|
tif = TIFFOpen(filename.c_str(), "rb");
|
|
|
|
}
|
2006-12-21 23:51:53 +01:00
|
|
|
|
2006-12-20 01:22:45 +01:00
|
|
|
return tif;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|