// RGB to 8-bit grayscale with the BT.601 luma weights in fixed point: // Y = (77 R + 150 G + 29 B + 128) >> 8, close to 0.299 R + 0.587 G + 0.114 B. // The weights sum to 256, so white (255, 255, 255) maps to 255. One clock. `timescale 1ns / 1ps module rgb_to_gray ( input logic clk, input logic rst, input logic in_valid, input logic in_sof, // first pixel of a frame input logic [7:0] r, input logic [7:0] g, input logic [7:0] b, output logic out_valid, output logic out_sof, output logic [7:0] gray ); logic [15:0] sum; // at most 255 * 256 + 128 = 65408 assign sum = 16'd77 * 16'(r) + 16'd150 * 16'(g) + 16'd29 * 16'(b) + 16'd128; always_ff @(posedge clk) begin if (rst) begin out_valid <= 1'b0; out_sof <= 1'b0; end else begin out_valid <= in_valid; out_sof <= in_sof & in_valid; end gray <= 8'(sum >> 8); end endmodule