選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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