From af4fbdd75826d05f7b432f5eb13b55b5fb62b65a Mon Sep 17 00:00:00 2001 From: awxkee Date: Sun, 10 Sep 2023 18:43:46 +0400 Subject: [PATCH] transform 10bit images to RGBA1010102 --- Sources/SDWebImageNukePlugin.swift | 22 +++++- Sources/avif/AVIF+Errors.swift | 20 ++++- Sources/avif/AVIFDecoder.swift | 25 ++++++- Sources/avif/AVIFEncoder.swift | 22 +++++- Sources/avif/AnimatedEncoder.swift | 22 +++++- Sources/avif/Data+AVIFFormat.swift | 23 +++++- Sources/avifc/AVIFEncoding.mm | 22 +++++- Sources/avifc/AVIFImageXForm.h | 24 +++++- Sources/avifc/AVIFImageXForm.mm | 71 ++++++++++++++---- Sources/avifc/PlatformImage.mm | 21 +++++- Sources/avifc/Rgb1010102Converter.h | 34 +++++++++ Sources/avifc/Rgb1010102Converter.mm | 101 ++++++++++++++++++++++++++ Sources/avifc/RgbTransfer.h | 34 +++++++++ Sources/avifc/RgbTransfer.mm | 53 ++++++++++++++ Sources/avifc/include/AVIFEncoding.h | 20 ++++- Sources/avifc/include/PlatformImage.h | 19 +++++ Sources/avifnuke/AVIFNukePlugin.swift | 20 ++++- 17 files changed, 518 insertions(+), 35 deletions(-) create mode 100644 Sources/avifc/Rgb1010102Converter.h create mode 100644 Sources/avifc/Rgb1010102Converter.mm create mode 100644 Sources/avifc/RgbTransfer.h create mode 100644 Sources/avifc/RgbTransfer.mm diff --git a/Sources/SDWebImageNukePlugin.swift b/Sources/SDWebImageNukePlugin.swift index 2efb918..1dc9fa3 100644 --- a/Sources/SDWebImageNukePlugin.swift +++ b/Sources/SDWebImageNukePlugin.swift @@ -1,9 +1,27 @@ // -// File.swift -// +// SDWebImageAVIFCoder.swift +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 30/08/2023. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// import Foundation import SDWebImage diff --git a/Sources/avif/AVIF+Errors.swift b/Sources/avif/AVIF+Errors.swift index 3bb6ea5..f274ab3 100644 --- a/Sources/avif/AVIF+Errors.swift +++ b/Sources/avif/AVIF+Errors.swift @@ -1,9 +1,27 @@ // // AVIF+Errors.swift -// +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 27/05/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// import Foundation diff --git a/Sources/avif/AVIFDecoder.swift b/Sources/avif/AVIFDecoder.swift index 6f5f22f..3e3677d 100644 --- a/Sources/avif/AVIFDecoder.swift +++ b/Sources/avif/AVIFDecoder.swift @@ -1,9 +1,26 @@ // -// AVIFImage.swift -// Nuke-AVIF-Plugin +// AVIFDecoder.swift +// avif.swift [https://github.com/awxkee/avif.swift] // -// Created by delneg on 2021/12/05. -// Copyright © 2021 delneg. All rights reserved. +// Created by Radzivon Bartoshyk on 12/12/2021. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. // import Foundation diff --git a/Sources/avif/AVIFEncoder.swift b/Sources/avif/AVIFEncoder.swift index 35cbd10..d5e8044 100644 --- a/Sources/avif/AVIFEncoder.swift +++ b/Sources/avif/AVIFEncoder.swift @@ -1,9 +1,27 @@ // -// File.swift -// +// AVIFEncoder.swift +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 01/05/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// import Foundation import avifc diff --git a/Sources/avif/AnimatedEncoder.swift b/Sources/avif/AnimatedEncoder.swift index aa0a73b..d3f1872 100644 --- a/Sources/avif/AnimatedEncoder.swift +++ b/Sources/avif/AnimatedEncoder.swift @@ -1,9 +1,27 @@ // -// File.swift -// +// AnimatedEncoder.swift +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 22/06/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// import Foundation import avifc diff --git a/Sources/avif/Data+AVIFFormat.swift b/Sources/avif/Data+AVIFFormat.swift index 97a2257..33d1e06 100644 --- a/Sources/avif/Data+AVIFFormat.swift +++ b/Sources/avif/Data+AVIFFormat.swift @@ -1,9 +1,26 @@ // // Data+AVIFFormat.swift -// Nuke-AVIF-Plugin iOS +// avif.swift [https://github.com/awxkee/avif.swift] // -// Created by delneg on 2021/12/05. -// Copyright © 2021 delneg. All rights reserved. +// Created by Radzivon Bartoshyk on 05/12/2021. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. // //https://www.garykessler.net/library/file_sigs.html diff --git a/Sources/avifc/AVIFEncoding.mm b/Sources/avifc/AVIFEncoding.mm index 2ee025b..cf45e36 100644 --- a/Sources/avifc/AVIFEncoding.mm +++ b/Sources/avifc/AVIFEncoding.mm @@ -1,9 +1,27 @@ // -// AVIFEncoder.m -// +// AVIFEncoding.mm +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 01/05/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// #import #if __has_include() diff --git a/Sources/avifc/AVIFImageXForm.h b/Sources/avifc/AVIFImageXForm.h index 5d5a204..c902997 100644 --- a/Sources/avifc/AVIFImageXForm.h +++ b/Sources/avifc/AVIFImageXForm.h @@ -1,8 +1,26 @@ // // AVIFImageXForm.h -// +// avif.swift [https://github.com/awxkee/avif.swift] // -// Created by Radzivon Bartoshyk on 02/09/2023. +// Created by Radzivon Bartoshyk on 02/09/2022. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. // #ifndef AVIFImageXForm_h @@ -21,4 +39,4 @@ @end -#endif /* Header_h */ +#endif /* AVIFImageXForm_h */ diff --git a/Sources/avifc/AVIFImageXForm.mm b/Sources/avifc/AVIFImageXForm.mm index ad2872f..4d93a28 100644 --- a/Sources/avifc/AVIFImageXForm.mm +++ b/Sources/avifc/AVIFImageXForm.mm @@ -1,8 +1,26 @@ // -// AVIFImageXForm.m -// +// AVIFImageXForm.mm +// avif.swift [https://github.com/awxkee/avif.swift] // -// Created by Radzivon Bartoshyk on 02/09/2023. +// Created by Radzivon Bartoshyk on 02/09/2022. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. // #import @@ -12,6 +30,8 @@ #import #import "PerceptualQuantinizer.h" #import "TargetConditionals.h" +#import "Rgb1010102Converter.h" +#import "RgbTransfer.h" @implementation AVIFImageXForm @@ -43,10 +63,16 @@ - (_Nullable CGImageRef)formCGImage:(nonnull avifDecoder*)decoder scale:(CGFloat int newWidth = rgbImage.width; int newHeight = rgbImage.height; int newRowBytes = rgbImage.rowBytes; - int depth = isImageRequires64Bit ? 16 : 8; + int depth = decoder->image->depth == 10 ? 10 : (decoder->image->depth > 8 ? 16 : 8); int stride = rgbImage.rowBytes; auto pixelsData = reinterpret_cast(malloc(stride * newHeight)); - memcpy(pixelsData, rgbImage.pixels, stride * newHeight); + + if (![RgbTransfer CopyBuffer:rgbImage.pixels dst:pixelsData stride:stride width:newWidth height:newHeight pixelSize:isImageRequires64Bit ? sizeof(uint16_t) : sizeof(uint8_t)]) { + avifRGBImageFreePixels(&rgbImage); + free(pixelsData); + return nil; + } + avifRGBImageFreePixels(&rgbImage); CGColorSpaceRef colorSpace; @@ -100,10 +126,18 @@ - (_Nullable CGImageRef)formCGImage:(nonnull avifDecoder*)decoder scale:(CGFloat transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084) { [PerceptualQuantinizer transfer:reinterpret_cast(pixelsData) stride:stride width:newWidth height:newHeight U16:depth > 8 depth:depth half:depth > 8]; - if (depth > 8) { - colorSpace = CGColorSpaceCreateExtendedLinearized(CGColorSpaceCreateWithName(kCGColorSpaceExtendedITUR_2020)); + if (depth > 8 && depth != 10) { + if (@available(iOS 14.0, *)) { + colorSpace = CGColorSpaceCreateExtendedLinearized(CGColorSpaceCreateWithName(kCGColorSpaceExtendedITUR_2020)); + } else { + colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020); + } } else { - colorSpace = CGColorSpaceCreateLinearized(CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020)); + if (@available(iOS 14.0, *)) { + colorSpace = CGColorSpaceCreateLinearized(CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020)); + } else { + colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020); + } } } else if (colorPrimaries == AVIF_COLOR_PRIMARIES_BT2020 && transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_LINEAR) { @@ -152,10 +186,21 @@ - (_Nullable CGImageRef)formCGImage:(nonnull avifDecoder*)decoder scale:(CGFloat colorSpace = CGColorSpaceCreateDeviceRGB(); } int flags; - if (isImageRequires64Bit) { - flags = (int)kCGImageByteOrder16Little | (int)kCGImageAlphaLast | (int)kCGBitmapFloatComponents; + if (depth == 10) { + flags = (int)kCGBitmapByteOrder32Big | (int)kCGImagePixelFormatRGB101010 | (int)kCGImageAlphaLast; + uint8_t* rgb1010102Buffer = reinterpret_cast(malloc(newWidth * 4 * sizeof(uint8_t) * newHeight)); + if (![Rgb1010102Converter F16ToRGBA1010102:pixelsData dst:rgb1010102Buffer stride:&stride width:newWidth height:newHeight]) { + free(pixelsData); + return NULL; + } + free(pixelsData); + pixelsData = rgb1010102Buffer; } else { - flags = (int)kCGBitmapByteOrder32Big | (int)kCGImageAlphaPremultipliedLast; + if (isImageRequires64Bit) { + flags = (int)kCGImageByteOrder16Little | (int)kCGImageAlphaLast | (int)kCGBitmapFloatComponents; + } else { + flags = (int)kCGBitmapByteOrder32Big | (int)kCGImageAlphaPremultipliedLast; + } } CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixelsData, stride*newHeight, AV1CGDataProviderReleaseDataCallback); if (!provider) { @@ -163,8 +208,8 @@ - (_Nullable CGImageRef)formCGImage:(nonnull avifDecoder*)decoder scale:(CGFloat return NULL; } - CGImageRef imageRef = CGImageCreate(newWidth, newHeight, depth, isImageRequires64Bit ? 64 : 32, - newRowBytes, colorSpace, flags, provider, NULL, false, kCGRenderingIntentDefault); + CGImageRef imageRef = CGImageCreate(newWidth, newHeight, depth, (depth == 10 || depth == 8) ? 32 : 64, + stride, colorSpace, flags, provider, NULL, false, kCGRenderingIntentDefault); return imageRef; } diff --git a/Sources/avifc/PlatformImage.mm b/Sources/avifc/PlatformImage.mm index 475aeae..175e88e 100644 --- a/Sources/avifc/PlatformImage.mm +++ b/Sources/avifc/PlatformImage.mm @@ -1,8 +1,27 @@ // -// PlatformImage.m +// PlatformImage.mm +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 04/05/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// #import #import "PlatformImage.h" diff --git a/Sources/avifc/Rgb1010102Converter.h b/Sources/avifc/Rgb1010102Converter.h new file mode 100644 index 0000000..fe80c97 --- /dev/null +++ b/Sources/avifc/Rgb1010102Converter.h @@ -0,0 +1,34 @@ +// +// Rgb1010102Converter.h +// avif.swift [https://github.com/awxkee/avif.swift] +// +// Created by Radzivon Bartoshyk on 10/09/2022. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#ifndef Rgb1010102Converter_h +#define Rgb1010102Converter_h + +@interface Rgb1010102Converter : NSObject ++(bool)F16ToRGBA1010102:(nonnull uint8_t*)data dst:(nonnull uint8_t*)dst stride:(nonnull int*)stride width:(int)width height:(int)height; ++(bool)F16ToU16:(nonnull uint8_t*)src dst:(nonnull uint8_t*)dst stride:(int)stride width:(int)width height:(int)height; +@end + +#endif /* Rgb1010102Converter_h */ diff --git a/Sources/avifc/Rgb1010102Converter.mm b/Sources/avifc/Rgb1010102Converter.mm new file mode 100644 index 0000000..9e43777 --- /dev/null +++ b/Sources/avifc/Rgb1010102Converter.mm @@ -0,0 +1,101 @@ +// +// Rgb1010102Converter.mm +// avif.swift [https://github.com/awxkee/avif.swift] +// +// Created by Radzivon Bartoshyk on 10/09/2022. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + + +#import +#import "Rgb1010102Converter.h" +#import "Accelerate/Accelerate.h" +#import + +@implementation Rgb1010102Converter: NSObject + ++(bool)F16ToU16:(nonnull uint8_t*)src dst:(nonnull uint8_t*)dst stride:(int)stride width:(int)width height:(int)height { + vImage_Buffer srcBuffer = { + .data = (void*)src, + .width = static_cast(width * 4), + .height = static_cast(height), + .rowBytes = static_cast(stride) + }; + + vImage_Buffer dstBuffer = { + .data = dst, + .width = static_cast(width * 4), + .height = static_cast(height), + .rowBytes = static_cast(stride) + }; + const float scale = 1.0f / float((1 << 10) - 1); + vImage_Error vEerror = vImageConvert_16Fto16U(&srcBuffer, &dstBuffer,kvImageNoFlags); + if (vEerror != kvImageNoError) { + return false; + } + + vImage_Buffer permuteBuffer = { + .data = dst, + .width = static_cast(width), + .height = static_cast(height), + .rowBytes = static_cast(stride) + }; + const uint8_t permuteMap[4] = { 3,0,1,2 }; + vEerror = vImagePermuteChannels_ARGB16U(&permuteBuffer, &permuteBuffer, permuteMap, kvImageNoFlags); + if (vEerror != kvImageNoError) { + return false; + } + + return true; +} + ++(bool)F16ToRGBA1010102:(nonnull uint8_t*)data dst:(nonnull uint8_t*)dst stride:(nonnull int*)stride width:(int)width height:(int)height { + std::vector intermediateBuffer(height * (*stride)); + if (![self F16ToU16:data dst:intermediateBuffer.data() stride:*stride width:width height:height]) { + return false; + } + + int newStride = width * sizeof(uint8_t) * 4; + + vImage_Buffer srcBuffer = { + .data = (void*)intermediateBuffer.data(), + .width = static_cast(width), + .height = static_cast(height), + .rowBytes = static_cast(*stride) + }; + + vImage_Buffer dstBuffer = { + .data = dst, + .width = static_cast(width), + .height = static_cast(height), + .rowBytes = static_cast(newStride) + }; + const float scale = powf(2, 10) - 1; + const uint8_t permuteMap[4] = { 0,1,2,3 }; + vImage_Error vEerror = vImageConvert_ARGB16UToRGBA1010102(&srcBuffer, &dstBuffer, 0, (int32_t)scale, permuteMap, kvImageNoFlags); + if (vEerror != kvImageNoError) { + return false; + } + + *stride = newStride; + return true; +} + +@end diff --git a/Sources/avifc/RgbTransfer.h b/Sources/avifc/RgbTransfer.h new file mode 100644 index 0000000..cdf54ee --- /dev/null +++ b/Sources/avifc/RgbTransfer.h @@ -0,0 +1,34 @@ +// +// RgbTransfer.h +// avif.swift [https://github.com/awxkee/avif.swift] +// +// Created by Radzivon Bartoshyk on 10/09/2022. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#ifndef RgbTransfer_h +#define RgbTransfer_h + +@interface RgbTransfer : NSObject ++(bool)CopyBuffer:(nonnull uint8_t*)src dst:(nonnull uint8_t*)dst stride:(int)stride width:(int)width height:(int)height pixelSize:(int)pixelSize; +@end + + +#endif /* RgbTransfer_h */ diff --git a/Sources/avifc/RgbTransfer.mm b/Sources/avifc/RgbTransfer.mm new file mode 100644 index 0000000..3501b85 --- /dev/null +++ b/Sources/avifc/RgbTransfer.mm @@ -0,0 +1,53 @@ +// +// RgbTransfer.mm +// avif.swift [https://github.com/awxkee/avif.swift] +// +// Created by Radzivon Bartoshyk on 10/09/2022. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +#import +#import "RgbTransfer.h" +#import "Accelerate/Accelerate.h" + +@implementation RgbTransfer + ++(bool)CopyBuffer:(nonnull uint8_t*)src dst:(nonnull uint8_t*)dst stride:(int)stride width:(int)width height:(int)height pixelSize:(int)pixelSize { + vImage_Buffer srcBuffer = { + .data = (void*)src, + .width = static_cast(width * 4), + .height = static_cast(height), + .rowBytes = static_cast(stride) + }; + + vImage_Buffer dstBuffer = { + .data = dst, + .width = static_cast(width * 4), + .height = static_cast(height), + .rowBytes = static_cast(stride) + }; + vImage_Error vEerror = vImageCopyBuffer(&srcBuffer, &dstBuffer, static_cast(pixelSize), kvImageNoFlags); + if (vEerror != kvImageNoError) { + return false; + } + return true; +} + +@end diff --git a/Sources/avifc/include/AVIFEncoding.h b/Sources/avifc/include/AVIFEncoding.h index 2a5aa86..0c7abbe 100644 --- a/Sources/avifc/include/AVIFEncoding.h +++ b/Sources/avifc/include/AVIFEncoding.h @@ -1,9 +1,27 @@ // // AVIFEncoding.h -// +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 01/05/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// #import "AVIFImageMacros.h" diff --git a/Sources/avifc/include/PlatformImage.h b/Sources/avifc/include/PlatformImage.h index 791be8c..ae2ce5a 100644 --- a/Sources/avifc/include/PlatformImage.h +++ b/Sources/avifc/include/PlatformImage.h @@ -1,8 +1,27 @@ // // PlatformImage.h +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 04/05/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// #ifndef PlatformImage_h #define PlatformImage_h diff --git a/Sources/avifnuke/AVIFNukePlugin.swift b/Sources/avifnuke/AVIFNukePlugin.swift index ed721e6..592e41d 100644 --- a/Sources/avifnuke/AVIFNukePlugin.swift +++ b/Sources/avifnuke/AVIFNukePlugin.swift @@ -1,9 +1,27 @@ // // AVIFNukePlugin.swift -// +// avif.swift [https://github.com/awxkee/avif.swift] // // Created by Radzivon Bartoshyk on 26/06/2022. // +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// import Foundation import Nuke