DrawImageOnGPU.cu
4.25 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "cuda_kernels.h"
#include "../interface/logger.hpp"
typedef unsigned char uchar;
typedef unsigned int uint32;
typedef int int32;
namespace cuda_common
{
__global__ void kernel_drawPixel(float* d_srcRGB, int src_width, int src_height,
int left, int top, int right, int bottom)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (((x == left || x == right) && y >= top && y <= bottom) || ((y == top || y == bottom) && x >= left && x <= right))
{
d_srcRGB[(y*src_width) + x] = 0;
d_srcRGB[(src_width*src_height) + (y*src_width) + x] = 255;
d_srcRGB[(2 * src_width*src_height) + (y*src_width) + x] = 0;
}
}
cudaError_t DrawImage(float* d_srcRGB, int src_width, int src_height, int left, int top, int right, int bottom)
{
dim3 block(32, 16, 1);
dim3 grid((src_width + (block.x - 1)) / block.x, (src_height + (block.y - 1)) / block.y, 1);
kernel_drawPixel << < grid, block >> >(d_srcRGB, src_width, src_height, left, top, right, bottom);
cudaError_t cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
LOG_ERROR("Draw 32 kernel_memcopy launch failed:{}",cudaGetErrorString(cudaStatus));
return cudaStatus;
}
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
LOG_ERROR("cudaDeviceSynchronize returned error code {} after launching kernel_bilinear!", cudaStatus);
return cudaStatus;
}
return cudaStatus;
}
__global__ void kernel_drawPixel(unsigned char* d_srcRGB, int src_width, int src_height,
int left, int top, int right, int bottom)
{
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if (((x == left || x == right) && y >= top && y <= bottom) || ((y == top || y == bottom) && x >= left && x <= right))
{
d_srcRGB[(y*src_width) + x] = 0;
d_srcRGB[(src_width*src_height) + (y*src_width) + x] = 255;
d_srcRGB[(2 * src_width*src_height) + (y*src_width) + x] = 0;
}
}
cudaError_t DrawImage(unsigned char* d_srcRGB, int src_width, int src_height, int left, int top, int right, int bottom)
{
dim3 block(32, 16, 1);
dim3 grid((src_width + (block.x - 1)) / block.x, (src_height + (block.y - 1)) / block.y, 1);
kernel_drawPixel << < grid, block >> >(d_srcRGB, src_width, src_height, left, top, right, bottom);
cudaError_t cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
LOG_ERROR("Draw 68 kernel_memcopy launch failed: {}",cudaGetErrorString(cudaStatus));
return cudaStatus;
}
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
LOG_ERROR("cudaDeviceSynchronize returned error code {} after launching kernel_bilinear!", cudaStatus);
return cudaStatus;
}
return cudaStatus;
}
__global__ void kernel_drawLine(float* d_srcRGB, int src_width, int src_height,
int begin_x, int begin_y, int end_x, int end_y)
{
int min_x = end_x < begin_x ? end_x : begin_x;
int max_x = end_x < begin_x ? begin_x : end_x;
int min_y = end_y < begin_y ? end_y : begin_y;
int max_y = end_y < begin_y ? begin_y : end_y;
const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y;
if ((x - begin_x) * (end_y - begin_y) == (end_x - begin_x) * (y - begin_y)
&& min_x <= x && x <= max_x
&& min_y <= y && y <= max_y)
{
d_srcRGB[(y*src_width) + x] = 0;
d_srcRGB[(src_width*src_height) + (y*src_width) + x] = 255;
d_srcRGB[(2 * src_width*src_height) + (y*src_width) + x] = 0;
}
}
cudaError_t DrawLine(float* d_srcRGB, int src_width, int src_height, int begin_x, int begin_y, int end_x, int end_y)
{
dim3 block(32, 16, 1);
dim3 grid((src_width + (block.x - 1)) / block.x, (src_height + (block.y - 1)) / block.y, 1);
kernel_drawLine << < grid, block >> >(d_srcRGB, src_width, src_height, begin_x, begin_y, end_x, end_y);
cudaError_t cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
LOG_ERROR("Draw 112 kernel_memcopy launch failed: {}",cudaGetErrorString(cudaStatus));
return cudaStatus;
}
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
LOG_ERROR("cudaDeviceSynchronize returned error code {} after launching kernel_bilinear!", cudaStatus);
return cudaStatus;
}
return cudaStatus;
}
}