找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5897|回复: 0
打印 上一主题 下一主题
收起左侧

JPG图片C语言解码显示例子-期末倾情奉献

[复制链接]
跳转到指定楼层
楼主
ID:90014 发表于 2015-9-14 18:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
     企图解码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)
        {
            /* 成功 */
        }

    }
}












分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表