color inversion
Inverting color in an image.
November 12, 2025 · 82 words · 1 min read · tech, programming, machine-learning
#include <cuda_runtime.h>
__global__ void invert_kernel(unsigned char* image, int width, int height) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
image[i*4] = 255 - image[i*4];
image[i*4+1] = 255 - image[i*4+1];
image[i*4+2] = 255 - image[i*4+2];
}
// 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();
}
a quote i like
Live a good life. If there are gods, and they are just, then they will not care how devout you have been, but will welcome you based on the virtues you have lived by. If there are gods, but unjust, then you should not want to worship them. ― Marcus Aurelius