---
title: "C语言读取bmp位图文件(含bmp格式定义)"
author: "hereson3"
date: "2009-02-09T17:45:57Z"
tags: []
slug: "c-bmp-bmp"
draft: false
lastmod: "2009-02-09T17:45:57Z"
id: 325824
url: "https://kungsoft.iteye.com/blog/325824"
---

loadbmp.h

#ifndef \_LOADBMP\_H_  
#define \_LOADBMP\_H_  
typedef unsigned char    BYTE;  
typedef unsigned short    WORD;  
typedef unsigned long    DWORD;

typedef struct {  
    /* BITMAPFILEHEADER\*/  
    BYTE    bfType\[2];  
    DWORD    bfSize;  
    WORD    bfReserved1;  
    WORD    bfReserved2;  
    DWORD    bfOffBits;  
} BMPFILEHEAD;

#define FILEHEADSIZE 14

/* windows style\*/  
typedef struct {  
    /* BITMAPINFOHEADER\*/  
    DWORD    BiSize;  
    DWORD    BiWidth;  
    DWORD    BiHeight;  
    WORD    BiPlanes;  
    WORD    BiBitCount;  
    DWORD    BiCompression;  
    DWORD    BiSizeImage;  
    DWORD    BiXpelsPerMeter;  
    DWORD    BiYpelsPerMeter;  
    DWORD    BiClrUsed;  
    DWORD    BiClrImportant;  
} BMPINFOHEAD;

#define INFOHEADSIZE 40

typedef struct \_BMP {  
    BMPINFOHEAD info;  
    unsigned char \*rgba;  
    unsigned char \*yuy2;  
    unsigned char \*yv12;  
} BMP, \*PBMP;

int LoadBMP(char \*name, PBMP pbmp);  
int ReleaseBMP(PBMP pbmp);  
void rgb\_to\_yuv(unsigned int r, unsigned int g, unsigned int b,  
    unsigned int \*y, unsigned int \*u, unsigned int \*v);  
#endif/\*\_LOADBMP\_H\_\*/

loadbmp.c

#include &lt;stdio.h&gt;  
#include &lt;sys/types.h&gt;  
#include &lt;sys/stat.h&gt;  
#include &lt;fcntl.h&gt;  
#include &lt;unistd.h&gt;  
#include &lt;stdlib.h&gt;

#include "loadbmp.h"

void rgb\_to\_yuv(unsigned int r, unsigned int g, unsigned int b,  
    unsigned int \*y, unsigned int \*u, unsigned int \*v)  
{  
    double dy, du, dv;

    dy = (0.257 * (double)r) +  
        (0.504 * (double)g) + (0.098 * (double)b) + 16.0;  
    dv= (0.439 * (double)r) - (0.368 * (double)g) - (0.071 * (double)b) +  
        128.0;  
    du = -(0.148 * (double)r) - (0.291 * (double)g) +  
        (0.439 * (double)b) + 128.0;

    \*y = (unsigned int)dy & 0xff;  
    \*v = (unsigned int)dv & 0xff;  
    \*u = (unsigned int)du & 0xff;  
}

static void  
convert\_to\_yv12(PBMP pbmp)  
{  
    int x, y;  
    unsigned char \*in\_ptr, \*out\_ptr\_y, \*out\_ptr\_u, \*out\_ptr\_v;  
    unsigned int Y, U, V;

    in\_ptr = pbmp-&gt;rgba;  
    out\_ptr\_y = pbmp-&gt;yv12;  
    out\_ptr\_u = out\_ptr\_y + pbmp-&gt;info.BiWidth * pbmp-&gt;info.BiHeight;  
    out\_ptr\_v = out\_ptr\_u + pbmp-&gt;info.BiWidth * pbmp-&gt;info.BiHeight / 4;  
    for (y = 0; y &lt; pbmp-&gt;info.BiHeight; y++) {  
        for (x = 0; x &lt; pbmp-&gt;info.BiWidth; x++) {  
            rgb\_to\_yuv(in\_ptr\[0], in\_ptr\[1], in\_ptr\[2], &Y, &U, &V);  
            in\_ptr += pbmp-&gt;info.BiBitCount / 8;  
            \*out\_ptr\_y++ = Y;  
            if (x % 2 == 0 && y % 2 == 0) {  
                \*out\_ptr\_u++ = V;  
                \*out\_ptr\_v++ = U;  
            }  
        }  
    }  
}

static void  
convert\_to\_yuy2(PBMP pbmp)  
{  
    int x, y;  
    unsigned char \*in, \*out;  
    unsigned int Y, U, V;

    in = pbmp-&gt;rgba;  
    out = pbmp-&gt;yuy2;  
    for (y = 0; y &lt; pbmp-&gt;info.BiHeight; y++) {  
        for (x = 0; x &lt; pbmp-&gt;info.BiWidth; x++) {  
            static int cnt = 0;  
            rgb\_to\_yuv(in\[0], in\[1], in\[2], &Y, &U, &V);  
            in += pbmp-&gt;info.BiBitCount / 8;  
            \*(out) = Y;  
            if (cnt %2 == 0)  
                \*(out+1) = V;  
            else  
                \*(out+1) = U;  
            out+= 2;  
            cnt++;  
        }  
    }  
}

int LoadBMP(char \*name, PBMP pbmp)  
{  
    int        fd;  
    int        headsize;  
    BMPFILEHEAD    bmpf;  
    unsigned char     headbuffer\[INFOHEADSIZE];

    fd = open(name, O\_RDONLY);  
    if (fd &lt; 0) {  
        perror("open");  
        return -1;  
    }

    read(fd, &headbuffer, FILEHEADSIZE);  
    bmpf.bfType\[0] = headbuffer\[0];  
    bmpf.bfType\[1] = headbuffer\[1];  
    if (\*(WORD\*)&bmpf.bfType\[0] != 0x4D42) /* 'BM' \*/  
        return -2;    /* not bmp image\*/

    bmpf.bfOffBits = \*((DWORD\*)&headbuffer\[10]);

    if (read(fd,&headsize,sizeof(DWORD)) != sizeof(DWORD))  
        return 0;    /* not bmp image\*/

    read(fd, &headbuffer, INFOHEADSIZE - sizeof(DWORD));  
    pbmp-&gt;info.BiWidth = \*(DWORD\*)(&headbuffer\[0]);  
    pbmp-&gt;info.BiHeight = \*(DWORD\*)(&headbuffer\[4]);  
    pbmp-&gt;info.BiBitCount = \*(DWORD\*)(&headbuffer\[10]);  
    pbmp-&gt;info.BiSizeImage = \*(DWORD\*)(&headbuffer\[16]);

    pbmp-&gt;rgba = (unsigned char \*)malloc(pbmp-&gt;info.BiSizeImage);  
    if (!pbmp-&gt;rgba) {  
        perror("malloc");  
        exit(EXIT\_FAILURE);  
    }  
//    pbmp-&gt;yuy2 = (unsigned char \*)malloc(pbmp-&gt;info.BiWidth * pbmp-&gt;info.BiHeight * 2);  
//    if (!pbmp-&gt;yuy2) {  
//        perror("malloc");  
//        exit(EXIT\_FAILURE);  
//    }  
//    pbmp-&gt;yv12 = (unsigned char \*)malloc(pbmp-&gt;info.BiWidth * pbmp-&gt;info.BiHeight * 3 / 2);  
//    if (!pbmp-&gt;yv12) {  
//        perror("malloc");  
//        exit(EXIT\_FAILURE);  
//    }

    lseek(fd, bmpf.bfOffBits, SEEK\_SET);  
    if (read(fd, pbmp-&gt;rgba, pbmp-&gt;info.BiSizeImage) != pbmp-&gt;info.BiSizeImage) {  
        perror("read");  
        return -4;  
    }

//    convert\_to\_yv12(pbmp);  
//    convert\_to\_yuy2(pbmp);  
    printf("LoadBMP: %s %ldx%ldx%d\\n", name, pbmp-&gt;info.BiWidth, pbmp-&gt;info.BiHeight, pbmp-&gt;info.BiBitCount);  
    close(fd);

    return 0;  
}

int ReleaseBMP(PBMP pbmp)  
{  
    if (pbmp-&gt;rgba) {  
        free(pbmp-&gt;rgba);  
        pbmp-&gt;rgba = NULL;  
    }  
//    if (pbmp-&gt;yuy2) {  
//        free(pbmp-&gt;yuy2);  
//        pbmp-&gt;yuy2 = NULL;  
//    }  
//    if (pbmp-&gt;yv12) {  
//        free(pbmp-&gt;yv12);  
//        pbmp-&gt;yv12 = NULL;  
//    }  
    return 0;  
}