-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
502 lines (417 loc) · 14.3 KB
/
Source.cpp
File metadata and controls
502 lines (417 loc) · 14.3 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "stdio.h"
#include "stdlib.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <bitset>
#include <sys/types.h>
//userspace std::
using namespace std;
#define CLIP(X) ( (X) > 128 ? 255 : 0)
//#define CLIP(X) ( (X) > 255 ? 255 : (X) < 0 ? 0 : X)
// RGB -> YUV
#define RGB2Y(R, G, B) CLIP(( ( 66 * (R) + 129 * (G) + 25 * (B) + 128) >> 8) + 16)
#pragma pack(push, 1) // 確保結構體按1字節對齊
struct BITMAPFILEHEADER {
uint16_t bfType; // 文件類型,必須是BM(0x4D42)
uint32_t bfSize; // 文件大小,以字節為單位
uint16_t bfReserved1; // 保留,必須為0
uint16_t bfReserved2; // 保留,必須為0
uint32_t bfOffBits; // 位圖數據的偏移量
};
struct BITMAPINFOHEADER {
uint32_t biSize; // 信息頭的大小,以字節為單位
int32_t biWidth; // 圖片的寬度,以像素為單位
int32_t biHeight; // 圖片的高度,以像素為單位
uint16_t biPlanes; // 顏色平面數,必須是1
uint16_t biBitCount; // 每像素的位數
uint32_t biCompression; // 壓縮類型
uint32_t biSizeImage; // 圖片大小,以字節為單位
int32_t biXPelsPerMeter; // 水平分辨率,每米像素數
int32_t biYPelsPerMeter; // 垂直分辨率,每米像素數
uint32_t biClrUsed; // 使用的顏色數
uint32_t biClrImportant; // 重要的顏色數
};
#pragma pack(pop) // 恢復到之前的字節對齊方式
unsigned char * bmp_read(int*, int*, char *);
int bmp_write(unsigned char *image, int, int, char *);
int bmp_write_2bytes(unsigned char *image, int, int, char *);
int bmp_print(unsigned char *image, int, int);
int generate_test_img( char *);
int test_2bytes();
int outputSizeRGB( unsigned char *image, int, int, char * );
// for one bit
int bmp_write_bit( unsigned char *image, int, int, char * );
int main(int argc, char *argv[]) {
unsigned char *image;
int * xsize = (int*)malloc(sizeof(int));
int * ysize = (int*)malloc(sizeof(int));
char imageName[128] = "source";
char outputFileName[128] = "output.txt";
int mode = 0;
if( argc == 4 ) {
//xsize = stoi( argv[1] );
//ysize = stoi( argv[2] );
strcpy( imageName, argv[1] );
strcpy( outputFileName, argv[2] );
mode = stoi( argv[3] );
} else {
printf( "Not enough parameters\n" );
printf( "./BMPtoRGB SourcePicName OutputFileName mode\n" );
printf( "Mode:\n" );
printf( "0 to RGB888, 3bytes\n" );
printf( "1 to RGB565, 2bytes\n" );
printf( "2 to Gray, 1bit per pixel\n" );
printf( "3 to print original RGB to screen\n" );
printf( "4 to print size and RGB to file\n" );
return -1;
} // end if
//image = (unsigned char *)malloc((size_t)xsize * ysize * 3);
//if (image == NULL) {
// printf( "Can't open image %s.bmp\n", imageName );
// return -1;
//} // end if
if( (image = bmp_read( xsize, ysize, imageName )) != NULL) {
//bmp_print( image, xsize, ysize );
switch (mode) {
case 0:
printf( "Output RGB 3bytes\n" );
bmp_write( image, *xsize, *ysize, outputFileName );
break;
case 1:
printf( "Output RGB 2bytes\n" );
bmp_write_2bytes(image, *xsize, *ysize, outputFileName);
break;
case 2:
printf( "Output Gray 1bit per pixel\n" );
bmp_write_bit(image, *xsize, *ysize, outputFileName);
break;
case 3:
printf( "Print RGB, for testing, maybe not correct.\n" );
bmp_print( image, *xsize, *ysize );
break;
case 4:
printf( "Output size and RGB for image verify.\n" );
outputSizeRGB( image, *xsize, *ysize, outputFileName );
break;
default:
printf( "Wrong mode" );
break;
} // end switch
//bmp_write( image, xsize, ysize, "splash.txt" );
//bmp_write_bit(image, xsize, ysize, "bit_72.txt");
} // end if
//generate_test_img("testimg.txt");
/*
for(int y = 0; y != ysize; ++y) {
for(int x = 0; x != xsize; ++x) {
// set (R,G,B) = (255,0,0)
// R
*(image + 3 * (y * xsize + x) + 2) = 255;
// G
*(image + 3 * (y * xsize + x) + 1) = 0;
// B
*(image + 3 * (y * xsize + x) + 0) = 0;
}
}
*/
// bmp_write(image, xsize, ysize, "onlyRed");
free(image);
//system("pause");
}
unsigned char * bmp_read(int * xsize, int * ysize, char *filename) {
char fname_bmp[128];
sprintf_s( fname_bmp, "%s", filename);
FILE *fp;
errno_t err;
err = fopen_s(&fp, fname_bmp, "rb");
if( err != 0 ) {
printf( "Can't open %s\n", fname_bmp );
return NULL;
} // end if
unsigned char header[54];
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
//fread(header, sizeof(unsigned char), 54, fp);
fread(&fileHeader, sizeof(BITMAPFILEHEADER), 1, fp);
fread(&infoHeader, sizeof(BITMAPINFOHEADER), 1, fp);
std::cout << "Test with header: width = " << infoHeader.biWidth << ", height = " << infoHeader.biHeight << std::endl;
*xsize = infoHeader.biWidth;
*ysize = infoHeader.biHeight;
unsigned char * image;
image = (unsigned char *)malloc((size_t)(*xsize) * (*ysize) * 3);
fread(image, sizeof(unsigned char), (size_t)(*xsize) * (*ysize) * 3, fp);
fclose(fp);
return image;
}
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) {
char name[128];
sprintf_s( name, "%s", filename);
FILE *fp;
errno_t err;
err = fopen_s(&fp, name, "w");
if( err != 0 ) {
printf( "Can't open %s\n", name );
return -1;
} // end if
fprintf_s(fp, "Copy and paste the following values to where you want\n");
fprintf_s(fp, "-------------------------------------------------------------------\n");
int w = 0;
for(int y = ysize - 1; y >= 0; --y) {
for(int x = 0; x != xsize; ++x) {
// set (R,G,B) = (255,0,0)
// B
fprintf_s( fp, "0x%02x, ", *(image + 3 * (y * xsize + x) + 0));
// G
fprintf_s( fp, "0x%02x, ", *(image + 3 * (y * xsize + x) + 1));
// R
fprintf_s( fp, "0x%02x, ", *(image + 3 * (y * xsize + x) + 2));
w++;
if( w % 3 == 0 )
fprintf_s( fp, "\n");
} // end inner for
} // end outer for
printf( "Output values to %s DONE\n", name );
fclose(fp);
return 0;
}
int bmp_write_2bytes(unsigned char *image, int xsize, int ysize, char *filename) {
char name[128];
sprintf_s( name, "%s", filename);
FILE *fp;
errno_t err;
err = fopen_s(&fp, name, "w");
if( err != 0 ) {
printf( "Can't open %s\n", name );
return -1;
} // end if
fprintf_s(fp, "Copy and paste the following values to where you want\n");
fprintf_s(fp, "-------------------------------------------------------------------\n");
int w = 0;
for(int y = ysize - 1; y >= 0; --y) {
for(int x = 0; x != xsize; ++x) {
unsigned char byteH = 0x00;
unsigned char byteL = 0x00;
unsigned char byteR = 0x00;
unsigned char byteG = 0x00;
unsigned char byteB = 0x00;
byteB = (*(image + 3 * (y * xsize + x) + 2) + 1) / 8;
if( byteB != 0 ) byteB -= 1;
// printf( "%d\n", (*(image + 3 * (y * xsize + x) + 2) + 1) / 8);
byteG = (*(image + 3 * (y * xsize + x) + 1) + 1) / 4;
if( byteG != 0 ) byteG -= 1;
byteR = (*(image + 3 * (y * xsize + x) + 0) + 1) / 8;
if( byteR != 0 ) byteR -= 1;
// printf("R=%d, G=%d, B=%d\n ", byteR, byteG, byteB);
byteH |= (byteG >> 3);
byteH |= (byteB << 3);
byteL |= (byteR);
byteL |= (byteG << 5);
// B
//printf("0x%02x, ", *(image + 3 * (y * xsize + x) + 0));
// G
//printf("0x%02x, ", *(image + 3 * (y * xsize + x) + 1));
// R
//printf("0x%02x, ", *(image + 3 * (y * xsize + x) + 2));
fprintf_s( fp, "0x%02x, 0x%02x, ", byteH, byteL);
//printf( "[0x%02x, 0x%02x]\n", byteH, byteL );
w++;
if( w % 3 == 0 )
fprintf_s( fp, "\n");
}
}
printf( "Output values to %s DONE\n", name );
fclose(fp);
return 0;
} // end function bmp_write_2bytes
int bmp_print(unsigned char *image, int xsize, int ysize) {
int w = 0;
for(int y = ysize - 1; y >= 0; --y) {
for(int x = 0; x != xsize; ++x) {
//for(int y = 0; y < 5; ++y) {
//for(int x = 0; x != 5; ++x) {
unsigned char byteH = 0x00;
unsigned char byteL = 0x00;
unsigned char byteR = 0x00;
unsigned char byteG = 0x00;
unsigned char byteB = 0x00;
byteR = (*(image + 3 * (y * xsize + x) + 2) + 1) / 8 - 1;
byteG = (*(image + 3 * (y * xsize + x) + 1) + 1) / 4 - 1;
byteB = (*(image + 3 * (y * xsize + x) + 0) + 1) / 8 - 1;
printf("R=%d, G=%d, B=%d\n ", byteR, byteG, byteB);
byteH |= (byteG >> 3);
byteH |= (byteB << 3);
byteL |= (byteR);
byteL |= (byteG << 2);
// B
printf("0x%02x, ", *(image + 3 * (y * xsize + x) + 0));
// G
printf("0x%02x, ", *(image + 3 * (y * xsize + x) + 1));
// R
printf("0x%02x, ", *(image + 3 * (y * xsize + x) + 2));
printf( "[0x%02x, 0x%02x]\n", byteH, byteL );
// set (R,G,B) = (255,0,0)
// B
//printf("0x%x, ", *(image + 3 * (y * xsize + x) + 0));
// G
//printf("0x%x, ", *(image + 3 * (y * xsize + x) + 1));
// R
//printf("0x%x, ", *(image + 3 * (y * xsize + x) + 2));
w++;
if( w >= 3 )
printf("\n");
}
}
return 0;
}
int generate_test_img( char *filename) {
char name[128];
sprintf_s( name, "%s", filename);
FILE *fp;
errno_t err;
err = fopen_s(&fp, name, "w");
if( err != 0 ) {
printf( "Can't open %s\n", name );
return -1;
} // end if
fprintf_s(fp, "Copy and paste the following values to where you want\n");
fprintf_s(fp, "-------------------------------------------------------------------\n");
int w = 0;
for(int y = 250 - 1; y >= 0; --y) {
for(int x = 0; x != 250; ++x) {
// set (R,G,B) = (255,0,0)
if( y >= 0 && y <= 100 ) {
// B
fprintf_s( fp, "0x%02x, ", 0);
// G
fprintf_s( fp, "0x%02x, ", 0);
// R
fprintf_s( fp, "0x%02x, ", 255);
} else if( y > 100 && y < 200 ) {
// B
fprintf_s( fp, "0x%02x, ", 255);
// G
fprintf_s( fp, "0x%02x, ", 0);
// R
fprintf_s( fp, "0x%02x, ", 0);
} else {
// B
fprintf_s( fp, "0x%02x, ", 0);
// G
fprintf_s( fp, "0x%02x, ", 255);
// R
fprintf_s( fp, "0x%02x, ", 0);
} // end if
w++;
if( w % 3 == 0 )
fprintf_s( fp, "\n");
}
}
printf( "Output values to %s DONE\n", name );
fclose(fp);
return 0;
} // end function generate_test_img
int test_2bytes(){
unsigned char byteH = 0x00;
unsigned char byteL = 0x00;
unsigned char byteR = 0x00;
unsigned char byteG = 0x00;
unsigned char byteB = 0x00;
//byteR = / 8;
return 0;
} // end function test_2bytes()
int bmp_write_bit( unsigned char *image, int xsize, int ysize, char * filename ) {
char name[128];
sprintf_s( name, "%s", filename);
FILE *fp;
errno_t err;
err = fopen_s(&fp, name, "w");
if( err != 0 ) {
printf( "Can't open %s\n", name );
return -1;
} // end if
fprintf_s(fp, "Copy and paste the following values to your file\n");
fprintf_s(fp, "-------------------------------------------------------------------\n");
int w = 1;
unsigned char value = 0;
int debug_count = 0;
int frameBufferCounter = 0;
int shift_count = 0;
unsigned char mask1 = 1, mask0 = 0;
for(int y = ysize - 1; y >= 0; --y) {
for(int x = 0; x < xsize; ++x) {
unsigned char byteY = RGB2Y(*(image + 3 * (y * xsize + x) + 2),
*(image + 3 * (y * xsize + x) + 1),
*(image + 3 * (y * xsize + x) + 0));
//frameBuffer[ /*x + y * s*/frameBufferCounter ] =
// bit operaction
// unsigned char byteY = *(image + (y * xsize + x));
printf("%d,", *(image + 3 * (y * xsize + x)));
if( byteY > 128 ) {
// 1
value = value | mask1;
} else {
// value = value | mask0;
} // end if
shift_count++;
if( shift_count >= 8 ) {
shift_count = 0;
//mask1 = 1;
debug_count++;
// frameBuffer[ /*x + y * s*/frameBufferCounter++ ] = value;
fprintf_s( fp, "0x%02x, ", value);
value = 0;
w++;
} else {
value = value << 1;
} // end if
/*
if( y==0 ) {
printf("%d, %d, %d\n", *(image + 3 * (y * xsize + x) + 2),
*(image + 3 * (y * xsize + x) + 1),
*(image + 3 * (y * xsize + x) + 0));
}
*/
if( w % 8 == 0 ) {
fprintf_s( fp, "\n");
printf("\n");
w = 1;
}
}
}
printf( "Output values to %s DONE\n", name );
fclose(fp);
return 0;
} // end function bmp_write_bit
int outputSizeRGB(unsigned char *image, int xsize, int ysize, char *filename) {
char name[128];
sprintf_s( name, "%s", filename);
FILE *fp;
errno_t err;
err = fopen_s(&fp, name, "w");
if( err != 0 ) {
printf( "Can't open %s\n", name );
return -1;
} // end if
printf( "Start to output to file %d %d\n", xsize, ysize );
//fprintf_s(fp, "Copy and paste the following values to where you want\n");
//fprintf_s(fp, "-------------------------------------------------------------------\n");
fprintf_s(fp, "%d %d\n", xsize, ysize);
int w = 0;
for(int y = ysize - 1; y >= 0; --y) {
for(int x = 0; x != xsize; ++x) {
// set (R,G,B) = (255,0,0)
// B
fprintf_s( fp, "%02x ", *(image + 3 * (y * xsize + x) + 0));
// G
fprintf_s( fp, "%02x ", *(image + 3 * (y * xsize + x) + 1));
// R
fprintf_s( fp, "%02x ", *(image + 3 * (y * xsize + x) + 2));
fprintf_s( fp, "\n");
} // end inner for
} // end outer for
printf( "Output values to %s DONE\n", name );
fclose(fp);
return 0;
}