source: libpipi/trunk/pipi/filter/yuv.c @ 4699

Last change on this file since 4699 was 4699, checked in by Sam Hocevar, 10 years ago

Clamp Y to 16 - 235, as the MPEG standard uses.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
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 4699 2010-10-19 16:33:10Z 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
29pipi_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;
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            yp = (yp * 220.0 + 16.0) / 255.0;
60
61            u = 0.5 - 0.14713 * r - 0.28886 * g + 0.436 * b;
62            if (u < 0.0) u = 0.0;
63            if (u > 1.0) u = 1.0;
64
65            v = 0.5 + 0.615 * r - 0.51499 * g - 0.10001 * b;
66            if (v < 0.0) v = 0.0;
67            if (v > 1.0) v = 1.0;
68
69            dstdata[d] = yp;
70            dstdata[d + 1] = u;
71            dstdata[d + 2] = v;
72            dstdata[d + 3] = a;
73        }
74    }
75
76    return dst;
77}
78
79pipi_image_t *pipi_yuv2rgb(pipi_image_t *src)
80{
81    pipi_image_t *dst;
82    pipi_pixels_t *srcp, *dstp;
83    float *srcdata, *dstdata;
84    int x, y, w, h;
85
86    w = src->w;
87    h = src->h;
88
89    srcp = pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
90    srcdata = (float *)srcp->pixels;
91
92    dst = pipi_new(w, h);
93    dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
94    dstdata = (float *)dstp->pixels;
95
96    for(y = 0; y < h; y++)
97    {
98        for(x = 0; x < w; x++)
99        {
100            double r, g, b, a, yp, u, v;
101            int d = 4 * (y * w + x);
102
103            yp = (srcdata[d] * 255.0 - 16.0) / 220.0;
104            u = srcdata[d + 1] - 0.5;
105            v = srcdata[d + 2] - 0.5;
106            a = srcdata[d + 3];
107
108            r = yp + 1.13983 * v;
109            if (r < 0.0) r = 0.0;
110            if (r > 1.0) r = 1.0;
111            g = yp - 0.39465 * u - 0.58060 * v;
112            if (g < 0.0) g = 0.0;
113            if (g > 1.0) g = 1.0;
114            b = yp + 2.03211 * u;
115            if (b < 0.0) b = 0.0;
116            if (b > 1.0) b = 1.0;
117
118            dstdata[d] = r;
119            dstdata[d + 1] = g;
120            dstdata[d + 2] = b;
121            dstdata[d + 3] = a;
122        }
123    }
124
125    return dst;
126}
127
Note: See TracBrowser for help on using the repository browser.