Subversion Repositories filter_foundry

Rev

Rev 287 | Rev 289 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 287 Rev 288
Line 173... Line 173...
173
        return abs(a-b);
173
        return abs(a-b);
174
}
174
}
175
 
175
 
176
uint16_t gFactoryRndIndexCounter = 0;
176
uint16_t gFactoryRndIndexCounter = 0;
177
uint32_t gFactoryRndLookup[56];
177
uint32_t gFactoryRndLookup[56];
-
 
178
uint32_t gFactoryRndSeed;
-
 
179
uint32_t gFactoryRndSeedSave;
178
 
180
 
179
int32_t factory_rnd(int32_t a, int32_t b) {
181
void factory_fill_rnd_lookup(uint32_t seed);
180
        // Algorithm of Filter Factory
-
 
181
 
182
 
-
 
183
uint32_t factory_rnd(uint32_t a, uint32_t b) {
182
        uint16_t rndCounterA, rndCounterB;
184
        uint16_t rndCounterA, rndCounterB;
183
        uint32_t result;
185
        uint32_t result;
184
        int32_t range;
186
        int32_t range;
185
 
187
 
-
 
188
        if (gFactoryRndSeed != gFactoryRndSeedSave) {
-
 
189
                // (Intentional) behavior of Filter Foundry
-
 
190
                factory_fill_rnd_lookup(gFactoryRndSeed);
-
 
191
                gFactoryRndIndexCounter = 0;
-
 
192
        }
-
 
193
 
-
 
194
        // Algorithm of Filter Factory
-
 
195
 
186
        rndCounterA = gFactoryRndIndexCounter % 55 + 1;
196
        rndCounterA = gFactoryRndIndexCounter % 55 + 1;
187
        rndCounterB = (gFactoryRndIndexCounter + 31) % 55 + 1;
197
        rndCounterB = (gFactoryRndIndexCounter + 31) % 55 + 1;
188
        gFactoryRndIndexCounter = rndCounterA;
198
        gFactoryRndIndexCounter = rndCounterA;
189
 
199
 
190
        result = gFactoryRndLookup[rndCounterA] - gFactoryRndLookup[rndCounterB];
200
        result = gFactoryRndLookup[rndCounterA] - gFactoryRndLookup[rndCounterB];
Line 194... Line 204...
194
        range = b - a;
204
        range = b - a;
195
        if (range < 0) return 0;
205
        if (range < 0) return 0;
196
        return a + (result % (range+1));
206
        return a + (result % (range + 1));
197
}
207
}
198
 
208
 
199
void factory_fill_rnd_lookup(int32_t seed) {
209
void factory_fill_rnd_lookup(uint32_t seed) {
200
        // Algorithm of Filter Factory
210
        // Algorithm of Filter Factory
201
 
211
 
202
        unsigned int i, j;
212
        uint32_t i, j;
203
        unsigned int v1, v2, v3;
213
        uint32_t v1, v2, v3;
204
 
214
 
205
        // 161803398 = 1.61803398 * 10^8 ~= phi * 10^8
215
        // 161803398 = 1.61803398 * 10^8 ~= phi * 10^8
206
        v2 = 161803398 - (seed & 0x7fff);
216
        v2 = 161803398 - (seed & 0x7fff);
207
        gFactoryRndLookup[55] = v2;
217
        gFactoryRndLookup[55] = v2;
208
 
218
 
Line 218... Line 228...
218
                for (j=1; j<=55;) {
228
                for (j=1; j<=55;) {
219
                        gFactoryRndLookup[j++] = gFactoryRndLookup[j] - gFactoryRndLookup[((j + 30) % 55) + 1];
229
                        gFactoryRndLookup[j++] = gFactoryRndLookup[j] - gFactoryRndLookup[((j + 30) % 55) + 1];
220
                }
230
                }
221
        }
231
        }
222
 
232
 
-
 
233
        gFactoryRndSeedSave = seed;
-
 
234
 
223
        return;
235
        return;
224
}
236
}
225
 
237
 
226
int32_t factory_rst(int32_t seed) {
238
int32_t factory_rst(uint32_t seed) {
227
        // We implement rst(i) differently in Filter Foundry:
239
        // We implement rst(i) differently in Filter Foundry:
228
        // Every call of rst() will renew the lookup table,
240
        // Every call of rst() will renew the lookup table,
229
        // while in Filter Factory, there are strange things going
241
        // while in Filter Factory, there are strange things going
230
        // on, like only setting the state in pixel [0,0,0] and
242
        // on, like only setting the state in pixel [0,0,0] and
231
        // only before rnd() is called, etc.
243
        // only before rnd() is called, etc.
232
 
244
 
233
        gFactoryRndIndexCounter = 0;
245
        gFactoryRndSeed = seed;
234
        factory_fill_rnd_lookup(seed);
-
 
235
 
246
 
236
        return 0;
247
        return 0;
237
}
248
}
238
 
249
 
239
void factory_initialize_rnd_variables() {
250
void factory_initialize_rnd_variables() {
240
        const int32_t DEFAULT_SEED = 0;
-
 
241
        gFactoryRndIndexCounter = 0;
251
        gFactoryRndSeed = 0; // default seed
242
        factory_fill_rnd_lookup(DEFAULT_SEED);
252
        gFactoryRndSeedSave = gFactoryRndSeed + 1; // force rnd() to call factory_fill_rnd_lookup()
243
}
253
}
244
 
254
 
245
/* rnd(a,b) Random number between a and b, inclusive */
255
/* rnd(a,b) Random number between a and b, inclusive */
246
value_type ff_rnd(value_type a,value_type b){
256
value_type ff_rnd(value_type a,value_type b){
247
        return factory_rnd(a,b);
257
        return factory_rnd(a,b);