Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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