Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct _lbheader{
- unsigned short identifier; // 0x0000
- unsigned int filesize; // 0x0002
- unsigned int reserved; // 0x0006
- unsigned int bitmap_dataoffset; // 0x000A
- unsigned int bitmap_headersize; // 0x000E
- unsigned int width; // 0x0012
- unsigned int height; // 0x0016
- unsigned short planes; // 0x001A
- unsigned short bits_perpixel; // 0x001C
- unsigned int compression; // 0x001E
- unsigned int bitmap_datasize; // 0x0022
- unsigned int hresolution; // 0x0026
- unsigned int vresolution; // 0x002A
- unsigned int usedcolors; // 0x002E
- unsigned int importantcolors; // 0x0032
- unsigned int palette; // 0x0036
- } __attribute__((packed,aligned(1))) lbheader;
- int readbmp(char* filename, lbheader& hbmp, int mode, unsigned char* buffer)
- {
- FILE* ifp;
- char c[128];
- unsigned char* ptr;
- sprintf(c, "%s", filename);
- ifp = fopen(c, "rb");
- if(ifp==NULL){
- printf("readbmp: file open error\n");
- return -1;
- }
- ptr = (unsigned char *)&hbmp;
- fread(ptr, sizeof(unsigned char), sizeof(lbheader), ifp);
- if(mode==1){
- fread(buffer, sizeof(unsigned char), (hbmp.width*hbmp.height*3), ifp);
- }
- else{
- fclose(ifp);
- return (hbmp.width*hbmp.height*3);
- }
- fclose(ifp);
- return 1;
- }
- int writebmp(char* filename, lbheader hbmp, unsigned char* buffer)
- {
- FILE* ofp;
- char c[128];
- unsigned char* ptr;
- sprintf(c, "%s", filename);
- ofp = fopen(c, "wb");
- if(ofp==NULL){
- printf("writebmp: file open error\n");
- return -1;
- }
- //write header
- ptr = (unsigned char *)&hbmp;
- fwrite(ptr, sizeof(unsigned char), sizeof(lbheader), ofp);
- //write reverse pixel
- int i,j;
- for(i=0;i<hbmp.height;i++)
- for(j=0;j<hbmp.width;j++)
- fwrite(buffer+i*hbmp.width*3+(hbmp.width-j-1)*3, sizeof(unsigned char), 3, ofp);
- fclose(ofp);
- return 1;
- }
- int main(int argc, char *argv[])
- {
- unsigned char* bimage;
- lbheader bmpinfo;
- if(argc == 2)
- {
- bimage = (unsigned char *)malloc(sizeof(unsigned char)*readbmp(argv[1], bmpinfo, 0, bimage));
- readbmp(argv[1], bmpinfo, 1, bimage);
- }
- else
- {
- bimage = (unsigned char *)malloc(sizeof(unsigned char)*readbmp("./default.bmp", bmpinfo, 0, bimage));
- readbmp("./default.bmp", bmpinfo, 1, bimage);
- }
- writebmp("./output.bmp",bmpinfo,bimage);
- //system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment