Bmp2Asc 所谓的短学期设计

话说2个礼拜前在学校是做短学期,然后做完了就回家去了~恩,现在都结束了,在清理电脑的时候发现了这个代码~

反正也没什么用了,贴一下留作纪念吧
程序很简单,就是一个把bmp图片转化成asc字符画的程序~
三个.c文件三个.h文件~
其实这几个.h文件写的不是很好,如果写一下#ifdef的话应该会更加好
先是bmp文件读写的.h文件和.c

/* * bmpRW.h * bmp2ascii * * Created by zerob13 on 6/30/10. * Copyright 2010 MyCompanyName. All rights reserved. * / #include #include #include int BmpRead(char,unsigned char *,BITMAPINFOHEADER *);

/* * bmpRW.cpp * bmp2ascii * * Created by zerob13 on 6/30/10. * Copyright 2010 MyCompanyName. All rights reserved. * / #include "Typedef.h" #include "bmpRW.h" int BmpRead(char fp,unsigned char pimage,BITMAPINFOHEADER *bb){ BITMAPFILEHEADER *bmpfh; //bmp文件头指针 BITMAPINFOHEADER *bmpih; //bmp文件信息头指针 unsigned char *data; //bmp文件数据指针 int i=0; int IMAGE_SIZE; / bmp文件头读取 / FILE *file=fopen(fp,"rb+"); bmpfh=(BITMAPFILEHEADER)malloc(sizeof(BITMAPFILEHEADER)); if(!bmpfh) { printf("Memory not enough!\n"); exit(1); } fseek(file,sizeof(short),SEEKSET); fread(bmpfh,sizeof(BITMAPFILEHEADER),1,file); /* bmp文件信息头读入 */ bmpih=(BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER)); if(!bmpih) { printf("Memory not enough!\n"); exit(1); } fseek(file,sizeof(BITMAPFILEHEADER)+sizeof(short),SEEKSET); fread(bmpih,sizeof(BITMAPINFOHEADER),1,file); /* bmp文件数据读入 / IMAGE_SIZE=(bmpih->biSizeImage); data=(unsigned char)calloc(IMAGESIZE,sizeof(char)); if(!data) { printf("Memory not enough!\n"); exit(1); } fseek(file,bmpfh->bfOffBits,SEEKSET); fread(data,IMAGE_SIZE,1,file); fclose(file); for(i=0;i### 然后是转换的部分

/* * buff2asc.h * bmp2ascii * * Created by zerob13 on 7/1/10. * Copyright 2010 MyCompanyName. All rights reserved. * */ void buff2asc(int ,int ,int ,unsigned char *,char ansmap[][Max_W]);

/* * buff2asc.c * 转换ascii函数 * * Created by zerob13 on 7/1/10. * Copyright 2010 MyCompanyName. All rights reserved. * */ #include"Typedef.h" #include"bmpRW.h" #include "buff2asc.h" void buff2asc(int size,int h,int w,unsigned char *buffer,char ansmap[][Max_W]){ int i,j,k; i=j=0; for(k=0;k### 之后是比较重要的一个.h函数,定义了bmp文件的一些信息

/* * Typedef.h * bmp2ascii * This file includes all types about bmp. * Created by zerob13 on 6/30/10. * Copyright 2010 MyCompanyName. All rights reserved. * / typedef short WORD; typedef int DWORD; typedef unsigned int LONG; typedef char BYTE; #define Max_W 800 #define Max_H 800 / 位图文件头 * */ typedef struct tagBITMAPFILEHEADER { //WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } BITMAPFILEHEADER; / 位图信息头 * */ typedef struct tagBITMAPINFOHEADER{ DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER; / 颜色表 */ typedef struct tagRGBQUAD { BYTE rgbBlue; BYTE rgbGreen; BYTE rgbRed; BYTE rgbReserved; } RGBQUAD; / *位图信息 * */ typedef struct tagBITMAPINFO { BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors[1]; } BITMAPINFO;

最后是主函数~

/* * main.c * 主函数部分 * * Created by zerob13 on 7/1/10. * Copyright 2010 MyCompanyName. All rights reserved. * / //头文件部分 #include"Typedef.h"//bmp文件类型的头文件 #include"bmpRW.h"//bmp文件读写头文件 #include"buff2asc.h"//转换为ascii的头文件 int main (int argc, char * const argv[]) { unsigned char *buffer;//缓冲区,用于储存图片色彩矩阵 int size;//图片大小 BITMAPINFOHEADER *bmpih;//图片信息头,用于获取图片分辨率 int w,h;//图片分辨率 char fp[2048],fp2[2048];//输入文件,输出文件的地址 char ansmap[Max_H][Max_W];//输出ascii矩阵 int i,j; FILE *out;//输出文件指针 //获取空间 buffer=(unsigned char)malloc(3Max_WMax_H); bmpih=(BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER)); //判断工作模式,图片地址是否已经从参数中输入 if(argc1){ printf("please input your file:)\n"); scanf("%s",fp); }else{ if(argc2) { strcpy(fp,argv[1]); }else{ printf("Error\n"); exit(1); } } //生成输出文件 strcpy(fp2,fp); strcat(fp2,"-answer.txt"); out=fopen(fp2,"w+"); //读入数据 size=BmpRead(fp,buffer,bmpih); i=0;j=0; h=bmpih->biHeight; w=bmpih->biWidth; //转换图像 buff2asc(size,h,w,buffer,ansmap); //输出结果 for(i=h-1;i>=0;i--) { fwrite(ansmap[i],sizeof(ansmap[i]),1,out); for(j=0;j### 最后是makefile~

CC=gcc bmp2ascall:main.o bmpRW.o buff2asc.o $(CC) -o $@ $^ buff2asc.o:buff2asc.c bmpRW.h Typedef.h buff2asc.h $(CC) -c $