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