RenHao

multimedia_20150427_hk2

Apr 27th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct _lbheader{
  5.     unsigned short identifier;      // 0x0000
  6.     unsigned int filesize;          // 0x0002
  7.     unsigned int reserved;          // 0x0006
  8.     unsigned int bitmap_dataoffset; // 0x000A
  9.     unsigned int bitmap_headersize; // 0x000E
  10.     unsigned int width;             // 0x0012
  11.     unsigned int height;            // 0x0016
  12.     unsigned short planes;          // 0x001A
  13.     unsigned short bits_perpixel;   // 0x001C
  14.     unsigned int compression;       // 0x001E
  15.     unsigned int bitmap_datasize;   // 0x0022
  16.     unsigned int hresolution;       // 0x0026
  17.     unsigned int vresolution;       // 0x002A
  18.     unsigned int usedcolors;        // 0x002E
  19.     unsigned int importantcolors;   // 0x0032
  20.     unsigned int palette;           // 0x0036
  21. } __attribute__((packed,aligned(1))) lbheader;
  22.  
  23.  
  24. int readbmp(char* filename, lbheader& hbmp, int mode, unsigned char* buffer)
  25. {
  26.     FILE* ifp;
  27.     char c[128];
  28.     unsigned char* ptr;
  29.  
  30.     sprintf(c, "%s", filename);
  31.     ifp = fopen(c, "rb");
  32.     if(ifp==NULL){
  33.         printf("readbmp: file open error\n");
  34.         return -1;
  35.     }
  36.  
  37.     ptr = (unsigned char *)&hbmp;
  38.     fread(ptr, sizeof(unsigned char), sizeof(lbheader), ifp);
  39.    
  40.     if(mode==1){
  41.         fread(buffer, sizeof(unsigned char), (hbmp.width*hbmp.height*3), ifp);
  42.     }
  43.     else{
  44.         fclose(ifp);
  45.         return (hbmp.width*hbmp.height*3);
  46.     }
  47.  
  48.     fclose(ifp);
  49.     return 1;
  50. }
  51. int writebmp(char* filename, lbheader hbmp, unsigned char* buffer)
  52. {
  53.     FILE* ofp;
  54.     char c[128];
  55.     unsigned char* ptr;
  56.  
  57.     sprintf(c, "%s", filename);
  58.     ofp = fopen(c, "wb");
  59.     if(ofp==NULL){
  60.         printf("writebmp: file open error\n");
  61.         return -1;
  62.     }
  63.  
  64.     //write header
  65.     ptr = (unsigned char *)&hbmp;
  66.     fwrite(ptr, sizeof(unsigned char), sizeof(lbheader), ofp);
  67.    
  68.     //write reverse pixel
  69.     int i,j;
  70.     for(i=0;i<hbmp.height;i++)
  71.         for(j=0;j<hbmp.width;j++)
  72.                fwrite(buffer+i*hbmp.width*3+(hbmp.width-j-1)*3, sizeof(unsigned char), 3, ofp);
  73.  
  74.     fclose(ofp);
  75.     return 1;
  76. }
  77.  
  78. int main(int argc, char *argv[])
  79. {
  80.   unsigned char* bimage;
  81.   lbheader bmpinfo;
  82.   if(argc == 2)
  83.   {
  84.           bimage = (unsigned char *)malloc(sizeof(unsigned char)*readbmp(argv[1], bmpinfo, 0, bimage));
  85.           readbmp(argv[1], bmpinfo, 1, bimage);
  86.   }
  87.   else
  88.   {
  89.         bimage = (unsigned char *)malloc(sizeof(unsigned char)*readbmp("./default.bmp", bmpinfo, 0, bimage));
  90.         readbmp("./default.bmp", bmpinfo, 1, bimage);
  91.   }
  92.   writebmp("./output.bmp",bmpinfo,bimage);
  93.  
  94.   //system("PAUSE");   
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment