Update contrib.
2 * LIBOIL - Library of Optimized Inner Loops
3 * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 //Portions Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
33 #include "liboil/liboil.h"
34 #include "liboilfunction.h"
35 #include "liboil/liboilcolorspace.h"
36 #include "liboil/liboiltest.h"
45 * Converts AYUV pixels to ARGB pixels. AYUV pixels are in the
46 * JPEG colorspace. Note that this function doesn't follow normal
47 * liboil pixel conventions.
49 * (This function should be replaced by one that handles other
50 * conversion factors.)
52 OIL_DEFINE_CLASS (ayuv2argb_u8, "uint8_t *d_4xn, uint8_t *s_4xn, int n");
55 #define clamp(x,a,b) clamp_lower(clamp_upper(x,b),a)
56 #define clamp_lower(x,a) ((x<a)?(a):(x))
57 #define clamp_upper(x,b) ((x>b)?(b):(x))
59 /* from the JFIF spec */
60 #define YUV_TO_R(y,u,v) clamp((y) + 1.402*((v)-128.0),0,255)
61 #define YUV_TO_G(y,u,v) clamp((y) - 0.34414*((u)-128.0) - 0.71414*((v)-128.0),0,255)
62 #define YUV_TO_B(y,u,v) clamp((y) + 1.772*((u)-128.0),0,255)
64 #define muldiv256(x,y) (((x)*(y) + 128)>>8)
65 #define YUV_TO_R_INT8(y,u,v) clamp((y) + muldiv256(359,(v)-128),0,255)
66 #define YUV_TO_G_INT8(y,u,v) clamp((y) + muldiv256(-88,(u)-128) + muldiv256(-183,(v)-128),0,255)
67 #define YUV_TO_B_INT8(y,u,v) clamp((y) + muldiv256(454,(u)-128),0,255)
69 #define muldiv65536(x,y) (((x)*(y) + 32768)>>16)
70 #define YUV_TO_R_INT16(y,u,v) clamp((y) + muldiv65536(91881,(v)-128),0,255)
71 #define YUV_TO_G_INT16(y,u,v) clamp((y) - muldiv65536(22554,(u)-128) - muldiv65536(46802,(v)-128),0,255)
72 #define YUV_TO_B_INT16(y,u,v) clamp((y) + muldiv65536(116130,(u)-128),0,255)
77 #define X(x) ((int)((x)*(1<<(SHIFT+8)) + 0.5))
78 static int16_t jfif_matrix[][4] = {
79 { 0, 0, -8192, -8192 },
81 { 0, 16384, 16384, 16384 },
82 { 0, 0, -5638, 29032 },
83 { 0, 22970, -11700, 0 },
88 colorspace_argb_test (OilTest *test)
90 int16_t *data = oil_test_get_source_data (test, OIL_ARG_SRC2);
92 memcpy (data, jfif_matrix, 4*2*6);
95 OIL_DEFINE_CLASS_FULL (colorspace_argb, "uint32_t *d, uint32_t *s, int16_t *s2_24, int n", colorspace_argb_test);
98 colorspace_argb_ref (uint32_t *dest, const uint32_t *src, const int16_t *matrix, int n)
103 int sa = (oil_argb_A(src[i])<<SHIFT) + matrix[0*4+0];
104 int sr = (oil_argb_R(src[i])<<SHIFT) + matrix[0*4+1];
105 int sg = (oil_argb_G(src[i])<<SHIFT) + matrix[0*4+2];
106 int sb = (oil_argb_B(src[i])<<SHIFT) + matrix[0*4+3];
109 #define MUL(a,b) (((a)*(b))>>16)
110 da = (MUL(sa,matrix[1*4+0]) + MUL(sr,matrix[2*4+0]) + MUL(sg,matrix[3*4+0]) +
111 MUL(sb,matrix[4*4+0]) + matrix[5*4+0] + 8)>>(2*SHIFT - 8);
112 dr = (MUL(sa,matrix[1*4+1]) + MUL(sr,matrix[2*4+1]) + MUL(sg,matrix[3*4+1]) +
113 MUL(sb,matrix[4*4+1]) + matrix[5*4+1] + 8)>>(2*SHIFT - 8);
114 dg = (MUL(sa,matrix[1*4+2]) + MUL(sr,matrix[2*4+2]) + MUL(sg,matrix[3*4+2]) +
115 MUL(sb,matrix[4*4+2]) + matrix[5*4+2] + 8)>>(2*SHIFT - 8);
116 db = (MUL(sa,matrix[1*4+3]) + MUL(sr,matrix[2*4+3]) + MUL(sg,matrix[3*4+3]) +
117 MUL(sb,matrix[4*4+3]) + matrix[5*4+3] + 8)>>(2*SHIFT - 8);
118 dest[i] = oil_argb(da, dr, dg, db);
122 OIL_DEFINE_IMPL_REF (colorspace_argb_ref, colorspace_argb);
126 ayuv2argb_u8_ref (uint8_t *argb, const uint8_t *ayuv, int n)
132 argb[1] = YUV_TO_R_INT8(ayuv[1], ayuv[2], ayuv[3]);
133 argb[2] = YUV_TO_G_INT8(ayuv[1], ayuv[2], ayuv[3]);
134 argb[3] = YUV_TO_B_INT8(ayuv[1], ayuv[2], ayuv[3]);
140 OIL_DEFINE_IMPL_REF (ayuv2argb_u8_ref, ayuv2argb_u8);
147 OilFunctionClass* __oil_function_class_ayuv2argb_u8() {
148 return &_oil_function_class_ayuv2argb_u8;
154 OilFunctionClass* __oil_function_class_colorspace_argb() {
155 return &_oil_function_class_colorspace_argb;
163 OilFunctionImpl* __oil_function_impl_colorspace_argb_ref() {
164 return &_oil_function_impl_colorspace_argb_ref;
170 OilFunctionImpl* __oil_function_impl_ayuv2argb_u8_ref() {
171 return &_oil_function_impl_ayuv2argb_u8_ref;
179 EXPORT_C void** _oil_function_class_ptr_ayuv2argb_u8 () {
180 oil_function_class_ptr_ayuv2argb_u8 = __oil_function_class_ayuv2argb_u8();
181 return &oil_function_class_ptr_ayuv2argb_u8->func;
187 EXPORT_C void** _oil_function_class_ptr_colorspace_argb () {
188 oil_function_class_ptr_colorspace_argb = __oil_function_class_colorspace_argb();
189 return &oil_function_class_ptr_colorspace_argb->func;