Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

pixels.c 8.3 KiB

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