📄 wavelet.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>#include "wavelet.h"#include <ctype.h>static int read_char(FILE *fp);static int read_int(FILE *fp);IntImage new_intimage(int width, int height){ IntImage image; image = (IntImage) calloc(1,sizeof(struct IntImage_struct)); if (image==NULL) goto error; image->data = (IntPixel*) calloc(width*height,sizeof(IntPixel)); if (image->data==NULL) goto error; image->width = width; image->height = height; image->size = width*height; image->bpp = 0; image->min_val = (IntPixel) 0; image->max_val = (IntPixel) 0; return image; error: err_SimpleMessage(err_GetErrorMessage(Error_NotEnoughMemory)); return NULL;}Image new_image(int width, int height){ Image image; image = (Image) calloc(1,sizeof(struct Image_struct)); if (image==NULL) goto error; image->data = (Pixel*) calloc(width*height,sizeof(Pixel)); if (image->data==NULL) goto error; image->width = width; image->height = height; image->size = width*height; image->bpp = 0; image->min_val = (Pixel) 0; image->max_val = (Pixel) 0; return image; error: err_SimpleMessage(err_GetErrorMessage(Error_NotEnoughMemory)); return NULL;}void free_intimage(IntImage img){ if (img) { if (img->data) free(img->data); free(img); }}void free_image(Image img){ if (img) { if (img->data) free(img->data); free(img); }}/************************************************************************ * Functionname: load_intimage * -------------------------------------------------------------------- * PARAMETER: * file: filename of image * max_val: scaling of grey values to [0..max_val] * * RETURN: * Image that shoud be loaded, if not possible return NULL * -------------------------------------------------------------------- * DESCRIPTION: * This function loads an IntImage (PGM, ASCII or binary * encoded (P5 or P3 format) ) from a file. ************************************************************************/IntImage load_intimage(char *file, int max_val){ IntImage img; FILE *fp; IntPixel *data; int width, height, i, max, ch1, ch2; fp=fopen(file,"rb"); if (fp==NULL) goto error; ch1=getc(fp); ch2=getc(fp); if (ch1!='P' || (ch2!='5' && ch2!='2')) goto error1; width=read_int(fp); height=read_int(fp); if ((width==0) || (height==0) ) goto error1; max=read_int(fp); img=new_intimage(width,height); img->bpp=8; data=img->data; for (i=0; i<img->size; i++) { if (ch2=='5') *data=getc(fp); else *data=read_int(fp); data++; } fclose(fp); return img; error1: err_SimpleMessage(err_GetErrorMessage(Error_WrongFileFormat)); return NULL; error: err_SimpleMessage(err_GetErrorMessage(Error_CantOpenFile)); return NULL;}/************************************************************************ * Functionname: load_image * -------------------------------------------------------------------- * PARAMETER: * file: filename of image * max_val: scaling of grey values to [0..max_val] * * RETURN: * Image that shoud be loaded, if not possible return NULL * -------------------------------------------------------------------- * DESCRIPTION: * This function loads an IntImage with load_intimage * and then converts to Image. ************************************************************************/extern Image load_image(char *file, int max_val){ Image img; IntImage intimg; intimg = load_intimage(file, max_val); if (!intimg) return NULL; img = intimage_to_image(intimg); if (!intimg) return NULL; return img;}/************************************************************************//* Functionname: save_image_P5 *//* -------------------------------------------------------------------- *//* Parameter: *//* img: Image that shoud be saved *//* file: filename of image *//* -------------------------------------------------------------------- *//* Description: save an image as PGM (P5 binary decoded) file *//* *//************************************************************************/extern int save_image_P5(char *file, Image img){ FILE *fp; Pixel *data; long i; int p; fp=fopen(file,"wb"); if (fp==NULL) goto error; fprintf(fp,"P5\n"); fprintf(fp,"%d %d\n%d ",img->width,img->height,255); data=img->data; for (i=0;i<img->size;i++) { p=floor(*data+0.5); if (p<0) p=0; if (p>255) p=255;/* putc(*data,fp); */ putc(p,fp); data++; } fclose(fp); return 1; error: err_SimpleMessage(err_GetErrorMessage(Error_CantOpenFile)); return 0;}void clear_image(Image img){ int i; PreCondition(img!=NULL,"image==NULL"); for (i=0;i<img->size;i++) (img->data)[i]=(Pixel) 0;}extern void copy_into_image(Image img1,Image img2,int x,int y)/* copy img2 into img1 at position (x,y)*/{ int start,i,j,aim; Pixel *temp; temp=img2->data; start=img1->width*y+x; for (i=0;i<img2->height;i++) { for (j=0;j<img2->width;j++) { aim=start+j+img1->width*i; img1->data[aim]=*temp; temp++; } }}extern void copy_into_intimage(IntImage img1,IntImage img2,int x,int y){/* copy img2 into img1 at position (x,y)*/ int start,i,j,aim; IntPixel *temp; temp=img2->data; start=img1->width*y+x; for (i=0;i<img2->height;i++) { for (j=0;j<img2->width;j++) { aim=start+j+img1->width*i; img1->data[aim]=*temp; temp++; } }}void copy_part_of_image_into_image(Image dest_img, int dest_x, int dest_y, Image src_img, int src_x, int src_y, int width, int height){ Pixel *sp,*dp; int y,siz; sp=get_pixel_adr(src_img,src_x,src_y); dp=get_pixel_adr(dest_img,dest_x,dest_y); siz=width*sizeof(Pixel); for (y=0;y<height;y++) { memcpy(dp,sp,siz); sp+=src_img->width; dp+=dest_img->width; }}extern void copy_part_of_image(Image img1,Image img2,int x,int y)/* copy part of img2 begining at position (x,y) into img1 */{ int i,j,width,height,start,step; Pixel *data; width=img1->width; height=img1->height; start=img2->width*y+x; data=img1->data; for (i=0;i<height;i++) { step=i*img2->width; for (j=0;j<width;j++){ *data=img2->data[start+j+step]; data++; } }}extern void scale_image(Image img,int maximum)/* scale image to [0..maximum]*/{ int i; Pixel max,min,multi; for (i=0;i<img->size;i++) { if (img->data[i]<min) min=img->data[i]; else if (img->data[i]>max) max=img->data[i]; } multi=(Pixel)maximum/(max-min); for (i=0;i<img->size;i++) img->data[i]=multi*(img->data[i]-min); img->min_val=0; img->max_val=maximum;}int string_to_pixel(char *str, Pixel *p){ float ppp; if (sscanf(str,"%f",&ppp)==1) { *p=(Pixel) ppp; return 1; } else { *p=0.0; return 0; }}Image intimage_to_image(IntImage i){ Image img; int j; img=new_image(i->width,i->height); if (img==NULL) goto error; for (j=0;j<i->size;j++) img->data[j]=(Pixel)i->data[j]; img->bpp=i->bpp; return img; error: err_SimpleMessage(err_GetErrorMessage(Error_NotEnoughMemory)); return NULL;}IntImage image_to_intimage(Image i){ IntImage img; int j,multi=1,max,min; img=(IntImage)calloc(1,sizeof(struct IntImage_struct)); if (img==NULL) goto error; img->data=(IntPixel *)calloc(i->size,sizeof(IntPixel)); if (img->data==NULL) goto error; img->width=i->width; img->height=i->height; img->size=i->size; img->bpp=i->bpp; max=i->max_val; min=i->min_val; if ((max-min)!=0) multi=255.0/(max-min); for (j=0;j<img->size;j++) img->data[j]=(int)((i->data[j]-min)*multi+0.5); return img; error: err_SimpleMessage(err_GetErrorMessage(Error_NotEnoughMemory)); return NULL; }static int read_char(FILE *fp)/*read a character from file, but skip any comments*/{ int ch; ch=getc(fp); if (ch=='#'){ do { ch=getc(fp); } while (ch!='\n' && ch!=EOF); } return ch;}static int read_int(FILE *fp)/*read an ascii integer from file, and skip leading tabstops,new lines ...*/{ int r,ch; do { ch=read_char(fp); } while (ch==' ' || ch=='\n' || ch=='\t'); if (ch<'0' || ch>'9') goto error; r=ch-'0'; while ( (ch=read_char(fp)) >='0' && (ch <= '9') ) { r*=10; r+=ch-'0'; } return r; error: return 0;}Image_tree new_image_tree(){ Image_tree t; t=(Image_tree) calloc(1,sizeof(struct Image_tree_struct)); t->entropy=0.0; t->image=NULL; t->coarse=t->horizontal=t->vertical=t->diagonal=t->doubletree=NULL; t->level=0; t->codec_data=NULL; t->significance_map=NULL; return t;}void free_image_tree(Image_tree t){ if (t->coarse) free_image_tree(t->coarse); if (t->horizontal) free_image_tree(t->horizontal); if (t->vertical) free_image_tree(t->vertical); if (t->diagonal) free_image_tree(t->diagonal); if (t->doubletree) free_image_tree(t->doubletree); if (t->image) free_image(t->image); if (t->significance_map) free_intimage(t->significance_map); if (t->codec_data) free(t->codec_data); t->image=NULL; free(t);}/*********************************************************************** * Functionname: get_image_infos * -------------------------------------------------------------------- * Parameter: * Image image: input image * Pixel *min,*max,*avg,*var: return minimum, maximum, * average and variance of current image * -------------------------------------------------------------------- * Description: * get statistical information of Image ************************************************************************/void get_image_infos(Image image, Image_info info){ int x,y; Pixel p,sp,sp2; sp=sp2=(Pixel)0.0; p=get_pixel(image,0,0); info->min=info->max=p; for (y=0;y<image->height;y++) for (x=0;x<image->width;x++) { p=get_pixel(image,x,y); info->max=MAX(info->max,p); info->min=MIN(info->min,p); sp+=p; sp2+=p*p; } sp=sp/image->width/image->height; sp2=sp2/image->width/image->height; info->mean=sp; info->var=sp2-sp*sp; info->rms=sqrt(sp2);}/*********************************************************************** * Functionname: get_difference_image * -------------------------------------------------------------------- * Parameter: * Image image1, image 2: input images * * Return: * Image : difference of image1 and image 2, * NULL if error occurs ************************************************************************/Image get_difference_image(Image image1, Image image2){ Image diff; int i,max,w,h; Pixel *d,*i1,*i2; if ((!image1) || (!image2)) return NULL; w=image1->width; h=image1->height; if (image2->width != w || image2->height != h) return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -