summaryrefslogtreecommitdiff
path: root/pixman/pixman-format.c
blob: f89c7cf96ffad3ceba2879a8e7755fac98c8b8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright © 2009 Benjamin Otte
 *
 * 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.
 *
 * Author: Benjamin Otte <otte@gnome.org>
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pixman-private.h"

#include <stdlib.h>

static uint32_t *
alloc_bits (pixman_format_code_t format,
            int                  width,
            int                  height,
            uint32_t **          bits,
            int *                rowstrides_bytes)
{
    int stride;
    int buf_size;
    int bpp;

    /* what follows is a long-winded way, avoiding any possibility of integer
     * overflows, of saying:
     * stride = ((width * bpp + 0x1f) >> 5) * sizeof (uint32_t);
     */

    bpp = PIXMAN_FORMAT_BPP (format);
    assert (bpp > 0);
    if (pixman_multiply_overflows_int (width, bpp))
	return NULL;

    stride = width * bpp;
    if (pixman_addition_overflows_int (stride, 0x1f))
	return NULL;

    stride += 0x1f;
    stride >>= 5;

    stride *= sizeof (uint32_t);

    if (pixman_multiply_overflows_int (height, stride))
	return NULL;

    buf_size = height * stride;

    bits[0] = calloc (buf_size, 1);
    rowstrides_bytes[0] = stride;

    return bits[0];
}

static uint32_t *
alloc_y444 (pixman_format_code_t format,
            int                  width,
            int                  height,
            uint32_t **          bits,
            int *                rowstrides_bytes)
{
    int stride, bytes_per_plane;

    if (pixman_addition_overflows_int (width, 3))
        return NULL;
    stride = ROUND_UP_4 (width);

    if (pixman_multiply_overflows_int (stride, height))
	return NULL;
    bytes_per_plane = stride * height;

    if (pixman_multiply_overflows_int (bytes_per_plane, 3))
        return NULL;

    bits[0] = calloc (bytes_per_plane, 3);
    bits[1] = bits[0] + bytes_per_plane;
    bits[2] = bits[1] + bytes_per_plane;
    rowstrides_bytes[0] = stride;
    rowstrides_bytes[1] = stride;
    rowstrides_bytes[2] = stride;

    return bits[0];
}

static uint32_t *
alloc_y422 (pixman_format_code_t format,
            int                  width,
            int                  height,
            uint32_t **          bits,
            int *                rowstrides_bytes)
{
    int y_stride, uv_stride;

    if (pixman_addition_overflows_int (width, 7))
        return NULL;

    y_stride = ROUND_UP_4 (width);
    uv_stride = ROUND_UP_8 (width) / 2;

    if (pixman_addition_overflows_int (y_stride, 2 * uv_stride) ||
        pixman_multiply_overflows_int (y_stride + 2 * uv_stride, height))
	return NULL;

    bits[0] = calloc (y_stride + 2 * uv_stride, height);
    bits[1] = bits[0] + y_stride * height;
    bits[2] = bits[1] + uv_stride * height;
    rowstrides_bytes[0] = y_stride;
    rowstrides_bytes[1] = uv_stride;
    rowstrides_bytes[2] = uv_stride;

    return bits[0];
}

typedef struct {
    pixman_format_code_t                format;
    pixman_bool_t                       supported_source;
    pixman_bool_t                       supported_destination;
    pixman_bool_t                       alpha;
    unsigned int                        num_planes;
    pixman_color_space_t                default_color_space;
    uint32_t *                          (* alloc_bits) (pixman_format_code_t format,
                                                         int                  width,
                                                         int                  height,
                                                         uint32_t **          bits,
                                                         int *                rowstrides_bytes);
} format_info_t;

static const format_info_t format_infos[] = {
    { PIXMAN_a2b10g10r10, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x2b10g10r10, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a2r10g10b10, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x2r10g10b10, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a8r8g8b8, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x8r8g8b8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a8b8g8r8, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x8b8g8r8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_b8g8r8a8, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_b8g8r8x8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_r8g8b8a8, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_r8g8b8x8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_r8g8b8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_b8g8r8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    /* 16 bpp formats */
    { PIXMAN_r5g6b5, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_b5g6r5, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a1r5g5b5, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x1r5g5b5, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a1b5g5r5, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x1b5g5r5, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a4r4g4b4, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x4r4g4b4, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a4b4g4r4, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x4b4g4r4, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    /* 8bpp formats */
    { PIXMAN_a8, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_r3g3b2, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_b2g3r3, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a2r2g2b2, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a2b2g2r2, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_c8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_g8, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_x4a4, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    /* Collides with PIXMAN_c8
       { PIXMAN_x4c4, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
     */
    /* Collides with PIXMAN_g8
       { PIXMAN_x4g4, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
     */
    /* 4bpp formats */
    { PIXMAN_a4, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_r1g2b1, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_b1g2r1, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a1r1g1b1, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_a1b1g1r1, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_c4, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_g4, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    /* 1bpp formats */
    { PIXMAN_a1, TRUE, TRUE, TRUE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    { PIXMAN_g1, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits },
    /* planar formats */
    { PIXMAN_y444, TRUE, TRUE, FALSE, 3, PIXMAN_COLOR_SPACE_YCBCR_SD, alloc_y444 },
    { PIXMAN_y422, TRUE, TRUE, FALSE, 3, PIXMAN_COLOR_SPACE_YCBCR_SD, alloc_y422 },
    /* packed formats */
    { PIXMAN_yuy2, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_YCBCR_SD, alloc_bits },
    { PIXMAN_yvyu, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_YCBCR_SD, alloc_bits },
    { PIXMAN_uyvy, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_YCBCR_SD, alloc_bits },
    { PIXMAN_vyuy, TRUE, TRUE, FALSE, 1, PIXMAN_COLOR_SPACE_YCBCR_SD, alloc_bits },
    /* YUV formats */
    { PIXMAN_yv12, TRUE, FALSE, FALSE, 1, PIXMAN_COLOR_SPACE_ARGB, alloc_bits }
};

/* default values for "invalid" at the end */
static const format_info_t invalid_format =
    { 0, FALSE, FALSE, FALSE, 0, PIXMAN_COLOR_SPACE_ARGB, NULL };


static const format_info_t *
_get_format_info (pixman_format_code_t format)
{
    unsigned int i;

    for (i = 0; i < ARRAY_LENGTH (format_infos); i++) {
        if (format_infos[i].format == format)
            return &format_infos[i];
    }
#if DEBUG
    fprintf (stderr, "In %s: Invalid format code %u\n", FUNC, (unsigned) format);
#endif
    return &invalid_format;
}

uint32_t *
_pixman_format_alloc_bits (pixman_format_code_t format,
                           int                  width,
                           int                  height,
                           uint32_t **          bits,
                           int *                rowstrides)
{
    static const format_info_t *info;

    info = _get_format_info (format);
    if (info->alloc_bits == NULL)
        return NULL;

    return info->alloc_bits (format, width, height, bits, rowstrides);
}

pixman_color_space_t
_pixman_format_get_default_color_space (pixman_format_code_t format)
{
  return _get_format_info (format)->default_color_space;
}

/**
 * pixman_format_supported_source:
 * @format: A pixman_format_code_t format
 *
 * Return value: whether the provided format code is a supported
 * format for a pixman surface used as a source in
 * rendering.
 *
 * Currently, all pixman_format_code_t values are supported.
 **/
PIXMAN_EXPORT pixman_bool_t
pixman_format_supported_source (pixman_format_code_t format)
{
    return _get_format_info (format)->supported_source;
}

/**
 * pixman_format_supported_destination:
 * @format: A pixman_format_code_t format
 *
 * Return value: whether the provided format code is a supported
 * format for a pixman surface used as a destination in
 * rendering.
 *
 * Currently, all pixman_format_code_t values are supported
 * except for the YUV formats.
 **/
PIXMAN_EXPORT pixman_bool_t
pixman_format_supported_destination (pixman_format_code_t format)
{
    return _get_format_info (format)->supported_destination;
}

/**
 * pixman_format_num_planes:
 * @format: A pixman_format_code_t format
 *
 * Returns value: the number of planes for the given format. If
 * this number is different from 1, you must use 
 * pixman_image_create_planar() to create images in this format.
 **/
PIXMAN_EXPORT unsigned int
pixman_format_num_planes (pixman_format_code_t format)
{
    unsigned int num_planes;
    
    num_planes = _get_format_info (format)->num_planes;
    assert (num_planes <= PIXMAN_MAX_PLANES);

    return num_planes;
}

/**
 * pixman_format_is_opaque:
 * @format: A pixman_format_code_t format
 *
 * Returns: TRUE if images in the given @format are guaranteed to
 * be completely opaque
 **/
PIXMAN_EXPORT pixman_bool_t
pixman_format_is_opaque (pixman_format_code_t format)
{
    return !_get_format_info (format)->alpha;
}