求助!C语言写的Bmp转YUV图片再转回来
//
// main.c
// RGB
//
// Created by wangyi on 11-9-24.
// Copyright 2011年 ZheJiang University. All rights reserved.
//
#ifndef BMP_FILE_BMP_H
#define BMP_FILE_BMP_H
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef unsigned char BYTE;
#pragma pack(1)
/* bmp file head */
typedef struct BMP_FILE_HEADER
{
WORD bType;
DWORD bSize;
WORD bReserved1;
WORD bReserved2;
DWORD bOffset;
} BMPFILEHEADER;
/* bmp information head */
typedef struct BMP_INFO
{
DWORD bInfoSize;
long bWidth;
long bHeight;
WORD bPlanes;
WORD bBitCount;
DWORD bCompression;
DWORD bmpImageSize;
long bXPelsPerMeter;
long bYPelsPerMeter;
DWORD bClrUsed;
DWORD bClrImportant;
} BMPINF;
/*RGB_QUAD*/
typedef struct RGB_QUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReversed;
} RGBQUAD;
void openbmp(char *name);
void rgb2Yuv(long int height,long int width);
int exportbmp(char *name,BYTE *array);
int getthreshold(BYTE *array);
#pragma pack()
//////////////////////////////////////////////////////////////////////////
BMPFILEHEADER fileheader;
BMPINF infoheader;
//////////////////////////////////////////////////////////////////////////
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long int width,height; //图像的宽度、高度
int bitcount; //每个像素的位数
BYTE *array; //存放RGB数据变量
void openbmp(char *name)
{
FILE *fp;
unsigned char zero[4];
memset(zero,0,4);
if(name==NULL)
return;
if((fp=fopen(name, "rb"))==NULL)
{
printf("File open error\n");
return;
}
else
{
fread(&fileheader, sizeof(fileheader), 1, fp);
fread(&infoheader, sizeof(infoheader), 1, fp);
width=infoheader.bWidth;
height=infoheader.bHeight;
bitcount=infoheader.bBitCount;//bit per pixel
printf("width is %d height is %d bitcount %d",width,height,bitcount);
//adding the whole byte
int linebyte=(int)((width*bitcount/8+3)/4*4); //保证每行byte数为4的倍数
fseek(fp, fileheader.bOffset, 0);// change fp
array=(BYTE *)malloc(sizeof(BYTE)*width*height*bitcount/8); //allocate enough bytes
if(array==NULL)
{
printf("out of space");
return;
}
for ( int ni = 0 ; ni < height ; ni ++ )
{
for ( int nj = 0 ; nj < width ; nj ++ )
{
fread( array+ ((ni * width) + nj) *bitcount/8 , 1, 1, fp ) ;
fread( array+ ((ni * width) + nj) *bitcount/8 + 1, 1, 1, fp ) ;
fread( array + ((ni * width) + nj) *bitcount/8 + 2, 1, 1, fp ) ;
}
//deal with extra line byte
if (linebyte-width*bitcount/8>0) {
fread(zero, sizeof(unsigned char), (linebyte-width*bitcount/8), fp);
}
}
fclose(fp);
fp=NULL;
}
}
void rgb2Yuv(long int height,long int width)
{
int i,j;
// int threshold;
BYTE R,G,B;
for (i=0; i<height; i++) {
for (j=0; j<width; j++) {
//read RGB ->YUV
R=array[i*width+j];
G=array[i*width+j+1];
B=array[i*width+j+2];
BYTE Y= (BYTE)(0.3*R + 0.59*G + 0.11*B); // RGB->YUV
BYTE U= (BYTE)((B-Y) * 0.493);
BYTE V= (BYTE)((R-Y) * 0.877);
Y+=250;// threshold=(int)getthershold(array); //Y+250;
B= (BYTE)(Y + 2.032*U);
G= (BYTE)(Y - 0.394*U - 0.581*V);
R= (BYTE)(Y + 1.140*V);
array[i*width+j]=R;
array[i*width+j+1]=G;
array[i*width+j+2]=B;
}
}
}
int exportbmp(char *name,BYTE *array)
{
int ni,nj;
unsigned long int size=0;
if(!array)
return 0;
unsigned char zero[4];
memset(zero,0,4);
FILE *fp=fopen(name,"wb");
if(fp==NULL)
{
printf("cannot write bmp");
return 0;
}
int linebyte=(int)((width*bitcount/8+3)/4*4);
size= fwrite(&fileheader, sizeof(fileheader), 1, fp);
size= fwrite(&infoheader, sizeof(infoheader), 1, fp);
for (ni=0; ni<height; ni++) {
for (nj=0; nj<width; nj++) {
fwrite(array+(ni*width+nj)*bitcount/8, 1, 1, fp);
fwrite(array+(ni*width+nj)*bitcount/8+1, 1, 1, fp);
fwrite(array+(ni*width+nj)*bitcount/8+2, 1, 1, fp);
}
if(linebyte-width*bitcount/8>0)
{
fwrite(array+ ((ni * width) + nj) * bitcount/8 + 3, sizeof(unsigned char),
(linebyte - width* bitcount/8), fp);
}
}
/*
BMPFILEHEADER fileHead; //…í???a?o??o??∑Ω·ππ±‰??£¨?ó–¥??o??∑–≈?¢
fileHead.bType = 0x4D42; //bmp??–?
//bfSize???o?ò??o?4∏??è≥…≤?∑÷÷?∫?
fileHead.bSize= sizeof(BMPFILEHEADER) + sizeof(BMPINF) + colorTablesize + lByte*height;
fileHead.bReserved1 = 0;
fileHead.bReserved2 = 0;
fileHead.bOffset=54+colorTablesize; //bfOffBits???o?ò??o??∞??∏?≤?∑÷à?–??’o‰÷?∫?
fwrite(&fileHead, sizeof(BMPINF),1, fp); //–¥??o??∑Ωˉ??o?
BMPINF head; //…í???a?o–≈?¢?∑Ω·ππ±‰??£¨?ó–¥–≈?¢?∑–≈?¢
head.bBitCount=bitcount;
head.bClrImportant=0;
head.bClrUsed=0;
head.bCompression=0;
head.bHeight=height;
head.bPlanes=1;
head.bInfoSize=40;
head.bmpImageSize=lByte*height;
head.bWidth=width;
head.bXPelsPerMeter=0;
head.bYPelsPerMeter=0;
fwrite(&head, sizeof(BMPINF),1, fp); //–¥?a?o–≈?¢?∑Ωˉ??o?
fwrite(array, height*lByte, 1, fp); //–¥?a?o????Ωˉ??o?
*/
fclose(fp);
fp=NULL;
//π?±’??o?
return 1;
}
/*
int getthreshold(BYTE *array)
{
}*/
int main (int argc, const char * argv[])
{
char img_in[100];
char img_out[100];
scanf("%s",img_in);
openbmp(img_in);
getchar();
rgb2Yuv(height,width);
scanf("%s",img_out);
exportbmp(img_out,array);
free(array);
return 0;
}
自己写的一个程序,有点乱,一直检查不出有什么问题