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

📄 jpgmain.c

📁 bmp图像转换成jpeg图像的一个linux的小程序
💻 C
字号:
/******************************************************************************
//               INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2003 Intel Corporation. All Rights Reserved.
//
//  Description:
//    Intel(R) Integrated Performance Primitives Sample Code JPEG Codec Main 
//	  Entry
//
******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "sampjpeg.h"

/******************************************************************************
// Name:             show_help
// Description:      
//		This function displays the argument requirement
// Input Arguments:
//		argv		- Pointer to the command line parameters
// Returns:
//		None
******************************************************************************/
void show_help(char **argv)
{
	printf("\n\n*****************Help information**********************\n");
	printf("When using the encoder, input arguments are:\n");
	printf("	%s -enc input_file output_file -h\n", argv[0]);
	printf("which use high quality encodig or\n");
	printf("	%s -enc input_file output_file -l\n", argv[0]);
	printf("which use low quality encoding\n\n");
	printf("When using the decoder, input arguments are:\n");
	printf("	%s -dec input_file output_file\n", argv[0]);
	printf("\n\n****************Help information end****************\n\n\n");
}

/******************************************************************************
// Name:             parse_command_line
// Description:      
//		This function processes the command line arguments into function 
//		parameters
// Input arguments
//		argc		- Command line arguments number
//		argv		- Command line arguments
// Ouput arguments
//		option		- Pointer to the encoder/decoder option. 0: encoder, 
//					  1: decoder
//		quality		- Pointer to the quality indicator. 0: low quality,
//					  1: high quality
//		src_file	- Pointer to the file pointer that will be used as input
//		dst_file	- Pointer to the file pointer that will be used as output
// Returns:
//		0	 - Error
//		1	 - Ok
******************************************************************************/
int parse_command_line(int argc, char* argv[], int *option, int *quality,
					   FILE **src_file, FILE **dst_file)
{
	if(4 > argc) {		
		printf("Not enough parameters, function stopped\n");
		return 0;
	}

	if(!strcmp(argv[1], "-enc")) {	/* The option is encode */
		if(5 != argc) {
			printf("Not enough parameters for encoding, function stopped\n");
			return 0;
		}	
		if(strcmp(argv[4], "-h") && strcmp(argv[4], "-l")) {
			printf("Not correct parameter for quality definition\n");
			return 0;
		} else if(!strcmp(argv[4], "-h")) {
			*quality = 1;
		} else {
			*quality = 0;
		}

		*option = 0; /* The encoder option is on */
		if(!strstr(argv[2], ".bmp")){
			printf("The application only support BMP format for encode input\n");
			printf("Please check the input file \n\n");
			return 0;
		}
	} else if (!strcmp(argv[1], "-dec")) {
		if(!strstr(argv[2], ".jpg")){
			printf("The application only support JPEG format for decode input\n");
			printf("Please check the input file \n\n");
			return 0;
		}

		*option = 1;	/* The decoder option is on */
	} else {				/* The input is error */
		printf("Not correct parameter for encode/decode option\n");
		return 0;
	}

	*src_file = fopen(argv[2], "rb");
	if(*src_file == NULL) {
		printf("Input file not available, please check\n\n");
		return 0;
	}

	*dst_file = fopen(argv[3], "wb");
	if(*dst_file == NULL) {
		printf("Output file not available, please check\n\n");
		return 0;
	}


	return 1;
}

/******************************************************************************
// Name:             show_property_bmp
// Description:      
//		This function shows the BMP file property for JPEG encoder
// Input arguments
//		in_file		- Pointer to the input BMP file name
//		out_file	- Pointer to the output JPEG file name
//		enc_state	- Pointer to the JPEG encoder state structure
// Returns:
//		None
******************************************************************************/
void show_property_bmp(char in_file[], char out_file[], 	
					   jpeg_enc_state *enc_state)
{
	printf("Encoder input file		%s\n", in_file);
	printf("Encoder output file		%s\n", out_file);
	printf("BMP width			%d\n", enc_state->width);
	printf("BMP height			%d\n", enc_state->height);
	switch (enc_state->color_mode) {
	case 2:
		printf("BMP BGR format:			BGR 888\n");
		break;
	case 4:
		printf("BMP BGR format:			BGR 555\n");
		break;
	case 8:
		printf("BMP BGR format:			BGR 565\n");
	};
}

/******************************************************************************
// Name:             show_property_jpeg
// Description:      
//		This function shows the JPEG file property for JPEG decoder
// Input arguments
//		in_file		- Pointer to the input JPEG file name
//		out_file	- Pointer to the output BMP file name
//		dec_state	- Pointer to the JPEG decoder state structure
// Returns:
//		None
******************************************************************************/
void show_property_jpeg(char in_file[], char out_file[], 	
					   jpeg_dec_state *dec_state)
{
	printf("Decoder input file		%s\n", in_file);
	printf("Decoder output file		%s\n", out_file);
	printf("JPEG width			%d\n", dec_state->width);
	printf("JPEG height			%d\n", dec_state->height);
	printf("Output BGR format:		BGR 888\n");
}

/******************************************************************************
// Name:             write_bmp
// Description:      
//		This function shows the write the BGR data to a BMP file
// Input arguments
//		picture		- Pointer to the picture plane structure
//		dst			- Output file handle
// Returns:
//		None
******************************************************************************/
void write_bmp(sample_picture *picture, FILE *dst) 
{
	unsigned char data8[2];
	short data16;
	int data32;
	int	buf_size;
	int offset;

	/* Write the BMP file indicator */
	data8[0] = 'B';
	data8[1] = 'M';
	fwrite(data8, 1, 2, dst);

	/* Write the file size */
	buf_size = picture->pic_plane_step[0] * picture->pic_height + 54; 
	/* 54 is the header size */
	fwrite(&buf_size, 4, 1, dst);

	/*Write the reserved bytes */
	data16 = 0;
	fwrite(&data16, 2, 1, dst);
	fwrite(&data16, 2, 1, dst);

	/* Write the offset of the raw data start position */
	offset = 54;
	fwrite(&offset, 4, 1, dst);

	/* Write the bitmap header size */
	buf_size = 40; /* 54 - 14 = 40 */
	fwrite(&buf_size, 4, 1, dst);
	
	/* Write the width and height */
	fwrite(&(picture->pic_width), 4, 1, dst);
	fwrite(&(picture->pic_height), 4, 1, dst);

	/* Write plane number */
	data16 = 1;
	fwrite(&data16, 2, 1, dst);

	/* Write bit per pixel */
	data16 = 24;
	fwrite(&data16, 2, 1, dst);

	/* Write bit format: BGR 888 */
	data32 = JPEG_BI_RGB;
	fwrite(&data32, 4, 1, dst);

	/* Write the raw data size */
	buf_size = picture->pic_plane_step[0] * picture->pic_height;
	fwrite(&buf_size, 4, 1, dst);

	/* Write the X and Y Pels mer meter */
	data32 = 72;
	fwrite(&data32, 4, 1, dst);
	fwrite(&data32, 4, 1, dst);

	/* Write the bit color used mode, since it will be BGR888, write a "0" */
	data32 = 0;
	fwrite(&data32, 4, 1, dst);
	/* Write the bit color importance indicator */
	fwrite(&data32, 4, 1, dst);

	/* Write the BGR data */
	fwrite(picture->pic_plane[0], 1, buf_size, dst);
}

/******************************************************************************
// Name:             main
// Description:      
//		Entry point of the exe file
// Input Arguments:
//		argc		- number of command line arguments
//		argv[]		- Pointer to the command line parameters. The arguments 
//					  can be:
//						1. for encoding the input with high(low) quality 
//						   requirement: "jpeg.exe -enc in_file out_file -h(-l)
//						2. for decoding the input jpeg file:
//						   "jpeg.exe -dec in_file out_file
//						3. for help information:"jpeg.exe -help"
// Returns:
//		0			- Encoder/decoder completed.
//		-1			- Error occured or process aborted
******************************************************************************/
int main(int argc, char* argv[])
{
    FILE *src_file = NULL;		/* Pointer to the input file */
    FILE *dst_file = NULL;		/* Pointer to the output file */
	int	enc_dec_option;	
	int quality_ind;
	sample_bitstream bitstream;
	sample_picture picture;
	int length;
	jpeg_enc_state *enc_state;
	jpeg_dec_state *dec_state;
  
	/* 
	// There must be no less than 3 arguments in the command line: 
	// input file, output file, encode/decode option, if encode, 
	// there must be the quality requirement.
	// For example:
	// encode the input with high(low) quality can be:
			jpeg.exe -enc input_file output_file -h(-l)
	// decode the input can be:
	//		jpeg.exe -dec input_file output_file  
	*/

	/* The following steps will parse and check the arguments */

	if(2 == argc && !strcmp("-help", argv[1])) {
		show_help(argv);
		return -1;
	}

	if(!parse_command_line(argc, argv, &enc_dec_option, &quality_ind, &src_file, 
		&dst_file)) {
		show_help(argv);
		printf("Function aborted\n\n");
		return -1;
	}

	/* The following steps will carry out the encoder or decoder */
	if(0 == enc_dec_option) { /* Encoder is invoked */
		/* Call the initialize function */
		enc_state = (jpeg_enc_state *) malloc (sizeof(jpeg_enc_state));
		if(SAMPLE_STATUS_NOERR != encoder_init_alloc_jpeg(src_file, quality_ind, 
			enc_state, &picture, &bitstream)){
			printf("Initialization failed\n\n");
			return -1;
		} 

		/* Show the properties of the BMP file */
		printf("*************Encoding begin***************\n");
		show_property_bmp(argv[2], argv[3], enc_state);

		/* Call the JPEG encoding function */
		if(SAMPLE_STATUS_NOERR != encode_jpeg(&picture, &bitstream, enc_state)){
			printf("Encoding failed\n\n");
			return -1;
		}

		/* Output the bitstream of JPEG file */
		length = bitstream.bs_cur_byte - bitstream.bs_buffer;
		fwrite(bitstream.bs_buffer, 1, length, dst_file);

		/* Free the allocated buffers */
		encoder_free_jpeg(enc_state);

		free(enc_state);
		printf("*************Encoding end***************\n\n");
	} else {				/* Decoder is invoked */		
		/* Call the initialize function */
		dec_state = (jpeg_dec_state *) malloc (sizeof(jpeg_dec_state));		
		if(SAMPLE_STATUS_NOERR != decoder_init_alloc_jpeg(src_file,  
			dec_state, &bitstream, &picture)){
			printf("Initialization failed\n\n");
			return -1;
		} 

		/* Call the JPEG decoding function */
		printf("*************Decoding begin***************\n");
		show_property_jpeg(argv[2], argv[3], dec_state);
		if(SAMPLE_STATUS_NOERR != decode_jpeg(&bitstream, &picture, dec_state)){
			printf("Decoding failed\n\n");
			return -1;
		}

		/* Output the BMP file */		
		write_bmp(&picture, dst_file);

		/* Free the allocated buffers */
		decoder_free_jpeg(&bitstream, &picture, dec_state);

		free(dec_state);
		printf("*************Decoding ends***************\n");
	}

	if(NULL != src_file) {
		fclose(src_file);
		src_file = NULL;
	}

	if(NULL != dst_file) {
		fclose(dst_file);
		dst_file = NULL;
	}

	return 0;
}

/* EOF */

⌨️ 快捷键说明

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