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

📄 jpeg_demo.c

📁 jpeg codec on both Linux and windows target
💻 C
📖 第 1 页 / 共 2 页
字号:
        
        encoder_image_format = IMAGE_FORMAT_PACKED_YUV_444;  
        ImageToEncode = ScaledImage_YUV444;
    }
    else
    {
        ImageToEncode = decoder_input_stream;
        encoder_image_format = InputFormat;  
    }

    EncoderFile  = fopen (output_filename, "wb"); 
 
    //------------------------------------   Call the encoder   ---------------------------------------- 
    
    // allocated buffer to hold encoded jpeg data. to be safe, we assume no compression
    encoded_jpeg_buffer=(UINT8 *)malloc(ScaledImageWidth * ScaledImageHeight * 3 ); 

    encoded_jpeg_buffer_ptr = encoded_jpeg_buffer;                   // encoded_jpeg_buffer_ptr points to the buffer start at beginning
    encoded_jpeg_buffer_ptr = SRI_JPEG_Encoder (ImageToEncode, 
                                            encoded_jpeg_buffer_ptr,
                                            ScaledImageQuality, 
                                            encoder_image_format, 
                                            ScaledImageWidth, 
                                            ScaledImageHeight);
     
    if (VerboseMode) 
        printf(" -------- Scaled image is encoded OK ! -------- \n");

    //------------------------   write encoded JPEG data to output file   ------------------------------ 
    fwrite (encoded_jpeg_buffer, 1, 
            encoded_jpeg_buffer_ptr - encoded_jpeg_buffer,   // encoded_jpeg_buffer_ptr points to the bitstream data end.
            EncoderFile); 
 

    if (VerboseMode)
        printf(" -------- Scaled image is written OK ! -------- \n");

    
    printf(" ==================== JPEG image has been successfully scaled ! ==================== \n");


    //--------------------------------------------   Clean up   ----------------------------------------- 
    fclose (EncoderFile);

    free(decoder_input_stream);
    free(decoded_image_buffer);  
        

    if (InputFormat == IMAGE_FORMAT_JPEG)
    {
        free(ScaledImage_YUV444);
    }

    free(encoded_jpeg_buffer);

    return (0);
}
  
//********************************************************************************************
//
//   To parse the command line parameters
//
//******************************************************************************************** 
INT32 ParseCommands( INT32  argc, 
                  char  *argv[], 
                  char  input_filename[], 
                  char  output_filename[], 
                  UINT16   *ScaledImageHeight,
                  UINT16   *ScaledImageWidth,
                  UINT16   *ScaledImageQuality,
                  UINT16   *MaintainResultionRatio,
                  UINT16   *DebugImageOutput,
                  UINT16   *InputFormat,
                  UINT16   *VerboseMode)
{
    INT32 i;
    INT32 tmp;

    for( i = 1; i < argc; ) 
    {
        if( argv[i][0] != '-' ) 
        {
            printf("Oops!, option %s does not start with - \n", argv[i]);
            usage(argv[0]);
        }

        switch(argv[i][1]) 
        {
            // input file
            case 'i':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }
                // select input filename
                strcpy( input_filename, argv[i++] );
                break;

            // output file
            case 'o':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }
                // select output filename
                strcpy( output_filename, argv[i++] );
                break;

            // width of scaled image
            case 'w':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }

                tmp = atoi( argv[i++] );

                if (tmp < 0)
                {
                     printf("Sorry!, -w width of scaled image cannot be negative number %d !\n", tmp);
                     return 0;
                }
                *ScaledImageWidth = (UINT16)tmp;
                break;

            // height of scaled image
            case 'h':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }

                tmp = atoi( argv[i++] );

                if (tmp < 0)
                {
                     printf("Sorry!, -h height of scaled image cannot be negative number %d !\n", tmp);
                     return 0;
                }
                *ScaledImageHeight = (UINT16)tmp;
                break;

            // quality of scaled image
            case 'q':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }
                tmp = atoi( argv[i++] );

                if (tmp < 0)
                {
                     printf("Sorry!, -q quality_level cannot be negative number %d !\n", tmp);
                     return 0;
                }
                
                *ScaledImageQuality = (UINT16)tmp;
                break;

            // maintain resolution ratio of scaled image
            case 'r':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }

                tmp = atoi( argv[i++] );

                if (tmp != 0 && tmp != 1)
                {
                     printf("Sorry!,-r maintain_resolution_ratio can only be 0 or 1, cannot be %d !\n", tmp);
                     return 0;
                }
                *MaintainResultionRatio = (UINT16)tmp;
                break;

            // debug image output
            case 'd':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }

                tmp = atoi( argv[i++] );

                if (tmp != 0 && tmp != 1)
                {
                     printf("Sorry!, -d debug_mode can only be 0 or 1, cannot be %d !\n", tmp);
                     return 0;
                }
                *DebugImageOutput = (UINT16)tmp;
                break;

            // verose mode, print JPEG information
            case 'v':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }

                tmp = atoi( argv[i++] );

                if (tmp != 0 && tmp != 1)
                {
                    printf("Sorry!, -v verose_mode can only be 0 or 1, cannot be %d !\n", tmp);
                    return 0;
                }
                *VerboseMode = (UINT16)tmp;
                break;

            // Input format, can be JPEG or raw YUV images.
            case 'f':
                i++;
                if( i >= argc )
                {
                    usage(argv[0]);
                }

                tmp = atoi( argv[i++] );

                if (tmp != 0 && tmp != 1 && tmp != 2 && tmp != 4 && tmp != 5 && tmp != 6 &&
                    tmp != 7 && tmp != 8 && tmp != 9 && tmp != 10 && tmp != 11 && tmp != 12)
                {
                    printf("Sorry!, -f input_format %d not support!\n", tmp);
                    return 0;
                }
                *InputFormat = (UINT16)tmp;
                break;

            // unknown args                                                                                                                                                                                                                                                                                                                                                                                              
            default:
                printf("Oops!, I do not recognize option %s\n", argv[i++]);
                usage(argv[0]);
                break;
         }    
    }

    return 1;
}

//********************************************************************************************
//
//   To show how to use the command line parameters
//
//******************************************************************************************** 
static void usage( char *str )
{
    // Usage error message 
    printf("\nUsage: %s -i input_file  \n", str);
    printf("                  -o output_file \n");
    printf("                  -w scaled_image_width \n");
    printf("                  -h scaled_image_height \n");
    printf("                  -q quality (0 to 100) \n");
    printf("                  -r maintain_resolution_ratio (0 or 1) \n");
    printf("                  -d debug_image_output(0 or 1)\n");
    printf("                  -f input_format(0: JPEG;  1:YUV400;  2:YUV420;  4:YUV422;  5:YUV444:  6:RGB444)\n");
    printf("                  -v verbose_mode (0 or 1) \n\n");

    printf("Example: JScale.exe -i Canon_001.JPG  -o Scaled_001.jpg -w 640 -h 480 -q 95 -r 1  -d 0 -v 1 -f 9\n"); 

    exit(1);
}

⌨️ 快捷键说明

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