allow customization of JPEG output image quality using syntax similar to png256, but using 0-100 as suffix, e.g. render_to_file(m,'image.jpeg','jpeg80') - closes #198 - patch from Dennis Luxen

This commit is contained in:
Dane Springmeyer 2009-04-07 15:48:51 +00:00
parent 39bdab7762
commit 4276ae24fe
3 changed files with 49 additions and 6 deletions

View file

@ -11,6 +11,14 @@ Developers: Please commit along with changes.
For a complete change history, see the SVN log.
0.6.1 SVN
---------
- AGG Renderer: Added option to control output JPEG quality (r1078) (#198)
- Plugins: Fixed segfault in OGC Plugin with empty geometries (r1074) (#292)
Mapnik 0.6.0 Release
--------------------

View file

@ -31,7 +31,7 @@
// boost
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
// stl
#include <string>

View file

@ -39,6 +39,7 @@ extern "C"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
namespace mapnik
{
@ -50,7 +51,24 @@ namespace mapnik
//all this should go into image_writer factory
if (type=="png") save_as_png(ss,image);
else if (type == "png256") save_as_png256(ss,image);
else if (type=="jpeg") save_as_jpeg(ss,85,image);
else if (boost::algorithm::istarts_with(type,std::string("jpeg")))
{
int quality = 85;
try
{
if(type.substr(4).length() != 0)
{
quality = boost::lexical_cast<int>(type.substr(4));
if(quality<1 || quality>100)
throw ImageWriterException("invalid jpeg quality: " + type.substr(4));
}
save_as_jpeg(ss,quality,image);
}
catch(boost::bad_lexical_cast &)
{
throw ImageWriterException("invalid jpeg quality: " + type.substr(4));
}
}
else throw ImageWriterException("unknown file type: " + type);
return ss.str();
}
@ -66,9 +84,26 @@ namespace mapnik
//all this should go into image_writer factory
if (type=="png") save_as_png(file,image);
else if (type == "png256") save_as_png256(file,image);
else if (type=="jpeg") save_as_jpeg(file,85,image);
else throw ImageWriterException("unknown file type: " + type);
}
else if (boost::algorithm::istarts_with(type,std::string("jpeg")))
{
int quality = 85;
try
{
if(type.substr(4).length() != 0)
{
quality = boost::lexical_cast<int>(type.substr(4));
if(quality<0 || quality>100)
throw ImageWriterException("invalid jpeg quality: " + type.substr(4) + " out of bounds");
}
save_as_jpeg(file,quality,image);
}
catch(boost::bad_lexical_cast &)
{
throw ImageWriterException("invalid jpeg quality: " + type.substr(4) + " not a number");
}
}
else throw ImageWriterException("unknown file type: " + type);
}
}
template <typename T>