Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

141 wiersze
3.6 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_RGBA_F:
  47. bytes = img->w * img->h * 4 * sizeof(float);
  48. break;
  49. default:
  50. return NULL;
  51. }
  52. img->p[type].pixels = malloc(bytes);
  53. }
  54. /* Convert pixels */
  55. if(img->last_modified == PIPI_PIXELS_RGBA32
  56. && type == PIPI_PIXELS_RGBA_F)
  57. {
  58. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels;
  59. float *dest = (float *)img->p[type].pixels;
  60. init_tables();
  61. for(y = 0; y < img->h; y++)
  62. for(x = 0; x < img->w; x++)
  63. for(i = 0; i < 4; i++)
  64. dest[4 * (y * img->w + x) + i]
  65. = u8tof32(src[4 * (y * img->w + x) + i]);
  66. }
  67. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  68. && type == PIPI_PIXELS_RGBA32)
  69. {
  70. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  71. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  72. init_tables();
  73. for(y = 0; y < img->h; y++)
  74. for(x = 0; x < img->w; x++)
  75. for(i = 0; i < 4; i++)
  76. {
  77. double p, e;
  78. uint8_t d;
  79. p = src[4 * (y * img->w + x) + i];
  80. d = (int)(255.999 * pow(p, 1. / GAMMA));
  81. dest[4 * (y * img->w + x) + i] = d;
  82. e = p - u8tof32(d);
  83. if(x < img->w - 1)
  84. src[4 * (y * img->w + x + 1) + i] += e * .4375;
  85. if(y < img->h - 1)
  86. {
  87. if(x < img->w - 1)
  88. src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875;
  89. src[4 * ((y + 1) * img->w + x) + i] += e * .3125;
  90. if(x < img->w - 1)
  91. src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625;
  92. }
  93. }
  94. }
  95. else
  96. {
  97. memset(img->p[type].pixels, 0, bytes);
  98. }
  99. img->last_modified = type;
  100. return &img->p[type];
  101. }
  102. static void init_tables(void)
  103. {
  104. static int done = 0;
  105. int i;
  106. if(done)
  107. return;
  108. for(i = 0; i < 256; i++)
  109. u8tof32_table[i] = pow((double)i / 255., GAMMA);
  110. done = 1;
  111. }