-
Notifications
You must be signed in to change notification settings - Fork 36
/
loadImage.cpp
182 lines (144 loc) · 4.75 KB
/
loadImage.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* http://github.com/dusty-nv/jetson-inference
*/
#include "loadImage.h"
#include "cudaMappedMemory.h"
#include <QImage>
// loadImageRGBA
bool loadImageRGBA( const char* filename, float4** cpu, float4** gpu, int* width, int* height )
{
if( !filename || !cpu || !gpu || !width || !height )
{
printf("loadImageRGB - invalid parameter\n");
return false;
}
// load original image
QImage qImg;
if( !qImg.load(filename) )
{
printf("failed to load image %s\n", filename);
return false;
}
if( *width != 0 && *height != 0 )
qImg = qImg.scaled(*width, *height, Qt::IgnoreAspectRatio);
const uint32_t imgWidth = qImg.width();
const uint32_t imgHeight = qImg.height();
const uint32_t imgPixels = imgWidth * imgHeight;
const size_t imgSize = imgWidth * imgHeight * sizeof(float) * 4;
printf("loaded image %s (%u x %u) %zu bytes\n", filename, imgWidth, imgHeight, imgSize);
// allocate buffer for the image
if( !cudaAllocMapped((void**)cpu, (void**)gpu, imgSize) )
{
printf(LOG_CUDA "failed to allocated %zu bytes for image %s\n", imgSize, filename);
return false;
}
float4* cpuPtr = *cpu;
for( uint32_t y=0; y < imgHeight; y++ )
{
for( uint32_t x=0; x < imgWidth; x++ )
{
const QRgb rgb = qImg.pixel(x,y);
const float4 px = make_float4(float(qRed(rgb)),
float(qGreen(rgb)),
float(qBlue(rgb)), 1.0f);
cpuPtr[y*imgWidth+x] = px;
}
}
*width = imgWidth;
*height = imgHeight;
return true;
}
// loadImageRGB
bool loadImageRGB( const char* filename, float3** cpu, float3** gpu, int* width, int* height, const float3& mean )
{
if( !filename || !cpu || !gpu || !width || !height )
{
printf("loadImageRGB - invalid parameter\n");
return false;
}
// load original image
QImage qImg;
if( !qImg.load(filename) )
{
printf("failed to load image %s\n", filename);
return false;
}
if( *width != 0 && *height != 0 )
qImg = qImg.scaled(*width, *height, Qt::IgnoreAspectRatio);
const uint32_t imgWidth = qImg.width();
const uint32_t imgHeight = qImg.height();
const uint32_t imgPixels = imgWidth * imgHeight;
const size_t imgSize = imgWidth * imgHeight * sizeof(float) * 3;
printf("loaded image %s (%u x %u) %zu bytes\n", filename, imgWidth, imgHeight, imgSize);
// allocate buffer for the image
if( !cudaAllocMapped((void**)cpu, (void**)gpu, imgSize) )
{
printf(LOG_CUDA "failed to allocated %zu bytes for image %s\n", imgSize, filename);
return false;
}
float* cpuPtr = (float*)*cpu;
for( uint32_t y=0; y < imgHeight; y++ )
{
for( uint32_t x=0; x < imgWidth; x++ )
{
const QRgb rgb = qImg.pixel(x,y);
const float mul = 1.0f; //1.0f / 255.0f;
const float3 px = make_float3((float(qRed(rgb)) - mean.x) * mul,
(float(qGreen(rgb)) - mean.y) * mul,
(float(qBlue(rgb)) - mean.z) * mul );
// note: caffe/GIE is band-sequential (as opposed to the typical Band Interleaved by Pixel)
cpuPtr[imgPixels * 0 + y * imgWidth + x] = px.x;
cpuPtr[imgPixels * 1 + y * imgWidth + x] = px.y;
cpuPtr[imgPixels * 2 + y * imgWidth + x] = px.z;
}
}
*width = imgWidth;
*height = imgHeight;
return true;
}
// loadImageBGR
bool loadImageBGR( const char* filename, float3** cpu, float3** gpu, int* width, int* height, const float3& mean )
{
if( !filename || !cpu || !gpu || !width || !height )
{
printf("loadImageRGB - invalid parameter\n");
return false;
}
// load original image
QImage qImg;
if( !qImg.load(filename) )
{
printf("failed to load image %s\n", filename);
return false;
}
if( *width != 0 && *height != 0 )
qImg = qImg.scaled(*width, *height, Qt::IgnoreAspectRatio);
const uint32_t imgWidth = qImg.width();
const uint32_t imgHeight = qImg.height();
const uint32_t imgPixels = imgWidth * imgHeight;
const size_t imgSize = imgWidth * imgHeight * sizeof(float) * 3;
printf("loaded image %s (%u x %u) %zu bytes\n", filename, imgWidth, imgHeight, imgSize);
// allocate buffer for the image
if( !cudaAllocMapped((void**)cpu, (void**)gpu, imgSize) )
{
printf(LOG_CUDA "failed to allocated %zu bytes for image %s\n", imgSize, filename);
return false;
}
float* cpuPtr = (float*)*cpu;
for( uint32_t y=0; y < imgHeight; y++ )
{
for( uint32_t x=0; x < imgWidth; x++ )
{
const QRgb rgb = qImg.pixel(x,y);
const float mul = 1.0f; //1.0f / 255.0f;
const float3 px = make_float3((float(qBlue(rgb)) - mean.x) * mul,
(float(qGreen(rgb)) - mean.y) * mul,
(float(qRed(rgb)) - mean.z) * mul );
// note: caffe/GIE is band-sequential (as opposed to the typical Band Interleaved by Pixel)
cpuPtr[imgPixels * 0 + y * imgWidth + x] = px.x;
cpuPtr[imgPixels * 1 + y * imgWidth + x] = px.y;
cpuPtr[imgPixels * 2 + y * imgWidth + x] = px.z;
}
}
return true;
}