You need to write a function that takes as input a bitmap image and the coordinates of a rectangle, and it has to calculate the average color of all pixels inside the rectangle.
We have already defined the following type for storing the result:
struct Result {
float avg[3];
};
You need to implement the following function:
Result calculate(int ny, int nx, const float *data,
int y0, int x0, int y1, int x1)
Here data is a color image with ny*nx pixels, and each pixel consists of three color components, red, green, and blue. In total, there are ny*nx*3 floating point numbers in the array data.
The color components are numbered 0 <= c < 3, x coordinates are numbered 0 <= x < nx, y coordinates are numbered 0 <= y < ny, and the value of this color component is stored in data[c + 3 * x + 3 * nx * y].
The parameters y0, x0, y1, and x1 indicate the location of the rectangle. The upper left corner of the rectangle is at coordinates (x0, y0), and the lower right corner is at coordinates (x1-1, y1-1). That is, the width of the rectangle is x1-x0 pixels and the height is y1-y0 pixels. The coordinates satisfy 0 <= y0 < y1 <= ny and 0 <= x0 < x1 <= nx.
In the result that you return, avg[c] has to contain the arithmetic mean of the color component c for all pixels inside the rectangle.
Details
Even though the input and output are single-precision floating-point numbers, you must do all arithmetic with double-precision floating point numbers, and only round the final result back to single precision.
You can assume that there are at most 10 million pixels in the input image.
// Courtesy of Furry Guy
#include <iostream>
#include <cctype>
#include <limits>
int main()
{
std::cout << "Do you want someone to do all the work for you? ";
char answer{};
std::cin >> answer;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::toupper(answer) == 'Y' )
{ std::cout << "Post it in the Jobs section.\n"; }
else
{ std::cout << "Show what you have coded so far.\n"; }
std::cout << "Good luck.\n";
return 0;
}
with only 10M, using more than one computer seems pointless unless you have like a lab of antiques like pentium or earlier boxes, just thread it or use cuda?
It is unclear what your question is, though.
doubles would lose precision, compared to 64 bit ints, for the accumulation phase. The division step will need floating point, of course. The max value of one color channel is 255* 10M which fits in a 64 bit int. There is also a way to average-as-you-go for better numerics but you don't need it here because it fits in the int.
the struct serves no purpose, other than an obtuse requirement.