不凋零的树叶
2018-04-28 03:46
采纳率: 100%
浏览 1.0k
已采纳

我在网上找了个bmp转YUV的程序进行修改,但读不出数据,不知什么问题,有谁懂的请帮忙看下,谢谢了

#include
#include
#include "bmp2rgb.h"

u_int8_t BMP2RGB(BITMAPFILEHEADER file_header,BITMAPINFOHEADER info_header, FILE* bmpFile, u_int8_t* rgbBuf);//24bit RGB
u_int8_t RGB24ToYUV420(int Width,int Height,u_int8_t* rgbBuf,u_int8_t*YuvBuffer);

#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a) < (b)) ? (a) : (b))

int main(int argc, char** argv)
{

//设置命令行参数
argv[1]= "boot_logo.bmp";
argv[2]="boot_logo.yuv";    //相当于设置文件名
char* bmpFileName = argv[1];
char* yuvFileName = argv[2];

//打开文件
FILE* bmpFile = fopen(bmpFileName, "rb");
if (bmpFile == NULL)
{
    printf(" Open the BMP file.\n");
    exit(1);
}
else
{
    printf("The BMP file is %s\n", bmpFileName);
}


FILE* yuvFile = fopen(yuvFileName, "wb");
if (yuvFile == NULL)
{
    printf("Cannot open the YUV file.\n");
    exit(1);
}
else
{
    printf("The YUV file is %s\n", yuvFileName);
}

//读取BMP文件头,信息头,读取错误时的处理代码
BITMAPFILEHEADER file_header;
BITMAPINFOHEADER info_header;
if (fread(&file_header, sizeof(BITMAPFILEHEADER), 1, bmpFile) != 1)
    if (file_header.bfType != 0x4D42)
    {
        printf("Not BMP file.\n");
        exit(1);
    }
if (fread(&info_header, sizeof(BITMAPINFOHEADER), 1, bmpFile) != 1)
{
    printf("read info header error!");
    exit(1);
}//结束读取BMP文件头

//读取图像尺寸
int width = info_header.biWidth;
int height = info_header.biHeight;

//开辟缓冲区 buf
u_int8_t* yBuf = (u_int8_t*)malloc(height*width);
u_int8_t* uBuf = (u_int8_t*)malloc(height*width / 4);
u_int8_t* vBuf = (u_int8_t*)malloc(height*width / 4);
u_int8_t* rgbBuf = (u_int8_t*)malloc(height*width * 3);
u_int8_t*YuvBuffer =(u_int8_t*)malloc(height*width * 5);
if (yBuf == NULL || uBuf == NULL || vBuf == NULL || rgbBuf == NULL || YuvBuffer==NULL)
{
    printf("Not enough memory\n");
    exit(1);
}

//BMP与RGB的转换,得到RGB数据
if (BMP2RGB(file_header, info_header, bmpFile, rgbBuf))
{
    printf("BMP2RGB error\n");
    exit(1);
}
//RGB与YUV的转换,得到YUV数据

// int flip = 0;
/*读取到的图像数据是倒序存放的,flip=0保证了RGB2YUV可以正确地对其转换*/
/* if (RGB2YUV(width, height, rgbBuf, yBuf, uBuf, vBuf, flip))
{
printf("RGB2YUV error\n");
exit(1);
}

//将yuv按顺序写入yuvfile文件
fwrite(yBuf, 1, width * height, yuvFile);
fwrite(uBuf, 1, (width * height) / 4, yuvFile);
fwrite(vBuf, 1, (width * height) / 4, yuvFile);*/
if( RGB24ToYUV420( width, height, rgbBuf,YuvBuffer))
{
    printf("RGB24ToYUV420 error\n");
  exit(1);
}
int len=0;
len= fwrite(YuvBuffer, 1,sizeof(YuvBuffer), yuvFile);

printf("len ==%d byte\n",len);
//打印宽高,方便yuv观看程序打开
printf("width is %d", width);
printf("\n");
printf("height is %d", height); 
printf("\n");
//清理内存
free(rgbBuf);
free(YuvBuffer); 
free(yBuf); free(uBuf); free(vBuf);  
fclose(bmpFile);
fclose(yuvFile);

return 0;

}

u_int8_t BMP2RGB(BITMAPFILEHEADER file_header,BITMAPINFOHEADER info_header, FILE* bmpFile, u_int8_t* rgbBuf)
{
BITMAPFILEHEADER file_h=file_header;
BITMAPINFOHEADER info_h=info_header;
FILE* pFile =bmpFile;
int w=0,h=0;
//确定像素的实际点阵数
w = (info_h.biWidth*info_h.biBitCount + 31) / 32 * 4;//w为实际一行的字节数
h = info_h.biHeight;//h为列数
// printf("w==%d,h==%d\n",w,h);
//开辟实际字节数量的缓冲区,读数据,一次读取一个字节
u_int8_t* dataBuf = (u_int8_t*)malloc(w*h);
/*使用文件头的字节偏移属性bfOffBits
直接把文件指针定位到像素值数据的起始 /
fseek(pFile, file_h.bfOffBits, 0);
fread(dataBuf, 1, w*h, pFile);
unsigned char
data = dataBuf;
u_int8_t* rgb = rgbBuf;

//开始写入rgb
int i, j;
for (j = 0; j < h; j++)//j控制行循环
{
    for (i = 0; i < w; i += 3)//i控制列循环
    {
        *rgb = data[i + w*j];//B
        *(rgb + 1) = data[i + w*j + 1];//G
        *(rgb + 2) = data[i + w*j + 2];//R
        rgb += 3;
    }
}
//释放内存
free(dataBuf);
return 0;

}
/***************************************************************************************************************/

u_int8_t RGB24ToYUV420(int Width,int Height,u_int8_t* rgbBuf,u_int8_t*YuvBuffer)

{

u_int8_t* yuvBuf=YuvBuffer;//YUV空间

int nWidth=Width;

int nHeight=Height;

/////////////////////下面转换算法是网上查到的

int i, j;

u_int8_t*bufY = yuvBuf;

u_int8_t*bufU = yuvBuf + nWidth * nHeight;

u_int8_t*bufV = bufU + (nWidth* nHeight* 1/4);

u_int8_t*Y=bufY;

u_int8_t*U=bufU;

u_int8_t*V=bufV;

u_int8_t*bufRGB;  
unsigned char y, u, v, r, g, b;  
if (NULL==rgbBuf)  
{  
     printf("NULL==rgbBuf\n");
   return 1 ;  
}  
for (j = 0; j<nHeight;j++)  
{  
    bufRGB = rgbBuf + nWidth * (nHeight - 1-j) * 3 ;   
    for (i = 0;i<nWidth;i++)  
    {  
        int pos = nWidth * i + j;  
        r= *(bufRGB++);  
        g = *(bufRGB++);  
        b = *(bufRGB++);  
        y =(unsigned char)(( 66 * r + 129 * g +  25 * b + 128) >>8) + 16;//16  
        v = (unsigned char)((-38 * r -  74 * g + 112 * b + 128) >>8) +128  ; //128            
        u = (unsigned char)((112 * r -  94 * g -  18 * b + 128) >> 8) + 128 ;  
        *(bufY++)=max(0,min(y, 255 ));  

        if (j%2==0&&i%2 ==0)  
        {  
            if (u>255)  
            {  
                u=255;  
            }  
            if (u<0)  
            {  
                u = 0;  
            }  
            *(bufU++) =u;  
            //存u分量  
        }  
        else  
        {  
            //存v分量  
            if (i%2==0)  
            {  
                if (v>255)  
                {  
                    v = 255;  
                }  
                if (v<0)  
                {  
                    v = 0;  
                }  
                *(bufV++) =v;  
            }  
        }  
    }  
}
return 0;  

}


#include <stdio.h>
#include "sys/types.h"
#include <stdlib.h>
typedef unsigned long  DWORD;//32bit
typedef unsigned short WORD;//16bit
typedef unsigned long  LONG; //32bit

typedef struct tagBITMAPFILEHEADER {
    //0x00~0x01,说明文件的类型
    WORD bfType; 
    //0x02~0x05,说明文件的大小,用字节B为单位
    DWORD bfSize;
    //0x06~0x07,保留,设置为0
    WORD bfReserved1;
    //0x08~0x09,保留,设置为0
    WORD bfReserved2;
    //0x0a~0x0d,说明从BITMAP_FILE_HEADER结构开始到实际的图像数据之间的字节偏移量
    DWORD bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER {
    //0x0e~0x11,说明当前结构体所需字节数
    DWORD biSize;
    //0x12~0x15,以像素为单位说明图像的宽度
    LONG biWidth;
    //0x16~0x19,以像素为单位说明图像的高度
    LONG biHeight;
    //0x1a~0x1b,说明位面数,必须为1
    WORD biPlanes;
    //0x1c~0x1d,说明图像的位深度
    WORD biBitCount;
    //0x1e~0x21,说明图像是否压缩及压缩类型
    DWORD biCompression;
    //0x22~0x25,以字节为单位说明图像大小,必须是4的整数倍
    DWORD biSizeImage;
    //0x26~0x29,目标设备的水平分辨率,像素/米 
    LONG biXPelsPerMeter;
    //0x2a~0x2d,目标设备的垂直分辨率,像素/米
    LONG biYPelsPerMeter;
    //0x2e~0x31,说明图像实际用到的颜色数,如果为0,则颜色数为2的biBitCount次方
    DWORD biClrUsed;
    //0x32~0x35,说明对图像显示有重要影响的颜色索引的数目,如果是0,表示都重要。
    DWORD biClrImportant;
} BITMAPINFOHEADER;



运行环境linux,bmp图片24位

运行结果:
The BMP file is boot_logo.bmp
The YUV file is boot_logo.yuv
len ==8 byte
width is 185729024
height is 0

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 白色一大坨 2018-04-28 05:25
    已采纳

    给你分享一下我用过的函数,输入一个rgb或gbr的图片矩阵及其宽高,输出一个转换后的灰度图:

     // BAYER GRBG
    // Y = 0.299R + 0.587G + 0.114B
    double g_RGB2YUV_RTable[] = 
    { 
    0.000,0.299,0.598,0.897,1.196,1.495,1.794,2.093,2.392,2.691,2.990,3.289,3.588,3.887,4.186,4.485,4.784,5.083,5.382,5.681,5.980,6.279,6.578,6.877,7.176,7.475,
    7.774,8.073,8.372,8.671,8.970,9.269,9.568,9.867,10.166,10.465,10.764,11.063,11.362,11.661,11.960,12.259,12.558,12.857,13.156,13.455,13.754,14.053,14.352,14.651,14.950,
    15.249,15.548,15.847,16.146,16.445,16.744,17.043,17.342,17.641,17.940,18.239,18.538,18.837,19.136,19.435,19.734,20.033,20.332,20.631,20.930,21.229,21.528,21.827,22.126,22.425,
    22.724,23.023,23.322,23.621,23.920,24.219,24.518,24.817,25.116,25.415,25.714,26.013,26.312,26.611,26.910,27.209,27.508,27.807,28.106,28.405,28.704,29.003,29.302,29.601,29.900,
    30.199,30.498,30.797,31.096,31.395,31.694,31.993,32.292,32.591,32.890,33.189,33.488,33.787,34.086,34.385,34.684,34.983,35.282,35.581,35.880,36.179,36.478,36.777,37.076,37.375,
    37.674,37.973,38.272,38.571,38.870,39.169,39.468,39.767,40.066,40.365,40.664,40.963,41.262,41.561,41.860,42.159,42.458,42.757,43.056,43.355,43.654,43.953,44.252,44.551,44.850,
    45.149,45.448,45.747,46.046,46.345,46.644,46.943,47.242,47.541,47.840,48.139,48.438,48.737,49.036,49.335,49.634,49.933,50.232,50.531,50.830,51.129,51.428,51.727,52.026,52.325,
    52.624,52.923,53.222,53.521,53.820,54.119,54.418,54.717,55.016,55.315,55.614,55.913,56.212,56.511,56.810,57.109,57.408,57.707,58.006,58.305,58.604,58.903,59.202,59.501,59.800,
    60.099,60.398,60.697,60.996,61.295,61.594,61.893,62.192,62.491,62.790,63.089,63.388,63.687,63.986,64.285,64.584,64.883,65.182,65.481,65.780,66.079,66.378,66.677,66.976,67.275,
    67.574,67.873,68.172,68.471,68.770,69.069,69.368,69.667,69.966,70.265,70.564,70.863,71.162,71.461,71.760,72.059,72.358,72.657,72.956,73.255,73.554,73.853,74.152,74.451,74.750,
    75.049,75.348,75.647,75.946,76.245
    };
    
    double g_RGB2YUV_GTable[] = 
    {
    0.000,0.587,1.174,1.761,2.348,2.935,3.522,4.109,4.696,5.283,5.870,6.457,7.044,7.631,8.218,8.805,9.392,9.979,10.566,11.153,11.740,12.327,12.914,13.501,14.088,14.675,
    15.262,15.849,16.436,17.023,17.610,18.197,18.784,19.371,19.958,20.545,21.132,21.719,22.306,22.893,23.480,24.067,24.654,25.241,25.828,26.415,27.002,27.589,28.176,28.763,29.350,
    29.937,30.524,31.111,31.698,32.285,32.872,33.459,34.046,34.633,35.220,35.807,36.394,36.981,37.568,38.155,38.742,39.329,39.916,40.503,41.090,41.677,42.264,42.851,43.438,44.025,
    44.612,45.199,45.786,46.373,46.960,47.547,48.134,48.721,49.308,49.895,50.482,51.069,51.656,52.243,52.830,53.417,54.004,54.591,55.178,55.765,56.352,56.939,57.526,58.113,58.700,
    59.287,59.874,60.461,61.048,61.635,62.222,62.809,63.396,63.983,64.570,65.157,65.744,66.331,66.918,67.505,68.092,68.679,69.266,69.853,70.440,71.027,71.614,72.201,72.788,73.375,
    73.962,74.549,75.136,75.723,76.310,76.897,77.484,78.071,78.658,79.245,79.832,80.419,81.006,81.593,82.180,82.767,83.354,83.941,84.528,85.115,85.702,86.289,86.876,87.463,88.050,
    88.637,89.224,89.811,90.398,90.985,91.572,92.159,92.746,93.333,93.920,94.507,95.094,95.681,96.268,96.855,97.442,98.029,98.616,99.203,99.790,100.377,100.964,101.551,102.138,102.725,
    103.312,103.899,104.486,105.073,105.660,106.247,106.834,107.421,108.008,108.595,109.182,109.769,110.356,110.943,111.530,112.117,112.704,113.291,113.878,114.465,115.052,115.639,116.226,116.813,117.400,
    117.987,118.574,119.161,119.748,120.335,120.922,121.509,122.096,122.683,123.270,123.857,124.444,125.031,125.618,126.205,126.792,127.379,127.966,128.553,129.140,129.727,130.314,130.901,131.488,132.075,
    132.662,133.249,133.836,134.423,135.010,135.597,136.184,136.771,137.358,137.945,138.532,139.119,139.706,140.293,140.880,141.467,142.054,142.641,143.228,143.815,144.402,144.989,145.576,146.163,146.750,
    147.337,147.924,148.511,149.098,149.685
    };
    
    double g_RGB2YUV_BTable[] = 
    {
    0.000,0.114,0.228,0.342,0.456,0.570,0.684,0.798,0.912,1.026,1.140,1.254,1.368,1.482,1.596,1.710,1.824,1.938,2.052,2.166,2.280,2.394,2.508,2.622,2.736,2.850,
    2.964,3.078,3.192,3.306,3.420,3.534,3.648,3.762,3.876,3.990,4.104,4.218,4.332,4.446,4.560,4.674,4.788,4.902,5.016,5.130,5.244,5.358,5.472,5.586,5.700,
    5.814,5.928,6.042,6.156,6.270,6.384,6.498,6.612,6.726,6.840,6.954,7.068,7.182,7.296,7.410,7.524,7.638,7.752,7.866,7.980,8.094,8.208,8.322,8.436,8.550,
    8.664,8.778,8.892,9.006,9.120,9.234,9.348,9.462,9.576,9.690,9.804,9.918,10.032,10.146,10.260,10.374,10.488,10.602,10.716,10.830,10.944,11.058,11.172,11.286,11.400,
    11.514,11.628,11.742,11.856,11.970,12.084,12.198,12.312,12.426,12.540,12.654,12.768,12.882,12.996,13.110,13.224,13.338,13.452,13.566,13.680,13.794,13.908,14.022,14.136,14.250,
    14.364,14.478,14.592,14.706,14.820,14.934,15.048,15.162,15.276,15.390,15.504,15.618,15.732,15.846,15.960,16.074,16.188,16.302,16.416,16.530,16.644,16.758,16.872,16.986,17.100,
    17.214,17.328,17.442,17.556,17.670,17.784,17.898,18.012,18.126,18.240,18.354,18.468,18.582,18.696,18.810,18.924,19.038,19.152,19.266,19.380,19.494,19.608,19.722,19.836,19.950,
    20.064,20.178,20.292,20.406,20.520,20.634,20.748,20.862,20.976,21.090,21.204,21.318,21.432,21.546,21.660,21.774,21.888,22.002,22.116,22.230,22.344,22.458,22.572,22.686,22.800,
    22.914,23.028,23.142,23.256,23.370,23.484,23.598,23.712,23.826,23.940,24.054,24.168,24.282,24.396,24.510,24.624,24.738,24.852,24.966,25.080,25.194,25.308,25.422,25.536,25.650,
    25.764,25.878,25.992,26.106,26.220,26.334,26.448,26.562,26.676,26.790,26.904,27.018,27.132,27.246,27.360,27.474,27.588,27.702,27.816,27.930,28.044,28.158,28.272,28.386,28.500,
    28.614,28.728,28.842,28.956,29.070
    };
    
    // RGB
    int RgbToY(unsigned char *pRgb, int nWidth, int nHeight, unsigned char *pY)
    {
        unsigned char *pRgbTmp = pRgb;
        int nWidthBytes = nWidth * 3;
        for (int i = 0; i < nHeight; i++)
        {
            pRgbTmp = pRgb + i * nWidthBytes;
            for (int j = 0; j < nWidth; j++)
            {
                *pY = (int)(g_RGB2YUV_RTable[*pRgbTmp] + g_RGB2YUV_GTable[*(pRgbTmp + 1)] + g_RGB2YUV_BTable[*(pRgbTmp + 2)]);
    
                pRgbTmp += 3;
                pY++;
            }
        }
    
        return 0;
    }
    
    int RgbToY_Flip(unsigned char *pRgb, int nWidth, int nHeight, unsigned char *pY)
    {
        unsigned char *pRgbTmp = pRgb;
        int nWidthBytes = nWidth * 3;
        for (int i = 0; i < nHeight; i++)
        {
            pRgbTmp = pRgb + (nHeight - i - 1)* nWidthBytes;
            for (int j = 0; j < nWidth; j++)
            {
                *pY = (int)(g_RGB2YUV_RTable[*pRgbTmp] + g_RGB2YUV_GTable[*(pRgbTmp + 1)] + g_RGB2YUV_BTable[*(pRgbTmp + 2)]);
    
                pRgbTmp += 3;
                pY++;
            }
        }
    
        return 0;
    }
    
    // BGR
    int BgrToY( unsigned char *pRgb, int nWidth, int nHeight, unsigned char *pY )
    { 
        unsigned char *pRgbTmp = pRgb;
        int nWidthBytes = nWidth * 3;
        for (int i = 0; i < nHeight; i++)
        {
            pRgbTmp = pRgb + i * nWidthBytes;
            for (int j = 0; j < nWidth; j++)
            {
                *pY = (int)(g_RGB2YUV_RTable[*(pRgbTmp + 2)] + g_RGB2YUV_GTable[*(pRgbTmp + 1)] + g_RGB2YUV_BTable[*pRgbTmp]);
    
                pRgbTmp += 3;
                pY++;
            }
        }
    
        return 0;
    }
    
    int BgrToY_Flip(unsigned char *pRgb, int nWidth, int nHeight, unsigned char *pY)
    {
        unsigned char *pRgbTmp = pRgb;
        int nWidthBytes = nWidth * 3;
        for (int i = 0; i < nHeight; i++)
        {
            pRgbTmp = pRgb + (nHeight - i - 1)* nWidthBytes;
            for (int j = 0; j < nWidth; j++)
            {
                *pY = (int)(g_RGB2YUV_RTable[*(pRgbTmp + 2)] + g_RGB2YUV_GTable[*(pRgbTmp + 1)] + g_RGB2YUV_BTable[*pRgbTmp]);
    
                pRgbTmp += 3;
                pY++;
            }
        }
    
        return 0;
    }
    
    
    点赞 评论
  • wsssssssssssss 2018-04-28 05:47

    这几个函数是已经在实际使用的,我也测试过,没啥问题

    点赞 评论
  • 白色一大坨 2018-04-28 08:07

    打开图片获取数据区的函数:

        BITMAPFILEHEADER    BmpFileHeader;
        FILE* pFile;
        int ret = fopen_s(&pFile, lpszFileName, "rb+");
        if (ret != 0) return -1;
        fread(&BmpFileHeader, sizeof(BmpFileHeader), 1, pFile);
        if (BmpFileHeader.bfType != 0x4d42)                 return -1;
    
        LPBITMAPINFO pBmpInfo = NULL;
        pBmpInfo = (LPBITMAPINFO)new char[sizeof(BITMAPINFOHEADER)+256 * sizeof(RGBQUAD)];
        fread(pBmpInfo, BmpFileHeader.bfOffBits - sizeof(BITMAPFILEHEADER), 1, pFile);
        m_lWidth = pBmpInfo->bmiHeader.biWidth;
        m_lHeight = pBmpInfo->bmiHeader.biHeight;
        m_wBitCount = pBmpInfo->bmiHeader.biBitCount;
    
    
        int nLineByte = (m_lWidth * m_wBitCount + 31) / 32 * 4;
        int nDatalen = nLineByte * m_lHeight;
        if (m_pbtBmpBuf != NULL){ delete[] m_pbtBmpBuf; m_pbtBmpBuf = NULL; }
        m_pbtBmpBuf = new BYTE[nDatalen];
        BYTE    *pImageTemp = new BYTE[nDatalen];//图像数据区
        memset(pImageTemp, 0, nDatalen);
        fread(pImageTemp, nDatalen, 1, pFile);
        fclose(pFile);
        //调用转换函数
            delete[] pBmpInfo;
        pBmpInfo = NULL;
        delete[] pImageTemp;
        pImageTemp = NULL;
    
    点赞 评论

相关推荐 更多相似问题