Skip to content

Commit

Permalink
transform 10bit images to RGBA1010102
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 10, 2023
1 parent 7238458 commit af4fbdd
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 35 deletions.
22 changes: 20 additions & 2 deletions Sources/SDWebImageNukePlugin.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 19 additions & 1 deletion Sources/avif/AVIF+Errors.swift
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 21 additions & 4 deletions Sources/avif/AVIFDecoder.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 20 additions & 2 deletions Sources/avif/AVIFEncoder.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 20 additions & 2 deletions Sources/avif/AnimatedEncoder.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 20 additions & 3 deletions Sources/avif/Data+AVIFFormat.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 20 additions & 2 deletions Sources/avifc/AVIFEncoding.mm
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>
#if __has_include(<libavif/avif.h>)
Expand Down
24 changes: 21 additions & 3 deletions Sources/avifc/AVIFImageXForm.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,4 +39,4 @@
@end


#endif /* Header_h */
#endif /* AVIFImageXForm_h */
71 changes: 58 additions & 13 deletions Sources/avifc/AVIFImageXForm.mm
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>
Expand All @@ -12,6 +30,8 @@
#import <CoreGraphics/CoreGraphics.h>
#import "PerceptualQuantinizer.h"
#import "TargetConditionals.h"
#import "Rgb1010102Converter.h"
#import "RgbTransfer.h"

@implementation AVIFImageXForm

Expand Down Expand Up @@ -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<unsigned char*>(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;
Expand Down Expand Up @@ -100,10 +126,18 @@ - (_Nullable CGImageRef)formCGImage:(nonnull avifDecoder*)decoder scale:(CGFloat
transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084) {
[PerceptualQuantinizer transfer:reinterpret_cast<uint8_t*>(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) {
Expand Down Expand Up @@ -152,19 +186,30 @@ - (_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<uint8_t*>(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) {
free(pixelsData);
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;
}

Expand Down
21 changes: 20 additions & 1 deletion Sources/avifc/PlatformImage.mm
Original file line number Diff line number Diff line change
@@ -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 <Foundation/Foundation.h>
#import "PlatformImage.h"
Expand Down
Loading

0 comments on commit af4fbdd

Please sign in to comment.