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

📄 cjpeg.c

📁 jpeg压缩C代码,包括库的源代码和一个测试程序的源代码
💻 C
字号:
/*
* cjpeg.c
*
* Copyright (C) 1991-1998, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains a command-line user interface for the JPEG compressor.
* It should work on any system with Unix- or MS-DOS-style command lines.
*
* Two different command line styles are permitted, depending on the
* compile-time switch TWO_FILE_COMMANDLINE:
*	cjpeg [options]  inputfile outputfile
*	cjpeg [options]  [inputfile]
* In the second style, output is always to standard output, which you'd
* normally redirect to a file or pipe to some other program.  Input is
* either from a named file or from standard input (typically redirected).
* The second style is convenient on Unix but is unhelpful on systems that
* don't support pipes.  Also, you MUST use the first style if your system
* doesn't do binary I/O to stdin/stdout.
* To simplify script writing, the "-outfile" switch is provided.  The syntax
*	cjpeg [options]  -outfile outputfile  inputfile
* works regardless of which command line style is used.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <stdio.h>

#ifdef DSP_TERMINAL
#include "iekc64.h"
#include "iekterm.h"
#endif

#include "jpeglib.h"

unsigned __int64 g_startcount;
unsigned __int64 g_endcount;

int g_test_time = 0;
int g_test = 0;

void ReadTimestampCounter(unsigned __int64 *count);

int main (int argc, char **argv)
{
	JPEG_COMPRESS_PARAM cinfo;
	JPEG_IMAGE_PARAM image;
	YUV_FRAME frame;

	char input_file_name[256];
	char output_file_name[256];
	char output_main_name[256];

	int width = 352;
	int height = 288;
	int quality = 75;
	int frames = 1;
	int frame_num = 0;
	int size;
	int length;
	int i;
	
	FILE * input_file;
	FILE * output_file;

	int tmp_time, tot_time = 0;
	unsigned __int64 start_count, end_count;
	
	if(argc <= 5) 
	{
		printf("usage: inputfile outputfile width height quality frames", argv[0]);
		return 0;
	}

	// get parameters
	sprintf(input_file_name, "%s", argv[1]);
	sprintf(output_main_name, "%s", argv[2]);
	width   = atoi(argv[3]);
	height  = atoi(argv[4]);
	quality = atoi(argv[5]);
	if(argc > 6)
		frames = atoi(argv[6]);

	/* Open the input file. */
	if ((input_file = fopen(input_file_name, "rb")) == NULL) 
	{
		printf("can't open %s\n", argv[1]);
		return 0;
	}
	
	if((width % 16) || (height % 16))
	{
		printf("Unsupported image format x=%d,y=%d, must be a multiple of 16", width, height);
		return 0;
	}

	if(frames <= 0) 
		frames = 1;

	printf("********************************************************************\n");
	printf("* input file : %s\n", input_file_name);
	printf("* output file: %s\n", output_main_name);
	printf("* width      : %d\n", width);
	printf("* height     : %d\n", height);
	printf("* frames     : %d\n", frames);
	printf("********************************************************************\n");

	printf("\n encode start...\n");

	// init buffer
	size = width * height * 3 /2;
	if((frame.y = malloc(size)) == NULL)
	{
		printf("\nERROR! image buffer malloc failed!");
		return 0;
	}
	frame.u = frame.y + width * height;
	frame.v = frame.y + width * height * 5 / 4;

	image.frame  = &frame;
	image.width  = width;
	image.height = height;
	image.size   = size;
	if((image.bitstream = malloc(size)) == NULL)
	{
		printf("\nERROR! stream_buffer malloc failed!");
		return 0;
	}

	// set image quality
	JPEG_SetQuality(&cinfo, quality);

	for(i = 0; i < frames; i ++)
	{
		if(!fread(frame.y, size, 1, input_file))
		{	
			printf("\nread source file failed!");	
			break;	
		}

		// compress one picture
		ReadTimestampCounter(&start_count);
		if(!JPEG_Compress(&cinfo, &image))
		{
			printf("\nERROR! jpeg_compress_data failed!");
			break;;
		}
		ReadTimestampCounter(&end_count);
		tmp_time = (int)((end_count - start_count) / 2800);
		tot_time += tmp_time;

		length = image.length;

		// ouput one encoded picture
		sprintf(output_file_name, "%s%d.jpg",output_main_name, i);
		if ((output_file = fopen(output_file_name, "wb")) == NULL) 
		{
			printf("\nERROR! can't open %s\n", output_main_name);
			break;
		}
		if(!fwrite(image.bitstream, length, 1, output_file))
		{
			printf("\nERROR! output stream file failed!");
			break;
		}
		/* After finish_compress, we can close the output file. */
		fclose(output_file);

		frame_num ++;
		printf("\n %s size = %d, time = %d us", output_file_name, length, tmp_time);
	}

	fclose(input_file);
	free(frame.y);

	/* And we're done! */
	if(frame_num > 0)
	{
		printf("\n总共压缩 %d 帧\n平均每帧 = %d us\n", frame_num, tot_time/frame_num);
		if(g_test > 0)
			printf("\n总共测试 %d 次\n平均每次 %d 时钟\n平均每帧 = %d us...\n\n", 
			g_test, g_test_time/g_test, g_test_time/frame_num/600);

	}
	printf("\n\n encode end...\n\n");
	
}

void ReadTimestampCounter(unsigned __int64 *count)
{ 
#ifdef _WIN64
	*count = 0;
#else 
#ifdef _WIN32
    unsigned __int64 c;
    __asm push eax
    __asm push edx
    __asm _emit 0x0f
    __asm _emit 0x31
    __asm mov dword ptr c,   eax
    __asm mov dword ptr c+4, edx
    __asm pop edx
    __asm pop eax
    *count = c;
#endif
#ifdef LINUX
	*count = 0;
#endif
#endif
}

void StartTimer()
{
	ReadTimestampCounter(&g_startcount);
}
void StopTimer()
{
	ReadTimestampCounter(&g_endcount);
	g_test_time += (int)((g_endcount - g_startcount - 13 - 1)-93);
	g_test ++;
}

⌨️ 快捷键说明

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