SkiaSharp
SkiaSharp copied to clipboard
[BUG] Color of jpeg image changed after decode/encode
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.

I can confirm that using above image - using both bitmap and image - and the SkImage will make it entirely black on downscaling (#932).
What happens if you try SKImage.FromEncodedData? The bitmap loses it's colorspace information for backwards compatibility.
in this project I cannot access SKData but I did Q1 from here https://github.com/mono/SkiaSharp/issues/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.
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! 🍺🕺
just to confirm - old:

new - more saturated/vibrant colors on the skin tones:

it only seems to have effected a few "types" of images (their color type or space I presume)
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... https://github.com/mono/SkiaSharp/issues/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
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
We encountered this problem with another JPEG image. Adding the image here. images.zip Is there any estimate about this issue?
@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
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...
@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.

@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
@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.
@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
@AndersMad Here are input and output produced by the latest beta images.zip
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...
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.
@Ziriax Thank you for your suggestion. I tried setting colorspace explicitly, but, unfortunately, this does not help.
I'm interested to find out what is going on. I will debug this. Hopefully I can find the reason.
@AlexNosk confirmed that Adobe Photoshop (latest version) shows a color difference too.. So only Google Chromium display the Google Skia output correctly.
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
@mattleibow @AndersMad @Ziriax I encountered a similar issue with another Jpeg image. Attached the files for testing. images.zip
@mattleibow @AndersMad @ziriax , I to have this problem with different images. Attaching them to for testing images.zip