Coverage Report

Created: 2026-12-01 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/foreign/cairo.c
Line
Count
Source
1
/* Shared code for cairo based loaders like svgload and pdfload.
2
 */
3
4
/*
5
6
  This file is part of VIPS.
7
8
  VIPS is free software; you can redistribute it and/or modify
9
  it under the terms of the GNU Lesser General Public License as published by
10
  the Free Software Foundation; either version 2 of the License, or
11
  (at your option) any later version.
12
13
  This program is distributed in the hope that it will be useful,
14
  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
  GNU Lesser General Public License for more details.
17
18
  You should have received a copy of the GNU Lesser General Public License
19
  along with this program; if not, write to the Free Software
20
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
  02110-1301  USA
22
23
 */
24
25
/*
26
27
  These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
28
29
 */
30
31
#ifdef HAVE_CONFIG_H
32
#include <config.h>
33
#endif /*HAVE_CONFIG_H*/
34
35
#include <vips/vips.h>
36
#include <vips/internal.h>
37
38
/* Convert from Cairo-style premultiplied BGRA to RGBA.
39
 *
40
 * See also openslide's argb2rgba().
41
 */
42
void
43
vips__premultiplied_bgra2rgba(guint32 *restrict p, int n)
44
0
{
45
0
  int x;
46
47
0
  for (x = 0; x < n; x++) {
48
0
    guint32 bgra = GUINT32_FROM_BE(p[x]);
49
0
    guint8 a = bgra & 0xff;
50
51
0
    guint32 rgba;
52
53
0
    if (a == 0 ||
54
0
      a == 255)
55
0
      rgba =
56
0
        (bgra & 0x00ff00ff) |
57
0
        (bgra & 0x0000ff00) << 16 |
58
0
        (bgra & 0xff000000) >> 16;
59
0
    else
60
      /* Undo premultiplication.
61
       */
62
0
      rgba =
63
0
        ((255 * ((bgra >> 8) & 0xff) / a) << 24) |
64
0
        ((255 * ((bgra >> 16) & 0xff) / a) << 16) |
65
0
        ((255 * ((bgra >> 24) & 0xff) / a) << 8) |
66
0
        a;
67
68
0
    p[x] = GUINT32_TO_BE(rgba);
69
0
  }
70
0
}
71
72
/* Unpremultiplied RGBA (vips convention) to cairo-style premul BGRA.
73
 */
74
void
75
vips__rgba2bgra_premultiplied(guint32 *restrict p, int n)
76
0
{
77
0
  int x;
78
79
0
  for (x = 0; x < n; x++) {
80
0
    guint32 rgba = GUINT32_FROM_BE(p[x]);
81
0
    guint8 a = rgba & 0xff;
82
83
0
    guint32 bgra;
84
85
0
    if (a == 0)
86
0
      bgra = 0;
87
0
    else if (a == 255)
88
0
      bgra = (rgba & 0x00ff00ff) |
89
0
        (rgba & 0x0000ff00) << 16 |
90
0
        (rgba & 0xff000000) >> 16;
91
0
    else {
92
0
      int r = (rgba >> 24) & 0xff;
93
0
      int g = (rgba >> 16) & 0xff;
94
0
      int b = (rgba >> 8) & 0xff;
95
96
0
      r = ((r * a) + 128) >> 8;
97
0
      g = ((g * a) + 128) >> 8;
98
0
      b = ((b * a) + 128) >> 8;
99
100
0
      bgra = (b << 24) | (g << 16) | (r << 8) | a;
101
0
    }
102
103
0
    p[x] = GUINT32_TO_BE(bgra);
104
0
  }
105
0
}
106
107
/* Convert from PDFium-style BGRA to RGBA.
108
 */
109
void
110
vips__bgra2rgba(guint32 *restrict p, int n)
111
311
{
112
311
  int x;
113
114
622
  for (x = 0; x < n; x++) {
115
311
    guint32 bgra = GUINT32_FROM_BE(p[x]);
116
117
311
    guint rgba;
118
119
    /* Leave G and A, swap R and B.
120
     */
121
311
    rgba =
122
311
      (bgra & 0x00ff00ff) |
123
311
      (bgra & 0x0000ff00) << 16 |
124
311
      (bgra & 0xff000000) >> 16;
125
126
311
    p[x] = GUINT32_TO_BE(rgba);
127
311
  }
128
311
}
129
130
/*
131
 * Convert from Cairo-style premultiplied RGBA128F to straight RGBA, for one row.
132
 * It also linearizes the pixel values.
133
134
 * Processes 'n' pixels in the 'p' buffer.
135
 * The data is assumed to be RGBA (R, G, B, A) 32-bit floats per pixel.
136
 */
137
void
138
vips__premultiplied_rgb1282scrgba(float *restrict p, int n)
139
0
{
140
0
  vips_col_make_tables_RGB_16();
141
0
  for (int x = 0; x < n; x++) {
142
    // CLIP is much faster than FCLIP, and we want an int result
143
0
    int ri = VIPS_CLIP(0, (int) (p[0] * 65535), 65535);
144
0
    int gi = VIPS_CLIP(0, (int) (p[1] * 65535), 65535);
145
0
    int bi = VIPS_CLIP(0, (int) (p[2] * 65535), 65535);
146
147
    // linearize the values with LUT
148
0
    float r = vips_v2Y_16[ri];
149
0
    float g = vips_v2Y_16[gi];
150
0
    float b = vips_v2Y_16[bi];
151
0
    float a = p[3];
152
153
0
    p[0] = a > 0.00001 ? r / a : 0.0F;
154
0
    p[1] = a > 0.00001 ? g / a : 0.0F;
155
0
    p[2] = a > 0.00001 ? b / a : 0.0F;
156
157
0
    p += 4;
158
0
  }
159
0
}