libjpeg ver. 6b jpeg_stdio_src vs jpeg_mem_src

后端 未结 3 969
长发绾君心
长发绾君心 2021-01-06 00:27

I am using Libjpeg version 6b. In version 8 they have a nice function to read data out of the memory called jpeg_mem_src(...), unfortunately ver. 6b does not ha

相关标签:
3条回答
  • 2021-01-06 01:18

    Write your own...

    /* Read JPEG image from a memory segment */
    static void init_source (j_decompress_ptr cinfo) {}
    static boolean fill_input_buffer (j_decompress_ptr cinfo)
    {
        ERREXIT(cinfo, JERR_INPUT_EMPTY);
    return TRUE;
    }
    static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
    {
        struct jpeg_source_mgr* src = (struct jpeg_source_mgr*) cinfo->src;
    
        if (num_bytes > 0) {
            src->next_input_byte += (size_t) num_bytes;
            src->bytes_in_buffer -= (size_t) num_bytes;
        }
    }
    static void term_source (j_decompress_ptr cinfo) {}
    static void jpeg_mem_src (j_decompress_ptr cinfo, void* buffer, long nbytes)
    {
        struct jpeg_source_mgr* src;
    
        if (cinfo->src == NULL) {   /* first time for this JPEG object? */
            cinfo->src = (struct jpeg_source_mgr *)
                (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
                SIZEOF(struct jpeg_source_mgr));
        }
    
        src = (struct jpeg_source_mgr*) cinfo->src;
        src->init_source = init_source;
        src->fill_input_buffer = fill_input_buffer;
        src->skip_input_data = skip_input_data;
        src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
        src->term_source = term_source;
        src->bytes_in_buffer = nbytes;
        src->next_input_byte = (JOCTET*)buffer;
    }
    

    and then to use it:

    ...
        /* Step 2: specify data source (eg, a file) */
        jpeg_mem_src(&dinfo, buffer, nbytes);
    ...
    

    where buffer is a pointer to the memory chunk containing the compressed jpeg image, and nbytes is the length of that buffer.

    0 讨论(0)
  • 2021-01-06 01:21

    Or you can also try to use GNU's fmemopen() function which should be declared in stdio.h header file.

    FILE * source = fmemopen(inbuffer, inlength, "rb");
    if (source == NULL)
    {
        fprintf(stderr, "Calling fmemopen() has failed.\n");
        exit(1);
    }
    
    // ...
    
    jpeg_stdio_src(&cinfo, source);
    
    // ...
    
    fclose(source);
    
    0 讨论(0)
  • 2021-01-06 01:25

    Answering to poor s093294 who has been waiting for an answer for more than a year. I can't comment, so creating a new answer is the only way.

    ERREXIT is a macro in libjpeg. Include jerror.h and you're all set.

    0 讨论(0)
提交回复
热议问题