Coverage Report

Created: 2026-01-16 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavif/src/reformat_libsharpyuv.c
Line
Count
Source
1
// Copyright 2022 Google LLC
2
// SPDX-License-Identifier: BSD-2-Clause
3
4
#include "avif/internal.h"
5
6
#if defined(AVIF_LIBSHARPYUV_ENABLED)
7
#include <limits.h>
8
#include <sharpyuv/sharpyuv.h>
9
#include <sharpyuv/sharpyuv_csp.h>
10
11
avifResult avifImageRGBToYUVLibSharpYUV(avifImage * image, const avifRGBImage * rgb, const avifReformatState * state)
12
1.42k
{
13
    // The width, height, and stride parameters of SharpYuvConvertWithOptions()
14
    // and SharpYuvConvert() are all of the int type.
15
1.42k
    if (rgb->width > INT_MAX || rgb->height > INT_MAX || rgb->rowBytes > INT_MAX || image->yuvRowBytes[AVIF_CHAN_Y] > INT_MAX) {
16
0
        return AVIF_RESULT_NOT_IMPLEMENTED;
17
0
    }
18
1.42k
    const SharpYuvColorSpace colorSpace = {
19
1.42k
        state->yuv.kr, state->yuv.kb, image->depth, (state->yuv.range == AVIF_RANGE_LIMITED) ? kSharpYuvRangeLimited : kSharpYuvRangeFull
20
1.42k
    };
21
22
1.42k
    SharpYuvConversionMatrix matrix;
23
    // Fills in 'matrix' for the given YUVColorSpace.
24
1.42k
    SharpYuvComputeConversionMatrix(&colorSpace, &matrix);
25
1.42k
#if SHARPYUV_VERSION >= SHARPYUV_MAKE_VERSION(0, 4, 0)
26
1.42k
    SharpYuvOptions options;
27
1.42k
    SharpYuvOptionsInit(&matrix, &options);
28
1.42k
    if (image->transferCharacteristics == AVIF_TRANSFER_CHARACTERISTICS_UNSPECIFIED) {
29
        // Set to sRGB for backward compatibility.
30
1.41k
        options.transfer_type = kSharpYuvTransferFunctionSrgb;
31
1.41k
    } else {
32
8
        options.transfer_type = (SharpYuvTransferFunctionType)image->transferCharacteristics;
33
8
    }
34
1.42k
    const int sharpyuvRes = SharpYuvConvertWithOptions(&rgb->pixels[state->rgb.offsetBytesR],
35
1.42k
                                                       &rgb->pixels[state->rgb.offsetBytesG],
36
1.42k
                                                       &rgb->pixels[state->rgb.offsetBytesB],
37
1.42k
                                                       state->rgb.pixelBytes,
38
1.42k
                                                       rgb->rowBytes,
39
1.42k
                                                       rgb->depth,
40
1.42k
                                                       image->yuvPlanes[AVIF_CHAN_Y],
41
1.42k
                                                       image->yuvRowBytes[AVIF_CHAN_Y],
42
1.42k
                                                       image->yuvPlanes[AVIF_CHAN_U],
43
1.42k
                                                       image->yuvRowBytes[AVIF_CHAN_U],
44
1.42k
                                                       image->yuvPlanes[AVIF_CHAN_V],
45
1.42k
                                                       image->yuvRowBytes[AVIF_CHAN_V],
46
1.42k
                                                       image->depth,
47
1.42k
                                                       rgb->width,
48
1.42k
                                                       rgb->height,
49
1.42k
                                                       &options);
50
#else
51
    const int sharpyuvRes = SharpYuvConvert(&rgb->pixels[state->rgb.offsetBytesR],
52
                                            &rgb->pixels[state->rgb.offsetBytesG],
53
                                            &rgb->pixels[state->rgb.offsetBytesB],
54
                                            state->rgb.pixelBytes,
55
                                            rgb->rowBytes,
56
                                            rgb->depth,
57
                                            image->yuvPlanes[AVIF_CHAN_Y],
58
                                            image->yuvRowBytes[AVIF_CHAN_Y],
59
                                            image->yuvPlanes[AVIF_CHAN_U],
60
                                            image->yuvRowBytes[AVIF_CHAN_U],
61
                                            image->yuvPlanes[AVIF_CHAN_V],
62
                                            image->yuvRowBytes[AVIF_CHAN_V],
63
                                            image->depth,
64
                                            rgb->width,
65
                                            rgb->height,
66
                                            &matrix);
67
#endif // SHARPYUV_VERSION >= SHARPYUV_MAKE_VERSION(0, 4, 0)
68
1.42k
    if (!sharpyuvRes) {
69
0
        return AVIF_RESULT_REFORMAT_FAILED;
70
0
    }
71
72
1.42k
    return AVIF_RESULT_OK;
73
1.42k
}
74
75
#else
76
77
avifResult avifImageRGBToYUVLibSharpYUV(avifImage * image, const avifRGBImage * rgb, const avifReformatState * state)
78
{
79
    (void)image;
80
    (void)rgb;
81
    (void)state;
82
    return AVIF_RESULT_NOT_IMPLEMENTED;
83
}
84
#endif // defined(AVIF_LIBSHARPYUV_ENABLED)