📄 bmp.c
字号:
#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <Egui.h>#include "bmp.h"EGui_FBinfo Efbinfo;char *read_file (const char* filename, size_t* length){ int fd; struct stat file_info; char* buffer; /* Open the file. */ fd = open (filename, O_RDONLY); if (fd<0) { printf ("Open file %s error\n",filename); exit ; } /* Get information about the file. */ fstat (fd, &file_info); *length = file_info.st_size; /* Make sure the file is an ordinary file. */ if (!S_ISREG (file_info.st_mode)) { /* It's not, so give up. */ close (fd); return NULL; } /* Allocate a buffer large enough to hold the file's contents. */ buffer = (char*) malloc (*length); if (buffer ==NULL) { printf ("malloc file buffer error\n"); return NULL; } /* Read the file into the buffer. */ read (fd, buffer, *length); /* Finish up. */ close (fd); return buffer;}intmain(int argc, char **argv){ char * rgbbuf; int width,height; Ecolor * bgcolor; int w,h; EGui_Window * ewindow; if (argc <2) { printf (" A 24 bits BMP display program for Egui\n"); printf ("usage : %s filename\n",argv[0]); return; } if (Egui_open (&Efbinfo)) return ; bgcolor = malloc (sizeof ( Ecolor)); if (bgcolor == NULL) { printf ("malloc color ERROR\n"); return -1; } bgcolor->r = bgcolor->g = bgcolor->b = 0xcc; /* create window will initial color's pixel. */ ewindow = Egui_CreateWindow (&Efbinfo,150,100,200, 150,bgcolor,EGUI_WINDOW_TOP); if (ewindow == NULL) { printf ("New windows failed\n"); Egui_close (); return 1; } width = 200 ; height = 150 ; rgbbuf = (char *) malloc ( width * height * 3); if (rgbbuf == NULL) { printf ("Get a buffer for RGB error!\n"); return ; } get_bmpbuf(width,height,rgbbuf,argv[1]); Egui_drawwindow(ewindow); w = ewindow->frame_w; h = ewindow->title_h; Egui_drawrgb (w,w + h,width-1,height-1,rgbbuf,ewindow); free(rgbbuf); egui_loop (); Egui_CloseWindow (ewindow); free (bgcolor); Egui_close ();}int get_bmpbuf(int width,int height,char * rgbbuf, char * filename){ char *buffer; size_t length; BITMAPFILE *bm; int i,j; char pad_num; char *aBitmapBits; int tmpwidth; buffer = read_file(filename, &length); bm = (BITMAPFILE *)buffer; if (bm->bmfh.bfType!=0x4D42 || bm->bmih.biBitCount != 24) { printf("Not a 24bit BITMAP.\n"); return -1; } if ( bm->bmih.biCompression != 0) { printf ("I'm sorry,i don't read compression BMP file\n"); return -1; } pad_num = bm->bmih.biWidth * 3 %4==0? 0 : 4 - bm->bmih.biWidth * 3 %4; aBitmapBits = (char *)((char *)bm + bm->bmfh.bfOffBits); if (height > bm->bmih.biHeight) height = bm->bmih.biHeight; tmpwidth = width; if (width > bm->bmih.biWidth) tmpwidth = bm->bmih.biWidth; for (j=0,i=height-1; j < height; j ++,i--) { //line copy memcpy((rgbbuf + j * width * 3), (void *)(aBitmapBits + i * (bm->bmih.biWidth * 3 + pad_num)),tmpwidth * 3); } free(buffer); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -