deli
deli

Reputation: 21

libjpeg image distortion issues

I am attempting to compress a RGB image into a JPEG using libjpeg from ijg. In the end the idea is to take an image with a image sensor and compress the data into a JPEG on an ARM Cortex M3 controller; however, for testing purposes I've been using BMP files converted to RGB .data files using GIMP.

I've been able to successfully compress the RGB data into a JPEG image on an SD card using the controller and libjpeg, but I am seeing a distorted line across the image using this method, and I am unsure what is causing this, and how to fix it.

Here's the compressed JPEG image: https://i.sstatic.net/PlzJr.jpg

Here's the source BMP image: https://i.sstatic.net/dLCXG.png

Here's relevant code snippets:

static uint8_t* _pucImageSrc ;
static uint8_t* _pucImageDst ;
static uint8_t*_pucCapturedBuf;

uint8_t _compress_image_jpg(SJPEGTest *pImage, sdCardDataHandler *datahandler){
    SJpegData sJpegData;

    _pucImageDst=malloc(datahandler->sourceSize);
    _pucCapturedBuf = datahandler->sourcePtr;

    pImage->dwTimeC=getTicks();
    JpegData_Init( &sJpegData ) ;
    JpegData_SetSource( &sJpegData, _pucCapturedBuf, datahandler->sourceSize) ;
    JpegData_SetDestination( &sJpegData, _pucImageDst, datahandler->sourceSize ) ;
    JpegData_SetDimensions( &sJpegData, 512, 384, 3 ) ;
    JpegData_SetParameters( &sJpegData, 25, JPG_DATA_RGB, JPG_METHOD_IFAST ) ;

        if ( ijg_compress( &sJpegData ) == 0 ){
            pImage->dwTimeC=1+getTicks()-pImage->dwTimeC;
            pImage->dwFinalLength=sJpegData.dwDstLength;
            pImage->dwTimeD=getTicks();
        } else {
             return JPEG_CONVERSION_ERRORED;    
        }

    datahandler->destSize = sJpegData.dwDstLength;
    datahandler->destPtr = _pucImageDst;

    return JPEG_CONVERSION_COMPLETE;
}

extern uint32_t ijg_compress( SJpegData* pData ){
    struct jpeg_compress_struct cinfo ;
    struct my_error_mgr       jerr ;
    JSAMPROW row_pointer ;          /* pointer to a single row */

    assert( pData != NULL ) ;

    cinfo.err = jpeg_std_error( &jerr.pub ) ;
    if(setjmp(jerr.setjmp_buffer)){
        jpeg_destroy_compress(&cinfo);
        return 1;
    }
    jpeg_create_compress( &cinfo ) ;
    jpeg_mem_dest( &cinfo, &(pData->pucDst), (unsigned long*)&(pData->dwDstLength) ) ;

    cinfo.image_width      = pData->dwWidth ;
    cinfo.image_height     = pData->dwHeight ;
    cinfo.input_components = pData->dwBPP ;
    cinfo.in_color_space   = pData->eInput ;

    jpeg_set_defaults( &cinfo ) ;
    cinfo.dct_method = pData->eMethod ;
    jpeg_set_quality( &cinfo, pData->dwQuality, true ) ;
    jpeg_start_compress( &cinfo, true ) ;

    while ( cinfo.next_scanline < cinfo.image_height )
    {
        row_pointer = (JSAMPROW) &pData->pucSrc[cinfo.next_scanline*cinfo.image_width*cinfo.input_components] ;
        jpeg_write_scanlines( &cinfo, &row_pointer, 1 ) ;
    }

    jpeg_finish_compress( &cinfo ) ;
    jpeg_destroy_compress(&cinfo);

    return 0 ;
}

Upvotes: 2

Views: 177

Answers (1)

deli
deli

Reputation: 21

After having to port libjpeg to a new controller that did not have a certain library installed I was able to determine the issue. Some memory was not being free'd correctly due me at one point replacing free() calls with membag_free(), then reverting back to free() but missing one call. Doh! Thanks for the help!

Upvotes: 0

Related Questions