Skip to content

Commit

Permalink
webp支持
Browse files Browse the repository at this point in the history
  • Loading branch information
lingtonke committed Mar 16, 2018
1 parent 73232f3 commit 1dcfb5f
Show file tree
Hide file tree
Showing 16 changed files with 2,508 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ - (IBAction)reload:(id)sender
size:0],
[ImageURLPrefix stringByAppendingString:@"gif/1.gif"],
[ImageURLPrefix stringByAppendingString:@"gif/3.gif"],
[ImageURLPrefix stringByAppendingString:@"webp/test.webp"],
];
self.imageView.scaleMode = self.scaleModeSwitch.selectedSegmentIndex;
self.imageView.predrawEnabled = self.predrawSwitch.on;
Expand Down
4 changes: 4 additions & 0 deletions LKImageKitExample/Class/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "ExampleCustomPropertyViewController.h"
#import "ExampleImageWallOptimizedViewController.h"
#import "ExampleImageWallViewController.h"
#import "LKImageWebPDecoder.h"

@implementation MainViewControllerCell

Expand Down Expand Up @@ -45,6 +46,9 @@ - (void)awakeFromNib
- (void)viewDidLoad
{
[super viewDidLoad];
LKImageConfiguration *config = [LKImageConfiguration defaultConfiguration];
config.decoderList = [config.decoderList arrayByAddingObject:[[LKImageWebPDecoder alloc] init]];
[[LKImageManager defaultManager] setConfiguration:config];
//[LKImageNetworkFileLoader clearCache];
}

Expand Down
29 changes: 29 additions & 0 deletions LKImageKitExample/LKImageViewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
073AD0F31FEB7B0A00DEAE34 /* ExampleImageWallViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 073AD0F21FEB7B0A00DEAE34 /* ExampleImageWallViewController.m */; };
073AD0F51FEB840400DEAE34 /* ImageWall.png in Resources */ = {isa = PBXBuildFile; fileRef = 073AD0F41FEB840400DEAE34 /* ImageWall.png */; };
073AD0F71FEB8DFE00DEAE34 /* CustomProperty.png in Resources */ = {isa = PBXBuildFile; fileRef = 073AD0F61FEB8DFE00DEAE34 /* CustomProperty.png */; };
07467760205BEB6D0079A946 /* WebP.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0746775D205BEB6D0079A946 /* WebP.framework */; };
07467761205BEB6D0079A946 /* LKImageWebPDecoder.m in Sources */ = {isa = PBXBuildFile; fileRef = 0746775E205BEB6D0079A946 /* LKImageWebPDecoder.m */; };
0770C87C201857090018407D /* Common.m in Sources */ = {isa = PBXBuildFile; fileRef = 0770C87B201857060018407D /* Common.m */; };
07CAEE512004802A002B96CA /* ExampleImageWallOptimizedViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 07CAEE502004802A002B96CA /* ExampleImageWallOptimizedViewController.m */; };
07CAEE54200483B3002B96CA /* ExampleNetworkFileLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 07CAEE53200483B3002B96CA /* ExampleNetworkFileLoader.m */; };
Expand Down Expand Up @@ -85,6 +87,9 @@
073AD0F21FEB7B0A00DEAE34 /* ExampleImageWallViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ExampleImageWallViewController.m; sourceTree = "<group>"; };
073AD0F41FEB840400DEAE34 /* ImageWall.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ImageWall.png; sourceTree = "<group>"; };
073AD0F61FEB8DFE00DEAE34 /* CustomProperty.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = CustomProperty.png; sourceTree = "<group>"; };
0746775D205BEB6D0079A946 /* WebP.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = WebP.framework; sourceTree = "<group>"; };
0746775E205BEB6D0079A946 /* LKImageWebPDecoder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKImageWebPDecoder.m; sourceTree = "<group>"; };
0746775F205BEB6D0079A946 /* LKImageWebPDecoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKImageWebPDecoder.h; sourceTree = "<group>"; };
0770C87A201857060018407D /* Common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Common.h; sourceTree = "<group>"; };
0770C87B201857060018407D /* Common.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Common.m; sourceTree = "<group>"; };
07CAEE4F2004802A002B96CA /* ExampleImageWallOptimizedViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ExampleImageWallOptimizedViewController.h; sourceTree = "<group>"; };
Expand All @@ -100,6 +105,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
07467760205BEB6D0079A946 /* WebP.framework in Frameworks */,
073AD0CE1FE8C30F00DEAE34 /* LKImageKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -111,6 +117,7 @@
isa = PBXGroup;
children = (
073AD0BC1FE8C11400DEAE34 /* Info.plist */,
0746775B205BEB360079A946 /* Plugin */,
073AD0B61FE8C11400DEAE34 /* Class */,
073AD0AF1FE8C11400DEAE34 /* Resource */,
073AD0C51FE8C21900DEAE34 /* LKImageKit */,
Expand Down Expand Up @@ -186,6 +193,25 @@
name = Products;
sourceTree = "<group>";
};
0746775B205BEB360079A946 /* Plugin */ = {
isa = PBXGroup;
children = (
0746775C205BEB360079A946 /* LKImageWebPDecoder */,
);
name = Plugin;
path = ../Plugin;
sourceTree = "<group>";
};
0746775C205BEB360079A946 /* LKImageWebPDecoder */ = {
isa = PBXGroup;
children = (
0746775F205BEB6D0079A946 /* LKImageWebPDecoder.h */,
0746775E205BEB6D0079A946 /* LKImageWebPDecoder.m */,
0746775D205BEB6D0079A946 /* WebP.framework */,
);
path = LKImageWebPDecoder;
sourceTree = "<group>";
};
079406C41F19FC85001F844C /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -314,6 +340,7 @@
0770C87C201857090018407D /* Common.m in Sources */,
073AD0C31FE8C11400DEAE34 /* MainViewController.m in Sources */,
07CAEE512004802A002B96CA /* ExampleImageWallOptimizedViewController.m in Sources */,
07467761205BEB6D0079A946 /* LKImageWebPDecoder.m in Sources */,
073AD0C21FE8C11400DEAE34 /* main.m in Sources */,
073959151FF242C700D94D27 /* ExampleUtil.m in Sources */,
073AD0C11FE8C11400DEAE34 /* AppDelegate.m in Sources */,
Expand Down Expand Up @@ -462,6 +489,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 557RBV9698;
FRAMEWORK_SEARCH_PATHS = "\"$(SRCROOT)/../Plugin\"/**";
FRAMEWORK_VERSION = "";
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -480,6 +508,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 557RBV9698;
FRAMEWORK_SEARCH_PATHS = "\"$(SRCROOT)/../Plugin\"/**";
FRAMEWORK_VERSION = "";
INFOPLIST_FILE = "$(SRCROOT)/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
1 change: 1 addition & 0 deletions LKImageKitExample/Resource/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<segment title="big.jpg"/>
<segment title="small.gif"/>
<segment title="big.gif"/>
<segment title="test.webp"/>
</segments>
<connections>
<action selector="reload:" destination="B3s-Jr-exZ" eventType="valueChanged" id="7gt-xZ-Zft"/>
Expand Down
14 changes: 14 additions & 0 deletions Plugin/LKImageWebPDecoder/LKImageWebPDecoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Tencent is pleased to support the open source community by making LKImageKit available.
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
// https://opensource.org/licenses/BSD-3-Clause
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//
// Created by lingtonke

#import <LKImageKit/LKImageKit.h>

@interface LKImageWebPDecoder : LKImageDecoder

@end
123 changes: 123 additions & 0 deletions Plugin/LKImageWebPDecoder/LKImageWebPDecoder.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// Tencent is pleased to support the open source community by making LKImageKit available.
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
// https://opensource.org/licenses/BSD-3-Clause
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//
// Created by lingtonke

#import "LKImageWebPDecoder.h"
#import "webP/demux.h"
#import "webP/decode.h"

@implementation LKImageWebPDecoder

static void FreeWebPImageData(void *info, const void *data, size_t size)
{
free((void*)data);
}

- (LKImageDecodeResult *)decodeResultFromData:(NSData *)data request:(LKImageRequest *)request
{
NSDate *date = [NSDate date];
WebPData webp_data;
webp_data.bytes = data.bytes;
webp_data.size = data.length;

WebPDemuxer* demux = WebPDemux(&webp_data);
if (!demux)
{
return nil;
}
LKImageDecodeResult *result = [[LKImageDecodeResult alloc] init];
result.type = @"webP";
uint32_t width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
uint32_t height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
// ... (Get information about the features present in the WebP file).
uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);

NSMutableArray *array = [NSMutableArray array];

// ... (Iterate over all frames).
WebPIterator iter;
NSInteger duration = 0;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapinfo = kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst;
CGContextRef context = CGBitmapContextCreate(nil, width, height, 8, 4*width, colorSpace, bitmapinfo);
if (!context)
{
WebPDemuxDelete(demux);
result.error = [LKImageError errorWithCode:LKImageErrorCodeDecodeFailed];
return result;
}


if (WebPDemuxGetFrame(demux, 1, &iter)) {
do {
if (duration == 0)
{
duration = iter.duration;
}
WebPDecoderConfig config;
if (!WebPInitDecoderConfig(&config))
{
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
WebPDemuxReleaseIterator(&iter);
WebPDemuxDelete(demux);
return nil;
}
config.output.colorspace = MODE_bgrA;
VP8StatusCode retCode = WebPDecode(iter.fragment.bytes, iter.fragment.size, &config);
if (retCode != VP8_STATUS_OK)
{
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
WebPDemuxReleaseIterator(&iter);
WebPDemuxDelete(demux);
result.error = [LKImageError errorWithCode:LKImageErrorCodeDecodeFailed];
return result;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
config.output.u.RGBA.rgba,
config.output.u.RGBA.size,
FreeWebPImageData);
CGImageRef imageRef = CGImageCreate(iter.width, iter.height, 8, 32, 4*iter.width, colorSpace, bitmapinfo, provider, nil, NO, kCGRenderingIntentDefault);
UIImage *newImage = nil;
CGContextDrawImage(context, CGRectMake(iter.x_offset, height - iter.height - iter.y_offset, iter.width, iter.height), imageRef);
CGImageRef image = CGBitmapContextCreateImage(context);
newImage = [[UIImage alloc] initWithCGImage:image];
[array addObject:newImage];
CGImageRelease(imageRef);
CGImageRelease(image);
CGDataProviderRelease(provider);

// ... (Consume 'iter'; e.g. Decode 'iter.fragment' with WebPDecode(),
// ... and get other frame properties like width, height, offsets etc.
// ... see 'struct WebPIterator' below for more info).
} while (WebPDemuxNextFrame(&iter));
WebPDemuxReleaseIterator(&iter);
}
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
// ... (Extract metadata).
WebPChunkIterator chunk_iter;
if (flags & ICCP_FLAG) WebPDemuxGetChunk(demux, "ICCP", 1, &chunk_iter);
// ... (Consume the ICC profile in 'chunk_iter.chunk').
WebPDemuxReleaseChunkIterator(&chunk_iter);
if (flags & EXIF_FLAG) WebPDemuxGetChunk(demux, "EXIF", 1, &chunk_iter);
// ... (Consume the EXIF metadata in 'chunk_iter.chunk').
WebPDemuxReleaseChunkIterator(&chunk_iter);
if (flags & XMP_FLAG) WebPDemuxGetChunk(demux, "XMP ", 1, &chunk_iter);
// ... (Consume the XMP metadata in 'chunk_iter.chunk').

WebPDemuxReleaseChunkIterator(&chunk_iter);
WebPDemuxDelete(demux);
NSLog(@"WebPDecodeTime:%f",[[NSDate date] timeIntervalSinceDate:date]);
UIImage *image = [LKImageUtil imageFromImages:array duration:duration/1000.0];
result.image = image;
return result;
}

@end
Loading

0 comments on commit 1dcfb5f

Please sign in to comment.