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] Image background not transparent: becomes gray #244

Closed
gunnsth opened this issue Feb 4, 2020 · 2 comments
Closed

[BUG] Image background not transparent: becomes gray #244

gunnsth opened this issue Feb 4, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@gunnsth
Copy link
Contributor

gunnsth commented Feb 4, 2020

Description

A clear and concise description of what the bug is.
An issue report was received via email:

We have some problem with transparency when put an image into document.
Some of our images have not fully transparent background and when we put
this image into document this pixels becomes gray.

Possibly related to #180

Expected Behavior

Image is expected to have transparent background when opened in a PDF viewer.

Actual Behavior

  1. Run the provided example code
  2. View the output PDF
  3. Visually it is seen that the background is gray and not transparent as expected. Also looks like some kind of gray border around the image.

image

Attachments

A full example was included with the report.

@gunnsth gunnsth added the bug Something isn't working label Feb 4, 2020
@gunnsth
Copy link
Contributor Author

gunnsth commented Feb 4, 2020

We have seen a similar problem in the past where such gray shadows are created.

In that case, when we analyzed the image data with go and I could see that there are gray RGBA pixels with low alpha values A around 10-15. The range of A is 0-255 where 0 is transparent and 255 fully opaque. So, it makes sense that we see those gray areas. What does not make sense however, is that when I view this image in Chrome or Preview, I don't notice those gray areas. So I am not sure if some image viewers filter out some alpha values.

To solve this problem in the past we added functionality in unipdf to transform the alpha values. The mapping function takes in an alpha value and returns an alpha value. The following code worked well to make the image in the PDF without this gray shadow and and should be harmless to other signatures that do not have this issue:

This is implemented on model.Image type.

img.AlphaMap(func(alpha byte) byte {
   if alpha > 50 {
      return alpha
   } else {
      return 0 // Transparent
   }
})

However, for the present example this function is not accessible since its a creator.Image. Need to determine what is the best approach to address this.

Also with respect to related issue #180.

Note that I have not verified yet that this is the same problem, but looks similar and good to get this on record anyway.

@gunnsth
Copy link
Contributor Author

gunnsth commented Feb 10, 2020

After further analysis, it was found that the image was not clean, containing alpha values on the border. The following processing yields a clean image:

// Open image file.
imgFile, err := os.Open(imagePath)
if err != nil {
	log.Fatal(err)
}
defer imgFile.Close()

// Read image file.
img, err := model.ImageHandling.Read(imgFile)
if err != nil {
	log.Fatal(err)
}

// Remove image background noise.
img.AlphaMap(func(alpha byte) byte {
	// Increase/Decrease alpha threshold depending on the input image
	// noise profile.
	if alpha <= 245 {
		return 0
	}

	return alpha
})

// Add creator image.
cImg, err := c.NewImage(img)
if err != nil {
	log.Fatal(err)
}

creates a clean image in the PDF.

Note that this only solves this specific problem. For more advanced image processing, it would make sense to use a dedicated image processing library prior to embedding the image into unipdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant