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.
 
 
 
 
 
 

243 lines
7.1 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. case PIPI_PIXELS_Y_F:
  53. bytes = img->w * img->h * sizeof(float);
  54. break;
  55. default:
  56. return NULL;
  57. }
  58. img->p[type].pixels = malloc(bytes);
  59. }
  60. /* Convert pixels */
  61. if(img->last_modified == PIPI_PIXELS_RGBA32
  62. && type == PIPI_PIXELS_RGBA_F)
  63. {
  64. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels;
  65. float *dest = (float *)img->p[type].pixels;
  66. init_tables();
  67. for(y = 0; y < img->h; y++)
  68. for(x = 0; x < img->w; x++)
  69. for(i = 0; i < 4; i++)
  70. dest[4 * (y * img->w + x) + i]
  71. = u8tof32(src[4 * (y * img->w + x) + i]);
  72. }
  73. else if(img->last_modified == PIPI_PIXELS_BGR24
  74. && type == PIPI_PIXELS_RGBA_F)
  75. {
  76. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_BGR24].pixels;
  77. float *dest = (float *)img->p[type].pixels;
  78. init_tables();
  79. for(y = 0; y < img->h; y++)
  80. for(x = 0; x < img->w; x++)
  81. {
  82. dest[4 * (y * img->w + x)]
  83. = u8tof32(src[3 * (y * img->w + x) + 2]);
  84. dest[4 * (y * img->w + x) + 1]
  85. = u8tof32(src[3 * (y * img->w + x) + 1]);
  86. dest[4 * (y * img->w + x) + 2]
  87. = u8tof32(src[3 * (y * img->w + x)]);
  88. dest[4 * (y * img->w + x) + 3] = 1.0;
  89. }
  90. }
  91. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  92. && type == PIPI_PIXELS_RGBA32)
  93. {
  94. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  95. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  96. init_tables();
  97. for(y = 0; y < img->h; y++)
  98. for(x = 0; x < img->w; x++)
  99. for(i = 0; i < 4; i++)
  100. {
  101. double p, e;
  102. uint8_t d;
  103. p = src[4 * (y * img->w + x) + i];
  104. if(p < 0.) d = 0.;
  105. else if(p > 1.) d = 255;
  106. else d = (int)(255.999 * pow(p, 1. / GAMMA));
  107. dest[4 * (y * img->w + x) + i] = d;
  108. e = p - u8tof32(d);
  109. if(x < img->w - 1)
  110. src[4 * (y * img->w + x + 1) + i] += e * .4375;
  111. if(y < img->h - 1)
  112. {
  113. if(x < img->w - 1)
  114. src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875;
  115. src[4 * ((y + 1) * img->w + x) + i] += e * .3125;
  116. if(x < img->w - 1)
  117. src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625;
  118. }
  119. }
  120. }
  121. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  122. && type == PIPI_PIXELS_BGR24)
  123. {
  124. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  125. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  126. init_tables();
  127. for(y = 0; y < img->h; y++)
  128. for(x = 0; x < img->w; x++)
  129. for(i = 0; i < 3; i++)
  130. {
  131. double p, e;
  132. uint8_t d;
  133. p = src[4 * (y * img->w + x) + i];
  134. if(p < 0.) d = 0.;
  135. else if(p > 1.) d = 255;
  136. else d = (int)(255.999 * pow(p, 1. / GAMMA));
  137. dest[3 * (y * img->w + x) + i] = d;
  138. e = p - u8tof32(d);
  139. if(x < img->w - 1)
  140. src[4 * (y * img->w + x + 1) + i] += e * .4375;
  141. if(y < img->h - 1)
  142. {
  143. if(x < img->w - 1)
  144. src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875;
  145. src[4 * ((y + 1) * img->w + x) + i] += e * .3125;
  146. if(x < img->w - 1)
  147. src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625;
  148. }
  149. }
  150. }
  151. else if(img->last_modified == PIPI_PIXELS_Y_F
  152. && type == PIPI_PIXELS_RGBA_F)
  153. {
  154. float *src = (float *)img->p[PIPI_PIXELS_Y_F].pixels;
  155. float *dest = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  156. init_tables();
  157. for(y = 0; y < img->h; y++)
  158. for(x = 0; x < img->w; x++)
  159. {
  160. float p = src[y * img->w + x];
  161. dest[4 * (y * img->w + x)] = p;
  162. dest[4 * (y * img->w + x) + 1] = p;
  163. dest[4 * (y * img->w + x) + 2] = p;
  164. dest[4 * (y * img->w + x) + 3] = 1.0;
  165. }
  166. }
  167. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  168. && type == PIPI_PIXELS_Y_F)
  169. {
  170. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  171. float *dest = (float *)img->p[PIPI_PIXELS_Y_F].pixels;
  172. init_tables();
  173. for(y = 0; y < img->h; y++)
  174. for(x = 0; x < img->w; x++)
  175. {
  176. float p = 0.;
  177. p += 0.299 * src[4 * (y * img->w + x)];
  178. p += 0.587 * src[4 * (y * img->w + x) + 1];
  179. p += 0.114 * src[4 * (y * img->w + x) + 2];
  180. dest[y * img->w + x] = p;
  181. }
  182. }
  183. else
  184. {
  185. memset(img->p[type].pixels, 0, bytes);
  186. }
  187. img->last_modified = type;
  188. return &img->p[type];
  189. }
  190. static void init_tables(void)
  191. {
  192. static int done = 0;
  193. int i;
  194. if(done)
  195. return;
  196. for(i = 0; i < 256; i++)
  197. u8tof32_table[i] = pow((double)i / 255., GAMMA);
  198. done = 1;
  199. }