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

Rgb image with transparency color will have transparency removed when being saved #763

Closed
4 tasks done
devedse opened this issue Oct 30, 2018 · 3 comments
Closed
4 tasks done
Projects
Milestone

Comments

@devedse
Copy link
Contributor

devedse commented Oct 30, 2018

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

When simply opening and saving the image below all transparency is removed.

Input:
snake

Output:
snake_output

Steps to Reproduce

The following code reproduces the problem:

using (var img = Image.Load(image1path))
{
    using (var fs = new FileStream(outputImage, FileMode.Create))
    {
        img.SaveAsPng(fs);
    }
}

Investigation into issue

To help you guys out a bit I did some investigation myself to see where the issue comes from. First of all I did see that my image was detected as an Rgb image but it still had a transparent title bar in it.

PNG Analyzer shows a Transparency header though:
image

Doing some debugging I finally found out what was causing the problem. When the image is loaded a bool: 'hasTrans' is set to true and the transparent color is set to 211, 211, 211. This basically means that everywhere in the image where the color 211, 211, 211 is used it will be rendered as transparent.

However the issue occurs when we save the image again. The hasTrans flag is not taken into account when saving the image and thus it will simply be stored as Rgb without any data about transparency.

Possible resolutions

As far as I can think of right now there's 2 possible resolutions:

  1. Store transparency data on the image metadata and use this when writing the image again
  2. If the data of an Rgb image contains a transparency value set to 0, save the image as Rgba32

I guess that both solution still have quite a bit of work to do though.

System Configuration

  • ImageSharp version: Github version as of 31-10-2018 and also Myget 1.0.0-dev002131
  • Other ImageSharp packages and versions:
  • Environment (Operating system, version and so on): Win10
  • .NET Framework version: .NET Core 2.1
  • Additional information:
@devedse
Copy link
Contributor Author

devedse commented Oct 31, 2018

I worked on implementing option 1 in the attached pull request. Can you have a look at it?

@JimBobSquarePants
Copy link
Member

Ah great stuff. I think I actually left a TODO in the code base regarding this.

@JimBobSquarePants JimBobSquarePants added this to To Do in ImageSharp via automation Nov 23, 2018
@JimBobSquarePants JimBobSquarePants added this to the 1.0.0-rc1 milestone Nov 23, 2018
@JimBobSquarePants
Copy link
Member

Fixed with #764

ImageSharp automation moved this from To Do to Done Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
ImageSharp
  
Done
Development

No branches or pull requests

2 participants