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

📄 main.c

📁 傅立叶变换和小波变换是图像压缩的重要工具。该代大戏是利用小波变换进行图像压缩。
💻 C
📖 第 1 页 / 共 2 页
字号:

/*****

<> File IO todos :
	1. BGR/RGB option

*****/

#include "wave.h"
#include "image.h"
#include "codeimage.h"
#include "mathutil.h"
#include "coder.h"
#include "transform.h"
#include <math.h>
#include <crblib/fileutil.h>
#include <crblib/strutil.h>
#include <crblib/tsc.h>

#include <windows.h>
#include <drawdriver/drawdriver.h>

//---------------------------------------

static void DoMessages(HWND hwnd);
static bool IsKeyDown(int KeyCode);
int CountBmps(const char * bmpNameBase,int * pbaseN);
bool ExistsBmp(const char * bmpNameBase,int i,char * foundName);

typedef struct
{
	int y,Width;
	Driver_Window * Window;
} DrawDriverContext;

bool DrawDriver_GiveLinesCB(DrawDriverContext *Context, ubyte *LineData, int NumLines)
{	
	assert( (int)Context->Window->Width >= Context->Width );

	while(NumLines--)
	{
	uword * ptr;
	int x;
		if ( Context->y >= (int)Context->Window->Height )
			return false;

		assert( Context->Window->Stride >= Context->Window->Width * 2 );
		ptr = (uword *)(Context->Window->Buffer + (Context->y * Context->Window->Stride));
		
		switch( Context->Window->Mode )
		{
		case DRIVER_MODE_555:
			for(x = Context->Width;x--;)
			{
			ubyte b,g,r;
				b = *LineData++;
				g = *LineData++;
				r = *LineData++;
				*ptr++ = (((uword)r>>3)<<10) | (((uword)g>>3)<<5) | (((uword)b>>3)) ;
			}
			break;
		case DRIVER_MODE_565:
			for(x = Context->Width;x--;)
			{
			ubyte b,g,r;
				b = *LineData++;
				g = *LineData++;
				r = *LineData++;
				*ptr++ = (((uword)r>>3)<<11) | (((uword)g>>2)<<5) | (((uword)b>>3)) ;
			}
			break;
		}

		Context->y ++;
	}

return true;
}


#ifdef WINAPP //{

BOOL ParseCommandLine( char *CmdLine, char ***Argv, int *Argc );

#pragma warning(disable : 4028)
int PASCAL WinMain( HINSTANCE instance, HINSTANCE prev_inst, LPSTR cmdLine,
                    int cmdShow )
{
#pragma warning(default : 4028)

char **argv;
int argc;

#else //}{

int main(int argc,char *argv[])
{
HINSTANCE instance = 0;

#endif //} WINAPP

char *fromName,*toName;
FILE *streamFP,*compFP;
Image * im,viewIm;
int totCompLen,frameLen,fps,offset;
int width,height,streamLen,f,frames;
int targetLen,compBufLen,frameCompLen;
float targetRMSE;
ubyte * compBuf = NULL;
double spf,decTime,totTime;
bool encoding,seekByRMSE;
HWND viewer = NULL;
CodeImager * encoder;
CodeImager * decoder;
DrawDriverContext DDC;
bool fullscreen;
bool paused,nodisplay;
char * dumpRawName = NULL;
FILE * dumpRawFP = NULL;
bool bmpsIn,dumpBmps;
char bmpNameBase[1024];
int bmpBaseN;
	
	//---------------------------------------

#ifdef WINAPP //{
	ParseCommandLine(cmdLine,&argv,&argc);
#endif

	dprintf("Wave (Video) v0.6 by Charles Bloom, Sept-Dec of 1999\n");
	printf("  compiled %s,%s\n",__DATE__,__TIME__);

	fromName = toName = NULL;
	width = height = 0;
	targetLen = 4096;
	targetRMSE = 0;
	fps = 20;
	offset = 0;
	seekByRMSE = false;
	fullscreen = false;
	paused = false;
	nodisplay = false;
	dumpRawName = NULL;

	//---------------------------------------
	// process options

	argv++; argc--;
	while(argc>0)
	{
	char * str;
		str = *argv++; argc--;
		if ( *str == '-' || *str == '/' )
		{
			str++;
			switch(toupper(*str++))
			{
				case 'K':
				{
				float k;
					k = atof(str);
					dprintf("got option : target = %1.1f k\n",k);
					targetLen = (int)(k * 1024 + 0.5f);
					break;
				}
				case 'L':
				{
				int b;
					b = atoi(str);
					dprintf("got option : target = %d k\n",b);
					targetLen = b;
					break;
				}
				case 'E':
					targetRMSE = atof(str);
					seekByRMSE = true;
					dprintf("got option : target rmse = %f\n",targetRMSE);
					break;
				case 'F':
					fps = atoi(str);
					dprintf("got option : fps = %d\n",fps);
					break;
				case 'O':
					offset = atoi(str);
					dprintf("got option : offset = %d\n",offset);
					break;
				case 'S':
					fullscreen = true;
					dprintf("got option : view fullscreen\n");
					break;
				case 'P':
					paused = true;
					dprintf("got option : start paused\n");
					break;
				case 'N':
					nodisplay = true;
					dprintf("got option : no display when decoding (for timing)\n");
					break;
				case 'R':
					dumpRawName = str;
					dprintf("got option : dump to raw : %s\n",dumpRawName);
					dumpBmps = false;
					break;
				case 'B':
					dumpRawName = str;
					dumpBmps = true;
					dprintf("got option : dump to bmp seq : %s\n",dumpRawName);
					break;
				default:
					fprintf(stderr,"unknown option : %c : skipped\n",str[-1]);
					break;
			}
		}
		else
		{
			     if ( ! fromName ) fromName = str;
			else if ( ! toName ) toName = str;
			else if ( ! width ) width = atoi(str);
			else if ( ! height ) height = atoi(str);
			else
				fprintf(stderr,"extraneous parameter : %s : ignored\n",str);
		}
	}

	if ( ! fromName )
	{
		errputs("to encode : wave [options] <from> <to> [w] [h]");
		errputs("to decode : wave <wave stream>");
		errputs("encode options : ");
		errputs("  k# : set target comp K # (per frame) [4]");
		errputs("  l# : set target comp bytes # (per frame) [4096]");
		errputs("  e# : set target rmse # <use -e or -k or -l>");
		errputs("  f# : set frame rate (fps) [20]");
		errputs("  o# : offset in bytes into the raw stream");
		errputs("decode options : ");
		errputs("  s  : use fullscreen mode [window]");
		errputs("  p  : start paused [play]");
		errputs("  n  : no display (for timing)");
		errputs("  rN : dump to raw file name N");
		errputs("  bN : dump to bmp sequence name N");
		exit(0);
	}

	if ( toName )	encoding = true;
	else			encoding = false;

	printf("tsc estimated MHZ = %d\n",tscMHZ());

	if ( encoding )
		nodisplay = false;

	//---------------------------------------

	if ( encoding )
	{
		dprintf("packing stream %s to %s\n",FilePart(fromName),FilePart(toName));

		if ( stristr(fromName,".bmp") )
			bmpsIn = true;
		else
			bmpsIn = false;

		if ( bmpsIn )
		{
		char *p;
		bool ex;
		char name[1024];
		Image *im;

			streamFP = NULL;

			strcpy(bmpNameBase,fromName);
			p = strchr(bmpNameBase,'.');
			assert(p);
			*p = 0;
			p--;
			while( isdigit(*p) ) { *p = 0; p--; }

			frames = CountBmps(bmpNameBase,&bmpBaseN);

			ex = ExistsBmp(bmpNameBase,bmpBaseN,name);
			if ( ! ex )
				errexit("no bmps found");

			im = Image_CreateFromFileName(name);
			if ( ! im )
				errexit("bmp failed load");

			width = im->width;
			height = im->height;

			Image_Free(im);

			frameLen = width * height * 3;

			streamLen = frames * frameLen;
		}
		else
		{
			if ( width == 0 || height == 0 )
				errexit("must specify width & height on command line for raw stream input");

			frameLen = width * height * 3;

			streamFP = fopen(fromName,"rb");
			if ( ! streamFP )
			{
				errexit("fopen input stream failed!");
			}

			streamLen = flength(streamFP) - offset;
	
			frames = streamLen / frameLen;
		}

		printf("packing %d frames (%d x %d)\n",frames,width,height);
		
		totCompLen = 0;
	}
	else
	{
		dprintf("unpacking stream %s\n",FilePart(fromName));

		compFP = fopen(fromName,"rb");
		if ( !compFP )
			errexit("compFP fopen failed!");

		totCompLen = flength(compFP);
		frewind(compFP);
			
		FRead(compFP,&width,sizeof(int));
		FRead(compFP,&height,sizeof(int));
		FRead(compFP,&frames,sizeof(int));
		FRead(compFP,&fps,sizeof(int));

		targetLen = totCompLen / frames;
		
		frameLen = width * height * 3;
		streamLen = frameLen * frames;
		
		dprintf("%-20s : %6d k -> %6d k = %1.2f bpp\n",
												FilePart(fromName),
												totCompLen>>10,streamLen>>10,
												(totCompLen*8.0f/(float)(streamLen/3)));
	}

	if ( frames < 1 )
		errexit("frames < 1");

	// make image with sizes padded to next multiple of 32
	{ //--------
	int padw,padh;
	padw = (width  + 31)&(~31);
	padh = (height + 31)&(~31);
	im = Image_Create(padw,padh);
	if ( ! im )
		errexit("image could not be created");
	} //--------

	float_RoundToZero();
	assert(float_DoesRoundToZero());

	float_SinglePrecision();

	compBufLen = targetLen*2 + 8192;
	compBuf = malloc(compBufLen);
	assert(compBuf);

	viewIm = *im;
	viewIm.BGRplane = malloc(im->width*im->height*3);

	if ( ! nodisplay )
	{
		viewer = DrawDriver_CreateStandardWindow(instance,width,height);

		if ( ! viewer )
			errexit("DrawDriver_CreateWindow failed");

		if ( encoding || ! fullscreen )
		{
			//window mode
			if ( ! DrawDriver_Startup_WindowMode(instance,viewer) )
				errexit("DrawDriver_Startup failed");
		}
		else
		{
		#if 0 //{ // force a standard video mode
			if ( width <= 320 && height <= 240 )
			{
				if ( ! DrawDriver_Startup_Closest(instance,viewer,320,240) )
					errexit("DrawDriver_Startup failed");
			}
			else if ( width <= 640 && height <= 480 )
			{
				if ( ! DrawDriver_Startup_Closest(instance,viewer,640,480) )
					errexit("DrawDriver_Startup failed");
			}
			else
		#endif //}
			{
				if ( ! DrawDriver_Startup_Closest(instance,viewer,width,height) )
					errexit("DrawDriver_Startup failed");
			}
		}
	}

	encoder = CodeImager_Create(im->width,im->height);
	decoder = CodeImager_Create(im->width,im->height);
	assert( encoder && decoder );

	//---------------------------------------
	// encode

	if ( ! nodisplay )
	{

⌨️ 快捷键说明

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