• 4932阅读
  • 2回复

[提问]【请问QT如何实现一个简单的图像处理功能?已经写好了图像处理部分的程序】 [复制链接]

上一主题 下一主题
 

只看楼主 倒序阅读 楼主  发表于: 2015-04-09
程序如下:
是一个图像处理的程序,改变图像的色调:

//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谢谢!


只看该作者 1楼 发表于: 2015-04-09
对了,这是bmp.h:

/*bmp.h*/<br/><br/>#ifndef bmp<br/>#define bmp<br/>#pragma pack(1)  //规定struct对齐为1字节,防止内存对齐<br/>/*****************<br/>移植用变量大小标定<br/>******************/<br/>typedef unsigned short int16;<br/>typedef unsigned int int32;<br/>typedef unsigned char int8;<br/><br/>/*********************<br/>bmp图像的文件头<br/>**********************/<br/>typedef struct tagBITMAPFILEHEADER<br/>{<br/>    int8 bfType[2];//位图文件的类型,必须为BM(1-2字节)<br/>    int32 bfSize;//位图文件的大小,以字节为单位(3-6字节,低位在前)<br/>    int16 bfReserved1;//位图文件保留字,必须为0(7-8字节)<br/>    int16 bfReserved2;//位图文件保留字,必须为0(9-10字节)<br/>    int32 bfOffBits;//位图数据的起始位置,以相对于位图(11-14字节,低位在前)<br/>    //文件头的偏移量表示,以字节为单位<br/>}BITMAPFILEHEADER;<br/><br/>/****************<br/>bmp图像的信息头<br/>*****************/<br/><br/>typedef struct tagBITMAPINFOHEADER{<br/>    int32 biSize;//本结构所占用字节数(15-18字节)<br/>    int32 biWidth;//位图的宽度,以像素为单位(19-22字节)<br/>    int32 biHeight;//位图的高度,以像素为单位(23-26字节)<br/>    int16 biPlanes;//目标设备的级别,必须为1(27-28字节)<br/>    int16 biBitCount;//每个像素所需的位数,必须是1(双色),(29-30字节)<br/>    //4(16色),8(256色)16(高彩色)或24(真彩色)之一<br/>    int32 biCompression;//位图压缩类型,必须是0(不压缩),(31-34字节)<br/>    //1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一<br/>    int32 biSizeImage;//位图的大小(其中包含了为了补齐行数是4的倍数而添加的空字节),以字节为单位(35-38字节)<br/>    int32 biXPelsPerMeter;//位图水平分辨率,每米像素数(39-42字节)<br/>    int32 biYPelsPerMeter;//位图垂直分辨率,每米像素数(43-46字节)<br/>    int32 biClrUsed;//位图实际使用的颜色表中的颜色数(47-50字节)<br/>    int32 biClrImportant;//位图显示过程中重要的颜色数(51-54字节)<br/>}BITMAPINFOHEADER;<br/><br/>/*******************<br/>bmp图像的调色板<br/>********************/<br/><br/>typedef struct tagRGBQUAD{<br/>    int8 rgbBlue;//蓝色的亮度(值范围为0-255)<br/>    int8 rgbGreen;//绿色的亮度(值范围为0-255)<br/>    int8 rgbRed;//红色的亮度(值范围为0-255)<br/>    int8 rgbReserved;//保留,必须为0<br/>}RGBQUAD;<br/><br/>/*typedef struct tagBITMAPINFO{<br/>    BITMAPINFOHEADERbmiHeader;//位图信息头<br/>    RGBQUADbmiColors[1];//颜色表<br/>}BITMAPINFO;*/<br/><br/>#endif<br/><br/>

只看该作者 2楼 发表于: 2015-04-12
      
快速回复
限100 字节
 
上一个 下一个