⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bcw3.c

📁 基于小波的图像压缩
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crblib/inc.h>

#define SHIFT(x)		((x)>>15)

static void do_tdec_line(int * to,int *from,int len)
{
int x,*ptr,*low,*high,half,A;

	if ( len <= 2 ) errexit("special-cased to len > 2 , scumbag");
	if ( len&1 ) errexit("len shouldn't be odd");

	half = len>>1;

	ptr = from;
	low = to;	high = to + half;
	for(x=0;x<half;x++) {
		if ( x == 0 || x== (half-1) || x== (half-2) )  {
			A = SHIFT( 19195 * ptr[0] );
		} else {
			A = SHIFT( ( 18432 * ptr[2] + 4859 * ptr[0] ) - ((ptr[-2] + ptr[4])<<11) );
		}

		*high = ptr[1] - A;
		*low  = ptr[0] + *high;
		*high -= SHIFT( 9598 * (*low) );
		ptr += 2; low++; high++;
	}

	low = to;	high = to + half;
	for(x=0;x<half;x++) {
		if ( x == 0 || x == 1 || x== (half-1) )  {
			A = SHIFT( 13573 * high[0] );
		} else {
			A = SHIFT( ( 27909 * high[0] - 18432 * high[-1] ) + ((high[1] + high[-2])<<11) );
		}

		*low++  -= A;
		high++;
	}
}

static void un_tdec_line(int *to,int *from,int len)
{
int x,*ptr,*low,*high,half,A;
	half = len>>1;

	low = from;	high = from + half;
	for(x=0;x<half;x++) {
		if ( x == 0 || x == 1 || x== (half-1) )  {
			A = SHIFT( 13573 * high[0] );
		} else {
			A = SHIFT( ( 27909 * high[0] - 18432 * high[-1] ) + ((high[1] + high[-2])<<11) );
		}

		*low++  += A;
		high++;
	}

	ptr = to;
	low = from;	high = from + half;
	for(x=0;x<half;x++) {
		*high += SHIFT( 9598 * (*low) );
		ptr[0] = *low  - *high;
		ptr += 2; low++; high++;
	}

	ptr = to;
	low = from;	high = from + half;
	for(x=0;x<half;x++) {
		if ( x == 0 || x== (half-1) || x== (half-2) )  {
			A = SHIFT( 19195 * ptr[0] );
		} else {
			A = SHIFT( ( 18432 * ptr[2] + 4859 * ptr[0] ) - ((ptr[-2] + ptr[4])<<11) );
		}
		ptr[1] = *high + A;
		ptr += 2; low++; high++;
	}
}

void bcw3_2D(int **rows, int width, int height, int levels,bool inverse)
{
int x, y, w, h, l;
int *buffer,*tempbuf,*temprow;

    if (width%(1 << (levels+1)) || height%(1 << (levels+1)))
		errexit("width and height must be divisible by 2^(levels+1)");
  
    /* Allocate a work array (for transposing columns) */
    
   	if ( (buffer = newarray(int,height+max(width,height)+height)) == NULL )
		errexit("malloc failed");
	temprow = buffer+height;
	tempbuf = buffer+height+height;

	if ( !inverse ) {

		for (l = 0; l < levels; l++) {
			w = width >> l;
			h = height >> l;
      
			/* Rows */
	
			do_tdec_line(temprow,rows[h-1],w);
			for (y = h-2; y >=0; y--) {
				do_tdec_line(rows[y+1],rows[y],w);
			}
	
			/* Columns */
	
			for (x = 0; x < w; x++) {
					for (y = 1; y < h; y++) buffer[y-1] = rows[y][x];
					buffer[h-1] = temprow[x];
				do_tdec_line(tempbuf,buffer,h);
					for (y = 0; y < h; y++) rows[y][x] = tempbuf[y];
			}
		}

    } else {

		for (l = levels-1; l >= 0; l--) { /** backwards in scale **/
			w = width >> l;
			h = height >> l;

			/* Columns */
	
			for (x = 0; x < w; x++) {
					for (y = 0; y < h; y++) buffer[y] = rows[y][x];
				un_tdec_line(tempbuf,buffer,h);
					for (y = 0; y < h-1; y++) rows[y+1][x] = tempbuf[y];
					temprow[x] = tempbuf[h-1];
			}

			/* Rows */
			for (y = 0; y < h-1; y++) {
				un_tdec_line(rows[y],rows[y+1],w);
			} 
			un_tdec_line(rows[h-1],temprow,w);
		}
	}

	free(buffer);
}


void bcwQuad(int *band,int w,int h,int fullw,bool inverse)
{
int x, y;
int *buffer,*tempbuf,*bptr,*temprow;

   	if ( (buffer = newarray(int,h+h+max(w,h))) == NULL ) {
		errputs("malloc failed"); exit(10);
	}
	temprow = buffer+h;
	tempbuf = buffer+h+h;
  
	if ( !inverse ) { /* forward transform. */
	
		bptr = band + (h-1)*fullw;
		do_tdec_line(temprow,bptr,w);
		for (y = (h-1); y--;) {
			bptr -= fullw;
			do_tdec_line(bptr+fullw,bptr,w);
		}
    
		for (x = 0; x < w; x++) {
			bptr = band + x + fullw;
			for (y = 0; y < (h-1); y++) { buffer[y] = *bptr; bptr += fullw; }
			buffer[h-1] = temprow[x];
			do_tdec_line(tempbuf,buffer,h);
			bptr = band + x;
			for (y = 0; y < h; y++) { *bptr = tempbuf[y]; bptr += fullw; }
		}

    } else {

		for (x = 0; x < w; x++) {
			bptr = band + x;
			for (y = 0; y < h; y++) { buffer[y] = *bptr; bptr += fullw; }
			un_tdec_line(tempbuf,buffer,h);
			bptr = band + x + fullw;
			for (y = 0; y < h-1; y++) { *bptr = tempbuf[y]; bptr += fullw; }
			temprow[x] = tempbuf[h-1];
		}

		bptr = band;
		for (y = (h-1); y--; ){
			un_tdec_line(bptr,bptr+fullw,w);
			bptr += fullw;
		}
		un_tdec_line(bptr,temprow,w);

	}

	free(buffer);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -