ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/swaprasheader.c
Revision: 1.2
Committed: Tue Mar 26 13:02:21 1991 UTC (33 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
minor bug fixes for system compatibility

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 #include <stdio.h>
8 #include "rasterfile.h"
9
10 main(argc, argv)
11 int argc;
12 char *argv[];
13 {
14 int i;
15
16 for (i = 1; i < argc; i++)
17 fixfile(argv[i]);
18
19 if (argc == 1)
20 fixstream(stdin, stdout);
21
22 exit(0);
23 }
24
25
26 fixfile(name)
27 char *name;
28 {
29 struct rasterfile head;
30 FILE *fp;
31
32 if ((fp = fopen(name, "r+")) == NULL) {
33 fprintf(stderr, "%s: cannon open for read/write\n", name);
34 exit(1);
35 }
36 if (fread((char *)&head, sizeof(head), 1, fp) != 1) {
37 fprintf(stderr, "%s: read error\n", name);
38 exit(1);
39 }
40 swapbytes((char *)&head, sizeof(head), sizeof(int));
41 if (fseek(fp, 0L, 0) == -1) {
42 fprintf(stderr, "%s: seek error\n", name);
43 exit(1);
44 }
45 if (fwrite((char *)&head, sizeof(head), 1, fp) != 1
46 || fclose(fp) == -1) {
47 fprintf(stderr, "%s: write error\n", name);
48 exit(1);
49 }
50 }
51
52
53 fixstream(in, out)
54 register FILE *in, *out;
55 {
56 struct rasterfile head;
57 register int c;
58
59 if (fread((char *)&head, sizeof(head), 1, in) != 1) {
60 fprintf(stderr, "read error\n");
61 exit(1);
62 }
63 swapbytes((char *)&head, sizeof(head), sizeof(int));
64 fwrite((char *)&head, sizeof(head), 1, out);
65 while ((c = getc(in)) != EOF)
66 putc(c, out);
67 if (ferror(out)) {
68 fprintf(stderr, "write error\n");
69 exit(1);
70 }
71 }
72
73
74 swapbytes(top, size, width)
75 char *top;
76 int size, width;
77 {
78 register char *b, *e;
79 register int c;
80
81 for ( ; size > 0; size -= width, top += width) {
82 b = top; e = top+width-1;
83 while (b < e) {
84 c = *e;
85 *e-- = *b;
86 *b++ = c;
87 }
88 }
89 }