static void interppixel_NDC_clamped (const ImageBuf &buf, float x, float y, float *pixel, bool envlatlmode) { int fx = buf.spec().full_x; int fy = buf.spec().full_y; int fw = buf.spec().full_width; int fh = buf.spec().full_height; x = static_cast<float>(fx) + x * static_cast<float>(fw); y = static_cast<float>(fy) + y * static_cast<float>(fh); const int maxchannels = 64; // Reasonable guess float p[4][maxchannels]; DASSERT (buf.spec().nchannels <= maxchannels && "You need to increase maxchannels"); int n = std::min (buf.spec().nchannels, maxchannels); x -= 0.5f; y -= 0.5f; int xtexel, ytexel; float xfrac, yfrac; xfrac = floorfrac (x, &xtexel); yfrac = floorfrac (y, &ytexel); // Clamp int xnext = Imath::clamp (xtexel+1, buf.xmin(), buf.xmax()); int ynext = Imath::clamp (ytexel+1, buf.ymin(), buf.ymax()); xnext = Imath::clamp (xnext, buf.xmin(), buf.xmax()); ynext = Imath::clamp (ynext, buf.ymin(), buf.ymax()); // Get the four texels buf.getpixel (xtexel, ytexel, p[0], n); buf.getpixel (xnext, ytexel, p[1], n); buf.getpixel (xtexel, ynext, p[2], n); buf.getpixel (xnext, ynext, p[3], n); if (envlatlmode) { // For latlong environment maps, in order to conserve energy, we // must weight the pixels by sin(t*PI) because pixels closer to // the pole are actually less area on the sphere. Doing this // wrong will tend to over-represent the high latitudes in // low-res MIP levels. We fold the area weighting into our // linear interpolation by adjusting yfrac. float w0 = (1.0f - yfrac) * sinf ((float)M_PI * (ytexel+0.5f)/(float)fh); float w1 = yfrac * sinf ((float)M_PI * (ynext+0.5f)/(float)fh); yfrac = w0 / (w0 + w1); } // Bilinearly interpolate bilerp (p[0], p[1], p[2], p[3], xfrac, yfrac, n, pixel); }
void ImageBuf::interppixel (float x, float y, float *pixel) const { const int maxchannels = 64; // Reasonable guess float p[4][maxchannels]; DASSERT (spec().nchannels <= maxchannels && "You need to increase maxchannels in ImageBuf::interppixel"); int n = std::min (spec().nchannels, maxchannels); x -= 0.5f; y -= 0.5f; int xtexel, ytexel; float xfrac, yfrac; xfrac = floorfrac (x, &xtexel); yfrac = floorfrac (y, &ytexel); getpixel (xtexel, ytexel, p[0], n); getpixel (xtexel+1, ytexel, p[1], n); getpixel (xtexel, ytexel+1, p[2], n); getpixel (xtexel+1, ytexel+1, p[3], n); bilerp (p[0], p[1], p[2], p[3], xfrac, yfrac, n, pixel); }
static bool resample_ (ImageBuf &dst, const ImageBuf &src, bool interpolate, ROI roi, int nthreads) { if (nthreads != 1 && roi.npixels() >= 1000) { // Lots of pixels and request for multi threads? Parallelize. ImageBufAlgo::parallel_image ( boost::bind(resample_<DSTTYPE,SRCTYPE>, boost::ref(dst), boost::cref(src), interpolate, _1 /*roi*/, 1 /*nthreads*/), roi, nthreads); return true; } // Serial case const ImageSpec &srcspec (src.spec()); const ImageSpec &dstspec (dst.spec()); int nchannels = src.nchannels(); // Local copies of the source image window, converted to float float srcfx = srcspec.full_x; float srcfy = srcspec.full_y; float srcfw = srcspec.full_width; float srcfh = srcspec.full_height; float dstfx = dstspec.full_x; float dstfy = dstspec.full_y; float dstfw = dstspec.full_width; float dstfh = dstspec.full_height; float dstpixelwidth = 1.0f / dstfw; float dstpixelheight = 1.0f / dstfh; float *pel = ALLOCA (float, nchannels); ImageBuf::Iterator<DSTTYPE> out (dst, roi); ImageBuf::ConstIterator<SRCTYPE> srcpel (src); for (int y = roi.ybegin; y < roi.yend; ++y) { // s,t are NDC space float t = (y-dstfy+0.5f)*dstpixelheight; // src_xf, src_xf are image space float coordinates float src_yf = srcfy + t * srcfh - 0.5f; // src_x, src_y are image space integer coordinates of the floor int src_y; (void) floorfrac (src_yf, &src_y); for (int x = roi.xbegin; x < roi.xend; ++x) { float s = (x-dstfx+0.5f)*dstpixelwidth; float src_xf = srcfx + s * srcfw - 0.5f; int src_x; (void) floorfrac (src_xf, &src_x); if (interpolate) { src.interppixel (src_xf, src_yf, pel); for (int c = roi.chbegin; c < roi.chend; ++c) out[c] = pel[c]; } else { srcpel.pos (src_x, src_y, 0); for (int c = roi.chbegin; c < roi.chend; ++c) out[c] = srcpel[c]; } ++out; } } return true; }
static bool resize_ (ImageBuf &dst, const ImageBuf &src, Filter2D *filter, ROI roi, int nthreads) { if (nthreads != 1 && roi.npixels() >= 1000) { // Lots of pixels and request for multi threads? Parallelize. ImageBufAlgo::parallel_image ( boost::bind(resize_<DSTTYPE,SRCTYPE>, boost::ref(dst), boost::cref(src), filter, _1 /*roi*/, 1 /*nthreads*/), roi, nthreads); return true; } // Serial case const ImageSpec &srcspec (src.spec()); const ImageSpec &dstspec (dst.spec()); int nchannels = dstspec.nchannels; // Local copies of the source image window, converted to float float srcfx = srcspec.full_x; float srcfy = srcspec.full_y; float srcfw = srcspec.full_width; float srcfh = srcspec.full_height; // Ratios of dst/src size. Values larger than 1 indicate that we // are maximizing (enlarging the image), and thus want to smoothly // interpolate. Values less than 1 indicate that we are minimizing // (shrinking the image), and thus want to properly filter out the // high frequencies. float xratio = float(dstspec.full_width) / srcfw; // 2 upsize, 0.5 downsize float yratio = float(dstspec.full_height) / srcfh; float dstfx = dstspec.full_x; float dstfy = dstspec.full_y; float dstfw = dstspec.full_width; float dstfh = dstspec.full_height; float dstpixelwidth = 1.0f / dstfw; float dstpixelheight = 1.0f / dstfh; float *pel = ALLOCA (float, nchannels); float filterrad = filter->width() / 2.0f; // radi,radj is the filter radius, as an integer, in source pixels. We // will filter the source over [x-radi, x+radi] X [y-radj,y+radj]. int radi = (int) ceilf (filterrad/xratio); int radj = (int) ceilf (filterrad/yratio); int xtaps = 2*radi + 1; int ytaps = 2*radj + 1; bool separable = filter->separable(); float *xfiltval = NULL, *yfiltval = NULL; if (separable) { // Allocate temp space to cache the filter weights xfiltval = ALLOCA (float, xtaps); yfiltval = ALLOCA (float, ytaps); } #if 0 std::cerr << "Resizing " << srcspec.full_width << "x" << srcspec.full_height << " to " << dstspec.full_width << "x" << dstspec.full_height << "\n"; std::cerr << "ratios = " << xratio << ", " << yratio << "\n"; std::cerr << "examining src filter support radius of " << radi << " x " << radj << " pixels\n"; std::cerr << "dst range " << roi << "\n"; std::cerr << "separable filter\n"; #endif // We're going to loop over all output pixels we're interested in. // // (s,t) = NDC space coordinates of the output sample we are computing. // This is the "sample point". // (src_xf, src_xf) = source pixel space float coordinates of the // sample we're computing. We want to compute the weighted sum // of all the source image pixels that fall under the filter when // centered at that location. // (src_x, src_y) = image space integer coordinates of the floor, // i.e., the closest pixel in the source image. // src_xf_frac and src_yf_frac are the position within that pixel // of our sample. ImageBuf::Iterator<DSTTYPE> out (dst, roi); for (int y = roi.ybegin; y < roi.yend; ++y) { float t = (y-dstfy+0.5f)*dstpixelheight; float src_yf = srcfy + t * srcfh; int src_y; float src_yf_frac = floorfrac (src_yf, &src_y); // If using separable filters, our vertical set of filter tap // weights will be the same for the whole scanline we're on. Just // compute and normalize them once. float totalweight_y = 0.0f; if (separable) { for (int j = 0; j < ytaps; ++j) { float w = filter->yfilt (yratio * (j-radj-(src_yf_frac-0.5f))); yfiltval[j] = w; totalweight_y += w; } for (int i = 0; i <= ytaps; ++i) yfiltval[i] /= totalweight_y; } for (int x = roi.xbegin; x < roi.xend; ++x) { float s = (x-dstfx+0.5f)*dstpixelwidth; float src_xf = srcfx + s * srcfw; int src_x; float src_xf_frac = floorfrac (src_xf, &src_x); for (int c = 0; c < nchannels; ++c) pel[c] = 0.0f; if (separable) { // Cache and normalize the horizontal filter tap weights // just once for this (x,y) position, reuse for all vertical // taps. float totalweight_x = 0.0f; for (int i = 0; i < xtaps; ++i) { float w = filter->xfilt (xratio * (i-radi-(src_xf_frac-0.5f))); xfiltval[i] = w; totalweight_x += w; } if (totalweight_x != 0.0f) { for (int i = 0; i < xtaps; ++i) // normalize x filter xfiltval[i] /= totalweight_x; // weights ImageBuf::ConstIterator<SRCTYPE> srcpel (src, src_x-radi, src_x+radi+1, src_y-radj, src_y+radj+1, 0, 1, ImageBuf::WrapClamp); for (int j = -radj; j <= radj; ++j) { float wy = yfiltval[j+radj]; if (wy == 0.0f) { // 0 weight for this y tap -- move to next line srcpel.pos (srcpel.x(), srcpel.y()+1, srcpel.z()); continue; } for (int i = 0; i < xtaps; ++i, ++srcpel) { float w = wy * xfiltval[i]; for (int c = 0; c < nchannels; ++c) pel[c] += w * srcpel[c]; } } } // Copy the pixel value (already normalized) to the output. DASSERT (out.x() == x && out.y() == y); if (totalweight_y == 0.0f) { // zero it out for (int c = 0; c < nchannels; ++c) out[c] = 0.0f; } else { for (int c = 0; c < nchannels; ++c) out[c] = pel[c]; } } else { // Non-separable float totalweight = 0.0f; ImageBuf::ConstIterator<SRCTYPE> srcpel (src, src_x-radi, src_x+radi+1, src_y-radi, src_y+radi+1, 0, 1, ImageBuf::WrapClamp); for (int j = -radj; j <= radj; ++j) { for (int i = -radi; i <= radi; ++i, ++srcpel) { float w = (*filter)(xratio * (i-(src_xf_frac-0.5f)), yratio * (j-(src_yf_frac-0.5f))); totalweight += w; if (w == 0.0f) continue; DASSERT (! srcpel.done()); for (int c = 0; c < nchannels; ++c) pel[c] += w * srcpel[c]; } } DASSERT (srcpel.done()); // Rescale pel to normalize the filter and write it to the // output image. DASSERT (out.x() == x && out.y() == y); if (totalweight == 0.0f) { // zero it out for (int c = 0; c < nchannels; ++c) out[c] = 0.0f; } else { for (int c = 0; c < nchannels; ++c) out[c] = pel[c] / totalweight; } } ++out; } } return true; }