-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImProcInPlainC.cpp
573 lines (479 loc) · 17.8 KB
/
ImProcInPlainC.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "ImProcInPlainC.h"
#include <conio.h> // for getch
#include <stdio.h> // for old C-Style file operations
#include <stdlib.h> // for exit
void WaitForUserPressKey()
{
printf("Press any key to exit");
_getch();
}
bool RowIsProperlyAligned( int rowSize )
{
if ( ((((rowSize+3)/4)*4)-rowSize) == 0 )
{
return true;
};
return false;
}
// temporary storage
unsigned char localTempColorImage[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS];
unsigned char localTempGrayImage[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
unsigned char localGrayPalette[ myMAXCOLORS*4]; // 4 not 3
// Beware: sizeof is not always what we need because of padding of compiler
tDWord sizeOfmyBITMAPINFOHEADER = 40; // sizeof( myBITMAPINFOHEADER ) = 40;
tDWord sizeOfmyBITMAPFILEHEADER = 14; // sizeof( myBITMAPFILEHEADER ) = 16 != 14;
void FillGrayPalette()
{
for ( int i=0; i< myMAXCOLORS; i++)
{
localGrayPalette[i*4] = (unsigned char) i; // Blue
localGrayPalette[i*4+1] = (unsigned char) i; // Green
localGrayPalette[i*4+2] = (unsigned char) i; // Red
localGrayPalette[i*4+3] = 0; // Reserved
}
}
void ConvertGrayImageToColorImage( unsigned char source[][NUMBER_OF_COLUMNS],
unsigned char destination[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS])
{
for ( int row = 0; row < NUMBER_OF_ROWS; row++)
{
for (int column = 0; column < NUMBER_OF_COLUMNS; column++)
{
destination[row][column][B] = source[row][column];
destination[row][column][G] = source[row][column];
destination[row][column][R] = source[row][column];
}
}
}
void ConvertColorImageToGrayImage( unsigned char source[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],
unsigned char destination[][NUMBER_OF_COLUMNS])
{
int temp;
for ( int row = 0; row < NUMBER_OF_ROWS; row++)
{
for (int column = 0; column < NUMBER_OF_COLUMNS; column++)
{
temp = (int)source[row][column][B]
+ (int)source[row][column][G]
+ (int)source[row][column][R];
destination[row][column] = (unsigned char) (temp /3);
}
}
}
void StoreBgrImageAsTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],
char bmpFileName[] )
{
if ( RowIsProperlyAligned( NUMBER_OF_COLORS*NUMBER_OF_COLUMNS ) == false )
{
printf("Fatal error: Row size %d\n is not properly aligned\n",
NUMBER_OF_COLUMNS*NUMBER_OF_COLORS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
myBITMAPFILEHEADER bf;
bf.bfType = 0;
bf.bfOffBits = 0;
bf.bfSize = 0;
// Beware: sizeof is not always what we need because of padding of compiler
tDWord sizeOfmyBITMAPINFOHEADER = 40; // sizeof( myBITMAPINFOHEADER ) = 40;
tDWord sizeOfmyBITMAPFILEHEADER = 14; // sizeof( myBITMAPFILEHEADER ) = 16 != 14;
bf.bfType = myBMP_TAG;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits = sizeOfmyBITMAPINFOHEADER
+ sizeOfmyBITMAPFILEHEADER
+ 0; // no palette here
bf.bfSize = (tDWord) NUMBER_OF_ROWS
* (tDWord) ( NUMBER_OF_COLUMNS * NUMBER_OF_COLORS ) // no need for alignment here
+ (tDWord) bf.bfOffBits ;
// int A[62];
myBITMAPINFOHEADER bi;
bi.biSize = sizeOfmyBITMAPINFOHEADER; // sizeof( myBITMAPINFOHEADER );
bi.biWidth = NUMBER_OF_COLUMNS;
bi.biHeight = NUMBER_OF_ROWS;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = 0;
bi.biSizeImage = 0; // for RGB images
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
///////////
FILE * bmpFile;
errno_t err;
// Old C syntax was:
// bmpFile = fopen( bmpFileName, "wb" ); mus be replaced to fopen_s
// Do not ask about new syntax: old unclear, unreliable but fast and compact C
err = fopen_s( &bmpFile, bmpFileName, "wb" );
if ( err != 0 ) // old way of error handling
{
printf("Fatal error: Can not open file: \n%s\nfor writing\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
size_t res;
/*
// This does not work here because of size 14 (not multiple of 4) : special compiler switch is needed
res = fwrite( &bf, 1, sizeOfmyBITMAPFILEHEADER, bmpFile );
if ( res != sizeOfmyBITMAPFILEHEADER )
{
report error ...
}
*/
// wrap-out: hard manual work
res = fwrite( &bf.bfType, 1, 2, bmpFile );
if ( res != 2 )
{
fclose( bmpFile );
printf("Fatal error: Can not write myBITMAPFILEHEADER to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// Hope that write will be OK here, no error checks ...
res = fwrite( &bf.bfSize, 1, 4, bmpFile );
res = fwrite( &bf.bfReserved1, 1, 2, bmpFile );
res = fwrite( &bf.bfReserved2, 1, 2, bmpFile );
res = fwrite( &bf.bfOffBits, 1, 4, bmpFile );
// This work because of size 40
res = fwrite( &bi, 1, sizeOfmyBITMAPINFOHEADER, bmpFile );
if ( res != sizeOfmyBITMAPINFOHEADER )
{
fclose( bmpFile );
printf("Fatal error: Can not write myBITMAPINFOHEADER to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// No need for row alignment here: fast work
int rowSize = NUMBER_OF_COLUMNS*NUMBER_OF_COLORS;
int dataSizeInBytes = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS * NUMBER_OF_COLORS;
res = fwrite( image, 1, dataSizeInBytes , bmpFile );
if ( res != (unsigned int) dataSizeInBytes )
{
fclose( bmpFile );
printf("Fatal error: Can not write pixels to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
fclose( bmpFile );
printf("True Color BMP File\n %s\n was created\n", bmpFileName );
}
void StoreBgrImageAsGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],
char bmpFileName[] )
{
// Bad memory efficiency, but fast code development
ConvertColorImageToGrayImage( image,
localTempGrayImage);
StoreGrayImageAsGrayBmpFile( localTempGrayImage, bmpFileName );
}
void StoreGrayImageAsTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],
char bmpFileName[] )
{
// Bad memory efficiency, but fast code development
ConvertGrayImageToColorImage( image,
localTempColorImage);
StoreBgrImageAsTrueColorBmpFile( localTempColorImage, bmpFileName );
}
void StoreGrayImageAsGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],
char bmpFileName[] )
{
if ( RowIsProperlyAligned( NUMBER_OF_COLUMNS ) == false )
{
printf("Fatal error: Row size %d\n is not properly aligned\n",
NUMBER_OF_COLUMNS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
FillGrayPalette();
myBITMAPFILEHEADER bf;
bf.bfType = 0;
bf.bfOffBits = 0;
bf.bfSize = 0;
bf.bfType = myBMP_TAG;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits = sizeOfmyBITMAPINFOHEADER
+ sizeOfmyBITMAPFILEHEADER
+ myMAXCOLORS*4; // Gray Palette present
bf.bfSize = (tDWord) NUMBER_OF_ROWS
* (tDWord) ( NUMBER_OF_COLUMNS ) // no need for alignment here
+ (tDWord) bf.bfOffBits ;
myBITMAPINFOHEADER bi;
bi.biSize = sizeOfmyBITMAPINFOHEADER; // sizeof( myBITMAPINFOHEADER );
bi.biWidth = NUMBER_OF_COLUMNS;
bi.biHeight = NUMBER_OF_ROWS;
bi.biPlanes = 1;
bi.biBitCount = 8;
bi.biCompression = 0;
bi.biSizeImage = NUMBER_OF_ROWS*NUMBER_OF_COLUMNS; // needed here
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
///////////
FILE * bmpFile;
errno_t err;
// Old C syntax was:
// bmpFile = fopen( bmpFileName, "wb" ); mus be replaced to fopen_s
// Do not ask about new syntax: old unclear, unreliable but fast and compact C
err = fopen_s( &bmpFile, bmpFileName, "wb" );
if ( err != 0 ) // old way of error handling
{
printf("Fatal error: Can not open file: \n%s\nfor writing\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
size_t res;
/*
// This does not work here because of size 14 (not multiple of 4) : special compiler switch is needed
res = fwrite( &bf, 1, sizeOfmyBITMAPFILEHEADER, bmpFile );
if ( res != sizeOfmyBITMAPFILEHEADER )
{
report error ...
}
*/
// wrap-out: hard manual work
res = fwrite( &bf.bfType, 1, 2, bmpFile );
if ( res != 2 )
{
fclose( bmpFile );
printf("Fatal error: Can not write myBITMAPFILEHEADER to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// Hope that write will be OK here, no error checks ...
res = fwrite( &bf.bfSize, 1, 4, bmpFile );
res = fwrite( &bf.bfReserved1, 1, 2, bmpFile );
res = fwrite( &bf.bfReserved2, 1, 2, bmpFile );
res = fwrite( &bf.bfOffBits, 1, 4, bmpFile );
// This work because of size 40
res = fwrite( &bi, 1, sizeOfmyBITMAPINFOHEADER, bmpFile );
if ( res != sizeOfmyBITMAPINFOHEADER )
{
fclose( bmpFile );
printf("Fatal error: Can not write myBITMAPINFOHEADER to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// write palette
res = fwrite( localGrayPalette, 1, myMAXCOLORS*4, bmpFile );
if ( res != myMAXCOLORS*4 )
{
fclose( bmpFile );
printf("Fatal error: Can not write myGrayPalette to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// No need for row alignment here: fast work
int rowSize = NUMBER_OF_COLUMNS;
int dataSizeInBytes = NUMBER_OF_ROWS * NUMBER_OF_COLUMNS;
res = fwrite( image, 1, dataSizeInBytes , bmpFile );
if ( res != (unsigned int) dataSizeInBytes )
{
fclose( bmpFile );
printf("Fatal error: Can not write pixels to file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
fclose( bmpFile );
printf("Gray BMP File\n %s\n was created\n", bmpFileName );
}
void LoadBgrImageFromTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],
char bmpFileName[] )
{
FILE * bmpFile;
errno_t err;
// Old C syntax was:
// bmpFile = fopen( bmpFileName, "rb" ); mus be replaced to fopen_s
// Do not ask about new syntax: old unclear, unreliable but fast and compact C
err = fopen_s( &bmpFile, bmpFileName, "rb" );
if ( err != 0 ) // old way of error handling
{
printf("Fatal error: Can not open file: \n%s\nfor reading\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
myBITMAPFILEHEADER bf;
size_t res;
//res = fread( &bf, 1, sizeOfmyBITMAPFILEHEADER, bmpFile ); - this does not work because of 14
res = fread( &bf.bfType, 1, 2, bmpFile );
if ( res != 2 )
{
fclose( bmpFile );
printf("Fatal error: Can not read BITMAPFILEHEADER from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
res = fread( &bf.bfSize, 1, 4, bmpFile );
res = fread( &bf.bfReserved1, 1, 2, bmpFile );
res = fread( &bf.bfReserved2, 1, 2, bmpFile );
res = fread( &bf.bfOffBits, 1, 4, bmpFile );
// Provide some checks:
if ( bf.bfType != myBMP_TAG )
{
fclose( bmpFile );
printf("Fatal error: File: \n%s\n is not BMP file", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
myBITMAPINFOHEADER bi;
res = fread( &bi, 1, sizeOfmyBITMAPINFOHEADER, bmpFile );
if ( res != sizeOfmyBITMAPINFOHEADER )
{
fclose( bmpFile );
printf("Fatal error: Can not read BITMAPINFOHEADER from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( bi.biBitCount!=24 )
{
fclose( bmpFile );
printf("Fatal error: File: \n%s\n is not True Color BMP file", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( RowIsProperlyAligned( bi.biWidth*NUMBER_OF_COLORS ) == false )
{
printf("Fatal error: Row size %d\n is not properly aligned\n",
bi.biWidth*NUMBER_OF_COLORS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( bi.biHeight != NUMBER_OF_ROWS )
{
fclose( bmpFile );
printf("Fatal error: Height of bmp file: \n%s\n is not %d\n", bmpFileName, NUMBER_OF_ROWS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( bi.biWidth != NUMBER_OF_COLUMNS )
{
fclose( bmpFile );
printf("Fatal error: Width of bmp file: \n%s\n is not %d\n", bmpFileName, NUMBER_OF_COLUMNS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// Other values of the header must be checked.
// The problem is that many third party software
// dose not fill all the fields properly
int dataSizeInBytes = bi.biWidth*bi.biHeight*NUMBER_OF_COLORS;
res = fread( image, 1, dataSizeInBytes , bmpFile );
if ( res != (unsigned int) dataSizeInBytes )
{
fclose( bmpFile );
printf("Fatal error: Can not read pixels from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
fclose( bmpFile );
}
void LoadBgrImageFromGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS][NUMBER_OF_COLORS],
char bmpFileName[] )
{
LoadGrayImageFromGrayBmpFile( localTempGrayImage, bmpFileName );
ConvertGrayImageToColorImage( localTempGrayImage, image );
}
void LoadGrayImageFromTrueColorBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],
char bmpFileName[] )
{
LoadBgrImageFromTrueColorBmpFile( localTempColorImage, bmpFileName );
ConvertColorImageToGrayImage( localTempColorImage, image);
}
void LoadGrayImageFromGrayBmpFile( unsigned char image[][NUMBER_OF_COLUMNS],
char bmpFileName[] )
{
FILE * bmpFile;
errno_t err;
// Old C syntax was:
// bmpFile = fopen( bmpFileName, "rb" ); mus be replaced to fopen_s
// Do not ask about new syntax: old unclear, unreliable but fast and compact C
err = fopen_s( &bmpFile, bmpFileName, "rb" );
if ( err != 0 ) // old way of error handling
{
printf("Fatal error: Can not open file: \n%s\nfor reading\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
myBITMAPFILEHEADER bf;
size_t res;
res = fread( &bf.bfType, 1, 2, bmpFile );
//res = fread( &bf, 1, sizeOfmyBITMAPFILEHEADER, bmpFile );
if ( res != 2 )
{
fclose( bmpFile );
printf("Fatal error: Can not read BITMAPFILEHEADER from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
res = fread( &bf.bfSize, 1, 4, bmpFile );
res = fread( &bf.bfReserved1, 1, 2, bmpFile );
res = fread( &bf.bfReserved2, 1, 2, bmpFile );
res = fread( &bf.bfOffBits, 1, 4, bmpFile );
// Provide some checks:
if ( bf.bfType != myBMP_TAG )
{
fclose( bmpFile );
printf("Fatal error: File: \n%s\n is not BMP file", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
myBITMAPINFOHEADER bi;
res = fread( &bi, 1, sizeOfmyBITMAPINFOHEADER, bmpFile );
if ( res != sizeOfmyBITMAPINFOHEADER )
{
fclose( bmpFile );
printf("Fatal error: Can not read BITMAPINFOHEADER from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( bi.biBitCount!=8 )
{
fclose( bmpFile );
printf("Fatal error: File: \n%s\n is not Gray BMP file", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( RowIsProperlyAligned( bi.biWidth ) == false )
{
printf("Fatal error: Row size %d\n is not properly aligned\n",
bi.biWidth );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( bi.biHeight != NUMBER_OF_ROWS )
{
fclose( bmpFile );
printf("Fatal error: Height of bmp file: \n%s\n is not %d\n", bmpFileName, NUMBER_OF_ROWS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
if ( bi.biWidth != NUMBER_OF_COLUMNS )
{
fclose( bmpFile );
printf("Fatal error: Width of bmp file: \n%s\n is not %d\n", bmpFileName, NUMBER_OF_COLUMNS );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// Other values of the header must be checked.
// The problem is that many third party software
// dose not fill all the fields properly
res = fread( localGrayPalette, 1, myMAXCOLORS*4, bmpFile );
if ( res != myMAXCOLORS*4 )
{
fclose( bmpFile );
printf("Fatal error: Can not read PALETTE from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
// Palette must be checked
int dataSizeInBytes = bi.biWidth*bi.biHeight;
res = fread( image, 1, dataSizeInBytes , bmpFile );
if ( res != (unsigned int) dataSizeInBytes )
{
fclose( bmpFile );
printf("Fatal error: Can not read pixels from file: \n%s\n", bmpFileName );
WaitForUserPressKey();
exit(-1); // exit with some error
}
fclose( bmpFile );
}