forked from wzpan/2dMorphing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.cpp
181 lines (157 loc) · 4.28 KB
/
merge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/******************************************************************/
/*Merging three channel's BW format images into one RGB color image */
/*File name: merge.c */
/*Author: Yurong Sun, Joseph Pan */
/*Description: */
/* Read greyscale BW format image file */
/* Create color SGI format image file */
/******************************************************************/
#include <iostream>
using namespace std;
#define IXSIZE 400
#define IYSIZE 400
#define IZSIZE 3
void putbyte(FILE *outf, unsigned char val)
{
unsigned char buf[1];
buf[0] = val;
fwrite(buf,1,1,outf);
}
void putshort(FILE *outf, unsigned short val)
{
unsigned char buf[2];
buf[0] = (val>>8);
buf[1] = (val>>0);
fwrite(buf,2,1,outf);
}
static int putlong(FILE *outf, unsigned long val)
{
unsigned char buf[4];
buf[0] = (val>>24);
buf[1] = (val>>16);
buf[2] = (val>>8);
buf[3] = (val>>0);
return fwrite(buf,4,1,outf);
}
unsigned short getshort(FILE *inf)
{
unsigned char buf[2];
fread(buf, 2, 1, inf);
return (buf[0]<<8) + (buf[1]<<0);
}
static long getlong(FILE *inf)
{
unsigned char buf[4];
fread(buf, 4, 1, inf);
return (buf[0]<<24) + (buf[1]<<16)+ (buf[2]<<8) + (buf[3]<<0);
}
void main(int argc, char **argv)
{
FILE *of, *inf;
char iname[80];
int i, x, y,z;
unsigned short ixsi, iysi, izsi;//, ixsi1, iysi1;
unsigned char inbuf[IXSIZE][IYSIZE][IZSIZE];
// make sure the user invokes this program properly
if(argc != 5) {
fprintf(stderr, "Usage: merge red.bw green.bw blue.bw out.rgb\n");
exit(1);
}
/* Read the input BW files*/
// red channel
if ((inf = fopen(argv[1], "rb"))==NULL)
{
fprintf(stderr, "readImage: Can't open file %s\n", argv[1]);
exit(1);
}
else
{
//ixsi1 = getshort(inf);
ixsi = getshort(inf); // width
//iysi1 = getshort(inf); // height
iysi = getshort(inf);
for (y=0; y < iysi; y++)
{
for (x=0; x < ixsi; x++)
{
fread(&inbuf[x][y][0], 1, 1, inf);
}
}
}
fclose(inf);
// green channel
if ((inf = fopen(argv[2], "rb"))==NULL)
{
fprintf(stderr, "readImage: Can't open file %s\n", argv[1]);
exit(1);
}
else
{
//ixsi1 = getshort(inf);
ixsi = getshort(inf);
//iysi1 = getshort(inf);
iysi = getshort(inf);
for (y=0; y < iysi; y++)
{
for (x=0; x < ixsi; x++)
{
fread(&inbuf[x][y][1], 1, 1, inf);
}
}
}
fclose(inf);
// blue channel
if ((inf=fopen(argv[3], "rb"))==NULL)
{
fprintf(stderr, "readImage: Can't open file %s\n", argv[1]);
exit(1);
}
else
{
//ixsi1 = getshort(inf);
ixsi = getshort(inf);
//iysi1 = getshort(inf);
iysi = getshort(inf);
for (y=0; y < iysi; y++)
{
for (x=0; x < ixsi; x++)
{
fread(&inbuf[x][y][2], 1, 1, inf);
}
}
}
fclose(inf);
/*Begin to write output files */
of = fopen(argv[4],"wb");
if(!of) {
fprintf(stderr,"sgiimage: can't open output file\n");
exit(1);
}
putshort(of,474); /* MAGIC */
putbyte(of,0); /* STORAGE is VERBATIM */
putbyte(of,1); /* BPC is 1 */
putshort(of,2); /* DIMENSION is 2 */
putshort(of,ixsi); /* XSIZE */
putshort(of,iysi); /* YSIZE */
putshort(of,3); /* ZSIZE */
putlong(of,0); /* PIXMIN is 0 */
putlong(of,255); /* PIXMAX is 255 */
for(i=0; i<4; i++) /* DUMMY 4 bytes */
putbyte(of,0);
strcpy(iname,"No Name");
fwrite(iname,80,1,of); /* IMAGENAME */
putlong(of,0); /* COLORMAP is 0 */
for(i=0; i<404; i++) /* DUMMY 404 bytes */
putbyte(of,0);
for (z=0; z < 3; z++)
{
for(y=0; y<iysi; y++)
{
for(x=0; x<ixsi; x++)
{
fwrite(&inbuf[x][y][z],1,1,of);
}
}
}
fclose(of);
}