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 |
while (fgets(buf, sizeof(buf), fin) != NULL && buf[0] != '\n') |
49 |
fputs(buf, stdout); |
50 |
/* add new header info. */ |
51 |
printf("%s\n\n", progname); |
52 |
/* get picture size */ |
53 |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
54 |
fprintf(stderr, "%s: bad picture size\n", progname); |
55 |
exit(1); |
56 |
} |
57 |
/* write new picture size */ |
58 |
fputresolu(YMAJOR|YDECR, yres, xres, stdout); |
59 |
/* compute buffer capacity */ |
60 |
nrows = sizeof(buf)/sizeof(COLR)/yres; |
61 |
rotate(fin); /* rotate the image */ |
62 |
exit(0); |
63 |
} |
64 |
|
65 |
|
66 |
rotate(fp) /* rotate picture */ |
67 |
FILE *fp; |
68 |
{ |
69 |
register COLR *inline; |
70 |
register int xoff, inx, iny; |
71 |
long start, ftell(); |
72 |
|
73 |
if ((inline = (COLR *)malloc(xres*sizeof(COLR))) == NULL) { |
74 |
fprintf(stderr, "%s: out of memory\n", progname); |
75 |
exit(1); |
76 |
} |
77 |
start = ftell(fp); |
78 |
for (xoff = 0; xoff < xres; xoff += nrows) { |
79 |
if (fseek(fp, start, 0) < 0) { |
80 |
fprintf(stderr, "%s: seek error\n", progname); |
81 |
exit(1); |
82 |
} |
83 |
for (iny = yres-1; iny >= 0; iny--) { |
84 |
if (freadcolrs(inline, xres, fp) < 0) { |
85 |
fprintf(stderr, "%s: read error\n", progname); |
86 |
exit(1); |
87 |
} |
88 |
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
89 |
bcopy((char *)inline[xoff+inx], |
90 |
(char *)scanbar[inx*yres+iny], |
91 |
sizeof(COLR)); |
92 |
} |
93 |
for (inx = 0; inx < nrows && xoff+inx < xres; inx++) |
94 |
if (fwritecolrs(scanbar+inx*yres, yres, stdout) < 0) { |
95 |
fprintf(stderr, "%s: write error\n", progname); |
96 |
exit(1); |
97 |
} |
98 |
} |
99 |
free((char *)inline); |
100 |
} |