### Integrate WebP Product as Target Dependency in Swift Package Source: https://github.com/the-swift-collective/libwebp/blob/main/README.md This example demonstrates how to declare a Swift package, including adding the libwebp product as a library dependency for a specific target. It utilizes the swift-tools-version and PackageDescription APIs. ```swift // swift-tools-version: 5.8 import PackageDescription let package = Package( name: "MyPackage", products: [ .library( name: "MyLibrary", targets: ["MyLibrary"] ), ], dependencies: [ .package(url: "https://github.com/the-swift-collective/libwebp.git", from: "1.4.0") ], targets: [ .target( name: "MyLibrary", dependencies: [ /* add the webp product as a library dependency. */ .product(name: "WebP", package: "libwebp"), ] ), ] ) ``` -------------------------------- ### Create WebP Instance from RGBA Data in Swift Source: https://context7.com/the-swift-collective/libwebp/llms.txt Initializes a WebP struct with RGBA pixel data, width, and height. This allows for in-memory representation of an image before encoding or further processing. The example demonstrates creating a checkerboard pattern and performing a roundtrip encode/decode to verify integrity. It requires the WebP library. ```swift import WebP // Create a 2x2 checkerboard pattern let width = 2 let height = 2 let rgba: [UInt8] = [ // Pixel (0,0): White 255, 255, 255, 255, // Pixel (1,0): Black 0, 0, 0, 255, // Pixel (0,1): Black 0, 0, 0, 255, // Pixel (1,1): White 255, 255, 255, 255 ] let webp = WebP(width: width, height: height, rgba: rgba) // Encode and decode roundtrip do { let encoded = try webp.encode(quality: 100) let decoded = try WebP.decode(encoded) assert(decoded.width == width) assert(decoded.height == height) assert(decoded.rgba.count == width * height * 4) } catch { print("Roundtrip failed: \(error)") } ``` -------------------------------- ### Access WebP Library Version Information in Swift Source: https://context7.com/the-swift-collective/libwebp/llms.txt Shows how to retrieve the version information for the WebP Swift package and the underlying C libraries (libwebp, sharpyuv). It accesses the static `WebP.version` property for the Swift package and uses C functions like `WebPGetDecoderVersion` for the low-level libraries. The code also demonstrates how to unpack the C library version into major, minor, and patch components. ```swift import WebP // Get package version print("WebP Swift package version: (WebP.version)") // Low-level C library version access import libwebp import sharpyuv let decoderVersion = WebPGetDecoderVersion() let encoderVersion = WebPGetEncoderVersion() let sharpYUVVersion = SharpYuvGetVersion() // Unpack version (format: major.minor.patch) let major = (decoderVersion >> 16) & 0xFF let minor = (decoderVersion >> 8) & 0xFF let patch = decoderVersion & 0xFF print("libwebp decoder: (major).(minor).(patch)") ``` -------------------------------- ### Integrate WebP Swift Package into Project Dependencies Source: https://context7.com/the-swift-collective/libwebp/llms.txt Shows how to add the WebP Swift package as a dependency in your project's `Package.swift` file. This enables the use of the WebP module in your executable or library targets. It requires Swift 5.8 or later and specifies the Git repository URL and version range. ```swift // swift-tools-version: 5.8 import PackageDescription let package = Package( name: "MyImageApp", products: [ .executable( name: "MyImageApp", targets: ["MyImageApp"] ), ], dependencies: [ .package( url: "https://github.com/the-swift-collective/libwebp.git", from: "1.4.0" ) ], targets: [ .executableTarget( name: "MyImageApp", dependencies: [ .product(name: "WebP", package: "libwebp"), ] ), ] ) ``` -------------------------------- ### Handle WebP Decoding and Encoding Errors in Swift Source: https://context7.com/the-swift-collective/libwebp/llms.txt Demonstrates how to catch and handle specific WebP errors during image decoding and encoding operations using Swift's `do-catch` blocks. It handles unknown decoding/encoding errors and general NSError exceptions for file operations. The function takes a file path as input and returns a WebP image object or nil if an error occurs. ```swift import WebP import Foundation func processWebPImage(path: String) -> WebP? { do { let data = try Data(contentsOf: URL(fileURLWithPath: path)) let bytes = [UInt8](data) let image = try WebP.decode(bytes) return image } catch WebPError.unknownDecodingError { print("Invalid or corrupted WebP data") return nil } catch WebPError.unknownEncodingError { print("Failed to encode image data") return nil } catch let error as NSError { print("File error: (error.localizedDescription)") return nil } } // Usage if let image = processWebPImage(path: "photo.webp") { print("Successfully loaded (image.width)x(image.height) image") } else { print("Failed to load image") } ``` -------------------------------- ### Add libwebp as a Swift Package Dependency Source: https://github.com/the-swift-collective/libwebp/blob/main/README.md This code snippet shows how to add the libwebp Swift package as a dependency in your project's Package.swift file. It specifies the package URL and the version to use. ```swift dependencies: [ .package(url: "https://github.com/the-swift-collective/libwebp.git", from: "1.4.0"), ] ``` -------------------------------- ### Decode WebP Images to RGBA Data in Swift Source: https://context7.com/the-swift-collective/libwebp/llms.txt Converts WebP-encoded byte data into raw RGBA pixel data along with width and height information. This method requires the WebP library and Foundation framework. It takes an array of UInt8 representing the WebP data and returns a WebPImage struct containing RGBA data, width, and height, or throws a WebPError on failure. ```swift import WebP import Foundation // Load WebP file data let webpData = try Data(contentsOf: URL(fileURLWithPath: "image.webp")) let bytes = [UInt8](webpData) // Decode WebP to RGBA do { let image = try WebP.decode(bytes) print("Decoded image: \(image.width)x\(image.height)") print("RGBA data size: \(image.rgba.count) bytes") // Access pixel data // RGBA format: 4 bytes per pixel (R, G, B, A) let firstPixelRed = image.rgba[0] let firstPixelGreen = image.rgba[1] let firstPixelBlue = image.rgba[2] let firstPixelAlpha = image.rgba[3] } catch WebPError.unknownDecodingError { print("Failed to decode WebP image") } catch { print("Error: \(error)") } ``` -------------------------------- ### Encode RGBA Data to WebP Format in Swift Source: https://context7.com/the-swift-collective/libwebp/llms.txt Converts raw RGBA pixel data into WebP-encoded bytes with configurable quality settings. This function requires the WebP library and Foundation. It takes width, height, and an array of RGBA bytes, along with an optional quality parameter (0-100). It returns Data representing the encoded WebP image or throws a WebPError on failure. ```swift import WebP import Foundation // Create raw RGBA image data (100x100 red square) let width = 100 let height = 100 var rgba = [UInt8]() for _ in 0..<(width * height) { rgba.append(255) // Red rgba.append(0) // Green rgba.append(0) // Blue rgba.append(255) // Alpha } // Create WebP instance let webp = WebP(width: width, height: height, rgba: rgba) // Encode to WebP format // Quality: 0-100 (0=smallest file, 100=best quality) do { let encodedData = try webp.encode(quality: 80) print("Encoded WebP size: \(encodedData.count) bytes") // Save to file let data = Data(encodedData) try data.write(to: URL(fileURLWithPath: "output.webp")) } catch WebPError.unknownEncodingError { print("Failed to encode WebP image") } catch { print("Error: \(error)") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.