📄 showbmp.c
字号:
#include <sys/param.h>#include <sys/file.h>#include <sys/time.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <dirent.h>#include <linux/limits.h>#include <ctype.h>#include <linux/ppw.h>#define EXTEND_MAX 30/*error messages*/#define ERR_TOOMANY "Too many files\n"#define ERR_UNFOUND "No such file or directory\n"#define ERR_FORMAT "Unknown file format\n"#define ERR_PATH "Is a directory\n"#define ERR_BITMAP "Not a bitmap image\n" #define ERR_COMPRESSED "Image is compressed\n"#define MSG_COPYRIGHT "\n--- PET Linux BMP --> .h format convertor.---\n\n"typedef short WORD;typedef long DWORD;/*the file head of BMP format file*/typedef struct { /* 0x00 */ WORD Type; /*Must be "BM"(0x4d42)*/ /* 0x02 */ DWORD file_size; /*the size of file */ /* 0x06 */ WORD reserved1; /*reserved*/ /* 0x08 */ WORD reserved2; /*reserved*/ /* 0x0A */ DWORD offset; /*the offset of the image data*/ /* 0x0E */ DWORD sizestruct; /*the size of the BMP file head*/ /* 0x12 */ DWORD width; /*the width of the image*/ /* 0x16 */ DWORD height; /*the height of the image*/ /* 0x1A */ WORD planes; /*the plane number */ /* 0x1C */ WORD bitCount; /*the bit number per pixel*/ /* 0x1E */ DWORD Compression; /* compression specification */ /* 0 -- none; 1 -- RLE 8-bit/pixel; 2 -- RLE 4-bit/pixel; 3 -- bitfields */ /* 0x22 */ DWORD SizeImage; /*the byte number of the image*/ /* 0x26 */ DWORD XpelsPerMeter; /* 0x2A */ DWORD YPelsPerMeter; /* 0x2E */ DWORD colorUsed; /*how many color used*/ /* 0x32 */ DWORD colorImportant; /*the index of the most important color*/ /* 0x36 */ /* palette 1 byte for blue; 1 byte for green; 1 byte for red; 1 byte always set to 0*/ /* 0x436 the rest are the data of bitmap*/ }BMPHEAD;#define TYPE_OTHER 0#define TYPE_BMP 1#define TYPE_TIFF 2static BMPHEAD *g_filehead;static unsigned char *g_image_data;static long g_image_height;static long g_image_width;static short g_bit_count;static void FileNameSplit(int ,char **,char *,char *); static void usage(void);static int OpenImageFile(char *);static short GetFileType(int fpImage);static unsigned char GetImage(int,int,int);static void PrintImageData();static unsigned int sInfo[4096];int ShowBitmap(char *filename,int x,int y){ int fpImage; short file_type; Boolean retVal=False; fpImage = OpenImageFile(filename); if(fpImage!=-1) { file_type = GetFileType(fpImage); /*Get the file type*/ switch(file_type){ /*get the image information and data*/ case TYPE_BMP: if(GetImage(fpImage,x,y)) retVal=True; break; default: fprintf(stderr,ERR_FORMAT); break; } /*end of switch*/ close(fpImage); } return retVal;}static int OpenImageFile(char *filename){ char curr_path[51]; int fpImage; if((fpImage=open(filename,0))==-1){ fprintf(stderr,ERR_UNFOUND); } return fpImage;}/*get the file type by reading the file head*/static short GetFileType(int fpImage){ lseek(fpImage,0,SEEK_SET); g_filehead=(BMPHEAD*)malloc(sizeof(BMPHEAD)); read(fpImage,(BMPHEAD*)g_filehead,sizeof(BMPHEAD)); //printf("type:%x\nsize:%d\nBit count:%d\nwidth:%d,height:%d\n", // g_filehead->Type,g_filehead->file_size, // g_filehead->bitCount,g_filehead->width,g_filehead->height); if(((BMPHEAD*)g_filehead)->Type==0x4d42){ /*is BMP file*/ return TYPE_BMP; } else free(g_filehead); /*for next format */ /*bellow can hold other picture type*/ return TYPE_OTHER;}/*get image data in a certain format from the BMP file*/static unsigned char GetImage(int fpImage,int x,int y){ int h,w; /*loop value*/ long BytesPerLineSrc,BytesPerLineDst; BMPHEAD *bmphead; BitmapType tempbmp={0}; GraphicsContext gc={colorBlack,Mode_SRC,BlackPattern}; DWORD fstRGB; /*the first item in the table of RGB*/ bmphead=(BMPHEAD*)g_filehead;/* bmphead->bitCount=(bmphead->bitCount>>8)|(bmphead->bitCount<<8); bmphead->Compression=((bmphead->Compression>>16)&0x000000ff)|((bmphead->Compression>>8)&0x0000ff00)|((bmphead->Compression<<8)&0x00ff0000)|((bmphead->Compression<<16)&0xff000000); bmphead->width=((bmphead->width>>24)&0x000000ff)|((bmphead->width>>8)&0x0000ff00)|((bmphead->width<<8)&0x00ff0000)|((bmphead->width<<24)&0xff000000); bmphead->height=((bmphead->height>>24)&0x000000ff)|((bmphead->height>>8)&0x0000ff00)|((bmphead->height<<8)&0x00ff0000)|((bmphead->height<<24)&0xff000000); bmphead->offset=((bmphead->offset>>24)&0x000000ff)|((bmphead->offset>>8)&0x0000ff00)|((bmphead->offset<<8)&0x00ff0000)|((bmphead->offset<<24)&0xff000000);*/ if(bmphead->bitCount!=1){ /*must be a bitmap image*/ fprintf(stderr,ERR_BITMAP); return 0; } /*end of if*/ else { /*read color index table */ lseek(fpImage,sizeof(BMPHEAD),SEEK_SET); read(fpImage,&fstRGB,sizeof(DWORD)); /*the first item of the color index table*/ } if(bmphead->Compression){ fprintf(stderr,ERR_COMPRESSED); return 0; } /*end of if*/ if(bmphead->width > 192 || bmphead->height > 64) { return 0; } BytesPerLineSrc=(bmphead->width*bmphead->bitCount+31)/32*4; BytesPerLineDst=(bmphead->width*bmphead->bitCount+7)/8; g_image_data=(unsigned char *)malloc(bmphead->height*BytesPerLineDst*sizeof(unsigned char)); for(h=0;h<bmphead->height;h++){ lseek(fpImage,bmphead->offset+(bmphead->height-h-1)*BytesPerLineSrc,SEEK_SET); read(fpImage,&g_image_data[h*BytesPerLineDst],BytesPerLineDst); } /*end of for*/ tempbmp.width=bmphead->width; tempbmp.height=bmphead->height; tempbmp.pixel_depth=bmphead->bitCount; tempbmp.img=g_image_data;#if 0 { int i; unsigned char *p=tempbmp.img; printf("p:%x\n",tempbmp.img); for (i = 0;i < 100;i++) { printf("%02x,",p[i]); if (i % 16 == 0 && i != 0) printf("\n"); } printf("\n"); }#endif PntDrawBitmap(x,y,&tempbmp,&gc); free(g_image_data); return -1;} /*end of getBmpData()*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -