~ubuntu-branches/ubuntu/gutsy/amsn/gutsy

« back to all changes in this revision

Viewing changes to utils/webcamsn/src/colorspace.c

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Karkoulis
  • Date: 2006-01-04 15:26:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104152602-ipe1yg00rl3nlklv
Tags: 0.95-1
New Upstream Release (closes: #345052, #278575).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005  Ole André Vadla Ravnås <oleavr@gmail.com>
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 */
 
17
 
 
18
#include "mimic-private.h"
 
19
 
 
20
#define RED_INDEX_1      0
 
21
#define GREEN_INDEX_1    1
 
22
#define BLUE_INDEX_1     2
 
23
 
 
24
#define RED_INDEX_2      3
 
25
#define GREEN_INDEX_2    4
 
26
#define BLUE_INDEX_2     5
 
27
 
 
28
/*
 
29
 * _rgb_to_yuv
 
30
 *
 
31
 * Internal helper-function used to convert an image
 
32
 * from RGB 24-bpp packed-pixel to YUV420 planar.
 
33
 */
 
34
void _rgb_to_yuv(const guchar *input_rgb,
 
35
                 guchar *output_y,
 
36
                 guchar *output_cb,
 
37
                 guchar *output_cr,
 
38
                 gint width,
 
39
                 gint height)
 
40
{
 
41
    gint y, x;
 
42
 
 
43
    for (y = 0; y < height; y += 2) {
 
44
 
 
45
        const guchar *src1, *src2;
 
46
        guchar *dst1, *dst2, *dst3, *dst4;
 
47
        gint num_cols;
 
48
 
 
49
        src1 = input_rgb + ((height - 1 - y) * width * 3);
 
50
        src2 = input_rgb + ((height - 2 - y) * width * 3);
 
51
 
 
52
        dst1 = output_y + (y * width);
 
53
        dst2 = output_y + ((y + 1) * width);
 
54
        dst3 = output_cb + ((y / 2) * (width / 2));
 
55
        dst4 = output_cr + ((y / 2) * (width / 2));
 
56
 
 
57
        num_cols = width / 2;
 
58
 
 
59
        for (x = 0; x < num_cols; x++) {
 
60
 
 
61
            gint expr1, expr2, expr3, expr4, expr5, v;
 
62
 
 
63
            expr1 = (src1[BLUE_INDEX_1] * 19595) + (src1[GREEN_INDEX_1] * 38470) + (src1[RED_INDEX_1] * 7471);
 
64
            expr2 = (src1[BLUE_INDEX_2] * 19595) + (src1[GREEN_INDEX_2] * 38470) + (src1[RED_INDEX_2] * 7471);
 
65
            expr3 = (src2[BLUE_INDEX_1] * 19595) + (src2[GREEN_INDEX_1] * 38470) + (src2[RED_INDEX_1] * 7471);
 
66
            expr4 = (src2[BLUE_INDEX_2] * 19595) + (src2[GREEN_INDEX_2] * 38470) + (src2[RED_INDEX_2] * 7471);
 
67
 
 
68
            expr5 = expr1 + expr2 + expr3 + expr4;
 
69
 
 
70
            dst1[0] = expr1 >> 16;
 
71
            dst1[1] = expr2 >> 16;
 
72
            dst2[0] = expr3 >> 16;
 
73
            dst2[1] = expr4 >> 16;
 
74
 
 
75
            v = (((src1[BLUE_INDEX_1] + src1[BLUE_INDEX_2] + src2[BLUE_INDEX_1] + src2[BLUE_INDEX_2]) << 16) - expr5 + 131071) >> 16;
 
76
            dst3[0] = _clamp_value(((v * 57475) >> 18) + 128);
 
77
            
 
78
            v = (((src1[RED_INDEX_1] + src1[RED_INDEX_2] + src2[RED_INDEX_1] + src2[RED_INDEX_2]) << 16) - expr5 + 131071) >> 16;
 
79
            dst4[0] = ((v * 32244) >> 18) + 128;
 
80
 
 
81
            src1 += 6;
 
82
            src2 += 6;
 
83
 
 
84
            dst1 += 2;
 
85
            dst2 += 2;
 
86
            dst3++;
 
87
            dst4++;
 
88
 
 
89
        }
 
90
 
 
91
    }
 
92
 
 
93
}
 
94
 
 
95
/*
 
96
 * _yuv_to_rgb
 
97
 *
 
98
 * Internal helper-function used to convert an image
 
99
 * from YUV420 planar to RGB 24-bpp packed-pixel.
 
100
 */
 
101
void _yuv_to_rgb(const guchar *input_y,
 
102
                 const guchar *input_cb,
 
103
                 const guchar *input_cr,
 
104
                 guchar *output_rgb,
 
105
                 guint width,
 
106
                 guint height)
 
107
{
 
108
    const guchar *src_y, *src_cb, *src_cr;
 
109
    guchar *dst_rgb;
 
110
    guint i, j, rgb_stride;
 
111
    
 
112
    src_y  = input_y;
 
113
    src_cb = input_cb;
 
114
    src_cr = input_cr;
 
115
    
 
116
    rgb_stride = width * 3;
 
117
    dst_rgb = output_rgb + (rgb_stride * (height - 1));
 
118
    
 
119
    for (i = 0; i < height; i++) {
 
120
        const guchar *p_y, *p_cb, *p_cr;
 
121
        guchar *p_rgb;
 
122
 
 
123
        p_y = src_y;
 
124
        p_cb = src_cb;
 
125
        p_cr = src_cr;
 
126
 
 
127
        p_rgb = dst_rgb;
 
128
 
 
129
        for (j = 0; j < width; j++) {
 
130
            gint v;
 
131
 
 
132
            v = ((p_y[0] * 65536) + ((p_cr[0] - 128) * 133169)) / 65536;
 
133
            p_rgb[0] = _clamp_value(v);
 
134
 
 
135
            v = ((p_y[0] * 65536) - ((p_cr[0] - 128) * 25821) - ((p_cb[0] - 128) * 38076)) / 65536;
 
136
            p_rgb[1] = _clamp_value(v);
 
137
 
 
138
            v = ((p_y[0] * 65536) + ((p_cb[0] - 128) * 74711)) / 65536;
 
139
            p_rgb[2] = _clamp_value(v);
 
140
 
 
141
            p_y++;
 
142
            if ((j + 1) % 2 == 0) {
 
143
                p_cb++;
 
144
                p_cr++;
 
145
            }
 
146
 
 
147
            p_rgb += 3;
 
148
        }
 
149
 
 
150
        src_y += width;
 
151
        if ((i + 1) % 2 == 0) {
 
152
            src_cb += (width + 1) / 2;
 
153
            src_cr += (width + 1) / 2;
 
154
        }
 
155
 
 
156
        dst_rgb -= rgb_stride;
 
157
 
 
158
    }
 
159
 
 
160
}
 
161