Question

After couple of days I had to give up and submit this question. What I am trying to do basically is pretty straight forward - here is the code :

switch ( state )
{
case INIT:
        if (jpeg_read_header(&p_jpeg.info, true) == JPEG_SUSPENDED)
        {   
            return SUSPEND;
        }
        if (  p_jpeg.info.jpeg_color_space == JCS_CMYK  
           || p_jpeg.info.jpeg_color_space == JCS_YCCK ) return UNSUPPORTED;

        p_jpeg.info.out_color_space = JCS_RGB;
        p_jpeg.info.dct_method =  JDCT_FLOAT;
        p_jpeg.info.dither_mode = JDITHER_FS;
        p_jpeg.info.do_fancy_upsampling = TRUE;
        p_jpeg.info.enable_2pass_quant = FALSE;
        p_jpeg.info.do_block_smoothing = TRUE;
        jpeg_calc_output_dimensions(&p_jpeg.info);
        int row_size = p_jpeg.info.output_width * p_jpeg.info.output_components;
        p_jpeg.samples = (*p_jpeg.info.mem->alloc_sarray)((j_common_ptr) &p_jpeg.info,
                          JPOOL_IMAGE,
                          row_size, 1);

        state = START;
case START:
        if (!jpeg_start_decompress(&p_jpeg.info))
        {   
            return SUSPEND;
        }
        state = WORK;
case WORK:
    JSAMPARRAY samples = p_jpeg.samples;

    while (info->output_scanline < info->output_height) {
        if (jpeg_read_scanlines(info, samples, 1) != 1 ) //one scanline only
        {
            return SUSPEND;   // we need more data            
        }
        //copy line to destination
        char* line = (char*)samples[0];
        for (unsigned i = 0; i < info->output_width; ++i)
        {
            (*row).r  = *line++;
            (*row).g  = *line++;
            (*row).b  = *line++;
        }
   } //end while
   state = DONE;
case DONE:
        while (!jpeg_finish_decompress(&p_jpeg.info))
        {
            return SUSPEND;
        }
        return SUCCESS;
} //switch

And with all the files I have from various cameras and other sources everything works like a charm - image gets loaded into RGB buffer and voila! However what bothers me really is fact that I encountered certain JPG images that gets loaded badly distorted - what's worse - also example JPGS included with 8d distribution of libjpeg I am using gets distorted - which means I am doing something wrong probably. Could someone point me in the right direction please ? What is wrong with the code above that sometime works but in - let me stress than again in VERY few cases it gives me badly distorted image (look at attached example of reference image loaded with above code).

Thanks for any helps!

Original image file from libjpg 8d distribution: Original image file from libjpg 8d distribution

Image distortion I am getting in this case with the code above Image distortion I am getting in this case with the code above

PS: The code above is essential extract from the real code - which I guess is irrelevant anyway.

Was it helpful?

Solution

Ok , I finally managed to get it solved. The problem wasn't in image decoding / loading part but in texture generation part which was missing this little line :

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top