This repository was archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
494 lines (493 loc) · 10.5 KB
/
main.c
File metadata and controls
494 lines (493 loc) · 10.5 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
/*
ImageProcessWithC
Copyright (C) 2015 KosWu
KosWu's Blog: http://blog.koswu.com
Email :ws00298046@163.com
*/
/*HeadFile Declaration*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*struct align*/
#pragma pack(2)
/*Donstant define*/
#define STARBAR "*********************************************"
#define NAMEMAX 40
#define BI_RGB 0
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3
/*Typedef about variable*/
typedef unsigned int DWORD;
typedef int LONG;
typedef unsigned short WORD;
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned char BYTE;
/* FILEHEADER */
typedef struct tagBITMAPFILEHEADER
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} fileheader;
typedef struct{ /* BITMAPheader */
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} bitmapheader;
/*COLORTABLE*/
typedef struct {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
/*BITMAPDATA*/
typedef struct
{
uchar * mapdata;
uint mapheight;
uint mapwidth;
uint linebites;
RGBQUAD * colortablefp;
int mapimagebit;
char * filename;
}bmpData;
/*IMAGE pixel with 24-bit image*/
typedef struct
{
char R;
char G;
char B;
}colorimage;
/*function*/
bool readfilehead (FILE * file);
bool readbmp (bmpData * temp);
bool writebmp (bmpData map);
bool write4ascii (bmpData bmp,char filename[]);
bool bmpzoom (bmpData * temp,float scale);
char * chargets (char *buf,int bufsize,FILE * steam);
bool write4ascii (bmpData map,char filename[]);
bool bmpgra (bmpData * temp);
/*main*/
int main (void)
{
/*Bariavle*/
bmpData bmp;
char filename[NAMEMAX];
char ch;
float scale;
char asciiname[NAMEMAX];
/*Copyright*/
puts ("Welcome to use ImageProcessWithC");
puts ("Copyright (C) 2015 KosWu");
puts (STARBAR);
/*Get file path*/
GETFILE:fputs ("Please input bmp image path\n(Example: /sdcard/xx.bmp):",stdout);
chargets (filename,NAMEMAX-1,stdin);
bmp.filename=filename;
/*Read bmp*/
while (!readbmp (&bmp))
{
fputs ("Read file false,Please try another file:",stdout);
chargets (filename,NAMEMAX-1,stdin);
bmp.filename=filename;
}
/*Print menu*/
while (ch!='q')
{
puts (STARBAR);
puts ("Function menu");
puts ("a)Image resizing b)Image grayimn");
puts ("c)Output file with ASCII PRINT d)Image save to file");
puts ("r)read Another image q)quite");
puts (STARBAR);
/*Read input*/
fputs ("Please input your select: ",stdout);
ch=getchar ();
while (getchar()!='\n');//Absorb Enter
switch (ch)
{
/*Zooming*/
case 'a':
fputs ("Please input zoom level :",stdout);
scanf ("%f",&scale);
while (getchar()!='\n');//Absorb Enter
if (!bmpzoom (&bmp,scale))
{
puts ("Error!");
}
else
{
puts ("Done!");
}
break;
/*Graying*/
case 'b':
if (!bmpgra (&bmp))
{
puts ("Error!");
}
else
{
puts ("Done!");
}
break;
/*ASCII Print*/
case 'c':
fputs ("Please input save path(If file exists,Will be output to tail)\(Example: /sdcard/ascii.txt):",stdout);
chargets (asciiname,NAMEMAX-1,stdin);
if (!write4ascii (bmp,asciiname))
{
puts ("Output Error!");
}
else
{
puts ("Output Done!");
}
break;
/*Save bmp*/
case 'd' :
fputs ("Please input The save file path(If file exists,Will cover files\n(Example: /sdcard/picture.bmp):",stdout);
chargets (bmp.filename,NAMEMAX-1,stdin);
if (!writebmp(bmp))
{
puts ("Save Error!");
}
else
{
puts ("Save Done!");
}
break;
/*Read Again*/
case 'r':
/*Free Space*/
free (bmp.mapdata);
if (bmp.mapimagebit<16)
{
free (bmp.colortablefp);
}
goto GETFILE;//Return to get The file path
break;
case 'q':
puts ("Program Finish,Good Bye!");
return 0;
default:
puts ("Input Error,Please input Again!");
}
}
return 0;
}
/*Filehead Read Function*/
bool readfilehead (FILE * file)
{
/*Read filehead*/
fileheader headtemp;
fread (&headtemp,sizeof (fileheader),1,file);
if (headtemp.bfType!=0x4D42)
{
puts ("The File isn't a bitmap file!");
return false;
}
printf ("The file is bitmap file,The image size is%ubyte\n",headtemp.bfSize);
return true;
}
/*Bitmap read Function*/
bool readbmp (bmpData * temp)
{
/*Variable*/
FILE * fp;
bitmapheader map;
/*Open image file*/
fputs ("Opening The file……",stdout);
if ((fp=fopen (temp->filename,"rb"))==NULL)
{
puts ("Error");
return false;
}
puts ("Done");
/*read and skip filehead*/
if (!readfilehead (fp))
{
return false;
}
fseek (fp,sizeof (fileheader),SEEK_SET);
/*read maphead*/
bitmapheader maphead;
fread (&maphead,sizeof(bitmapheader),1,fp);
printf ("The Image Height is%dpixel,Width is%dpixel\nThe image%ubitmap\n",maphead.biHeight,maphead.biWidth,maphead.biBitCount);
puts (STARBAR);
if (maphead.biCompression!=BI_RGB)
{
puts ("The Image is compound image,The process can't support!");
return false;
}
//Read bmpData
temp->mapheight=maphead.biHeight;
temp->mapwidth=maphead.biWidth;
temp->mapimagebit=maphead.biBitCount;
//Calculate image size
temp->linebites=(maphead.biBitCount*maphead.biWidth/8+3)/4*4;
//Read Color Table
if (maphead.biBitCount<16)
{
/*Create Color table Space*/
temp->colortablefp=(RGBQUAD *)malloc (sizeof (RGBQUAD)*pow(2,maphead.biBitCount));
fread (temp->colortablefp,sizeof (RGBQUAD),pow (2,maphead.biBitCount),fp);
}
/*Create Data Space*/
temp->mapdata=(uchar *) malloc (temp->linebites*temp->mapheight);
/*Read Mapdata*/
fread(temp->mapdata,1,(temp->linebites)*(temp->mapheight),fp);
/*Close File*/
if (fclose (fp)==EOF)
{
puts ("Close Error!");
}
return true;
}
/*Output Image*/
bool writebmp (bmpData map)
{
/*Variable*/
fileheader filehead;
bitmapheader maphead;
FILE * output;
int temp;
/*To determine if there is a data structure*/
if (map.mapimagebit<1||map.mapimagebit>36)
{
return false;
}
/*Open file*/
if ((output=fopen (map.filename,"wb"))==NULL)
{
puts ("Save file Error!");
}
/*Calculat color table size*/
if (map.mapimagebit<16)
{
temp=sizeof (RGBQUAD)*pow(2,map.mapimagebit);
}
else
{
temp=0;
}
/*Write filehead*/
filehead.bfType=0x4D42;
filehead.bfSize=temp+sizeof (fileheader)+sizeof (bitmapheader)+(map.linebites)*map.mapheight;
filehead.bfReserved1=0;
filehead.bfReserved2=0;
filehead.bfOffBits=sizeof (fileheader)+sizeof (bitmapheader)+temp;
/*Write MapData Head*/
maphead.biSize=40;
maphead.biWidth=map.mapwidth;
maphead.biHeight=map.mapheight;
maphead.biPlanes=1;
maphead.biBitCount=map.mapimagebit;
maphead.biCompression=BI_RGB;
maphead.biXPelsPerMeter=0;
maphead.biYPelsPerMeter=0;
maphead.biClrUsed=temp/sizeof (RGBQUAD);
maphead.biClrImportant=0;
/*Write to file*/
fwrite (&filehead,sizeof (fileheader),1,output);
fwrite (&maphead,sizeof (bitmapheader),1,output);
if (map.mapimagebit<16)
{
fwrite (map.colortablefp,temp,1,output);
}
fwrite (map.mapdata,map.linebites*map.mapheight,1,output);
/*Close File*/
if ((fclose (output))==EOF)
{
puts ("Close File Error!");
return false;
}
return true;
}
/*Image zoom Function*/
bool bmpzoom (bmpData * temp,float scale)
{
/*Variable*/
bmpData newbmp;
int linedataize;
long i,j;
/*Write maphead*/
newbmp.mapheight=(long)(temp->mapheight*scale+0.5);
newbmp.mapwidth=(long)(temp->mapwidth*scale+0.5);
newbmp.mapimagebit=temp->mapimagebit;
newbmp.linebites=(newbmp.mapimagebit*newbmp.mapwidth/8+3)/4*4;
newbmp.colortablefp=temp->colortablefp;
newbmp.filename=temp->filename;
/*Create mapdata Space*/
newbmp.mapdata=(uchar *)malloc (newbmp.linebites*newbmp.mapheight);
/*Write map data*/
if (newbmp.mapimagebit==8)
{
for (i=0;i<newbmp.mapheight;i++)
{
for (j=0;j<newbmp.mapwidth;j++)
{
(*(newbmp.mapdata+(newbmp.linebites*i)+j))=*(temp->mapdata+((int)(temp->linebites*i*scale+0.5))+(int)(j*scale+0.5));
}
}
/*Free Old Data Space*/
free (temp->mapdata);
/*Assignment Again*/
*temp=newbmp;
return true;
}
if (newbmp.mapimagebit==24)
{
colorimage *newfp=(colorimage *)newbmp.mapdata;
colorimage *oldfp=(colorimage *)temp->mapdata;
/*Write map data*/
for (i=0;i<newbmp.mapheight;i++)
{
for (j=0;j<newbmp.mapwidth;j++)
{
*(newfp+((newbmp.mapwidth*i))+j)=*(oldfp+((temp->mapwidth)*((int)(i/scale+0.5))+((int)(j/scale+0.5))));
}
}
/*Free old Data Space*/
free (temp->mapdata);
/*Assignment Again*/
*temp=newbmp;
return true;
}
puts ("Can't support The image bit.");
return false;
}
/* Ascii Print Function*/
bool write4ascii (bmpData map,char filename[])
{
if (map.mapimagebit!=24)
{
puts ("Sorry,Can't support The image bit.");
return false;
}
/*Variable*/
FILE * fp;
uint grlevel;
uint w,h;
colorimage * rgb;
/*Pointers*/
rgb=(colorimage *)map.mapdata;
/*Open file*/
if ((fp=fopen (filename,"a"))==NULL)
{
puts ("Open failed.");
return false;
}
/*Write file*/
for (h=0;h<map.mapheight;h++)
{
for (w=0;w<map.mapwidth;w++)
{
/*Calculated pointer position*/
rgb=((colorimage *)map.mapdata)+(map.mapheight-1-h)*map.mapwidth+w;
/*Calculat gralevel*/
grlevel=(rgb->R*30+rgb->G*59+rgb->B*11)/100;
/*Output Char*/
if (grlevel<32)
{
fputc ('#',fp);
}
else if (grlevel<64)
{
fputc ('&',fp);
}
else if (grlevel<96)
{
fputc ('$',fp);
}
else if (grlevel<128)
{
fputc ('*',fp);
}
else if (grlevel<159)
{
fputc ('+',fp);
}
else if (grlevel<191)
{
fputc (';',fp);
}
else if (grlevel<223)
{
fputc ('.',fp);
}
else
{
fputc (' ',fp);
}
}
/*Put \n*/
fputc ('\n',fp);
}
fputc ('\n',fp);
/*Close file*/
fclose (fp);
return true;
}
/*gray Function*/
bool bmpgra (bmpData * temp)
{
if (temp->mapimagebit==24)
{
/*Variable*/
int h,w;
colorimage * rgb;
int gralevel;
/*Printer*/
rgb=(colorimage *)(temp->mapdata);
/*process image data*/
for (h=0;h<temp->mapheight;h++)
{
for (w=0;w<temp->mapwidth;w++)
{
/*Pointer ++*/
rgb++;
/*calculat gralevel*/
gralevel=(rgb->R*30+rgb->G*59+rgb->B*11)/100;
/*Write*/
rgb->R=rgb->G=rgb->B=gralevel;
}
}
return true;
}
puts ("Error,Can't Support The image bit");
return false;
}
/*Read Chars Function*/
char * chargets (char *buf,int bufsize,FILE * steam)
{
short temp;
char * ch;
if ((ch=fgets (buf,bufsize,steam))!=buf)
{
return ch;
}
for (temp=0;temp<bufsize;temp++)
{
if (buf [temp]=='\n')
{
buf[temp]='\0';
return ch;
}
}
return ch;
}