Color Inversion
168 字
1 分钟
Color Inversion
Color Inversion
题面

答案
#include <cuda_runtime.h>
__global__ void invert_kernel(unsigned char* image, int width, int height) { int x = blockDim.x * blockIdx.x + threadIdx.x; int id = x * 4; if (id < width * height * 4) { image[id] = 255 - image[id]; image[id + 1] = 255 - image[id + 1]; image[id + 2] = 255 - image[id + 2]; image[id + 3] = image[id + 3]; }}// image_input, image_output are device pointers (i.e. pointers to memory on the GPU)extern "C" void solve(unsigned char* image, int width, int height) { int threadsPerBlock = 256; int blocksPerGrid = (width * height + threadsPerBlock - 1) / threadsPerBlock;
invert_kernel<<<blocksPerGrid, threadsPerBlock>>>(image, width, height); cudaDeviceSynchronize();}通道为:(R,G,B,ALPHA)就是前三个通道是 RGB 通道,需要进行取反,也就是原值换成 255 - 原值,而第四个通道表示的是透明度,不需要变。
Color Inversion
https://dongyanzhang.com/posts/leetgpu/color-inversion/


