Code Monkey home page Code Monkey logo

avif.swift's Introduction

avif.swift

What's This?

Introducing AVIF.swift, the ultimate image decoding solution for AVIF images on iOS and macOS. The AVIFDecoder.decode method allows you to easily read and write AVIF images using the power of Swift. With AVIF.swift, you can decode AVIF images with up to 50% smaller file sizes and 25% better compression than JPEG, all while maintaining the highest quality. This package is compatible with both Swift Package Manager and CocoaPods. Try AVIF.swift today and see the difference for yourself. Check it out at https://github.com/awxkee/avif.swift

This package is provides full (compatibility support) for AVIF images for all apple platforms. Supports encode AVIF and decode AVIF images in convinient and fast way

A package to display AVIF on iOS, MacOS, Catalyst, WatchOS, tvOS or encode AVIF images. Also provider AVIF support for Nuke. Have support for older versions of iOS, WatchOS, MacOSX, tvOS, Catalyst and all the simulators that doesn't have support for AVIF images

Package based on dav1d to have the best speed of decompressing on devices that do not have support for AV1 hardware codec. As AVIF encoder have aom as just this one looks reasonable to encode AVIF images on mobile devices
Main aim of the project is to use AVIF image on all Apple platforms etc with usable speed and convenience

Supports animated AVIF's with realtime FPS like 24+ Also supports encoding animated AVIF's

Handles HDR images with PQ, HLG and SMPTE428 transfer functions

Precompiled for iOS 11+, Mac OS 12+, Mac Catalyst 14+, WatchOS 6+, tvOS 13+

Installation

Go to File / Swift Packages / Add Package Dependency… and enter package repository URL https://github.com/awxkee/avif.swift.git, then select the latest master branch at the time of writing.

Usage

import avif
// Decompress data
let uiImage: UIImage? = AVIFDecoder.decode(Data(), sampleSize: .zero) // or any max CGSize of image
// Compress
let data: Data = try AVIFEncoder().encode(image: UIImage())

// Decode animated
let animatedDecoder = AnimatedDecoder(withData: Data())
let frame: CGImage = try animatedDecoder.get(frame: 1)
let image: UIImage = try animatedDecoder.getImage(frame: 1) 

// Encode animation
import avifc

let animatedEncoder = AVIFAnimatedEncoder()
animatedEncoder.create()
try animatedEncoder.addImage(UIImage(), duration: 250)
let encodedData = animatedEncoder.encode()

Nuke Plugin

If you wish to use AVIF with Nuke you may add avifnuke library to project and activate the plugin on app init

import avifnuke

AVIFNukePlugin.enable()

let imageView = UIImageView()
let avifimageURL = URL(string: "https://bestavifdomain.com/sample.avif")!
Nuke.loadImage(with: url, into: imageView)

SDWebImage Plugin

If you wish to use AVIF with SDWebImage you may use provided plugin

import SDWebImage
#if canImport(avif)
import avif
#endif

public class SDWebImageAVIFCoder: NSObject, SDImageCoder {
    public func canDecode(from data: Data?) -> Bool {
        guard let data else { return false }
        return data.isAVIFFormat
    }

    public func decodedImage(with data: Data?, options: [SDImageCoderOption : Any]? = nil) -> UIImage? {
        guard let data else {
            return nil
        }
        return AVIFDecoder.decode(data)
    }

    public func canEncode(to format: SDImageFormat) -> Bool {
        return true
    }

    public func encodedData(with image: UIImage?, format: SDImageFormat, options: [SDImageCoderOption : Any]? = nil) -> Data? {
        guard let image else {
            return nil
        }
        return try? AVIFEncoder.encode(image: image, quality: 50)
    }

    public override init() {
    }
}

And after register the plugin

SDImageCodersManager.shared.addCoder(SDWebImageAVIFCoder())

Currently, avif nuke and SDWebImage plugin do not support animated avifs so you have to do it yourself

Disclaimer

#AVIF Alliance for Open Media has developed the AVIF image format, a format that makes images have a smaller file size than with JPEG, PNG, GIF, or HEIF, without sacrificing image quality. AVIF offers lossy and lossless compression and has already 70% support by web browsers. It is regarded as a significant advancement in media compression. It is the goal of AOMedia to create open, royalty-free software standards for multimedia distribution. Specifically, AVIF will be free for everyone to use. There is a long list of big companies behind AOMedia, including Netflix, Google, Facebook, Apple, and Microsoft. In terms of image file formats for the web, JPG and PNG are considered the most popular. Several years ago, Google developed a format called WebP that delivers images 30% smaller than JPGs, while maintaining image quality. With AVIF, images are 50% smaller than JPG while maintaining the same quality.

TODO

  • Tests
  • Some examples

avif.swift's People

Contributors

awxkee 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

Watchers

 avatar

avif.swift's Issues

Issue in LogarithmicToneMapper

Please see the attached Xcode build error when using the master branch (April 8th 2024).
Changing from log10f_c to log10f works on my machine.

Versions:

  • Machine: Macbook Pro 2019 - 2.6 GHz 6-Core Intel Core i7
  • Swift version: 5.10
  • Xcode Version: 15.3

Screenshot 2024-04-08 at 15 56 45

Package & Target name conflicts with `SDWebImage/libwebp-Xcode`

Conflicting package: https://github.com/SDWebImage/libwebp-Xcode

We face the error below when trying to use two packages together since both of them provide target and package named libwebp.

xcodebuild: error: Could not resolve package dependencies: multiple products named 'libwebp' in: 'libwebp-ios', 'libwebp-xcode'

xcodebuild: error: Could not resolve package dependencies:
 multiple targets named 'libwebp' in: 'libwebp-ios', 'libwebp-xcode'; consider using the `moduleAliases` parameter in manifest to provide unique names

To resolve this, awxkee/libwebp-ios#1 should be merged first and then update depedencies in this repository.

Conflicting types for 'SharpYuvGammaToLinear/Gamma' due to dependency conflict

I am encountering a problem related to conflicting types for the function 'SharpYuvGammaToLinear/Gamma' while using the avif.swift library.
The conflict seems to stem from conflicting dependencies involving the libwebp libraries.

The dependency chain is as follows:

  • The avif.swift library relies on libwebp-ios.
  • libwebp-ios has a dependency on libwebp.
  • Additionally, there is another library, libwebp-Xcode (https://github.com/SDWebImage/libwebp-Xcode), which also depends on libwebp.
    This dependency structure results in a conflict regarding the 'SharpYuvGammaToLinear/Gamma' function, leading to the mentioned error.

Steps to Reproduce:

Include avif.swift and libwebp-ios(https://github.com/SDWebImage/libwebp-Xcode) as dependencies in an Xcode project.
스크린샷 2023-10-16 오후 3 33 59

I'd like to hear your opinion on what to do in times like this.

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.