Browse Source

Gmt module: Add functions Convert2PNG and Convert2JPG.

ObjPtr
Michael Uleysky 8 years ago
parent
commit
e1443f1636
  1. 3
      modules/gmt/modgmt.cpp
  2. 242
      modules/gmt/modgmt_map.cpp
  3. 24
      modules/gmt/modgmt_map.h

3
modules/gmt/modgmt.cpp

@ -46,6 +46,9 @@ int gmt_module_init(void* p)
RegisterFunction("DrawFrame",GMT_DrawFrame);
RegisterFunction("Map",GMT_Map);
RegisterFunction("Convert2PDF",GMT_Convert2PDF);
RegisterFunction("Convert2PNG",GMT_Convert2PNG);
RegisterFunction("Convert2JPG",GMT_Convert2JPG);
RegisterFunction("Convert2JPEG",GMT_Convert2JPG);
CheckGhostscriptAbilities();
// Calculating bounding box is critical

242
modules/gmt/modgmt_map.cpp

@ -269,3 +269,245 @@ ObjectBase* GMT_Convert2PDF(const ObjectList* input)
fail:
return 0;
}
// Creating png from eps or pdf
/*
Input:
One argument must be GMTMap or GMTMapPDF.
Other parameter are optional name-value pairs.
resolution, res or r - output resolution (pixels/inch). Default: 300.
Instead of resolution, width (width or w) or height (height or h) in pixels may be specified. Resolution, width and height are mutually exclusive. Large value of resolution may result in ghostscript error.
colorspace, cspace or cs - is a string, one of "mono" or "monochrome" (black and white image), "monodiffused" or "monod" (black and white image made from grayscale by error diffusion), "16c" or "16" (16 colors), "256c" or "256" (256 colors), "gray", "grey" or "g" (256 shades of gray), "full", "8bit" or "color" (8-bit per component rgb). Some of these values may return error, this depends on ghostscript setup in system. Default: "color".
For colorspaces "monod", "color" and "gray" may be also specified parameter downscale (dscale or ds) with small integer value (from 1 to 10). The image initially rendered in dscale*res resolution and downscaling to res resolution. This increase output quality (lines and contours are more "soft"), but may result in ghostscript error if rendering resolution is too high.
textantialiasing or taa - is a string, can be "none" ("no"), "small" ("s") or "full" ("f"). Controls text antialiasing. Default: "full".
graphicsantialiasing or gaa - is a string, can be "none", "small" or "full". Controls graphics antialiasing. Default: "full".
antialiasing or aa - can be used to set graphics and text antialiasing simultaneously.
*/
ObjectBase* GMT_Convert2PNG(const ObjectList* input)
{
if(!gs_abilities.havepdf) return 0; // No pdf support, so, no png
double r;
bool suc=true;
const GMTMap* map=0;
std::string gsdev("");
bool ispdf;
uint aat=0,aag=0;
std::string aats("full"),aags("full");
uint dscale=0;
// Map to convert
{
for(ObjectList::ListValues::size_type i=0;i<input->Size();i++)
{
OBType<ObjectGMTMap> m(input->At(i));
OBType<ObjectGMTMapPDF> p(input->At(i));
if(m)
{
if(0!=map) goto fail; // Duplicate
map=m;
ispdf=false;
}
if(p)
{
if(0!=map) goto fail; // Duplicate
map=p;
ispdf=true;
}
}
if(0==map) goto fail; // Map not found
}
// Determine resolution
{
SearchParameter<double,false,PMin<1> > res(input,"r","res","resolution");
BaseM2Double w(input,"width","w"), h(input,"height","h");
// Check existence
if( ( res.Exist() && w.Exist() ) || ( res.Exist() && h.Exist() ) || ( w.Exist() && h.Exist() )) goto fail; // Only one parameter allowed
r=300; // Default value
if(res.Exist()) r=res(&suc);
if(w.Exist()) r=w(&suc)*72.0/(map->Bbrx()-map->Bblx());
if(h.Exist()) r=h(&suc)*72.0/(map->Bbry()-map->Bbly());
if(!suc) goto fail; // Error
}
// Color model
{
std::string cs;
BaseMD2String cspace(input,"8bit","colorspace","cspace","cs");
cs=cspace(&suc);
if(!suc) goto fail; // Error
tolower(cs);
if(("mono"==cs || "monochrome"==cs) && gs_abilities.havepngmono) gsdev="pngmono";
if(("monod"==cs || "monodiffused"==cs) && gs_abilities.havepngmonod) gsdev="pngmonod";
if(("16c"==cs || "16"==cs) && gs_abilities.havepng16) gsdev="png16";
if(("256c"==cs || "256"==cs) && gs_abilities.havepng256) gsdev="png256";
if(("gray"==cs || "grey"==cs || "g"==cs) && gs_abilities.havepnggray) gsdev="pnggray";
if(("full"==cs || "8bit"==cs || "color"==cs) && gs_abilities.havepng16m) gsdev="png16m";
if(""==gsdev) goto fail; // Incorrect value
}
// Antialiasing
{
BaseM2String aa(input,"antialiasing","aa"),taa(input,"textantialiasing","taa"),gaa(input,"graphicsantialiasing","gaa");
if(aa.Exist()) aags=aats=aa(&suc);
if(taa.Exist()) aats=taa(&suc);
if(gaa.Exist()) aags=gaa(&suc);
if(!suc) goto fail; // Error
tolower(aags); tolower(aats);
if("none"==aags || "no"==aags || "n"==aags) aag=1; if("none"==aats || "no"==aats || "n"==aats) aat=1;
if("small"==aags || "s"==aags) aag=2; if("small"==aats || "s"==aats) aat=2;
if("full"==aags || "f"==aags) aag=4; if("full"==aats || "f"==aats) aat=4;
if(0==aat || 0==aag) goto fail; // Incorrect value
}
// Downscale
if("pngmonod"==gsdev || "pnggray"==gsdev || "png16m"==gsdev)
{
SearchParameterWDefO<double,DoubleDefaultVal<1>,false,PMin<1>,PMax<10>,PInt > ds(input,"downscale","dscale","ds");
dscale=static_cast<uint>(ds(&suc));
if(!suc) goto fail; // Error
r*=dscale;
}
// Go!
{
std::string* pdf=0;
int ret;
if(!ispdf)
{
pdf=new std::string;
ret=eps2pdf(*(map->pValue()),pdf,r,aat,aag);
if(0!=ret) { delete pdf; goto fail; } // Something wrong
}
{
std::string* out=new std::string;
ret=GhostRun("-r"+ToString(r)+" -dTextAlphaBits="+ToString(aat)+" -dGraphicAlphaBits="+ToString(aag)+" -sDEVICE="+gsdev+((dscale>1)?(" -dDownScaleFactor="+ToString(dscale)):""),((0!=pdf)?*pdf:*(map->pValue())),0,0,out);
if(0!=pdf) delete pdf;
if(0!=ret) { delete out; goto fail; } // Something wrong
return new ObjectGMTImage(out);
}
}
fail:
return 0;
}
// Creating jpeg from eps or pdf
/*
Input:
One argument must be GMTMap or GMTMapPDF.
Other parameter are optional name-value pairs.
resolution, res or r - output resolution (pixels/inch). Default: 300.
Instead of resolution, width (width or w) or height (height or h) in pixels may be specified. Resolution, width and height are mutually exclusive. Large value of resolution may result in ghostscript error.
colorspace, cspace or cs - is a string, one "gray", "grey" or "g" (256 shades of gray), "full", "8bit" or "color" (8-bit per component rgb). Some of these values may return error, this depends on ghostscript setup in system. Default: "color".
quality, qual or q - JPEG quality level, integer from 0 to 100. Default: 75.
textantialiasing or taa - is a string, can be "none" ("no"), "small" ("s") or "full" ("f"). Controls text antialiasing. Default: "full".
graphicsantialiasing or gaa - is a string, can be "none", "small" or "full". Controls graphics antialiasing. Default: "full".
antialiasing or aa - can be used to set graphics and text antialiasing simultaneously.
*/
ObjectBase* GMT_Convert2JPG(const ObjectList* input)
{
if(!gs_abilities.havepdf) return 0; // No pdf support, so, no jpeg
double r;
bool suc=true;
const GMTMap* map=0;
std::string gsdev("");
bool ispdf;
uint aat=0,aag=0;
std::string aats("full"),aags("full");
uint qual;
// Map to convert
{
for(ObjectList::ListValues::size_type i=0;i<input->Size();i++)
{
OBType<ObjectGMTMap> m(input->At(i));
OBType<ObjectGMTMapPDF> p(input->At(i));
if(m)
{
if(0!=map) goto fail; // Duplicate
map=m;
ispdf=false;
}
if(p)
{
if(0!=map) goto fail; // Duplicate
map=p;
ispdf=true;
}
}
if(0==map) goto fail; // Map not found
}
// Determine resolution
{
SearchParameter<double,false,PMin<1> > res(input,"r","res","resolution");
BaseM2Double w(input,"width","w"), h(input,"height","h");
// Check existence
if( ( res.Exist() && w.Exist() ) || ( res.Exist() && h.Exist() ) || ( w.Exist() && h.Exist() )) goto fail; // Only one parameter allowed
r=300; // Default value
if(res.Exist()) r=res(&suc);
if(w.Exist()) r=w(&suc)*72.0/(map->Bbrx()-map->Bblx());
if(h.Exist()) r=h(&suc)*72.0/(map->Bbry()-map->Bbly());
if(!suc) goto fail; // Error
}
// Color model
{
std::string cs;
BaseMD2String cspace(input,"8bit","colorspace","cspace","cs");
cs=cspace(&suc);
if(!suc) goto fail; // Error
tolower(cs);
if(("gray"==cs || "grey"==cs || "g"==cs) && gs_abilities.havejpeggray) gsdev="jpeggray";
if(("full"==cs || "8bit"==cs || "color"==cs) && gs_abilities.havejpeg) gsdev="jpeg";
if(""==gsdev) goto fail; // Incorrect value
}
// Antialiasing
{
BaseM2String aa(input,"antialiasing","aa"),taa(input,"textantialiasing","taa"),gaa(input,"graphicsantialiasing","gaa");
if(aa.Exist()) aags=aats=aa(&suc);
if(taa.Exist()) aats=taa(&suc);
if(gaa.Exist()) aags=gaa(&suc);
if(!suc) goto fail; // Error
tolower(aags); tolower(aats);
if("none"==aags || "no"==aags || "n"==aags) aag=1; if("none"==aats || "no"==aats || "n"==aats) aat=1;
if("small"==aags || "s"==aags) aag=2; if("small"==aats || "s"==aats) aat=2;
if("full"==aags || "f"==aags) aag=4; if("full"==aats || "f"==aats) aat=4;
if(0==aat || 0==aag) goto fail; // Incorrect value
}
// Quality
{
SearchParameterWDefO<double,DoubleDefaultVal<75>,false,PMin<0>,PMax<100>,PInt > q(input,"quality","qual","q");
qual=static_cast<uint>(q(&suc));
if(!suc) goto fail; // Error
}
// Go!
{
std::string* pdf=0;
int ret;
if(!ispdf)
{
pdf=new std::string;
ret=eps2pdf(*(map->pValue()),pdf,r,aat,aag);
if(0!=ret) { delete pdf; goto fail; } // Something wrong
}
{
std::string* out=new std::string;
ret=GhostRun("-r"+ToString(r)+" -dTextAlphaBits="+ToString(aat)+" -dGraphicAlphaBits="+ToString(aag)+" -sDEVICE="+gsdev+" -dJPEGQ="+ToString(qual),((0!=pdf)?*pdf:*(map->pValue())),0,0,out);
if(0!=pdf) delete pdf;
if(0!=ret) { delete out; goto fail; } // Something wrong
return new ObjectGMTImage(out);
}
}
fail:
return 0;
}

24
modules/gmt/modgmt_map.h

@ -1,5 +1,6 @@
#ifndef MODGMT_MAP_H
#define MODGMT_MAP_H
#include <cmath>
#include "common.h"
class GMTBlob: public ObjectBase
@ -61,11 +62,34 @@ class ObjectGMTMap: public GMTMap
std::string Type() const override {return "GMTMap";}
};
class ObjectGMTImage: public GMTBlob
{
ObjectGMTImage(const ObjectGMTImage* p):GMTBlob(p) {};
public:
ObjectGMTImage(std::string* s):GMTBlob(s) {};
// Pure virtual overrides
ObjectBase* Copy() const override {return new ObjectGMTImage(this);}
std::string Type() const override {return "GMTImage";}
};
// Policy for get integer value
class PInt
{
public:
double operator()(double v, bool* suc) const {if(v!=floor(v)) *suc=false; return v;}
};
// Creating eps map from set of layers
ObjectBase* GMT_Map(const ObjectList* input);
// Converting map to pdf
ObjectBase* GMT_Convert2PDF(const ObjectList* input);
// Converting map to png
ObjectBase* GMT_Convert2PNG(const ObjectList* input);
// Converting map to jpeg
ObjectBase* GMT_Convert2JPG(const ObjectList* input);
#endif

Loading…
Cancel
Save