From 8db68fc5e3db7fe105e3366e27fcd7654a4795a4 Mon Sep 17 00:00:00 2001 From: artemp Date: Thu, 21 Aug 2014 12:43:43 +0100 Subject: [PATCH] hextree - avoid quantizing images with less than 3 pixels (hextree implementation requirement) --- include/mapnik/png_io.hpp | 61 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/include/mapnik/png_io.hpp b/include/mapnik/png_io.hpp index 5d338dabc..3df3197a4 100644 --- a/include/mapnik/png_io.hpp +++ b/include/mapnik/png_io.hpp @@ -654,40 +654,45 @@ void save_as_png8_hex(T1 & file, { unsigned width = image.width(); unsigned height = image.height(); - - // structure for color quantization - hextree tree(opts.colors); - if (opts.trans_mode >= 0) + if (width + height > 3) // at least 3 pixels (hextree implementation requirement) { - tree.setTransMode(opts.trans_mode); - } - if (opts.gamma > 0) - { - tree.setGamma(opts.gamma); - } - - for (unsigned y = 0; y < height; ++y) - { - typename T2::pixel_type const * row = image.getRow(y); - for (unsigned x = 0; x < width; ++x) + // structure for color quantization + hextree tree(opts.colors); + if (opts.trans_mode >= 0) { - unsigned val = row[x]; - tree.insert(mapnik::rgba(U2RED(val), U2GREEN(val), U2BLUE(val), U2ALPHA(val))); + tree.setTransMode(opts.trans_mode); + } + if (opts.gamma > 0) + { + tree.setGamma(opts.gamma); } - } - //transparency values per palette index - std::vector pal; - tree.create_palette(pal); - std::vector palette; - std::vector alphaTable; - for (unsigned i=0; i pal; + tree.create_palette(pal); + std::vector palette; + std::vector alphaTable; + for (unsigned i=0; i >(file, image, tree, palette, alphaTable, opts); + } + else { - palette.push_back(rgb(pal[i].r, pal[i].g, pal[i].b)); - alphaTable.push_back(pal[i].a); + throw std::runtime_error("Can't quantize images with less than 3 pixels"); } - - save_as_png8 >(file, image, tree, palette, alphaTable, opts); } template