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.
 
 
 
 
 
 

145 lines
3.7 KiB

  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * pixels.c: pixel-level image manipulation
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. #define GAMMA 2.2
  26. static void init_tables(void);
  27. static float u8tof32_table[256];
  28. static inline float u8tof32(uint8_t p) { return u8tof32_table[(int)p]; }
  29. /* Return a direct pointer to an image's pixels. */
  30. pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type)
  31. {
  32. size_t bytes = 0;
  33. int x, y, i;
  34. if(type < 0 || type >= PIPI_PIXELS_MAX)
  35. return NULL;
  36. if(img->last_modified == type)
  37. return &img->p[type];
  38. /* Allocate pixels if necessary */
  39. if(!img->p[type].pixels)
  40. {
  41. switch(type)
  42. {
  43. case PIPI_PIXELS_RGBA32:
  44. bytes = img->w * img->h * 4 * sizeof(uint8_t);
  45. break;
  46. case PIPI_PIXELS_RGBA_F:
  47. bytes = img->w * img->h * 4 * sizeof(float);
  48. break;
  49. default:
  50. return NULL;
  51. }
  52. img->p[type].pixels = malloc(bytes);
  53. }
  54. /* Convert pixels */
  55. if(img->last_modified == PIPI_PIXELS_RGBA32
  56. && type == PIPI_PIXELS_RGBA_F)
  57. {
  58. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels;
  59. float *dest = (float *)img->p[type].pixels;
  60. init_tables();
  61. for(y = 0; y < img->h; y++)
  62. for(x = 0; x < img->w; x++)
  63. for(i = 0; i < 4; i++)
  64. dest[4 * (y * img->w + x) + i]
  65. = u8tof32(src[4 * (y * img->w + x) + i]);
  66. }
  67. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  68. && type == PIPI_PIXELS_RGBA32)
  69. {
  70. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  71. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  72. init_tables();
  73. for(y = 0; y < img->h; y++)
  74. for(x = 0; x < img->w; x++)
  75. for(i = 0; i < 4; i++)
  76. {
  77. double p, e;
  78. uint8_t d;
  79. p = src[4 * (y * img->w + x) + i];
  80. if(p < 0.) d = 0.;
  81. else if(p > 1.) d = 255;
  82. else d = (int)(255.999 * pow(p, 1. / GAMMA));
  83. dest[4 * (y * img->w + x) + i] = d;
  84. e = p - u8tof32(d);
  85. if(x < img->w - 1)
  86. src[4 * (y * img->w + x + 1) + i] += e * .4375;
  87. if(y < img->h - 1)
  88. {
  89. if(x < img->w - 1)
  90. src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875;
  91. src[4 * ((y + 1) * img->w + x) + i] += e * .3125;
  92. if(x < img->w - 1)
  93. src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625;
  94. }
  95. }
  96. }
  97. else
  98. {
  99. memset(img->p[type].pixels, 0, bytes);
  100. }
  101. img->last_modified = type;
  102. return &img->p[type];
  103. }
  104. static void init_tables(void)
  105. {
  106. static int done = 0;
  107. int i;
  108. if(done)
  109. return;
  110. for(i = 0; i < 256; i++)
  111. u8tof32_table[i] = pow((double)i / 255., GAMMA);
  112. done = 1;
  113. }