示例#1
0
int parse_jpeg_quality(std::string const& params)
{
    int quality = 85;
    if (params != "jpeg")
    {
        for (auto const& kv : parse_image_options(params))
        {
            auto const& key = kv.first;
            auto const& val = kv.second;

            if ( key == "jpeg" ) continue;
            else if ( key.size() > 4  && key.substr(0,4) == "jpeg")
            {
                if (!mapnik::util::string2int(key.substr(4), quality))
                {
                    throw image_writer_exception("invalid jpeg quality: '" + key.substr(4)  + "'");
                }
            }
            else if ( key == "quality")
            {
                if (val && ! (*val).empty())
                {
                    if (!mapnik::util::string2int(*val, quality) || quality < 0 || quality > 100)
                    {
                        throw image_writer_exception("invalid jpeg quality: '" + *val + "'");
                    }
                }
            }
        }
    }
    return quality;
}
示例#2
0
void process_rgba8_jpeg(T const& image, std::string const& type, std::ostream & stream)
{
#if defined(HAVE_JPEG)
    int quality = 85;
    if (type != "jpeg")
    {
        for (auto const& kv : parse_image_options(type))
        {
            auto const& key = kv.first;
            auto const& val = kv.second;

            if ( key == "jpeg" ) continue;
            else if ( key == "quality")
            {
                if (val && ! (*val).empty())
                {
                    if (!mapnik::util::string2int(*val, quality) || quality < 0 || quality > 100)
                    {
                        throw image_writer_exception("invalid jpeg quality: '" + *val + "'");
                    }
                }
            }
        }
    }
    save_as_jpeg(stream, quality, image);
#else
    throw image_writer_exception("jpeg output is not enabled in your build of Mapnik");
#endif
}
示例#3
0
void handle_tiff_options(std::string const& type,
                        tiff_config & config)
{
    if (type == "tiff")
    {
        return;
    }
    if (type.length() > 4)
    {

        for (auto const& kv : parse_image_options(type))
        {
            auto const& key = kv.first;
            auto const& val = kv.second;
            if (key == "tiff") continue;
            else if (key == "compression")
            {
                if (val && !(*val).empty())
                {
                    if (*val == "deflate")
                    {
                        config.compression = COMPRESSION_DEFLATE;
                    }
                    else if (*val == "adobedeflate")
                    {
                        config.compression = COMPRESSION_ADOBE_DEFLATE;
                    }
                    else if (*val == "lzw")
                    {
                        config.compression = COMPRESSION_LZW;
                    }
                    else if (*val == "none")
                    {
                        config.compression = COMPRESSION_NONE;
                    }
                    else
                    {
                        throw image_writer_exception("invalid tiff compression: '" + *val + "'");
                    }
                }
            }
            else if (key == "method")
            {
                if (val && !(*val).empty())
                {
                    if (*val == "scanline")
                    {
                        config.method = TIFF_WRITE_SCANLINE;
                    }
                    else if (*val == "strip" || *val == "stripped")
                    {
                        config.method = TIFF_WRITE_STRIPPED;
                    }
                    else if (*val == "tiled")
                    {
                        config.method = TIFF_WRITE_TILED;
                    }
                    else
                    {
                        throw image_writer_exception("invalid tiff method: '" + *val + "'");
                    }
                }
            }
            else if (key == "zlevel")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.zlevel) || config.zlevel < 0 || config.zlevel > 9)
                    {
                        throw image_writer_exception("invalid tiff zlevel: '" + *val + "'");
                    }
                }
            }
            else if (key == "tile_height")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.tile_height) || config.tile_height < 0 )
                    {
                        throw image_writer_exception("invalid tiff tile_height: '" + *val + "'");
                    }
                }
            }
            else if (key == "tile_width")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.tile_width) || config.tile_width < 0 )
                    {
                        throw image_writer_exception("invalid tiff tile_width: '" + *val + "'");
                    }
                }
            }
            else if (key == "rows_per_strip")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.rows_per_strip) || config.rows_per_strip < 0 )
                    {
                        throw image_writer_exception("invalid tiff rows_per_strip: '" + *val + "'");
                    }
                }
            }
            else
            {
                throw image_writer_exception("unhandled tiff option: " + key);
            }
        }
    }
}
示例#4
0
void handle_webp_options(std::string const& type,
                        WebPConfig & config,
                        bool & alpha)
{
    if (type == "webp")
    {
        return;
    }
    if (type.length() > 4)
    {
        for (auto const& kv : parse_image_options(type))
        {
            auto const& key = kv.first;
            auto const& val = kv.second;

            if (key == "webp") continue;
            else if (key == "quality")
            {
                if (val && !(*val).empty())
                {
                    double quality = 90;
                    if (!mapnik::util::string2double(*val,quality) || quality < 0.0 || quality > 100.0)
                    {
                        throw image_writer_exception("invalid webp quality: '" + *val + "'");
                    }
                    config.quality = static_cast<float>(quality);
                }
            }
            else if (key == "method")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.method) || config.method < 0 || config.method > 6)
                    {
                        throw image_writer_exception("invalid webp method: '" + *val + "'");
                    }
                }
            }
            else if (key == "lossless")
            {
                if (val && !(*val).empty())
                {
                    #if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1 // >= v0.1.99 / 0x0100
                    if (!mapnik::util::string2int(*val,config.lossless) || config.lossless < 0 || config.lossless > 1)
                    {
                        throw image_writer_exception("invalid webp lossless: '" + *val + "'");
                    }
                    #else
                        #ifdef _MSC_VER
                          #pragma NOTE(compiling against webp that does not support the lossless flag)
                        #else
                          #warning "compiling against webp that does not support the lossless flag"
                        #endif
                    throw image_writer_exception("your webp version does not support the lossless option");
                    #endif
                }
            }
            else if (key == "image_hint")
            {
                if (val && !(*val).empty())
                {
                    #if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1 // >= v0.1.99 / 0x0100
                    int image_hint = 0;
                    if (!mapnik::util::string2int(*val,image_hint) || image_hint < 0 || image_hint > 3)
                    {
                        throw image_writer_exception("invalid webp image_hint: '" + *val + "'");
                    }
                    config.image_hint = static_cast<WebPImageHint>(image_hint);
                    #else
                        #ifdef _MSC_VER
                          #pragma NOTE(compiling against webp that does not support the image_hint flag)
                        #else
                          #warning "compiling against webp that does not support the image_hint flag"
                        #endif
                    throw image_writer_exception("your webp version does not support the image_hint option");
                    #endif
                }
            }
            else if (key == "alpha")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2bool(*val,alpha))
                    {
                        throw image_writer_exception("invalid webp alpha: '" + *val + "'");
                    }
                }
            }
            else if (key == "target_size")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.target_size))
                    {
                        throw image_writer_exception("invalid webp target_size: '" + *val + "'");
                    }
                }
            }
            else if (key == "target_psnr")
            {
                if (val && !(*val).empty())
                {
                    double psnr = 0;
                    if (!mapnik::util::string2double(*val,psnr))
                    {
                        throw image_writer_exception("invalid webp target_psnr: '" + *val + "'");
                    }
                    config.target_PSNR = psnr;
                }
            }
            else if (key == "segments")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.segments))
                    {
                        throw image_writer_exception("invalid webp segments: '" + *val + "'");
                    }
                }
            }
            else if (key == "sns_strength")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.sns_strength))
                    {
                        throw image_writer_exception("invalid webp sns_strength: '" + *val + "'");
                    }
                }
            }
            else if (key == "filter_strength")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.filter_strength))
                    {
                        throw image_writer_exception("invalid webp filter_strength: '" + *val + "'");
                    }
                }
            }
            else if (key == "filter_sharpness")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.filter_sharpness))
                    {
                        throw image_writer_exception("invalid webp filter_sharpness: '" + *val + "'");
                    }
                }
            }
            else if (key == "filter_type")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.filter_type))
                    {
                        throw image_writer_exception("invalid webp filter_type: '" + *val + "'");
                    }
                }
            }
            else if (key == "autofilter")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.autofilter))
                    {
                        throw image_writer_exception("invalid webp autofilter: '" + *val + "'");
                    }
                }
            }
            else if (key == "alpha_compression")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.alpha_compression))
                    {
                        throw image_writer_exception("invalid webp alpha_compression: '" + *val + "'");
                    }
                }
            }
            else if (key == "alpha_filtering")
            {
                if (val && !(*val).empty())
                {
                    #if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1 // >= v0.1.99 / 0x0100
                    if (!mapnik::util::string2int(*val,config.alpha_filtering))
                    {
                        throw image_writer_exception("invalid webp alpha_filtering: '" + *val + "'");
                    }
                    #else
                        #ifdef _MSC_VER
                          #pragma NOTE(compiling against webp that does not support the alpha_filtering flag)
                        #else
                          #warning "compiling against webp that does not support the alpha_filtering flag"
                        #endif
                    throw image_writer_exception("your webp version does not support the alpha_filtering option");
                    #endif
                }
            }
            else if (key == "alpha_quality")
            {
                if (val && !(*val).empty())
                {
                    #if (WEBP_ENCODER_ABI_VERSION >> 8) >= 1 // >= v0.1.99 / 0x0100
                    if (!mapnik::util::string2int(*val,config.alpha_quality))
                    {
                        throw image_writer_exception("invalid webp alpha_quality: '" + *val + "'");
                    }
                    #else
                        #ifdef _MSC_VER
                          #pragma NOTE(compiling against webp that does not support the alpha_quality flag)
                        #else
                          #warning "compiling against webp that does not support the alpha_quality flag"
                        #endif
                    throw image_writer_exception("your webp version does not support the alpha_quality option");
                    #endif
                }
            }
            else if (key == "pass")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.pass))
                    {
                        throw image_writer_exception("invalid webp pass: '******'");
                    }
                }
            }
            else if (key == "preprocessing")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.preprocessing))
                    {
                        throw image_writer_exception("invalid webp preprocessing: '" + *val + "'");
                    }
                }
            }
            else if (key == "partitions")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.partitions))
                    {
                        throw image_writer_exception("invalid webp partitions: '" + *val + "'");
                    }
                }
            }
            else if (key == "partition_limit")
            {
                if (val && !(*val).empty())
                {
                    if (!mapnik::util::string2int(*val,config.partition_limit))
                    {
                        throw image_writer_exception("invalid webp partition_limit: '" + *val + "'");
                    }
                }
            }
            else
            {
                throw image_writer_exception("unhandled webp option: " + key);
            }
        }
    }
}
示例#5
0
void handle_png_options(std::string const& type,
                        png_options & opts)
{
    if (type == "png" || type == "png24" || type == "png32")
    {
        opts.paletted = false;
        return;
    }
    else if (type == "png8" || type == "png256")
    {
        opts.paletted = true;
        return;
    }

    bool set_colors = false;
    bool set_gamma = false;

    for (auto const& kv : parse_image_options(type))
    {
        auto const& key = kv.first;
        auto const& val = kv.second;
        if (key == "png8" || key == "png256")
        {
            opts.paletted = true;
        }
        else if (key == "png" || key == "png24" || key == "png32")
        {
            opts.paletted = false;
        }
        else if (key == "m" && val)
        {
            if (*val == "o") opts.use_hextree = false;
            else if (*val == "h") opts.use_hextree = true;
        }
        else if (key == "e" && val && *val == "miniz")
        {
            opts.use_miniz = true;
        }
        else if (key == "c")
        {
            set_colors = true;
            if (!val || !mapnik::util::string2int(*val, opts.colors) || opts.colors < 1 ||  opts.colors > 256)
            {
                throw image_writer_exception("invalid color parameter: " + to_string(val));
            }
        }
        else if (key == "t")
        {
            if (!val || !mapnik::util::string2int(*val,opts.trans_mode) || opts.trans_mode < 0 || opts.trans_mode > 2)
            {
                throw image_writer_exception("invalid trans_mode parameter: " + to_string(val));
            }
        }
        else if (key == "g")
        {
            set_gamma = true;
            if (!val || !mapnik::util::string2double(*val, opts.gamma) || opts.gamma < 0)
            {
                throw image_writer_exception("invalid gamma parameter: " + to_string(val));
            }
        }
        else if (key == "z")
        {
            /*
              #define Z_NO_COMPRESSION         0
              #define Z_BEST_SPEED             1
              #define Z_BEST_COMPRESSION       9
              #define Z_DEFAULT_COMPRESSION  (-1)
            */
            if (!val || !mapnik::util::string2int(*val, opts.compression)
                || opts.compression < Z_DEFAULT_COMPRESSION
                || opts.compression > 10) // use 10 here rather than Z_BEST_COMPRESSION (9) to allow for MZ_UBER_COMPRESSION
            {
                throw image_writer_exception("invalid compression parameter: " + to_string(val) + " (only -1 through 10 are valid)");
            }
        }
        else if (key == "s")
        {
            if (!val) throw image_writer_exception("invalid compression parameter: <uninitialised>");

            if (*val == "default")
            {
                opts.strategy = Z_DEFAULT_STRATEGY;
            }
            else if (*val == "filtered")
            {
                opts.strategy = Z_FILTERED;
            }
            else if (*val == "huff")
            {
                opts.strategy = Z_HUFFMAN_ONLY;
            }
            else if (*val == "rle")
            {
                opts.strategy = Z_RLE;
            }
            else if (*val == "fixed")
            {
                opts.strategy = Z_FIXED;
            }
            else
            {
                throw image_writer_exception("invalid compression strategy parameter: " + *val);
            }
        }
        else
        {
            throw image_writer_exception("unhandled png option: " + key);
        }
    }
    // validation
    if (!opts.paletted && set_colors)
    {
        throw image_writer_exception("invalid color parameter: unavailable for true color (non-paletted) images");
    }
    if (!opts.paletted && set_gamma)
    {
        throw image_writer_exception("invalid gamma parameter: unavailable for true color (non-paletted) images");
    }
    if ((opts.use_miniz == false) && opts.compression > Z_BEST_COMPRESSION)
    {
        throw image_writer_exception("invalid compression value: (only -1 through 9 are valid)");
    }
}