c语言提取图片yiq分量,请教C语言处理图像的问题

复制内容到剪贴板

代码:#include

#include

typedef struct tagBITMAPFILEHEADER

{

unsigned short bfType;        //The map type of this file, bmp should be BM

unsigned long bfSize;        //Size of this file

unsigned short bfReserved1;

unsigned short bfReserved2;

unsigned long bfOffBits;        //The offset of the data in bytes

}BITMAPFILEHEADER;

typedef struct tagBIMAPINFOHEADER

{

unsigned long biSize;

unsigned long biWidth;        //Width of the picture

unsigned long biHeight;        //Height of the picture

unsigned short biPlanes;

unsigned short biBitCount;        //The bits used to represents a pixel

unsigned long biCompression;        //Type of compression

unsigned long biSizeImage;

unsigned long biXPelsPerMeter;

unsigned long biYPelsPerMeter;

unsigned long biClrUsed;

unsigned long biClrImportant;

}BITMAPINFOHEADER;

typedef struct tagRGBQUAD

{

unsigned char rgbBlue;

unsigned char rgbGreen;

unsigned char rgbRed;

unsigned char rgbReserved;

}RGBQUAD;

typedef struct tagBITMAPINFO

{

BITMAPINFOHEADER bmiHeader;

RGBQUAD bmiColors[256];

}BITMAPINFO;

void main(){

FILE *fp;

FILE *fp1;

char filename[50];        //The path of the file

BITMAPFILEHEADER bmpHeader1;

BITMAPINFO bmpHeader2;

//        RGBQUAD oldColor;

//        RGBQUAD        newColor;

unsigned int tempColor[3]={0,0,0},oldColor1[3],newColor1[3];        //Used in 24-bits map calculation

int oldIndex=0,newIndex=0;        //Get the user input of color index

int        Index161 = 0,Index162 =0;        //Seperate 1 byte into two parts, used in 16-bit map

int i;        //Counter

int colorValue=0;        //Index read out from the file

printf("\nPlease input the file path:");

gets(filename);

if((fp = fopen(filename,"rb")) == NULL){

printf("Failed tp open this file.\n");

exit(1);

}

fp1 = fopen("modified.bmp","wb");

/* The file header information*/

fread(&bmpHeader1.bfType,2,1,fp);

fwrite(&bmpHeader1.bfType,2,1,fp1);

fread(&bmpHeader1.bfSize,4,1,fp);

fwrite(&bmpHeader1.bfSize,4,1,fp1);

fread(&bmpHeader1.bfReserved1,2,1,fp);

fwrite(&bmpHeader1.bfReserved1,2,1,fp1);

fread(&bmpHeader1.bfReserved2,2,1,fp);

fwrite(&bmpHeader1.bfReserved2,2,1,fp1);

fread(&bmpHeader1.bfOffBits,4,1,fp);

fwrite(&bmpHeader1.bfOffBits,4,1,fp1);

/*The bitmap header information*/

fread(&bmpHeader2.bmiHeader.biSize,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biSize,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biWidth,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biWidth,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biHeight,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biHeight,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biPlanes,2,1,fp);

fwrite(&bmpHeader2.bmiHeader.biPlanes,2,1,fp1);

fread(&bmpHeader2.bmiHeader.biBitCount,2,1,fp);

fwrite(&bmpHeader2.bmiHeader.biBitCount,2,1,fp1);

fread(&bmpHeader2.bmiHeader.biCompression,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biCompression,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biSizeImage,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biSizeImage,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biXPelsPerMeter,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biXPelsPerMeter,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biYPelsPerMeter,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biYPelsPerMeter,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biClrUsed,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biClrUsed,4,1,fp1);

fread(&bmpHeader2.bmiHeader.biClrImportant,4,1,fp);

fwrite(&bmpHeader2.bmiHeader.biClrImportant,4,1,fp1);

switch(bmpHeader2.bmiHeader.biBitCount)

{

/*For the 2-bit map*/

case 1:

fseek(fp,54L,0);

for(i=0;i<8;i++)

{

fread(&colorValue,1,1,fp);

fwrite(&colorValue,1,1,fp1);

}

printf("\nThis image has 2 colors, which one do you want to change?(0 for black and 1 for white)");

while(scanf("%d",&oldIndex) != 1 || oldIndex != 0 || oldIndex != 1)

{

getchar();

printf("\nIt's an invalid value! Please input again:");

}

switch(oldIndex)

{

case 0:

while(!feof(fp))

{

fread(&colorValue,1,1,fp);

if(colorValue != 255)

colorValue = 255;

fwrite(&colorValue,1,1,fp1);

}

break;

case 1:

while(!feof(fp))

{

fread(&colorValue,1,1,fp);

if(colorValue != 0)

colorValue = 0;

fwrite(&colorValue,1,1,fp1);

}

break;

}

break;

/*For the 16-bit color map*/

case 4:

fseek(fp,54L,0);

printf("\nThis is the color palette of this image.");

printf("\nIndex Blue Green Red Reserved\n");

for(i=0;i<16;i++)

{

fread(&bmpHeader2.bmiColors[i].rgbBlue,1,1,fp);

fread(&bmpHeader2.bmiColors[i].rgbGreen,1,1,fp);

fread(&bmpHeader2.bmiColors[i].rgbRed,1,1,fp);

fread(&bmpHeader2.bmiColors[i].rgbReserved,1,1,fp);

fwrite(&bmpHeader2.bmiColors[i].rgbBlue,1,1,fp1);

fwrite(&bmpHeader2.bmiColors[i].rgbGreen,1,1,fp1);

fwrite(&bmpHeader2.bmiColors[i].rgbRed,1,1,fp1);

fwrite(&bmpHeader2.bmiColors[i].rgbReserved,1,1,fp1);

printf("%3d %4u %4u %4u %4u\n",i,bmpHeader2.bmiColors[i].rgbBlue,bmpHeader2.bmiColors[i].rgbGreen,bmpHeader2.bmiColors[i].rgbRed,bmpHeader2.bmiColors[i].rgbReserved);

}

printf("\nPlease input the index of the old color(from 0 to 255):");

while(scanf("%d",&oldIndex) !=1 || oldIndex>15 || oldIndex<0)

{

getchar();

printf("\nIt's an invalid value! Please input again:");

}

printf("\nPlease input the index of the new color(from 0 to 15):");

while(scanf("%d",&newIndex) !=1 || newIndex>15 || newIndex<0)

{

getchar();

printf("\nIt's an invalid value! Please input again:");

}

fseek(fp,bmpHeader1.bfOffBits,0);

while(!feof(fp))

{

fread(&colorValue,1,1,fp);

if(colorValue%16 == oldIndex)        //Get the first 4-bits of a byte

Index161 = newIndex;        //Check the index

else

Index161 = colorValue%16;

if(((colorValue-colorValue%16)/16) == oldIndex)        //Get the last 4-bits of a byte

Index162 = newIndex;

else

Index162 = (colorValue - colorValue%16)/16;

colorValue = Index162*16+Index161;

fwrite(&colorValue,1,1,fp1);

}

break;

/*For the 256-bit color map*/

case 8:

fseek(fp,54L,0);

printf("\nThis is the color palette of this image.");

printf("Index Blue Green Red Reserved\n");

for(i=0;i<256;i++)        //Read out the RGBQUAD information and display the palette

{

fread(&bmpHeader2.bmiColors[i].rgbBlue,1,1,fp);

fread(&bmpHeader2.bmiColors[i].rgbGreen,1,1,fp);

fread(&bmpHeader2.bmiColors[i].rgbRed,1,1,fp);

fread(&bmpHeader2.bmiColors[i].rgbReserved,1,1,fp);

fwrite(&bmpHeader2.bmiColors[i].rgbBlue,1,1,fp1);

fwrite(&bmpHeader2.bmiColors[i].rgbGreen,1,1,fp1);

fwrite(&bmpHeader2.bmiColors[i].rgbRed,1,1,fp1);

fwrite(&bmpHeader2.bmiColors[i].rgbReserved,1,1,fp1);

printf("%3d %4u %4u %4u %4u\n",i,bmpHeader2.bmiColors[i].rgbBlue,bmpHeader2.bmiColors[i].rgbGreen,bmpHeader2.bmiColors[i].rgbRed,bmpHeader2.bmiColors[i].rgbReserved);

}

printf("\nPlease input the index of the old color(from 0 to 255):");

while(scanf("%d",&oldIndex) !=1 || oldIndex>255 || oldIndex<0)

{

getchar();

printf("\nIt's an invalid value! Please input again:");

}

printf("\nPlease input the index of the new color(from 0 to 255):");

while(scanf("%d",&newIndex) !=1 || newIndex>255 || newIndex<0)

{

getchar();

printf("\nIt's an invalid value! Please input again:");

}

fseek(fp,bmpHeader1.bfOffBits,0);

while(!feof(fp))

{

fread(&colorValue,1,1,fp);

if(colorValue == oldIndex)

{

fwrite(&newIndex,1,1,fp1);

}

else

fwrite(&colorValue,1,1,fp1);

}

break;

/*For the 24-bit color map*/

case 24:

fseek(fp,54L,0);

/*Get the old and new RGB value of colors*/

printf("\nThis image has 224 colors, which one do you want to change?\n");

printf("Blue:");

scanf("%u",&oldColor1[2]);

printf("Green:");

scanf("%u",&oldColor1[1]);

printf("Red:");

scanf("%u",&oldColor1[0]);

printf("What is the new color?\n");

printf("Blue:");

scanf("%u",&newColor1[2]);

printf("Green:");

scanf("%u",&newColor1[1]);

printf("Red:");

scanf("%u",&newColor1[0]);

while(!feof(fp))

{

fread(&tempColor[0],1,1,fp);

fread(&tempColor[1],1,1,fp);

fread(&tempColor[2],1,1,fp);

if(tempColor[2] == oldColor1[2] && tempColor[1] == oldColor1[1] && tempColor[0] == oldColor1[0])

{

fwrite(&newColor1[0],1,1,fp1);

fwrite(&newColor1[1],1,1,fp1);

fwrite(&newColor1[2],1,1,fp1);

}

else

{

fwrite(&tempColor[0],1,1,fp1);

fwrite(&tempColor[1],1,1,fp1);

fwrite(&tempColor[2],1,1,fp1);

}

}

break;

default:

printf("This image is not supported by this program!");

break;

}

fclose(fp);

fclose(fp1);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值