Example #1
0
int JPEGDecoder::decode_mcu(void){

    status = pjpeg_decode_mcu();
    
    if (status)
    {
        is_available = 0 ;
        mcu_y = 0;       // <<<<<< Added to correct 2nd image bug
        delete pImage;   // <<<<<< Added to correct memory leak bug
#ifdef USE_SD_CARD
        g_pInFile.close();
#endif
        if (status != PJPG_NO_MORE_BLOCKS)
        {
            #ifdef DEBUG
            Serial.print("pjpeg_decode_mcu() failed with status ");
            Serial.println(status);
            #endif
            
            delete pImage;
            return -1;
        }
    }
    return 1;
}
int JPEGDecoder::decode_mcu(void){

    status = pjpeg_decode_mcu();
    
    if (status)
    {
        is_available = 0 ;

        g_pInFile.close();

        if (status != PJPG_NO_MORE_BLOCKS)
        {
            #ifdef DEBUG
            Serial.print("pjpeg_decode_mcu() failed with status ");
            Serial.println(status);
            #endif
            
            delete pImage;
            return -1;
        }
    }
    return 1;
}
Example #3
0
int main ( void )
{
    //FILE *fp;
    //FILE *fpout;
    unsigned char status;
    pjpeg_image_info_t pInfo;
    unsigned int sum;
    //unsigned int ra;
    unsigned int rb;
    //unsigned int x,rx;
    //unsigned int y,ry;


    //fp=fopen("base.bmp","rb");
    //if(fp==NULL) return(1);
    //dlen=fread(odat,1,122,fp);
    //fclose(fp);
    //if(dlen!=122) return(1);
    //fpout=fopen("out.bmp","wb");
    //if(fpout==NULL) return(1);
    //fwrite(odat,1,122,fpout);


    doff=0;

    status = pjpeg_decode_init(&pInfo, pjpeg_need_bytes_callback, NULL,0);

    if (status)
    {
        printf("pjpeg_decode_init() failed with status %u\n", status);
        return(1);
    }
    //x=0;
    //y=128-1;
    sum=0;
    while(status==0)
    {
        status=pjpeg_decode_mcu();
        //printf("decode %u\n",ra);
        if(status==0)
        {
            switch(pInfo.m_scanType)
            {
                case PJPG_GRAYSCALE: break;
                case PJPG_YH1V1:
                {
                    //rx=0;
                    //ry=0;
                    for(rb=0;rb<64;rb++)
                    {
                        sum+=pInfo.m_pMCUBufB[rb];
                        sum+=pInfo.m_pMCUBufG[rb];
                        sum+=pInfo.m_pMCUBufR[rb];
                        //odat[y-ry][x+rx][0]=pInfo.m_pMCUBufB[rb];
                        //odat[y-ry][x+rx][1]=pInfo.m_pMCUBufG[rb];
                        //odat[y-ry][x+rx][2]=pInfo.m_pMCUBufR[rb];
                        //rx++;
                        //if(rx>=8)
                        //{
                            //rx=0;
                            //ry++;
                        //}
                    }
                    //x+=8;
                    //if(x>=256)
                    //{
                        //y-=8;
                        //x=0;
                    //}
                    //// PJPG_H1V1: Each MCU contains is decoded to a single block of 8x8 RGB pixels.
////   unsigned char *m_pMCUBufR;
                    //for(rb=0;rb<64;rb++) sum+=pInfo.m_pMCUBufR[rb];
                    //fwrite(pInfo.m_pMCUBufR,1,64,fpout);
                    //fwrite(pInfo.m_pMCUBufR,1,64,fp);
   ////unsigned char *m_pMCUBufG;
                    //for(rb=0;rb<64;rb++) sum+=pInfo.m_pMCUBufG[rb];
                    //fwrite(pInfo.m_pMCUBufG,1,64,fpout);
                    //fwrite(pInfo.m_pMCUBufG,1,64,fp);
   ////unsigned char *m_pMCUBufB;
                    //for(rb=0;rb<64;rb++) sum+=pInfo.m_pMCUBufB[rb];
                    //fwrite(pInfo.m_pMCUBufB,1,64,fpout);
                    //fwrite(pInfo.m_pMCUBufB,1,64,fp);
                    break;
                }
                case PJPG_YH2V1: break;
                case PJPG_YH1V2: break;
                case PJPG_YH2V2: break;
            }
        }
    }
    if(status!=PJPG_NO_MORE_BLOCKS)
    {
        printf("status %u\n",status);
        return(1);
    }
    printf("----\n");
    printf("%d\n",pInfo.m_width);
    printf("%d\n",pInfo.m_height);
    printf("%d\n",pInfo.m_comps);
    printf("%d\n",pInfo.m_MCUSPerRow);
    printf("%d\n",pInfo.m_MCUSPerCol);
    printf("%d\n",pInfo.m_scanType);
    printf("%d\n",pInfo.m_MCUWidth);
    printf("%d\n",pInfo.m_MCUHeight);
    printf("---- sum 0x%08X %u\n",sum,sum);


    //fwrite(odat,1,sizeof(odat),fpout);
    //fclose(fpout);

    return(0);
}
Example #4
0
int main ( void )
{
    unsigned char status;
    pjpeg_image_info_t pInfo;
    unsigned int sum;
    unsigned int rb;



    doff=0;

    status = pjpeg_decode_init(&pInfo, pjpeg_need_bytes_callback,(void *)0,0);

    if (status)
    {
        return(1);
    }
    sum=0;
    while(status==0)
    {
        status=pjpeg_decode_mcu();
        //printf("decode %u\n",ra);
        if(status==0)
        {
            switch(pInfo.m_scanType)
            {
                case PJPG_GRAYSCALE: break;
                case PJPG_YH1V1:
                {
                    for(rb=0;rb<64;rb++)
                    {
                        sum+=pInfo.m_pMCUBufB[rb];
                        sum+=pInfo.m_pMCUBufG[rb];
                        sum+=pInfo.m_pMCUBufR[rb];
                    }
                    //// PJPG_H1V1: Each MCU contains is decoded to a single block of 8x8 RGB pixels.
////   unsigned char *m_pMCUBufR;
                    //for(rb=0;rb<64;rb++) sum+=pInfo.m_pMCUBufR[rb];
                    //fwrite(pInfo.m_pMCUBufR,1,64,fpout);
                    //fwrite(pInfo.m_pMCUBufR,1,64,fp);
   ////unsigned char *m_pMCUBufG;
                    //for(rb=0;rb<64;rb++) sum+=pInfo.m_pMCUBufG[rb];
                    //fwrite(pInfo.m_pMCUBufG,1,64,fpout);
                    //fwrite(pInfo.m_pMCUBufG,1,64,fp);
   ////unsigned char *m_pMCUBufB;
                    //for(rb=0;rb<64;rb++) sum+=pInfo.m_pMCUBufB[rb];
                    //fwrite(pInfo.m_pMCUBufB,1,64,fpout);
                    //fwrite(pInfo.m_pMCUBufB,1,64,fp);
                    break;
                }
                case PJPG_YH2V1: break;
                case PJPG_YH1V2: break;
                case PJPG_YH2V2: break;
            }
        }
    }
    if(status!=PJPG_NO_MORE_BLOCKS)
    {
        return(1);
    }
    printf("---- sum 0x%08X %u\n",sum,sum);
    return(0);
}
uint8_t *pjpeg_load_from_file(const char *pFilename, unsigned *x, unsigned *y, int *comps, pjpeg_scan_type_t *pScan_type, int reduce)
{
    pjpeg_image_info_t image_info;
    int mcu_x = 0;
    int mcu_y = 0;
    uint row_pitch;
    uint8_t *pImage;
    uint8_t status;
    uint decoded_width, decoded_height;
    uint row_blocks_per_mcu, col_blocks_per_mcu;
    
    *x = 0;
    *y = 0;
    *comps = 0;
    if (pScan_type) *pScan_type = PJPG_GRAYSCALE;
    
    g_pInFile = fopen(pFilename, "rb");
    if (!g_pInFile)
        return NULL;
    
    g_nInFileOfs = 0;
    
    fseek(g_pInFile, 0, SEEK_END);
    g_nInFileSize = ftell(g_pInFile);
    fseek(g_pInFile, 0, SEEK_SET);
    
    status = pjpeg_decode_init(&image_info, pjpeg_need_bytes_callback, NULL, (unsigned char)reduce);
    
    if (status)
    {
        printf("pjpeg_decode_init() failed with status %u\n", status);
        if (status == PJPG_UNSUPPORTED_MODE)
        {
            printf("Progressive JPEG files are not supported.\n");
        }
        
        fclose(g_pInFile);
        return NULL;
    }
    
    if (pScan_type)
        *pScan_type = image_info.m_scanType;
    
    // In reduce mode output 1 pixel per 8x8 block.
    decoded_width = reduce ? (image_info.m_MCUSPerRow * image_info.m_MCUWidth) / 8 : image_info.m_width;
    decoded_height = reduce ? (image_info.m_MCUSPerCol * image_info.m_MCUHeight) / 8 : image_info.m_height;
    
    row_pitch = decoded_width * 4;
    pImage = (uint8_t *)malloc(row_pitch * decoded_height);
    if (!pImage)
    {
        fclose(g_pInFile);
        return NULL;
    }
    
    row_blocks_per_mcu = image_info.m_MCUWidth >> 3;
    col_blocks_per_mcu = image_info.m_MCUHeight >> 3;
    
    for ( ; ; )
    {
        int y, x;
        uint8_t *pDst_row;
        
        status = pjpeg_decode_mcu();
        
        if (status)
        {
            if (status != PJPG_NO_MORE_BLOCKS)
            {
                printf("pjpeg_decode_mcu() failed with status %u\n", status);
                
                free(pImage);
                fclose(g_pInFile);
                return NULL;
            }
            
            break;
        }
        
        if (mcu_y >= image_info.m_MCUSPerCol)
        {
            free(pImage);
            fclose(g_pInFile);
            return NULL;
        }
        
        if (reduce)
        {
            // In reduce mode, only the first pixel of each 8x8 block is valid.
            pDst_row = pImage + mcu_y * col_blocks_per_mcu * row_pitch + mcu_x * row_blocks_per_mcu * image_info.m_comps;
            if (image_info.m_scanType == PJPG_GRAYSCALE)
            {
                *pDst_row = image_info.m_pMCUBufR[0];
            }
            else
            {
                uint y, x;
                for (y = 0; y < col_blocks_per_mcu; y++)
                {
                    uint src_ofs = (y * 128U);
                    for (x = 0; x < row_blocks_per_mcu; x++)
                    {
                        pDst_row[0] = image_info.m_pMCUBufR[src_ofs];
                        pDst_row[1] = image_info.m_pMCUBufG[src_ofs];
                        pDst_row[2] = image_info.m_pMCUBufB[src_ofs];
                        pDst_row += 3;
                        src_ofs += 64;
                    }
                    
                    pDst_row += row_pitch - 3 * row_blocks_per_mcu;
                }
            }
        }
        else
        {
            // Copy MCU's pixel blocks into the destination bitmap.
            pDst_row = pImage + (mcu_y * image_info.m_MCUHeight) * row_pitch + (mcu_x * image_info.m_MCUWidth * 4);
            
            for (y = 0; y < image_info.m_MCUHeight; y += 8)
            {
                const int by_limit = min(8, image_info.m_height - (mcu_y * image_info.m_MCUHeight + y));
                
                for (x = 0; x < image_info.m_MCUWidth; x += 8)
                {
                    uint8_t *pDst_block = pDst_row + x * 4;
                    
                    // Compute source byte offset of the block in the decoder's MCU buffer.
                    uint src_ofs = (x * 8U) + (y * 16U);
                    const uint8_t *pSrcR = image_info.m_pMCUBufR + src_ofs;
                    const uint8_t *pSrcG = image_info.m_pMCUBufG + src_ofs;
                    const uint8_t *pSrcB = image_info.m_pMCUBufB + src_ofs;
                    
                    const int bx_limit = min(8, image_info.m_width - (mcu_x * image_info.m_MCUWidth + x));
                    
                    if (image_info.m_scanType == PJPG_GRAYSCALE)
                    {
                        int bx, by;
                        for (by = 0; by < by_limit; by++)
                        {
                            uint8_t *pDst = pDst_block;
                            
                            for (bx = 0; bx < bx_limit; bx++)
                            {
                                *pDst++ = *pSrcR;
                                *pDst++ = *pSrcR;
                                *pDst++ = *pSrcR++;
                                *pDst++ = 0xFF;
                            }
                            
                            pSrcR += (8 - bx_limit);
                            
                            pDst_block += row_pitch;
                        }
                    }
                    else
                    {
                        int bx, by;
                        for (by = 0; by < by_limit; by++)
                        {
                            uint8_t *pDst = pDst_block;
                            
                            for (bx = 0; bx < bx_limit; bx++)
                            {
                                pDst[0] = *pSrcR++;
                                pDst[1] = *pSrcG++;
                                pDst[2] = *pSrcB++;
                                pDst[3] = 0xFF;
                                pDst += 4;
                            }
                            
                            pSrcR += (8 - bx_limit);
                            pSrcG += (8 - bx_limit);
                            pSrcB += (8 - bx_limit);
                            
                            pDst_block += row_pitch;
                        }
                    }
                }
                
                pDst_row += (row_pitch * 8);
            }
        }
        
        mcu_x++;
        if (mcu_x == image_info.m_MCUSPerRow)
        {
            mcu_x = 0;
            mcu_y++;
        }
    }
    
    fclose(g_pInFile);
    
    *x = decoded_width;
    *y = decoded_height;
    *comps = image_info.m_comps;
    
    return pImage;
}