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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;
  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. yp = (yp * 220.0 + 16.0) / 255.0;
  49. u = 0.5 - 0.14713 * r - 0.28886 * g + 0.436 * b;
  50. if (u < 0.0) u = 0.0;
  51. if (u > 1.0) u = 1.0;
  52. v = 0.5 + 0.615 * r - 0.51499 * g - 0.10001 * b;
  53. if (v < 0.0) v = 0.0;
  54. if (v > 1.0) v = 1.0;
  55. dstdata[d] = yp;
  56. dstdata[d + 1] = u;
  57. dstdata[d + 2] = v;
  58. dstdata[d + 3] = a;
  59. }
  60. }
  61. return dst;
  62. }
  63. pipi_image_t *pipi_yuv2rgb(pipi_image_t *src)
  64. {
  65. pipi_image_t *dst;
  66. pipi_pixels_t *srcp, *dstp;
  67. float *srcdata, *dstdata;
  68. int x, y, w, h;
  69. w = src->w;
  70. h = src->h;
  71. srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  72. srcdata = (float *)srcp->pixels;
  73. dst = pipi_new(w, h);
  74. dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  75. dstdata = (float *)dstp->pixels;
  76. for(y = 0; y < h; y++)
  77. {
  78. for(x = 0; x < w; x++)
  79. {
  80. double r, g, b, a, yp, u, v;
  81. int d = 4 * (y * w + x);
  82. yp = (srcdata[d] * 255.0 - 16.0) / 220.0;
  83. u = srcdata[d + 1] - 0.5;
  84. v = srcdata[d + 2] - 0.5;
  85. a = srcdata[d + 3];
  86. r = yp + 1.13983 * v;
  87. if (r < 0.0) r = 0.0;
  88. if (r > 1.0) r = 1.0;
  89. g = yp - 0.39465 * u - 0.58060 * v;
  90. if (g < 0.0) g = 0.0;
  91. if (g > 1.0) g = 1.0;
  92. b = yp + 2.03211 * u;
  93. if (b < 0.0) b = 0.0;
  94. if (b > 1.0) b = 1.0;
  95. dstdata[d] = r;
  96. dstdata[d + 1] = g;
  97. dstdata[d + 2] = b;
  98. dstdata[d + 3] = a;
  99. }
  100. }
  101. return dst;
  102. }