+ refactor palette stuff a bit

TODO: We need to change save_to_file|string interface
  to avoid passing dummy rgba_palette objects.
This commit is contained in:
Artem Pavlenko 2011-09-07 15:01:25 +00:00
parent 8b3bb3ccd0
commit 02c143895d
9 changed files with 68 additions and 52 deletions

View file

@ -62,8 +62,10 @@ using mapnik::image_32;
using mapnik::image_reader;
using mapnik::get_image_reader;
using mapnik::type_from_filename;
using namespace boost::python;
using mapnik::save_to_file;
using mapnik::save_to_string;
using namespace boost::python;
// output 'raw' pixels
PyObject* tostring1( image_32 const& im)
@ -81,7 +83,8 @@ PyObject* tostring1( image_32 const& im)
// encode (png,jpeg)
PyObject* tostring2(image_32 const & im, std::string const& format)
{
std::string s = save_to_string(im, format);
mapnik::rgba_palette pal;
std::string s = save_to_string(im, format, pal);
return
#if PY_VERSION_HEX >= 0x03000000
::PyBytes_FromStringAndSize
@ -111,7 +114,8 @@ void save_to_file1(mapnik::image_32 const& im, std::string const& filename, std:
void save_to_file2(mapnik::image_32 const& im, std::string const& filename)
{
mapnik::save_to_file(im,filename);
mapnik::rgba_palette pal;
mapnik::save_to_file(im,filename,pal);
}

View file

@ -63,7 +63,8 @@ PyObject* view_tostring1(image_view<image_data_32> const& view)
// encode (png,jpeg)
PyObject* view_tostring2(image_view<image_data_32> const & view, std::string const& format)
{
std::string s = save_to_string(view, format);
mapnik::rgba_palette pal;
std::string s = save_to_string(view, format, pal);
return
#if PY_VERSION_HEX >= 0x03000000
::PyBytes_FromStringAndSize
@ -87,13 +88,14 @@ PyObject* view_tostring3(image_view<image_data_32> const & view, std::string con
void save_view1(image_view<image_data_32> const& view, std::string const& filename, std::string const& type)
{
boost::shared_ptr<mapnik::rgba_palette> palette_ptr;
mapnik::save_to_file(view,filename,type,*palette_ptr);
mapnik::rgba_palette pal;
mapnik::save_to_file(view,filename,type,pal);
}
void save_view2(image_view<image_data_32> const& view, std::string const& filename)
{
mapnik::save_to_file(view,filename);
mapnik::rgba_palette pal;
mapnik::save_to_file(view,filename,pal);
}

View file

@ -236,7 +236,8 @@ void render_tile_to_file(const mapnik::Map& map,
{
mapnik::image_32 image(width,height);
render(map,image,1.0,offset_x, offset_y);
mapnik::save_to_file(image.data(),file,format);
mapnik::rgba_palette pal;
mapnik::save_to_file(image.data(),file,format,pal);
}
void render_to_file1(const mapnik::Map& map,
@ -255,7 +256,8 @@ void render_to_file1(const mapnik::Map& map,
{
mapnik::image_32 image(map.width(),map.height());
render(map,image,1.0,0,0);
mapnik::save_to_file(image,filename,format);
mapnik::rgba_palette pal;
mapnik::save_to_file(image,filename,format,pal);
}
}
@ -274,7 +276,8 @@ void render_to_file2(const mapnik::Map& map,const std::string& filename)
{
mapnik::image_32 image(map.width(),map.height());
render(map,image,1.0,0,0);
mapnik::save_to_file(image,filename);
mapnik::rgba_palette pal;
mapnik::save_to_file(image,filename,pal);
}
}
@ -296,7 +299,8 @@ void render_to_file3(const mapnik::Map& map,
{
mapnik::image_32 image(map.width(),map.height());
render(map,image,scale_factor,0,0);
mapnik::save_to_file(image,filename,format);
mapnik::rgba_palette pal;
mapnik::save_to_file(image,filename,format,pal);
}
}

View file

@ -118,7 +118,7 @@ class hextree : private boost::noncopyable
std::vector<unsigned> pal_remap_;
// rgba hashtable for quantization
typedef boost::unordered_map<rgba, int, rgba::hash_func> rgba_hash_table;
rgba_hash_table color_hashmap_;
mutable rgba_hash_table color_hashmap_;
// gamma correction to prioritize dark colors (>1.0)
double gamma_;
// look up table for gamma correction
@ -218,7 +218,7 @@ public:
}
// return color index in returned earlier palette
int quantize(rgba const& c)
int quantize(rgba const& c) const
{
byte a = preprocessAlpha(c.a);
unsigned ind=0;
@ -234,7 +234,7 @@ public:
int dist, newdist;
// find closest match based on mean of r,g,b,a
std::vector<rgba>::iterator pit =
std::vector<rgba>::const_iterator pit =
std::lower_bound(sorted_pal_.begin(), sorted_pal_.end(), c, rgba::mean_sort_cmp());
ind = pit-sorted_pal_.begin();
if (ind == sorted_pal_.size())

View file

@ -71,7 +71,7 @@ template <typename T>
MAPNIK_DECL void save_to_file(T const& image,
std::string const& filename,
std::string const& type,
rgba_palette& palette);
rgba_palette const& palette);
// guess type from file extension
template <typename T>
@ -81,7 +81,7 @@ MAPNIK_DECL void save_to_file(T const& image,
template <typename T>
MAPNIK_DECL void save_to_file(T const& image,
std::string const& filename,
rgba_palette& palette);
rgba_palette const& palette);
template <typename T>
MAPNIK_DECL std::string save_to_string(T const& image,
@ -90,12 +90,12 @@ MAPNIK_DECL std::string save_to_string(T const& image,
template <typename T>
MAPNIK_DECL std::string save_to_string(T const& image,
std::string const& type,
rgba_palette& palette);
rgba_palette const& palette);
template <typename T>
void save_as_png(T const& image,
std::string const& filename,
rgba_palette& palette);
rgba_palette const& palette);
#if defined(HAVE_JPEG)
template <typename T>
@ -219,21 +219,27 @@ void scale_image_bilinear8 (Image& target,const Image& source, double x_off_f=0,
inline MAPNIK_DECL void save_to_file (image_32 const& image,
std::string const& file,
std::string const& type,
rgba_palette& palette)
rgba_palette const& palette)
{
save_to_file<image_data_32>(image.data(), file, type, palette);
}
inline MAPNIK_DECL void save_to_file(image_32 const& image,
std::string const& file,
rgba_palette& palette)
rgba_palette const& palette)
{
save_to_file<image_data_32>(image.data(), file, palette);
}
inline MAPNIK_DECL std::string save_to_string(image_32 const& image,
std::string const& type)
{
return save_to_string<image_data_32>(image.data(), type);
}
inline MAPNIK_DECL std::string save_to_string(image_32 const& image,
std::string const& type,
rgba_palette& palette)
rgba_palette const& palette)
{
return save_to_string<image_data_32>(image.data(), type, palette);
}
@ -242,26 +248,26 @@ inline MAPNIK_DECL std::string save_to_string(image_32 const& image,
template MAPNIK_DECL void save_to_file<image_data_32>(image_data_32 const&,
std::string const&,
std::string const&,
rgba_palette&);
rgba_palette const&);
template MAPNIK_DECL void save_to_file<image_data_32>(image_data_32 const&,
std::string const&,
rgba_palette&);
rgba_palette const&);
template MAPNIK_DECL std::string save_to_string<image_data_32>(image_data_32 const&,
std::string const&,
rgba_palette&);
rgba_palette 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&,
rgba_palette&);
rgba_palette const&);
template MAPNIK_DECL void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&,
rgba_palette&);
rgba_palette const&);
template MAPNIK_DECL std::string save_to_string<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&,
rgba_palette&);
rgba_palette const&);
#endif
}

View file

@ -129,15 +129,15 @@ public:
enum palette_type { PALETTE_RGBA = 0, PALETTE_RGB = 1, PALETTE_ACT = 2 };
explicit rgba_palette(std::string const& pal, palette_type type = PALETTE_RGBA);
explicit rgba_palette();
rgba_palette();
const std::vector<rgb>& palette() const;
const std::vector<unsigned>& alphaTable() const;
unsigned quantize(rgba const& c);
inline unsigned quantize(unsigned const& c)
unsigned quantize(rgba const& c) const;
inline unsigned quantize(unsigned const& c) const
{
rgba_hash_table::iterator it = color_hashmap_.find(c);
rgba_hash_table::const_iterator it = color_hashmap_.find(c);
if (it != color_hashmap_.end())
{
return it->second;
@ -147,14 +147,14 @@ public:
}
}
bool valid();
bool valid() const;
private:
void parse(std::string const& pal, palette_type type);
private:
std::vector<rgba> sorted_pal_;
rgba_hash_table color_hashmap_;
mutable rgba_hash_table color_hashmap_;
unsigned colors_;
std::vector<rgb> rgb_pal_;

View file

@ -451,8 +451,8 @@ void save_as_png8_oct(T1 & file, T2 const& image, const unsigned max_colors = 25
template <typename T1, typename T2, typename T3>
void save_as_png8(T1 & file, T2 const& image, T3& tree,
std::vector<mapnik::rgb> palette, std::vector<unsigned> alphaTable,
void save_as_png8(T1 & file, T2 const& image, T3 const & tree,
std::vector<mapnik::rgb> const& palette, std::vector<unsigned> const& alphaTable,
int compression = Z_DEFAULT_COMPRESSION, int strategy = Z_DEFAULT_STRATEGY)
{
unsigned width = image.width();
@ -550,7 +550,7 @@ void save_as_png8_hex(T1 & file, T2 const& image, int colors = 256,
}
template <typename T1, typename T2>
void save_as_png8_pal(T1 & file, T2 const& image, rgba_palette& pal,
void save_as_png8_pal(T1 & file, T2 const& image, rgba_palette const& pal,
int compression = Z_DEFAULT_COMPRESSION, int strategy = Z_DEFAULT_STRATEGY)
{
save_as_png8<T1, T2, rgba_palette>(file, image, pal, pal.palette(), pal.alphaTable(), compression, strategy);

View file

@ -79,7 +79,7 @@ namespace mapnik
template <typename T>
std::string save_to_string(T const& image,
std::string const& type,
rgba_palette& palette)
rgba_palette const& palette)
{
std::ostringstream ss(std::ios::out|std::ios::binary);
save_to_stream(image, ss, type, palette);
@ -90,7 +90,7 @@ template <typename T>
void save_to_file(T const& image,
std::string const& filename,
std::string const& type,
rgba_palette& palette)
rgba_palette const& palette)
{
std::ofstream file (filename.c_str(), std::ios::out| std::ios::trunc|std::ios::binary);
if (file)
@ -235,7 +235,7 @@ template <typename T>
void save_to_stream(T const& image,
std::ostream & stream,
std::string const& type,
rgba_palette& palette)
rgba_palette const& palette)
{
if (stream)
{
@ -257,7 +257,7 @@ void save_to_stream(T const& image,
&gamma,
&use_octree);
if (&palette != NULL && palette.valid())
if (palette.valid())
save_as_png8_pal(stream, image, palette, compression, strategy);
else if (colors < 0)
save_as_png(stream, image, compression, strategy);
@ -346,7 +346,7 @@ void save_to_file(T const& image, std::string const& filename)
}
template <typename T>
void save_to_file(T const& image, std::string const& filename, rgba_palette& palette)
void save_to_file(T const& image, std::string const& filename, rgba_palette const& palette)
{
boost::optional<std::string> type = type_from_filename(filename);
if (type)
@ -419,18 +419,18 @@ template void save_to_file<image_data_32>(image_data_32 const&,
template void save_to_file<image_data_32>(image_data_32 const&,
std::string const&,
std::string const&,
rgba_palette& palette);
rgba_palette const& palette);
template void save_to_file<image_data_32>(image_data_32 const&,
std::string const&);
template void save_to_file<image_data_32>(image_data_32 const&,
std::string const&,
rgba_palette& palette);
rgba_palette const& palette);
template std::string save_to_string<image_data_32>(image_data_32 const&,
std::string const&,
rgba_palette& palette);
rgba_palette const& palette);
template void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&,
@ -439,18 +439,18 @@ template void save_to_file<image_view<image_data_32> > (image_view<image_data_32
template void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&,
std::string const&,
rgba_palette& palette);
rgba_palette const& palette);
template void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&);
template void save_to_file<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&,
rgba_palette& palette);
rgba_palette const& palette);
template std::string save_to_string<image_view<image_data_32> > (image_view<image_data_32> const&,
std::string const&,
rgba_palette& palette);
rgba_palette const& palette);

View file

@ -67,13 +67,13 @@ const std::vector<unsigned>& rgba_palette::alphaTable() const
return alpha_pal_;
}
bool rgba_palette::valid()
bool rgba_palette::valid() const
{
return colors_ > 0;
}
// return color index in returned earlier palette
unsigned rgba_palette::quantize(rgba const& c)
unsigned rgba_palette::quantize(rgba const& c) const
{
unsigned index = 0;
if (colors_ == 1) return index;
@ -89,7 +89,7 @@ unsigned rgba_palette::quantize(rgba const& c)
int dist, newdist;
// find closest match based on mean of r,g,b,a
std::vector<rgba>::iterator pit =
std::vector<rgba>::const_iterator pit =
std::lower_bound(sorted_pal_.begin(), sorted_pal_.end(), c, rgba::mean_sort_cmp());
index = pit - sorted_pal_.begin();
if (index == sorted_pal_.size()) index--;
@ -191,4 +191,4 @@ void rgba_palette::parse(std::string const& pal, palette_type type)
}
}
} // namespace mapnik
} // namespace mapnik