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