need a way to get the size of an image more elegantly than catting it and extracting the image size in an ad-hoc manner. the code isn't very efficient since the whole image is read. Reference: /n/atom/patch/applied/imagesize Date: Wed Feb 19 03:58:20 CET 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/imgsize.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/imgsize.c Wed Feb 19 03:57:34 2014 @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +int +Rfmt(Fmt *f) +{ + Rectangle r; + + r = va_arg(f->args, Rectangle); + return fmtprint(f, "%d %d %d %d", r.min.x, r.min.y, r.max.x, r.max.y); +} + +static Rectangle +findrect(int fd) +{ + Memimage *m; + Rectangle r; + + m = readmemimage(fd); + if(m == nil) + sysfatal("imgsize: readmemimage: %r"); + r = m->r; + freememimage(m); + return r; +} + +void +usage(void) +{ + fprint(2, "usage: imgsize\n"); + exits("usage"); +} + +void +main(int argc, char **argv) +{ + int i, fd; + + ARGBEGIN{ + default: + usage(); + }ARGEND + + fmtinstall('R', Rfmt); + if(argc > 0){ + for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd == -1) + sysfatal("imgsize: open: %r"); + if(argc>1) + print("%s %R\n", argv[i], findrect(fd)); + else + print("%R\n", findrect(fd)); + close(fd); + } + }else + print("%R\n", findrect(0)); + exits(""); +} --- /sys/man/1/imgsize Thu Jan 1 00:00:00 1970 +++ /sys/man/1/imgsize Wed Feb 19 03:57:34 2014 @@ -0,0 +1,21 @@ +.TH IMGSIZE 1 +.SH NAME +imagesize \- print image rectangle +.SH SYNOPSIS +.B imgsize +[ +.I file ... +] +.SH DESCRIPTION +Print the enclosing rectangle of each input image file +without punctuation. +If no files are given on the command line, standard +input is read. +If multiple files are given, each rectangle is preceeded +by the corresponding file name. +.SH SOURCE +.B /sys/src/cmd/imgsize.c +.SH "SEE ALSO" +.IR draw (2), +.IR image (6), +.IR memdraw (2).