Example quadtree lossy bitmap compression handler
- 2020-04-02 02:19:23
- OfStack
A quadtree program, can be compressed to 24 bitmap, applied to the Windows platform.
The 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);
//To simplify the code, only 24 bits of color are processed
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);
//Save the modified image to a file
fclose(pfin1);
fclose(pfout1);
}
//Unzip the picture and restore it
openbmp(pfin2,pfout2);
fclose(pfin2);
fclose(pfout2);
}
return 0;
}
C func...
#include "bmp.h"
//Pixel threshold function
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) //This function is used to determine whether the size of the segmented image is 1 in width and height.
{
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)) //The threshold value is set to 0xc0;
{
flag=0;
return flag; //Flag is the flag bit that decides whether to continue to segment the image
}
}
}
return flag;
}
//Quadtree partition function (the function arguments to be adjusted, especially the scope of the several parameters)!!
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) //If the flag bit is false, recursive segmentation is performed
{
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 //If the flag bit is true, the pixels in the range are unified
{
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;
}
//Image decompression function
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