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

📄 jpeg_demo.c

📁 jpeg codec on both Linux and windows target
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************************** 
*
*    Description:
*      Scale down a JPEG image
*
***************************************************************************************/

//------------------------------ C Standard Include Files -----------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//---------------------------------- Some type definition ------------------------------  
 
typedef int             INT32;
typedef unsigned int    UINT32;

typedef short           INT16;
typedef unsigned short  UINT16;

typedef char            INT8;
typedef unsigned char   UINT8;

typedef enum
{
    IMAGE_FORMAT_JPEG           = 0,
    IMAGE_FORMAT_YUV_400        = 1,

    IMAGE_FORMAT_PACKED_YUV_420 = 2,
    IMAGE_FORMAT_PACKED_YUV_411 = 3,
    IMAGE_FORMAT_PACKED_YUV_422 = 4,
    IMAGE_FORMAT_PACKED_YUV_444 = 5,
    IMAGE_FORMAT_PACKED_RGB_444 = 6,
    IMAGE_FORMAT_PACKED_BGR_444 = 7,

    IMAGE_FORMAT_PLANAR_YUV_420 = 8, 
    IMAGE_FORMAT_PLANAR_YUV_422 = 9,
    IMAGE_FORMAT_PLANAR_YUV_444 = 10,
    IMAGE_FORMAT_PLANAR_RGB_444 = 11, 
    IMAGE_FORMAT_PLANAR_BGR_444 = 12 
} IMAGE_FORMAT; 


//-------------------------- JPEG Enc/Dec interface functions -------------------------
#ifdef __cplusplus 
extern "C" 
#endif
UINT16  SRI_JPEG_Decoder(   UINT8  *decoder_input_stream,
                            UINT8  **decoded_image_buffer, 
                            UINT16 *decoder_image_format,  
                            UINT16 *image_height,
                            UINT16 *image_width ); 

#ifdef __cplusplus 
extern "C" 
#endif
UINT8* SRI_JPEG_Encoder (   UINT8  *yuv_image_to_encode,
                            UINT8  *encoded_jpeg_buffer_ptr, 
                            UINT32 quality_factor, 
                            UINT32 encoder_image_format, 
                            UINT32 image_width, 
                            UINT32 image_height);
 
#ifdef __cplusplus 
extern "C" 
#endif
UINT8 SRI_Scale_PackedYUV444(UINT8 *source_image,  
                            UINT16  source_image_height,
                            UINT16  source_image_width,
                            UINT8  *dest_image, 
                            UINT16  dest_image_height,
                            UINT16  dest_image_width);

// -------------------------------- Private Constants and Types --------------------------
#define FILENAME_SIZE 255


// ----------------------------- Local Function Prototypes ------------------------------
static void    usage(          char *str );

int ParseCommands( int    argc, 
                  char    *argv[], 
                  char    input_filename[], 
                  char    output_filename[], 
                  UINT16   *ScaledImageHeight,
                  UINT16   *ScaledImageWidth,
                  UINT16   *ScaledImageQuality,
                  UINT16   *MaintainResultionRatio,
                  UINT16   *DebugImageOutput,
                  UINT16   *InputFormat,
                  UINT16   *VerboseMode);

//=================================================================================
//  Function name:  main
//  Description:    The starting point of the program
//  Parameters:     standard command line input
//=================================================================================
int main(int argc, char *argv[])
{
    // -----------------------------  Application input/output Variables -----------------------------------
    char    input_filename[FILENAME_SIZE]   = "";
    char    output_filename[FILENAME_SIZE]  = ""; 
    char    debug_filename[FILENAME_SIZE]   = ""; 
    UINT16  ScaledImageWidth = 0;
    UINT16  ScaledImageHeight = 0;   
    UINT16  ScaledImageQuality = 95;  
    UINT16  MaintainResultionRatio = 0; 
    UINT16  DebugImageOutput = 0;
    UINT16  InputFormat = 0;
    UINT16  VerboseMode = 0;

    //-------------------------------------- Buffer for decoder  --------------------------------------------
    UINT8   *decoder_input_stream   =  NULL;    // Buffer to hold the compressed and undecoded JPEG bitstream data, read from JPEG file
    UINT8   *decoded_image_buffer   =  NULL;    // Buffer to store the decoded image in raw YUV pixles 

    //-------------------------------------- Buffer for encoder  --------------------------------------------
    UINT8   *ScaledImage_YUV444     =  NULL;    // scaled image in packed 444 format
    UINT8   *encoded_jpeg_buffer    =  NULL;    // Buffer to hold compressed JPEG bitstream
    UINT8   *encoded_jpeg_buffer_ptr=  NULL;    // Pointer to point to the encoding bit position
    UINT8   *ImageToEncode          =  NULL;    // image to encoder

    //------------------------------------------ File pointers  --------------------------------------------
    FILE    *DecoderFile    =  NULL;
    FILE    *EncoderFile    =  NULL;
    FILE    *fdebug         =  NULL;

    //-------------------------------------- Input JPEG image info -----------------------------------------
    UINT16  decoder_image_format;
    UINT16  encoder_image_format;
    UINT16  image_width, image_height; 
    UINT32  FileStartPos;
    UINT32  FileLength;
    INT32   ret;
    UINT32  Hfactor;
    UINT32  Vfactor;
    
 

    //-----------------------------------   Process command line arguments  --------------------------------
    if( !ParseCommands( argc, argv, input_filename, output_filename,  
                        &ScaledImageHeight,  &ScaledImageWidth, &ScaledImageQuality, 
                        &MaintainResultionRatio, &DebugImageOutput, &InputFormat, &VerboseMode))
    {
        return -1;
    }

    //-------------------------    check for input/output filenames and parameter -------------------------   
    if(strlen(input_filename) == 0 )
    {
        printf("input filename not specified\n");
        exit(1);
    }

    if(strlen(output_filename) == 0 )
    {
        printf("output filename not specified\n");
        exit(1);
    }
 
    if (ScaledImageWidth == 0)
    {
        printf("output image width not specified\n");
        exit(1);
    }

    if (ScaledImageHeight == 0)
    {
        printf("output image height not specified\n");
        exit(1);
    }
      

    //-------------------------    Read in JPEG bitstream data from input file  -------------------------   
    DecoderFile = fopen (input_filename, "rb");
    if(!DecoderFile)
    {
        printf("Can not open input file: %s\n", input_filename);
        exit(0);
    }

    fseek(DecoderFile,0,0);
    FileStartPos = ftell(DecoderFile);
    fseek(DecoderFile,0,2);
    FileLength = ftell(DecoderFile)-FileStartPos;

    fseek(DecoderFile,0,0);
    decoder_input_stream = (UINT8 *)malloc(FileLength*sizeof(UINT8));
    if(!decoder_input_stream)
    {
        printf("Can not allocate memory\n");
        exit(0);
    }

    fread (decoder_input_stream, 1, FileLength, DecoderFile);
    fclose (DecoderFile);

    if (VerboseMode) 
        printf(" --------  Input image loading is OK ! -------- \n");

    //------------------------------------   Call the decoder   ---------------------------------------- 
    if (InputFormat == IMAGE_FORMAT_JPEG)
    {
        // Before the JPEG image is decoded, the application normally does not know the image size, thus it is 
        // hard for the application to pre-allocate image buffer to hold decoded raw image.
        // 'decoded_image_buffer' is allocated inside the decoder and returns the decoded image from the decoder.
        // After the application finishes using this buffer (displayed or saved to a file) and does not need it,
        // the application should free the buffer, there is no other function call to the JPEG decoder.
        decoded_image_buffer = NULL;
        ret = SRI_JPEG_Decoder (decoder_input_stream, 
                            &decoded_image_buffer, 
                            &decoder_image_format, 
                            &image_height, 
                            &image_width);

        if(ret != 0)  // Check if decoder failed
        {
            printf("\nError %d",ret);
            free(decoder_input_stream);
            free(decoded_image_buffer);
            exit(1);
        }
        else
        {
            if (VerboseMode) 
                printf(" --------  Input image is decoded OK ! -------- \n");
        }

        if (MaintainResultionRatio)
        {
            Hfactor = ((UINT32)image_width  << 8) / ScaledImageWidth;
            Vfactor = ((UINT32)image_height << 8) / ScaledImageHeight;

            if (Hfactor > Vfactor)  // H scale more than V
            {
                // The scale will be based H
                ScaledImageHeight = image_height * ScaledImageWidth / image_width;
            }
            else if (Vfactor > Hfactor)
            {
                // The scale will be based V
                ScaledImageWidth = image_width * ScaledImageHeight / image_height;
            }
        }

        //----------------------------------   Scale the decoded image  --------------------------------------- 
 
        ScaledImage_YUV444 = (UINT8*)malloc( ScaledImageWidth * ScaledImageHeight * 3 );
        SRI_Scale_PackedYUV444( decoded_image_buffer,  
                            image_height,
                            image_width,
                            ScaledImage_YUV444, 
                            ScaledImageHeight,
                            ScaledImageWidth);

        if (VerboseMode) 
            printf(" -------- Decoded image is scaled OK ! -------- \n");
     
       
        //----------------------------- Dump decoded raw image data (YUV) to a file ----------------------------
        if (DebugImageOutput)
        {
            sprintf(debug_filename,"decoded_image_buffer_%dx%d.yuv",image_height, image_width);
            fdebug= fopen (debug_filename, "wb");
            fwrite (decoded_image_buffer, 1, image_height*image_width*3, fdebug); 
            fclose (fdebug); 

            sprintf(debug_filename,"ScaledImage_YUV444_%dx%d.yuv",ScaledImageHeight, ScaledImageWidth);
            fdebug= fopen (debug_filename, "wb");
            fwrite (ScaledImage_YUV444, 1, ScaledImageHeight*ScaledImageWidth*3, fdebug); 
            fclose (fdebug);
        }   

⌨️ 快捷键说明

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