JPEG support with ijg - getting access violation

前端 未结 7 1248
余生分开走
余生分开走 2021-01-06 12:00

I was recently trying to update my game to store graphics in compressed formats (JPEG and PNG).

Whilst I ended up settling on a different library, my initial attempt

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 12:02

    I agree with Hernán. This is not a good interface (I think the internal code itself is probably good), unless you really need to work low-level (and maybe not even then). I think ImageMagick is probably better. They have a "MagickWand" C interface that is more high level, not to mention that it supports many more formats.

    However, I was curious about libjpeg's interface, so I got a test program working to my satisfaction, based on your example program as well as libjpeg.doc, the IJG example, and USING THE IJG JPEG LIBRARY. Anyway, here's the code. It just prints out the dimensions, and the RGB of the first pixel of every row.

    I am very surprised you get an error with my code. It works fine for me, and compiles without any warnings. Can someone else test it?

    #include 
    #include 
    #include 
    
    int main(int argc, char* argv[])
    {
        struct jpeg_decompress_struct cinfo;
        struct jpeg_error_mgr jerr;
        JSAMPARRAY buffer;
        int row_stride;
    
        //initialize error handling
        cinfo.err = jpeg_std_error(&jerr);
    
        FILE* infile;
        infile = fopen("Sample.jpg", "rb");
        assert(infile != NULL);
    
        //initialize the decompression
        jpeg_create_decompress(&cinfo);
    
        //specify the input
        jpeg_stdio_src(&cinfo, infile);
    
        //read headers
        (void) jpeg_read_header(&cinfo, TRUE);
    
        jpeg_start_decompress(&cinfo);
    
        printf("width: %d, height: %d\n", cinfo.output_width, cinfo.output_height);
    
        row_stride = cinfo.output_width * cinfo.output_components;
    
        buffer = (*cinfo.mem->alloc_sarray)
            ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
    
        JSAMPLE firstRed, firstGreen, firstBlue; // first pixel of each row, recycled
        while (cinfo.output_scanline < cinfo.output_height)
        {
        (void)jpeg_read_scanlines(&cinfo, buffer, 1);
        firstRed = buffer[0][0];
        firstBlue = buffer[0][1];
        firstGreen = buffer[0][2];
        printf("R: %d, G: %d, B: %d\n", firstRed, firstBlue, firstGreen);
        }
    
        jpeg_finish_decompress(&cinfo);
    
        return 0;
    }
    

提交回复
热议问题