Bug 286127

Summary: Resize tool in digikam corrupt transparencies on png files
Product: [Applications] digikam Reporter: Salvatore Brigaglia <opensourcecat>
Component: Plugin-DImg-PNGAssignee: Digikam Developers <digikam-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: caulier.gilles
Priority: NOR    
Version: 2.3.0   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed In: 2.6.0
Sentry Crash Report:

Description Salvatore Brigaglia 2011-11-08 18:41:35 UTC
Version:           2.3.0 (using KDE 4.7.3) 
OS:                Linux

When resizing a PNG file with transparency the result image is corrupted in digikam main interface. That's valid for digikam image editor, digikam batch manager resize tool. But if you open the resulting image with gimpo or other viewer it's ok!

Reproducible: Always

Steps to Reproduce:
Try to resize this logo to 180px:
http://www.lyngsat-logo.com/hires/ss/sky_uk_movies_modern_greats.png
with digikam IE or Batch queue resize tool

Actual Results:  
The resulting image is ok but it is whown corrupted in digikam (if you open it in gimp og gwen it'sok)

Expected Results:  
the image is resized and the transparency is shown correctly

OS: Linux (x86_64) release 2.6.37.6-0.7-desktop
Compiler: gcc
Comment 1 caulier.gilles 2011-11-09 09:50:58 UTC
Do you use simple resize mode or restoration mode ?

Gilles Caulier
Comment 2 caulier.gilles 2011-11-09 09:56:18 UTC
In normal and restorations modes, using your test image and Showfoto 2.3.0, i cannot reproduce the problem. Alpha channel still here.

In digiKam Image Editor do you use versionning ? If yes, by default JPEG format is used to archive version, and JPEG do not store alpha channel 

Gilles Caulier
Comment 3 caulier.gilles 2011-11-09 10:01:04 UTC
I tested too Batch Queue Manager, and Alpha channel is preserved...

Gilles Caulier
Comment 4 caulier.gilles 2011-11-09 10:03:06 UTC
Ok, i can reproduce the dysfunction.

It's not resize tool, it's thumbnail and preview mode which cannot display aplha channel properly. I can reproduce the problem with original PNG image. Just try to open it in preview mode (F3)

Gilles Caulier
Comment 5 caulier.gilles 2011-11-09 10:07:08 UTC
ok, I found the problem :

In preview mode we don't display the full image, but preview JPEG embedded in IPTC metadata. There is no Alpha channel in this image.

To render thumbnails, we use also the preview image from IPTC.

To solve this issue, we must use a format which store alpha channel. To continu to use JPEG compression, JNG can be a solution, or PGF which use wavelets compression.

Gilles Caulier
Comment 6 Marcel Wiesweg 2011-11-26 15:56:42 UTC
The problem also arises if the image is stored as a JPEG: apparently, the PNG has pixels with 0 alpha but non-null RGB, as a left-over from its creation. When the image is "converted" to RGB by simply truncating the alpha channel, these reappear.
Comment 7 Marcel Wiesweg 2011-11-26 18:57:50 UTC
As we store the preview in JPEG, it seems better not to store a preview at all if there is transparency. We'd need to blend on white, the image may be altered significantly by this.
Comment 8 caulier.gilles 2011-11-26 22:28:35 UTC
Marcel,

Another alternative is to store preview in another format supporting alpha, as JNG, which is JPEG +alpha for the web. image codec is in libpng :

http://en.wikipedia.org/wiki/JPEG_Network_Graphics

Another possible format is PGF for ex...

Gilles Caulier
Comment 9 Marcel Wiesweg 2011-11-27 15:55:32 UTC
But would any other software than digikam be capable to use this preview?
Comment 10 caulier.gilles 2011-11-27 16:17:41 UTC
they can, if we specify the image format in dedicated IPTC tag.

look here :

https://projects.kde.org/projects/kde/kdegraphics/libs/libkexiv2/repository/revisions/master/entry/libkexiv2/kexiv2image.cpp#L864

none JNG or PGF are registered in Iptc.Application2.PreviewFormat.

look format list given by exiftool by tag "ObjectPreviewFileFormat" :

http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html

We can registered new value as 100 or 1000 with JNG of PGF to be sure to not be in conflict with another one.

Note : why not to migrate preview data from IPTC to XMP now, exemple into digiKam namespace ? We can do what we want into it (:=)))

Gilles Caulier
Comment 11 Marcel Wiesweg 2011-11-27 20:24:13 UTC
Git commit 8b2e921abf7ed0a38a3ac5318c88e58e775a7068 by Marcel Wiesweg.
Committed on 27/11/2011 at 21:23.
Pushed by mwiesweg into branch 'master'.

Provide methods to remove an alpha channel from an image. Remove alpha channel before saving to JPEG.

- hasTransparentPixels checks if, given there is an alpha channel,
  some pixels actually is transparent (alpha < 1.0)
- bitBlendImageOnColor allows to compose an image over a constant color
  This is equivalent to create a DImg, fill it with a color,
  blend the first image over the new one, and replace the first image with the result.
- removeAlphaChannel blends the image on a color, usually, white, and removes the alpha channel
- in updateMetadata:
  ensure that the previous embedded preview is always removed
  only add a new check that a preview is only added if the original image is significantly
  larger than the intended preview size
  To be discussed and currently unchanged: handling of transparent pixels

CCBUG: 286127

M  +189  -25   libs/dimg/dimg.cpp
M  +32   -0    libs/dimg/dimg.h

http://commits.kde.org/digikam/8b2e921abf7ed0a38a3ac5318c88e58e775a7068
Comment 12 Marcel Wiesweg 2012-02-20 11:35:39 UTC
Git commit 4543c0c0281379ced0be448665081a33fd0cac80 by Marcel Wiesweg.
Committed on 20/02/2012 at 12:33.
Pushed by mwiesweg into branch 'master'.

Disable creation of a preview for images with transparency
until a solution involving a better file format is available.

M  +2    -1    NEWS
M  +6    -1    libs/dimg/dimg.cpp

http://commits.kde.org/digikam/4543c0c0281379ced0be448665081a33fd0cac80