Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Color of jpeg image changed after decode/encode #931

Open
AlexNosk opened this issue Aug 16, 2019 · 24 comments
Open

[BUG] Color of jpeg image changed after decode/encode #931

AlexNosk opened this issue Aug 16, 2019 · 24 comments

Comments

@AlexNosk
Copy link

Description
After decoding and encoding image it's color is changed (blue color becomes brighter). The latest (1.68) version is used, tested on Windows.

Code

using (FileStream inputStream = File.Open(@"C:\Temp\in.jpeg", FileMode.Open))
using (SkiaSharp.SKBitmap bmp = SkiaSharp.SKBitmap.Decode(inputStream))
using (SkiaSharp.SKFileWStream outputStream = new SkiaSharp.SKFileWStream(@"C:\Temp\out.jpeg"))
{
SkiaSharp.SKPixmap.Encode(outputStream, bmp, SkiaSharp.SKEncodedImageFormat.Jpeg, 100);
}

Expected Behavior

The result image must look the same as input. Attached input and output images.
in
out

@AndersMad
Copy link

I can confirm that using above image - using both bitmap and image - and the SkImage will make it entirely black on downscaling (#932).

@mattleibow
Copy link
Contributor

What happens if you try SKImage.FromEncodedData? The bitmap loses it's colorspace information for backwards compatibility.

@AndersMad
Copy link

in this project I cannot access SKData but I did Q1 from here #1269 and that should be about the same right? being all codec and image - and see my Q to your A on Q1 there :) I'm am using that to test this (last) color bug with... I had this in my code too - must have been there for a (older) reason: if (!info.ColorSpace.IsSrgb) info = info.WithColorSpace(SKColorSpace.CreateSrgb());

but that, color space = null, codec + image only etc. it seems to lose some color data still (I'm am doing blind try'n'error here) - but I think I saw that the v2.80 made it "50%" better / closer to org. color - so most likely a Skia core thing.

@AndersMad
Copy link

AndersMad commented May 3, 2020

okay - so this might be fixed now - the only "cause" of the difference now is that Chrome shows the original image in a brighter color - but it's the only app that does so!.. windows explorer, adobe photoshop, paint .net, edge - all shows with the produced color.. so in my opinion v2.80 closes this one too! 🍺🕺

@AndersMad
Copy link

just to confirm - old:
5937-model-photo-OLD

new - more saturated/vibrant colors on the skin tones:
5937-model-photo-NEW

it only seems to have effected a few "types" of images (their color type or space I presume)

@mattleibow
Copy link
Contributor

okay - so this might be fixed now - the only "cause" of the difference now is that Chrome shows the original image in a brighter color - but it's the only app that does so!.. windows explorer, adobe photoshop, paint .net, edge - all shows with the produced color.. so in my opinion v2.80 closes this one too! 🍺🕺

Oooh, this reminds me of something... #1238

Might be that skia is using a codec that only Chrome understands. Try it in GIMP and see what happens.

See also: https://groups.google.com/forum/#!topic/skia-discuss/b8JtsaOw7_o

@AndersMad
Copy link

too me it seems like (not being concrete here) old image bad in chrome, new image (skia produced) a new kind of bad in all viewers.. new skia alpha version, old image bad in chrome, new image good in all viewers.. anyway - I'm happy with the new alpha :) so closed imho

@AlexNosk
Copy link
Author

AlexNosk commented Sep 16, 2020

We encountered this problem with another JPEG image. Adding the image here.
images.zip
Is there any estimate about this issue?

@AndersMad
Copy link

@AlexNosk as i said this has been fixed in the alpha - now its 2.84.0-preview.2. To prove it I scaled your image - here saved as WebP (but that should not matter - used to be prone to more "errors"): in.zip

@ziriax
Copy link
Contributor

ziriax commented Sep 16, 2020

FYI: The GIMP shows the images the same as Chrome...

When dumping the metadata with ExifTool, the in.jpeg gives an unknown color transform:

Color Transform                 : Unknown (RGB or CMYK)

I will try re-encoding using the official LibJpeg tools, and see...

@AlexNosk
Copy link
Author

@AndersMad Where can I get this (2.84.0-preview.2) build to test on my side? by the way the image you have attached still looks incorrect to me. Here is comparison. The top image is the original and the bottom is after resaving with SkiaSharp.
comparison

@AndersMad
Copy link

@AlexNosk I did save it as WebP - maybe theres a diff. there.. Try opening the image out formats in different browsers and editors to see - including the source image.

To get the beta version add this to your NuGet package source list: https://nugetized.blob.core.windows.net/skiasharp-eap/index.json

@AlexNosk
Copy link
Author

@AndersMad I have checked with the latest beta and IMHO the problem is still there. I have compared the input and output image in different browsers:
on Windows
IE - colors are different.
Edge - colors are the same
Chrome - colors are the same
on Mac
Safari - Colors are different
Chrome - Colors are different

In viewers and editors
on Windows
Standard Photos viewer - Colors are different
MS Paint - Colors are different
Paint 3D - Colors are different
on Mac
Standard Preview - Colors are different

Also checked on mobile devices -
on Android - Colors are the same
on iOS - Colors are different

The problem is that in our software SkiaSharp is used to process graphics when convert MS Word documents to different formats and our customers expect that colors they see in MS Word remain unchanged.

@AndersMad
Copy link

@AlexNosk but its better right ;) try send the out.jpg you generated with the beta and I will try it out in the latest Adobe Photoshop

@AlexNosk
Copy link
Author

@AndersMad Here are input and output produced by the latest beta
images.zip

@ziriax
Copy link
Contributor

ziriax commented Sep 22, 2020

I think this is related to #523

It was resolved by setting the colorspace explicitly. Maybe that helps in your case too?

Now, your JPEG files are 4 component CMYK or YCCK, see e.g. https://stackoverflow.com/questions/50798014/determining-color-space-for-jpeg

Not a lot of image software will handle these correctly I'm afraid...

@ziriax
Copy link
Contributor

ziriax commented Sep 22, 2020

Also, since no JPEG decoders support the CMYK to RGB conversion, Skia tries to do this manually. The source code contains some interesting comments:

https://source.chromium.org/chromium/chromium/src/+/master:third_party/skia/include/private/SkEncodedInfo.h;l=88?q=cmyk&ss=chromium%2Fchromium%2Fsrc:third_party%2Fskia%2F&start=11

        // JPEG
        // Photoshop actually writes inverted CMYK data into JPEGs, where zero
        // represents 100% ink coverage.  For this reason, we treat CMYK JPEGs
        // as having inverted CMYK.  libjpeg-turbo warns that this may break
        // other applications, but the CMYK JPEGs we see on the web expect to
        // be treated as inverted CMYK.

@AlexNosk
Copy link
Author

@ziriax Thank you for your suggestion. I tried setting colorspace explicitly, but, unfortunately, this does not help.

@ziriax
Copy link
Contributor

ziriax commented Sep 23, 2020

I'm interested to find out what is going on. I will debug this. Hopefully I can find the reason.

@AndersMad
Copy link

@AlexNosk confirmed that Adobe Photoshop (latest version) shows a color difference too.. So only Google Chromium display the Google Skia output correctly.

@ProDInfo
Copy link

ProDInfo commented Nov 6, 2020

I have the same problem.
A jpg file the pixel color is different to MS Paint or Paint.NET

I Atatch the image
481.zip

@AlexNosk
Copy link
Author

AlexNosk commented Mar 1, 2021

@mattleibow @AndersMad @ziriax I encountered a similar issue with another Jpeg image. Attached the files for testing.
images.zip

@mattleibow mattleibow added this to the v2.88.1 milestone May 22, 2022
@mattleibow mattleibow modified the milestones: v2.88.1, v2.88.x Planning Sep 11, 2022
@zleao
Copy link

zleao commented Nov 2, 2023

@mattleibow @AndersMad @ziriax , I to have this problem with different images. Attaching them to for testing
images.zip

@venetasoft
Copy link

venetasoft commented Oct 14, 2024

I’m experiencing the same issue with SkiaSharp NuGet 2.88.8 (.NET 8.0, Windows 11).

For some images, opening and saving an exact copy of them as a JPG (quality 100%) results in a slightly brighter image. Comparing all the pixels reveals that about 3% of them have changed.

These small differences are also visible when opening both the original image and the copy as new layers in Paint.NET, zooming in, and toggling the layers’ visibility.

Here’s the code I’m using:

// load a jpg form disk
var bmp = SKBitmap.Decode(jpgPath);

// save a copy
using (var data = bmp.Encode(SKEncodedImageFormat.Jpeg, 100))
using (var stream = File.OpenWrite(jpgPath + ".copy.jpg"))
data.SaveTo(stream);

This issue doesn’t occur with all images, and I’m not sure why. Attached is a JPG that suffers from this issue.

Original:
75
Copy:
75_copy

Saving the JPG as a PNG works perfectly, with pixel colors preserved. However, opening the newly created PNG and re-saving it as a JPG reproduces the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

7 participants