| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: pflip.c,v 2.10 2012/05/17 17:36:14 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* flip picture file horizontally and/or vertically
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include <stdio.h>
|
| 9 |
#include <time.h>
|
| 10 |
#include <string.h>
|
| 11 |
|
| 12 |
#include "platform.h"
|
| 13 |
#include "color.h"
|
| 14 |
#include "resolu.h"
|
| 15 |
|
| 16 |
int order; /* input orientation */
|
| 17 |
int xres, yres; /* resolution (scanlen, nscans) */
|
| 18 |
|
| 19 |
long *scanpos; /* scanline positions */
|
| 20 |
|
| 21 |
int fhoriz=0, fvert=0; /* flip flags */
|
| 22 |
|
| 23 |
int correctorder = 0; /* correcting orientation? */
|
| 24 |
|
| 25 |
FILE *fin; /* input file */
|
| 26 |
|
| 27 |
char *progname;
|
| 28 |
|
| 29 |
|
| 30 |
static void memerr(void);
|
| 31 |
static void scanfile(void);
|
| 32 |
static void flip(void);
|
| 33 |
|
| 34 |
|
| 35 |
static int
|
| 36 |
neworder(void) /* figure out new order from old */
|
| 37 |
{
|
| 38 |
register int no;
|
| 39 |
|
| 40 |
if (correctorder)
|
| 41 |
return(order); /* just leave it */
|
| 42 |
if ((no = order) & YMAJOR) {
|
| 43 |
if (fhoriz) no ^= XDECR;
|
| 44 |
if (fvert) no ^= YDECR;
|
| 45 |
} else {
|
| 46 |
if (fhoriz) no ^= YDECR;
|
| 47 |
if (fvert) no ^= XDECR;
|
| 48 |
}
|
| 49 |
return(no);
|
| 50 |
}
|
| 51 |
|
| 52 |
int
|
| 53 |
main(
|
| 54 |
int argc,
|
| 55 |
char *argv[]
|
| 56 |
)
|
| 57 |
{
|
| 58 |
static char picfmt[MAXFMTLEN] = PICFMT;
|
| 59 |
int i, rval;
|
| 60 |
SET_DEFAULT_BINARY();
|
| 61 |
SET_FILE_BINARY(stdout);
|
| 62 |
progname = argv[0];
|
| 63 |
|
| 64 |
for (i = 1; i < argc; i++)
|
| 65 |
if (!strcmp(argv[i], "-h"))
|
| 66 |
fhoriz++;
|
| 67 |
else if (!strcmp(argv[i], "-v"))
|
| 68 |
fvert++;
|
| 69 |
else if (!strcmp(argv[i], "-c"))
|
| 70 |
correctorder++;
|
| 71 |
else
|
| 72 |
break;
|
| 73 |
if (i < argc-2)
|
| 74 |
goto userr;
|
| 75 |
if (!fhoriz && !fvert)
|
| 76 |
fprintf(stderr, "%s: warning - no operation\n", argv[0]);
|
| 77 |
if (i >= argc || argv[i][0] == '-') {
|
| 78 |
if (fvert)
|
| 79 |
goto userr;
|
| 80 |
SET_FILE_BINARY(stdin);
|
| 81 |
fin = stdin;
|
| 82 |
} else if ((fin = fopen(argv[i], "r")) == NULL) {
|
| 83 |
fprintf(stderr, "%s: cannot open\n", argv[i]);
|
| 84 |
exit(1);
|
| 85 |
}
|
| 86 |
if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) {
|
| 87 |
fprintf(stderr, "%s: cannot open\n", argv[i+1]);
|
| 88 |
exit(1);
|
| 89 |
}
|
| 90 |
/* transfer header */
|
| 91 |
if ((rval = checkheader(fin, picfmt, stdout)) < 0) {
|
| 92 |
fprintf(stderr, "%s: input not a Radiance picture\n",
|
| 93 |
progname);
|
| 94 |
exit(1);
|
| 95 |
}
|
| 96 |
if (rval)
|
| 97 |
fputformat(picfmt, stdout);
|
| 98 |
/* add new header info. */
|
| 99 |
printargs(i, argv, stdout);
|
| 100 |
putchar('\n');
|
| 101 |
/* get picture size */
|
| 102 |
if ((order = fgetresolu(&xres, &yres, fin)) < 0) {
|
| 103 |
fprintf(stderr, "%s: bad picture size\n", progname);
|
| 104 |
exit(1);
|
| 105 |
}
|
| 106 |
/* write new picture size */
|
| 107 |
fputresolu(neworder(), xres, yres, stdout);
|
| 108 |
/* goto end if vertical flip */
|
| 109 |
if (fvert)
|
| 110 |
scanfile();
|
| 111 |
flip(); /* flip the image */
|
| 112 |
exit(0);
|
| 113 |
userr:
|
| 114 |
fprintf(stderr, "Usage: %s [-h][-v][-c] infile [outfile]\n",
|
| 115 |
progname);
|
| 116 |
exit(1);
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
static void
|
| 121 |
memerr(void)
|
| 122 |
{
|
| 123 |
fprintf(stderr, "%s: out of memory\n", progname);
|
| 124 |
exit(1);
|
| 125 |
}
|
| 126 |
|
| 127 |
|
| 128 |
static void
|
| 129 |
scanfile(void) /* scan to the end of file */
|
| 130 |
{
|
| 131 |
extern long ftell();
|
| 132 |
COLR *scanin;
|
| 133 |
int y;
|
| 134 |
|
| 135 |
if ((scanpos = (long *)malloc(yres*sizeof(long))) == NULL)
|
| 136 |
memerr();
|
| 137 |
if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
|
| 138 |
memerr();
|
| 139 |
for (y = yres-1; y > 0; y--) {
|
| 140 |
scanpos[y] = ftell(fin);
|
| 141 |
if (freadcolrs(scanin, xres, fin) < 0) {
|
| 142 |
fprintf(stderr, "%s: read error\n", progname);
|
| 143 |
exit(1);
|
| 144 |
}
|
| 145 |
}
|
| 146 |
scanpos[0] = ftell(fin);
|
| 147 |
free((void *)scanin);
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
static void
|
| 152 |
flip(void) /* flip the picture */
|
| 153 |
{
|
| 154 |
COLR *scanin, *scanout;
|
| 155 |
int y;
|
| 156 |
register int x;
|
| 157 |
|
| 158 |
if ((scanin = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
|
| 159 |
memerr();
|
| 160 |
if (fhoriz) {
|
| 161 |
if ((scanout = (COLR *)malloc(xres*sizeof(COLR))) == NULL)
|
| 162 |
memerr();
|
| 163 |
} else
|
| 164 |
scanout = scanin;
|
| 165 |
for (y = yres-1; y >= 0; y--) {
|
| 166 |
if (fvert && fseek(fin, scanpos[yres-1-y], 0) == EOF) {
|
| 167 |
fprintf(stderr, "%s: seek error\n", progname);
|
| 168 |
exit(1);
|
| 169 |
}
|
| 170 |
if (freadcolrs(scanin, xres, fin) < 0) {
|
| 171 |
fprintf(stderr, "%s: read error\n", progname);
|
| 172 |
exit(1);
|
| 173 |
}
|
| 174 |
if (fhoriz)
|
| 175 |
for (x = 0; x < xres; x++)
|
| 176 |
copycolr(scanout[x], scanin[xres-1-x]);
|
| 177 |
if (fwritecolrs(scanout, xres, stdout) < 0) {
|
| 178 |
fprintf(stderr, "%s: write error\n", progname);
|
| 179 |
exit(1);
|
| 180 |
}
|
| 181 |
}
|
| 182 |
free((void *)scanin);
|
| 183 |
if (fhoriz)
|
| 184 |
free((void *)scanout);
|
| 185 |
}
|