Coder Social home page Coder Social logo

sdwebimage / sdwebimageheifcoder Goto Github PK

View Code? Open in Web Editor NEW
33.0 7.0 7.0 43.23 MB

A SDWebImage coder plugin to support HEIF image without Apple's Image/IO framework

License: MIT License

Ruby 4.93% Objective-C 92.19% Swift 2.89%
macos ios tvos heif c objective-c cocoapods sdwebimage carthage

sdwebimageheifcoder's Introduction

SDWebImageHEIFCoder

CI Status Version License Platform SwiftPM compatible Carthage compatible codecov

4.x compatibility

SDWebImage 5.x change the custom image coder API. This master branch follow the 5.x branch of SDWebImage. For 4.x compatibility HEIF coder support, checkout 4.x branch.

What's for

This is a SDWebImage coder plugin to add High Efficiency Image File Format (HEIF) support. Which is built based on the open-sourced libheif codec.

This HEIF coder plugin currently support HEIF single/still image decoding as well as HEIC image encoding.

The decoding supports HDR HEIF image with 10/12 bit depth (larger than normal 8 bit) as well.

It support iOS 9+/macOS 10.11+ device without the dependency of Apple's Image/IO framework.

For iOS 8+/macOS 10.10+, use version lower than 0.10.0.

Performance

Apple's Image/IO framework supports Hardware-Accelerated HEIF decoding (A9+ chip) and encoding on (A10+ chip). And provide a backup Software decoding and encoding on all iOS 11+/macOS 10.13+ devices.

This coder is used for backward-compatible solution. And the codec only do Software decoding / encoding, which is slower than Image/IO. So if possible, choose to use Image/IO (SDWebImage's built-in coder) firstly.

Requirements

  • iOS 9.0
  • tvOS 9.0
  • macOS 10.11
  • watchOS 2.0
  • Xcode 11.0

Installation

CocoaPods

SDWebImageHEIFCoder is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SDWebImageHEIFCoder'

SDWebImageHEIFCoder contains subspecs libde265 & libx265. Which integrate the codec plugin for libheif to support HEIF image decoding/encoding.

To enable HEIF decoding, you should add libde265 subspec:

pod 'SDWebImageHEIFCoder/libde265'

To enable HEIF encoding, you should add libx265 subspec:

pod 'SDWebImageHEIFCoder/libx265'

By default will contains only libde265 subspec for most people's usage. Using libx265 encoding subspec only if you want HEIF encoding.

Carthage

SDWebImageHEIFCoder is available through Carthage.

Carthage does not support like CocoaPods' subspec, since most of user use HEIF decoding without x265 library. The framework through Carthage only supports libde265 for HEIF decoding.

github "SDWebImage/SDWebImageHEIFCoder"

Swift Package Manager (Xcode 11+)

SDWebImageHEIFCoder is available through Swift Package Manager.

The framework through SwiftPM only supports libde265 for HEIF decoding.

let package = Package(
    dependencies: [
        .package(url: "https://github.com/SDWebImage/SDWebImageHEIFCoder.git", from: "0.6")
    ]
)

Usage

Add Coder

To use HEIF coder, you should firstly add the SDImageHEIFCoder.sharedCoder to the coders manager. You can also detect the target platform compatibility for HEIF and choose add coder.

  • Objective-C
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, *)) {
    // These version supports Image/IO built-in decoding
} else {
    // Don't support HEIF decoding, add coder
    SDImageHEIFCoder *HEIFCoder = [SDImageHEIFCoder sharedCoder];
    [[SDImageCodersManager sharedManager] addCoder:HEIFCoder];
}
  • Swift
if #available(iOS 11.0, macOS 10.13, tvOS 11.0, *) {
    // These version supports Image/IO built-in decoding
} else {
    // Don't support HEIF decoding, add coder
    let HEIFCoder = SDImageHEIFCoder.shared
    SDImageCodersManager.shared.addCoder(HEIFCoder)
}

Loading

Then you can call the View Category method to start load HEIF images.

  • Objective-C
UIImageView *imageView;
[imageView sd_setImageWithURL:url];
  • Swift
let imageView: UIImageView
imageView.sd_setImage(with: url)

Decoding

SDImageHEIFCoder currently supports decode the static HEIF images.

Note HEIF sequence images(.heics) is not supported currently, only supported in built-in coder from SDWebImage for iOS 13+/macOS 10.15+, also supported by Safari and WebKit.

  • Objective-C
// HEIF image decoding
NSData *heifData;
UIImage *image = [[SDImageHEIFCoder sharedCoder] decodedImageWithData:heifData options:nil];
  • Swift
// HEIF image decoding
let heifData: Data
let image = SDImageHEIFCoder.shared.decodedImage(with: data, options: nil)

Thumbnail Decoding (0.7.0+)

HEIF image container supports embed thumbnail image. If we can found a suitable thumbnail image, we pick that instead for quickly display, else we will decode full pixel image and scale down.

  • Objective-C
// HEIF thumbnail image decoding
NSData *heifData;
CGSize thumbnailSize = CGSizeMake(300, 300);
UIImage *thumbnailImage = [[SDImageHEIFCoder sharedCoder] decodedImageWithData:heifData options:@{SDImageCoderDecodeThumbnailPixelSize : @(thumbnailSize}];
  • Swift
// HEIF thumbnail image decoding
let heifData: Data
let thumbnailSize = CGSize(width: 300, height: 300)
let image = SDImageHEIFCoder.shared.decodedImage(with: data, options: [.decodeThumbnailPixelSize: thumbnailSize])

Encoding

SDWebImageHEIFCoder also support HEIF encoding (need x265 subspec). You can encode UIImage to HEIF compressed image data.

  • Objective-C
UIImage *image;
NSData *imageData = [image sd_imageDataAsFormat:SDImageFormatHEIF];
// Encode Quality
NSData *lossyData = [[SDImageHEIFCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatHEIF options:@{SDImageCoderEncodeCompressionQuality : @(0.1)}]; // [0, 1] compression quality
NSData *limitedData = [[SDImageHEIFCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatHEIF options:@{SDImageCoderEncodeMaxFileSize : @(1024 * 10)}]; // v0.8.0 feature, limit output file size <= 10KB
  • Swift
let image;
let imageData = image.sd_imageData(as: .HEIF)
// Encode Quality
let lossyData = SDImageHEIFCoder.shared.encodedData(with: image, format: .heif, options: [.encodeCompressionQuality: 0.1]) // [0, 1] compression quality
let limitedData = SDImageHEIFCoder.shared.encodedData(with: image, format: .heif, options: [.encodeMaxFileSize: 1024 * 10]) // v0.8.0 feature, limit output file size <= 10KB

Thumbnail Encoding (0.8.0+)

  • Objective-C
// HEIF image thumbnail encoding
UIImage *image;
NSData *thumbnailData = [[SDImageHEIFCoder sharedCoder] encodedDataWithImage:image format:SDImageFormatHEIF options:@{SDImageCoderEncodeMaxPixelSize : @(CGSizeMake(200, 200)}, SDImageCoderEncodeEmbedThumbnail : @(YES)];
// v0.8.0 feature, encoding max pixel size
// v0.9.0 feature, control whether to embed thumbnail (max 320x320 pixels)
  • Swift
// HEIF image thumbnail encoding
let image: UIImage
let thumbnailData = SDImageHEIFCoder.shared.encodedData(with: image, format: .heif, options: [.encodeMaxPixelSize: CGSize(width: 200, height: 200), .encodeEmbedThumbnail : true])
// v0.8.0 feature, encoding max pixel size
// v0.9.0 feature, control whether to embed thumbnail (max 320x320 pixels)

See more documentation in SDWebImage Wiki - Coders

Screenshot

The images are from HEIF official site example

Author

DreamPiggy, [email protected]

License

SDWebImageHEIFCoder itself is available under the MIT license. See the LICENSE file for more info. However, when using libx265, the license will be subject to GPL licence (or commercial licence if you have one). Check x265.org for more information.

Thanks

sdwebimageheifcoder's People

Contributors

bpoplauschi avatar dreampiggy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

sdwebimageheifcoder's Issues

Metadata is lost after conversion

After decoding the HEIC picture with this library, all the meta information is lost. I wonder if other information except the rotation field can be retained?
After reviewing the libheif source code, I found the following method:

size_t exifsize = 0;
  uint8_t* exifdata = GetExifMetaData(handle, &exifsize);
  if (exifdata && exifsize > 4) {
    static const uint8_t kExifMarker = JPEG_APP0 + 1;
    jpeg_write_marker(&cinfo, kExifMarker, exifdata + 4,
                      static_cast<unsigned int>(exifsize - 4));
    free(exifdata);
  }

  size_t profile_size = heif_image_handle_get_raw_color_profile_size(handle);
  if (profile_size > 0) {
    uint8_t* profile_data = static_cast<uint8_t*>(malloc(profile_size));
    heif_image_handle_get_raw_color_profile(handle, profile_data);
    jpeg_write_icc_profile(&cinfo, profile_data, (unsigned int) profile_size);
    free(profile_data);
  }


  if (heif_image_get_bits_per_pixel(image, heif_channel_Y) != 8) {
    fprintf(stderr, "JPEG writer cannot handle image with >8 bpp.\n");
    return false;
  }
.......

But my C++ is very poor, do not know how to convert to OC code,any body have some idea? i'll be very thankful!

This library does not support bitcode

Thanks for creating this library I am using this lib. since five years its great.

Now I am getting issue for .heic image format file, SDWebImage not load images for .heic format . I am used SDWebImageHEICCoder , its load file successfully but build issue while archive due to bitcode enable yes. if disable bitcode than its working fine.

Can you please check this.

Example Not Work

The Example doesn't work fine. The heif images don't display. I test it in iphone 7p and ios 12.1.3. It often crashed. Please check it. Thanks!

Screenshot as below:

Podspec not linting

The podspec is not linting and I also get the same error when trying to build the example

  • I don't understand why we have the same files in Vendors/include/libheif and Vendors/libheif/libheif and I mean heif.h and heif-version.h
pod lib lint --allow-warnings --subspec=libheif

 -> SDWebImageHEIFCoder/libheif (0.2.0)
    - ERROR | [SDWebImageHEIFCoder/libheif] public_header_files: The pattern includes header files that are not listed in source_files (/Users/bogdanp/GitHub/SDWebImageHEIFCoder/SDWebImageHEIFCoder/Module/SDWebImageHEIFCoder.h).
    - ERROR | [SDWebImageHEIFCoder/libheif] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
    - NOTE  | [SDWebImageHEIFCoder/libheif] xcodebuild:  /Users/bogdanp/GitHub/SDWebImageHEIFCoder/Vendors/libheif/libheif/heif.h:31:10: fatal error: 'libheif/heif_version.h' file not found
    - NOTE  | [SDWebImageHEIFCoder/libheif] xcodebuild:  /Users/bogdanp/GitHub/SDWebImageHEIFCoder/Vendors/libheif/libheif/heif_plugin.h:28:10: fatal error: 'libheif/heif.h' file not found

[!] SDWebImageHEIFCoder did not pass validation, due to 2 errors.
You can use the `--no-clean` option to inspect any issue.

Support for AVIF(AV1 codec for HEIF container) image format

AVIF is a AV1 codec based HEIF container. The spec is here:
https://aomediacodec.github.io/av1-avif/

Current third-party HEIF decoder libheif does not support this format natively. But however, it's a future format which based on AV1 (VP9 's next successor). And it's the competition of current HEVC/H.265

In short term, since that AVIF spec is still in draft, we decide not to do some work for that. But however, after that spec is release or the third-party HEIF decoder libheif have support for AVIF, I'll update this and introduce the support.

animated Heic image can not play with animation

I have a animated Heic image. then I use ImageIO to decode it, it could return image that has many images. But when I use SDWebImageHEIFCoder decode it, it can not play with animation, the images is nil.

there is the code:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ll_interaction_heifenc" ofType:@"heic"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
self.imageView.image = [UIImage sd_imageWithData:imageData];

I used iPhone 12 ProMax, iOS 15.2. I had created the heic animated image by ImageMagick. convert *.png ll_interaction_heifenc.heic

Build error due to Legacy Build System

Context

Currently, you cannot build master branch due to libde265-Xcode at 1.0.8 due to Cartfile.resolved using Legacy Build System which is now deprecated in Xcode 13.4.

I created this issue in libde265-Xcode asking to release a new version in Carthage so this project can use it.

Workaround: specify dependency to point to at least 43a87a1bef850fed5646875d2e965e349ceef3aa.

Steps to repro

  1. git clone [email protected]:SDWebImage/SDWebImageHEIFCoder.git
  2. carthage update --platform iOS
 henrique@Henrique-Valcanaia-MacBook-Pro: ~/Developer/SDWebImageHEIFCoder: master: carthage update --platform iOS
*** Fetching libheif-Xcode
*** Fetching SDWebImage
*** Fetching libde265-Xcode
*** Checking out SDWebImage at "5.12.5"
*** Checking out libde265-Xcode at "1.0.8"
*** Checking out libheif-Xcode at "1.9.0"
*** xcodebuild output can be found in /var/folders/xp/l3qg7cxs1kg8hqy4lxd4dpj40000gn/T/carthage-xcodebuild.dlsvI3.log
*** Building scheme "libde265-iOS" in libde265.xcodeproj
Build Failed
	Task failed with exit code 65:
	/usr/bin/xcrun xcodebuild -project /Users/henrique/Developer/SDWebImageHEIFCoder/Carthage/Checkouts/libde265-Xcode/libde265.xcodeproj -scheme libde265-iOS -configuration Release -derivedDataPath /Users/henrique/Library/Caches/org.carthage.CarthageKit/DerivedData/13.4_13F17a/libde265-Xcode/1.0.8 -sdk iphoneos ONLY_ACTIVE_ARCH=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES archive VALIDATE_WORKSPACE=NO -archivePath /var/folders/xp/l3qg7cxs1kg8hqy4lxd4dpj40000gn/T/libde265-Xcode SKIP_INSTALL=YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=NO CLANG_ENABLE_CODE_COVERAGE=NO STRIP_INSTALLED_PRODUCT=NO (launched in /Users/henrique/Developer/SDWebImageHEIFCoder/Carthage/Checkouts/libde265-Xcode)

This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/xp/l3qg7cxs1kg8hqy4lxd4dpj40000gn/T/carthage-xcodebuild.dlsvI3.log

It's too slow

use heif_decode_image to decode a 1.7M image. It take 6 seconds on iPhone 7P. It too slow to decode. Can I make it fast ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.