← All problems
#66 RGB To Grayscale Easy

Implement a GPU program that converts an RGB image to grayscale on the GPU. Given an input RGB image represented as a 1D array of 32-bit floating point values, compute the corresponding grayscale image using the standard RGB to grayscale conversion formula.

The conversion formula is: gray = 0.299 × R + 0.587 × G + 0.114 × B

The input array input contains height × width × 3 elements, where the RGB values for each pixel are stored consecutively (R, G, B, R, G, B, ...). The output array output should contain height × width grayscale values.

Implementation Requirements

Example 1:

Input:  input = [255.0, 0.0, 0.0, 0.0, 255.0, 0.0, 0.0, 0.0, 255.0, 128.0, 128.0, 128.0], width=2, height=2
Output: output = [76.245, 149.685, 29.07, 128.0]

Example 2:

Input:  input = [100.0, 150.0, 200.0], width=1, height=1
Output: output = [140.75]

Constraints

Problem text & starters from the official AlphaGPU/leetgpu-challenges. Solve it on leetgpu.com.