|
| 1 | +# Image Serialization Plugin |
| 2 | + |
| 3 | +Image Serialization Plugin is a plugin based on the PluginAPI, it provides support for encoding and decoding images. It leverages the plugin architecture to extend its support for different image formats without needing to modify the core system. |
| 4 | + |
| 5 | +Plugins that conform to the `ImageSerializationPlugin` protocol can be registered into the `PluginRegistry` and used to encode or decode images in different formats, such as PNG, JPEG, WebP, HEIC, and more. |
| 6 | + |
| 7 | +When a plugin supporting a specific image format is available, the `ImageSerializer` can dynamically choose the correct plugin based on the image format required, ensuring modularity and scalability in image handling. |
| 8 | + |
| 9 | + |
| 10 | +# Image Serialization Plugin |
| 11 | + |
| 12 | +The **Image Serialization Plugin** extends the functionality of the SnapshotTesting library by enabling support for multiple image formats through a plugin architecture. This PluginAPI allows image encoding and decoding to be easily extended without modifying the core logic of the system. |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +The **Image Serialization Plugin** provides an interface for encoding and decoding images in various formats. By conforming to both the `ImageSerialization` and `SnapshotTestingPlugin` protocols, it integrates with the broader plugin system, allowing for the seamless addition of new image formats. The default implementation supports PNG, but this architecture allows users to define custom plugins for other formats. |
| 17 | + |
| 18 | +### Image Serialization Plugin Architecture |
| 19 | + |
| 20 | +The **Image Serialization Plugin** relies on the PluginAPI that is a combination of protocols and a centralized registry to manage and discover plugins. The architecture allows for dynamic registration of image serialization plugins, which can be automatically discovered at runtime using the Objective-C runtime. This makes the system highly extensible, with plugins being automatically registered without the need for manual intervention. |
| 21 | + |
| 22 | +#### Key Components: |
| 23 | + |
| 24 | +1. **`ImageSerialization` Protocol**: |
| 25 | + - Defines the core methods for encoding and decoding images. |
| 26 | + - Requires plugins to specify the image format they support using the `ImageSerializationFormat` enum. |
| 27 | + - Provides methods for encoding (`encodeImage`) and decoding (`decodeImage`) images. |
| 28 | + |
| 29 | +2. **`ImageSerializationFormat` Enum**: |
| 30 | + - Represents supported image formats. |
| 31 | + - Includes predefined formats such as `.png` and extensible formats through the `.plugins(String)` case, allowing for custom formats to be introduced via plugins. |
| 32 | + |
| 33 | +3. **`ImageSerializer` Class**: |
| 34 | + - Responsible for encoding and decoding images using the registered plugins. |
| 35 | + - Retrieves available plugins from the `PluginRegistry` and uses the first matching plugin for the requested image format. |
| 36 | + - Provides default implementations for PNG encoding and decoding if no plugin is available for a given format. |
| 37 | + |
| 38 | +#### Example Plugin Flow: |
| 39 | + |
| 40 | +1. **Plugin Discovery**: |
| 41 | + - Plugins are automatically discovered at runtime through the Objective-C runtime, which identifies classes that conform to both the `ImageSerialization` and `SnapshotTestingPlugin` protocols. |
| 42 | + |
| 43 | +2. **Plugin Registration**: |
| 44 | + - Each plugin registers itself with the `PluginRegistry`, allowing it to be retrieved when needed for image serialization. |
| 45 | + |
| 46 | +3. **Image Encoding/Decoding**: |
| 47 | + - When an image needs to be serialized, the `ImageSerializer` checks the available plugins for one that supports the requested format. |
| 48 | + - If no plugin is found, it defaults to the built-in PNG encoding/decoding methods. |
| 49 | + |
| 50 | +#### Extensibility |
| 51 | + |
| 52 | +The plugin architecture allows developers to introduce new image formats without modifying the core SnapshotTesting library. By creating a new plugin that conforms to `ImageSerializationPlugin`, you can easily add support for additional image formats. |
| 53 | + |
| 54 | +Here are a few example plugins demonstrating how to extend the library with new image formats: |
| 55 | + |
| 56 | +- **[Image Serialization Plugin - HEIC](https://github.com/mackoj/swift-snapshot-testing-plugin-heic)**: Enables storing images in the `.heic` format, which reduces file sizes compared to PNG. |
| 57 | +- **[Image Serialization Plugin - WEBP](https://github.com/mackoj/swift-snapshot-testing-plugin-webp)**: Allows storing images in the `.webp` format, which offers better compression than PNG. |
| 58 | +- **[Image Serialization Plugin - JXL](https://github.com/mackoj/swift-snapshot-testing-plugin-jxl)**: Facilitates storing images in the `.jxl` format, which provides superior compression and quality compared to PNG. |
| 59 | + |
| 60 | +## Usage |
| 61 | + |
| 62 | +For example, if you want to use JPEG XL as a new image format for your snapshots, you can follow these steps. This approach applies to any image format as long as you have a plugin that conforms to `ImageSerializationPlugin`. |
| 63 | + |
| 64 | +1. **Add the Dependency**: Include the appropriate image serialization plugin as a dependency in your `Package.swift` file. For JPEG XL, it would look like this: |
| 65 | + |
| 66 | + ```swift |
| 67 | + .package(url: "https://github.com/mackoj/swift-snapshot-testing-plugin-jxl.git", revision: "0.0.1"), |
| 68 | + ``` |
| 69 | + |
| 70 | +2. **Link to Your Test Target**: Add the image serialization plugin to your test target's dependencies: |
| 71 | + |
| 72 | + ```swift |
| 73 | + .product(name: "JXLImageSerializer", package: "swift-snapshot-testing-plugin-jxl"), |
| 74 | + ``` |
| 75 | + |
| 76 | +3. **Import and Set Up**: In your test file, import the serializer and configure the image format in the `setUp()` method: |
| 77 | + |
| 78 | + ```swift |
| 79 | + import JXLImageSerializer |
| 80 | + |
| 81 | + override class func setUp() { |
| 82 | + SnapshotTesting.imageFormat = JXLImageSerializer.imageFormat |
| 83 | + } |
| 84 | + ``` |
| 85 | + |
| 86 | + Alternatively, you can specify the image format for individual assertions: |
| 87 | + |
| 88 | + ```swift |
| 89 | + assertSnapshot(of: label, as: .image(precision: 0.9, format: JXLImageSerializer.imageFormat)) |
| 90 | + ``` |
| 91 | + |
| 92 | +This setup demonstrates how to integrate a specific image format plugin. Replace `JXLImageSerializer` with the appropriate plugin and format for other image formats. |
0 commit comments