Skip to content

ar90n/mameJpeg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is mameJpeg ?

mameJpeg is a header only library for encoding/decoding JPEG images.

Example

#include "mameJpeg.h"

int main(int argc, char** argv)
{
    const char* input_path = "input_path_to_your_env";
    const char* output_path = "output_path_to_your_env";

    FILE* input_fp = fopen( input_file_path, "r" );
    if( input_fp == NULL )
    {
        fprintf(stderr, "cannot open %s\n", input_file_path);
        exit( -1 );
    }

    uint16_t width;
    uint16_t height;
    uint8_t component_num;
    size_t decode_work_buffer_size;
    bool ret = mameJpeg_getDecodeBufferSize( mameJpeg_input_from_file_callback,
                                             input_fp,
                                             &width,
                                             &height,
                                             &component_num,
                                             &decode_work_buffer_size );
    if( !ret )
    {
        fprintf(stderr, "cannot read jpeg info\n");
        fclose( input_fp );
        exit( -1 );
    }
    rewind( input_fp );

    printf("width         :%d\n", width );
    printf("height        :%d\n", height );
    printf("components    :%d\n", component_num );
    printf("working buffer:%lu\n", decode_work_buffer_size );

    size_t decode_image_buffer_size = component_num * width * height;
    uint8_t decode_image_buffer[ decode_image_buffer_size ];
    mameJpeg_memory_callback_param output_param = {
        .buffer_ptr = decode_image_buffer,
        .buffer_pos = 0,
        .buffer_size = decode_image_buffer_size
    };

    uint8_t decode_work_buffer[ decode_work_buffer_size];
    mameJpeg_context decode_context[1];
    ret = mameJpeg_initializeDecode( decode_context,
                                     mameJpeg_input_from_file_callback,
                                     input_fp,
                                     mameJpeg_output_to_memory_callback,
                                     &output_param,
                                     decode_work_buffer,
                                     decode_work_buffer_size );
    if( !ret )
    {
        fprintf(stderr, "cannot initialize for decoding\n");
        fclose( input_fp );
        exit( -1 );
    }
    
    ret = mameJpeg_decode( decode_context );
    fclose( input_fp );
    if( !ret )
    {
        fprintf(stderr, "cannot decode\n");
        exit( -1 );
    }

    FILE* output_fp = fopen( output_file_path, "w" );
    if( output_fp == NULL )
    {
        fprintf(stderr, "cannot open %s\n", output_file_path);
        fclose( output_fp );
        exit( -1 );
    }

    mameJpeg_memory_callback_param input_param = {
        .buffer_ptr = decode_image_buffer,
        .buffer_pos = 0,
        .buffer_size = decode_image_buffer_size
    };

    mameJpeg_format format = ( component_num == 1 ) ? MAMEJPEG_FORMAT_Y_444 : MAMEJPEG_FORMAT_YCBCR_444;
    size_t encode_work_buffer_size;
    mameJpeg_getEncodeBufferSize( width, height, format, &encode_work_buffer_size );

    uint8_t encode_work_buffer[ encode_work_buffer_size ];
    mameJpeg_context encode_context[1];
    mameJpeg_initializeEncode( encode_context,
            mameJpeg_input_from_memory_callback,
            &input_param,
            mameJpeg_output_to_file_callback,
            output_fp,
            width,
            height,
            format,
            encode_work_buffer,
            encode_work_buffer_size);

    mameJpeg_encode( encode_context );

    fclose( output_fp );
    return 0;
}

Features

  • only include mameJpeg.h.
  • support encoding and decoding for YCBCR400, YCBCR420, YCBCR422 and YCBCR444.
  • read and write data by input and output callback functions.

About

Header only plain jpeg codec

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages