-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.cu
54 lines (40 loc) · 1.63 KB
/
process.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "process.cuh"
//#include <iostream>
extern "C"
//process 1 pixels per thread
__global__
void changeBightnessSaturation(unsigned char *inData_d, unsigned char* outData_d, const float sat, const float bright, const int imgWidth, const int imgHeight )
{
//Thread index
const int xIndex = blockIdx.x * blockDim.x + threadIdx.x;
const int yIndex = blockIdx.y * blockDim.y + threadIdx.y;
if(xIndex>imgWidth || yIndex>imgHeight) //Only valid threads perform memory I/O
return;
const int step=imgWidth*3;
const int rgb_id = yIndex * step + (3*xIndex); //index for RGB channel array
unsigned char *inPixelPtr = &inData_d[rgb_id];
unsigned char *outPixelPtr = &outData_d[rgb_id];
int R, G ,B;
R = inPixelPtr[0] * bright;
G = inPixelPtr[1] * bright;
B = inPixelPtr[2] * bright;
//Weight for saturation
const double W=sqrt(R*R *0.299 + G*G*0.587 + B*B*0.114) ;
R = W + (R - W) * sat;
G = W + (G - W) * sat;
B = W + (B - W) * sat;
//Clamp 0 to 255 and write to output
outPixelPtr[0]=(R <0)?0:((R>255)?255:R);
outPixelPtr[1]=(G<0)?0:((G>255)?255:G);
outPixelPtr[2]=(B<0)?0:((B>255)?255:B);
//Move pointers to next pixel
inPixelPtr=inPixelPtr+3*(blockDim.x);
outPixelPtr=outPixelPtr+3*(blockDim.x);
}
// Kernel laucher
void cudaProcessImage(unsigned char* in_d, unsigned char* out_d, const float sat, float bright, int width, int height)
{
const dim3 block(width/4,1); //should be able to process up to 4K videos
const dim3 grid(4,height);
changeBightnessSaturation <<< grid, block >>>(in_d, out_d, sat, bright, width, height);
}