forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
side.c
executable file
·245 lines (205 loc) · 6.63 KB
/
side.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/***********************************************
*
* file d:\cips\side.c
*
* Functions: This file contains
* main
* print_side_usage
*
* Purpose:
* This file contains the main calling
* routine for a program which
* takes two images and pastes them
* together side by side or top to bottom
* into a new image file.
*
* There are three files: two input files
* (file1 and file2), and one output
* file (file3).
*
* External Calls:
* read_image_array
* write_image_array
* get_image_size
* allocate_image_array
* free_image_array
* create_allocate_tiff_file
* create_allocate_bmp_file
* read_bm_header
* read_bmp_file_header
* read_bm_header
*
* Modifications:
* 19 April 1992 - created
* 13 August 1998 - modified to work on an
* entire image at one time.
* 19 September 1998 - modified to work with
* all I O routines in imageio.c.
*
*************************************************/
#include "cips.h"
int print_side_usage();
int does_not_exist();
int create_image_file();
int get_image_size();
int read_image_array();
int free_image_array();
int write_image_array();
int read_tiff_header();
int create_allocate_tiff_file();
int read_bmp_file_header();
int read_bm_header();
int create_allocate_bmp_file();
int main(argc, argv)
int argc;
char *argv[];
{
char method[80], name1[80], name2[80], name3[80];
int i, j;
long length1, length2, length3,
width1, width2, width3;
short **image1, **image2, **image3;
struct bmpfileheader bmp_file_header;
struct bitmapheader bmheader;
struct tiff_header_struct tiff_file_header;
/******************************************
*
* Interpret the command line parameters.
*
*******************************************/
if(argc != 5){
print_side_usage();
exit(0);
}
strcpy(name1, argv[1]);
strcpy(name2, argv[2]);
strcpy(name3, argv[3]);
strcpy(method, argv[4]);
if(method[0] != 't' &&
method[0] != 'T' &&
method[0] != 's' &&
method[0] != 'S'){
printf("\nERROR: Did not choose a valid method");
print_side_usage();
exit(4);
}
if(does_not_exist(name1)){
printf("\nERROR: Input file %s does not exist",
name1);
print_side_usage();
exit(2);
}
if(does_not_exist(name2)){
printf("\nERROR: Input file %s does not exist",
name2);
print_side_usage();
exit(3);
}
/*******************************************
*
* Look at the sizes of the two input
* files. Ensure they are the correct
* dimensions and set the dimensions
* of the output image.
*
********************************************/
get_image_size(name1, &length1, &width1);
get_image_size(name2, &length2, &width2);
if(method[0] == 'T' || method[0] == 't'){
if(width1 != width2){
printf("\nERROR: input images are not "
"the same width");
exit(4);
} /* ends if widths are unequal */
else{
width3 = width1;
length3 = length1 + length2;
} /* ends else widths are ok */
} /* ends if method is T */
if(method[0] == 'S' || method[0] == 's'){
if(length1 != length2){
printf("\nERROR: input images are not "
"the same length");
exit(4);
} /* ends if lengths are unequal */
else{
width3 = width1 + width2;
length3 = length1;
} /* ends else lengths are ok */
} /* ends if method is S */
/*******************************************
*
* Create the output image to be the same
* type as the first input image.
*
********************************************/
if(is_a_tiff(name1)){
read_tiff_header(name1, &tiff_file_header);
tiff_file_header.image_length = length3;
tiff_file_header.image_width = width3;
create_allocate_tiff_file(name3,
&tiff_file_header);
}
if(is_a_bmp(name1)){
read_bmp_file_header(name1,
&bmp_file_header);
read_bm_header(name1, &bmheader);
bmheader.height = length3;
bmheader.width = width3;
create_allocate_bmp_file(name3,
&bmp_file_header,
&bmheader);
}
/*******************************************
*
* Allocate the image arrays and read the
* two input images.
*
*********************************************/
image1 = allocate_image_array(length1, width1);
image2 = allocate_image_array(length2, width2);
image3 = allocate_image_array(length3, width3);
read_image_array(name1, image1);
read_image_array(name2, image2);
/*******************************************
*
* First do the side by side option.
*
*********************************************/
if(method[0] == 'S' || method[0] == 's'){
for(i=0; i<length1; i++)
for(j=0; j<width1; j++)
image3[i][j] = image1[i][j];
for(i=0; i<length2; i++)
for(j=0; j<width2; j++)
image3[i][j+width1] = image2[i][j];
} /* ends if side-by-side method */
/********************************************
*
* Now do the top to bottom option.
*
*********************************************/
if(method[0] == 'T' || method[0] == 't'){
for(i=0; i<length1; i++)
for(j=0; j<width1; j++)
image3[i][j] = image1[i][j];
for(i=0; i<length2; i++)
for(j=0; j<width2; j++)
image3[i+length1][j] = image2[i][j];
} /* ends top-to-bottom method */
write_image_array(name3, image3);
free_image_array(image1, length1);
free_image_array(image2, length2);
free_image_array(image3, length3);
} /* ends main */
int print_side_usage()
{
printf(
"\n"
"\n usage: side in-file-1 in-file-2 "
"out-file method"
"\n where method is Top-to-bottom "
"or Side-by-side"
"\n");
return(1);
}