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

📄 jcmarker.c

📁 jpeg压缩C代码,包括库的源代码和一个测试程序的源代码
💻 C
字号:
/*
* jcmarker.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 routines to write JPEG datastream markers.
*/

#include "jpeg.h"

/* These marker codes are exported since applications and data source modules
* are likely to want to use them.
*/
typedef enum 
{
	M_SOF0  = 0xc0,
		M_SOF1  = 0xc1,
		M_SOF2  = 0xc2,
		M_SOF3  = 0xc3,
		
		M_SOF5  = 0xc5,
		M_SOF6  = 0xc6,
		M_SOF7  = 0xc7,
		
		M_JPG   = 0xc8,
		M_SOF9  = 0xc9,
		M_SOF10 = 0xca,
		M_SOF11 = 0xcb,
		
		M_SOF13 = 0xcd,
		M_SOF14 = 0xce,
		M_SOF15 = 0xcf,
		
		M_DHT   = 0xc4,
		
		M_DAC   = 0xcc,
		
		M_RST0  = 0xd0,
		M_RST1  = 0xd1,
		M_RST2  = 0xd2,
		M_RST3  = 0xd3,
		M_RST4  = 0xd4,
		M_RST5  = 0xd5,
		M_RST6  = 0xd6,
		M_RST7  = 0xd7,
		
		M_SOI   = 0xd8,
		M_EOI   = 0xd9,
		M_SOS   = 0xda,
		M_DQT   = 0xdb,
		M_DNL   = 0xdc,
		M_DRI   = 0xdd,
		M_DHP   = 0xde,
		M_EXP   = 0xdf,
		
		M_APP0  = 0xe0,
		M_APP1  = 0xe1,
		M_APP2  = 0xe2,
		M_APP3  = 0xe3,
		M_APP4  = 0xe4,
		M_APP5  = 0xe5,
		M_APP6  = 0xe6,
		M_APP7  = 0xe7,
		M_APP8  = 0xe8,
		M_APP9  = 0xe9,
		M_APP10 = 0xea,
		M_APP11 = 0xeb,
		M_APP12 = 0xec,
		M_APP13 = 0xed,
		M_APP14 = 0xee,
		M_APP15 = 0xef,
		
		M_JPG0  = 0xf0,
		M_JPG13 = 0xfd,
		M_COM   = 0xfe,
		
		M_TEM   = 0x01,
		
		M_ERROR = 0x100
} JPEG_MARKER;


// Emit a marker code
__inline void emit_marker (Bitstream *bs, int mark)
{
	*bs->ptr ++ = 0xFF;
	*bs->ptr ++ = (BYTE)mark;
}


// Emit a 2-byte integer; these are always MSB first in JPEG files
__inline void emit_2bytes (Bitstream *bs, int value)
{
	*bs->ptr ++ = (BYTE)(value >> 8);
	*bs->ptr ++ = (BYTE)value;
}

// Emit a DQT marker, Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking 
void emit_dqt (Bitstream *bs, unsigned short * qtbl, int index)
{
	int i;
	
	emit_marker(bs, M_DQT);
	
	emit_2bytes(bs, DCTSIZE2 + 1 + 2);
	
	*bs->ptr ++ = index;
	
	for (i = 0; i < DCTSIZE2; i++) // The table entries must be emitted in zigzag order.
		*bs->ptr ++ = (BYTE)qtbl[jpeg_natural_order[i]];
}


// Emit a DHT marker
void emit_dht (Bitstream *bs, JHUFF_TBL * htbl, int index)
{
	int length, i;
	
	emit_marker(bs, M_DHT);
	
	length = 0;
	for (i = 1; i <= 16; i++)
		length += htbl->bits[i];
	
	emit_2bytes(bs, length + 2 + 1 + 16);
	*bs->ptr ++ = index;
	
	for (i = 1; i <= 16; i++)
		*bs->ptr ++ = htbl->bits[i];
	
	for (i = 0; i < length; i++)
		*bs->ptr ++ = htbl->huffval[i];
}

// Emit a SOF marker
void emit_sof (Bitstream *bs, int code, int width, int height)
{
	int ci;
	
	emit_marker(bs, code);
	
	emit_2bytes(bs, 3 * 3/*cinfo->num_components*/ + 2 + 5 + 1); /* length */
	
	*bs->ptr ++ = 8;  //cinfo->data_precision
	emit_2bytes(bs, height);
	emit_2bytes(bs, width);
	
	*bs->ptr ++ = 3;//cinfo->num_components
	
	// Y
	*bs->ptr ++ = 0;
	*bs->ptr ++ = (2 << 4) + 2;
	*bs->ptr ++ = 0;

	// UV 
	for(ci = 1; ci < 3; ci ++)
	{
		*bs->ptr ++ = ci;
		*bs->ptr ++ = (1 << 4) + 1; // (compptr->h_samp_factor << 4) + compptr->v_samp_factor
		*bs->ptr ++ = 1; // compptr->quant_tbl_no
	}
}

// Emit a SOS marker
void emit_sos (Bitstream *bs)
{
	int i, td, ta;
	
	emit_marker(bs, M_SOS);
	
	emit_2bytes(bs, 2 * 3 + 2 + 1 + 3); // length
	
	*bs->ptr ++ = 3;
	
	for (i = 0; i < 3; i++) 
	{
		*bs->ptr ++ = i;
		td = (i > 0); //compptr->dc_tbl_no;
		ta = (i > 0); // compptr->ac_tbl_no
		*bs->ptr ++ = (td << 4) + ta;
	}
	
	*bs->ptr ++ = 0;			// Ss
	*bs->ptr ++ = DCTSIZE2-1;	// Se
	*bs->ptr ++ = 0;			// (cinfo->Ah << 4) + cinfo->Al
}

// Write datastream trailer.
void write_file_trailer (Bitstream *bs)
{
	emit_marker(bs, M_EOI);
}

/*
* Write a special marker.
* This is only recommended for writing COM or APPn markers.
* Must be called after jpeg_start_compress() and before
* first call to jpeg_write_scanlines() or jpeg_write_raw_data().
*/
int jpeg_write_marker (Bitstream *bs, int marker, const BYTE *dataptr, int datalen)
{
	//write_marker_header(cinfo, marker, datalen);
	if (datalen > (unsigned int) 65533)		// safety check
		return 0;
	
	emit_marker(bs, marker);
	
	emit_2bytes(bs, (int) (datalen + 2));	// total length
	
	memcpy(bs->ptr, dataptr, datalen); 
	bs->ptr += datalen;
	return 1;
}

/*
* Write datastream header.
* This consists of an SOI and optional APPn markers.
* We recommend use of the JFIF marker, but not the Adobe marker,
* when using YCbCr or grayscale data.  The JFIF marker should NOT
* be used for any other JPEG colorspace.  The Adobe marker is helpful
* to distinguish RGB, CMYK, and YCCK colorspaces.
* Note that an application can write additional header markers after
* jpeg_start_compress returns.
*/
int write_file_header (j_compress_ptr cinfo, Bitstream *bs, int width, int height)
{
	emit_marker(bs, M_SOI);	// first the SOI
	
	// 目前只输入 comment 信息, 考虑增加时间、版权等信息
	if(!jpeg_write_marker (bs, M_COM, "hikvision",9))
		return 0;
	
	// write frame header
	emit_dqt(bs, cinfo->quantval   , 0); // compptr->quant_tbl_no
	emit_dqt(bs, cinfo->quantval+64, 1); // compptr->quant_tbl_no
	
	emit_sof(bs, M_SOF0, width, height); // SOF code for baseline implementation
	
	// write scan header
	// Sequential mode: need both DC and AC tables
	emit_dht(bs, &cinfo->dc_huff_tbl_ptrs[0], 0);
	emit_dht(bs, &cinfo->ac_huff_tbl_ptrs[0], 0+0x10);
	emit_dht(bs, &cinfo->dc_huff_tbl_ptrs[1], 1);
	emit_dht(bs, &cinfo->ac_huff_tbl_ptrs[1], 1+0x10);
	
	emit_sos(bs);

	return 1;
}


⌨️ 快捷键说明

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