@@ 225,16 225,108 @@ checkfile(void)
exits("checkfile");
}
+Image *
+openimage(char *name)
+{
+ int i, n, p[2], fd;
+ char buf[64], *conv;
+ Waitmsg *wm;
+ Image *ret;
+
+ pipe(p);
+
+ if(fork() == 0){
+ close(1);
+ dup(p[0], 1);
+ execl("/bin/file", "file", "-m", name, nil);
+ }
+ wm = wait();
+ if(wm->msg != nil && *wm->msg != '\0')
+ sysfatal("%s", wm->msg);
+ free(wm);
+
+ n = read(p[1], buf, sizeof(buf));
+ buf[n - 1] = '\0'; /* replace the newline */
+
+ conv = nil;
+ for(i = 0; mimetab[i] != nil; i+=2){
+ if(strcmp(buf, mimetab[i]) == 0){
+ conv = mimetab[i+1];
+ break;
+ }
+ }
+ if(conv == nil)
+ sysfatal("unknown image format '%s'", buf);
+
+ if(*conv == '\0'){
+ fd = open(name, OREAD);
+ if(fd < 0)
+ sysfatal("%r");
+ ret = readimage(display, fd, 0);
+ close(fd);
+ }else{
+ if(fork() == 0){
+ close(1);
+ dup(p[0], 1);
+ sprint(buf, "/bin/%s", conv);
+ execl(buf, conv, "-9c", name, nil);
+ }
+ ret = readimage(display, p[1], 0);
+ }
+
+ close(p[0]);
+ close(p[1]);
+
+ return ret;
+}
+
+Image *
+resizeimage(Image *img, Point sdims)
+{
+ int p[2], fd;
+ char namebuf[64], dimsbuf[64];
+ Point idims;
+ Image *ret;
+
+ pipe(p);
+
+ idims = subpt(img->r.max, img->r.min);
+
+ sprint(namebuf, "/tmp/%s.%d.img", argv0, getpid());
+ fd = create(namebuf, OWRITE, 0600);
+ if(fd < 0)
+ sysfatal("couldn't open tmp file: %r");
+ writeimage(fd, img, 0);
+ close(fd);
+
+ if(fork() == 0){
+ close(1);
+ dup(p[0], 1);
+
+ if(idims.x < idims.y || (idims.x == idims.y && sdims.x > sdims.y)){
+ sprint(dimsbuf, "%d", sdims.x);
+ execl("/bin/resize", "resize", "-x", dimsbuf, namebuf, nil);
+ }else{
+ sprint(dimsbuf, "%d", sdims.y);
+ execl("/bin/resize", "resize", "-y", dimsbuf, namebuf, nil);
+ }
+ }
+
+ ret = readimage(display, p[1], 0);
+ close(p[0]);
+ close(p[1]);
+
+ return ret;
+}
+
// FIXME: some of this could be done in-process but i am lazy
// TODO: should check image formats in checkimage() instead
void
loadimages(void){
const Slide *s;
Image **nextimg, *cimg;
- Waitmsg *wm;
- Point idims, sdims;
- int i, j, n, cols, rows, p[2], fd;
- char buf[64], buf2[64], *conv;
+ Point sdims;
+ int i, j, cols, rows;
if(images != nil)
return;
@@ 244,8 336,6 @@ loadimages(void){
images = calloc(s->nlines, sizeof(Image*));
nextimg = images;
- pipe(p);
-
cols = 0;
i = 0;
while(i < s->nlines && *(s->lines[i]) == '\0') i++;
@@ 272,74 362,18 @@ loadimages(void){
sdims.x /= cols;
sdims.y /= rows;
- if(fork() == 0){
- close(1);
- dup(p[0], 1);
- execl("/bin/file", "file", "-m", s->lines[i], nil);
- }
- wm = wait();
- if(wm->msg != nil && *wm->msg != '\0')
- sysfatal("%s", wm->msg);
- free(wm);
-
- n = read(p[1], buf, sizeof(buf));
- buf[n - 1] = '\0'; /* replace the newline */
-
- conv = nil;
- for(j = 0; mimetab[j] != nil; j+=2){
- if(strcmp(buf, mimetab[j]) == 0){
- conv = mimetab[j+1];
- break;
- }
- }
- if(conv == nil)
- sysfatal("unknown image format '%s'", buf);
-
- if(fork() == 0){
- close(1);
- dup(p[0], 1);
- if(*conv == '\0')
- execl("/bin/cat", "cat", s->lines[i], nil);
- else{
- sprint(buf, "/bin/%s", conv);
- execl(buf, conv, "-c", s->lines[i], nil);
- }
- }
- cimg = readimage(display, p[1], 0);
+ cimg = openimage(s->lines[i]);
if(cimg == nil)
sysfatal("couldn't convert '%s': %r", s->lines[i]);
waitpid();
- idims = subpt(cimg->r.max, cimg->r.min);
-
- sprint(buf, "/tmp/%s.%d.img", argv0, getpid());
- fd = create(buf, OWRITE, 0600);
- if(fd < 0)
- sysfatal("couldn't open tmp file: %r");
- writeimage(fd, cimg, 0);
- close(fd);
-
- if(fork() == 0){
- close(1);
- dup(p[0], 1);
-
- if(idims.x < idims.y || (idims.x == idims.y && sdims.x > sdims.y)){
- sprint(buf2, "%d", sdims.x);
- execl("/bin/resize", "resize", "-x", buf2, buf, nil);
- }else{
- sprint(buf2, "%d", sdims.y);
- execl("/bin/resize", "resize", "-y", buf2, buf, nil);
- }
- }
- *nextimg = readimage(display, p[1], 0);
+ *nextimg = resizeimage(cimg, sdims);
if(*nextimg == nil)
sysfatal("couldn't scale '%s': %r", s->lines[i]);
+ freeimage(cimg);
nextimg++;
}
-
- close(p[0]);
- close(p[1]);
}
void
freeimages(void){