sl@0
|
1 |
/*
|
sl@0
|
2 |
* LIBOIL - Library of Optimized Inner Loops
|
sl@0
|
3 |
* Copyright (c) 2005 David A. Schleef <ds@schleef.org>
|
sl@0
|
4 |
* All rights reserved.
|
sl@0
|
5 |
*
|
sl@0
|
6 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
7 |
* modification, are permitted provided that the following conditions
|
sl@0
|
8 |
* are met:
|
sl@0
|
9 |
* 1. Redistributions of source code must retain the above copyright
|
sl@0
|
10 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
11 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
13 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
14 |
*
|
sl@0
|
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
sl@0
|
16 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
sl@0
|
17 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
sl@0
|
19 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
sl@0
|
20 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
sl@0
|
21 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
sl@0
|
23 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
sl@0
|
24 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
sl@0
|
25 |
* POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
26 |
*/
|
sl@0
|
27 |
|
sl@0
|
28 |
#ifdef HAVE_CONFIG_H
|
sl@0
|
29 |
#include "config.h"
|
sl@0
|
30 |
#endif
|
sl@0
|
31 |
|
sl@0
|
32 |
#include <liboil/liboilrandom.h>
|
sl@0
|
33 |
#include "liboil/liboilcolorspace.h"
|
sl@0
|
34 |
#include <stdlib.h>
|
sl@0
|
35 |
|
sl@0
|
36 |
|
sl@0
|
37 |
/**
|
sl@0
|
38 |
* SECTION:liboilrandom
|
sl@0
|
39 |
* @title: Random Number Generation
|
sl@0
|
40 |
* @short_description: Random number generation
|
sl@0
|
41 |
*/
|
sl@0
|
42 |
|
sl@0
|
43 |
static void
|
sl@0
|
44 |
_oil_random_bits (void *dest, int n)
|
sl@0
|
45 |
{
|
sl@0
|
46 |
int i;
|
sl@0
|
47 |
uint8_t *d = dest;
|
sl@0
|
48 |
for(i=0;i<n;i++){
|
sl@0
|
49 |
d[i] = (rand()>>16);
|
sl@0
|
50 |
}
|
sl@0
|
51 |
}
|
sl@0
|
52 |
|
sl@0
|
53 |
/**
|
sl@0
|
54 |
* oil_random_s32:
|
sl@0
|
55 |
* @dest:
|
sl@0
|
56 |
* @n:
|
sl@0
|
57 |
*
|
sl@0
|
58 |
* Writes random values in the range [-(1<<31), (1<<31)-1] to the
|
sl@0
|
59 |
* destination array.
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
#ifdef __SYMBIAN32__
|
sl@0
|
62 |
EXPORT_C
|
sl@0
|
63 |
#endif
|
sl@0
|
64 |
void
|
sl@0
|
65 |
oil_random_s32(oil_type_s32 *dest, int n)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
_oil_random_bits (dest, n*4);
|
sl@0
|
68 |
}
|
sl@0
|
69 |
|
sl@0
|
70 |
/**
|
sl@0
|
71 |
* oil_random_s64:
|
sl@0
|
72 |
*
|
sl@0
|
73 |
* Writes random values in the range [-(1<<63), (1<<63)-1] to the
|
sl@0
|
74 |
* destination array.
|
sl@0
|
75 |
*/
|
sl@0
|
76 |
#ifdef __SYMBIAN32__
|
sl@0
|
77 |
EXPORT_C
|
sl@0
|
78 |
#endif
|
sl@0
|
79 |
void
|
sl@0
|
80 |
oil_random_s64 (oil_type_s64 *dest, int n)
|
sl@0
|
81 |
{
|
sl@0
|
82 |
_oil_random_bits (dest, n*8);
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
/**
|
sl@0
|
86 |
* oil_random_s16:
|
sl@0
|
87 |
*
|
sl@0
|
88 |
* Writes random values in the range [-(1<<15), (1<<15)-1] to the
|
sl@0
|
89 |
* destination array.
|
sl@0
|
90 |
*/
|
sl@0
|
91 |
#ifdef __SYMBIAN32__
|
sl@0
|
92 |
EXPORT_C
|
sl@0
|
93 |
#endif
|
sl@0
|
94 |
void
|
sl@0
|
95 |
oil_random_s16 (oil_type_s16 *dest, int n)
|
sl@0
|
96 |
{
|
sl@0
|
97 |
_oil_random_bits (dest, n*2);
|
sl@0
|
98 |
}
|
sl@0
|
99 |
|
sl@0
|
100 |
/**
|
sl@0
|
101 |
* oil_random_s8:
|
sl@0
|
102 |
*
|
sl@0
|
103 |
* Writes random values in the range [-(1<<7), (1<<7)-1] to the
|
sl@0
|
104 |
* destination array.
|
sl@0
|
105 |
*/
|
sl@0
|
106 |
#ifdef __SYMBIAN32__
|
sl@0
|
107 |
EXPORT_C
|
sl@0
|
108 |
#endif
|
sl@0
|
109 |
void
|
sl@0
|
110 |
oil_random_s8 (oil_type_s8 *dest, int n)
|
sl@0
|
111 |
{
|
sl@0
|
112 |
_oil_random_bits (dest, n);
|
sl@0
|
113 |
}
|
sl@0
|
114 |
|
sl@0
|
115 |
/**
|
sl@0
|
116 |
* oil_random_u32:
|
sl@0
|
117 |
*
|
sl@0
|
118 |
* Writes random values in the range [0, (1<<32)-1] to the
|
sl@0
|
119 |
* destination array.
|
sl@0
|
120 |
*/
|
sl@0
|
121 |
#ifdef __SYMBIAN32__
|
sl@0
|
122 |
EXPORT_C
|
sl@0
|
123 |
#endif
|
sl@0
|
124 |
void
|
sl@0
|
125 |
oil_random_u32 (oil_type_u32 *dest, int n)
|
sl@0
|
126 |
{
|
sl@0
|
127 |
_oil_random_bits (dest, n*4);
|
sl@0
|
128 |
}
|
sl@0
|
129 |
|
sl@0
|
130 |
/**
|
sl@0
|
131 |
* oil_random_u64:
|
sl@0
|
132 |
*
|
sl@0
|
133 |
* Writes random values in the range [0, (1<<64)-1] to the
|
sl@0
|
134 |
* destination array.
|
sl@0
|
135 |
*/
|
sl@0
|
136 |
#ifdef __SYMBIAN32__
|
sl@0
|
137 |
EXPORT_C
|
sl@0
|
138 |
#endif
|
sl@0
|
139 |
void
|
sl@0
|
140 |
oil_random_u64 (oil_type_u64 *dest, int n)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
_oil_random_bits (dest, n*8);
|
sl@0
|
143 |
}
|
sl@0
|
144 |
|
sl@0
|
145 |
/**
|
sl@0
|
146 |
* oil_random_u16:
|
sl@0
|
147 |
*
|
sl@0
|
148 |
* Writes random values in the range [0, (1<<16)-1] to the
|
sl@0
|
149 |
* destination array.
|
sl@0
|
150 |
*/
|
sl@0
|
151 |
#ifdef __SYMBIAN32__
|
sl@0
|
152 |
EXPORT_C
|
sl@0
|
153 |
#endif
|
sl@0
|
154 |
void
|
sl@0
|
155 |
oil_random_u16 (oil_type_u16 *dest, int n)
|
sl@0
|
156 |
{
|
sl@0
|
157 |
_oil_random_bits (dest, n*2);
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
/**
|
sl@0
|
161 |
* oil_random_u8:
|
sl@0
|
162 |
*
|
sl@0
|
163 |
* Writes random values in the range [0, (1<<8)-1] to the
|
sl@0
|
164 |
* destination array.
|
sl@0
|
165 |
*/
|
sl@0
|
166 |
#ifdef __SYMBIAN32__
|
sl@0
|
167 |
EXPORT_C
|
sl@0
|
168 |
#endif
|
sl@0
|
169 |
void
|
sl@0
|
170 |
oil_random_u8 (oil_type_u8 *dest, int n)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
_oil_random_bits (dest, n);
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
/**
|
sl@0
|
176 |
* oil_random_f64:
|
sl@0
|
177 |
*
|
sl@0
|
178 |
* Writes random double-precision floating point values in the
|
sl@0
|
179 |
* range [0, 1.0) to the destination array.
|
sl@0
|
180 |
*/
|
sl@0
|
181 |
#ifdef __SYMBIAN32__
|
sl@0
|
182 |
EXPORT_C
|
sl@0
|
183 |
#endif
|
sl@0
|
184 |
void
|
sl@0
|
185 |
oil_random_f64 (oil_type_f64 *dest, int n)
|
sl@0
|
186 |
{
|
sl@0
|
187 |
int i;
|
sl@0
|
188 |
for(i=0;i<n;i++){
|
sl@0
|
189 |
dest[i] = (((rand()/(RAND_MAX+1.0))+rand())/(RAND_MAX+1.0));
|
sl@0
|
190 |
}
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
/**
|
sl@0
|
194 |
* oil_random_f32:
|
sl@0
|
195 |
*
|
sl@0
|
196 |
* Writes random single-precision floating point values in the
|
sl@0
|
197 |
* range [0, 1.0) to the destination array.
|
sl@0
|
198 |
*/
|
sl@0
|
199 |
#ifdef __SYMBIAN32__
|
sl@0
|
200 |
EXPORT_C
|
sl@0
|
201 |
#endif
|
sl@0
|
202 |
void
|
sl@0
|
203 |
oil_random_f32 (oil_type_f32 *dest, int n)
|
sl@0
|
204 |
{
|
sl@0
|
205 |
int i;
|
sl@0
|
206 |
for(i=0;i<n;i++){
|
sl@0
|
207 |
dest[i] = (rand()/(RAND_MAX+1.0));
|
sl@0
|
208 |
}
|
sl@0
|
209 |
}
|
sl@0
|
210 |
|
sl@0
|
211 |
/**
|
sl@0
|
212 |
* oil_random_alpha:
|
sl@0
|
213 |
*
|
sl@0
|
214 |
* Writes random values in the range [0, 255] to the destination
|
sl@0
|
215 |
* array suitable for alpha values. This is similar to oil_random_u8(),
|
sl@0
|
216 |
* except the values 0 and 255 are strongly favored.
|
sl@0
|
217 |
*/
|
sl@0
|
218 |
#ifdef __SYMBIAN32__
|
sl@0
|
219 |
EXPORT_C
|
sl@0
|
220 |
#endif
|
sl@0
|
221 |
void
|
sl@0
|
222 |
oil_random_alpha(uint8_t *dest, int n)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
int i;
|
sl@0
|
225 |
int x;
|
sl@0
|
226 |
for(i=0;i<n;i++){
|
sl@0
|
227 |
x = ((rand()>>8) & 0x1ff) - 0x80;
|
sl@0
|
228 |
if (x<0) x = 0;
|
sl@0
|
229 |
if (x>255) x = 255;
|
sl@0
|
230 |
dest[i] = x;
|
sl@0
|
231 |
}
|
sl@0
|
232 |
}
|
sl@0
|
233 |
|
sl@0
|
234 |
/**
|
sl@0
|
235 |
* oil_random_argb:
|
sl@0
|
236 |
* @dest: destination array.
|
sl@0
|
237 |
* @n: number of values to write.
|
sl@0
|
238 |
*
|
sl@0
|
239 |
* Creates valid random RGBA values and places them in the destination
|
sl@0
|
240 |
* array.
|
sl@0
|
241 |
*/
|
sl@0
|
242 |
#ifdef __SYMBIAN32__
|
sl@0
|
243 |
EXPORT_C
|
sl@0
|
244 |
#endif
|
sl@0
|
245 |
void
|
sl@0
|
246 |
oil_random_argb(uint32_t *dest, int n)
|
sl@0
|
247 |
{
|
sl@0
|
248 |
int i;
|
sl@0
|
249 |
int x;
|
sl@0
|
250 |
for(i=0;i<n;i++){
|
sl@0
|
251 |
x = ((rand()>>8) & 0x1ff) - 0x80;
|
sl@0
|
252 |
if (x<0) x = 0;
|
sl@0
|
253 |
if (x>255) x = 255;
|
sl@0
|
254 |
dest[i] = oil_argb_noclamp(x,
|
sl@0
|
255 |
oil_muldiv_255(x,oil_rand_u8()),
|
sl@0
|
256 |
oil_muldiv_255(x,oil_rand_u8()),
|
sl@0
|
257 |
oil_muldiv_255(x,oil_rand_u8()));
|
sl@0
|
258 |
}
|
sl@0
|
259 |
|
sl@0
|
260 |
}
|
sl@0
|
261 |
|
sl@0
|
262 |
|