I've come across a difference in MATLAB and libjpg when it opens a JPEG file.
Basically if you open a JPEG file in libjpg (either using libjpg9 or using Gimp) and save the file as a bitmap (lossless), then open both the JPEG and BMP file into matlab as below:
A=imread('example1.jpg');
B=imread('example2.bmp');
C=abs(double(A(:,:,1))-double(B)(:,:,1));
Now I would expect that since they are the same image that array C would contain zeros but it turns out that libjpg is up to 5 shades out on each channel in either direction!
The difference is bigger in sharper images.
The most troubling thing is that I am trying to make a focus stacking tool and I was finding my output images were sharper in Matlab than my c++ program using libjpeg. I have been looking at the two for weeks and I've narrowed down the difference to when the JPEG files are loaded. It appears that MATLAB loads the images as being sharper (or higher quality) than libjpg. This is clear when you compare the results from my two scripts.
Has anyone else come across this problem? Why does libjpg9 interpret jpeg files differently than MATLAB? And why does libjpg9 lose quality when loading JPEGs?
EDIT:
For an example, please download this zip. Inside it has a JPG in the sRGB colour space, and a MATLAB m script. To use the example open the JPG in gimp and export it as a BMP. Then simply run the script and it will load and compare the two files.
https://dl.dropboxusercontent.com/u/15883001/JPGError.zip
I have found that JPGs that are saved with GIMP are fine, the problem appears to be with JPGs in the sRGB colour space.
In the example you can also see that each channel is sharper when reading the JPG into MATLAB. This is my main concern, slightly different colour representations are fine but loss of detail (i.e. loss of sharpness) is really bad.
EDIT2:
I have used 'Paint Shop Pro 5.0' to read in the JPG and save it as a BMP and it matches the MATLAB version exactly! I am using MATLAB 7.8.0 (R2009a)
I tried using the latest version of Paint.NET but it was off too but in a way slightly different to libjpg, it looked like it was more randomly off rather than losing detail like libjpg. I also confirmed that this was the case in matlab as the amount of edges detected are roughly similar. This is more what I would expect with slight differences in colour spaces, overall the detail was not lost in Paint.NET's interpretation of the JPG.
I am now thinking it is a bug in libjpg.
For info I am using Gimp 2.8.10, and Paint.NET v3.5.11.