四叉樹有損位元影像壓縮處理常式樣本_C 語言

來源:互聯網
上載者:User

一個四叉樹課設程式,可以對24位元影像進行壓縮,應用於windows平台。

main.c

複製代碼 代碼如下:

#include "bmp.h"


int main()
{
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
FILE* pfin1 =fopen("test.bmp","rb");
FILE* pfout1 = fopen("test1.dat" , "wb");
FILE* pfout2 = fopen("test2.bmp" , "wb");
FILE* pfin2 =fopen("test1.dat","rb");
quadtree_t T=NULL;
//Read the Bitmap file header;
fread(&fileHeader,sizeof(BITMAPFILEHEADER),1,pfin1);
//Read the Bitmap info header;
fread(&infoHeader,sizeof(BITMAPINFOHEADER),1,pfin1);
//為簡化代碼,只處理24位彩色
if( infoHeader.biBitCount == 24 )
{
//int size = infoHeader.biWidth*infoHeader.biHeight;
int size = infoHeader.biWidth*infoHeader.biHeight;
RGB *img=NULL;
img=(RGB*)malloc(infoHeader.biHeight*infoHeader.biWidth*sizeof(RGB));
if(img!=NULL)
{
fread( img , sizeof(RGB) , size , pfin1 );
fwrite( &fileHeader , sizeof(fileHeader) , 1 , pfout1 );
fwrite( &infoHeader , sizeof(infoHeader) , 1 , pfout1 );
treediv(&T,0,infoHeader.biWidth-1,0,infoHeader.biHeight-1,pfout1,img,infoHeader.biWidth);
free(img);
//將修改後的圖片儲存到檔案

fclose(pfin1);
fclose(pfout1);
}
//將圖片解壓後還原
openbmp(pfin2,pfout2);
fclose(pfin2);
fclose(pfout2);
}
return 0;
}

func.c

複製代碼 代碼如下:

#include "bmp.h"


//像素閥值函數
int ComparePixel(short int width1,short int width2,short int height1,short int height2,RGB *img,short int W)
{
    RGB MAX,MIN;
    int flag,i,j,clr1,clr2;
    clr1=width2-width1;
    clr2=height2-height1;
    if(clr1<3||clr2<3)  //此函數用於判斷分割的圖片大小是否寬度與高度為1;
    {
        flag=1;
        return flag;
    }
    MAX.b=MIN.b=img[height1*W+width1].b;
    MAX.g=MIN.b=img[height1*W+width1].g;
    MAX.r=MIN.b=img[height1*W+width1].r;
    flag=1;
    for(i=height1;i<=height2;i++)
    {
        for(j=width1;j<=width2;j++)
        {
            if(img[i*W+j].r>MAX.r) MAX.r=img[i*W+j].r;
            else if(img[i*W+j].r<MIN.r) MIN.r=img[i*W+j].r;
            if(img[i*W+j].g>MAX.g) MAX.g=img[i*W+j].g;
            else if(img[i*W+j].g<MIN.g) MIN.g=img[i*W+j].g;
            if(img[i*W+j].b>MAX.b) MAX.b=img[i*W+j].b;
            else if(img[i*W+j].b<MIN.b) MIN.b=img[i*W+j].b;
            if((MAX.r-MIN.r>0x14)||(MAX.g-MIN.g>0x14)||(MAX.b-MIN.b>0x14)) //閥值設為0xc0;
            {
                flag=0;
                return flag;                                     //flag為標誌位,決定是否繼續分割映像
            }
        }
    }
    return flag;
}

//四叉樹分割函數(該函數的實參有待調整,特別是範圍那幾個參數)!!!!!!!!!
int treediv(quadtree_t *T,short int width1,short int width2,short int height1,short int height2,FILE* S,RGB *img,short int W)
{
    int flag=0;
    RGB *div;
    div=img;
    short int x1=width1,x2=width2;
    short int y1=height1,y2=height2;
    int w=W;
    flag=ComparePixel(x1,x2,y1,y2,div,w);
    if(!((*T) = (quadnode_t*)malloc(sizeof(quadnode_t))))
        return 0;
    if(!flag)                   //若標誌位為假,則進行遞迴分割
    {
        treediv(&((*T)->sub[0]),width1,(width1+width2)/2,height1,(height1+height2)/2,S,div,w);
        treediv(&((*T)->sub[1]),(width1+width2)/2+1,width2,height1,(height1+height2)/2,S,div,w);
        treediv(&((*T)->sub[2]),(width1+width2)/2+1,width2,(height1+height2)/2+1,height2,S,div,w);
        treediv(&((*T)->sub[3]),width1,(width1+width2)/2,(height1+height2)/2+1,height2,S,div,w);
    }
    else //如果標誌位為真,則將該範圍內像素統一
    {
        st.rgb.r=(*T)->pixel.r=(img[width1+height1*W].r+img[width2+height2*W].r)/2;
        st.rgb.g=(*T)->pixel.g=(img[width1+height1*W].g+img[width2+height2*W].g)/2;
        st.rgb.b=(*T)->pixel.b=(img[width1+height1*W].b+img[width2+height2*W].b)/2;
        st.x1=width1; st.x2=width2; st.y1=height1; st.y2=height2;
        fwrite( &st , sizeof(BLOCK) , 1 , S );
        *T=NULL; free(*T);
        //num++;
    }
    return 0;
}

//映像解壓函數
void openbmp(FILE *S1,FILE *S2)
{
    BITMAPFILEHEADER fileHeader;
    BITMAPINFOHEADER infoHeader;
    fread(&fileHeader,sizeof(BITMAPFILEHEADER),1,S1);
    fread(&infoHeader,sizeof(BITMAPINFOHEADER),1,S1);
    BLOCK sti;
    int p,q;
    int size = infoHeader.biWidth*infoHeader.biHeight;
    //RGB pic[infoHeader.biHeight][infoHeader.biWidth];
    RGB *pic;
    pic=(RGB*)malloc(infoHeader.biHeight*infoHeader.biWidth*sizeof(RGB));
    while(!feof(S1))
    {
        fread(&sti,sizeof(BLOCK),1,S1);
        int w1=sti.x1;int w2=sti.x2;
        int h1=sti.y1;int h2=sti.y2;
        for(p=h1;p<=h2;p++)
        {
            for(q=w1;q<=w2;q++)
            {
                pic[p*infoHeader.biWidth+q].b=sti.rgb.b;
                pic[p*infoHeader.biWidth+q].g=sti.rgb.g;
                pic[p*infoHeader.biWidth+q].r=sti.rgb.r;
            }
        }
    }
    fwrite( &fileHeader , sizeof(fileHeader) , 1 , S2 );
    fwrite( &infoHeader , sizeof(infoHeader) , 1 , S2 );
    fwrite(pic,sizeof(RGB),size,S2);
}

bmp.h

複製代碼 代碼如下:

#ifndef BMP_H_INCLUDED
#define BMP_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

/*像素類型*/
typedef struct{
BYTE b;
BYTE g;
BYTE r;
}RGB;

/*四叉樹結點類型*/
typedef struct quadnode_t
{
    RGB pixel;
    struct quadnode_t *sub[4];
}quadnode_t,*quadtree_t;

 

/*像素檔案儲存體結構*/
typedef struct block
{
    RGB rgb;
    short int x1,x2,y1,y2;
}BLOCK;

BLOCK st;

//static int num=0;

int treediv(quadtree_t *T,short int width1,short int width2,short int height1,short int height2,FILE* S,RGB *img,short int W);
int ComparePixel(short int width1,short int width2,short int height1,short int height2,RGB *img,short int W);
void openbmp(FILE *S1,FILE *S2);
#endif // BMP_H_INCLUDED

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.