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.
 
 
 
 
 
 

122 lines
2.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. static void init_tables(void);
  26. static float u8tof32_table[256];
  27. static inline float u8tof32(uint8_t p) { return u8tof32_table[(int)p]; }
  28. /* Return a direct pointer to an image's pixels. */
  29. pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type)
  30. {
  31. size_t bytes = 0;
  32. int x, y, i;
  33. if(type < 0 || type >= PIPI_PIXELS_MAX)
  34. return NULL;
  35. if(img->last_modified == type)
  36. return &img->p[type];
  37. /* Allocate pixels if necessary */
  38. if(!img->p[type].pixels)
  39. {
  40. switch(type)
  41. {
  42. case PIPI_PIXELS_RGBA32:
  43. bytes = img->w * img->h * 4 * sizeof(uint8_t);
  44. break;
  45. case PIPI_PIXELS_RGBA_F:
  46. bytes = img->w * img->h * 4 * sizeof(float);
  47. break;
  48. default:
  49. return NULL;
  50. }
  51. img->p[type].pixels = malloc(bytes);
  52. }
  53. /* Convert pixels */
  54. if(img->last_modified == PIPI_PIXELS_RGBA32
  55. && type == PIPI_PIXELS_RGBA_F)
  56. {
  57. uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels;
  58. float *dest = (float *)img->p[type].pixels;
  59. init_tables();
  60. for(y = 0; y < img->h; y++)
  61. for(x = 0; x < img->w; x++)
  62. for(i = 0; i < 4; i++)
  63. dest[4 * (y * img->w + x) + i]
  64. = u8tof32(src[4 * (y * img->w + x) + i]);
  65. }
  66. else if(img->last_modified == PIPI_PIXELS_RGBA_F
  67. && type == PIPI_PIXELS_RGBA32)
  68. {
  69. float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels;
  70. uint8_t *dest = (uint8_t *)img->p[type].pixels;
  71. for(y = 0; y < img->h; y++)
  72. for(x = 0; x < img->w; x++)
  73. for(i = 0; i < 4; i++)
  74. {
  75. double p = src[4 * (y * img->w + x) + i];
  76. dest[4 * (y * img->w + x) + i]
  77. = (int)(255.999 * pow(p, 1. / 2.2));
  78. }
  79. }
  80. else
  81. {
  82. memset(img->p[type].pixels, 0, bytes);
  83. }
  84. img->last_modified = type;
  85. return &img->p[type];
  86. }
  87. static void init_tables(void)
  88. {
  89. static int done = 0;
  90. int i;
  91. if(done)
  92. return;
  93. for(i = 0; i < 256; i++)
  94. u8tof32_table[i] = pow((double)i / 255., 2.2);
  95. done = 1;
  96. }