os/ossrv/genericopenlibs/liboil/src/ayuv2argb.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * LIBOIL - Library of Optimized Inner Loops
     3  * Copyright (c) 2003,2004 David A. Schleef <ds@schleef.org>
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions
     8  * are met:
     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.
    14  * 
    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.
    26  */
    27 //Portions Copyright (c)  2008-2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
    28 
    29 #ifdef HAVE_CONFIG_H
    30 #include "config.h"
    31 #endif
    32 
    33 #include "liboil/liboil.h"
    34 #include "liboilfunction.h"
    35 #include "liboil/liboilcolorspace.h"
    36 #include "liboil/liboiltest.h"
    37 #include <string.h>
    38 
    39 /**
    40  * oil_ayuv2argb_u8:
    41  * @d_4xn:
    42  * @s_4xn:
    43  * @n:
    44  *
    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.
    48  *
    49  * (This function should be replaced by one that handles other
    50  * conversion factors.)
    51  */
    52 OIL_DEFINE_CLASS (ayuv2argb_u8, "uint8_t *d_4xn, uint8_t *s_4xn, int n");
    53 
    54 
    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))
    58 
    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)
    63 
    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)
    68 
    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)
    73 
    74 
    75 #define SHIFT 6
    76 #define MULT (1<<6)
    77 #define X(x) ((int)((x)*(1<<(SHIFT+8)) + 0.5))
    78 static int16_t jfif_matrix[][4] = {
    79   { 0,      0,      -8192,   -8192  },
    80   { 16384,  0,      0,       0      },
    81   { 0,      16384,  16384,   16384  },
    82   { 0,      0,      -5638,   29032  },
    83   { 0,      22970,  -11700,  0      },
    84   { 0, 0, 0, 0 }
    85 };
    86 
    87 static void
    88 colorspace_argb_test (OilTest *test)
    89 {
    90   int16_t *data = oil_test_get_source_data (test, OIL_ARG_SRC2);
    91 
    92   memcpy (data, jfif_matrix, 4*2*6);
    93 }
    94 
    95 OIL_DEFINE_CLASS_FULL (colorspace_argb, "uint32_t *d, uint32_t *s, int16_t *s2_24, int n", colorspace_argb_test);
    96 
    97 static void
    98 colorspace_argb_ref (uint32_t *dest, const uint32_t *src, const int16_t *matrix, int n)
    99 {
   100   int i;
   101 
   102   for(i=0;i<n;i++){
   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];
   107     int da, dr, dg, db;
   108 
   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);
   119   }
   120 
   121 }
   122 OIL_DEFINE_IMPL_REF (colorspace_argb_ref, colorspace_argb);
   123 
   124 
   125 static void
   126 ayuv2argb_u8_ref (uint8_t *argb, const uint8_t *ayuv, int n)
   127 {
   128   int i;
   129 
   130   for(i=0;i<n;i++){
   131     argb[0] = ayuv[0];
   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]);
   135     argb+=4;
   136     ayuv+=4;
   137   }
   138 
   139 }
   140 OIL_DEFINE_IMPL_REF (ayuv2argb_u8_ref, ayuv2argb_u8);
   141 
   142 
   143 
   144 
   145 #ifdef	__SYMBIAN32__
   146  
   147 OilFunctionClass* __oil_function_class_ayuv2argb_u8() {
   148 		return &_oil_function_class_ayuv2argb_u8;
   149 }
   150 #endif
   151 
   152 #ifdef	__SYMBIAN32__
   153  
   154 OilFunctionClass* __oil_function_class_colorspace_argb() {
   155 		return &_oil_function_class_colorspace_argb;
   156 }
   157 #endif
   158 
   159 
   160 
   161 #ifdef	__SYMBIAN32__
   162  
   163 OilFunctionImpl* __oil_function_impl_colorspace_argb_ref() {
   164 		return &_oil_function_impl_colorspace_argb_ref;
   165 }
   166 #endif
   167 
   168 #ifdef	__SYMBIAN32__
   169  
   170 OilFunctionImpl* __oil_function_impl_ayuv2argb_u8_ref() {
   171 		return &_oil_function_impl_ayuv2argb_u8_ref;
   172 }
   173 #endif
   174 
   175 
   176 
   177 #ifdef	__SYMBIAN32__
   178  
   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;
   182 	}
   183 #endif
   184 
   185 #ifdef	__SYMBIAN32__
   186  
   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;
   190 	}
   191 #endif
   192