1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > c语言读取jpg字节 JPG图片C语言解码显示例子-期末倾情奉献

c语言读取jpg字节 JPG图片C语言解码显示例子-期末倾情奉献

时间:2021-10-07 09:10:29

相关推荐

c语言读取jpg字节 JPG图片C语言解码显示例子-期末倾情奉献

企图解码JPG的图片并且代码完全由自己来写,那是不容易的,据我所知这种代码完全可以当做硕士研究生的毕业论文了.

我今天用的是Tjpg解码库库函数大概1000行,显示过程中除了需要Tjpg解码库提供的两个库函数

/* Error code */

typedef enum {

JDR_OK = 0, /* 0: Succeeded */

JDR_INTR, /* 1: Interrupted by output function */

JDR_INP, /* 2: Device error or wrong termination of input stream */

JDR_MEM1, /* 3: Insufficient memory pool for the image */

JDR_MEM2, /* 4: Insufficient stream input buffer */

JDR_PAR, /* 5: Parameter error */

JDR_FMT1, /* 6: Data format error (may be damaged data) */

JDR_FMT2, /* 7: Right format but not supported */

JDR_FMT3 /* 8: Not supported JPEG standard */

} JRESULT;

/* Rectangular structure */

typedef struct {

WORD left, right, top, bottom;

} JRECT;

/* Decompressor object structure */

typedef struct JDEC JDEC;

struct JDEC {

UINT dctr;/* Number of bytes available in the input buffer */

BYTE* dptr;/* Current data read ptr */

BYTE* inbuf; /* Bit stream input buffer */

BYTE dmsk;/* Current bit in the current read byte */

BYTE scale;/* Output scaling ratio */

BYTE msx, msy; /* MCU size in unit of block (width, height) */

BYTE qtid[3]; /* Quantization table ID of each component */

SHORT dcv[3]; /* Previous DC element of each component */

WORD nrst;/* Restart inverval */

UINT width, height; /* Size of the input image (pixel) */

BYTE* huffbits[2][2]; /* Huffman bit distribution tables [id][dcac] */

WORD* huffcode[2][2]; /* Huffman code word tables [id][dcac] */

BYTE* huffdata[2][2]; /* Huffman decoded data tables [id][dcac] */

LONG* qttbl[4]; /* Dequaitizer tables [id] */

void* workbuf; /* Working buffer for IDCT and RGB output */

BYTE* mcubuf; /* Working buffer for the MCU */

void* pool;/* Pointer to available memory pool */

UINT sz_pool; /* Size of momory pool (bytes available) */

UINT (*infunc)(JDEC*, BYTE*, UINT);/* Pointer to jpeg stream input function */

void* device; /* Pointer to I/O device identifiler for the session */

};

之外,还需要自己写两个函数.

先来看下其中一个库函数的声明:

JRESULT jd_prepare (

JDEC* jd,/* Blank decompressor object */

UINT (*infunc)(JDEC*, BYTE*, UINT),/* JPEG strem input function */

void* pool,/* Working buffer for the decompression session */

UINT sz_pool,/* Size of working buffer */

void* dev/* I/O device identifier for the session */

)

UINT (*infunc)(JDEC*, BYTE*, UINT),/* JPEG strem input function */这是这个库函数的第二个参数的声明.这是一个指向函数的指针.

为了给这个参数赋值 首先要先写一个函数 这个函数由库函数调用.用来从磁盘读取图像的数据

UINT InputData(JDEC * jdec , BYTE *buff , UINT nbyte)

{

/* 这个函数将被jd_prepare调用 调用此函数后希望将得到的数据 保存在buff为首地址的内存中 数据的长度为nbyte */

if(buff)/* buff 如果不为NULL */

{

f_read(&fil , buff , nbyte ,&br);/* &fil 保存图片数据的地址 */

return br;

}

else/* 否则将移动数据 */

{

return(f_lseek(&fil, f_tell(dev->fp) + nbyte) == FR_OK) ? nbyte : 0;

}

}

UINT OutputData(JDEC *jdec , void bitmap , JRECT *rect)

{

/* */

LCD_Disp(rect->left,rect->top,rect->right,rect->bottom,(uint16_t*)bitmap);

return 1;

}

int main(void)

{

JDEC jdec;

JRESULT res;

uint8_t work[4096];

/* 首先要打开文件 */

/*......*/

res=jd_prepare(&jdec,InputData,work,4096, ... );/* 根据我的实验这个函数的最后一个参数可以不用传 但是为了能编译通过 随便传一个指针就可以了 */

if(res==JDR_OK)

{

res=jd_decomp(&jdec,OutputData,0);

if(res==JDR_OK)

{

/* 成功 */

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。