Error decoding animated webp iOS

自古美人都是妖i 提交于 2019-12-13 12:25:35

问题


I've been struggling for two days to display an animated webp image in a UIImageView with no success whatsoever.

Mainly the problem is in the decoding step of the file which gives this error: VP8_STATUS_UNSUPPORTED_FEATURE.

I tried

https://github.com/seanooi/iOS-WebP

https://github.com/mattt/WebPImageSerialization

These projects provide code for creating UIImage with webp files and they work fine with images with no animation but they both fail with the same error as above when attempting to decode images with animation.

I am jailbroken and checking the filesystem I saw that Facebook's Messenger app has some of it's stickers in .webp format and also in the License they mention Google's "webp" library so I'm sure somehow it's possible.


回答1:


Managed to decode animated .webp using the code snippet at the top of this header which also contains explanations of the data structures used.

static NSDictionary* DecodeWebPURL(NSURL *url) {

            NSMutableDictionary *info = [NSMutableDictionary dictionary];
            NSMutableArray *images = [NSMutableArray array];
            NSData *imgData = [NSData dataWithContentsOfURL:url];

            WebPData data;
            WebPDataInit(&data);

            data.bytes = (const uint8_t *)[imgData bytes];
            data.size = [imgData length];

            WebPDemuxer* demux = WebPDemux(&data);

            int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
            int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
            uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);

            if (flags & ANIMATION_FLAG) {
                WebPIterator iter;
                if (WebPDemuxGetFrame(demux, 1, &iter)) {

                    WebPDecoderConfig config;
                    WebPInitDecoderConfig(&config);

                    config.input.height = height;
                    config.input.width = width;
                    config.input.has_alpha = iter.has_alpha;
                    config.input.has_animation = 1;
                    config.options.no_fancy_upsampling = 1;
                    config.options.bypass_filtering = 1;
                    config.options.use_threads = 1;
                    config.output.colorspace = MODE_RGBA;

                    [info setObject:[NSNumber numberWithInt:iter.duration] forKey:@"duration"];

                    do {
                        WebPData frame = iter.fragment;

                        VP8StatusCode status = WebPDecode(frame.bytes, frame.size, &config);
                        if (status != VP8_STATUS_OK) {
                            NSLog(@"Error decoding frame");
                        }

                        uint8_t *data = WebPDecodeRGBA(frame.bytes, frame.size, &width, &height);
                        CGDataProviderRef provider = CGDataProviderCreateWithData(&config, data, config.options.scaled_width  * config.options.scaled_height * 4, NULL);

                        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
                        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaLast;
                        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

                        CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
                        [images addObject:[UIImage imageWithCGImage:imageRef]];
                        CGImageRelease(imageRef);
                        CGColorSpaceRelease(colorSpaceRef);
                        CGDataProviderRelease(provider);

                    } while (WebPDemuxNextFrame(&iter));

                    WebPDemuxReleaseIterator(&iter);
                }
            }
            WebPDemuxDelete(demux);
            [info setObject:images forKey:@"frames"];

            return info;
        }


来源:https://stackoverflow.com/questions/28231580/error-decoding-animated-webp-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!