Ejemplo n.º 1
0
// encode function calls
void encode(const char* type, const char* infilename, const char* outfilename)
{
	infile = fopen(infilename, "r+b");
	outfile = fopen(outfilename, "w+b");

	printf("filename     %s\n", infilename);
	//printf("size         %d\n", get_file_size(infilename));
	printf("----\n");
	if ( strcmp(type, "HUFF") == 0 )
	{
		start_timer();
		huff(infile, outfile);
		printf("t-cmprss     %.2f\n", elapsed_time());
	}
	else if ( strcmp(type, "LZ1") == 0 )
	{
		start_timer();
		lz1(infile, outfile);
		printf("t-cmprss     %.2f\n", elapsed_time());
	}
	else if ( strcmp(type, "LZ2") == 0 )
	{
		start_timer();
		lz2(infile, outfile);
		printf("t-cmprss     %.2f\n", elapsed_time());
	}
	else
		printf("Error: unknown encode function call");
}
Ejemplo n.º 2
0
int torhenc(unsigned char *in, int inlen, unsigned char *out, int outsize) {
  struct bufio bufio; bufioini(&bufio, in, inlen, out, outsize);
  HuffmanEncoder<EOB_CODE> huff(ReadWriteMem, &bufio, CHUNK_SIZE, 4*CHUNK_SIZE, EOB_CODE+1);
  unsigned char *p,*iep = in+inlen;
  for(p = in; p < iep; ) { unsigned char *ep = p + CHUNK_SIZE; if(ep>iep) ep = iep;
    for(; p < ep; p++) huff.encode(*p); 
    huff.flush();
  } huff.finish();
  return wrptr(&bufio) - out;
}
Ejemplo n.º 3
0
int main(int argv,char ** argc)
{	
	printf("The list of codes:\n");
	ololo();
	for (int i = 0; i < 5; i++)
	huff();
	run=first;
	root=malloc(sizeof (struct Sym));
	root->left=first;
	root->right=last;
	drop(root);
	out(root,0);
	return 0;
}
Ejemplo n.º 4
0
Archivo: main.c Proyecto: w4kfu/Stunts
int main(int argc, char **argv)
{
        int fd;
        struct stat st;
        unsigned char *buf = NULL;
	struct conf_c conf = {0};
	struct s_comp comp = {0};

	parse_opt(argc, argv, &conf);
	check_opt(&conf, argv[0]);
        fd = open(conf.in, O_RDONLY);
        if (fd == -1)
        {
                perror("open()");
                exit(EXIT_FAILURE);
        }
        if (fstat(fd, &st) == -1)
        {
                perror("fstat()");
                exit(EXIT_FAILURE);
        }
        if ((buf = malloc(sizeof (char) * st.st_size)) == NULL)
        {
                perror("malloc()");
                exit(EXIT_FAILURE);
        }
        if (read(fd, buf, st.st_size) != st.st_size)
        {
                perror("read()");
                goto clean;
        }
	huff(buf, st.st_size, &comp);
	if (comp.tree)
	{
		if (conf.dot)
			dotty(comp.tree, conf.dot);
		uncomp(&comp, buf + st.st_size);
		if (comp.buf_out)
			dump_to_file(conf.out, comp.buf_out, comp.size);
	}
clean:
        free(buf);
        close(fd);
        return 0;
}
Ejemplo n.º 5
0
void benchmark_test()
{
	char* benchmark_set[3] = {"news", "book1", "kennedy.xls"};
	char* benchmark_encode[3] = {"news_encode", "book1_encode", "kennedy.xls_encode"};
	char* benchmark_decode[3] = {"news_decode", "book1_decode", "kennedy.xls_decode"};
	char* command[3] = {"gzip news", "gzip book1", "gzip kennedy.xls"};
	char* command_encode[3] = {"news.gz", "book1.gz", "kennedy.xls.gz"};
	char* command_decode[3] = {"gzip -d news.gz", "gzip -d book1.gz", "gzip -d kennedy.xls.gz"};

	for (int i = 0; i < 3; i++)
	{

		int fsize = get_file_size(benchmark_set[i]);
		int COMPsize = 0;
		printf("filename     %s\n", benchmark_set[i]);
		printf("size         %d\n", fsize);
		printf("----\n");

		///////////////////////////////////////////////
		start_timer();
		infile = fopen(benchmark_set[i], "r+b");
		outfile = fopen(benchmark_encode[i], "w+b");
		huff(infile, outfile);
		COMPsize = get_file_size(benchmark_encode[i]);

		printf("HUFFsiz      %d\n", COMPsize);
		printf("%%save        %.2f%%\n", (1 - (double)COMPsize / (double)fsize) * 100);
		printf("t-cmprss     %.2f\n", elapsed_time());

		start_timer();
		infile = fopen(benchmark_encode[i], "r+b");
		outfile = fopen(benchmark_decode[i], "w+b");
		huff(infile, outfile);
		printf("t-expand     %.2f\n", elapsed_time());
		////////////////////////////////////////////////

		printf("----\n");

		start_timer();
		infile = fopen(benchmark_set[i], "r+b");
		outfile = fopen(benchmark_encode[i], "w+b");
		lz1(infile, outfile);
		COMPsize = get_file_size(benchmark_encode[i]);

		printf("LZ1siz       %d\n", COMPsize);
		printf("%%save        %.2f%%\n", (1 - (double)COMPsize / (double)fsize) * 100);
		printf("t-cmprss     %.2f\n", elapsed_time());

		start_timer();
		infile = fopen(benchmark_encode[i], "r+b");
		outfile = fopen(benchmark_decode[i], "w+b");
		unlz1(infile, outfile);
		printf("t-expand     %.2f\n", elapsed_time());

		////////////////////////////////////////////////

		printf("----\n");
		start_timer();
		infile = fopen(benchmark_set[i], "r+b");
		outfile = fopen(benchmark_encode[i], "w+b");
		lz2(infile, outfile);
		COMPsize = get_file_size(benchmark_encode[i]);

		printf("LZ2siz       %d\n", COMPsize);
		printf("%%save        %.2f%%\n", (1 - (double)COMPsize / (double)fsize) * 100);
		printf("t-cmprss     %.2f\n", elapsed_time());

		start_timer();
		infile = fopen(benchmark_encode[i], "r+b");
		outfile = fopen(benchmark_decode[i], "w+b");
		unlz2(infile, outfile);
		printf("t-expand     %.2f\n", elapsed_time());
		////////////////////////////////////////////////

		printf("----\n");

		start_timer();
		system(command[i]);
		COMPsize = get_file_size(command_encode[i]);

		printf("GZIPsiz      %d\n", COMPsize);
		printf("%%save        %.2f%%\n", (1 - (double)COMPsize / (double)fsize) * 100);
		printf("t-cmprss     %.2f\n", elapsed_time());

		start_timer();
		system(command_decode[i]);
		printf("t-expand     %.2f\n", elapsed_time());
		////////////////////////////////////////////////
		printf("\n\n\n");
	}
}
Ejemplo n.º 6
0
int torhdec(unsigned char *in, int inlen, unsigned char *out, int outlen ) {
  struct bufio bufio; bufioini(&bufio, in, inlen, out, outlen);
  HuffmanDecoder<EOB_CODE> huff(ReadWriteMem, &bufio, CHUNK_SIZE, EOB_CODE+1);
  for(int i=0; i<outlen; i++) out[i] = huff.decode();
  return wrptr(&bufio) - out;
}
Ejemplo n.º 7
0
int main(int /*argc*/, char** /*argv*/) {

	std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
	std::cout.precision(2);

    try {
        const std::vector<std::string> vsTestImagePaths = {
            {"data/airplane.png"},
            {"data/baboon.png"},
			{ "data/cameraman.tif" },
			{ "data/lena.png" },
            {"data/logo.tif"},
			{ "data/logo_noise.tif" },
            {"data/peppers.png"},
        };
        for(const std::string& sTestImagePath : vsTestImagePaths) {
            const cv::Mat oInput = cv::imread(sTestImagePath);
            if(oInput.empty() || oInput.type()!=CV_8UC3)
                CV_Error_(-1,("Could not load image at '%s', check local paths",sTestImagePath.c_str()));

			std::cout << "\n ***************************************** \n\n";

            // COMPRESSION
            cv::Mat_<uchar> Y,Cb,Cr;
            conv_rgb2ycbcr(oInput,USE_SUBSAMPLING,Y,Cb,Cr);
            const std::vector<cv::Mat_<uchar>> vBlocks_Y = decoup(Y);
            const std::vector<cv::Mat_<uchar>> vBlocks_Cb = decoup(Cb);
            const std::vector<cv::Mat_<uchar>> vBlocks_Cr = decoup(Cr);

			/* Test de-conversion */
			cv::Mat image_unconvert;
			conv_ycbcr2rgb(Y, Cb, Cr, USE_SUBSAMPLING, image_unconvert);
			cv::Mat diff;
			cv::absdiff(oInput, image_unconvert, diff);


            std::vector<cv::Mat_<uchar>> vBlocks;
            vBlocks.insert(vBlocks.end(),vBlocks_Y.begin(),vBlocks_Y.end());
            vBlocks.insert(vBlocks.end(),vBlocks_Cb.begin(),vBlocks_Cb.end());
            vBlocks.insert(vBlocks.end(),vBlocks_Cr.begin(),vBlocks_Cr.end());
            std::vector<cv::Mat_<float>> vDCTBlocks(vBlocks.size());

			/* Test block - unblock*/
			//const cv::Mat_<uchar> test = decoup_inv(vBlocks, Y.size());

			for (size_t b = 0; b < vBlocks.size(); ++b)
			{
				vDCTBlocks[b] = dct(vBlocks[b]);

				/* Test i_dct*/
				cv::Mat_<uchar> original = vBlocks[b];
				cv::Mat_<float> dct = vDCTBlocks[b];
				cv::Mat_<uchar> inverse = dct_inv(vDCTBlocks[b]);			
			}
			
			// Quantification
            std::vector<cv::Mat_<short>> vQuantifDCTBlocks(vDCTBlocks.size());
            for(size_t b=0; b<vDCTBlocks.size(); ++b)
                vQuantifDCTBlocks[b] = quantif(vDCTBlocks[b],USE_QUANT_QUALITY);

            std::vector<std::array<short,8*8>> vInlinedBlocks(vQuantifDCTBlocks.size());
			for (size_t b = 0; b < vQuantifDCTBlocks.size(); ++b)
			{
				vInlinedBlocks[b] = zigzag(vQuantifDCTBlocks[b]);

				// Test zigzag ...
				/*
				cv::Mat_<short> original = vQuantifDCTBlocks[b];
				std::array<short, 8 * 8> arr = vInlinedBlocks[b];
				cv::Mat_<short> inverse = zigzag_inv(vInlinedBlocks[b]);
				*/
			}
                
            const HuffOutput<short> oCode = huff(vInlinedBlocks);

            // @@@@ TODO: check compression rate here...
			cv::Size s = oInput.size();
			int nbPixel = s.height * s.width;

			// Size in bits

			double size_before = 8 * nbPixel * oInput.channels();
			double size_after_color = 8 * (Y.size().area() + Cb.size().area() + Cr.size().area());
			double size_after_dct = 8 * 8 * 8 * vInlinedBlocks.size();
			double size_after_pipeline= oCode.string.size();

			double compressionRate_after_color = 1 - (size_after_color / size_before);
			double compressionRate_after_dct = 1 - (size_after_dct / size_after_color);
			double compressionRate_afer_pipeline = 1 - (size_after_pipeline / size_before);

			/*
			double compressionRate_after_color = size_before / size_after_color;
			double compressionRate_after_dct = size_after_color / size_after_dct;
			double compressionRate_afer_pipeline = size_before /size_after_pipeline;
			*/

			std::cout << "Images: " << sTestImagePath << "\n";

			std::cout << "Size before color   : " << size_before/ (1000.0 * 8.0) << " ko\n";
			std::cout << "Size after color    : " << size_after_color/ (1000.0 * 8.0) << " ko\n";
			std::cout << "Size after dct      : " << size_after_dct/ (1000.0 * 8.0) << " ko\n";
			std::cout << "Size after pipeline : " << size_after_pipeline/ (1000.0 * 8.0) << " ko\n";
			
			std::cout << "Compression rate couleur seulement   : " << compressionRate_after_color << "%\n";
			std::cout << "Compression rate dct(+q+z) seulement : " << compressionRate_after_dct << "%\n";
			std::cout << "Compression rate fin pipeline        : " << compressionRate_afer_pipeline << "%\n";

            // DECOMPRESSION
            const std::vector<std::array<short,8*8>> vInlinedBlocks_decompr = huff_inv<8*8>(oCode);


			// Comment to test dct
            std::vector<cv::Mat_<short>> vQuantifDCTBlocks_decompr(vInlinedBlocks_decompr.size());
            for(size_t b=0; b<vInlinedBlocks_decompr.size(); ++b)
                vQuantifDCTBlocks_decompr[b] = zigzag_inv(vInlinedBlocks_decompr[b]);

			// Uncomment to test dct
			//std::vector<cv::Mat_<short>> vQuantifDCTBlocks_decompr(vInlinedBlocks.size());
			//for (size_t b = 0; b<vInlinedBlocks.size(); ++b)
			//	vQuantifDCTBlocks_decompr[b] = zigzag_inv(vInlinedBlocks[b]);
			
			// Comment to test dct
            std::vector<cv::Mat_<float>> vDCTBlocks_decompr(vQuantifDCTBlocks_decompr.size());
            for(size_t b=0; b<vQuantifDCTBlocks_decompr.size(); ++b)
                vDCTBlocks_decompr[b] = quantif_inv(vQuantifDCTBlocks_decompr[b],USE_QUANT_QUALITY);

			// Uncomment to test dct
			//std::vector<cv::Mat_<float>> vDCTBlocks_decompr(vQuantifDCTBlocks_decompr.size());
			//for (size_t b = 0; b<vQuantifDCTBlocks_decompr.size(); ++b)
			//	vDCTBlocks_decompr[b] = quantif_inv(vQuantifDCTBlocks_decompr[b], USE_QUANT_QUALITY);

			// Commment to test quantification
            std::vector<cv::Mat_<uchar>> vBlocks_decompr(vDCTBlocks_decompr.size());
			for (size_t b = 0; b<vDCTBlocks_decompr.size(); ++b)
				vBlocks_decompr[b] = dct_inv(vDCTBlocks_decompr[b]);

			// Uncomment to test quantification inverse
			//std::vector<cv::Mat_<uchar>> vBlocks_decompr(vDCTBlocks.size());
   //         for(size_t b=0; b<vDCTBlocks.size(); ++b)
   //             vBlocks_decompr[b] = dct_inv(vDCTBlocks[b]);

            const std::vector<cv::Mat_<uchar>> vBlocks_Y_decompr(vBlocks_decompr.begin(),vBlocks_decompr.begin()+vBlocks_Y.size());
            const std::vector<cv::Mat_<uchar>> vBlocks_Cb_decompr(vBlocks_decompr.begin()+vBlocks_Y.size(),vBlocks_decompr.begin()+vBlocks_Y.size()+vBlocks_Cb.size());
            const std::vector<cv::Mat_<uchar>> vBlocks_Cr_decompr(vBlocks_decompr.begin()+vBlocks_Y.size()+vBlocks_Cb.size(),vBlocks_decompr.end());
            const cv::Mat_<uchar> Y_decompr = decoup_inv(vBlocks_Y_decompr,Y.size());
            const cv::Mat_<uchar> Cb_decompr = decoup_inv(vBlocks_Cb_decompr,Cb.size());
            const cv::Mat_<uchar> Cr_decompr = decoup_inv(vBlocks_Cr_decompr,Cr.size());
            cv::Mat oInput_decompr;
            conv_ycbcr2rgb(Y_decompr,Cb_decompr,Cr_decompr,USE_SUBSAMPLING,oInput_decompr);

            cv::Mat oDisplay;
            cv::hconcat(oInput,oInput_decompr,oDisplay);
            cv::Mat oDiff;
            cv::absdiff(oInput,oInput_decompr,oDiff);
            cv::hconcat(oDisplay,oDiff,oDisplay);
            cv::imshow(sTestImagePath.substr(sTestImagePath.find_last_of("/\\")+1),oDisplay);
            cv::waitKey(1);
        }
        std::cout << "all done; press any key on a window to quit..." << std::endl;
        cv::waitKey(0);
        return 0;
    }
    catch(const cv::Exception& e) {
        std::cerr << "Caught cv::Exceptions: " << e.what() << std::endl;
    }
    catch(const std::runtime_error& e) {
        std::cerr << "Caught std::runtime_error: " << e.what() << std::endl;
    }
    catch(...) {
        std::cerr << "Caught unhandled exception." << std::endl;
    }
    return 1;
}