MST

星途 面试题库

面试题:C++指针在简单图像处理中的应用

假设你有一个表示图像像素的二维数组 `unsigned char image[height][width][3]`,分别代表RGB三个通道。请使用C++指针实现一个函数,将图像的所有像素的红色通道值翻倍(注意边界,不能超过255)。
14.0万 热度难度
编程语言C++

知识考点

AI 面试

面试题答案

一键面试
#include <iostream>
#include <cstdint>

void doubleRedChannel(uint8_t (*image)[3], int height, int width) {
    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            uint8_t* pixel = &image[i][j][0];
            int newRed = pixel[0] * 2;
            pixel[0] = newRed > 255? 255 : newRed;
        }
    }
}