⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpegdecoderapi.c

📁 jpeg图像文件软件解码器的arm版本的源代码程序
💻 C
字号:
///////////////////////////////////////////////////////////////////////////////
//
//  jpegdecoderapi.h
//
//  DESCRIPTION
//         Define functions that implements the JPEG Decoder API.      
//
//
///////////////////////////////////////////////////////////////////////////////


#include "irom_PlatformDefines.h"
#include "jpegdecoderapi.h"
#include "JPEGFieldDec.h"
#include "JPEGDecoder.h"

// external
extern JPEGSaveMCU FindSaveMCUFunction
(
    JPEGFieldDec    *decodedImage
);

// function pointer type definition
typedef void* (*GetMemoryFunction)(OP_UINT32 size);
typedef void (*FreeMemoryFunction)(void *x);

// Global variables


// input buffer pointer
static OP_UINT8                 *jpegInputBuffer;

// output buffer pointer;
static OP_UINT8                 *jpegOutputBuffer;

// JPEG decoding options
static JPEG_DECODING_OPTIONS    jpegDecodingOptions;

// input buffer size
static OP_UINT32                jpegInputBufferSize;

// memory allocation function pointer
static GetMemoryFunction        jpegDynMemAllocate; 

// memory deallocate function pointer
static FreeMemoryFunction       jpegDynMemDeallocate;

// Thumbnail information
static ThumbnailInfo            jpegThumbnailInfo;

// image information
static JPEG_IMAGE_INFO          jpegImageInfo;

// the JPEG decoder object
static JPEGDecoder              jpegDecoder;

// decoder state
static JPEG_DECODER_STATE       jpegDecoderState = JPEG_DECODER_STATE_UNINITIALIZED;


// JPEG Decoder API Functions

/////////////////////////////////////////////////////////////////////////////
// NAME  
//            jpegDecoderInitialize
//
// DESCRIPTION
//            This function initializes the JPEG Decoder and sets the image  
//            decoding configurations to default values.
//            
// INPUTS   
//            none.        
//
// OUTPUTS  
//            none.    
//
// RETURN VALUE
//          none.
//           
/////////////////////////////////////////////////////////////////////////////
void jpegDecoderInitialize
(
    void
)
{
    // clear input and output buffer pointers
    jpegInputBuffer = 0;
    jpegOutputBuffer = 0;
    jpegInputBufferSize = 0;

    // clear image info structure
    jpegImageInfo.imageWidth = 0;    
    jpegImageInfo.imageHeight = 0;                   
    jpegImageInfo.hasThumbnail = OP_FALSE;                
    jpegImageInfo.thumbImageWidth = 0;      
    jpegImageInfo.thumbImageHeight = 0;          

    // set default decoding options
    jpegDecodingOptions.useThumbnail = OP_FALSE;

    // update decoder state
    jpegDecoderState = JPEG_DECODER_STATE_INITIALIZED;
}

/////////////////////////////////////////////////////////////////////////////
// NAME  
//            jpegSetInputBuffer
//
// DESCRIPTION
//            This function allows the Decoder Controller to pass the decoder
//            the input buffer pointer and the input buffer size
//            
// INPUTS   
//            inputBuffer - input buffer pointer
//            bufferSize - input buffer size        
//
// OUTPUTS  
//            none.    
//
// RETURN VALUE
//          none.
//           
/////////////////////////////////////////////////////////////////////////////
void jpegSetInputBuffer
(
    OP_UINT8    *inputBuffer, 
    OP_UINT32   bufferSize
)
{
    // check if we are in the right state
    if(jpegDecoderState == JPEG_DECODER_STATE_INITIALIZED)
    {
        // store input buffer info
        jpegInputBuffer = inputBuffer;
        jpegInputBufferSize = bufferSize;

        // get JPEG image size information
        if(JPEGGetJPEGFrameSize(jpegInputBuffer, &jpegImageInfo.imageWidth, 
                                &jpegImageInfo.imageHeight))
        {
            return;
        }

        // get JPEG image thumbnail information
        if(!JPEGGetThumbnailInfo(jpegInputBuffer, &jpegThumbnailInfo))
        {   
            // copy thumbnail info
            jpegImageInfo.hasThumbnail = OP_TRUE;     
            jpegImageInfo.thumbImageWidth = jpegThumbnailInfo.thumbnailWidth;            
            jpegImageInfo.thumbImageHeight = jpegThumbnailInfo.thumbnailHeight;           
        }        

        // update decoder state
        jpegDecoderState = JPEG_DECODER_STATE_READY;
    }
}
     

/////////////////////////////////////////////////////////////////////////////
// NAME  
//            jpegGetImageInfo
//
// DESCRIPTION
//            This function returns image information to Image Decoder 
//            Controller
//            
// INPUTS   
//            info - pointer to JPEG image info structure    
//
// OUTPUTS  
//            info - update value in this structure.    
//
// RETURN VALUE
//          JPEG_DECODER_ERR_NONE if successful, an error code otherwise.
//           
/////////////////////////////////////////////////////////////////////////////
JPEG_DECODER_ERR jpegGetImageInfo
(
    JPEG_IMAGE_INFO     *info
)
{
    // check if we are in the right state
    if(jpegDecoderState == JPEG_DECODER_STATE_READY)
    {
        // copy image info
        info->imageWidth = jpegImageInfo.imageWidth;
        info->imageHeight = jpegImageInfo.imageHeight;
        info->hasThumbnail = jpegImageInfo.hasThumbnail;
        info->thumbImageWidth = jpegImageInfo.thumbImageWidth;
        info->thumbImageHeight = jpegImageInfo.thumbImageHeight;

        return JPEG_DECODER_ERR_NONE;
    }
    else
    {
        return JPEG_DECODER_ERR_INVALID_STATE;
    }

    jpegDecoderState = JPEG_DECODER_STATE_INITIALIZED;
        
}    


/////////////////////////////////////////////////////////////////////////////
// NAME  
//            jpegDecodeImage
//
// DESCRIPTION
//            This function decodes the JPEG image
//            
// INPUTS   
//            outputBuffer - output buffer pointer
//            func - callback function        
//
// OUTPUTS  
//            outputBuffer - store result to output buffer    
//
// RETURN VALUE
//          JPEG_DECODER_ERR_NONE if successful, an error code otherwise.
//           
/////////////////////////////////////////////////////////////////////////////
JPEG_DECODER_ERR jpegDecodeImage
(
    OP_UINT8        *outputBuffer, 
    OP_BOOLEAN      func(OP_UINT16 x, OP_UINT16 y)
)
{
    OP_UINT16       errcode;

    // check if we are in the right state
    if(jpegDecoderState == JPEG_DECODER_STATE_READY)
    {
        // save output buffer pointer
        jpegOutputBuffer = outputBuffer;

        // update decoder state
        jpegDecoderState = JPEG_DECODER_STATE_ACTIVE;

        // check if we want to decode the thumbnail
        if(!jpegDecodingOptions.useThumbnail)
        {
            // decode the main image
            jpegDecoder.AllocateMemory = jpegDynMemAllocate;      
            jpegDecoder.Deallocate = jpegDynMemDeallocate;

            JPEGDecoderConstruct(&jpegDecoder);

            errcode = JPEGDecodeFrame(&jpegDecoder, &FindSaveMCUFunction, func, 
                                      jpegInputBufferSize, 32, jpegImageInfo.imageWidth, 
                                      jpegImageInfo.imageHeight, jpegInputBuffer, 
                                      CSF_OUTPUT_RGB, jpegOutputBuffer, 0, 0);

            JPEGDecoderRelease(&jpegDecoder);
        }
        else
        {
            if(jpegThumbnailInfo.thumbnailType == THUMBNAIL_JPEG)
            {
                // decode thumbnail in JPEG format
                jpegDecoder.AllocateMemory = jpegDynMemAllocate;      
                jpegDecoder.Deallocate = jpegDynMemDeallocate;

                JPEGDecoderConstruct(&jpegDecoder);

                errcode = JPEGDecodeFrame(&jpegDecoder, &FindSaveMCUFunction, func,
                                  jpegInputBufferSize + 
                                  (OP_UINT32)(jpegThumbnailInfo.thumbnailData - jpegInputBuffer),
                                  32, jpegImageInfo.thumbImageWidth, jpegImageInfo.thumbImageWidth, 
                                  jpegThumbnailInfo.thumbnailData, 
                                  CSF_OUTPUT_RGB, jpegOutputBuffer, 0, 0);

                JPEGDecoderRelease(&jpegDecoder);
            }
            else
            {
                // decode thumbnail in RGB or indexed color format
                errcode = JPEGDecodeThumbnail(&jpegThumbnailInfo, (JPEGOutputType*)jpegOutputBuffer);
            }
        }

        if(errcode == JPEG_OK)
        {
            return JPEG_DECODER_ERR_NONE;
        }
        else
        {
            return JPEG_DECODER_ERR_OPERATION_FAILED;
        }
    }
    else
    {
        return JPEG_DECODER_ERR_INVALID_STATE;
    }
}    


/////////////////////////////////////////////////////////////////////////////
// NAME  
//            jpegSetDynMemAllocFuncs
//
// DESCRIPTION
//            This function allows the Decoder Controller to pass the dynamic
//            memory allocation function pointers to the decoder
//            
// INPUTS   
//            allocate - memory allocation function pointer
//            deallocate - memory deallocation function pointer        
//
// OUTPUTS  
//            none.    
//
// RETURN VALUE
//          none.
//           
/////////////////////////////////////////////////////////////////////////////
void jpegSetDynMemAllocFuncs
(
    void    *allocate(OP_UINT32 size), 
    void    deallocate(void *x))
{
    jpegDynMemAllocate = allocate;
    jpegDynMemDeallocate = deallocate;
}


/////////////////////////////////////////////////////////////////////////////
// NAME  
//            jpegSetDecodingOptions
//
// DESCRIPTION
//            This sets JPEG decoding options
//            
// INPUTS   
//            options - pointer to JPEG decoding options structure    
//
// OUTPUTS  
//            none.    
//
// RETURN VALUE
//          JPEG_DECODER_ERR_NONE if successful, an error code otherwise.
//           
/////////////////////////////////////////////////////////////////////////////
JPEG_DECODER_ERR jpegSetDecodingOptions 
(
    JPEG_DECODING_OPTIONS   *options
)
{
    // check if we are in the right state
    if(jpegDecoderState == JPEG_DECODER_STATE_READY)
    {
        jpegDecodingOptions.useThumbnail = options->useThumbnail;
        
        return JPEG_DECODER_ERR_NONE;
    }
    else
    {
        return JPEG_DECODER_ERR_INVALID_STATE;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -