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.
 
 
 
 
 
 

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