1 |
greg |
1.1 |
/* Copyright (c) 1988 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
/* |
7 |
|
|
* prot.c - program to rotate picture file 90 degrees clockwise. |
8 |
|
|
* |
9 |
|
|
* 2/26/88 |
10 |
|
|
*/ |
11 |
|
|
|
12 |
|
|
#include <stdio.h> |
13 |
|
|
|
14 |
|
|
#include "color.h" |
15 |
|
|
|
16 |
|
|
int xres, yres; /* input resolution */ |
17 |
greg |
1.6 |
double inpaspect = 1.0; /* input aspect ratio */ |
18 |
greg |
1.1 |
|
19 |
|
|
char buf[1<<20]; /* output buffer */ |
20 |
|
|
|
21 |
|
|
int nrows; /* number of rows output at once */ |
22 |
|
|
|
23 |
|
|
#define scanbar ((COLR *)buf) |
24 |
|
|
|
25 |
|
|
char *progname; |
26 |
|
|
|
27 |
|
|
|
28 |
greg |
1.6 |
headline(s) /* process line from header */ |
29 |
|
|
char *s; |
30 |
|
|
{ |
31 |
|
|
fputs(s, stdout); |
32 |
|
|
if (isaspect(s)) |
33 |
|
|
inpaspect *= aspectval(s); |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
|
37 |
greg |
1.1 |
main(argc, argv) |
38 |
|
|
int argc; |
39 |
|
|
char *argv[]; |
40 |
|
|
{ |
41 |
|
|
FILE *fin; |
42 |
|
|
|
43 |
|
|
progname = argv[0]; |
44 |
|
|
|
45 |
|
|
if (argc != 2 && argc != 3) { |
46 |
|
|
fprintf(stderr, "Usage: %s infile [outfile]\n", progname); |
47 |
|
|
exit(1); |
48 |
|
|
} |
49 |
|
|
if ((fin = fopen(argv[1], "r")) == NULL) { |
50 |
|
|
fprintf(stderr, "%s: cannot open\n", argv[1]); |
51 |
|
|
exit(1); |
52 |
|
|
} |
53 |
|
|
if (argc == 3 && freopen(argv[2], "w", stdout) == NULL) { |
54 |
|
|
fprintf(stderr, "%s: cannot open\n", argv[2]); |
55 |
|
|
exit(1); |
56 |
|
|
} |
57 |
greg |
1.6 |
/* transfer header */ |
58 |
|
|
getheader(fin, headline); |
59 |
greg |
1.1 |
/* add new header info. */ |
60 |
greg |
1.6 |
if (inpaspect < .99 || inpaspect > 1.01) |
61 |
|
|
fputaspect(1./inpaspect/inpaspect, stdout); |
62 |
greg |
1.1 |
printf("%s\n\n", progname); |
63 |
|
|
/* get picture size */ |
64 |
greg |
1.2 |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
65 |
greg |
1.1 |
fprintf(stderr, "%s: bad picture size\n", progname); |
66 |
|
|
exit(1); |
67 |
|
|
} |
68 |
|
|
/* write new picture size */ |
69 |
greg |
1.2 |
fputresolu(YMAJOR|YDECR, yres, xres, stdout); |
70 |
greg |
1.1 |
/* compute buffer capacity */ |
71 |
|
|
nrows = sizeof(buf)/sizeof(COLR)/yres; |
72 |
|
|
rotate(fin); /* rotate the image */ |
73 |
|
|
exit(0); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
rotate(fp) /* rotate picture */ |
78 |
|
|
FILE *fp; |
79 |
|
|
{ |
80 |
greg |
1.5 |
register COLR *inln; |
81 |
greg |
1.1 |
register int xoff, inx, iny; |
82 |
|
|
long start, ftell(); |
83 |
|
|
|
84 |
greg |
1.5 |
if ((inln = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
85 |
greg |
1.1 |
fprintf(stderr, "%s: out of memory\n", progname); |
86 |
|
|
exit(1); |
87 |
|
|
} |
88 |
|
|
start = ftell(fp); |
89 |
|
|
for (xoff = 0; xoff < xres; xoff += nrows) { |
90 |
|
|
if (fseek(fp, start, 0) < 0) { |
91 |
|
|
fprintf(stderr, "%s: seek error\n", progname); |
92 |
|
|
exit(1); |
93 |
|
|
} |
94 |
|
|
for (iny = yres-1; iny >= 0; iny--) { |
95 |
greg |
1.5 |
if (freadcolrs(inln, xres, fp) < 0) { |
96 |
greg |
1.1 |
fprintf(stderr, "%s: read error\n", progname); |
97 |
|
|
exit(1); |
98 |
|
|
} |
99 |
|
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
100 |
greg |
1.5 |
bcopy((char *)inln[xoff+inx], |
101 |
greg |
1.3 |
(char *)scanbar[inx*yres+iny], |
102 |
greg |
1.1 |
sizeof(COLR)); |
103 |
|
|
} |
104 |
|
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
105 |
|
|
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
106 |
|
|
fprintf(stderr, "%s: write error\n", progname); |
107 |
|
|
exit(1); |
108 |
|
|
} |
109 |
|
|
} |
110 |
greg |
1.5 |
free((char *)inln); |
111 |
greg |
1.1 |
} |