Achin 发表于 2012-2-3 14:57:40

LPC2478移植TJpgDec成功,分享用户自定义接口函数

#include "lpc24xx_types.h"
#include "ff.h"
#include "tjpgd.h"

#include "LCDConf.h"

// 以下3句宏定义在tjpgd.h中修改
#define        JD_SZBUF                2048/* Size of stream input buffer (should be multiple of 512) */
#define JD_FORMAT                1        /* Output RGB format 0:RGB888 (3 BYTE/pix), 1:RGB565 (1 WORD/pix) */
#define        JD_USE_SCALE        1        /* Use descaling feature for output */

#define DISP_XS    800
#define DISP_YS    600


static int MaskL = 0;
static int MaskR = DISP_XS - 1;
static int MaskT = 0;
static int MaskB = DISP_YS - 1;



u16 Covert_RGB565_to_BGR565(unsigned short inRGB)
{
    u16    outBGR = 0;
   
    u16 r,g,b;
    b = (inRGB    ) & 0x1F;
    g = (inRGB>> 5) & 0x3F;
    r = (inRGB>>11) & 0x1F;
    outBGR = (b << 11) | (g << 5) | r;


    return outBGR;
}


// 在屏幕上显示图片
// 屏幕的起始地址在LCD_UPBASE中保存
void lpc24xx_disp_blt        (
                            int left,      /* Left end (-32768 to 32767) */
                            int right,      /* Right end (-32768 to 32767, >=left) */
                            int top,      /* Top end (-32768 to 32767) */
                            int bottom,      /* Bottom end (-32768 to 32767, >=right) */
                            const uint16_t *pat    /* Pattern data */
                        )
{
    int yc, xc, xl, xs;
    uint16_t pd;
    uint16_t *pScrBuf;
   
    uint16_t i;


    if (left > right || top > bottom)
    {
      return;             /* Check varidity */
    }
    if (left > MaskR || right < MaskL|| top > MaskB || bottom < MaskT)
    {
      return;                /* Check if in active area */
    }
   
    yc = bottom - top+ 1;    /* Vertical size */
    xc = right- left + 1;
    xs = 0;                  /* Horizontal size and skip */

    if (top < MaskT)         /* Clip top of source image if it is out of active area */
    {
      pat+= xc * (MaskT - top);
      yc -= MaskT - top;
      top = MaskT;
    }
    if (bottom > MaskB)   /* Clip bottom of source image if it is out of active area */
    {
      yc -= bottom - MaskB;
      bottom = MaskB;
    }
    if (left < MaskL)         /* Clip left of source image if it is out of active area */
    {
      pat += MaskL - left;
      xc -= MaskL - left;
      xs += MaskL - left;
      left = MaskL;
    }
    if (right > MaskR)         /* Clip right of source image it is out of active area */
    {
      xc -= right - MaskR;
      xs += right - MaskR;
      right = MaskR;
    }

    // 已经计算出了要绘制的x方向pixels和y方向pixels
    // 上下左右的值已经校正为,为屏幕上的绝对位置,由此可以算出屏幕缓冲区的起始位置
   
    i = 0;
    do   /* Send image data */
    {
      pScrBuf = (uint16_t *)(LCD_UPBASE) + (top+i)*DISP_XS + left;
      i++;
      
      xl = xc;    // x方向的pixels
      do
      {
            pd = *pat++;
            *pScrBuf = Covert_RGB565_to_BGR565(pd);
            pScrBuf ++;
      } while (--xl);
      pat += xs;
    } while (--yc);
}


/*-----------------------------------*/
/* JPEG file loader                  */


/* User defined call-back function to input JPEG data */

UINT lpc24xx_tjd_input (
                            JDEC* jd,      /* Decoder object */
                            BYTE* buff,      /* Pointer to the read buffer (NULL:skip) */
                            UINT nd            /* Number of bytes to read/skip from input stream */
                        )
{
    UINT rb;
    FIL *fil = (FIL*)jd->device;    /* Input stream of this session */


    if (buff)   /* Read nd bytes from the input strem */
    {
      f_read(fil, buff, nd, &rb);
      return rb;      /* Returns number of bytes could be read */
    }
    else
    {
      return (f_lseek(fil, f_tell(fil) + nd) == FR_OK) ? nd : 0;/* Skip nd bytes on the input stream */
    }
}



/* User defined call-back function to output RGB bitmap */

UINT lpc24xx_tjd_output (
                            JDEC* jd,      /* Decoder object */
                            void* bitmap,    /* Bitmap data to be output */
                            JRECT* rect      /* Rectangular region to output */
                        )
{
    jd = jd;    /* Suppress warning (device identifier is not needed) */

//    特定的情况下返回0,停止解码,自行定义
//    if (!rect->left)   /* Check user interrupt at left end */
//    {
//      return 0;    /* Abort decompression */
//    }
   
//    /* Put the rectangular into the display */
    lpc24xx_disp_blt(rect->left, rect->right, rect->top, rect->bottom, (uint16_t*)bitmap);

    return 1;    /* Continue decompression */
}







// 绘制Jpeg
u32 Commander_DRAWJPEG(char *pPara)
{
    FIL   fJpeg;
    FRESULT FatFsRet;
   
    JDEC   jd;                                                                      /* Decoder object (124 bytes) */
    JRESULT rc;
    BYTE   scale;
   
    BYTE Buff __attribute__ ((aligned(4)));
   
    FatFs_Addons_MountDisk(_T("2:"));
    FatFsRet = f_open(&fJpeg,_T("2:/Test.jpg"),FA_READ);
    if(FatFsRet != FR_OK)
    {
      printf("打开 图片 失败!\n");
      
      f_close(&fJpeg);
      return FatFsRet;
    }
   
    rc = jd_prepare(&jd, lpc24xx_tjd_input, Buff, sizeof(Buff), &fJpeg);
    if (rc == JDR_OK)
    {
      for (scale = 0; scale < 3; scale++)                     //确定比例因子
      {
            if ((jd.width >> scale) <= 800 && (jd.height >> scale) <= 600)
            {
                break;
            }
      }
   
      rc = jd_decomp(&jd, lpc24xx_tjd_output, scale);         //开始解压JPEG文件
    }
    else
    {
      printf("图片解码失败!\n");
      f_close(&fJpeg);
      return FatFsRet;
    }
   
    f_close(&fJpeg);
    return 0;
}

19001579 发表于 2012-2-4 16:56:08

mark 正在找一个JPG编解码的程序。谢谢楼主

lf415744311 发表于 2012-3-3 22:52:10

楼主这个不错,学习了

muniao 发表于 2012-4-23 23:10:22

lz有没有移植教程啊?我移植后出现这样的警告。。
..\tjpgd.c(488): warning:#188-D: enumerated type mixed with another type
..\tjpgd.c:             if (b < 0) return 0 - b;                              /* Err: invalid code or input */
..\tjpgd.c:                               ^
..\tjpgd.c(492): warning:#188-D: enumerated type mixed with another type
..\tjpgd.c:                     if (e < 0) return 0 - e;                        /* Err: input */
..\tjpgd.c:                                       ^
..\tjpgd.c(510): warning:#188-D: enumerated type mixed with another type
..\tjpgd.c:                     if (b < 0) return 0 - b;                        /* Err: invalid code or input error */
..\tjpgd.c:                                       ^
..\tjpgd.c(518): warning:#188-D: enumerated type mixed with another type
..\tjpgd.c:                           if (d < 0) return 0 - d;                /* Err: input device */
..\tjpgd.c:                                             ^
..\tjpgd.c(831): warning:#188-D: enumerated type mixed with another type
..\tjpgd.c:                     rc = create_huffman_tbl(jd, seg, len);
..\tjpgd.c:                        ^
..\tjpgd.c(841): warning:#188-D: enumerated type mixed with another type
..\tjpgd.c:                     rc = create_qt_tbl(jd, seg, len);
..\tjpgd.c:                        ^
..\tjpgd.c: ..\tjpgd.c: 6 warnings, 0 errors
页: [1]
查看完整版本: LPC2478移植TJpgDec成功,分享用户自定义接口函数