Skip to content

Fix the compatible for libavif 0.2 API changes #3

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

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "SDWebImage/SDWebImage" ~> 5.0
github "SDWebImage/libavif-Xcode" >= 0.1.3
github "SDWebImage/libavif-Xcode" ~> 0.2.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "SDWebImage/SDWebImage" "5.1.0"
github "SDWebImage/libaom-Xcode" "1.0.1"
github "SDWebImage/libavif-Xcode" "0.1.4"
github "SDWebImage/libavif-Xcode" "0.2.0"
10 changes: 5 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PODS:
- libaom (1.0.1)
- libavif (0.1.4):
- libavif (0.2.0):
- libaom (>= 1.0.1)
- SDWebImage (5.1.0):
- SDWebImage/Core (= 5.1.0)
- SDWebImage/Core (5.1.0)
- SDWebImageAVIFCoder (0.2.0):
- libavif (~> 0.1.4)
- SDWebImageAVIFCoder (0.2.1):
- libavif (~> 0.2.0)
- SDWebImage (~> 5.0)

DEPENDENCIES:
Expand All @@ -24,9 +24,9 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
libaom: 1e48c68559b8d6191c1a9f266e0bee83b2dd21fd
libavif: 4f94ed672d45d6651ee0f784f5faf11b95449716
libavif: 251b8a20baa5b05467cc64c3b844eaa7b5cf3a62
SDWebImage: fb387001955223213dde14bc08c8b73f371f8d8f
SDWebImageAVIFCoder: ec08ff2cf12552223b51b7253c8201d264ecbbac
SDWebImageAVIFCoder: 0cc05dc868739b68d6b61856ef11e1aea6af68eb

PODFILE CHECKSUM: cb60778bff8fb5ce4fbc8792f6079317b7a897be

Expand Down
2 changes: 1 addition & 1 deletion SDWebImageAVIFCoder.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ Which is built based on the open-sourced libavif codec.
s.source_files = 'SDWebImageAVIFCoder/Classes/**/*', 'SDWebImageAVIFCoder/Module/SDWebImageAVIFCoder.h'

s.dependency 'SDWebImage', '~> 5.0'
s.dependency 'libavif', '~> 0.1.4'
s.dependency 'libavif', '~> 0.2.0'
end
15 changes: 12 additions & 3 deletions SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ - (nullable CGImageRef)sd_createAVIFImageWithData:(nonnull NSData *)data CF_RETU
.size = data.length
};
avifImage * avif = avifImageCreateEmpty();
avifResult result = avifImageRead(avif, &rawData);
avifDecoder *decoder = avifDecoderCreate();
avifResult result = avifDecoderRead(decoder, avif, &rawData);
if (result != AVIF_RESULT_OK) {
avifDecoderDestroy(decoder);
avifImageDestroy(avif);
return nil;
}
Expand All @@ -147,6 +149,7 @@ - (nullable CGImageRef)sd_createAVIFImageWithData:(nonnull NSData *)data CF_RETU

uint8_t * dest = calloc(width * components * height, sizeof(uint8_t));
if (!dest) {
avifDecoderDestroy(decoder);
avifImageDestroy(avif);
return nil;
}
Expand All @@ -162,6 +165,7 @@ - (nullable CGImageRef)sd_createAVIFImageWithData:(nonnull NSData *)data CF_RETU

// clean up
CGDataProviderRelease(provider);
avifDecoderDestroy(decoder);
avifImageDestroy(avif);

return imageRef;
Expand Down Expand Up @@ -274,17 +278,22 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
if (options[SDImageCoderEncodeCompressionQuality]) {
compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue];
}
int rescaledQuality = 63 - (int)((compressionQuality) * 63.0f);
int rescaledQuality = AVIF_WORST_QUALITY - (int)((compressionQuality) * AVIF_WORST_QUALITY);

avifRawData raw = AVIF_RAW_DATA_EMPTY;
avifResult result = avifImageWrite(avif, &raw, 2, rescaledQuality);
avifEncoder *encoder = avifEncoderCreate();
encoder->quality = rescaledQuality;
encoder->maxThreads = 2;
avifResult result = avifEncoderWrite(encoder, avif, &raw);

if (result != AVIF_RESULT_OK) {
avifEncoderDestroy(encoder);
return nil;
}

NSData *imageData = [NSData dataWithBytes:raw.data length:raw.size];
free(raw.data);
avifEncoderDestroy(encoder);

return imageData;
}
Expand Down