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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
|
/* babl - dynamically extendable universal pixel conversion library.
* Copyright (C) 2012, Øyvind Kolås
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdint.h>
#include <stdlib.h>
#include "babl.h"
#include "babl-cpuaccel.h"
#include "extensions/util.h"
#include "base/util.h"
#define LSHIFT 4
typedef float (* BablLookupFunction) (float value,
void *data);
#define babl_LOOKUP_MAX_ENTRIES (819200)
typedef struct BablLookup
{
BablLookupFunction function;
void *data;
int shift;
uint32_t positive_min, positive_max, negative_min, negative_max;
uint32_t bitmask[babl_LOOKUP_MAX_ENTRIES/32];
int entries;
float table[];
} BablLookup;
static BablLookup *babl_lookup_new (BablLookupFunction function,
void * data,
float start,
float end,
float precision);
#if 0
static void babl_lookup_free (BablLookup *lookup);
#endif
#include <string.h>
static inline float
babl_lookup (BablLookup *lookup,
float number)
{
union { float f; uint32_t i; } u;
union { float f; uint32_t i; } ub;
union { float f; uint32_t i; } ua;
uint32_t i;
float dx = 0.0;
u.f = number;
i = (u.i << LSHIFT ) >> lookup->shift;
if (i > lookup->positive_min && i < lookup->positive_max)
{
ua.i = ((i) << lookup->shift) >> LSHIFT;
ub.i = ((i+ 1) << lookup->shift) >> LSHIFT;
i = i - lookup->positive_min;
}
else if (i > lookup->negative_min && i < lookup->negative_max)
{
ua.i = ((i) << lookup->shift) >> LSHIFT;
ub.i = ((i+ 1) << lookup->shift) >> LSHIFT;
i = i - lookup->negative_min + (lookup->positive_max - lookup->positive_min);
}
else
{
return lookup->function (number, lookup->data);
}
{
uint32_t bm =u.i & 0b11110000000000000000000000000000;
ua.i |= bm;
ub.i |= bm;
}
dx = (u.f-ua.f) / (ub.f - ua.f);
{
if (!(lookup->bitmask[i/32] & (1UL<<(i & 31))))
{
lookup->table[i]= lookup->function (ua.f, lookup->data);
lookup->bitmask[i/32] |= (1UL<<(i & 31));
}
i++;
if (i< lookup->entries-2)
{
if (!(lookup->bitmask[i/32] & (1UL<<(i & 31))))
{
lookup->table[i]= lookup->function (ub.f, lookup->data);
lookup->bitmask[i/32] |= (1UL<<(i & 31));
}
return lookup->table[i-1] * (1.0f-dx) +
lookup->table[i] * (dx);
}
else
{
return lookup->table[i-1];
}
}
}
static BablLookup *
babl_lookup_new (BablLookupFunction function,
void * data,
float start,
float end,
float precision)
{
BablLookup *lookup;
union
{
float f;
uint32_t i;
} u;
int positive_min, positive_max, negative_min, negative_max;
int shift;
/* normalize input parameters */
if (start > end)
{ /* swap */
u.f = start;
start = end;
end = u.f;
}
if (precision <= 0.000005f) shift = 0; /* checked for later */
else if (precision <= 0.000010f) shift = 8;
else if (precision <= 0.000020f) shift = 9;
else if (precision <= 0.000040f) shift = 10;
else if (precision <= 0.000081f) shift = 11;
else if (precision <= 0.000161f) shift = 12;
else if (precision <= 0.000200f) shift = 13;
else if (precision <= 0.000324f) shift = 14;
else if (precision <= 0.000649f) shift = 15;
else shift = 16; /* a bit better than 8bit sRGB quality */
/* Adjust slightly away from 0.0, saving many entries close to 0, this
* causes lookups very close to zero to be passed directly to the
* function instead.
*/
if (start == 0.0f)
start = precision;
if (end == 0.0f)
end = -precision;
/* Compute start and */
if (start < 0.0f || end < 0.0f)
{
if (end < 0.0f)
{
u.f = start;
positive_max = (u.i << LSHIFT) >> shift;
u.f = end;
positive_min = (u.i << LSHIFT) >> shift;
negative_min = positive_max;
negative_max = positive_max;
}
else
{
u.f = 0 - precision;
positive_min = (u.i << LSHIFT) >> shift;
u.f = start;
positive_max = (u.i << LSHIFT) >> shift;
u.f = 0 + precision;
negative_min = (u.i << LSHIFT) >> shift;
u.f = end;
negative_max = (u.i << LSHIFT) >> shift;
}
}
else
{
u.f = start;
positive_min = (u.i << LSHIFT) >> shift;
u.f = end;
positive_max = (u.i << LSHIFT) >> shift;
negative_min = positive_max;
negative_max = positive_max;
}
if (shift == 0) /* short circuit, do not use ranges */
{
positive_min = positive_max = negative_min = negative_max = 0;
}
if ((positive_max-positive_min) + (negative_max-negative_min) > babl_LOOKUP_MAX_ENTRIES)
{
/* Reduce the size of the cache tables to fit within the bittable
* budget (the maximum allocation is around 2.18mb of memory
*/
int diff = (positive_max-positive_min) + (negative_max-negative_min) - babl_LOOKUP_MAX_ENTRIES;
if (negative_max - negative_min > 0)
{
if (negative_max - negative_min >= diff)
{
negative_max -= diff;
diff = 0;
}
else
{
diff -= negative_max - negative_min;
negative_max = negative_min;
}
}
if (diff)
positive_max-=diff;
}
lookup = calloc (sizeof (BablLookup) + sizeof (float) *
((positive_max-positive_min)+
(negative_max-negative_min)), 1);
lookup->positive_min = positive_min;
lookup->positive_max = positive_max;
lookup->negative_min = negative_min;
lookup->negative_max = negative_max;
lookup->shift = shift;
lookup->function = function;
lookup->data = data;
lookup->entries = (positive_max-positive_min)+
(negative_max-negative_min);
return lookup;
}
static BablLookup *fast_pow = NULL;
static inline float core_lookup (float val, void *userdata)
{
return babl_linear_to_gamma_2_2f (val);
}
static float
linear_to_gamma_2_2_lut (float val)
{
return babl_lookup (fast_pow, val);
}
static BablLookup *fast_rpow = NULL;
static inline float core_rlookup (float val, void *userdata)
{
return babl_gamma_2_2_to_linearf (val);
}
static float
gamma_2_2_to_linear_lut (float val)
{
return babl_lookup (fast_rpow, val);
}
#if 0
static void
babl_lookup_free (BablLookup *lookup)
{
free (lookup);
}
#endif
static void
conv_rgbaF_linear_rgbAF_gamma (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
float *fdst = (float *) dst;
int n = samples;
while (n--)
{
float red = *fsrc++;
float green = *fsrc++;
float blue = *fsrc++;
float alpha = *fsrc++;
if (alpha == 1.0f)
{
*fdst++ = linear_to_gamma_2_2_lut (red);
*fdst++ = linear_to_gamma_2_2_lut (green);
*fdst++ = linear_to_gamma_2_2_lut (blue);
*fdst++ = alpha;
}
else
{
float used_alpha = babl_epsilon_for_zero_float (alpha);
*fdst++ = linear_to_gamma_2_2_lut (red) * used_alpha;
*fdst++ = linear_to_gamma_2_2_lut (green) * used_alpha;
*fdst++ = linear_to_gamma_2_2_lut (blue) * used_alpha;
*fdst++ = alpha;
}
}
}
static void
conv_rgbaF_linear_rgba8_gamma (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
uint8_t *cdst = (uint8_t *) dst;
int n = samples;
while (n--)
{
float red = *fsrc++;
float green = *fsrc++;
float blue = *fsrc++;
float alpha = *fsrc++;
if (alpha <= 0) /* XXX: we need to drop alpha!! ? */
{
*cdst++ = 0;
*cdst++ = 0;
*cdst++ = 0;
*cdst++ = 0;
}
else
{
int val = linear_to_gamma_2_2_lut (red) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (green) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (blue) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = alpha * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
}
}
}
static void
conv_rgbaF_linear_rgbA8_gamma (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
uint8_t *cdst = (uint8_t *) dst;
int n = samples;
while (n--)
{
float red = *fsrc++;
float green = *fsrc++;
float blue = *fsrc++;
float alpha = *fsrc++;
if (alpha >= 1.0f)
{
int val = linear_to_gamma_2_2_lut (red) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (green) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (blue) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = 0xff;
}
else
{
float balpha = alpha * 0xff;
int val = linear_to_gamma_2_2_lut (red) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (green) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (blue) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = balpha + 0.5f;
}
}
}
static void
conv_yaF_linear_rgbA8_gamma (const Babl *conversion,unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
uint8_t *cdst = (uint8_t *) dst;
int n = samples;
while (n--)
{
float gray = *fsrc++;
float alpha = *fsrc++;
if (alpha >= 1.0f)
{
int val = linear_to_gamma_2_2_lut (gray) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = 0xff;
}
else if (alpha <= 0.0f)
{
*((uint32_t*)(cdst))=0;
cdst+=4;
}
else
{
float balpha = alpha * 0xff;
int val = linear_to_gamma_2_2_lut (gray) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = balpha + 0.5f;
}
}
}
static void
conv_rgbaF_linear_rgbA8_gamma_cairo (const Babl *conversion,unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
unsigned char *cdst = (unsigned char *) dst;
int n = samples;
while (n--)
{
float red = *fsrc++;
float green = *fsrc++;
float blue = *fsrc++;
float alpha = *fsrc++;
if (alpha >= 1.0f)
{
int val = linear_to_gamma_2_2_lut (blue) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (green) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (red) * 0xff + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = 0xff;
}
else
{
float balpha = alpha * 0xff;
int val = linear_to_gamma_2_2_lut (blue) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (green) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
val = linear_to_gamma_2_2_lut (red) * balpha + 0.5f;
*cdst++ = val >= 0xff ? 0xff : val <= 0 ? 0 : val;
*cdst++ = balpha + 0.5f;
}
}
}
static void
conv_rgbAF_linear_rgbAF_gamma (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
float *fdst = (float *) dst;
int n = samples;
while (n--)
{
float red = *fsrc++;
float green = *fsrc++;
float blue = *fsrc++;
float alpha = *fsrc++;
if (alpha == 1.0f)
{
*fdst++ = linear_to_gamma_2_2_lut (red);
*fdst++ = linear_to_gamma_2_2_lut (green);
*fdst++ = linear_to_gamma_2_2_lut (blue);
*fdst++ = *fsrc++;
}
else
{
float alpha_recip = 1.0f / alpha;
*fdst++ = linear_to_gamma_2_2_lut (red * alpha_recip) * alpha;
*fdst++ = linear_to_gamma_2_2_lut (green * alpha_recip) * alpha;
*fdst++ = linear_to_gamma_2_2_lut (blue * alpha_recip) * alpha;
*fdst++ = alpha;
}
}
}
static void
conv_rgbaF_linear_rgbaF_gamma (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
float *fdst = (float *) dst;
int n = samples;
while (n--)
{
*fdst++ = linear_to_gamma_2_2_lut (*fsrc++);
*fdst++ = linear_to_gamma_2_2_lut (*fsrc++);
*fdst++ = linear_to_gamma_2_2_lut (*fsrc++);
*fdst++ = *fsrc++;
}
}
static void
conv_rgbF_linear_rgbF_gamma (const Babl *conversion,unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
float *fdst = (float *) dst;
int n = samples;
while (n--)
{
*fdst++ = linear_to_gamma_2_2_lut (*fsrc++);
*fdst++ = linear_to_gamma_2_2_lut (*fsrc++);
*fdst++ = linear_to_gamma_2_2_lut (*fsrc++);
}
}
static void
conv_rgbaF_gamma_rgbaF_linear (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
float *fdst = (float *) dst;
int n = samples;
while (n--)
{
*fdst++ = gamma_2_2_to_linear_lut (*fsrc++);
*fdst++ = gamma_2_2_to_linear_lut (*fsrc++);
*fdst++ = gamma_2_2_to_linear_lut (*fsrc++);
*fdst++ = *fsrc++;
}
}
static void
conv_rgbF_gamma_rgbF_linear (const Babl *conversion,
unsigned char *src,
unsigned char *dst,
long samples)
{
float *fsrc = (float *) src;
float *fdst = (float *) dst;
int n = samples;
while (n--)
{
*fdst++ = gamma_2_2_to_linear_lut (*fsrc++);
*fdst++ = gamma_2_2_to_linear_lut (*fsrc++);
*fdst++ = gamma_2_2_to_linear_lut (*fsrc++);
}
}
#define o(src, dst) \
babl_conversion_new (src, dst, "linear", conv_ ## src ## _ ## dst, NULL)
#include "babl-verify-cpu.inc"
int init (void);
int
init (void)
{
BABL_VERIFY_CPU();
{
const Babl *yaF_linear = babl_format_new (
babl_model ("YA"),
babl_type ("float"),
babl_component ("Y"),
babl_component ("A"),
NULL);
const Babl *rgbaF_linear = babl_format_new (
babl_model ("RGBA"),
babl_type ("float"),
babl_component ("R"),
babl_component ("G"),
babl_component ("B"),
babl_component ("A"),
NULL);
const Babl *rgbAF_linear = babl_format_new (
babl_model ("RaGaBaA"),
babl_type ("float"),
babl_component ("Ra"),
babl_component ("Ga"),
babl_component ("Ba"),
babl_component ("A"),
NULL);
const Babl *rgbaF_gamma = babl_format_new (
babl_model ("R'G'B'A"),
babl_type ("float"),
babl_component ("R'"),
babl_component ("G'"),
babl_component ("B'"),
babl_component ("A"),
NULL);
const Babl *rgbAF_gamma = babl_format_new (
babl_model ("R'aG'aB'aA"),
babl_type ("float"),
babl_component ("R'a"),
babl_component ("G'a"),
babl_component ("B'a"),
babl_component ("A"),
NULL);
const Babl *rgbA8_gamma = babl_format_new (
babl_model ("R'aG'aB'aA"),
babl_type ("u8"),
babl_component ("R'a"),
babl_component ("G'a"),
babl_component ("B'a"),
babl_component ("A"),
NULL);
const Babl *rgba8_gamma = babl_format_new (
babl_model ("R'G'B'A"),
babl_type ("u8"),
babl_component ("R'"),
babl_component ("G'"),
babl_component ("B'"),
babl_component ("A"),
NULL);
const Babl *rgbF_linear = babl_format_new (
babl_model ("RGB"),
babl_type ("float"),
babl_component ("R"),
babl_component ("G"),
babl_component ("B"),
NULL);
const Babl *rgbF_gamma = babl_format_new (
babl_model ("R'G'B'"),
babl_type ("float"),
babl_component ("R'"),
babl_component ("G'"),
babl_component ("B'"),
NULL);
return 0; // XXX: the fast paths registered here doesn't correctly
// clamp negative values - disabling for now
{
float f;
float a;
/* tweaking the precision - does impact speed.. */
fast_pow = babl_lookup_new (core_lookup, NULL, 0.0f, 1.0f, 0.000199f);
fast_rpow = babl_lookup_new (core_rlookup, NULL, 0.0f, 1.0f, 0.000250f);
for (f = 0.0; f < 1.0f; f+= 0.0000001f)
{
a = linear_to_gamma_2_2_lut (f);
a = gamma_2_2_to_linear_lut (f);
}
if (a < -10)
f = 2;
}
{
const Babl *f32 = babl_format_new (
"name", "cairo-ARGB32",
babl_model ("R'aG'aB'aA"),
babl_type ("u8"),
babl_component ("B'a"),
babl_component ("G'a"),
babl_component ("R'a"),
babl_component ("A"),
NULL
);
babl_conversion_new (rgbaF_linear, f32, "linear", conv_rgbaF_linear_rgbA8_gamma_cairo, NULL);
}
o (rgbaF_linear, rgbA8_gamma);
o (rgbAF_linear, rgbAF_gamma);
o (rgbaF_linear, rgbAF_gamma);
o (rgbaF_linear, rgbaF_gamma);
o (rgbaF_linear, rgba8_gamma);
o (rgbaF_gamma, rgbaF_linear);
o (rgbF_linear, rgbF_gamma);
o (rgbF_gamma, rgbF_linear);
o (yaF_linear, rgbA8_gamma);
}
return 0;
}
void destroy (void);
void
destroy (void)
{
free (fast_rpow);
free (fast_pow);
}
|