-
Notifications
You must be signed in to change notification settings - Fork 1
/
asciiImage.c
958 lines (746 loc) · 29.1 KB
/
asciiImage.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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
/* asciiImage.c */
/*************************************************************************/
/* */
/* AUTHOR: */
/* Luka Krajger */
/* */
/* NAME: */
/* asciiImage */
/* */
/* DESCRIPTION: */
/* This program prints 24-bit .bmp image in ascii character format */
/* */
/* COMPILATION: */
/* /$ gcc -Wall -o asciiImage asciiImage.c -O2 -lm */
/* ( on Windows WINDOWS constant must be defined ) */
/* */
/*************************************************************************/
/*************************************************************************/
/* INCLUDES */
/*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h> /* Use -lm comipialtion flag */
/*************************************************************************/
/* MACROS AND CONSTANTS */
/*************************************************************************/
#define OK 1
#define ERROR (-1)
#define IMAGE_NAME_LEN 127
/* For reading binary files */
#ifdef WINDOWS
#define READ_BINARY_FILE "rb"
#else
#define READ_BINARY_FILE "r"
#endif
/* Bmp file related */
#define BMP_HEADER_SIZE 54
#define BMP_H_FILE_SIZE 0x02
#define BMP_H_OFFSET 0x0A
#define BMP_H_WIDTH 0x12
#define BMP_H_HEIGHT 0x16
#define BMP_H_RAW_SIZE 0x22
/* Html file related */
#define HTML_F_FAMILY "font-family: Courier, 'Courier New', monospace;"
#define HTML_F_SIZE "font-size: xx-small;"
#define HTML_F_WEIGHT "font-weight: bold;"
#define HTML_W_SPACE "white-space: pre;"
/*************************************************************************/
/* GLOBALS */
/*************************************************************************/
/* Structure for storing user input data */
struct userInputStruct {
int infoFlag;
int invertFlag ;
int sizeMode;
int bitGraphic;
int htmlMode;
} typedef userInput_s;
/* Structure for holding image data */
struct imageDataStruct {
int imgFileSize;
int imgWidth;
int imgHeight;
int imgRawSize;
int pixelOffset;
int paddedBytes;
int imgWidthInBytes;
char imgName[IMAGE_NAME_LEN+1];
} typedef imageData_s;
/*************************************************************************/
/* PROTOTYPING */
/*************************************************************************/
/* Bmp format related functions */
int isBmpFormat( unsigned char *imgHeader);
int bmpGetWidth( unsigned char *imgHeader );
int bmpGetHeight( unsigned char *imgHeader );
int bmpGetOffset( unsigned char *imgHeader );
int bmpGetRawSize( unsigned char *imgHeader );
int bmpGetFileSize( unsigned char *imgHeader );
int bmpGetPaddedBytes ( int pixelWidth );
int bmpGetWidthInBytes( int pixelWidth );
int storeBmpImageData( char *imagePath , imageData_s *imageData );
/* Image processing functions */
inline unsigned char getAsciiSymbol( unsigned char grayValue , int bitGraphic , int invertMode );
inline unsigned char pixelToGray( unsigned char redPix , unsigned char greenPix , unsigned char bluePix );
int makeGrayPixelMap( unsigned char **grayImageMap , imageData_s *imageData );
int printAsciiImage( unsigned char **grayImageMap, userInput_s *userInput, imageData_s *imageData );
void printImageInfo( imageData_s *imageData );
/* Other function prototypes */
unsigned char ** createPixelMap( int heightInPix , int widthInPix );
void destroyPixelMap( unsigned char **grayPixelMap, int heightInPix );
void htmlFilePrintFooter( FILE *htmlFilePtr );
void htmlFilePrintHeader( FILE *htmlFilePtr );
int byteToInt( unsigned char *dataArray , int dataOffset , int numOfBytes );
void initUserInput( userInput_s *userInput );
void helpFunction(void);
/*************************************************************************/
/* FUNCTIONS */
/*************************************************************************/
/********************************************************************************
* Function: main
* Input:
* Output:
* Description: main program function
********************************************************************************/
int main(int argc, char *argv[])
{
int i;
int retVal;
char *imagePath = "null";
unsigned char **grayPixelMap;
imageData_s imageData;
userInput_s userArgs;
/* Init */
initUserInput( &userArgs );
/*************************************************************************/
/* Get user input */
/*************************************************************************/
if( argc < 2 ) {
printf("Use -h or --help flag for help!\n");
return 0;
}
/* Loop input arguments */
for( i=1 ; i<argc ; i++ ) {
/* Get image name - first argument */
if( i == 1 ) {
imagePath = argv[1];
}
/* --info flag */
if( strcmp( argv[i] , "--info") == 0) {
userArgs.infoFlag = 1;
continue;
}
/* -i, --invert flags */
if( (strcmp( argv[i] , "-i" ) == 0 ) ||
(strcmp( argv[i] , "--invert")== 0) ) {
userArgs.invertFlag = 1;
}
/* -b, --bitGraphic flags */
if( (strcmp( argv[i] , "-b" ) == 0 ) ||
(strcmp( argv[i] , "--bitGraphic")== 0) ) {
if( argv[i+1] != NULL ) {
userArgs.bitGraphic = atoi(argv[i+1]);
if( (userArgs.bitGraphic < 1) || (userArgs.bitGraphic > 4) ) {
printf(" Warrning: -b option must be set to 1, 2, 3 or 4!\n");
userArgs.bitGraphic = 4; /* Using default value */
}
} else {
printf(" Warrning: -b option must be set to 1, 2, 3 or 4!\n");
}
continue;
}
/* -s, --size flags */
if( (strcmp( argv[i] , "-s" ) == 0 ) ||
(strcmp( argv[i] , "--size")== 0) ) {
if( argv[i+1] != NULL ) {
userArgs.sizeMode = atoi(argv[i+1]);
if( (userArgs.sizeMode < 1) || (userArgs.sizeMode > 10) ) {
printf(" Warrning: -s option must be set [ 1 - 10 ]!\n");
userArgs.sizeMode = 6; /* Using default value */
}
} else {
printf(" Warrning: -s option must be set [ 1 - 10 ]!\n");
}
continue;
}
/* --html flag */
if( strcmp( argv[i] , "--html" ) == 0 ) {
userArgs.htmlMode = 1;
}
/* -h, --help flags */
if( (strcmp( argv[i] , "-h" ) == 0 ) ||
(strcmp( argv[i] , "--help")== 0) ) {
helpFunction();
return 0;
}
} /* END Loop input arguments */
/*************************************************************************/
/* Get image info */
/*************************************************************************/
retVal = storeBmpImageData( imagePath , &imageData );
if( retVal < 0 ) {
return 0;
}
/* Print image data */
if( userArgs.infoFlag == 1 ) {
printImageInfo( &imageData );
return 0;
}
/*************************************************************************/
/* Make gray scale pixel map */
/*************************************************************************/
/* Allocate memory for gray pixel map */
grayPixelMap = createPixelMap( imageData.imgHeight , imageData.imgWidth );
if( grayPixelMap == NULL ) {
return 0;
}
/* Read image and store gray pixels in gray pixel map */
retVal = makeGrayPixelMap( grayPixelMap , &imageData );
if( retVal < 0 ) {
printf("Cannot create gray-scale pixel map!\n");
return 0;
}
/*************************************************************************/
/* Main */
/*************************************************************************/
/* Print image to output */
printAsciiImage ( grayPixelMap , &userArgs , &imageData );
/* Free memory of gray pixel map */
destroyPixelMap( grayPixelMap , imageData.imgHeight );
return 0;
}
/********************************************************************************
* FUNCTION: initUserInput
* INPUT: userInput - structure for storing user input data
* OUTPUT: /
* DESCRIPTION: This function initializes userInput_s
********************************************************************************/
void initUserInput( userInput_s *userInput )
{
userInput->infoFlag = 0;
userInput->invertFlag = 0;
userInput->sizeMode = 6;
userInput->bitGraphic = 4;
userInput->htmlMode = 0;
return;
}
/********************************************************************************
* FUNCTION: createGrayPixMap
* INPUT: heightInPix - map height
* widthInPix - map width
* OUTPUT: NULL or pointer to 2D pixel map
* DESCRIPTION: This function allocates memory on heap for gray scale pixel map
********************************************************************************/
unsigned char ** createPixelMap( int heightInPix , int widthInPix )
{
int i;
unsigned char **pixelMap;
/* Allocate memory for pixelMap - pointers to lines */
pixelMap = malloc( heightInPix * sizeof( unsigned char *));
if( pixelMap == NULL) {
printf("Cannot allocate memory for gray pixel map!\n");
return NULL;
}
/* Allocate memory for lines in grayPixelMap */
for( i=0 ; i < heightInPix ; i++ ) {
pixelMap[i] = malloc( widthInPix * sizeof(unsigned char));
if( pixelMap[i] == NULL ) {
printf("Cannot allocate memory for gray pixel map!\n");
return NULL;
}
}
return pixelMap;
}
/********************************************************************************
* FUNCTION: destroyGrayPixMap
* INPUT: **grayPixelMap - gray scale pixel map
* heightInPix - gray scale map height
* OUTPUT: /
* DESCRIPTION: This function frees memory on heap for gray scale pixel map
********************************************************************************/
void destroyPixelMap( unsigned char **grayPixelMap, int heightInPix )
{
int i;
/* Free memory for each individual line */
for( i = 0 ; i<heightInPix ; i++ ) {
free( grayPixelMap[i] );
}
/* Free pointers to each line */
free( grayPixelMap );
return;
}
/********************************************************************************
* FUNCTION: printAsciiImage
* INPUT: **grayImageMap - gray scale image map
* userInput - user input data strucure
* imageData - image data structure
* OUTPUT: ERROR or OK
* DESCRIPTION: This function prints ascii image to provided output
********************************************************************************/
int printAsciiImage( unsigned char **grayImageMap, userInput_s *userInput, imageData_s *imageData )
{
int pix;
int line;
int xAxe;
int yAxe;
int symTemp;
int symIndex;
int symAverage;
int symbolWidth; /* How many pixels from one line is in one printed symbol */
int symbolHeight; /* How many pixels from one column is in one printed symbol */
char outFilePath[IMAGE_NAME_LEN];
char *bufferedLine;
FILE *outFilePtr;
/*************************************************************************/
/* Printing settings */
/*************************************************************************/
/* Set width and height of one symbol - larger the width of symbol smaller the picture */
switch( userInput->sizeMode ) {
case 0 :
symbolWidth = 8; /* Default size */
break;
case 10:
symbolWidth = 1; /* Larges size output */
break;
default:
symbolWidth = (userInput->sizeMode-10) * (-2);
}
/* Height to width ratio is 2:1 */
symbolHeight = symbolWidth * 2;
/* Allocate memory for output line */
bufferedLine = malloc( ((imageData->imgWidth / symbolWidth) + 1) * sizeof(char));
if( bufferedLine == NULL ) {
printf("Could not allocate memory for bufferedLine!\n");
return ERROR;
}
/* Html mode */
if( userInput->htmlMode ) {
/* Create output filename */
snprintf( outFilePath , IMAGE_NAME_LEN , "%s%s" , imageData->imgName , ".html" );
/* Create or owerwrite file */
outFilePtr = fopen( outFilePath , "w" ); /* Print to html file */
if( outFilePtr == NULL ) {
printf("Could not open file %s", outFilePath );
free(bufferedLine);
return ERROR ;
}
/* Print html header to file */
htmlFilePrintHeader( outFilePtr );
} else {
outFilePtr = stdout; /* Print to console */
}
/*************************************************************************/
/* Print ascii image */
/*************************************************************************/
/* Move down the lines of 2D map */
for( yAxe = 0; yAxe < (imageData->imgHeight - symbolHeight); yAxe = yAxe + symbolHeight ) {
symIndex = 0;
/* Move throug the pixels in 2D map */
for( xAxe = 0; xAxe < (imageData->imgWidth - symbolWidth); xAxe = xAxe + symbolWidth ) {
symTemp = 0;
/* Clacualte average for one symbol */
for( line = yAxe ; line < (symbolHeight + yAxe); line++ ) {
for( pix = xAxe ; pix < (symbolWidth + xAxe); pix++ ) {
/* Add all pixel values in range of one symbol */
symTemp = symTemp + grayImageMap[line][pix];
}
}
/* Average */
symAverage = symTemp / ( symbolWidth * symbolHeight );
/* Store one ascii symbol */
bufferedLine[symIndex] = getAsciiSymbol(symAverage , userInput->bitGraphic , userInput->invertFlag );
symIndex++;
} /* END Move throug the pixels in 2D map */
bufferedLine[symIndex] = '\0';
fprintf( outFilePtr , "%s\n" , bufferedLine );
} /* END Move down the lines of 2D map */
/*************************************************************************/
/* Clean up */
/*************************************************************************/
/* Html mode */
if( userInput->htmlMode ) {
printf(" Ascii image printed to file %s\n" , outFilePath );
htmlFilePrintFooter( outFilePtr );
fclose(outFilePtr);
}
free(bufferedLine);
return OK;
}
/********************************************************************************
* FUNCTION: htmlFilePrintHeader
* INPUT: pointer to html file
* OUTPUT: /
* DESCRIPTION: This function prints html header to file
********************************************************************************/
void htmlFilePrintHeader( FILE *htmlFilePtr )
{
fprintf( htmlFilePtr, "%s", "<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body>\n<div style=\"");
fprintf( htmlFilePtr, "%s", HTML_W_SPACE HTML_F_FAMILY HTML_F_SIZE HTML_F_WEIGHT );
fprintf( htmlFilePtr, "%s", "\">\n");
return;
}
/********************************************************************************
* FUNCTION: htmlFilePrintFooter
* INPUT: pointer to html file
* OUTPUT: /
* DESCRIPTION: This function prints html footer to file
********************************************************************************/
void htmlFilePrintFooter( FILE *htmlFilePtr )
{
fprintf( htmlFilePtr, "%s", "</div>\n</body>\n</html>");
return;
}
/********************************************************************************
* Function: getAsciiSymbol
* Input: grayValue - pixel gray scale ( 0 - 255 )
* bitGraphic - 1 , 2 , 3 , 4 bit graphic
* inverMode - 1 for inverted
* Output: Ascii symbol
* Description: This function returns ascii symbol
********************************************************************************/
inline unsigned char getAsciiSymbol( unsigned char grayValue , int bitGraphic , int invertMode )
{
char retSymbol;
char tabel_1[] = "# "; /* 1 bit graphic */
char tabel_2[] = "#6+ "; /* 2 bit graphic */
char tabel_3[] = "#&$21:- "; /* 3 bit graphic */
char tabel_4[] = "##&8$62I1|:+-. "; /* 4 bit graphic */
/* Inverted mode */
if( invertMode == 1 ) {
grayValue = 255 - grayValue;
}
/* Calculate return symbol according to bitGraphic and grayValue */
switch ( bitGraphic ) {
case 1:
retSymbol = tabel_1[grayValue/128];
break;
case 2:
retSymbol = tabel_2[grayValue/64];
break;
case 3:
retSymbol = tabel_3[grayValue/32];
break;
case 4:
retSymbol = tabel_4[grayValue/16];
break;
default:
printf("%d Bit grapic not supported!\n" , bitGraphic );
retSymbol = 0;
}
return retSymbol;
}
/********************************************************************************
* FUNCTION: makeGrayPixelMap
* INPUT: **grayImageMap - 2D map for retriving grayscale pixels
* *imageData - image data struct
* OUTPUT: ERROR or OK
* DESCRIPTION: This function converts .bmp file to 2D gray pixel map
********************************************************************************/
int makeGrayPixelMap( unsigned char **grayImageMap , imageData_s *imageData )
{
int i, j;
int line;
int pixel;
int retVal;
int usefullBytesInLine;
FILE *filePtr;
unsigned char *lineBuffer;
/* Allocat memory for whole line of RGB pixels */
lineBuffer = malloc( imageData->imgWidthInBytes * sizeof(unsigned char));
if( lineBuffer == NULL ) {
printf("Cannot allocate memory for lineBuffer!\n");
return ERROR;
}
/* Open image file */
filePtr = fopen( imageData->imgName , READ_BINARY_FILE );
if( filePtr == NULL ) {
printf("Cannot open file %s!\n", imageData->imgName );
free(lineBuffer);
return ERROR;
}
/* Move to begining of RBG pixels */
retVal = fseek( filePtr , imageData->pixelOffset , SEEK_SET );
if( retVal < 0 ) {
printf("Error: fseek function!\n");
free(lineBuffer);
fclose(filePtr);
return ERROR;
}
/*************************************************************************/
/* Make gray image map */
/*************************************************************************/
/* INFO: bmp format stores first pixel line on the end of file */
line = imageData->imgHeight - 1;
/* INFO: bmp format adds zero bytes to the end of each line */
usefullBytesInLine = (imageData->imgWidthInBytes - imageData->paddedBytes );
/* Read all lines and store gray pixels in grayImageMap */
for( i=0 ; i < imageData->imgHeight ; i++ , line-- ) {
/* Read on line of RGB pixels */
retVal = fread( lineBuffer, 1 , imageData->imgWidthInBytes , filePtr );
if( retVal < 0 ) {
printf("Cannot read form file!\n");
free(lineBuffer);
fclose(filePtr);
return ERROR;
}
/* Convert line of RGB pixels to line of gray pixels */
for( j=0 , pixel=0 ; j < usefullBytesInLine ; j=j+3 , pixel++ ) {
/* Store gray pixel */
grayImageMap[line][pixel] = pixelToGray( lineBuffer[j], lineBuffer[j+1], lineBuffer[j+2]);
}
}
/* Clean up */
fclose(filePtr);
free(lineBuffer);
return OK;
}
/********************************************************************************
* FUNCTION: pixelToGray
* INPUT: redPix - color value ( 0 - 255 )
* greenPix - color value ( 0 - 255 )
* bluePix - color value ( 0 - 255 )
* OUTPUT: Gray pixel value
* DESCRIPTION: This function return gray-scale pixel
********************************************************************************/
inline unsigned char pixelToGray( unsigned char redPix , unsigned char greenPix , unsigned char bluePix )
{
unsigned char retVal = (int) (redPix + greenPix + bluePix ) / 3;
return retVal;
}
/********************************************************************************
* FUNCTION: storeBmpImageData
* INPUT: imagePath - image location on filesystem
* imageData - structure for retrived data
* OUTPUT: ERROR or OK
* DESCRIPTION: This function retrives data from image header and stores data
* in imageData_s
********************************************************************************/
int storeBmpImageData( char *imagePath , imageData_s *imageData )
{
int retVal;
FILE *filePtr;
unsigned char imageHeader[BMP_HEADER_SIZE];
/* Open and read image header */
filePtr = fopen( imagePath , "r" );
if( filePtr == NULL ) {
printf("Cannot open file %s!\n", imagePath );
return ERROR;
}
retVal = fread( imageHeader, 1 , BMP_HEADER_SIZE , filePtr );
if( retVal < 0 ) {
printf("Cannot read form file %s!\n", imagePath );
return ERROR;
}
fclose(filePtr);
/* Check image format */
if( !isBmpFormat(imageHeader) ) {
printf("File %s is not .bmp file!\n", imagePath );
return ERROR;
}
/* Check image name */
if( strlen( imagePath ) > IMAGE_NAME_LEN ) {
printf("Image path to long to store!\n");
return ERROR;
}
/* Store image data in structure */
memset( imageData->imgName , 0 , IMAGE_NAME_LEN+1 );
strncpy( imageData->imgName , imagePath , IMAGE_NAME_LEN );
imageData->imgWidth = bmpGetWidth(imageHeader);
imageData->imgHeight = bmpGetHeight(imageHeader);
imageData->imgRawSize = bmpGetRawSize(imageHeader);
imageData->pixelOffset = bmpGetOffset(imageHeader);
imageData->imgFileSize = bmpGetFileSize(imageHeader);
imageData->paddedBytes = bmpGetPaddedBytes( imageData->imgWidth );
imageData->imgWidthInBytes = bmpGetWidthInBytes( imageData->imgWidth );
return OK;
}
/********************************************************************************
* FUNCTION: bmpGetWidthInBytes
* INPUT: pixelWidth - width in pixels
* OUTPUT: Width in bytes
* DESCRIPTION: This function returns width of image array in bytes. Whidth is
* affected by number of pixels and zero byte padding.
* In bmp standard number of bytes in one line must be multiple of
* number 4. If it is not zero bytes are added.
********************************************************************************/
int bmpGetWidthInBytes( int pixelWidth )
{
int byteWidth;
byteWidth = pixelWidth * 3; /* For 24-bit image depth */
switch( byteWidth % 4 ) {
case 0 : /* Nothing padded */
break;
case 1 :
byteWidth = byteWidth + 3; /* 3 zero bytes padded */
break;
case 2 :
byteWidth = byteWidth + 2; /* 2 zero bytes padded */
break;
case 3 :
byteWidth = byteWidth + 1; /* 1 zero bytes padded */
break;
default:
printf("Cannot happen!\n");
}
return byteWidth;
}
/********************************************************************************
* FUNCTION: bmpGetPaddedBytes
* INPUT: pixelWidth - width in pixels
* OUTPUT: Padded bytes
* DESCRIPTION: This function returns number of added bytes.
* In bmp standard number of bytes in one line must be multiple of
* number 4. If it is not, zero bytes are added.
********************************************************************************/
int bmpGetPaddedBytes( int pixelWidth )
{
int byteWidth;
int paddedBytes;
paddedBytes = 0;
byteWidth = pixelWidth * 3; /* For 24-bit image depth */
switch( byteWidth % 4 ) {
case 0 : /* Nothing padded */
break;
case 1 :
paddedBytes = 3; /* 3 zero bytes padded */
break;
case 2 :
paddedBytes = 2; /* 2 zero bytes padded */
break;
case 3 :
paddedBytes = 1; /* 1 zero bytes padded */
break;
default:
printf("Cannot happen!\n");
}
return paddedBytes;
}
/********************************************************************************
* FUNCTION: bmpGetFileSize
* INPUT: *imgHeader - pointer to image header
* OUTPUT: Acctual file size
* DESCRIPTION: /
********************************************************************************/
int bmpGetFileSize( unsigned char *imgHeader )
{
return byteToInt( imgHeader , BMP_H_FILE_SIZE , 4 );
}
/********************************************************************************
* FUNCTION: bmpGetOffset
* INPUT: *imgHeader - pointer to image header
* OUTPUT: Offset where the pixel array starts in bmp file
* DESCRIPTION: /
********************************************************************************/
int bmpGetOffset( unsigned char *imgHeader )
{
return byteToInt( imgHeader , BMP_H_OFFSET , 4 );
}
/********************************************************************************
* FUNCTION: bmpGetWidth
* INPUT: *imgHeader - pointer to image header
* OUTPUT: Image width in pixels
* DESCRIPTION: /
********************************************************************************/
int bmpGetWidth( unsigned char *imgHeader )
{
return byteToInt( imgHeader , BMP_H_WIDTH , 4 );
}
/********************************************************************************
* FUNCTION: bmpGetHeight
* INPUT: *imgHeader - pointer to image header
* OUTPUT: Image height in pixels
* DESCRIPTION: /
********************************************************************************/
int bmpGetHeight( unsigned char *imgHeader )
{
return byteToInt( imgHeader , BMP_H_HEIGHT , 4 );
}
/********************************************************************************
* FUNCTION: bmpGetRawSize
* INPUT: *imgHeader - pointer to image header
* OUTPUT: Raw size of image data (including padded bytes)
* DESCRIPTION: Returns raw size of image data
********************************************************************************/
int bmpGetRawSize( unsigned char *imgHeader )
{
return byteToInt( imgHeader , BMP_H_RAW_SIZE , 4 );
}
/********************************************************************************
* FUNCTION: byteToInt
* INPUT: *dataArray - pointer to array of bytes
* dataOffset - offset in array
* numOfBytes - number of bytes
* OUTPUT: Integer value
* DESCRIPTION: This function reads given number of bytes starting form offset
* and returns integer value writen in this bytes
********************************************************************************/
int byteToInt( unsigned char *dataArray , int dataOffset , int numOfBytes )
{
int i;
int tmpInt;
int retVal;
retVal = 0;
if( numOfBytes <= 0 ) return 0;
/* Get value */
for( i = 0 ; i < numOfBytes ; i++ ) {
tmpInt = (int) pow( 256 , i );
retVal = retVal + ((int) dataArray[ dataOffset+i ] * tmpInt );
}
return retVal;
}
/********************************************************************************
* FUNCTION: isBmpFormat
* INPUT: imgHeader - image header file
* OUTPUT: True (1) or false (0)
* DESCRIPTION: This function checks image header if file is in BMP format
********************************************************************************/
int isBmpFormat( unsigned char *imgHeader )
{
if( ( imgHeader[0] == 0x42 ) && /* First byte in file */
( imgHeader[1] == 0x4d ) ) { /* Second byte in file */
return 1;
}
return 0;
}
/********************************************************************************
* FUNCTION: printImageInfo
* INPUT: imageData_s - image data holding structure
* OUTPUT: /
* DESCRIPTION: This function prints basic image data
********************************************************************************/
void printImageInfo( imageData_s *imageData )
{
printf("-----------------------------------\n");
printf("Image info:\n");
printf(" - file size: %d B\n - image width: %d pixels\n - image height: %d pixels\n",
imageData->imgFileSize , imageData->imgWidth , imageData->imgHeight );
printf(" - padded bytes: %d B\n - image header: %d B\n", imageData->paddedBytes,
imageData->pixelOffset );
printf("-----------------------------------\n");
return;
}
/********************************************************************************
* Function: helpFunction
* Input: /
* Output: /
* Description: This function prints help for this program
********************************************************************************/
void helpFunction( void )
{
printf("========================== HELP ==========================\n");
printf(" This program converts 24-bit .bmp image to ASCII image.\n");
printf(" Image is printed on standard output.\n\n");
printf(" Usage: asciiImage FILE [OPTION] \n");
printf(" Options:\n");
printf(" -b, -bitGraphic ... bit color option: 1 bit .. 4 bit\n");
printf(" -h, --help ... this menu\n");
printf(" --html ... print image to .html file\n");
printf(" --info ... print image info\n");
printf(" -i, --invert ... invert ascii colors\n");
printf(" -s, --size ... size option [1-10]\n\n");
printf("==========================================================\n");
return;
}