Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2000 SuSE, Inc. |
michael@0 | 3 | * Copyright © 1999 Keith Packard |
michael@0 | 4 | * |
michael@0 | 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
michael@0 | 6 | * documentation for any purpose is hereby granted without fee, provided that |
michael@0 | 7 | * the above copyright notice appear in all copies and that both that |
michael@0 | 8 | * copyright notice and this permission notice appear in supporting |
michael@0 | 9 | * documentation, and that the name of SuSE not be used in advertising or |
michael@0 | 10 | * publicity pertaining to distribution of the software without specific, |
michael@0 | 11 | * written prior permission. SuSE makes no representations about the |
michael@0 | 12 | * suitability of this software for any purpose. It is provided "as is" |
michael@0 | 13 | * without express or implied warranty. |
michael@0 | 14 | * |
michael@0 | 15 | * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL |
michael@0 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE |
michael@0 | 17 | * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
michael@0 | 18 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
michael@0 | 19 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
michael@0 | 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
michael@0 | 21 | * |
michael@0 | 22 | * Author: Keith Packard, SuSE, Inc. |
michael@0 | 23 | */ |
michael@0 | 24 | |
michael@0 | 25 | #ifdef HAVE_CONFIG_H |
michael@0 | 26 | #include <config.h> |
michael@0 | 27 | #endif |
michael@0 | 28 | #include <stdio.h> |
michael@0 | 29 | #include <stdlib.h> |
michael@0 | 30 | #include <limits.h> |
michael@0 | 31 | |
michael@0 | 32 | #include "pixman-private.h" |
michael@0 | 33 | |
michael@0 | 34 | pixman_bool_t |
michael@0 | 35 | _pixman_multiply_overflows_size (size_t a, size_t b) |
michael@0 | 36 | { |
michael@0 | 37 | return a >= SIZE_MAX / b; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | pixman_bool_t |
michael@0 | 41 | _pixman_multiply_overflows_int (unsigned int a, unsigned int b) |
michael@0 | 42 | { |
michael@0 | 43 | return a >= INT32_MAX / b; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | pixman_bool_t |
michael@0 | 47 | _pixman_addition_overflows_int (unsigned int a, unsigned int b) |
michael@0 | 48 | { |
michael@0 | 49 | return a > INT32_MAX - b; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | void * |
michael@0 | 53 | pixman_malloc_ab (unsigned int a, |
michael@0 | 54 | unsigned int b) |
michael@0 | 55 | { |
michael@0 | 56 | if (a >= INT32_MAX / b) |
michael@0 | 57 | return NULL; |
michael@0 | 58 | |
michael@0 | 59 | return malloc (a * b); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | void * |
michael@0 | 63 | pixman_malloc_abc (unsigned int a, |
michael@0 | 64 | unsigned int b, |
michael@0 | 65 | unsigned int c) |
michael@0 | 66 | { |
michael@0 | 67 | if (a >= INT32_MAX / b) |
michael@0 | 68 | return NULL; |
michael@0 | 69 | else if (a * b >= INT32_MAX / c) |
michael@0 | 70 | return NULL; |
michael@0 | 71 | else |
michael@0 | 72 | return malloc (a * b * c); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | static force_inline uint16_t |
michael@0 | 76 | float_to_unorm (float f, int n_bits) |
michael@0 | 77 | { |
michael@0 | 78 | uint32_t u; |
michael@0 | 79 | |
michael@0 | 80 | if (f > 1.0) |
michael@0 | 81 | f = 1.0; |
michael@0 | 82 | if (f < 0.0) |
michael@0 | 83 | f = 0.0; |
michael@0 | 84 | |
michael@0 | 85 | u = f * (1 << n_bits); |
michael@0 | 86 | u -= (u >> n_bits); |
michael@0 | 87 | |
michael@0 | 88 | return u; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | static force_inline float |
michael@0 | 92 | unorm_to_float (uint16_t u, int n_bits) |
michael@0 | 93 | { |
michael@0 | 94 | uint32_t m = ((1 << n_bits) - 1); |
michael@0 | 95 | |
michael@0 | 96 | return (u & m) * (1.f / (float)m); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /* |
michael@0 | 100 | * This function expands images from a8r8g8b8 to argb_t. To preserve |
michael@0 | 101 | * precision, it needs to know from which source format the a8r8g8b8 pixels |
michael@0 | 102 | * originally came. |
michael@0 | 103 | * |
michael@0 | 104 | * For example, if the source was PIXMAN_x1r5g5b5 and the red component |
michael@0 | 105 | * contained bits 12345, then the 8-bit value is 12345123. To correctly |
michael@0 | 106 | * expand this to floating point, it should be 12345 / 31.0 and not |
michael@0 | 107 | * 12345123 / 255.0. |
michael@0 | 108 | */ |
michael@0 | 109 | void |
michael@0 | 110 | pixman_expand_to_float (argb_t *dst, |
michael@0 | 111 | const uint32_t *src, |
michael@0 | 112 | pixman_format_code_t format, |
michael@0 | 113 | int width) |
michael@0 | 114 | { |
michael@0 | 115 | static const float multipliers[16] = { |
michael@0 | 116 | 0.0f, |
michael@0 | 117 | 1.0f / ((1 << 1) - 1), |
michael@0 | 118 | 1.0f / ((1 << 2) - 1), |
michael@0 | 119 | 1.0f / ((1 << 3) - 1), |
michael@0 | 120 | 1.0f / ((1 << 4) - 1), |
michael@0 | 121 | 1.0f / ((1 << 5) - 1), |
michael@0 | 122 | 1.0f / ((1 << 6) - 1), |
michael@0 | 123 | 1.0f / ((1 << 7) - 1), |
michael@0 | 124 | 1.0f / ((1 << 8) - 1), |
michael@0 | 125 | 1.0f / ((1 << 9) - 1), |
michael@0 | 126 | 1.0f / ((1 << 10) - 1), |
michael@0 | 127 | 1.0f / ((1 << 11) - 1), |
michael@0 | 128 | 1.0f / ((1 << 12) - 1), |
michael@0 | 129 | 1.0f / ((1 << 13) - 1), |
michael@0 | 130 | 1.0f / ((1 << 14) - 1), |
michael@0 | 131 | 1.0f / ((1 << 15) - 1), |
michael@0 | 132 | }; |
michael@0 | 133 | int a_size, r_size, g_size, b_size; |
michael@0 | 134 | int a_shift, r_shift, g_shift, b_shift; |
michael@0 | 135 | float a_mul, r_mul, g_mul, b_mul; |
michael@0 | 136 | uint32_t a_mask, r_mask, g_mask, b_mask; |
michael@0 | 137 | int i; |
michael@0 | 138 | |
michael@0 | 139 | if (!PIXMAN_FORMAT_VIS (format)) |
michael@0 | 140 | format = PIXMAN_a8r8g8b8; |
michael@0 | 141 | |
michael@0 | 142 | /* |
michael@0 | 143 | * Determine the sizes of each component and the masks and shifts |
michael@0 | 144 | * required to extract them from the source pixel. |
michael@0 | 145 | */ |
michael@0 | 146 | a_size = PIXMAN_FORMAT_A (format); |
michael@0 | 147 | r_size = PIXMAN_FORMAT_R (format); |
michael@0 | 148 | g_size = PIXMAN_FORMAT_G (format); |
michael@0 | 149 | b_size = PIXMAN_FORMAT_B (format); |
michael@0 | 150 | |
michael@0 | 151 | a_shift = 32 - a_size; |
michael@0 | 152 | r_shift = 24 - r_size; |
michael@0 | 153 | g_shift = 16 - g_size; |
michael@0 | 154 | b_shift = 8 - b_size; |
michael@0 | 155 | |
michael@0 | 156 | a_mask = ((1 << a_size) - 1); |
michael@0 | 157 | r_mask = ((1 << r_size) - 1); |
michael@0 | 158 | g_mask = ((1 << g_size) - 1); |
michael@0 | 159 | b_mask = ((1 << b_size) - 1); |
michael@0 | 160 | |
michael@0 | 161 | a_mul = multipliers[a_size]; |
michael@0 | 162 | r_mul = multipliers[r_size]; |
michael@0 | 163 | g_mul = multipliers[g_size]; |
michael@0 | 164 | b_mul = multipliers[b_size]; |
michael@0 | 165 | |
michael@0 | 166 | /* Start at the end so that we can do the expansion in place |
michael@0 | 167 | * when src == dst |
michael@0 | 168 | */ |
michael@0 | 169 | for (i = width - 1; i >= 0; i--) |
michael@0 | 170 | { |
michael@0 | 171 | const uint32_t pixel = src[i]; |
michael@0 | 172 | |
michael@0 | 173 | dst[i].a = a_mask? ((pixel >> a_shift) & a_mask) * a_mul : 1.0f; |
michael@0 | 174 | dst[i].r = ((pixel >> r_shift) & r_mask) * r_mul; |
michael@0 | 175 | dst[i].g = ((pixel >> g_shift) & g_mask) * g_mul; |
michael@0 | 176 | dst[i].b = ((pixel >> b_shift) & b_mask) * b_mul; |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | uint16_t |
michael@0 | 181 | pixman_float_to_unorm (float f, int n_bits) |
michael@0 | 182 | { |
michael@0 | 183 | return float_to_unorm (f, n_bits); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | float |
michael@0 | 187 | pixman_unorm_to_float (uint16_t u, int n_bits) |
michael@0 | 188 | { |
michael@0 | 189 | return unorm_to_float (u, n_bits); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | void |
michael@0 | 193 | pixman_contract_from_float (uint32_t *dst, |
michael@0 | 194 | const argb_t *src, |
michael@0 | 195 | int width) |
michael@0 | 196 | { |
michael@0 | 197 | int i; |
michael@0 | 198 | |
michael@0 | 199 | for (i = 0; i < width; ++i) |
michael@0 | 200 | { |
michael@0 | 201 | uint8_t a, r, g, b; |
michael@0 | 202 | |
michael@0 | 203 | a = float_to_unorm (src[i].a, 8); |
michael@0 | 204 | r = float_to_unorm (src[i].r, 8); |
michael@0 | 205 | g = float_to_unorm (src[i].g, 8); |
michael@0 | 206 | b = float_to_unorm (src[i].b, 8); |
michael@0 | 207 | |
michael@0 | 208 | dst[i] = (a << 24) | (r << 16) | (g << 8) | (b << 0); |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | uint32_t * |
michael@0 | 213 | _pixman_iter_get_scanline_noop (pixman_iter_t *iter, const uint32_t *mask) |
michael@0 | 214 | { |
michael@0 | 215 | return iter->buffer; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | #define N_TMP_BOXES (16) |
michael@0 | 219 | |
michael@0 | 220 | pixman_bool_t |
michael@0 | 221 | pixman_region16_copy_from_region32 (pixman_region16_t *dst, |
michael@0 | 222 | pixman_region32_t *src) |
michael@0 | 223 | { |
michael@0 | 224 | int n_boxes, i; |
michael@0 | 225 | pixman_box32_t *boxes32; |
michael@0 | 226 | pixman_box16_t *boxes16; |
michael@0 | 227 | pixman_bool_t retval; |
michael@0 | 228 | |
michael@0 | 229 | boxes32 = pixman_region32_rectangles (src, &n_boxes); |
michael@0 | 230 | |
michael@0 | 231 | boxes16 = pixman_malloc_ab (n_boxes, sizeof (pixman_box16_t)); |
michael@0 | 232 | |
michael@0 | 233 | if (!boxes16) |
michael@0 | 234 | return FALSE; |
michael@0 | 235 | |
michael@0 | 236 | for (i = 0; i < n_boxes; ++i) |
michael@0 | 237 | { |
michael@0 | 238 | boxes16[i].x1 = boxes32[i].x1; |
michael@0 | 239 | boxes16[i].y1 = boxes32[i].y1; |
michael@0 | 240 | boxes16[i].x2 = boxes32[i].x2; |
michael@0 | 241 | boxes16[i].y2 = boxes32[i].y2; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | pixman_region_fini (dst); |
michael@0 | 245 | retval = pixman_region_init_rects (dst, boxes16, n_boxes); |
michael@0 | 246 | free (boxes16); |
michael@0 | 247 | return retval; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | pixman_bool_t |
michael@0 | 251 | pixman_region32_copy_from_region16 (pixman_region32_t *dst, |
michael@0 | 252 | pixman_region16_t *src) |
michael@0 | 253 | { |
michael@0 | 254 | int n_boxes, i; |
michael@0 | 255 | pixman_box16_t *boxes16; |
michael@0 | 256 | pixman_box32_t *boxes32; |
michael@0 | 257 | pixman_box32_t tmp_boxes[N_TMP_BOXES]; |
michael@0 | 258 | pixman_bool_t retval; |
michael@0 | 259 | |
michael@0 | 260 | boxes16 = pixman_region_rectangles (src, &n_boxes); |
michael@0 | 261 | |
michael@0 | 262 | if (n_boxes > N_TMP_BOXES) |
michael@0 | 263 | boxes32 = pixman_malloc_ab (n_boxes, sizeof (pixman_box32_t)); |
michael@0 | 264 | else |
michael@0 | 265 | boxes32 = tmp_boxes; |
michael@0 | 266 | |
michael@0 | 267 | if (!boxes32) |
michael@0 | 268 | return FALSE; |
michael@0 | 269 | |
michael@0 | 270 | for (i = 0; i < n_boxes; ++i) |
michael@0 | 271 | { |
michael@0 | 272 | boxes32[i].x1 = boxes16[i].x1; |
michael@0 | 273 | boxes32[i].y1 = boxes16[i].y1; |
michael@0 | 274 | boxes32[i].x2 = boxes16[i].x2; |
michael@0 | 275 | boxes32[i].y2 = boxes16[i].y2; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | pixman_region32_fini (dst); |
michael@0 | 279 | retval = pixman_region32_init_rects (dst, boxes32, n_boxes); |
michael@0 | 280 | |
michael@0 | 281 | if (boxes32 != tmp_boxes) |
michael@0 | 282 | free (boxes32); |
michael@0 | 283 | |
michael@0 | 284 | return retval; |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | /* This function is exported for the sake of the test suite and not part |
michael@0 | 288 | * of the ABI. |
michael@0 | 289 | */ |
michael@0 | 290 | PIXMAN_EXPORT pixman_implementation_t * |
michael@0 | 291 | _pixman_internal_only_get_implementation (void) |
michael@0 | 292 | { |
michael@0 | 293 | return get_implementation (); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | #ifdef DEBUG |
michael@0 | 297 | |
michael@0 | 298 | void |
michael@0 | 299 | _pixman_log_error (const char *function, const char *message) |
michael@0 | 300 | { |
michael@0 | 301 | static int n_messages = 0; |
michael@0 | 302 | |
michael@0 | 303 | if (n_messages < 10) |
michael@0 | 304 | { |
michael@0 | 305 | fprintf (stderr, |
michael@0 | 306 | "*** BUG ***\n" |
michael@0 | 307 | "In %s: %s\n" |
michael@0 | 308 | "Set a breakpoint on '_pixman_log_error' to debug\n\n", |
michael@0 | 309 | function, message); |
michael@0 | 310 | |
michael@0 | 311 | n_messages++; |
michael@0 | 312 | } |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | #endif |