sl@0: /*
sl@0:  * LIBOIL - Library of Optimized Inner Loops
sl@0:  * Copyright (c) 2005 David A. Schleef <ds@schleef.org>
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms, with or without
sl@0:  * modification, are permitted provided that the following conditions
sl@0:  * are met:
sl@0:  * 1. Redistributions of source code must retain the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer.
sl@0:  * 2. Redistributions in binary form must reproduce the above copyright
sl@0:  *    notice, this list of conditions and the following disclaimer in the
sl@0:  *    documentation and/or other materials provided with the distribution.
sl@0:  * 
sl@0:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
sl@0:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
sl@0:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
sl@0:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
sl@0:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
sl@0:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
sl@0:  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sl@0:  * POSSIBILITY OF SUCH DAMAGE.
sl@0:  */
sl@0: 
sl@0: #ifdef HAVE_CONFIG_H
sl@0: #include "config.h"
sl@0: #endif
sl@0: 
sl@0: #include <liboil/liboilrandom.h>
sl@0: #include "liboil/liboilcolorspace.h"
sl@0: #include <stdlib.h>
sl@0: 
sl@0: 
sl@0: /**
sl@0:  * SECTION:liboilrandom
sl@0:  * @title: Random Number Generation
sl@0:  * @short_description: Random number generation
sl@0:  */
sl@0: 
sl@0: static void
sl@0: _oil_random_bits (void *dest, int n)
sl@0: {
sl@0:   int i;
sl@0:   uint8_t *d = dest;
sl@0:   for(i=0;i<n;i++){
sl@0:     d[i] = (rand()>>16);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_s32:
sl@0:  * @dest:
sl@0:  * @n:
sl@0:  *
sl@0:  * Writes random values in the range [-(1<<31), (1<<31)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_s32(oil_type_s32 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n*4);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_s64:
sl@0:  *
sl@0:  * Writes random values in the range [-(1<<63), (1<<63)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_s64 (oil_type_s64 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n*8);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_s16:
sl@0:  *
sl@0:  * Writes random values in the range [-(1<<15), (1<<15)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_s16 (oil_type_s16 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n*2);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_s8:
sl@0:  *
sl@0:  * Writes random values in the range [-(1<<7), (1<<7)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_s8 (oil_type_s8 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_u32:
sl@0:  *
sl@0:  * Writes random values in the range [0, (1<<32)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_u32 (oil_type_u32 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n*4);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_u64:
sl@0:  *
sl@0:  * Writes random values in the range [0, (1<<64)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_u64 (oil_type_u64 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n*8);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_u16:
sl@0:  *
sl@0:  * Writes random values in the range [0, (1<<16)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_u16 (oil_type_u16 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n*2);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_u8:
sl@0:  *
sl@0:  * Writes random values in the range [0, (1<<8)-1] to the
sl@0:  * destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_u8 (oil_type_u8 *dest, int n)
sl@0: {
sl@0:   _oil_random_bits (dest, n);
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_f64:
sl@0:  *
sl@0:  * Writes random double-precision floating point values in the
sl@0:  * range [0, 1.0) to the destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_f64 (oil_type_f64 *dest, int n)
sl@0: {
sl@0:   int i;
sl@0:   for(i=0;i<n;i++){
sl@0:     dest[i] = (((rand()/(RAND_MAX+1.0))+rand())/(RAND_MAX+1.0));
sl@0:   }
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_f32:
sl@0:  *
sl@0:  * Writes random single-precision floating point values in the
sl@0:  * range [0, 1.0) to the destination array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_f32 (oil_type_f32 *dest, int n)
sl@0: {
sl@0:   int i;
sl@0:   for(i=0;i<n;i++){
sl@0:     dest[i] = (rand()/(RAND_MAX+1.0));
sl@0:   }
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_alpha:
sl@0:  *
sl@0:  * Writes random values in the range [0, 255] to the destination
sl@0:  * array suitable for alpha values.  This is similar to oil_random_u8(),
sl@0:  * except the values 0 and 255 are strongly favored.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_alpha(uint8_t *dest, int n)
sl@0: {
sl@0:   int i;
sl@0:   int x;
sl@0:   for(i=0;i<n;i++){
sl@0:     x = ((rand()>>8) & 0x1ff) - 0x80;
sl@0:     if (x<0) x = 0;
sl@0:     if (x>255) x = 255;
sl@0:     dest[i] = x;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /**
sl@0:  * oil_random_argb:
sl@0:  * @dest: destination array.
sl@0:  * @n: number of values to write.
sl@0:  *
sl@0:  * Creates valid random RGBA values and places them in the destination
sl@0:  * array.
sl@0:  */
sl@0: #ifdef __SYMBIAN32__
sl@0: EXPORT_C
sl@0: #endif
sl@0: void
sl@0: oil_random_argb(uint32_t *dest, int n)
sl@0: {
sl@0:   int i;
sl@0:   int x;
sl@0:   for(i=0;i<n;i++){
sl@0:     x = ((rand()>>8) & 0x1ff) - 0x80;
sl@0:     if (x<0) x = 0;
sl@0:     if (x>255) x = 255;
sl@0:     dest[i] = oil_argb_noclamp(x,
sl@0:         oil_muldiv_255(x,oil_rand_u8()),
sl@0:         oil_muldiv_255(x,oil_rand_u8()),
sl@0:         oil_muldiv_255(x,oil_rand_u8()));
sl@0:   }
sl@0: 
sl@0: }
sl@0: 
sl@0: