Quad Tree lossy bitmap Compression Handler example _c language

Source: Internet
Author: User
Tags fread int size

A quad tree lesson Set Program, you can compress the 24-bit graph, applied to the Windows platform.

Main.c

Copy Code code as follows:

#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, handle only 24-bit color
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 picture to a file

Fclose (pfin1);
Fclose (PFOUT1);
}
Unzip a picture and restore it
Openbmp (PFIN2,PFOUT2);
Fclose (pfin2);
Fclose (PFOUT2);
}
return 0;
}

Func.c

Copy Code code as follows:

#include "Bmp.h"


Pixel Valve value 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&LT;3//This function is used to determine whether the split picture size is width and height 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&GT;MAX.R) MAX.R=IMG[I*W+J].R;
else if (IMG[I*W+J].R&LT;MIN.R) MIN.R=IMG[I*W+J].R;
if (IMG[I*W+J].G&GT;MAX.G) max.g=img[i*w+j].g;
else if (IMG[I*W+J].G&LT;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 valve value is set to 0xc0;
{
flag=0;
return flag; Flag as a flag bit to decide whether to continue splitting the image
}
}
}
return flag;
}

Quadtree partition function (the argument for this function needs to be adjusted, especially the parameters of the range)!!!!!!!!!
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, then a recursive split
{
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 pixel in that range is 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

Copy Code code as follows:

#ifndef bmp_h_included
#define Bmp_h_included

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

/* Pixel Type * *
typedef struct{
BYTE b;
BYTE G;
BYTE R;
}rgb;

/* Four fork tree node type * *
typedef struct QUADNODE_T
{
RGB Pixel;
struct quadnode_t *sub[4];
}quadnode_t,*quadtree_t;

/* Pixel file storage structure * *
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.