void R2Image:: Multiply(const R2Image& image) { // Multiply by image pixel-by-pixel assert(image.Width() == Width()); assert(image.Height() == Height()); assert(image.NChannels() == NChannels()); int nvalues = NValues(); for (int i = 0; i < nvalues; i++) { pixels[i] *= image.pixels[i]; } }
void R2Image:: Subtract(const R2Image& image) { // Subtract image pixel-by-pixel assert(image.Width() == Width()); assert(image.Height() == Height()); assert(image.NChannels() == NChannels()); int nvalues = NValues(); for (int i = 0; i < nvalues; i++) { pixels[i] -= image.pixels[i]; } }
void R2Image:: Divide(const R2Image& image) { // Add image pixel-by-pixel assert(image.Width() == Width()); assert(image.Height() == Height()); assert(image.NChannels() == NChannels()); int nvalues = NValues(); for (int i = 0; i < nvalues; i++) { if (image.pixels[i] == 0) { if (pixels[i] == 0.0) continue; else pixels[i] = FLT_MAX; } else { pixels[i] /= image.pixels[i]; } } }
void R2Image:: Filter(const R2Image& filter) { // Get useful variables int xr = filter.Width()/2; int yr = filter.Height()/2; R2Image copy(*this); // This is the straight-forward implementation (slow for large filters) for (int j = 0; j < Height(); j++) { for (int i = 0; i < Width(); i++) { for (int c = 0; c < NChannels(); c++) { int fc = (NChannels() == filter.NChannels()) ? c : 0; // Compute new value double sum = 0; for (int s = 0; s < filter.Width(); s++) { int x = i - xr + s; if (x < 0) x = 0; else if (x >= Width()) x = Width()-1; for (int t = 0; t < filter.Height(); t++) { int y = j - yr + t; if (y < 0) y = 0; else if (y >= Height()) y = Height()-1; double filter_value = filter.Value(s, t, fc); double image_value = copy.Value(x, y, c); sum += filter_value * image_value; } } // Assign new value for channel SetValue(i, j, c, sum); } } } }