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.

pixels.c 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 C2I(p) (pow(((double)p)/255., 2.2))
  26. #define I2C(p) ((int)255.999*pow(((double)p), 1./2.2))
  27. int pipi_getgray(pipi_image_t const *img, int x, int y, int *g)
  28. {
  29. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  30. {
  31. *g = 255;
  32. return -1;
  33. }
  34. *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
  35. return 0;
  36. }
  37. int pipi_getpixel(pipi_image_t const *img,
  38. int x, int y, double *r, double *g, double *b)
  39. {
  40. uint8_t *pixel;
  41. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  42. {
  43. *r = *g = *b = 1.;
  44. return -1;
  45. }
  46. pixel = img->pixels + y * img->pitch + x * img->channels;
  47. *b = C2I((unsigned char)pixel[0]);
  48. *g = C2I((unsigned char)pixel[1]);
  49. *r = C2I((unsigned char)pixel[2]);
  50. return 0;
  51. }
  52. int pipi_setpixel(pipi_image_t *img, int x, int y, double r, double g, double b)
  53. {
  54. uint8_t *pixel;
  55. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  56. return -1;
  57. pixel = img->pixels + y * img->pitch + x * img->channels;
  58. pixel[0] = I2C(b);
  59. pixel[1] = I2C(g);
  60. pixel[2] = I2C(r);
  61. return 0;
  62. }