Skip to content

Commit

Permalink
Generate noise in a saner way for 9-16 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
HolyWu committed Aug 22, 2014
1 parent 04198ed commit 8f272c4
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions AddGrain/AddGrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ struct AddGrainData {
long long idum;
std::vector<uint8_t> pNoiseSeeds;
// increase this to lessen inter-frame noise coherence and increase memory
std::vector<int8_t> pN[MAXP];
std::vector<int16_t> pN[MAXP];
int nPitch[MAXP], nHeight[MAXP], nSize[MAXP];
bool process[3];
};

template<typename T1, typename T2>
static void UpdateFrame(T1 *dstp, int width, int height, int stride, int plane, int noiseOffs, const AddGrainData *d) {
const int8_t * pNW2 = &(d->pN[plane][noiseOffs]);
const int16_t * pNW2 = &(d->pN[plane][noiseOffs]);
const int noisePitch2 = d->nPitch[plane];
assert(noiseOffs + noisePitch2 * (height - 1) + width <= d->nSize[plane]);
const unsigned shift1 = sizeof(T1) == 1 ? 0 : 16 - d->vi->format->bitsPerSample;
const int shift2 = sizeof(T1) == 1 ? 1 : 257;
const int lower = sizeof(T1) == 1 ? -128 : -32768;
const int upper = sizeof(T1) == 1 ? 127 : 32767;
const int shift2 = sizeof(T1) == 1 ? 8 : 0;
const int lower = sizeof(T1) == 1 ? SCHAR_MIN : SHRT_MIN;
const int upper = sizeof(T1) == 1 ? SCHAR_MAX : SHRT_MAX;

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
T2 val = (dstp[x] << shift1) ^ lower;
T2 tmp = pNW2[x] * shift2;
T2 tmp = pNW2[x] >> shift2;
val = val + tmp < lower ? lower : (val + tmp > upper ? upper : val + tmp);
dstp[x] = val ^ lower;
dstp[x] >>= shift1;
Expand Down Expand Up @@ -292,17 +292,16 @@ static void VS_CC addgrainCreate(const VSMap *in, VSMap *out, void *userData, VS
for (int x = 0; x < d.nPitch[plane]; x++)
lastLine[x] = GaussianRand(mean, pvar[plane], &d); // things to vertically smooth against
for (int y = 0; y < h; y++) {
std::vector<int8_t>::iterator pNW = d.pN[plane].begin() + y * d.nPitch[plane];
std::vector<int16_t>::iterator pNW = d.pN[plane].begin() + y * d.nPitch[plane];
float lastr = GaussianRand(mean, pvar[plane], &d); // something to horiz smooth against
for (int x = 0; x < d.nPitch[plane]; x++) {
float r = GaussianRand(mean, pvar[plane], &d);
r = lastr * d.hcorr + r * (1.f - d.hcorr); // horizontal correlation
lastr = r;
r = lastLine[x] * d.vcorr + r * (1.f - d.vcorr); // vert corr
lastLine[x] = r;

// set noise block
*pNW++ = (int8_t)floor(r + .5f); // round to nearest
*pNW++ = (int16_t)std::round(r * 256.f);
}
}
for (int x = d.storedFrames; x > 0; x--)
Expand Down

0 comments on commit 8f272c4

Please sign in to comment.