Bug 154631 - Export as gif ignores transparence of objects
Summary: Export as gif ignores transparence of objects
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Draw (show other bugs)
Version:
(earliest affected)
7.5.1.2 release
Hardware: x86-64 (AMD64) All
: medium normal
Assignee: Julien Nabet
URL:
Whiteboard: target:7.6.0 target:7.5.3
Keywords:
Depends on:
Blocks:
 
Reported: 2023-04-05 21:16 UTC by Gerhard Schmidt
Modified: 2023-04-10 16:53 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
The two-page draw file (42.47 KB, application/vnd.oasis.opendocument.graphics)
2023-04-05 21:17 UTC, Gerhard Schmidt
Details
The produced gif (54.53 KB, image/gif)
2023-04-05 21:18 UTC, Gerhard Schmidt
Details
The export dialog (49.92 KB, image/png)
2023-04-05 21:19 UTC, Gerhard Schmidt
Details
exported gif file with test patch (57.65 KB, image/gif)
2023-04-08 15:21 UTC, Julien Nabet
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Gerhard Schmidt 2023-04-05 21:16:46 UTC
Description:
When I export drawings that include transparent objects as gif, the transparent objects are not displayed. Exporting those to .bmp or .png format works correct, but not to .gif.

brgs, gsc 

Steps to Reproduce:
1. Open the attached draw document, select page 2 and mark all objects on this page.
2. Open the export dialogue in the main menu, select "selection only" and the gif file format.
3. Press ok.

Actual Results:
See attached gif-file.

Expected Results:
That it works like in the earlier days. 


Reproducible: Always


User Profile Reset: No

Additional Info:
Selecting a dark red color as transparent background color in gif is a crazy idea. Normally that should be WHITE (0xFFFFFF). I have to edit the palette for each of my gifs with an ancient version of PaintShopPro to get this to white.
Comment 1 Gerhard Schmidt 2023-04-05 21:17:43 UTC
Created attachment 186503 [details]
The two-page draw file
Comment 2 Gerhard Schmidt 2023-04-05 21:18:38 UTC
Created attachment 186504 [details]
The produced gif
Comment 3 Gerhard Schmidt 2023-04-05 21:19:37 UTC
Created attachment 186505 [details]
The export dialog
Comment 4 Julien Nabet 2023-04-06 12:03:04 UTC
Code pointer:
https://opengrok.libreoffice.org/xref/core/vcl/source/filter/egif/egif.cxx?r=cfb2a587#256
    241 bool GIFWriter::CreateAccess( const BitmapEx& rBmpEx )
    242 {
    243     if( bStatus )
    244     {
    245         Bitmap aMask( rBmpEx.GetAlphaMask() );
    246 
    247         aAccBmp = rBmpEx.GetBitmap();
    248         bTransparent = false;
    249 
    250         if( !aMask.IsEmpty() )
    251         {
    252             if( aAccBmp.Convert( BmpConversion::N8BitTrans ) )
    253             {
    254                 aMask.Convert( BmpConversion::N1BitThreshold );
    255                 aAccBmp.Replace( aMask, BMP_COL_TRANS );
    256                 bTransparent = true;
    257             }

LO goes into line 256 but BMP_COL_TRANS is defined as 
#define BMP_COL_TRANS               Color( 252, 3, 251 )
See https://opengrok.libreoffice.org/xref/core/include/vcl/bitmap.hxx?r=5244e7ca

which would correspond to the red color.

git grep -n BMP_COL_TRANS gives:
include/vcl/bitmap.hxx:67:#define BMP_COL_TRANS               Color( 252, 3, 251 )
vcl/source/bitmap/bitmap.cxx:922:            Color aTrans( BMP_COL_TRANS );
vcl/source/filter/egif/egif.cxx:255:                aAccBmp.Replace( aMask, BMP_COL_TRANS );
vcl/source/filter/egif/egif.cxx:395:    m_rGIF.WriteUChar( m_pAcc->GetBestPaletteIndex( BMP_COL_TRANS ) );

Noel: any thoughts about this specific color for transparency?
Comment 5 Julien Nabet 2023-04-08 15:21:19 UTC
Created attachment 186545 [details]
exported gif file with test patch

This patch helps:
diff --git a/vcl/source/filter/egif/egif.cxx b/vcl/source/filter/egif/egif.cxx
index 3aae0478cbe8..f908b6235133 100644
--- a/vcl/source/filter/egif/egif.cxx
+++ b/vcl/source/filter/egif/egif.cxx
@@ -392,7 +392,7 @@ void GIFWriter::WriteImageExtension( tools::Long nTimer, Disposal eDisposal )
     m_rGIF.WriteUChar( 0x04 );
     m_rGIF.WriteUChar( cFlags );
     m_rGIF.WriteUInt16( nDelay );
-    m_rGIF.WriteUChar( m_pAcc->GetBestPaletteIndex( BMP_COL_TRANS ) );
+    m_rGIF.WriteUChar( static_cast<sal_uInt8>(m_pAcc->GetBestPaletteIndex( BMP_COL_TRANS )) );
     m_rGIF.WriteUChar( 0x00 );
 
     if( m_rGIF.GetError() )


GetBestPaletteIndex returns sal_uInt16 so convert it to sal_uInt8 before using WriteUChar.

However it's full transparency, not mid transparency, see the attachment.
Comment 6 Noel Grandin 2023-04-10 12:05:42 UTC
Julien's patch is correct, GIF does not (as far as I can tell) support partial transparency, it only has full or none transparency.

See specs linked from https://en.wikipedia.org/wiki/GIF
Comment 7 Julien Nabet 2023-04-10 12:46:31 UTC
(In reply to Noel Grandin from comment #6)
> Julien's patch is correct, GIF does not (as far as I can tell) support
> partial transparency, it only has full or none transparency.
> 
> See specs linked from https://en.wikipedia.org/wiki/GIF

Thank you Noel for the feedback, I had missed this info.
I've submitted the patch here:
https://gerrit.libreoffice.org/c/core/+/150184
Comment 8 Commit Notification 2023-04-10 13:36:51 UTC
Julien Nabet committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/3ebe2e7170b093bfd1c417b5907f713df591654a

tdf#154631: fix Export as gif ignores transparence of objects

It will be available in 7.6.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 9 Julien Nabet 2023-04-10 13:40:45 UTC
Fix cherry-picked on 7.5 branch waiting for feedback
https://gerrit.libreoffice.org/c/core/+/150195
Comment 10 Commit Notification 2023-04-10 16:53:11 UTC
Julien Nabet committed a patch related to this issue.
It has been pushed to "libreoffice-7-5":

https://git.libreoffice.org/core/commit/6f0f99b550ea22f781ef6894e043e175c0a22fc7

tdf#154631: fix Export as gif ignores transparence of objects

It will be available in 7.5.3.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.