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

(nullable NSData *)sd_encodedWebpDataWithImage returns NIL after 0.14.2 update #94

Closed
DanielZanchi opened this issue Jan 25, 2024 · 12 comments · Fixed by #95
Closed

(nullable NSData *)sd_encodedWebpDataWithImage returns NIL after 0.14.2 update #94

DanielZanchi opened this issue Jan 25, 2024 · 12 comments · Fixed by #95
Labels
encoding WebP Encoding related

Comments

@DanielZanchi
Copy link

After updating the framework to 0.14.2 if I try to get the data of a clear (transparent) UIImage it will return NIL.

- (nullable NSData *)sd_encodedWebpDataWithImage
enters line 904 and returns NIL:

    if (error != kvImageNoError) {
        return nil;
    }
@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 26, 2024

clear (transparent) UIImage

Can you provide a reproduce demo ? Or code to trigger this.

After updating the framework to 0.14.2

From which version to 0.14.2 ? Is there any iOS version bump or min-deployment-target bump during the version upgrade?

I can't find anything related to WebP Encoding during the recent changes.

@dreampiggy dreampiggy added the encoding WebP Encoding related label Jan 26, 2024
@DanielZanchi
Copy link
Author

I don't have that problem with version 0.14.1.
after 0.14.2 i got that problem

@DanielZanchi
Copy link
Author

DanielZanchi commented Jan 29, 2024

to reproduce:
extension to create an image from a color:

extension UIColor {
    
	public func image(ofSize size: CGSize, scale: CGFloat = 1) -> UIImage {
		let format = UIGraphicsImageRendererFormat.default()
		format.scale = scale
        return UIGraphicsImageRenderer(size: size, format: format).image { rendererContext in
            self.setFill()
            rendererContext.fill(CGRect(origin: .zero, size: size))
        }
    }
    
}

create the image and get the WebP data:

let clearImage = UIColor.clear.image(ofSize: CGSize(width: 512, height: 512), scale: 1)
let data = SDImageWebPCoder.shared.encodedData(with: clearImage, format: .webP, options: [.encodeMaxFileSize: WhatsappLimits.maxStaticStickerFileSize - 10])

data will be nil

@dreampiggy
Copy link
Contributor

0.14.1...0.14.2

Is this cause the issue ?

image

But I don't think this vImage changes is wrong. Because the vImageBuffer_initWithCGImage should handle this case

Or this is Apple's vImage bug...They should match the documentation behavior:

See: https://developer.apple.com/documentation/accelerate/1399118-vimagebuffer_initwithcgimage

@dreampiggy
Copy link
Contributor

If this still cause issue, I can just revert this changes and re-release v0.14.3

@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 30, 2024

Your example code, seems want to encode 8-bit WebP CGImage (using RGBA8888) into the wide color (extended color) (RGBA16161616), this cause issue

@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 30, 2024

Seems suck on vImage's API

If the input color use monochome (White and Black only) CGImage

image

We want to encode the 8bit non-HDR WebP data

This need special code to handle this, or vImage will report error :(

image

@dreampiggy
Copy link
Contributor

But I can not revert to previous one, since that's color space is used to solve #90

A better solid fix, is:

  1. We only supports some number of color spaces (because of libwebp encoding the vImage convert color space)
  2. If your input image does not provide the color space we support, we will convert always into sRGBA (for example, a white color or black color CGImage only, which use monochrome colorspace)

@dreampiggy
Copy link
Contributor

Fixed, but I also found SDWebImage's SDImageCoderHelper CGImageCreateScaled contains the same issue.

We don't really happy with the Monochrome CGImage, you can always use [UIColor colorWithRed:0 green:0 blue:0 alpha:1] instead of clear color

Our encoding API in history, only prefers the rgba color, not white-alpha color, because this is the most common use case and we're not professional Image Editing library :)

@dreampiggy
Copy link
Contributor

@DanielZanchi Try using the #95 and test again

My test case can succefully encode this monochrome clear color

@DanielZanchi
Copy link
Author

@DanielZanchi Try using the #95 and test again

My test case can succefully encode this monochrome clear color

That works! Thanks

@dreampiggy
Copy link
Contributor

@DanielZanchi Use 0.14.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
encoding WebP Encoding related
Projects
None yet
2 participants