Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jpegutil.c Source File

jpegutil.c

00001 #include "jpegutil.h"
00002 
00003 FILE *jpegfile;
00004 int jpeg_filesize = 0;
00005 int jpeg_filepos = 0;
00006 
00007 unsigned char pjpeg_need_bytes_callback(unsigned char* pBuf, unsigned char buf_size, unsigned char *pBytes_actually_read, void *pCallback_data)
00008 {
00009    unsigned int n = min((unsigned int)(jpeg_filesize - jpeg_filepos), (unsigned int)buf_size);
00010    if (n && (fread(pBuf, 1, n, jpegfile) != n))
00011       return PJPG_STREAM_READ_ERROR;
00012    *pBytes_actually_read = (unsigned char)(n);
00013    jpeg_filepos += n;
00014    return 0;
00015 }
00016 
00017 void ReadJPEGFromFile(const char *filename, NokiaLCD *lcd)
00018 {
00019     pjpeg_image_info_t imageInfo;
00020     jpegfile = fopen(filename,"rb");
00021     fseek(jpegfile, 0, SEEK_END);
00022     jpeg_filesize = ftell(jpegfile);
00023     jpeg_filepos = 0;
00024     fseek(jpegfile, 0, SEEK_SET);
00025     int status = pjpeg_decode_init(&imageInfo, pjpeg_need_bytes_callback, NULL);
00026    //const unsigned int row_pitch = imageInfo.m_width * imageInfo.m_comps;
00027    int mcu_x = 0;
00028    int mcu_y = 0;
00029 
00030    for ( ; ; )
00031    {
00032       status = pjpeg_decode_mcu();
00033 
00034       if (status)
00035       {
00036          if (status != PJPG_NO_MORE_BLOCKS)
00037          {
00038             //pc.printf("pjpeg_decode_mcu() failed with status %u\n", status);
00039             fclose(jpegfile);
00040             return;
00041          }
00042 
00043          break;
00044       }
00045 
00046       if (mcu_y >= imageInfo.m_MCUSPerCol)
00047       {
00048          fclose(jpegfile);
00049          return;
00050       }
00051             
00052       // Copy MCU's pixel blocks into the destination bitmap.
00053 
00054       for (int y = 0; y < imageInfo.m_MCUHeight; y += 8)
00055       {
00056          const int by_limit = min(8, imageInfo.m_height - (mcu_y * imageInfo.m_MCUHeight + y));
00057          for (int x = 0; x < imageInfo.m_MCUWidth; x += 8)
00058          {
00059 
00060             unsigned int src_ofs = (x * 8U) + (y * 16U);
00061             const unsigned char *pSrcR = imageInfo.m_pMCUBufR + src_ofs;
00062             const unsigned char *pSrcG = imageInfo.m_pMCUBufG + src_ofs;
00063             const unsigned char *pSrcB = imageInfo.m_pMCUBufB + src_ofs;
00064 
00065             const int bx_limit = min(8, imageInfo.m_width - (mcu_x * imageInfo.m_MCUWidth + x));
00066                         
00067             if (imageInfo.m_scanType == PJPG_GRAYSCALE)
00068             {
00069                for (int by = 0; by < by_limit; by++)
00070                {
00071                   for (int bx = 0; bx < bx_limit; bx++)
00072                   {
00073                       unsigned int color = ((*pSrcR++) << 16);   
00074                       (*lcd).pixel(mcu_x*imageInfo.m_MCUWidth+x+bx,mcu_y*imageInfo.m_MCUHeight+y+by,color);
00075                   }
00076                   pSrcR += (8 - bx_limit);
00077                }
00078             }
00079             else
00080             {
00081                for (int by = 0; by < by_limit; by++)
00082                {
00083                   for (int bx = 0; bx < bx_limit; bx++)
00084                   {
00085                      unsigned int color = ((*pSrcR++) << 16) | ((*pSrcG++) << 8) | (*pSrcB++);
00086                      (*lcd).pixel((130-imageInfo.m_width)/2+mcu_x*imageInfo.m_MCUWidth+x+bx,(130-imageInfo.m_height)/2+mcu_y*imageInfo.m_MCUHeight+y+by,color);
00087                   }
00088 
00089                   pSrcR += (8 - bx_limit);
00090                   pSrcG += (8 - bx_limit);
00091                   pSrcB += (8 - bx_limit);
00092 
00093                }
00094             }
00095          }
00096       }
00097 
00098       mcu_x++;
00099       if (mcu_x == imageInfo.m_MCUSPerRow)
00100       {
00101          mcu_x = 0;
00102          mcu_y++;
00103       }
00104    }
00105       
00106    fclose(jpegfile);
00107 }