-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix2bmp.c
278 lines (240 loc) · 8.06 KB
/
matrix2bmp.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* matrix2bmp.c
*
* This code creates a bitmap image according
* to 3 channel (RGB) matrices.
*
* Last modified on March 01, 2021
*
* by Freeman Sun
*/
#include <stdint.h>
#include <stdio.h>
#include <windows.h>
// Global Variables for BMP Chunks
uint16_t bmp_header = 0x4D42; // Bytes 0-1
uint64_t bmp_total_size; // Bytes 2-9
uint32_t bmp_data_adress = 0x00000036; // Bytes 10-13
uint32_t bmp_CoreHeader_size = 0x00000028; // Bytes 14-17
uint32_t bmp_width; // Bytes 18-21
uint32_t bmp_height; // Bytes 22-25
uint16_t bmp_color_planes = 0x0001; // Bytes 26-27
uint16_t bmp_bit_per_pixel = 0x0018; // Bytes 28-29
uint32_t bmp_compression_method = 0x00000000; // Bytes 30-33
uint32_t bmp_data_size; // Bytes 34-37
uint32_t bmp_horizontal_resolution = 0x00000000; // Bytes 38-41
uint32_t bmp_vertical_resolution = 0x00000000; // Bytes 42-45
uint32_t bmp_num_of_colors_in_palette = 0x00000000; // Bytes 46-49
uint32_t bmp_num_of_important_colors = 0x00000000; // Bytes 50-54
// Other Global Variables
uint32_t width, height;
int mat2bmp(
uint8_t R_m[height][width],
uint8_t G_m[height][width],
uint8_t B_m[height][width],
char* bmpName)
{
// Transfer the data from GB matrices into the BMP file
// Arguments:
// R_m -> RED Channel Matrix
// G_m -> GREEN Channel Matrix
// B_m -> BLUE Channel Matrix
// bmpName -> name for the BMP that will be created
// Set variable values
bmp_width = width;
bmp_height = height;
bmp_data_size = ((width*3)+(width%4))*height;
bmp_total_size = bmp_data_size + bmp_data_adress;
FILE *fPtr;
if( (fPtr = fopen(bmpName, "wb")) == NULL ) {
MessageBox(0, bmpName, "Not opened!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return -1;
}
uint8_t Byte;
uint64_t longInt;
// Print bmp_header
longInt = bmp_header;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 0
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 1
// Print bmp_total_size
longInt = bmp_total_size;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 2
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 3
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 4
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 5
Byte = longInt>>32;
fprintf(fPtr, "%c", Byte); // Byte 6
Byte = longInt>>40;
fprintf(fPtr, "%c", Byte); // Byte 7
Byte = longInt>>48;
fprintf(fPtr, "%c", Byte); // Byte 8
Byte = longInt>>56;
fprintf(fPtr, "%c", Byte); // Byte 9
// Print bmp_data_adress
longInt = bmp_data_adress;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 10
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 11
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 12
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 13
// Print bmp_CoreHeader_size
longInt = bmp_CoreHeader_size;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 14
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 15
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 16
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 17
// Print bmp_width
longInt = bmp_width;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 18
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 19
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 20
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 21
// Print bmp_height
longInt = bmp_height;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 22
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 23
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 24
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 25
// Print bmp_color_planes
longInt = bmp_color_planes;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 26
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 27
// Print bmp_bit_per_pixel
longInt = bmp_bit_per_pixel;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 28
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 29
// Print bmp_compression_method
longInt = bmp_compression_method;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 30
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 31
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 32
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 33
// Print bmp_data_size
longInt = bmp_data_size;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 34
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 35
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 36
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 37
// Print bmp_horizontal_resolution
longInt = bmp_horizontal_resolution;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 38
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 39
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 40
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 41
// Print bmp_vertical_resolution
longInt = bmp_vertical_resolution;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 42
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 43
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 44
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 45
// Print bmp_num_of_colors_in_palette
longInt = bmp_num_of_colors_in_palette;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 46
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 47
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 48
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 49
// Print bmp_num_of_important_colors
longInt = bmp_num_of_important_colors;
Byte = longInt;
fprintf(fPtr, "%c", Byte); // Byte 50
Byte = longInt>>8;
fprintf(fPtr, "%c", Byte); // Byte 51
Byte = longInt>>16;
fprintf(fPtr, "%c", Byte); // Byte 52
Byte = longInt>>24;
fprintf(fPtr, "%c", Byte); // Byte 53
printf("width: %d\n", bmp_width);
printf("height: %d\n", bmp_height);
printf("data adress: %d\n", bmp_data_adress);
printf("data size: %d\n", bmp_data_size);
// Print actual data
uint32_t x,y;
for(y = 1; y <= bmp_height; y++) {
for(x = 0; x < bmp_width; x++) {
fprintf(fPtr, "%c", B_m[bmp_height-y][x]);
fprintf(fPtr, "%c", G_m[bmp_height-y][x]);
fprintf(fPtr, "%c", R_m[bmp_height-y][x]);
}
for(x = 0; x < (bmp_width%4); x++) {
fprintf(fPtr, "%c", 0);
}
}
fclose(fPtr);
return 0;
} // mat2bmp()
int main(int argc, char** argv)
{
width = 13;
height = 7;
uint8_t red[7][13] = {
{255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000},
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000} };
uint8_t green[7][13] = {
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{000, 000, 255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 000},
{000, 000, 255, 255, 255, 255, 255, 255, 000, 000, 255, 255, 000},
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000} };
uint8_t blue[7][13] = {
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000},
{255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 000, 000, 255},
{255, 255, 000, 000, 255, 255, 255, 255, 255, 255, 000, 000, 255},
{000, 000, 255, 255, 000, 000, 255, 255, 000, 000, 255, 255, 000} };
mat2bmp(red, green, blue, argv[1]);
return 0;
} // main()