An LGPL/GPL-licensed artwork library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

art_alphagamma.c 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Libart_LGPL - library of basic graphic primitives
  2. * Copyright (C) 1998 Raph Levien
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. /* Some functions to build alphagamma tables */
  20. #include "config.h"
  21. #include "art_alphagamma.h"
  22. #include <math.h>
  23. /**
  24. * art_alphagamma_new: Create a new #ArtAlphaGamma.
  25. * @gamma: Gamma value.
  26. *
  27. * Create a new #ArtAlphaGamma for a specific value of @gamma. When
  28. * correctly implemented (which is generally not the case in libart),
  29. * alpha compositing with an alphagamma parameter is equivalent to
  30. * applying the gamma transformation to source images, doing the alpha
  31. * compositing (in linear intensity space), then applying the inverse
  32. * gamma transformation, bringing it back to a gamma-adjusted
  33. * intensity space.
  34. *
  35. * Return value: The newly created #ArtAlphaGamma.
  36. **/
  37. ArtAlphaGamma *
  38. art_alphagamma_new (double gamma)
  39. {
  40. int tablesize;
  41. ArtAlphaGamma *alphagamma;
  42. int i;
  43. int *table;
  44. art_u8 *invtable;
  45. double s, r_gamma;
  46. tablesize = ceil (gamma * 8);
  47. if (tablesize < 10)
  48. tablesize = 10;
  49. alphagamma = (ArtAlphaGamma *)art_alloc (sizeof(ArtAlphaGamma) +
  50. ((1 << tablesize) - 1) *
  51. sizeof(art_u8));
  52. alphagamma->gamma = gamma;
  53. alphagamma->invtable_size = tablesize;
  54. table = alphagamma->table;
  55. for (i = 0; i < 256; i++)
  56. table[i] = (int)floor (((1 << tablesize) - 1) *
  57. pow (i * (1.0 / 255), gamma) + 0.5);
  58. invtable = alphagamma->invtable;
  59. s = 1.0 / ((1 << tablesize) - 1);
  60. r_gamma = 1.0 / gamma;
  61. for (i = 0; i < 1 << tablesize; i++)
  62. invtable[i] = (int)floor (255 * pow (i * s, r_gamma) + 0.5);
  63. return alphagamma;
  64. }
  65. /**
  66. * art_alphagamma_free: Free an #ArtAlphaGamma.
  67. * @alphagamma: An #ArtAlphaGamma.
  68. *
  69. * Frees the #ArtAlphaGamma.
  70. **/
  71. void
  72. art_alphagamma_free (ArtAlphaGamma *alphagamma)
  73. {
  74. art_free (alphagamma);
  75. }