Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

125 рядки
3.0 KiB

  1. /*
  2. * libpipi Pathetic image processing interface 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. * yuv.c: YUV conversion functions
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. pipi_image_t *pipi_rgb2yuv(pipi_image_t *src)
  25. {
  26. pipi_image_t *dst;
  27. pipi_pixels_t *srcp, *dstp;
  28. float *srcdata, *dstdata;
  29. int x, y, w, h, gray;
  30. w = src->w;
  31. h = src->h;
  32. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  33. srcdata = (float *)srcp->pixels;
  34. dst = pipi_new(w, h);
  35. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  36. dstdata = (float *)dstp->pixels;
  37. for(y = 0; y < h; y++)
  38. {
  39. for(x = 0; x < w; x++)
  40. {
  41. double r, g, b, a, yp, u, v;
  42. int d = 4 * (y * w + x);
  43. r = srcdata[d];
  44. g = srcdata[d + 1];
  45. b = srcdata[d + 2];
  46. a = srcdata[d + 3];
  47. yp = 0.299 * r + 0.587 * g + 0.114 * b;
  48. u = 0.5 - 0.14713 * r - 0.28886 * g + 0.436 * b;
  49. if (u < 0.0) u = 0.0;
  50. if (u > 1.0) u = 1.0;
  51. v = 0.5 + 0.615 * r - 0.51499 * g - 0.10001 * b;
  52. if (v < 0.0) v = 0.0;
  53. if (v > 1.0) v = 1.0;
  54. dstdata[d] = v;
  55. dstdata[d + 1] = yp;
  56. dstdata[d + 2] = u;
  57. dstdata[d + 3] = a;
  58. }
  59. }
  60. return dst;
  61. }
  62. pipi_image_t *pipi_yuv2rgb(pipi_image_t *src)
  63. {
  64. pipi_image_t *dst;
  65. pipi_pixels_t *srcp, *dstp;
  66. float *srcdata, *dstdata;
  67. int x, y, w, h, gray;
  68. w = src->w;
  69. h = src->h;
  70. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  71. srcdata = (float *)srcp->pixels;
  72. dst = pipi_new(w, h);
  73. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  74. dstdata = (float *)dstp->pixels;
  75. for(y = 0; y < h; y++)
  76. {
  77. for(x = 0; x < w; x++)
  78. {
  79. double r, g, b, a, yp, u, v;
  80. int d = 4 * (y * w + x);
  81. v = srcdata[d] - 0.5;
  82. yp = srcdata[d + 1];
  83. u = srcdata[d + 2] - 0.5;
  84. a = srcdata[d + 3];
  85. r = yp + 1.13983 * v;
  86. if (r < 0.0) r = 0.0;
  87. if (r > 1.0) r = 1.0;
  88. g = yp - 0.39465 * u - 0.58060 * v;
  89. if (g < 0.0) g = 0.0;
  90. if (g > 1.0) g = 1.0;
  91. b = yp + 2.03211 * u;
  92. if (b < 0.0) b = 0.0;
  93. if (b > 1.0) b = 1.0;
  94. dstdata[d] = r;
  95. dstdata[d + 1] = g;
  96. dstdata[d + 2] = b;
  97. dstdata[d + 3] = a;
  98. }
  99. }
  100. return dst;
  101. }