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.
 
 
 
 
 
 

204 lines
5.8 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_BGR24:
  47. bytes = img->w * img->h * 3 * sizeof(uint8_t);
  48. break;
  49. case PIPI_PIXELS_RGBA_F:
  50. bytes = img->w * img->h * 4 * sizeof(float);
  51. break;
  52. default:
  53. return NULL;
  54. }
  55. img->p[type].pixels = malloc(bytes);
  56. }
  57. /* Convert pixels */
  58. if(img->last_modified == PIPI_PIXELS_RGBA32
  59. && type == PIPI_PIXELS_RGBA_F)
  60. {
  61. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels;
  62. float *dest = (float *)img->p[type].pixels;
  63. init_tables();
  64. for(y = 0; y < img->h; y++)
  65. for(x = 0; x < img->w; x++)
  66. for(i = 0; i < 4; i++)
  67. dest[4 * (y * img->w + x) + i]
  68. = u8tof32(src[4 * (y * img->w + x) + i]);
  69. }
  70. else if(img->last_modified == PIPI_PIXELS_BGR24
  71. && type == PIPI_PIXELS_RGBA_F)
  72. {
  73. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_BGR24].pixels;
  74. float *dest = (float *)img->p[type].pixels;
  75. init_tables();
  76. for(y = 0; y < img->h; y++)
  77. for(x = 0; x < img->w; x++)
  78. {
  79. dest[4 * (y * img->w + x)]
  80. = u8tof32(src[3 * (y * img->w + x) + 2]);
  81. dest[4 * (y * img->w + x) + 1]
  82. = u8tof32(src[3 * (y * img->w + x) + 1]);
  83. dest[4 * (y * img->w + x) + 2]
  84. = u8tof32(src[3 * (y * img->w + x)]);
  85. dest[4 * (y * img->w + x) + 3] = 1.0;
  86. }
  87. }
  88. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  89. && type == PIPI_PIXELS_RGBA32)
  90. {
  91. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  92. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  93. init_tables();
  94. for(y = 0; y < img->h; y++)
  95. for(x = 0; x < img->w; x++)
  96. for(i = 0; i < 4; i++)
  97. {
  98. double p, e;
  99. uint8_t d;
  100. p = src[4 * (y * img->w + x) + i];
  101. if(p < 0.) d = 0.;
  102. else if(p > 1.) d = 255;
  103. else d = (int)(255.999 * pow(p, 1. / GAMMA));
  104. dest[4 * (y * img->w + x) + i] = d;
  105. e = p - u8tof32(d);
  106. if(x < img->w - 1)
  107. src[4 * (y * img->w + x + 1) + i] += e * .4375;
  108. if(y < img->h - 1)
  109. {
  110. if(x < img->w - 1)
  111. src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875;
  112. src[4 * ((y + 1) * img->w + x) + i] += e * .3125;
  113. if(x < img->w - 1)
  114. src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625;
  115. }
  116. }
  117. }
  118. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  119. && type == PIPI_PIXELS_BGR24)
  120. {
  121. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  122. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  123. init_tables();
  124. for(y = 0; y < img->h; y++)
  125. for(x = 0; x < img->w; x++)
  126. for(i = 0; i < 3; i++)
  127. {
  128. double p, e;
  129. uint8_t d;
  130. p = src[4 * (y * img->w + x) + i];
  131. if(p < 0.) d = 0.;
  132. else if(p > 1.) d = 255;
  133. else d = (int)(255.999 * pow(p, 1. / GAMMA));
  134. dest[3 * (y * img->w + x) + i] = d;
  135. e = p - u8tof32(d);
  136. if(x < img->w - 1)
  137. src[4 * (y * img->w + x + 1) + i] += e * .4375;
  138. if(y < img->h - 1)
  139. {
  140. if(x < img->w - 1)
  141. src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875;
  142. src[4 * ((y + 1) * img->w + x) + i] += e * .3125;
  143. if(x < img->w - 1)
  144. src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. memset(img->p[type].pixels, 0, bytes);
  151. }
  152. img->last_modified = type;
  153. return &img->p[type];
  154. }
  155. static void init_tables(void)
  156. {
  157. static int done = 0;
  158. int i;
  159. if(done)
  160. return;
  161. for(i = 0; i < 256; i++)
  162. u8tof32_table[i] = pow((double)i / 255., GAMMA);
  163. done = 1;
  164. }