+ remove unused code

+ add 'add_border' helper method for debugging
  raster alignment issues
This commit is contained in:
Artem Pavlenko 2010-02-03 12:19:30 +00:00
parent ede29a1aff
commit f3eed56396

View file

@ -40,11 +40,11 @@
namespace mapnik { namespace mapnik {
class ImageWriterException : public std::exception class ImageWriterException : public std::exception
{ {
private: private:
std::string message_; std::string message_;
public: public:
ImageWriterException(const std::string& message) ImageWriterException(const std::string& message)
: message_(message) {} : message_(message) {}
@ -54,69 +54,69 @@ namespace mapnik {
{ {
return message_.c_str(); return message_.c_str();
} }
}; };
MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map, MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename, std::string const& filename,
std::string const& type); std::string const& type);
template <typename T> template <typename T>
MAPNIK_DECL void save_to_file(T const& image, MAPNIK_DECL void save_to_file(T const& image,
std::string const& filename, std::string const& filename,
std::string const& type); std::string const& type);
// guess type from file extension // guess type from file extension
template <typename T> template <typename T>
MAPNIK_DECL void save_to_file(T const& image, MAPNIK_DECL void save_to_file(T const& image,
std::string const& filename); std::string const& filename);
template <typename T> template <typename T>
MAPNIK_DECL std::string save_to_string(T const& image, MAPNIK_DECL std::string save_to_string(T const& image,
std::string const& type); std::string const& type);
template <typename T> template <typename T>
void save_as_png(T const& image, void save_as_png(T const& image,
std::string const& filename); std::string const& filename);
template <typename T> template <typename T>
void save_as_jpeg(std::string const& filename, void save_as_jpeg(std::string const& filename,
int quality, int quality,
T const& image); T const& image);
inline bool is_png (std::string const& filename) inline bool is_png (std::string const& filename)
{ {
return boost::algorithm::iends_with(filename,std::string(".png")); return boost::algorithm::iends_with(filename,std::string(".png"));
} }
inline bool is_jpeg (std::string const& filename) inline bool is_jpeg (std::string const& filename)
{ {
return boost::algorithm::iends_with(filename,std::string(".jpg")) || return boost::algorithm::iends_with(filename,std::string(".jpg")) ||
boost::algorithm::iends_with(filename,std::string(".jpeg")); boost::algorithm::iends_with(filename,std::string(".jpeg"));
} }
inline bool is_tiff (std::string const& filename) inline bool is_tiff (std::string const& filename)
{ {
return boost::algorithm::iends_with(filename,std::string(".tif")) || return boost::algorithm::iends_with(filename,std::string(".tif")) ||
boost::algorithm::iends_with(filename,std::string(".tiff")); boost::algorithm::iends_with(filename,std::string(".tiff"));
} }
inline bool is_pdf (std::string const& filename) inline bool is_pdf (std::string const& filename)
{ {
return boost::algorithm::iends_with(filename,std::string(".pdf")); return boost::algorithm::iends_with(filename,std::string(".pdf"));
} }
inline bool is_svg (std::string const& filename) inline bool is_svg (std::string const& filename)
{ {
return boost::algorithm::iends_with(filename,std::string(".svg")); return boost::algorithm::iends_with(filename,std::string(".svg"));
} }
inline bool is_ps (std::string const& filename) inline bool is_ps (std::string const& filename)
{ {
return boost::algorithm::iends_with(filename,std::string(".ps")); return boost::algorithm::iends_with(filename,std::string(".ps"));
} }
inline boost::optional<std::string> type_from_filename(std::string const& filename) inline boost::optional<std::string> type_from_filename(std::string const& filename)
{ {
typedef boost::optional<std::string> result_type; typedef boost::optional<std::string> result_type;
if (is_png(filename)) return result_type("png"); if (is_png(filename)) return result_type("png");
if (is_jpeg(filename)) return result_type("jpeg"); if (is_jpeg(filename)) return result_type("jpeg");
@ -125,140 +125,45 @@ namespace mapnik {
if (is_svg(filename)) return result_type("svg"); if (is_svg(filename)) return result_type("svg");
if (is_ps(filename)) return result_type("ps"); if (is_ps(filename)) return result_type("ps");
return result_type(); return result_type();
} }
inline std::string guess_type( const std::string & filename ) inline std::string guess_type( const std::string & filename )
{ {
std::string::size_type idx = filename.find_last_of("."); std::string::size_type idx = filename.find_last_of(".");
if ( idx != std::string::npos ) { if ( idx != std::string::npos ) {
return filename.substr( idx + 1 ); return filename.substr( idx + 1 );
} }
return "<unknown>"; return "<unknown>";
} }
template <typename T> template <typename T>
double distance(T x0,T y0,T x1,T y1) double distance(T x0,T y0,T x1,T y1)
{ {
double dx = x1-x0; double dx = x1-x0;
double dy = y1-y0; double dy = y1-y0;
return std::sqrt(dx * dx + dy * dy); return std::sqrt(dx * dx + dy * dy);
} }
template <typename Image>
inline void scale_down2(Image& target,const Image& source)
{
int source_width=source.width();
int source_height=source.height();
int target_width=target.width(); // add 1-px border around image - useful for debugging alignment issues
int target_height=target.height(); template <typename T>
if (target_width<source_width/2 || target_height<source_height/2) void add_border(T & image)
return; {
int y1,x1; for (unsigned x = 0; x < image.width();++x)
for (int y=0;y<target_height;++y)
{ {
y1=2*y; image(x,0) = 0xff0000ff; // red
for(int x=0;x<target_width;++x) image(x,image.height()-1) = 0xff00ff00; //green
}
for (unsigned y = 0; y < image.height();++y)
{ {
x1=2*x; image(0,y) = 0xff00ffff; //yellow
//todo calculate average??? image(image.width()-1,y) = 0xffff0000; // blue
target(x,y)=source(x1,y1);
}
}
} }
}
template <typename Image,int scale> template <typename Image>
struct image_op inline void scale_image (Image& target,const Image& source)
{ {
static void scale_up(Image& target,const Image& source)
{
if (scale<3) return;
int source_width=source.width();
int source_height=source.height();
int target_width=target.width();
int target_height=target.height();
if (target_width<scale*source_width || target_height<scale*source_height)
return;
for (int y=0;y<source_height;++y)
{
for(int x=0;x<source_width;++x)
{
unsigned p=source(x,y);
for (int i=0;i<scale;++i)
for (int j=0;j<scale;++j)
target(scale*x+i,scale*y+j)=p;
}
}
}
};
template <typename Image>
struct image_op<Image,2>
{
static void scale_up(Image& target,const Image& source)
{
int source_width=source.width();
int source_height=source.height();
int target_width=target.width();
int target_height=target.height();
if (target_width<2*source_width || target_height<2*source_height)
return;
for (int y=0;y<source_height;++y)
{
for(int x=0;x<source_width;++x)
{
target(2*x,2*y)=source(x,y);
target(2*x+1,2*y)=source(x,y);
target(2*x+1,2*y+1)=source(x,y);
target(2*x,2*y+1)=source(x,y);
}
}
}
};
namespace
{
template <typename Image>
inline void scale_up(Image& target,const Image& source,unsigned scale)
{
int source_width=source.width();
int source_height=source.height();
int target_width=target.width();
int target_height=target.height();
if (target_width<scale*source_width || target_height<scale*source_height)
return;
for (int y=0;y<source_height;++y)
{
for(int x=0;x<source_width;++x)
{
unsigned p=source(x,y);
for (int i=0;i<scale;++i)
for (int j=0;j<scale;++j)
target(scale*x+i,scale*y+j)=p;
}
}
}
}
template <typename Image>
void scale_image(Image& target,const Image& source,unsigned scale)
{
if (scale==2)
{
image_op<Image,2>::scale_up(target,source);
}
else
{
scale_up<Image>(target,source,scale);
}
}
template <typename Image>
inline void scale_image (Image& target,const Image& source)
{
int source_width=source.width(); int source_width=source.width();
int source_height=source.height(); int source_height=source.height();
@ -306,11 +211,15 @@ namespace mapnik {
++ys; ++ys;
} }
} }
}
template <typename Image> #ifdef MAPNIK_DEBUG
inline void scale_image_bilinear (Image& target,const Image& source) add_border(target);
{ #endif
}
template <typename Image>
inline void scale_image_bilinear (Image& target,const Image& source)
{
int source_width=source.width(); int source_width=source.width();
int source_height=source.height(); int source_height=source.height();
@ -378,11 +287,11 @@ namespace mapnik {
target(x,y)=out; target(x,y)=out;
} }
} }
} }
template <typename Image> template <typename Image>
inline void scale_image_bilinear8 (Image& target,const Image& source) inline void scale_image_bilinear8 (Image& target,const Image& source)
{ {
int source_width=source.width(); int source_width=source.width();
int source_height=source.height(); int source_height=source.height();
@ -438,44 +347,44 @@ namespace mapnik {
target(x,y)=(0xff<<24) | (r<<16) | (r<<8) | r; target(x,y)=(0xff<<24) | (r<<16) | (r<<8) | r;
} }
} }
} }
inline MAPNIK_DECL void save_to_file (image_32 const& image, inline MAPNIK_DECL void save_to_file (image_32 const& image,
std::string const& file, std::string const& file,
std::string const& type) std::string const& type)
{ {
save_to_file<image_data_32>(image.data(),file,type); save_to_file<image_data_32>(image.data(),file,type);
} }
inline MAPNIK_DECL void save_to_file(image_32 const& image, inline MAPNIK_DECL void save_to_file(image_32 const& image,
std::string const& file) std::string const& file)
{ {
save_to_file<image_data_32>(image.data(),file); save_to_file<image_data_32>(image.data(),file);
} }
inline MAPNIK_DECL std::string save_to_string(image_32 const& image, inline MAPNIK_DECL std::string save_to_string(image_32 const& image,
std::string const& type) std::string const& type)
{ {
return save_to_string<image_data_32>(image.data(),type); return save_to_string<image_data_32>(image.data(),type);
} }
#ifdef _MSC_VER #ifdef _MSC_VER
template MAPNIK_DECL void save_to_file<image_data_32>(image_data_32 const&, template MAPNIK_DECL void save_to_file<image_data_32>(image_data_32 const&,
std::string const&, std::string const&,
std::string const&); std::string const&);
template MAPNIK_DECL void save_to_file<image_data_32>(image_data_32 const&, template MAPNIK_DECL void save_to_file<image_data_32>(image_data_32 const&,
std::string const&); std::string const&);
template MAPNIK_DECL std::string save_to_string<image_data_32>(image_data_32 const&, template MAPNIK_DECL std::string save_to_string<image_data_32>(image_data_32 const&,
std::string const&); std::string const&);
template MAPNIK_DECL void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&, template MAPNIK_DECL void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&, std::string const&,
std::string const&); std::string const&);
template MAPNIK_DECL void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&, template MAPNIK_DECL void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&); std::string const&);
template MAPNIK_DECL std::string save_to_string<image_view<image_data_32> > (image_view<image_data_32> const&, template MAPNIK_DECL std::string save_to_string<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&); std::string const&);
#endif #endif