我们从2011年坚守至今,只想做存粹的技术论坛。  由于网站在外面,点击附件后要很长世间才弹出下载,请耐心等待,勿重复点击不要用Edge和IE浏览器下载,否则提示不安全下载不了

 找回密码
 立即注册
搜索
查看: 817|回复: 2

发一份调试成功 可以读取 拷贝bmp 图片的源码

[复制链接]

该用户从未签到

4

主题

37

回帖

1137

积分

二级逆天

积分
1137

终身成就奖特殊贡献奖

QQ
发表于 2018-3-12 17:21:33 | 显示全部楼层 |阅读模式
#include  <stdio.h>
#include  <stdlib.h>


#include <sys/mman.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/ioctl.h>
#include <linux/types.h>
#include <string.h>



typedef struct                       /**** BMP file header structure ****/  
{  
    unsigned int   bfSize;           /* Size of file */  
    unsigned short bfReserved1;      /* Reserved */  
    unsigned short bfReserved2;      /* ... */  
    unsigned int   bfOffBits;        /* Offset to bitmap data */  
} MyBITMAPFILEHEADER;




typedef struct                       /**** BMP file info structure ****/  
{  
    unsigned int   biSize;           /* Size of info header */  
    int            biWidth;          /* Width of image */  
    int            biHeight;         /* Height of image */  
    unsigned short biPlanes;         /* Number of color planes */  
    unsigned short biBitCount;       /* Number of bits per pixel */  
    unsigned int   biCompression;    /* Type of compression to use */  
    unsigned int   biSizeImage;      /* Size of image data */  
    int            biXPelsPerMeter;  /* X pixels per meter */  
    int            biYPelsPerMeter;  /* Y pixels per meter */  
    unsigned int   biClrUsed;        /* Number of colors used */  
    unsigned int   biClrImportant;   /* Number of important colors */  
} MyBITMAPINFOHEADER;  


typedef unsigned char BYTE;



typedef struct  tagRGBQUAD{  

    BYTE rgbBlue;//蓝色的亮度(值范围为0-255)  
    BYTE rgbGreen;//绿色的亮度(值范围为0-255)  
    BYTE rgbRed;//红色的亮度(值范围为0-255)  
    BYTE rgbReserved;//保留,必须为0  
}RGBQUAD;  



void SaveBmp(const char *filename,unsigned char *rgbbuf,int width,int height)  
{  
    MyBITMAPFILEHEADER bfh;  
    MyBITMAPINFOHEADER bih;  

   
    /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */  
    unsigned short bfType=0x4d42;            
    bfh.bfReserved1 = 0;  
    bfh.bfReserved2 = 0;  
    bfh.bfSize = 2+sizeof(MyBITMAPFILEHEADER) + sizeof(MyBITMAPINFOHEADER)+width*height*3;  
    bfh.bfOffBits = 0x36;  
  
    bih.biSize = sizeof(MyBITMAPINFOHEADER);  
    bih.biWidth = width;  
    bih.biHeight = height;  
    bih.biPlanes = 1;  
    bih.biBitCount = 24;  
    bih.biCompression = 0;  
    bih.biSizeImage = 0;  
    bih.biXPelsPerMeter = 5000;  
    bih.biYPelsPerMeter = 5000;  
    bih.biClrUsed = 0;  
    bih.biClrImportant = 0;  



  
    FILE *file = fopen(filename, "wb");  
    if (!file)  
    {  
        printf("Could not write file\n");  
        return;  
    }  
  
    /*Write headers*/  
    fwrite(&bfType,sizeof(bfType),1,file);  
    fwrite(&bfh,sizeof(bfh),1, file);  
    fwrite(&bih,sizeof(bih),1, file);  
  
    fwrite(rgbbuf,width*height*3,1,file);  
    fclose(file);  



   
}  



int main()
{

    unsigned char *buffer;
    int length,imagesize;

    int fd,width,height;


    fd = open("k1.bmp", O_RDONLY);
    if (fd == -1) {
        printf("Unable to open file '\n");
        return;
    }

    imagesize = lseek(fd, 0, SEEK_END);
    printf("imgsize=%d \n",imagesize);
    lseek(fd, 0, SEEK_SET);

   
    buffer = malloc(imagesize);
   
    if (buffer == NULL) {
        printf("Unable to allocate memory for MJPEG image\n");
        return;
    }

    read(fd,buffer, imagesize);
    close(fd);

    SaveBmp("test.bmp", buffer+0x36 ,640, 360);

   
    return 0;



}


linux 下编译命令:

gcc bmp.c  -o bmp

运行:
./bmp     (运行前先将 k1.bmp  copy到运行目录下, 运行后可发现有test.bmp 文件生成 )
回复

使用道具 举报

该用户从未签到

12

主题

7221

回帖

100

积分

游客

积分
100

终身成就奖特殊贡献奖原创先锋奖优秀斑竹奖

QQ
发表于 2018-3-12 21:04:31 | 显示全部楼层
回复

使用道具 举报

该用户从未签到

49

主题

644

回帖

828

积分

PADS-170802高级班

积分
828

终身成就奖

QQ
发表于 2018-3-13 08:50:50 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

论坛开启做任务可以
额外奖励金币快速赚
积分升级了


Copyright ©2011-2024 NTpcb.com All Right Reserved.  Powered by Discuz! (NTpcb)

本站信息均由会员发表,不代表NTpcb立场,如侵犯了您的权利请发帖投诉

平平安安
TOP
快速回复 返回顶部 返回列表