~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to libfreerdp/primitives/prim_colors.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.2.5)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20141111122050-7z628f4ab38qxad5
Tags: upstream-1.1.0~git20140921.1.440916e+dfsg1
ImportĀ upstreamĀ versionĀ 1.1.0~git20140921.1.440916e+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* FreeRDP: A Remote Desktop Protocol Client
 
2
 * Color conversion operations.
 
3
 * vi:ts=4 sw=4:
 
4
 *
 
5
 * Copyright 2011 Stephen Erisman
 
6
 * Copyright 2011 Norbert Federa <nfedera@thinstuff.com>
 
7
 * Copyright 2011 Martin Fleisz <mfleisz@thinstuff.com>
 
8
 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
 
9
 *
 
10
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 
11
 * not use this file except in compliance with the License. You may obtain
 
12
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 
13
 * Unless required by applicable law or agreed to in writing, software
 
14
 * distributed under the License is distributed on an "AS IS" BASIS,
 
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 
16
 * or implied. See the License for the specific language governing
 
17
 * permissions and limitations under the License.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <freerdp/types.h>
 
25
#include <freerdp/primitives.h>
 
26
 
 
27
#include "prim_internal.h"
 
28
#include "prim_colors.h"
 
29
 
 
30
#ifndef MINMAX
 
31
#define MINMAX(_v_, _l_, _h_) \
 
32
        ((_v_) < (_l_) ? (_l_) : ((_v_) > (_h_) ? (_h_) : (_v_)))
 
33
#endif /* !MINMAX */
 
34
 
 
35
/* ------------------------------------------------------------------------- */
 
36
pstatus_t general_yCbCrToRGB_16s16s_P3P3(
 
37
        const INT16 *pSrc[3],  INT32 srcStep,
 
38
        INT16 *pDst[3],  INT32 dstStep,
 
39
        const prim_size_t *roi) /* region of interest */
 
40
{
 
41
        /**
 
42
         * The decoded YCbCr coeffectients are represented as 11.5 fixed-point
 
43
         * numbers:
 
44
         *
 
45
         * 1 sign bit + 10 integer bits + 5 fractional bits
 
46
         *
 
47
         * However only 7 integer bits will be actually used since the value range
 
48
         * is [-128.0, 127.0].  In other words, the decoded coefficients are scaled
 
49
         * by << 5 when interpreted as INT16.
 
50
         * It was scaled in the quantization phase, so we must scale it back here.
 
51
         */
 
52
        const INT16 *yptr  = pSrc[0];
 
53
        const INT16 *cbptr = pSrc[1];
 
54
        const INT16 *crptr = pSrc[2];
 
55
        INT16 *rptr = pDst[0];
 
56
        INT16 *gptr = pDst[1];
 
57
        INT16 *bptr = pDst[2];
 
58
        int srcbump = (srcStep - (roi->width * sizeof(UINT16))) / sizeof(UINT16);
 
59
        int dstbump = (dstStep - (roi->width * sizeof(UINT16))) / sizeof(UINT16);
 
60
        int y;
 
61
 
 
62
        for (y=0; y<roi->height; y++)
 
63
        {
 
64
                int x;
 
65
                for (x=0; x<roi->width; ++x)
 
66
                {
 
67
                        /* INT32 is used intentionally because we calculate
 
68
                         * with shifted factors!
 
69
                         */
 
70
                        INT32 y  = (INT32) (*yptr++);
 
71
                        INT32 cb = (INT32) (*cbptr++);
 
72
                        INT32 cr = (INT32) (*crptr++);
 
73
                        INT32 r,g,b;
 
74
 
 
75
                        /*
 
76
                         * This is the slow floating point version kept here for reference.
 
77
                         * y = y + 4096; // 128<<5=4096 so that we can scale the sum by>>5
 
78
                         * r = y + cr*1.403f;
 
79
                         * g = y - cb*0.344f - cr*0.714f;
 
80
                         * b = y + cb*1.770f;
 
81
                         * y_r_buf[i]  = MINMAX(r>>5, 0, 255);
 
82
                         * cb_g_buf[i] = MINMAX(g>>5, 0, 255);
 
83
                         * cr_b_buf[i] = MINMAX(b>>5, 0, 255);
 
84
                         */
 
85
 
 
86
                        /*
 
87
                         * We scale the factors by << 16 into 32-bit integers in order to
 
88
                         * avoid slower floating point multiplications.  Since the final
 
89
                         * result needs to be scaled by >> 5 we will extract only the
 
90
                         * upper 11 bits (>> 21) from the final sum.
 
91
                         * Hence we also have to scale the other terms of the sum by << 16.
 
92
                         * R: 1.403 << 16 = 91947
 
93
                         * G: 0.344 << 16 = 22544, 0.714 << 16 = 46792
 
94
                         * B: 1.770 << 16 = 115998
 
95
                         */
 
96
                        y = (y+4096)<<16;
 
97
 
 
98
                        r = y + cr*91947;
 
99
                        g = y - cb*22544 - cr*46792;
 
100
                        b = y + cb*115998;
 
101
 
 
102
                        *rptr++ = MINMAX(r>>21, 0, 255);
 
103
                        *gptr++ = MINMAX(g>>21, 0, 255);
 
104
                        *bptr++ = MINMAX(b>>21, 0, 255);
 
105
                }
 
106
                yptr  += srcbump;
 
107
                cbptr += srcbump;
 
108
                crptr += srcbump;
 
109
                rptr += dstbump;
 
110
                gptr += dstbump;
 
111
                bptr += dstbump;
 
112
        }
 
113
        return PRIMITIVES_SUCCESS;
 
114
}
 
115
 
 
116
/* ------------------------------------------------------------------------- */
 
117
pstatus_t general_RGBToYCbCr_16s16s_P3P3(
 
118
        const INT16 *pSrc[3],  INT32 srcStep,
 
119
        INT16 *pDst[3],  INT32 dstStep,
 
120
        const prim_size_t *roi) /* region of interest */
 
121
{
 
122
        /* The encoded YCbCr coefficients are represented as 11.5 fixed-point
 
123
         * numbers:
 
124
         *
 
125
         * 1 sign bit + 10 integer bits + 5 fractional bits
 
126
         *
 
127
         * However only 7 integer bits will be actually used since the value
 
128
         * range is [-128.0, 127.0].  In other words, the encoded coefficients
 
129
         * is scaled by << 5 when interpreted as INT16.
 
130
         * It will be scaled down to original during the quantization phase.
 
131
         */
 
132
        const INT16 *rptr = pSrc[0];
 
133
        const INT16 *gptr = pSrc[1];
 
134
        const INT16 *bptr = pSrc[2];
 
135
        INT16 *yptr  = pDst[0];
 
136
        INT16 *cbptr = pDst[1];
 
137
        INT16 *crptr = pDst[2];
 
138
        int srcbump = (srcStep - (roi->width * sizeof(UINT16))) / sizeof(UINT16);
 
139
        int dstbump = (dstStep - (roi->width * sizeof(UINT16))) / sizeof(UINT16);
 
140
        int y;
 
141
 
 
142
        for (y=0; y<roi->height; y++)
 
143
        {
 
144
                int x;
 
145
                for (x=0; x<roi->width; ++x)
 
146
                {
 
147
                        /* INT32 is used intentionally because we calculate with
 
148
                         * shifted factors!
 
149
                         */
 
150
                        INT32 r = (INT32) (*rptr++);
 
151
                        INT32 g = (INT32) (*gptr++);
 
152
                        INT32 b = (INT32) (*bptr++);
 
153
 
 
154
                        /* We scale the factors by << 15 into 32-bit integers in order
 
155
                         * to avoid slower floating point multiplications.  Since the
 
156
                         * terms need to be scaled by << 5 we simply scale the final
 
157
                         * sum by >> 10
 
158
                         *
 
159
                         * Y:  0.299000 << 15 = 9798,  0.587000 << 15 = 19235, 
 
160
                         *     0.114000 << 15 = 3735
 
161
                         * Cb: 0.168935 << 15 = 5535,  0.331665 << 15 = 10868, 
 
162
                         *     0.500590 << 15 = 16403
 
163
                         * Cr: 0.499813 << 15 = 16377, 0.418531 << 15 = 13714,
 
164
                         *     0.081282 << 15 = 2663
 
165
                         */
 
166
                        INT32 y  = (r *  9798 + g *  19235 + b *  3735) >> 10;
 
167
                        INT32 cb = (r * -5535 + g * -10868 + b * 16403) >> 10;
 
168
                        INT32 cr = (r * 16377 + g * -13714 + b * -2663) >> 10;
 
169
 
 
170
                        *yptr++  = (INT16) MINMAX(y - 4096, -4096, 4095);
 
171
                        *cbptr++ = (INT16) MINMAX(cb, -4096, 4095);
 
172
                        *crptr++ = (INT16) MINMAX(cr, -4096, 4095);
 
173
                }
 
174
                yptr  += srcbump;
 
175
                cbptr += srcbump;
 
176
                crptr += srcbump;
 
177
                rptr += dstbump;
 
178
                gptr += dstbump;
 
179
                bptr += dstbump;
 
180
        }
 
181
        return PRIMITIVES_SUCCESS;
 
182
}
 
183
 
 
184
/* ------------------------------------------------------------------------- */
 
185
pstatus_t general_RGBToRGB_16s8u_P3AC4R(
 
186
        const INT16 *pSrc[3],   /* 16-bit R,G, and B arrays */
 
187
        int srcStep,                    /* bytes between rows in source data */
 
188
        BYTE *pDst,                     /* 32-bit interleaved ARGB (ABGR?) data */
 
189
        int dstStep,                    /* bytes between rows in dest data */
 
190
        const prim_size_t *roi) /* region of interest */
 
191
{
 
192
        const INT16 *r  = pSrc[0];
 
193
        const INT16 *g  = pSrc[1];
 
194
        const INT16 *b  = pSrc[2];
 
195
        BYTE *dst = pDst;
 
196
        int x,y;
 
197
        int srcbump = (srcStep - (roi->width * sizeof(UINT16))) / sizeof(UINT16);
 
198
        int dstbump = (dstStep - (roi->width * sizeof(UINT32)));
 
199
 
 
200
        for (y=0; y<roi->height; ++y)
 
201
        {
 
202
                for (x=0; x<roi->width; ++x)
 
203
                {
 
204
                        *dst++ = (BYTE) (*b++);
 
205
                        *dst++ = (BYTE) (*g++);
 
206
                        *dst++ = (BYTE) (*r++);
 
207
                        *dst++ = ((BYTE) (0xFFU));
 
208
                }
 
209
                dst += dstbump;
 
210
                r += srcbump;
 
211
                g += srcbump;
 
212
                b += srcbump;
 
213
        }
 
214
        return PRIMITIVES_SUCCESS;
 
215
}
 
216
 
 
217
/* ------------------------------------------------------------------------- */
 
218
void primitives_init_colors(primitives_t* prims)
 
219
{
 
220
        prims->RGBToRGB_16s8u_P3AC4R  = general_RGBToRGB_16s8u_P3AC4R;
 
221
        prims->yCbCrToRGB_16s16s_P3P3 = general_yCbCrToRGB_16s16s_P3P3;
 
222
        prims->RGBToYCbCr_16s16s_P3P3 = general_RGBToYCbCr_16s16s_P3P3;
 
223
 
 
224
        primitives_init_colors_opt(prims);
 
225
}
 
226
 
 
227
/* ------------------------------------------------------------------------- */
 
228
void primitives_deinit_colors(primitives_t* prims)
 
229
{
 
230
        /* Nothing to do. */
 
231
}
 
232