2 * Copyright © 2000 SuSE, Inc.
3 * Copyright © 1999 Keith Packard
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of SuSE not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. SuSE makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
15 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Author: Keith Packard, SuSE, Inc.
31 #include "pixman-private.h"
34 _pixman_multiply_overflows_size (size_t a
, size_t b
)
36 return a
>= SIZE_MAX
/ b
;
40 _pixman_multiply_overflows_int (unsigned int a
, unsigned int b
)
42 return a
>= INT32_MAX
/ b
;
46 _pixman_addition_overflows_int (unsigned int a
, unsigned int b
)
48 return a
> INT32_MAX
- b
;
52 pixman_malloc_ab_plus_c (unsigned int a
, unsigned int b
, unsigned int c
)
54 if (!b
|| a
>= INT32_MAX
/ b
|| (a
* b
) > INT32_MAX
- c
)
57 return malloc (a
* b
+ c
);
61 pixman_malloc_ab (unsigned int a
,
64 if (a
>= INT32_MAX
/ b
)
67 return malloc (a
* b
);
71 pixman_malloc_abc (unsigned int a
,
75 if (a
>= INT32_MAX
/ b
)
77 else if (a
* b
>= INT32_MAX
/ c
)
80 return malloc (a
* b
* c
);
83 static force_inline
uint16_t
84 float_to_unorm (float f
, int n_bits
)
93 u
= f
* (1 << n_bits
);
99 static force_inline
float
100 unorm_to_float (uint16_t u
, int n_bits
)
102 uint32_t m
= ((1 << n_bits
) - 1);
104 return (u
& m
) * (1.f
/ (float)m
);
108 * This function expands images from a8r8g8b8 to argb_t. To preserve
109 * precision, it needs to know from which source format the a8r8g8b8 pixels
112 * For example, if the source was PIXMAN_x1r5g5b5 and the red component
113 * contained bits 12345, then the 8-bit value is 12345123. To correctly
114 * expand this to floating point, it should be 12345 / 31.0 and not
118 pixman_expand_to_float (argb_t
*dst
,
120 pixman_format_code_t format
,
123 static const float multipliers
[16] = {
125 1.0f
/ ((1 << 1) - 1),
126 1.0f
/ ((1 << 2) - 1),
127 1.0f
/ ((1 << 3) - 1),
128 1.0f
/ ((1 << 4) - 1),
129 1.0f
/ ((1 << 5) - 1),
130 1.0f
/ ((1 << 6) - 1),
131 1.0f
/ ((1 << 7) - 1),
132 1.0f
/ ((1 << 8) - 1),
133 1.0f
/ ((1 << 9) - 1),
134 1.0f
/ ((1 << 10) - 1),
135 1.0f
/ ((1 << 11) - 1),
136 1.0f
/ ((1 << 12) - 1),
137 1.0f
/ ((1 << 13) - 1),
138 1.0f
/ ((1 << 14) - 1),
139 1.0f
/ ((1 << 15) - 1),
141 int a_size
, r_size
, g_size
, b_size
;
142 int a_shift
, r_shift
, g_shift
, b_shift
;
143 float a_mul
, r_mul
, g_mul
, b_mul
;
144 uint32_t a_mask
, r_mask
, g_mask
, b_mask
;
147 if (!PIXMAN_FORMAT_VIS (format
))
148 format
= PIXMAN_a8r8g8b8
;
151 * Determine the sizes of each component and the masks and shifts
152 * required to extract them from the source pixel.
154 a_size
= PIXMAN_FORMAT_A (format
);
155 r_size
= PIXMAN_FORMAT_R (format
);
156 g_size
= PIXMAN_FORMAT_G (format
);
157 b_size
= PIXMAN_FORMAT_B (format
);
159 a_shift
= 32 - a_size
;
160 r_shift
= 24 - r_size
;
161 g_shift
= 16 - g_size
;
162 b_shift
= 8 - b_size
;
164 a_mask
= ((1 << a_size
) - 1);
165 r_mask
= ((1 << r_size
) - 1);
166 g_mask
= ((1 << g_size
) - 1);
167 b_mask
= ((1 << b_size
) - 1);
169 a_mul
= multipliers
[a_size
];
170 r_mul
= multipliers
[r_size
];
171 g_mul
= multipliers
[g_size
];
172 b_mul
= multipliers
[b_size
];
174 /* Start at the end so that we can do the expansion in place
177 for (i
= width
- 1; i
>= 0; i
--)
179 const uint32_t pixel
= src
[i
];
181 dst
[i
].a
= a_mask
? ((pixel
>> a_shift
) & a_mask
) * a_mul
: 1.0f
;
182 dst
[i
].r
= ((pixel
>> r_shift
) & r_mask
) * r_mul
;
183 dst
[i
].g
= ((pixel
>> g_shift
) & g_mask
) * g_mul
;
184 dst
[i
].b
= ((pixel
>> b_shift
) & b_mask
) * b_mul
;
189 pixman_float_to_unorm (float f
, int n_bits
)
191 return float_to_unorm (f
, n_bits
);
195 pixman_unorm_to_float (uint16_t u
, int n_bits
)
197 return unorm_to_float (u
, n_bits
);
201 pixman_contract_from_float (uint32_t *dst
,
207 for (i
= 0; i
< width
; ++i
)
211 a
= float_to_unorm (src
[i
].a
, 8);
212 r
= float_to_unorm (src
[i
].r
, 8);
213 g
= float_to_unorm (src
[i
].g
, 8);
214 b
= float_to_unorm (src
[i
].b
, 8);
216 dst
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | (b
<< 0);
221 _pixman_iter_get_scanline_noop (pixman_iter_t
*iter
, const uint32_t *mask
)
227 _pixman_iter_init_bits_stride (pixman_iter_t
*iter
, const pixman_iter_info_t
*info
)
229 pixman_image_t
*image
= iter
->image
;
230 uint8_t *b
= (uint8_t *)image
->bits
.bits
;
231 int s
= image
->bits
.rowstride
* 4;
233 iter
->bits
= b
+ s
* iter
->y
+ iter
->x
* PIXMAN_FORMAT_BPP (info
->format
) / 8;
237 #define N_TMP_BOXES (16)
240 pixman_region16_copy_from_region32 (pixman_region16_t
*dst
,
241 pixman_region32_t
*src
)
244 pixman_box32_t
*boxes32
;
245 pixman_box16_t
*boxes16
;
246 pixman_bool_t retval
;
248 boxes32
= pixman_region32_rectangles (src
, &n_boxes
);
250 boxes16
= pixman_malloc_ab (n_boxes
, sizeof (pixman_box16_t
));
255 for (i
= 0; i
< n_boxes
; ++i
)
257 boxes16
[i
].x1
= boxes32
[i
].x1
;
258 boxes16
[i
].y1
= boxes32
[i
].y1
;
259 boxes16
[i
].x2
= boxes32
[i
].x2
;
260 boxes16
[i
].y2
= boxes32
[i
].y2
;
263 pixman_region_fini (dst
);
264 retval
= pixman_region_init_rects (dst
, boxes16
, n_boxes
);
270 pixman_region32_copy_from_region16 (pixman_region32_t
*dst
,
271 pixman_region16_t
*src
)
274 pixman_box16_t
*boxes16
;
275 pixman_box32_t
*boxes32
;
276 pixman_box32_t tmp_boxes
[N_TMP_BOXES
];
277 pixman_bool_t retval
;
279 boxes16
= pixman_region_rectangles (src
, &n_boxes
);
281 if (n_boxes
> N_TMP_BOXES
)
282 boxes32
= pixman_malloc_ab (n_boxes
, sizeof (pixman_box32_t
));
289 for (i
= 0; i
< n_boxes
; ++i
)
291 boxes32
[i
].x1
= boxes16
[i
].x1
;
292 boxes32
[i
].y1
= boxes16
[i
].y1
;
293 boxes32
[i
].x2
= boxes16
[i
].x2
;
294 boxes32
[i
].y2
= boxes16
[i
].y2
;
297 pixman_region32_fini (dst
);
298 retval
= pixman_region32_init_rects (dst
, boxes32
, n_boxes
);
300 if (boxes32
!= tmp_boxes
)
306 /* This function is exported for the sake of the test suite and not part
309 PIXMAN_EXPORT pixman_implementation_t
*
310 _pixman_internal_only_get_implementation (void)
312 return get_implementation ();
316 _pixman_log_error (const char *function
, const char *message
)
318 static int n_messages
= 0;
325 "Set a breakpoint on '_pixman_log_error' to debug\n\n",