Skip to content

Commit ad88f81

Browse files
committed
Fix the compile issue with libavif 0.11.0+ by removing the limit alpha range check
Update the min deps to 0.11.0+ as well
1 parent 33ad65f commit ad88f81

File tree

4 files changed

+15
-64
lines changed

4 files changed

+15
-64
lines changed

Cartfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
github "SDWebImage/SDWebImage" ~> 5.10
2-
github "SDWebImage/libavif-Xcode" >= 0.9.1
2+
github "SDWebImage/libavif-Xcode" >= 0.11.0

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let package = Package(
1818
// Dependencies declare other packages that this package depends on.
1919
// .package(url: /* package url */, from: "1.0.0"),
2020
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.10.0"),
21-
.package(url: "https://github.com/SDWebImage/libavif-Xcode.git", from: "0.9.1")
21+
.package(url: "https://github.com/SDWebImage/libavif-Xcode.git", from: "0.11.0")
2222
],
2323
targets: [
2424
// Targets are the basic building blocks of a package. A target can define a module or a test suite.

SDWebImageAVIFCoder.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ Which is built based on the open-sourced libavif codec.
4040
}
4141

4242
s.dependency 'SDWebImage', '~> 5.10'
43-
s.dependency 'libavif', '>= 0.9.1'
43+
s.dependency 'libavif', '>= 0.11.0'
4444
s.libraries = 'c++'
4545
end

SDWebImageAVIFCoder/Classes/Conversion.m

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,8 @@ static avifBool avifPrepareReformatState(const avifImage * image, const avifRGBI
155155
state->rgbMaxChannelF = (float)state->rgbMaxChannel;
156156
state->biasY = (state->yuvRange == AVIF_RANGE_LIMITED) ? (float)(16 << (state->yuvDepth - 8)) : 0.0f;
157157
state->biasUV = (float)(1 << (state->yuvDepth - 1));
158-
state->biasA = (image->alphaRange == AVIF_RANGE_LIMITED) ? (float)(16 << (state->yuvDepth - 8)) : 0.0f;
159158
state->rangeY = (float)((state->yuvRange == AVIF_RANGE_LIMITED) ? (219 << (state->yuvDepth - 8)) : state->yuvMaxChannel);
160159
state->rangeUV = (float)((state->yuvRange == AVIF_RANGE_LIMITED) ? (224 << (state->yuvDepth - 8)) : state->yuvMaxChannel);
161-
state->rangeA = (float)((image->alphaRange == AVIF_RANGE_LIMITED) ? (219 << (state->yuvDepth - 8)) : state->yuvMaxChannel);
162160

163161
uint32_t cpCount = 1 << image->depth;
164162
if (state->mode == AVIF_REFORMAT_MODE_IDENTITY) {
@@ -386,6 +384,7 @@ static CGImageRef CreateCGImage8(avifImage * avif) {
386384
uint8_t const permuteMap[4] = {0, 1, 2, 3};
387385
switch(avif->yuvFormat) {
388386
case AVIF_PIXEL_FORMAT_NONE:
387+
case AVIF_PIXEL_FORMAT_COUNT:
389388
NSLog(@"Invalid pixel format.");
390389
goto end_all;
391390
case AVIF_PIXEL_FORMAT_YUV420:
@@ -567,51 +566,11 @@ static CGImageRef CreateCGImage8(avifImage * avif) {
567566

568567
if(hasAlpha) { // alpha
569568
vImage_Buffer alphaBuffer = {0};
570-
if(avif->alphaRange == AVIF_RANGE_LIMITED) {
571-
float* floatAlphaBufferData = NULL;
572-
floatAlphaBufferData = calloc(avif->width * avif->height, sizeof(float));
573-
scaledAlphaBufferData = calloc(avif->width * avif->height, sizeof(uint8_t));
574-
if(floatAlphaBufferData == NULL || scaledAlphaBufferData == NULL) {
575-
err = kvImageMemoryAllocationError;
576-
goto end_prepare_alpha;
577-
}
578-
vImage_Buffer origAlphaBuffer = {
579-
.data = avif->alphaPlane,
580-
.width = avif->width,
581-
.height = avif->height,
582-
.rowBytes = avif->alphaRowBytes,
583-
};
584-
vImage_Buffer floatAlphaBuffer = {
585-
.data = floatAlphaBufferData,
586-
.width = avif->width,
587-
.height = avif->height,
588-
.rowBytes = avif->width * sizeof(float),
589-
};
590-
alphaBuffer.width = avif->width;
591-
alphaBuffer.height = avif->height;
592-
alphaBuffer.data = scaledAlphaBufferData;
593-
alphaBuffer.rowBytes = avif->width * sizeof(uint8_t);
594-
err = vImageConvert_Planar8toPlanarF(&origAlphaBuffer, &floatAlphaBuffer, 255.0f, 0.0f, kvImageNoFlags);
595-
if(err != kvImageNoError) {
596-
NSLog(@"Failed to convert alpha planes from uint8 to float: %ld", err);
597-
goto end_prepare_alpha;
598-
}
599-
err = vImageConvert_PlanarFtoPlanar8(&floatAlphaBuffer, &alphaBuffer, 235.0f, 16.0f, kvImageNoFlags);
600-
if(err != kvImageNoError) {
601-
NSLog(@"Failed to convert alpha planes from float to uint8: %ld", err);
602-
goto end_prepare_alpha;
603-
}
604-
end_prepare_alpha:
605-
free(floatAlphaBufferData);
606-
if(err != kvImageNoError) {
607-
goto end_alpha;
608-
}
609-
} else {
610-
alphaBuffer.width = avif->width;
611-
alphaBuffer.height = avif->height;
612-
alphaBuffer.data = avif->alphaPlane;
613-
alphaBuffer.rowBytes = avif->alphaRowBytes;
614-
}
569+
// libavif 0.11.0: alphaRange field was removed from the avifImage struct. It it presumed that alpha plane is always full range.
570+
alphaBuffer.width = avif->width;
571+
alphaBuffer.height = avif->height;
572+
alphaBuffer.data = avif->alphaPlane;
573+
alphaBuffer.rowBytes = avif->alphaRowBytes;
615574
if(monochrome) { // alpha_mono
616575
uint8_t* tmpBufferData = NULL;
617576
uint8_t* monoBufferData = NULL;
@@ -861,22 +820,13 @@ static CGImageRef CreateCGImage16U(avifImage * avif) {
861820
};
862821
float offset = 0.0f;
863822
float rangeMax = 0.0f;
823+
// libavif 0.11.0: alphaRange field was removed from the avifImage struct. It it presumed that alpha plane is always full range.
864824
if(avif->depth == 10) {
865-
if(avif->alphaRange == AVIF_RANGE_LIMITED) {
866-
offset = 64.0f;
867-
rangeMax = 940.0f;
868-
} else {
869-
offset = 0.0f;
870-
rangeMax = 1023.0f;
871-
}
825+
offset = 0.0f;
826+
rangeMax = 1023.0f;
872827
} else if(avif->depth == 12) {
873-
if(avif->alphaRange == AVIF_RANGE_LIMITED) {
874-
offset = 256.0f;
875-
rangeMax = 3760.0f;
876-
} else {
877-
offset = 0.0f;
878-
rangeMax = 4095.0f;
879-
}
828+
offset = 0.0f;
829+
rangeMax = 4095.0f;
880830
}
881831
float const scale = (float)(rangeMax - offset) / 65535.0f;
882832
err = vImageConvert_16UToF(&origAlpha, &floatAlphaBuffer, 0.0f, 1.0f, kvImageNoFlags);
@@ -919,6 +869,7 @@ static CGImageRef CreateCGImage16U(avifImage * avif) {
919869
uint8_t const permuteMap[4] = {0, 1, 2, 3};
920870
switch(avif->yuvFormat) {
921871
case AVIF_PIXEL_FORMAT_NONE:
872+
case AVIF_PIXEL_FORMAT_COUNT:
922873
NSLog(@"Invalid pixel format.");
923874
goto end_all;
924875
case AVIF_PIXEL_FORMAT_YUV420:

0 commit comments

Comments
 (0)