|
| 1 | +// |
| 2 | +// JpegXLAnimatedDecoder.mm |
| 3 | +// JxclCoder [https://github.com/awxkee/jxl-coder-swift] |
| 4 | +// |
| 5 | +// Created by Radzivon Bartoshyk on 27/10/2023. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | +// |
| 25 | + |
| 26 | +#import <Foundation/Foundation.h> |
| 27 | +#import "JpegXLAnimatedDecoder.h" |
| 28 | +#import "JxlAnimatedDecoder.hpp" |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +template <typename DataType> |
| 32 | +class JXLDDataWrapper { |
| 33 | +public: |
| 34 | + JXLDDataWrapper(const std::vector<DataType>& src): data(src) {} |
| 35 | + const std::vector<DataType> data; |
| 36 | +}; |
| 37 | + |
| 38 | +static void JXLDCGData8ProviderReleaseDataCallback(void *info, const void *data, size_t size) { |
| 39 | + auto dataWrapper = static_cast<JXLDDataWrapper<uint8_t>*>(info); |
| 40 | + delete dataWrapper; |
| 41 | +} |
| 42 | + |
| 43 | +@implementation JpegXLAnimatedDecoder { |
| 44 | + JxlAnimatedDecoder* dec; |
| 45 | + std::vector<uint8_t> mSrc; |
| 46 | +} |
| 47 | + |
| 48 | +-(nullable id)initWith:(nonnull NSData*)data error:(NSError * _Nullable *_Nullable)error { |
| 49 | + try { |
| 50 | + const uint8_t* ptr = reinterpret_cast<const uint8_t*>([data bytes]); |
| 51 | + mSrc.resize([data length]); |
| 52 | + std::copy(ptr, ptr + [data length], mSrc.begin()); |
| 53 | + dec = new JxlAnimatedDecoder(mSrc); |
| 54 | + } catch (AnimatedDecoderError& err) { |
| 55 | + NSString *str = [[NSString alloc] initWithCString:err.what() encoding:NSUTF8StringEncoding]; |
| 56 | + *error = [[NSError alloc] initWithDomain:@"JpegXLAnimatedDecoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: str }]; |
| 57 | + return nil; |
| 58 | + } catch (std::bad_alloc &err) { |
| 59 | + NSString *str = [[NSString alloc] initWithCString:err.what() encoding:NSUTF8StringEncoding]; |
| 60 | + *error = [[NSError alloc] initWithDomain:@"JpegXLAnimatedDecoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: str }]; |
| 61 | + return nil; |
| 62 | + } |
| 63 | + return self; |
| 64 | +} |
| 65 | + |
| 66 | +-(nullable JXLSystemImage *)get:(int)frame |
| 67 | + error:(NSError *_Nullable * _Nullable)error { |
| 68 | + try { |
| 69 | + JxlFrame jxlFrame = dec->getFrame(frame); |
| 70 | + auto wrapper = new JXLDDataWrapper<uint8_t>(jxlFrame.pixels); |
| 71 | + |
| 72 | + CGDataProviderRef provider = CGDataProviderCreateWithData(wrapper, |
| 73 | + wrapper->data.data(), |
| 74 | + wrapper->data.size(), |
| 75 | + JXLDCGData8ProviderReleaseDataCallback); |
| 76 | + if (!provider) { |
| 77 | + delete wrapper; |
| 78 | + *error = [[NSError alloc] initWithDomain:@"JXLCoder" |
| 79 | + code:500 |
| 80 | + userInfo:@{ NSLocalizedDescriptionKey: @"CoreGraphics cannot allocate required provider" }]; |
| 81 | + return nullptr; |
| 82 | + } |
| 83 | + |
| 84 | + int bitsPerComponent = sizeof(uint8_t) * 8; |
| 85 | + int components = 4; |
| 86 | + int bitsPerPixel = bitsPerComponent*components; |
| 87 | + int stride = 4 * dec->getWidth() * sizeof(uint8_t); |
| 88 | + |
| 89 | + CGColorSpaceRef colorSpace; |
| 90 | + if (jxlFrame.iccProfile.size() > 0) { |
| 91 | + CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, jxlFrame.iccProfile.data(), jxlFrame.iccProfile.size()); |
| 92 | + colorSpace = CGColorSpaceCreateWithICCData(iccData); |
| 93 | + CFRelease(iccData); |
| 94 | + } else { |
| 95 | + colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 96 | + } |
| 97 | + |
| 98 | + if (!colorSpace) { |
| 99 | + colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 100 | + } |
| 101 | + |
| 102 | + int flags; |
| 103 | + flags = (int)kCGImageByteOrderDefault; |
| 104 | + if (components == 4) { |
| 105 | + flags |= (int)kCGImageAlphaLast; |
| 106 | + } else { |
| 107 | + flags |= (int)kCGImageAlphaNone; |
| 108 | + } |
| 109 | + |
| 110 | + CGImageRef imageRef = CGImageCreate(dec->getWidth(), dec->getHeight(), bitsPerComponent, |
| 111 | + bitsPerPixel, |
| 112 | + stride, |
| 113 | + colorSpace, flags, provider, NULL, false, kCGRenderingIntentDefault); |
| 114 | + if (!imageRef) { |
| 115 | + *error = [[NSError alloc] initWithDomain:@"JXLCoder" |
| 116 | + code:500 |
| 117 | + userInfo:@{ NSLocalizedDescriptionKey: @"CoreGraphics cannot allocate CGImageRef" }]; |
| 118 | + return NULL; |
| 119 | + } |
| 120 | + JXLSystemImage *image = nil; |
| 121 | + #if JXL_PLUGIN_MAC |
| 122 | + image = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeZero]; |
| 123 | + #else |
| 124 | + image = [UIImage imageWithCGImage:imageRef scale:1 orientation:UIImageOrientationUp]; |
| 125 | + #endif |
| 126 | + |
| 127 | + return image; |
| 128 | + } catch (AnimatedDecoderError& err) { |
| 129 | + NSString *str = [[NSString alloc] initWithCString:err.what() encoding:NSUTF8StringEncoding]; |
| 130 | + *error = [[NSError alloc] initWithDomain:@"JpegXLAnimatedDecoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: str }]; |
| 131 | + return nil; |
| 132 | + } catch (std::bad_alloc &err) { |
| 133 | + NSString *str = [[NSString alloc] initWithCString:err.what() encoding:NSUTF8StringEncoding]; |
| 134 | + *error = [[NSError alloc] initWithDomain:@"JpegXLAnimatedDecoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: str }]; |
| 135 | + return nil; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +-(NSUInteger)framesCount { |
| 140 | + return static_cast<NSUInteger>(dec->getNumberOfFrames()); |
| 141 | +} |
| 142 | + |
| 143 | +-(int)loopCount { |
| 144 | + return static_cast<int>(dec->getLoopCount()); |
| 145 | +} |
| 146 | + |
| 147 | +-(int)frameDuration:(int)frame { |
| 148 | + return static_cast<int>(dec->getFrameDuration(frame)); |
| 149 | +} |
| 150 | + |
| 151 | +-(void)deinit { |
| 152 | + if (dec) { |
| 153 | + delete dec; |
| 154 | + } |
| 155 | + mSrc.clear(); |
| 156 | +} |
| 157 | + |
| 158 | +@end |
0 commit comments