1/* 2 * Copyright © 2000 SuSE, Inc. 3 * Copyright © 1999 Keith Packard 4 * 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. 14 * 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. 21 * 22 * Author: Keith Packard, SuSE, Inc. 23 */ 24 25#ifdef HAVE_CONFIG_H 26#include <pixman-config.h> 27#endif 28#include <stdio.h> 29#include <stdlib.h> 30 31#include "pixman-private.h" 32 33pixman_bool_t 34_pixman_multiply_overflows_size (size_t a, size_t b) 35{ 36 return a >= SIZE_MAX / b; 37} 38 39pixman_bool_t 40_pixman_multiply_overflows_int (unsigned int a, unsigned int b) 41{ 42 return a >= INT32_MAX / b; 43} 44 45pixman_bool_t 46_pixman_addition_overflows_int (unsigned int a, unsigned int b) 47{ 48 return a > INT32_MAX - b; 49} 50 51void * 52pixman_malloc_ab_plus_c (unsigned int a, unsigned int b, unsigned int c) 53{ 54 if (!b || a >= INT32_MAX / b || (a * b) > INT32_MAX - c) 55 return NULL; 56 57 return malloc (a * b + c); 58} 59 60void * 61pixman_malloc_ab (unsigned int a, 62 unsigned int b) 63{ 64 if (a >= INT32_MAX / b) 65 return NULL; 66 67 return malloc (a * b); 68} 69 70void * 71pixman_malloc_abc (unsigned int a, 72 unsigned int b, 73 unsigned int c) 74{ 75 if (a >= INT32_MAX / b) 76 return NULL; 77 else if (a * b >= INT32_MAX / c) 78 return NULL; 79 else 80 return malloc (a * b * c); 81} 82 83static force_inline uint16_t 84float_to_unorm (float f, int n_bits) 85{ 86 uint32_t u; 87 88 if (f > 1.0) 89 f = 1.0; 90 if (f < 0.0) 91 f = 0.0; 92 93 u = f * (1 << n_bits); 94 u -= (u >> n_bits); 95 96 return u; 97} 98 99static force_inline float 100unorm_to_float (uint16_t u, int n_bits) 101{ 102 uint32_t m = ((1 << n_bits) - 1); 103 104 return (u & m) * (1.f / (float)m); 105} 106 107/* 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 110 * originally came. 111 * 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 115 * 12345123 / 255.0. 116 */ 117void 118pixman_expand_to_float (argb_t *dst, 119 const uint32_t *src, 120 pixman_format_code_t format, 121 int width) 122{ 123 static const float multipliers[16] = { 124 0.0f, 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), 140 }; 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; 145 int i; 146 147 if (!PIXMAN_FORMAT_VIS (format)) 148 format = PIXMAN_a8r8g8b8; 149 150 /* 151 * Determine the sizes of each component and the masks and shifts 152 * required to extract them from the source pixel. 153 */ 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); 158 159 a_shift = 32 - a_size; 160 r_shift = 24 - r_size; 161 g_shift = 16 - g_size; 162 b_shift = 8 - b_size; 163 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); 168 169 a_mul = multipliers[a_size]; 170 r_mul = multipliers[r_size]; 171 g_mul = multipliers[g_size]; 172 b_mul = multipliers[b_size]; 173 174 /* Start at the end so that we can do the expansion in place 175 * when src == dst 176 */ 177 for (i = width - 1; i >= 0; i--) 178 { 179 const uint32_t pixel = src[i]; 180 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; 185 } 186} 187 188uint16_t 189pixman_float_to_unorm (float f, int n_bits) 190{ 191 return float_to_unorm (f, n_bits); 192} 193 194float 195pixman_unorm_to_float (uint16_t u, int n_bits) 196{ 197 return unorm_to_float (u, n_bits); 198} 199 200void 201pixman_contract_from_float (uint32_t *dst, 202 const argb_t *src, 203 int width) 204{ 205 int i; 206 207 for (i = 0; i < width; ++i) 208 { 209 uint32_t a, r, g, b; 210 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); 215 216 dst[i] = (a << 24) | (r << 16) | (g << 8) | (b << 0); 217 } 218} 219 220uint32_t * 221_pixman_iter_get_scanline_noop (pixman_iter_t *iter, const uint32_t *mask) 222{ 223 return iter->buffer; 224} 225 226void 227_pixman_iter_init_bits_stride (pixman_iter_t *iter, const pixman_iter_info_t *info) 228{ 229 pixman_image_t *image = iter->image; 230 uint8_t *b = (uint8_t *)image->bits.bits; 231 int s = image->bits.rowstride * 4; 232 233 iter->bits = b + s * iter->y + iter->x * PIXMAN_FORMAT_BPP (info->format) / 8; 234 iter->stride = s; 235} 236 237#define N_TMP_BOXES (16) 238 239pixman_bool_t 240pixman_region16_copy_from_region32 (pixman_region16_t *dst, 241 const pixman_region32_t *src) 242{ 243 int n_boxes, i; 244 pixman_box32_t *boxes32; 245 pixman_box16_t *boxes16; 246 pixman_bool_t retval; 247 248 boxes32 = pixman_region32_rectangles (src, &n_boxes); 249 250 boxes16 = pixman_malloc_ab (n_boxes, sizeof (pixman_box16_t)); 251 252 if (!boxes16) 253 return FALSE; 254 255 for (i = 0; i < n_boxes; ++i) 256 { 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; 261 } 262 263 pixman_region_fini (dst); 264 retval = pixman_region_init_rects (dst, boxes16, n_boxes); 265 free (boxes16); 266 return retval; 267} 268 269pixman_bool_t 270pixman_region32_copy_from_region16 (pixman_region32_t *dst, 271 const pixman_region16_t *src) 272{ 273 int n_boxes, i; 274 pixman_box16_t *boxes16; 275 pixman_box32_t *boxes32; 276 pixman_box32_t tmp_boxes[N_TMP_BOXES]; 277 pixman_bool_t retval; 278 279 boxes16 = pixman_region_rectangles (src, &n_boxes); 280 281 if (n_boxes > N_TMP_BOXES) 282 boxes32 = pixman_malloc_ab (n_boxes, sizeof (pixman_box32_t)); 283 else 284 boxes32 = tmp_boxes; 285 286 if (!boxes32) 287 return FALSE; 288 289 for (i = 0; i < n_boxes; ++i) 290 { 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; 295 } 296 297 pixman_region32_fini (dst); 298 retval = pixman_region32_init_rects (dst, boxes32, n_boxes); 299 300 if (boxes32 != tmp_boxes) 301 free (boxes32); 302 303 return retval; 304} 305 306pixman_bool_t 307pixman_region32_copy_from_region64f (pixman_region32_t *dst, 308 const pixman_region64f_t *src) 309{ 310 int n_boxes, i; 311 pixman_box64f_t *boxes64f; 312 pixman_box32_t *boxes32; 313 pixman_box32_t tmp_boxes[N_TMP_BOXES]; 314 pixman_bool_t retval; 315 316 boxes64f = pixman_region64f_rectangles (src, &n_boxes); 317 318 if (n_boxes > N_TMP_BOXES) 319 boxes32 = pixman_malloc_ab (n_boxes, sizeof (pixman_box32_t)); 320 else 321 boxes32 = tmp_boxes; 322 323 if (!boxes32) 324 return FALSE; 325 326 for (i = 0; i < n_boxes; ++i) 327 { 328 boxes32[i].x1 = boxes64f[i].x1; 329 boxes32[i].y1 = boxes64f[i].y1; 330 boxes32[i].x2 = boxes64f[i].x2; 331 boxes32[i].y2 = boxes64f[i].y2; 332 } 333 334 pixman_region32_fini (dst); 335 retval = pixman_region32_init_rects (dst, boxes32, n_boxes); 336 337 if (boxes32 != tmp_boxes) 338 free (boxes32); 339 340 return retval; 341} 342 343/* This function is exported for the sake of the test suite and not part 344 * of the ABI. 345 */ 346PIXMAN_EXPORT pixman_implementation_t * 347_pixman_internal_only_get_implementation (void) 348{ 349 return get_implementation (); 350} 351 352void 353_pixman_log_error (const char *function, const char *message) 354{ 355 static int n_messages = 0; 356 357 if (n_messages < 10) 358 { 359 fprintf (stderr, 360 "*** BUG ***\n" 361 "In %s: %s\n" 362 "Set a breakpoint on '_pixman_log_error' to debug\n\n", 363 function, message); 364 365 n_messages++; 366 } 367} 368