double smooth_noise_firstdim(int integer_x, int integer_y, double fractional_x,int seed) { double v0 = noise(integer_x - 1, integer_y,seed); double v1 = noise(integer_x, integer_y,seed); double v2 = noise(integer_x + 1, integer_y,seed); double v3 = noise(integer_x + 2, integer_y,seed); return cubic_interpolate(v0, v1, v2, v3, fractional_x); }
static double build_pcm(signed short *pcm, int samples, int rate, struct track_t *tr, double position, float pitch, float start_vol, float end_vol) { int s; double sample, step; float vol, gradient; sample = position * tr->rate; step = (double)pitch * tr->rate / rate; vol = start_vol; gradient = (end_vol - start_vol) / samples; for(s = 0; s < samples; s++) { int c, sa, q; float f, i[PLAYER_CHANNELS][4]; /* 4-sample window for interpolation */ sa = (int)sample; if(sample < 0.0) sa--; f = sample - sa; sa--; for(q = 0; q < 4; q++, sa++) { if(sa < 0 || sa >= tr->length) { for(c = 0; c < PLAYER_CHANNELS; c++) i[c][q] = 0.0; } else { signed short *ts; int c; ts = track_get_sample(tr, sa); for(c = 0; c < PLAYER_CHANNELS; c++) i[c][q] = (float)ts[c]; } } for(c = 0; c < PLAYER_CHANNELS; c++) { *pcm++ = vol * cubic_interpolate(i[c], f) + (float)(rand() % 32768) / 32768 - 0.5; /* dither */ } sample += step; vol += gradient; } return (double)pitch * samples / rate; }
double smooth_noise(Vector2D p,int seed){ double x=p.x(); double y=p.y(); int integer_x = (int)x; double fractional_x = x - integer_x; int integer_y = (int)y; double fractional_y = y - integer_y; double t0 = smooth_noise_firstdim(integer_x, integer_y - 1, fractional_x,seed); double t1 = smooth_noise_firstdim(integer_x, integer_y, fractional_x,seed); double t2 = smooth_noise_firstdim(integer_x, integer_y + 1, fractional_x,seed); double t3 = smooth_noise_firstdim(integer_x, integer_y + 2, fractional_x,seed); double resu= cubic_interpolate(t0, t1, t2, t3, fractional_y); return MathUtils::fonctionQuadratique(-0.2,1.2,resu); }
void Pixmap::resample_Y ( unsigned int newHeight ) { Color* newPixels = (Color *)malloc ( sizeof(Color) * m_width * newHeight ); if ( newHeight < m_height ) { float ratio = m_height / (float)newHeight; for ( unsigned int w = 0; w < m_width; ++w ) { unsigned int h = 0; for ( float y = 0.0f; h < newHeight; y += ratio, ++h ) { float from = y; float to = std::min((float)m_height, y + ratio); unsigned int absoluteValue = (unsigned int)floor(from); float fractionaryPart = from-absoluteValue; float pixelAccum [ 4 ] = { 0.0f, 0.0f, 0.0f, 0.0f }; if ( fractionaryPart > 0.000001f ) { float factor = 1.0f - fractionaryPart; Color& color = m_pixels [ m_width*absoluteValue + w ]; pixelAccum[0] += ( color.r() / 255.0f ) * factor; pixelAccum[1] += ( color.g() / 255.0f ) * factor; pixelAccum[2] += ( color.b() / 255.0f ) * factor; pixelAccum[3] += ( color.a() / 255.0f ) * factor; from = static_cast<float>(absoluteValue + 1); } from = floor(from); while ( to-from > 1.0f ) { Color& color = m_pixels [ m_width*(unsigned int)from + w ]; pixelAccum[0] += color.r() / 255.0f; pixelAccum[1] += color.g() / 255.0f; pixelAccum[2] += color.b() / 255.0f; pixelAccum[3] += color.a() / 255.0f; ++from; } if ( fabs(to-from) > 0.000001f ) { float factor = fabs(to-from); Color& color = m_pixels [ m_width*(unsigned int)from + w ]; pixelAccum[0] += (color.r() / 255.0f) * factor; pixelAccum[1] += (color.g() / 255.0f) * factor; pixelAccum[2] += (color.b() / 255.0f) * factor; pixelAccum[3] += (color.a() / 255.0f) * factor; } newPixels [ m_width*h + w ] = Color ( static_cast<unsigned char>(pixelAccum[0] / ratio * 255.0f), static_cast<unsigned char>(pixelAccum[1] / ratio * 255.0f), static_cast<unsigned char>(pixelAccum[2] / ratio * 255.0f), static_cast<unsigned char>(pixelAccum[3] / ratio * 255.0f) ); } } } else if ( newHeight > m_height ) { float ratio = m_height / (float)newHeight; for ( unsigned int w = 0; w < m_width; ++w ) { for ( unsigned int h = 0; h < newHeight; ++h ) { unsigned int up = (unsigned int)floor(h * ratio); unsigned int down = std::min(m_height-1, up+1); unsigned int upPrev = up > 0 ? up-1 : 0; unsigned int downNext = std::min(m_height-1, down+1); Color& u = m_pixels [ up*m_width + w ]; Color& d = m_pixels [ down*m_width + w ]; Color& u_ = m_pixels [ upPrev*m_width + w ]; Color& dn = m_pixels [ downNext*m_width + w ]; float colorValues [ 4 ][ 4 ] = { { u.r() / 255.0f, u.g() / 255.0f, u.b() / 255.0f, u.a() / 255.0f }, { d.r() / 255.0f, d.g() / 255.0f, d.b() / 255.0f, d.a() / 255.0f }, { u_.r() / 255.0f, u_.g() / 255.0f, u_.b() / 255.0f, u_.a() / 255.0f }, { dn.r() / 255.0f, dn.g() / 255.0f, dn.b() / 255.0f, dn.a() / 255.0f } }; float tangents [ 2 ][ 4 ] = { { colorValues[1][0]-colorValues[2][0], colorValues[1][1]-colorValues[2][1], colorValues[1][2]-colorValues[2][2], colorValues[1][3]-colorValues[2][3] }, { colorValues[3][0]-colorValues[0][0], colorValues[3][1]-colorValues[0][1], colorValues[3][2]-colorValues[0][2], colorValues[3][3]-colorValues[0][3] } }; float interpolatedValues [ 4 ]; float alpha = h*ratio; alpha = alpha - floor(alpha); interpolatedValues[0] = clamp(0.0f, cubic_interpolate(colorValues[0][0], tangents[0][0], alpha, colorValues[1][0], tangents[1][0]), 1.0f); interpolatedValues[1] = clamp(0.0f, cubic_interpolate(colorValues[0][1], tangents[0][1], alpha, colorValues[1][1], tangents[1][1]), 1.0f); interpolatedValues[2] = clamp(0.0f, cubic_interpolate(colorValues[0][2], tangents[0][2], alpha, colorValues[1][2], tangents[1][2]), 1.0f); interpolatedValues[3] = clamp(0.0f, cubic_interpolate(colorValues[0][3], tangents[0][3], alpha, colorValues[1][3], tangents[1][3]), 1.0f); newPixels [ h*m_width + w ] = Color ( static_cast<unsigned char>(interpolatedValues[0] * 255.0f), static_cast<unsigned char>(interpolatedValues[1] * 255.0f), static_cast<unsigned char>(interpolatedValues[2] * 255.0f), static_cast<unsigned char>(interpolatedValues[3] * 255.0f) ); } } } free ( m_pixels ); m_pixels = newPixels; m_height = newHeight; }
void Pixmap::resample_X ( unsigned int newWidth ) { Color* newPixels = (Color *)malloc ( sizeof(Color) * newWidth * m_height ); if ( newWidth < m_width ) { float ratio = m_width / (float)newWidth; for ( unsigned int h = 0; h < m_height; ++h ) { unsigned int w = 0; for ( float x = 0.0f; w < newWidth; x += ratio, ++w ) { float from = x; float to = std::min((float)m_width, x + ratio); unsigned int absoluteValue = (unsigned int)floor(from); float fractionaryPart = from-absoluteValue; float pixelAccum [ 4 ] = { 0.0f, 0.0f, 0.0f, 0.0f }; if ( fractionaryPart > 0.000001f ) { float factor = 1.0f - fractionaryPart; Color& color = m_pixels [ m_width*h + absoluteValue ]; pixelAccum[0] += ( color.r() / 255.0f ) * factor; pixelAccum[1] += ( color.g() / 255.0f ) * factor; pixelAccum[2] += ( color.b() / 255.0f ) * factor; pixelAccum[3] += ( color.a() / 255.0f ) * factor; from = static_cast<float>(absoluteValue + 1); } from = floor(from); while ( to-from > 1.0f ) { Color& color = m_pixels [ m_width*h + (unsigned int)from ]; pixelAccum[0] += color.r() / 255.0f; pixelAccum[1] += color.g() / 255.0f; pixelAccum[2] += color.b() / 255.0f; pixelAccum[3] += color.a() / 255.0f; ++from; } if ( fabs(to-from) > 0.000001f ) { float factor = fabs(to-from); Color& color = m_pixels [ m_width*h + (unsigned int)from ]; pixelAccum[0] += (color.r() / 255.0f) * factor; pixelAccum[1] += (color.g() / 255.0f) * factor; pixelAccum[2] += (color.b() / 255.0f) * factor; pixelAccum[3] += (color.a() / 255.0f) * factor; } newPixels [ newWidth*h + w ] = Color ( static_cast<unsigned char>(pixelAccum[0] / ratio * 255.0f), static_cast<unsigned char>(pixelAccum[1] / ratio * 255.0f), static_cast<unsigned char>(pixelAccum[2] / ratio * 255.0f), static_cast<unsigned char>(pixelAccum[3] / ratio * 255.0f) ); } } } else if ( newWidth > m_width ) { float ratio = m_width / (float)newWidth; for ( unsigned int h = 0; h < m_height; ++h ) { for ( unsigned int w = 0; w < newWidth; ++w ) { unsigned int left = (unsigned int)floor(w * ratio); unsigned int right = std::min(m_width-1, left+1); unsigned int leftPrev = left > 0 ? left-1 : 0; unsigned int rightNext = std::min(m_width-1, right+1); Color& l = m_pixels [ h*m_width + left ]; Color& r = m_pixels [ h*m_width + right ]; Color& lp = m_pixels [ h*m_width + leftPrev ]; Color& rn = m_pixels [ h*m_width + rightNext ]; float colorValues [ 4 ][ 4 ] = { { l.r() / 255.0f, l.g() / 255.0f, l.b() / 255.0f, l.a() / 255.0f }, { r.r() / 255.0f, r.g() / 255.0f, r.b() / 255.0f, r.a() / 255.0f }, { lp.r() / 255.0f, lp.g() / 255.0f, lp.b() / 255.0f, lp.a() / 255.0f }, { rn.r() / 255.0f, rn.g() / 255.0f, rn.b() / 255.0f, rn.a() / 255.0f } }; float tangents [ 2 ][ 4 ] = { { colorValues[1][0]-colorValues[2][0], colorValues[1][1]-colorValues[2][1], colorValues[1][2]-colorValues[2][2], colorValues[1][3]-colorValues[2][3] }, { colorValues[3][0]-colorValues[0][0], colorValues[3][1]-colorValues[0][1], colorValues[3][2]-colorValues[0][2], colorValues[3][3]-colorValues[0][3] } }; float interpolatedValues [ 4 ]; float alpha = w*ratio; alpha = alpha - floor(alpha); interpolatedValues[0] = clamp(0.0f, cubic_interpolate(colorValues[0][0], tangents[0][0], alpha, colorValues[1][0], tangents[1][0]), 1.0f); interpolatedValues[1] = clamp(0.0f, cubic_interpolate(colorValues[0][1], tangents[0][1], alpha, colorValues[1][1], tangents[1][1]), 1.0f); interpolatedValues[2] = clamp(0.0f, cubic_interpolate(colorValues[0][2], tangents[0][2], alpha, colorValues[1][2], tangents[1][2]), 1.0f); interpolatedValues[3] = clamp(0.0f, cubic_interpolate(colorValues[0][3], tangents[0][3], alpha, colorValues[1][3], tangents[1][3]), 1.0f); newPixels [ h*newWidth + w ] = Color ( static_cast<unsigned char>(interpolatedValues[0] * 255.0f), static_cast<unsigned char>(interpolatedValues[1] * 255.0f), static_cast<unsigned char>(interpolatedValues[2] * 255.0f), static_cast<unsigned char>(interpolatedValues[3] * 255.0f) ); } } } free ( m_pixels ); m_pixels = newPixels; m_width = newWidth; }