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

📄 main.c

📁 C代码完成jpeg编码和解码
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include "dct.h" 
#include "scenary.h"  		/* Header file containing input image as a 1D array */

#pragma DATA_SECTION (image_in,"ext_sdram")
#pragma DATA_SECTION (image_out,"ext_sdram")

/* 1D array to hold output image */
unsigned char image_out[IMAGE_SIZE];
/* 1D array to hold the current block */
short block[BLOCK_SIZE];
/* 1D array to hold the data stream after encoding */
int stream[BLOCK_SIZE];
/* the value of the first coefficient in the pre-block used for encoding and decoding respectively */
short preblock=0, Ipreblock=0;

/* Q12 DCT coefficients (actual coefficient x 2^12 ) */
const short coe[8][8]=
{   
	4096,    4096,    4096,    4096,    4096,    4096,    4096,   4096,
	5681,    4816,    3218,    1130,   -1130,   -3218,   -4816,  -5681,
	5352,    2217,   -2217,   -5352,   -5352,   -2217,    2217,   5352,
	4816,   -1130,   -5681,   -3218,    3218,    5681,    1130,  -4816,
	4096,   -4096,   -4096,    4096,    4096,   -4096,   -4096,   4096,
	3218,   -5681,    1130,    4816,   -4816,   -1130,    5681,  -3218,
	2217,   -5352,    5352,   -2217,   -2217,    5352,   -5352,   2217,
	1130,   -3218,    4816,   -5681,    5681,   -4816,    3218,  -1130
};

/* Quantization matrix, (1/coefficient) x 2^15 */
short quant[BLOCK_SIZE]=
{
	2048,    2979,    3277,    2048,    1365,    819,    643,    537,
    2731,    2731,    2341,    1725,    1260,    565,    546,    596,
    2341,    2521,    2048,    1365,    819,     575,    475,    585,
    2341,    1927,    1490,    1130,    643,     377,    410,    529,
    1820,    1490,    886,     585,     482,     301,    318,    426,
    1365,    936,     596,     512,     405,     315,    290,    356,
    669,     512,     420,     377,     318,     271,    273,    324,
    455,     356,     345,     334,     293,     328,    318,    331
};

/* Quantization matrix */
short iquant[BLOCK_SIZE]=
{
	16,		11,		10,		16,		24,		40,		51,		61,
	12,		12,		14,		19,		26,		58,		60,		55,
	14,		13,		16,		24,		40,		57,		69,		56,
	14,		17,		22,		29,		51,		87,		80,		62,
	18,		22,		37,		56,		68,		109,	103,	77,
	24,		35,		55,		64,		81,		104,	113,	92,
	49,		64,		78,		87,		103,	121,	120,	101,
	72,		92,		95,		98,		112,	100,	103,	99
};

/* DC Huffman coefficient */
short dcHuffman[12]=
{
	0x0,	0x2,	0x3,	0x4,	0x5,	0x6,
	0xE,	0x1E,	0x3E,	0x7E,	0xFE,	0x1FE
};

/* number of bit of DC Huffman coefficient */
int DCbit[12]=
{
	2,	3,	3,	3,	3,	3,	4,	5,	6,	7,	8,	9
};

/* AC Huffman coefficient */
short acHuffman[16][11]=
{
	0x000A,	0x0000,	0x0001,	0x0004,	0x000B,	0x001A,	0x0078,	0x00F8,	0x03F6,	0xFF82,	0xFF83,
	0xFFFF,	0x000C,	0x001B,	0x0079,	0x01F6,	0x07F6,	0xFF84,	0xFF85,	0xFF86,	0xFF87,	0xFF88,
	0xFFFF,	0x001C,	0x00F9,	0x03F7,	0x0FF4,	0xFF89,	0xFF8A,	0xFF8B,	0xFF8C,	0xFF8D,	0xFF8E,
	0xFFFF,	0x003A,	0x01F7,	0x0FF5,	0xFF8F,	0xFF90,	0xFF91,	0xFF92,	0xFF93,	0xFF94,	0xFF95,
	0xFFFF,	0x003B,	0x03F8,	0xFF96,	0xFF97,	0xFF98,	0xFF99,	0xFF9A,	0xFF9B,	0xFF9C,	0xFF9D,
	0xFFFF,	0x007A,	0x07F7,	0xFF9E,	0xFF9F,	0xFFA0,	0xFFA1,	0xFFA2,	0xFFA3,	0xFFA4,	0xFFA5,
	0xFFFF,	0x007B,	0x0FF6,	0xFFA6,	0xFFA7,	0xFFA8,	0xFFA9,	0xFFAA,	0xFFAB,	0xFFAC,	0xFFAD,
	0xFFFF,	0x00FA,	0x0FF7,	0xFFAE,	0xFFAF,	0xFFB0,	0xFFB1,	0xFFB2,	0xFFB3,	0xFFB4,	0xFFB5,
	0xFFFF,	0x01F8,	0x7FC0,	0xFFB6,	0xFFB7,	0xFFB8,	0xFFB9,	0xFFBA,	0xFFBB,	0xFFBC,	0xFFBD,
	0xFFFF,	0x01F9,	0xFFBE,	0xFFBF,	0xFFC0,	0xFFC1,	0xFFC2,	0xFFC3,	0xFFC4,	0xFFC5,	0xFFC6,
	0xFFFF,	0x01FA,	0xFFC7,	0xFFC8,	0xFFC9,	0xFFCA,	0xFFCB,	0xFFCC,	0xFFCD,	0xFFCE,	0xFFCF,
	0xFFFF,	0x03F9,	0xFFD0,	0xFFD1,	0xFFD2,	0xFFD3,	0xFFD4,	0xFFD5,	0xFFD6,	0xFFD7,	0xFFD8,
	0xFFFF,	0x03FA,	0xFFD9,	0xFFDA,	0xFFDB,	0xFFDC,	0xFFDD,	0xFFDE,	0xFFDF,	0xFFE0,	0xFFE1,
	0xFFFF,	0x07F8,	0xFFE2,	0xFFE3,	0xFFE4,	0xFFE5,	0xFFE6,	0xFFE7,	0xFFE8,	0xFFE9,	0xFFEA,
	0xFFFF,	0xFFEB,	0xFFEC,	0xFFED,	0xFFEE,	0xFFEF,	0xFFF0,	0xFFF1,	0xFFF2,	0xFFF3,	0xFFF4,
	0x07F9,	0xFFF5,	0xFFF6,	0xFFF7,	0xFFF8,	0xFFF9,	0xFFFA,	0xFFFB,	0xFFFC,	0xFFFD,	0xFFFE
};

/* number of bit of AC Huffman coefficient */
int ACbit[16][11]=
{
	4,	2,	2,	3,	4,	5,	7,	8,	10,	16,	16,
	0,	4,	5,	7,	9,	11,	16,	16,	16,	16,	16,
	0,	5,	8,	10,	12,	16,	16,	16,	16,	16,	16,
	0,	6,	9,	12,	16,	16,	16,	16,	16,	16,	16,
	0,	6,	10,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	7,	11,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	7,	12,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	8,	12,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	9,	15,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	9,	16,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	9,	16,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	10,	16,	16,	16,	16,	16,	16,	16,	16,	16,
	0,	10,	16,	16,	16,	16,	16,	16,	16,	16,	16,	
	0,	11,	16,	16,	16,	16,	16,	16,	16,	16,	16,	
	0,	16,	16,	16,	16,	16,	16,	16,	16,	16,	16,	
	11,	16,	16,	16,	16,	16,	16,	16,	16,	16,	16
};

/* function of DCT and IDCT */
extern void dct(void);
extern void idct(void);
/* function of Quantization and Inverse-Quantization */
extern void quantization(void);
extern void iquantization(void);
/* function of Zigzag and Inverse-Zigzag */
extern void zigzag(void);
extern void izigzag(void);
/* function of Encoding */
extern void dc_encoding(void);
extern void ac_encoding(void);
/* function of Decoding */
extern void dc_decoding(void);


void main()
{
	int imageWidthBlocks, imageHeightBlocks, curBlockX, curBlockY, blockStart, pixelX, pixelY, curValue;

	/* Define the size of the image in blocks */
	imageHeightBlocks = IMAGE_LEN / BLOCK_LEN;
	imageWidthBlocks = IMAGE_LEN / BLOCK_LEN; // Here both are the same since square image

	// Loop through the different blocks - in a raster order
	for (curBlockY=0; curBlockY < imageHeightBlocks; curBlockY++)
	{
		for (curBlockX=0; curBlockX < imageWidthBlocks; curBlockX++)
		{
			blockStart = (curBlockY * BLOCK_LEN * IMAGE_LEN + curBlockX * BLOCK_LEN);
			// Now fill block[] with the values of the current block
			// Loop through pixels in current block
			for (pixelY=0; pixelY<BLOCK_LEN; pixelY++)
			{
				for(pixelX=0; pixelX<BLOCK_LEN; pixelX++)
				{
					// Grab the current pixel
					block[pixelY*BLOCK_LEN + pixelX] = (short) image_in[blockStart + pixelY*IMAGE_LEN + pixelX];
				} //pixelX
			} //pixelY

			dct();                // Perform DCT on the current block
			quantization();       // Perform Quantization
			zigzag();             // Perform Zigzag
			dc_encoding();        // Perform DC Encoding
			ac_encoding();        // Perform AC Encoding

			dc_decoding();        // Perform Inverse DC Decoding
			ac_decoding();        // Perform Inverse AC Decoding
			izigzag();            // Perform Inverse Zigzag
			iquantization();      // Perform Inverse Quantization
			idct();               // Perform the Inverse DCT

			// Fill the image_out variable with the output of the IDCT
			// Loop through pixels in current block
			for (pixelY=0; pixelY<BLOCK_LEN; pixelY++)
			{
				for(pixelX=0; pixelX<BLOCK_LEN; pixelX++)
				{
					// Grab the current pixel value
					curValue = block[pixelY*BLOCK_LEN + pixelX];
					// Hack to deal with negative numbers
					if (curValue < 0)
					{
						image_out[blockStart + pixelY*IMAGE_LEN + pixelX] = (unsigned char) (-curValue);
					} else {
						image_out[blockStart + pixelY*IMAGE_LEN + pixelX] = (unsigned char) curValue;
					} //if
				} //pixelX
			} //pixelY
		} //curBlockX
	} //curBlockY

	for (;;);    /* Wait */
}

⌨️ 快捷键说明

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