| 1 | 
greg | 
1.1 | 
/* Copyright (c) 1991 Regents of the University of California */ | 
| 2 | 
  | 
  | 
 | 
| 3 | 
  | 
  | 
#ifndef lint | 
| 4 | 
  | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 5 | 
  | 
  | 
#endif | 
| 6 | 
  | 
  | 
 | 
| 7 | 
  | 
  | 
/* | 
| 8 | 
  | 
  | 
 * flip picture file horizontally and/or vertically | 
| 9 | 
  | 
  | 
 */ | 
| 10 | 
  | 
  | 
 | 
| 11 | 
  | 
  | 
#include "standard.h" | 
| 12 | 
  | 
  | 
 | 
| 13 | 
  | 
  | 
#include "color.h" | 
| 14 | 
  | 
  | 
 | 
| 15 | 
  | 
  | 
int     xres, yres;                     /* input resolution */ | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
long    *scanpos;                       /* scanline positions */ | 
| 18 | 
  | 
  | 
 | 
| 19 | 
  | 
  | 
int     fhoriz, fvert;                  /* flip flags */ | 
| 20 | 
  | 
  | 
 | 
| 21 | 
  | 
  | 
FILE    *fin;                           /* input file */ | 
| 22 | 
  | 
  | 
 | 
| 23 | 
  | 
  | 
char    *progname; | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
main(argc, argv) | 
| 27 | 
  | 
  | 
int     argc; | 
| 28 | 
  | 
  | 
char    *argv[]; | 
| 29 | 
  | 
  | 
{ | 
| 30 | 
  | 
  | 
        int     i; | 
| 31 | 
  | 
  | 
 | 
| 32 | 
  | 
  | 
        progname = argv[0]; | 
| 33 | 
  | 
  | 
 | 
| 34 | 
  | 
  | 
        for (i = 1; i < argc; i++) | 
| 35 | 
  | 
  | 
                if (!strcmp(argv[i], "-h")) | 
| 36 | 
  | 
  | 
                        fhoriz++; | 
| 37 | 
  | 
  | 
                else if (!strcmp(argv[i], "-v")) | 
| 38 | 
  | 
  | 
                        fvert++; | 
| 39 | 
  | 
  | 
                else | 
| 40 | 
  | 
  | 
                        break; | 
| 41 | 
  | 
  | 
        if (i >= argc || argv[i][0] == '-') { | 
| 42 | 
  | 
  | 
                fprintf(stderr, "Usage: %s [-h][-v] infile [outfile]\n", | 
| 43 | 
  | 
  | 
                                progname); | 
| 44 | 
  | 
  | 
                exit(1); | 
| 45 | 
  | 
  | 
        } | 
| 46 | 
  | 
  | 
        if ((fin = fopen(argv[i], "r")) == NULL) { | 
| 47 | 
  | 
  | 
                fprintf(stderr, "%s: cannot open\n", argv[1]); | 
| 48 | 
  | 
  | 
                exit(1); | 
| 49 | 
  | 
  | 
        } | 
| 50 | 
  | 
  | 
        if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) { | 
| 51 | 
  | 
  | 
                fprintf(stderr, "%s: cannot open\n", argv[i+1]); | 
| 52 | 
  | 
  | 
                exit(1); | 
| 53 | 
  | 
  | 
        } | 
| 54 | 
  | 
  | 
                                        /* transfer header */ | 
| 55 | 
  | 
  | 
        copyheader(fin, stdout); | 
| 56 | 
  | 
  | 
                                        /* add new header info. */ | 
| 57 | 
greg | 
1.2 | 
        printargs(i, argv, stdout); | 
| 58 | 
greg | 
1.1 | 
        putchar('\n'); | 
| 59 | 
  | 
  | 
                                        /* get picture size */ | 
| 60 | 
  | 
  | 
        if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { | 
| 61 | 
  | 
  | 
                fprintf(stderr, "%s: bad picture size\n", progname); | 
| 62 | 
  | 
  | 
                exit(1); | 
| 63 | 
  | 
  | 
        } | 
| 64 | 
  | 
  | 
                                        /* write new picture size */ | 
| 65 | 
  | 
  | 
        fputresolu(YMAJOR|YDECR, xres, yres, stdout); | 
| 66 | 
  | 
  | 
                                        /* goto end if vertical flip */ | 
| 67 | 
  | 
  | 
        if (fvert) | 
| 68 | 
  | 
  | 
                scanfile(); | 
| 69 | 
  | 
  | 
        flip();                         /* flip the image */ | 
| 70 | 
  | 
  | 
        exit(0); | 
| 71 | 
  | 
  | 
} | 
| 72 | 
  | 
  | 
 | 
| 73 | 
  | 
  | 
 | 
| 74 | 
  | 
  | 
memerr() | 
| 75 | 
  | 
  | 
{ | 
| 76 | 
  | 
  | 
        fprintf(stderr, "%s: out of memory\n", progname); | 
| 77 | 
  | 
  | 
        exit(1); | 
| 78 | 
  | 
  | 
} | 
| 79 | 
  | 
  | 
 | 
| 80 | 
  | 
  | 
 | 
| 81 | 
  | 
  | 
scanfile()                              /* scan to the end of file */ | 
| 82 | 
  | 
  | 
{ | 
| 83 | 
  | 
  | 
        extern long     ftell(); | 
| 84 | 
  | 
  | 
        COLR    *scanin; | 
| 85 | 
  | 
  | 
        int     y; | 
| 86 | 
  | 
  | 
 | 
| 87 | 
  | 
  | 
        if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL) | 
| 88 | 
  | 
  | 
                memerr(); | 
| 89 | 
  | 
  | 
        if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) | 
| 90 | 
  | 
  | 
                memerr(); | 
| 91 | 
  | 
  | 
        for (y = yres-1; y >= 0; y--) { | 
| 92 | 
  | 
  | 
                scanpos[y] = ftell(fin); | 
| 93 | 
  | 
  | 
                if (freadcolrs(scanin, xres, fin) < 0) { | 
| 94 | 
  | 
  | 
                        fprintf(stderr, "%s: read error\n", progname); | 
| 95 | 
  | 
  | 
                        exit(1); | 
| 96 | 
  | 
  | 
                } | 
| 97 | 
  | 
  | 
        } | 
| 98 | 
  | 
  | 
        free((char *)scanin); | 
| 99 | 
  | 
  | 
} | 
| 100 | 
  | 
  | 
 | 
| 101 | 
  | 
  | 
 | 
| 102 | 
  | 
  | 
flip()                                  /* flip the picture */ | 
| 103 | 
  | 
  | 
{ | 
| 104 | 
  | 
  | 
        COLR    *scanin, *scanout; | 
| 105 | 
  | 
  | 
        int     y; | 
| 106 | 
  | 
  | 
        register int    x; | 
| 107 | 
  | 
  | 
 | 
| 108 | 
  | 
  | 
        if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL) | 
| 109 | 
  | 
  | 
                memerr(); | 
| 110 | 
  | 
  | 
        if (fhoriz) { | 
| 111 | 
  | 
  | 
                if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL) | 
| 112 | 
  | 
  | 
                        memerr(); | 
| 113 | 
  | 
  | 
        } else | 
| 114 | 
  | 
  | 
                scanout = scanin; | 
| 115 | 
  | 
  | 
        for (y = yres-1; y >= 0; y--) { | 
| 116 | 
  | 
  | 
                if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) { | 
| 117 | 
  | 
  | 
                        fprintf(stderr, "%s: seek error\n", progname); | 
| 118 | 
  | 
  | 
                        exit(1); | 
| 119 | 
  | 
  | 
                } | 
| 120 | 
  | 
  | 
                if (freadcolrs(scanin, xres, fin) < 0) { | 
| 121 | 
  | 
  | 
                        fprintf(stderr, "%s: read error\n", progname); | 
| 122 | 
  | 
  | 
                        exit(1); | 
| 123 | 
  | 
  | 
                } | 
| 124 | 
  | 
  | 
                if (fhoriz) | 
| 125 | 
  | 
  | 
                        for (x = 0; x < xres; x++) | 
| 126 | 
  | 
  | 
                                copycolr(scanout[x], scanin[xres-1-x]); | 
| 127 | 
  | 
  | 
                if (fwritecolrs(scanout, xres, stdout) < 0) { | 
| 128 | 
  | 
  | 
                        fprintf(stderr, "%s: write error\n", progname); | 
| 129 | 
  | 
  | 
                        exit(1); | 
| 130 | 
  | 
  | 
                } | 
| 131 | 
  | 
  | 
        } | 
| 132 | 
  | 
  | 
        free((char *)scanin); | 
| 133 | 
  | 
  | 
        free((char *)scanout); | 
| 134 | 
  | 
  | 
} |