ScottA
ScottA

Reputation: 61

Need example of jpeglib-turbo that works in VS2013 x64

I'm trying to learn how to use the jpeg-turbo library. And I'm have a devil of a time getting started. The example.c example in the doc folder, and every single example I find on the web, crashes in VS2013 when I try to read a .jpg file. They compile fine. But when I run them they crash with an access violation error.

What I really need is a tiny working (beginner friendly) example that is known to run properly in VS2013 x64. Including the main(){} code block code. And if there's anything special in the VS project properties that I might need to set that could be causing this crashing.

I'm pulling my hair out just trying to get one simple example working.

Thanks for the help.

*Edit-- Here is a very small example. I've also tried to get jpeglib to run with and without using Boost/GIL But it always crashes when loading the image: exception at 0x00000000774AE4B4 (ntdll.dll)

#include <stdio.h>
#include <assert.h>
#include <jpeglib.h>

#pragma warning(disable: 4996)

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("source.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); <----This guy seems to be the culprit

    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;
}

Upvotes: 0

Views: 979

Answers (2)

ScottA
ScottA

Reputation: 61

One more very important thing that I learned that I'd like to share. When writing to a new .jpg image. If the new image size is different than the source image. It will typically crash. Especially if the new size is larger than the source. I'm guessing this happens because it takes a much longer time to re-sample the color data to a different size. So this type of action might require it's own thread to prevent crashing.

I wasted a lot of time chasing code errors and compiler settings due to this one. So watch out for that one.

Upvotes: 1

ScottA
ScottA

Reputation: 61

I found the problem.

In my VS project's Linker->Input->Additional Dependencies. I changed it to use turbojpeg-static.lib. Or jpeg-static.lib when I'm using the non turbo enhanced libraries.

The turbojpeg.lib or jpeg.lib crashes for some reason when reading the image.

FYI, I am using the libjpeg-turbo-1.4.2-vc64.exe version with VS2013. And this is how I got it to work.

Upvotes: 1

Related Questions