|
企圖解碼JPG的圖片并且代碼完全由自己來寫,那是不容易的,據(jù)我所知這種代碼完全可以當(dāng)做碩士研究生的畢業(yè)論文了.
我今天用的是Tjpg解碼庫庫函數(shù)大概1000行,顯示過程中除了需要Tjpg解碼庫提供的兩個(gè)庫函數(shù)
/* 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 */
};
之外,還需要自己寫兩個(gè)函數(shù).
先來看下其中一個(gè)庫函數(shù)的聲明:
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 */這是這個(gè)庫函數(shù)的第二個(gè)參數(shù)的聲明.這是一個(gè)指向函數(shù)的指針.
為了給這個(gè)參數(shù)賦值 首先要先寫一個(gè)函數(shù) 這個(gè)函數(shù)由庫函數(shù)調(diào)用.用來從磁盤讀取圖像的數(shù)據(jù)
UINT InputData(JDEC * jdec , BYTE *buff , UINT nbyte)
{
/* 這個(gè)函數(shù)將被jd_prepare 調(diào)用 調(diào)用此函數(shù)后希望將得到的數(shù)據(jù) 保存在buff為首地址的內(nèi)存中 數(shù)據(jù)的長度為nbyte */
if(buff)/* buff 如果不為NULL */
{
f_read(&fil , buff , nbyte ,&br);/* &fil 保存圖片數(shù)據(jù)的地址 */
return br;
}
else/* 否則將移動(dòng)數(shù)據(jù) */
{
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, ... );/* 根據(jù)我的實(shí)驗(yàn)這個(gè)函數(shù)的最后一個(gè)參數(shù)可以不用傳 但是為了能編譯通過 隨便傳一個(gè)指針就可以了 */
if(res==JDR_OK)
{
res=jd_decomp(&jdec,OutputData,0);
if(res==JDR_OK)
{
/* 成功 */
}
}
}
|
|