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 |
|
|
|
18 |
|
|
char buf[1<<20]; /* output buffer */ |
19 |
|
|
|
20 |
|
|
int nrows; /* number of rows output at once */ |
21 |
|
|
|
22 |
|
|
#define scanbar ((COLR *)buf) |
23 |
|
|
|
24 |
|
|
char *progname; |
25 |
|
|
|
26 |
|
|
|
27 |
|
|
main(argc, argv) |
28 |
|
|
int argc; |
29 |
|
|
char *argv[]; |
30 |
|
|
{ |
31 |
|
|
FILE *fin; |
32 |
|
|
|
33 |
|
|
progname = argv[0]; |
34 |
|
|
|
35 |
|
|
if (argc != 2 && argc != 3) { |
36 |
|
|
fprintf(stderr, "Usage: %s infile [outfile]\n", progname); |
37 |
|
|
exit(1); |
38 |
|
|
} |
39 |
|
|
if ((fin = fopen(argv[1], "r")) == NULL) { |
40 |
|
|
fprintf(stderr, "%s: cannot open\n", argv[1]); |
41 |
|
|
exit(1); |
42 |
|
|
} |
43 |
|
|
if (argc == 3 && freopen(argv[2], "w", stdout) == NULL) { |
44 |
|
|
fprintf(stderr, "%s: cannot open\n", argv[2]); |
45 |
|
|
exit(1); |
46 |
|
|
} |
47 |
|
|
/* copy header */ |
48 |
greg |
1.4 |
copyheader(fin, stdout); |
49 |
greg |
1.1 |
/* add new header info. */ |
50 |
|
|
printf("%s\n\n", progname); |
51 |
|
|
/* get picture size */ |
52 |
greg |
1.2 |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
53 |
greg |
1.1 |
fprintf(stderr, "%s: bad picture size\n", progname); |
54 |
|
|
exit(1); |
55 |
|
|
} |
56 |
|
|
/* write new picture size */ |
57 |
greg |
1.2 |
fputresolu(YMAJOR|YDECR, yres, xres, stdout); |
58 |
greg |
1.1 |
/* compute buffer capacity */ |
59 |
|
|
nrows = sizeof(buf)/sizeof(COLR)/yres; |
60 |
|
|
rotate(fin); /* rotate the image */ |
61 |
|
|
exit(0); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
rotate(fp) /* rotate picture */ |
66 |
|
|
FILE *fp; |
67 |
|
|
{ |
68 |
|
|
register COLR *inline; |
69 |
|
|
register int xoff, inx, iny; |
70 |
|
|
long start, ftell(); |
71 |
|
|
|
72 |
|
|
if ((inline = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
73 |
|
|
fprintf(stderr, "%s: out of memory\n", progname); |
74 |
|
|
exit(1); |
75 |
|
|
} |
76 |
|
|
start = ftell(fp); |
77 |
|
|
for (xoff = 0; xoff < xres; xoff += nrows) { |
78 |
|
|
if (fseek(fp, start, 0) < 0) { |
79 |
|
|
fprintf(stderr, "%s: seek error\n", progname); |
80 |
|
|
exit(1); |
81 |
|
|
} |
82 |
|
|
for (iny = yres-1; iny >= 0; iny--) { |
83 |
|
|
if (freadcolrs(inline, xres, fp) < 0) { |
84 |
|
|
fprintf(stderr, "%s: read error\n", progname); |
85 |
|
|
exit(1); |
86 |
|
|
} |
87 |
|
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
88 |
greg |
1.3 |
bcopy((char *)inline[xoff+inx], |
89 |
|
|
(char *)scanbar[inx*yres+iny], |
90 |
greg |
1.1 |
sizeof(COLR)); |
91 |
|
|
} |
92 |
|
|
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
93 |
|
|
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
94 |
|
|
fprintf(stderr, "%s: write error\n", progname); |
95 |
|
|
exit(1); |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
free((char *)inline); |
99 |
|
|
} |