#!/usr/bin/pike
/*
* paster.pike
*
* Resize a png into another png
*
* History:
* 28/09/2020 Creation from resizer.pike
*
* Author: Dario Rodriguez dario@softhome.net
* This program is distributed under the terms of the GNU GPL v2.1+
*/
int
main(int argc, array(string) argv)
{
int i;
string geom;
mapping imagemapping;
int xsize,ysize;
int xoff,yoff;
string destfilename;
Image.Image image,alpha;
array(Image.Layer) layers=({});
Image.Layer l;
if(argc<4 || (argc%2)!=0 || (argc>1 && argv[1]=="--help")) {
Stdio.werror("Syntax: "+argv[0]+" <geom1> { <file1.png> | <htmlcolor1> } [<geom2> { <file2.png> | <htmlcolor2> } [...]] <outfile.png>\n");
Stdio.werror("Example: "+argv[0]+" 200x300+0+0 bg.png 150x0+25+25 image.png 100x1+0+0 \"#ff0000\" dest.png\n");
Stdio.werror("Example result: dest.png\n");
return(1);
}
destfilename=argv[argc-1];
write("* Loading images...\n");
for(i=1;(i+1)<argc;i+=2) {
geom=argv[i];
if(sizeof(argv[i+1])>1 && argv[i+1][0]=='#') {
string htmlcolor=argv[i+1];
int r,g,b,a;
a=0;
htmlcolor-="#";
sscanf(htmlcolor[0..1],"%x",r);
sscanf(htmlcolor[2..3],"%x",g);
sscanf(htmlcolor[4..5],"%x",b);
if(sizeof(htmlcolor)>6)
sscanf(htmlcolor[6..7],"%x",a);
write("r:"+r+" g:"+g+" b:"+b+" a:"+a+"\n");
image=Image.Image(1,1,r,g,b);
imagemapping=([ "image":image, "alpha":Image.Image(1,1,255-a,255-a,255-a) ]);
} else {
imagemapping=Image._load(argv[i+1]);
}
xsize=ysize=xoff=yoff=0;
if(sizeof(geom/"x")>1)
xsize=(int) (geom/"x")[0];
if(sizeof(geom/"x")>1)
ysize=(int) (((geom/"x")[1])/"+")[0];
if(sizeof(geom/"+")>1)
xoff=(int) (geom/"+")[1];
if(sizeof(geom/"+")>2)
yoff=(int) (geom/"+")[2];
image=imagemapping["image"];
alpha=imagemapping["alpha"];
xsize=(xsize!=0)?xsize:(ysize==0)?image->xsize():ysize*image->xsize()/image->ysize();
ysize=(ysize!=0)?ysize:(xsize==0)?image->ysize():xsize*image->ysize()/image->xsize();
if(xsize!=image->xsize() || ysize!=image->ysize()) {
image=image->scale(xsize,ysize);
if(!intp(alpha))
alpha=alpha->scale(xsize,ysize);
}
if(intp(alpha))
alpha=Image.Image(xsize,ysize,255,255,255);
l=Image.Layer(image,alpha,"normal");
l->set_offset(xoff,yoff);
layers+=({l});
}
l=Image.lay(layers);
l->set_offset(0,0);
write("* Writing "+destfilename+"...\n");
Stdio.write_file(destfilename,Image.PNG.encode(l->image(),(["alpha":l->alpha()])));
write("* Finished successfully\n");
return(0);
}