Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

85 linhas
1.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_internals.h"
  24. #include "pipi.h"
  25. #define C2I(p) ((int)255.999*pow(((double)p)/255., 2.2))
  26. #define I2C(p) ((int)255.999*pow(((double)p)/255., 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, int *r, int *g, int *b)
  39. {
  40. uint8_t *pixel;
  41. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  42. {
  43. *r = 255;
  44. *g = 255;
  45. *b = 255;
  46. return -1;
  47. }
  48. pixel = img->pixels + y * img->pitch + x * img->channels;
  49. *b = C2I((unsigned char)pixel[0]);
  50. *g = C2I((unsigned char)pixel[1]);
  51. *r = C2I((unsigned char)pixel[2]);
  52. return 0;
  53. }
  54. int pipi_setpixel(pipi_image_t *img, int x, int y, int r, int g, int b)
  55. {
  56. uint8_t *pixel;
  57. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  58. return -1;
  59. pixel = img->pixels + y * img->pitch + x * img->channels;
  60. pixel[0] = I2C(b);
  61. pixel[1] = I2C(g);
  62. pixel[2] = I2C(r);
  63. return 0;
  64. }