程序如下:
是一个图像处理的程序,改变图像的色调:
//graphic.c
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
#define colorbit 256
static BITMAPFILEHEADER bitmapfileheader;
static BITMAPINFOHEADER bitmapinfoheader;
int8 pixel_data_24[1000][1000][3]; //不会声明三维的可变大小数组,暂时用1000*1000的数组
//前一个1000位横坐标,bgr顺序排列;
//无法从图片里读取pixeldata。now
int8 pixel_out[1000][1000];
RGBQUAD color[256];
void readfileheader(FILE * ptr);
void readinfoheader(FILE * ptr);
void readpixeldata24(FILE * ptr);
void importtothenewfile(FILE * src,FILE * dest);
void printpixeldata24(FILE * ptr);
void change_24_2_gray(void);
void creat_RGBQUARD(void);
int main (void)
{
FILE * gptrin;
FILE * gptrout;
FILE * debugout;
if((gptrin=fopen("graphictest.bmp","r"))==NULL){ //graphic.bmp
printf("file in open error\n");
exit(1);
}
if((gptrout=fopen("newbmp.bmp","w"))==NULL){
printf("file out open error\n");
exit(2);
}
debugout=fopen("debug.txt","w");
readfileheader(gptrin);
readinfoheader(gptrin);
switch(bitmapinfoheader.biBitCount)
{
case 24:readpixeldata24(gptrin);
printpixeldata24(debugout);
change_24_2_gray();
importtothenewfile(gptrin,gptrout);
break;
default :printf("sorry the image's bitcount is not 24,the function hasn't support it now\n");
}
fclose(gptrout);
fclose(gptrin);
return 0;
}
void creat_RGBQUARD()
{
int i;
for(i=0;i<256;i++)
{
color.rgbBlue=i;
color.rgbGreen=i;
color.rgbRed=i;
color.rgbReserved=0;
}
}
void change_24_2_gray()
{
int i,j,k;
creat_RGBQUARD();
for(i=bitmapinfoheader.biHeight-1;i>=0;i--)
{
for(j=0;j<bitmapinfoheader.biWidth;j++)
{
pixel_out[j]=(int8)(pixel_data_24[j][0]*0.11+pixel_data_24[j][1]*0.59+pixel_data_24[j][2]*0.3);
}
}
}
void printpixeldata24(FILE * ptr)
{
int i,j,k,num=0;
for(i=bitmapinfoheader.biHeight-1;i>=0;i--)
{
for(j=0;j<bitmapinfoheader.biWidth;j++)
{
for(k=0;k<3;k++)
{
num++;
fprintf(ptr,"%x ",pixel_data_24[j][k]);
if(num==9)
{
fprintf(ptr,"\r\n");
num=0;
}
}
}
}
}
void readpixeldata24(FILE * ptr)
{
int i,j,k,num=0;
fseek(ptr,bitmapfileheader.bfOffBits,0);
for(i=bitmapinfoheader.biHeight-1;i>=0;i--)
{
for(j=0;j<bitmapinfoheader.biWidth;j++)
{
for(k=0;k<3;k++)
{
fseek(ptr,bitmapfileheader.bfOffBits+num,0);
num++;
fread(&(pixel_data_24[j][k]),1,1,ptr);
//printf("%d ",pixel_data_24[j][k]);
}
}
}
//fread(&(pixel_data_24[0][0][0]),sizeof(int8),1,ptr);
printf("the pixel is %d\n",pixel_data_24[0][0][0]);
}
void importtothenewfile(FILE * src,FILE * dest)
{
int i,j,k,num=0;
fseek(dest,0,0);
bitmapfileheader.bfOffBits=1078; //256位调色板
bitmapinfoheader.biBitCount=8; //设置为256色
bitmapinfoheader.biClrUsed=256; //实际使用调色板256色
bitmapinfoheader.biClrImportant=256;
bitmapinfoheader.biSizeImage=bitmapinfoheader.biWidth*bitmapinfoheader.biHeight;
bitmapfileheader.bfSize=bitmapfileheader.bfOffBits+bitmapinfoheader.biSizeImage;
fwrite(&(bitmapfileheader),sizeof(BITMAPFILEHEADER),1,dest);
fwrite(&(bitmapinfoheader),sizeof(BITMAPINFOHEADER),1,dest);
fseek(dest,54,0);
fwrite(color,sizeof(RGBQUAD),256,dest);
fseek(dest,bitmapfileheader.bfOffBits,0);
for(i=bitmapinfoheader.biHeight-1;i>=0;i--)
{
for(j=0;j<bitmapinfoheader.biWidth;j++)
{
fseek(dest,bitmapfileheader.bfOffBits+num,0);
num++;
fwrite(&(pixel_out[j]),1,1,dest);
}
}
printf("the pixel's num import is %d\n",num);
}
void readfileheader(FILE * ptr)
{
fread(&bitmapfileheader,sizeof(BITMAPFILEHEADER),1,ptr);
printf("the graph style is %c %c ,the real pixel data is behind %d byte,the graph size is %d\n",\
bitmapfileheader.bfType[0],\
bitmapfileheader.bfType[1],bitmapfileheader.bfOffBits,bitmapfileheader.bfSize);
}
void readinfoheader(FILE * ptr)
{
fread(&bitmapinfoheader,sizeof(BITMAPINFOHEADER),1,ptr);
printf("this infoheader size is %d\nevery pixel need %d bit\nimportant number of color is %d\n\
graph really use %d kind of color\nthe number of compress is %d\nthe height of the graph is\
%d\nthe width of the graph is %d\nthe graph size is %d\n",\
bitmapinfoheader.biSize,bitmapinfoheader.biBitCount,bitmapinfoheader.biClrImportant,\
bitmapinfoheader.biClrUsed,bitmapinfoheader.biCompression,bitmapinfoheader.biHeight,\
bitmapinfoheader.biWidth,bitmapinfoheader.biSizeImage);
}
现在的问题是想在QT中实现一个界面,先显示一张图片,然后点击Pushbutton,通过该程序完成图片转换后,显示转换好的图片。
新手,不知从何处下手,还望指教!学习!O(∩_∩)O谢谢!