Giter Site home page Giter Site logo

ainame / swift-webp Goto Github PK

View Code? Open in Web Editor NEW
157.0 5.0 37.0 2.45 MB

A think libwebp wrapper in Swift that provides both encode/decode APIs

License: MIT License

Swift 98.33% C 0.41% Shell 1.26%
webp swift libwebp swift-wrapper image-processing

swift-webp's Introduction

Swift-WebP

Build Status Carthage compatible

Swift Wrapper of libwebp

Target

Swift-WebP aims to deal with image processing flexibly for WebP format in Swift unlike an image loading library; such as SDWebImage. So this library allows you to use many features libwebp has without hassles to use C API from Swift. And also you don't need to install libwebp by yourself if you install this via Carthage. This contains it inside the framework.

Support Versions:

  • libwebp: v1.0.3
  • iOS Deployment Target: 8.0
  • macOS Deployment Target: 10.11

Features

  • Support mutiplatform; iOS, macOS, and Linux (swift-docker)
  • Support SPM
  • Advanced Encoder API - WebPEncoder, WebPEncoderConfig
  • Advanced Decoding API - WebPDecoder, WebPDecoderOptions
  • Image inspection for WebP files - WebPImageInspector

TODO

  • Progressively encoding/decoding option
  • Animated WebP

Usage

Encoding

let image = UIImage(named: "demo")
let encoder = WebPEncoder()
let queue =  DispatchQueue(label: "me.ainam.webp")

// should encode in background
queue.async {
    let data = try! encoder.encode(image, config: .preset(.picture, quality: 95))
    // using webp binary data...
}

Decoding

let data: Data = loadWebPData()
let encoder = WebPDecoder()
let queue =  DispatchQueue(label: "me.ainam.webp")

// should decode in background
queue.async {
    var options = WebPDecoderOptions()
    options.scaledWidth = Int(originalWidth / 2)
    options.scaledHeight = Int(originalHeight / 2)
    let cgImage = try! decoder.decode(data, options: options)
    let webpImage = UIImage(cgImage: cgImage)

    DispatchQueue.main.async {
        self.imageView.image = webpImage
    }
}

Example

Please check example projects

Installation

This library won't support CocoaPods. It's difficult to create and maintain podspec files for static libraries. (But contribution for that is always welcome.)

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate WebP into your Xcode project using Carthage, specify it in your Cartfile:

github "ainame/Swift-WebP"

Run carthage update --use-submodules to build the framework and drag the built `WebP.framework** into your Xcode project.

Don't forget to use --use-submodules flag!

Author

ainame

License

Swift-WebP is available under the MIT license. See the LICENSE file for more info.

swift-webp's People

Contributors

1000ch avatar ainame avatar ggustavolopez 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  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  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  avatar

Watchers

 avatar  avatar  avatar  avatar

swift-webp's Issues

Carthage stalls at check out?

Hello,

First off would like to thank you for working on WebP as it's still at its infancy! I tried to use carthage but it hangs around checkout, possibly has to do with github? Not to sure.

Thanks

supported cocoapod?

I get some problem when import webp c in swift
tried modulemap, vendored frameworks

I see there a pod spec in your project , dose it works?

when I running on iOS simulator by Xcode, I got a fatal error

the console said:
dyld: Library not loaded: /usr/local/opt/webp/lib/libwebp.7.dylib Referenced from: /Users/username/Library/Developer/CoreSimulator/Devices/BDCB7B76-F51B-42D6-B6C2-0F8B66A889C6/data/Containers/Bundle/Application/EE7CB8B0-1477-43D8-B990-A0C8097B8C25/PracticeWebp.app/PracticeWebp Reason: no suitable image found. Did find: /usr/local/opt/webp/lib/libwebp.7.dylib: mach-o, but not built for iOS simulator

Minimum support - iOS 10?

I installed it via Carthage, but XCode gives me error on line import WebP

Module file's minimum deployment target is ios10.1 v10.1: $project_path/Carthage/Build/iOS/WebP.framework/Modules/WebP.swiftmodule/arm.swiftmodule

Latest sample app does not decode webp Data properly.

I have run the latest sample app and when using encode() function it seems that it is able to encode since it returns result with length > 0. But when tries to decode back the webP data using the decoder function, the image shows gibberish preview on XCode.

The 2 test cases on the webp framework only test if the result is not nil but does not test the content is correct.

PNG to Webp

PNG convert to webp is error, like this:
image

Can you fix this?

Support SPM build properly

This package currently gets this error to be resolved.

ainame-mbp:Swift-WebP ainame$ swift build
'WebP' /Users/ainame/src/github.com/ainame/Swift-WebP: error: invalid custom path './Modules/CWebP' for target 'CWebP'
ainame-mbp:Swift-WebP ainame$ swift build
ld: warning: Could not find auto-linked library 'libwebp'
Undefined symbols for architecture x86_64:
  "_WebPConfigInitInternal", referenced from:
      _WebPConfigPreset in WebPEncoderConfig.swift.o
  "_WebPDecode", referenced from:
      _$s4WebP0A8PDecoderV6decode33_113B4816CCC7256F27C3E0A94BA32E40LL_6configy10Foundation4DataV_AA0aB6ConfigVztKFySwKXEfU_ in WebPDecoder.swift.o
  "_WebPEncode", referenced from:
      _$s4WebP0A8PEncoderV6encode33_091C83EEEFDDB0E21770FDBD873E5ECCLL_8importer6config11originWidth0L6Height6stride06resizeM00pN010Foundation4DataVSpys5UInt8VG_s5Int32VSpySo0A8PPictureVG_ArTtXEAA0aB6ConfigVS5itKF in WebPEncoder.swift.o
  "_WebPInitDecoderConfigInternal", referenced from:
      _WebPInitDecoderConfig in WebPDecoderConfig.swift.o
  "_WebPMemoryWrite", referenced from:
      _$s4WebP0A8PEncoderV6encode33_091C83EEEFDDB0E21770FDBD873E5ECCLL_8importer6config11originWidth0L6Height6stride06resizeM00pN010Foundation4DataVSpys5UInt8VG_s5Int32VSpySo0A8PPictureVG_ArTtXEAA0aB6ConfigVS5itKFATSPyAQGSg_SiSPyAVGSgtcfU_ in WebPEncoder.swift.o
  "_WebPMemoryWriterInit", referenced from:
      _$s4WebP0A8PEncoderV6encode33_091C83EEEFDDB0E21770FDBD873E5ECCLL_8importer6config11originWidth0L6Height6stride06resizeM00pN010Foundation4DataVSpys5UInt8VG_s5Int32VSpySo0A8PPictureVG_ArTtXEAA0aB6ConfigVS5itKF in WebPEncoder.swift.o
  "_WebPPictureFree", referenced from:
      _$s4WebP0A8PEncoderV6encode33_091C83EEEFDDB0E21770FDBD873E5ECCLL_8importer6config11originWidth0L6Height6stride06resizeM00pN010Foundation4DataVSpys5UInt8VG_s5Int32VSpySo0A8PPictureVG_ArTtXEAA0aB6ConfigVS5itKF in WebPEncoder.swift.o
  "_WebPPictureImportBGR", referenced from:
      _$s4WebP0A8PEncoderV6encode3BGR6config11originWidth0F6Height6stride06resizeG00jH010Foundation4DataVSpys5UInt8VG_AA0aB6ConfigVS5itKFs5Int32VSpySo0A8PPictureVG_AqUtcfU_ in WebPEncoder.swift.o
  "_WebPPictureImportBGRA", referenced from:
      _$s4WebP0A8PEncoderV6encode4BGRA6config11originWidth0F6Height6stride06resizeG00jH010Foundation4DataVSpys5UInt8VG_AA0aB6ConfigVS5itKFs5Int32VSpySo0A8PPictureVG_AqUtcfU_ in WebPEncoder.swift.o
  "_WebPPictureImportBGRX", referenced from:
      _$s4WebP0A8PEncoderV6encode4BGRX6config11originWidth0F6Height6stride06resizeG00jH010Foundation4DataVSpys5UInt8VG_AA0aB6ConfigVS5itKFs5Int32VSpySo0A8PPictureVG_AqUtcfU_ in WebPEncoder.swift.o
  "_WebPPictureImportRGB", referenced from:
      _$s4WebP0A8PEncoderV6encode3RGB6config11originWidth0F6Height6stride06resizeG00jH010Foundation4DataVSpys5UInt8VG_AA0aB6ConfigVS5itKFs5Int32VSpySo0A8PPictureVG_AqUtcfU_ in WebPEncoder.swift.o
  "_WebPPictureImportRGBA", referenced from:
      _$s4WebP0A8PEncoderV6encode4RGBA6config11originWidth0F6Height6stride06resizeG00jH010Foundation4DataVSpys5UInt8VG_AA0aB6ConfigVS5itKFs5Int32VSpySo0A8PPictureVG_AqUtcfU_ in WebPEncoder.swift.o
  "_WebPPictureImportRGBX", referenced from:
      _$s4WebP0A8PEncoderV6encode4RGBX6config11originWidth0F6Height6stride06resizeG00jH010Foundation4DataVSpys5UInt8VG_AA0aB6ConfigVS5itKFs5Int32VSpySo0A8PPictureVG_AqUtcfU_ in WebPEncoder.swift.o
  "_WebPPictureInitInternal", referenced from:
      _WebPPictureInit in WebPEncoder.swift.o
  "_WebPPictureRescale", referenced from:
      _$s4WebP0A8PEncoderV6encode33_091C83EEEFDDB0E21770FDBD873E5ECCLL_8importer6config11originWidth0L6Height6stride06resizeM00pN010Foundation4DataVSpys5UInt8VG_s5Int32VSpySo0A8PPictureVG_ArTtXEAA0aB6ConfigVS5itKF in WebPEncoder.swift.o
  "_WebPValidateConfig", referenced from:
      _$s4WebP0A8PEncoderV6encode33_091C83EEEFDDB0E21770FDBD873E5ECCLL_8importer6config11originWidth0L6Height6stride06resizeM00pN010Foundation4DataVSpys5UInt8VG_s5Int32VSpySo0A8PPictureVG_ArTtXEAA0aB6ConfigVS5itKF in WebPEncoder.swift.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)

can't fetch it

18:43 $ carthage update --use-submodules
*** Fetching Swift-WebP
Parse error: expected submodule commit SHA in output of task (ls-tree -z HEAD Carthage/Checkouts/Swift-WebP) but encountered:

I cant get it to build, any help is appreciated

18:45 $ carthage version
0.33.0

Corrupt output WebP when using CGImage obtained from CIImage (rendered within CIContext)

Hi again. I'm working on a simple command-line MacOS application. WebP encoding works just fine when simply re-encoding source CGImage to WebP. Let's discuss the very basic example:

import Foundation
import AppKit
import WebP

// input file path
let inputURL:URL = URL(fileURLWithPath: "/absolute/path/to/input.jpg")

// get CGImageSource
let cgSource = CGImageSourceCreateWithURL(inputURL as CFURL, nil)

// get CGImage from CGImageSource
let inputFile = CGImageSourceCreateImageAtIndex(cgSource!, 0, nil)

// convert CGImage to NSImage
let nsImage = NSImage.init(cgImage: inputFile!, size: NSZeroSize)

// encode NSImage to WebP format
let encoder = WebPEncoder()
let data = try encoder.encode(nsImage, config: .preset(.photo, quality: 95))

// write data to disk
try data.write(to: URL(fileURLWithPath: "/absolute/path/to/output.webp"))

However, when I first render input image in CIContext and then obtain CGimage from that context, the resulting WebP terribly corrupt. Here's a simple example:

import Foundation
import AppKit
import WebP

// input file path
let inputURL:URL = URL(fileURLWithPath: "/absolute/path/to/input.jpg")

// get CGImageSource
let cgSource = CGImageSourceCreateWithURL(inputURL as CFURL, nil)

// get CGImage from CGImageSource
let inputFile = CGImageSourceCreateImageAtIndex(cgSource!, 0, nil)

// convert inputFile (CGImage) to CIImage
let ciImage = CIImage(cgImage: inputFile!)

// render CIImage in CIContext and get CGImage back
let cgImage = context.createCGImage(
    ciImage,
    from: ciImage.extent,
    format: CIFormat.RGBA8,
    colorSpace: CGColorSpace(name: CGColorSpace.extendedSRGB)!
)

// convert CGImage to NSImage
let nsImage = NSImage.init(cgImage: inputFile!, size: NSZeroSize)

// encode NSImage to WebP format
let encoder = WebPEncoder()
let data = try encoder.encode(nsImage, config: .preset(.photo, quality: 95))

// write data to disk
try data.write(to: URL(fileURLWithPath: "/absolute/path/to/output.webp"))

Can you please verify on your end what could be causing output file corruption? Also, it would be extremely useful (at least for me) if the encoder would already accept CGImage without first converting it to NSimage. Thanks in advance!

Changing encoder quality has no effect on the output file

I'm currently playing with your framework. However, changing output quality has zero effect on the output file. Can you please verify? The two configs below produce the exact same image:

var config = WebPEncoderConfig.preset(.photo, quality: 1)
var config = WebPEncoderConfig.preset(.photo, quality: 100)

Make it clear please

I don't understand your samples, most of us just want get image from URL and convert it for displaying. Your samples are not clear enough to allow us to do that.
I need something like that

if let _ = downloadUrl, let url = URL(string: downloadUrl!) {
                    if let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil) {
                        let scale = UIScreen.main.scale
                        let options: [NSString: Any] = [
                            kCGImageSourceThumbnailMaxPixelSize:
                                max(self.contentView.layer.bounds.width, self.contentView.layer.bounds.height) as NSObject,
                            kCGImageSourceCreateThumbnailFromImageAlways: true
                        ]
                        if let imgref = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary) {
                            let scaledImage = UIImage(cgImage: imgref, scale: scale, orientation: .up)
                            self.image.image = scaledImage
                            self.image.snp.makeConstraints({
                                $0.width.equalToSuperview()
                            })
                        }
                    }

So, how can I put a URL here and how to convert it ?

Can't get it working in Xcode 10

I followed the Carthage instructions, but I'm getting this error. I dragged the Framework in and am not getting any syntax highlighting errors in Xcode.

ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

"No such module WebP" in Xcode 11.2.1

  1. Run your "iOS Example" in Xcode 11.2.1(11B500)
  2. Error message "No such module WebP" when import

My previous project was able to import Webp after setting up Carthage, it happens the same problem after using latesting Xcode version.

Unable to build framework using Xcode 12 Beta 5

Following error occurs at least for me when running command: "carthage update --use-submodules"

*** Checking out Swift-WebP at "v0.4.0"
*** No cache found for Swift-WebP, building with all downstream dependencies
*** xcodebuild output can be found in /var/folders/0j/5v48p04s3pxc50pp0zjqt8xr0000gn/T/carthage-xcodebuild.TxIJaP.log
*** Downloading Swift-WebP.framework binary at "v0.4.0"
*** Building scheme "WebP iOS" in WebP.xcworkspace
Build Failed
	Task failed with exit code 1:
	/usr/bin/xcrun lipo -create /Users/XXX/Library/Caches/org.carthage.CarthageKit/DerivedData/12.0_12A8189h/Swift-WebP/v0.4.0/Build/Intermediates.noindex/ArchiveIntermediates/WebP\ iOS/IntermediateBuildFilesPath/UninstalledProducts/iphoneos/WebP.framework/WebP /Users/XXX/Library/Caches/org.carthage.CarthageKit/DerivedData/12.0_12A8189h/Swift-WebP/v0.4.0/Build/Products/Release-iphonesimulator/WebP.framework/WebP -output /Users/XXX/Pods/NSCSDK/Carthage/build/iOS/WebP.framework/WebP

This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/0j/5v48p04s3pxc50pp0zjqt8xr0000gn/T/carthage-xcodebuild.TxIJaP.log

Are others able to use the component with Xcode 12?

[Security] Workflow deploy.yml is using vulnerable action actions/checkout

The workflow deploy.yml is referencing action actions/checkout using references v1. However this reference is missing the commit a6747255bd19d7a757dbdda8c654a9f84db19839 which may contain fix to the some vulnerability.
The vulnerability fix that is missing by actions version could be related to:
(1) CVE fix
(2) upgrade of vulnerable dependency
(3) fix to secret leak and others.
Please consider to update the reference to the action.

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.