BaseJpegDeocde exampe program

Dependencies:   BaseJpegDecode Terminal BaseUsbHost mbed mbed-rtos

Fork of BaseJpegDecode by Norimasa Okamoto

SimpleJpegDecode.cpp

Committer:
va009039
Date:
2012-10-30
Revision:
4:7d88de31c55a
Child:
5:033432f9baf3

File content as of revision 4:7d88de31c55a:

#include "SimpleJpegDecode.h"

#define DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);} while(0);
#define ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};

SimpleJpegDecode::SimpleJpegDecode()
{
    for(int i = 0; i < 6; i++) {
        m_block_data[i] = new uint8_t[64];
        ASSERT(m_block_data[i]);
    }
    clearOnResult();
}

void SimpleJpegDecode::output(int mcu, int block, int scan, int value)
{
    int sc = (block < m_yblocks) ? 0 : 1;
    inputBLOCK(mcu, block, scan, value * qt[sc][scan]);
}

void SimpleJpegDecode::outputDC(int mcu, int block, int value)
{
    output(mcu, block, 0, value);
    DC_count++;
}

void SimpleJpegDecode::outputAC(int mcu, int block, int scan, int value)
{
    output(mcu, block, scan, value);
    AC_count++;
}

void SimpleJpegDecode::outputMARK(uint8_t c)
{
}

void SimpleJpegDecode::outputBLOCK(int mcu, int block, uint8_t* values)
{
    BLOCK_count++;

    memcpy(m_block_data[block], values, 64);
    if (block < m_yblocks+1) {
        return;
    }
    int mcu_x = mcu % (width/16);
    int mcu_y = mcu / (width/16);
    uint8_t yuv[3];
    if (m_yblocks == 2) {
        for(int y = 0; y < 8; y++) {
            for(int x = 0; x < 16; x++) {
                yuv[0] = m_block_data[x/8][y*8+x%8];
                yuv[1] = m_block_data[2][y*8+x/2];
                yuv[2] = m_block_data[3][y*8+x/2];
                onResult(mcu_x * 16 + x, mcu_y * 8 + y, yuv);
            }
        }
    } else if (m_yblocks == 4) {
        for(int y = 0; y < 16; y++) {
            for(int x = 0; x < 16; x++) {
                int block = (y/8)*2+x/8;
                yuv[0] = m_block_data[block][(y%8)*8+x%8];
                yuv[1] = m_block_data[4][(y/2)*8+x/2];
                yuv[2] = m_block_data[5][(y/2)*8+x/2];
                onResult(mcu_x * 16 + x, mcu_y * 16 + y, yuv);
            }
        }
    } else {
        ASSERT(m_yblocks == 2 || m_yblocks == 4);
    }    
}

void SimpleJpegDecode::onResult(int x, int y, uint8_t* yuv)
{
  if(m_pCbItem && m_pCbMeth)
    (m_pCbItem->*m_pCbMeth)(x, y, yuv);
  else if(m_pCb)
    m_pCb(x, y, yuv);
}

void SimpleJpegDecode::setOnResult( void (*pMethod)(int, int, uint8_t*) )
{
  m_pCb = pMethod;
  m_pCbItem = NULL;
  m_pCbMeth = NULL;
}

void SimpleJpegDecode::clearOnResult()
{
  m_pCb = NULL;
  m_pCbItem = NULL;
  m_pCbMeth = NULL;
}