-
Notifications
You must be signed in to change notification settings - Fork 289
/
imageIO.cpp
405 lines (319 loc) · 11.5 KB
/
imageIO.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "imageIO.h"
#include "cudaMappedMemory.h"
#include "cudaColorspace.h"
#include "filesystem.h"
#include "logging.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb/stb_image_resize.h"
#include <memory>
namespace {
struct Deleter {
void operator()(unsigned char* data) const noexcept(noexcept(stbi_image_free)) { stbi_image_free(data); }
};
using StbBuffer = std::unique_ptr<unsigned char[], Deleter>;
}
// loadImageIO (internal)
static StbBuffer loadImageIO( const char* filename, int* width, int* height, int* channels )
{
// validate parameters
if( !filename || !width || !height || !channels )
{
LogError(LOG_IMAGE "loadImageIO() - invalid parameter(s)\n");
return NULL;
}
// verify file path
const std::string path = locateFile(filename);
if( path.length() == 0 )
{
LogError(LOG_IMAGE "failed to find file '%s'\n", filename);
return NULL;
}
// load original image
int imgWidth = 0;
int imgHeight = 0;
int imgChannels = 0;
auto img = StbBuffer(stbi_load(path.c_str(), &imgWidth, &imgHeight, &imgChannels, *channels));
if( !img )
{
LogError(LOG_IMAGE "failed to load '%s'\n", path.c_str());
LogError(LOG_IMAGE "(error: %s)\n", stbi_failure_reason());
return NULL;
}
if( *channels != 0 )
imgChannels = *channels;
// validate dimensions for sanity
LogVerbose(LOG_IMAGE "loaded '%s' (%ix%i, %i channels)\n", filename, imgWidth, imgHeight, imgChannels);
if( imgWidth < 0 || imgHeight < 0 || imgChannels < 0 || imgChannels > 4 )
{
LogError(LOG_IMAGE "'%s' has invalid dimensions\n", filename);
return NULL;
}
// if the user provided a desired size, resize the image if necessary
const int resizeWidth = *width;
const int resizeHeight = *height;
if( resizeWidth > 0 && resizeHeight > 0 && resizeWidth != imgWidth && resizeHeight != imgHeight )
{
const auto img_org = std::move(img);
LogVerbose(LOG_IMAGE "resizing '%s' to %ix%i\n", filename, resizeWidth, resizeHeight);
// allocate memory for the resized image
img.reset(new unsigned char[resizeWidth * resizeHeight * imgChannels * sizeof(unsigned char)]);
if( !img )
{
LogError(LOG_IMAGE "failed to allocated memory to resize '%s' to %ix%i\n", filename, resizeWidth, resizeHeight);
return NULL;
}
// resize the original image
if( !stbir_resize_uint8(img_org.get(), imgWidth, imgHeight, 0,
img.get(), resizeWidth, resizeHeight, 0, imgChannels) )
{
LogError(LOG_IMAGE "failed to resize '%s' to %ix%i\n", filename, resizeWidth, resizeHeight);
return NULL;
}
// update resized dimensions
imgWidth = resizeWidth;
imgHeight = resizeHeight;
}
*width = imgWidth;
*height = imgHeight;
*channels = imgChannels;
return img;
}
// loadImage
bool loadImage( const char* filename, void** output, int* width, int* height, imageFormat format, cudaStream_t stream )
{
// validate parameters
if( !filename || !output || !width || !height )
{
LogError(LOG_IMAGE "loadImage() - invalid parameter(s)\n");
return NULL;
}
// check that the requested format is supported
if( !imageFormatIsRGB(format) )
{
LogError(LOG_IMAGE "loadImage() -- unsupported output image format requested (%s)\n", imageFormatToStr(format));
LogError(LOG_IMAGE " supported output formats are:\n");
LogError(LOG_IMAGE " * rgb8\n");
LogError(LOG_IMAGE " * rgba8\n");
LogError(LOG_IMAGE " * rgb32\n");
LogError(LOG_IMAGE " * rgba32\n");
return NULL;
}
// attempt to load the data from disk
int imgWidth = *width;
int imgHeight = *height;
int imgChannels = imageFormatChannels(format);
auto img = loadImageIO(filename, &imgWidth, &imgHeight, &imgChannels);
if( !img )
return false;
// allocate CUDA buffer for the image
const size_t imgSize = imageFormatSize(format, imgWidth, imgHeight);
if( !cudaAllocMapped((void**)output, imgSize) )
{
LogError(LOG_IMAGE "loadImage() -- failed to allocate %zu bytes for image '%s'\n", imgSize, filename);
return false;
}
// convert from uint8 to float
if( format == IMAGE_RGB32F || format == IMAGE_RGBA32F )
{
const imageFormat inputFormat = (imgChannels == 3) ? IMAGE_RGB8 : IMAGE_RGBA8;
const size_t inputImageSize = imageFormatSize(inputFormat, imgWidth, imgHeight);
void* inputImgGPU = NULL;
if( !cudaAllocMapped(&inputImgGPU, inputImageSize) )
{
LogError(LOG_IMAGE "loadImage() -- failed to allocate %zu bytes for image '%s'\n", inputImageSize, filename);
return false;
}
memcpy(inputImgGPU, img.get(), imageFormatSize(inputFormat, imgWidth, imgHeight));
if( CUDA_FAILED(cudaConvertColor(inputImgGPU, inputFormat, *output, format, imgWidth, imgHeight, stream)) )
{
printf(LOG_IMAGE "loadImage() -- failed to convert image from %s to %s ('%s')\n", imageFormatToStr(inputFormat), imageFormatToStr(format), filename);
return false;
}
CUDA(cudaFreeHost(inputImgGPU));
}
else
{
// uint8 output can be straight copied to GPU memory
memcpy(*output, img.get(), imgSize);
}
*width = imgWidth;
*height = imgHeight;
return true;
}
// loadImageRGBA
bool loadImageRGBA( const char* filename, float4** output, int* width, int* height, cudaStream_t stream )
{
return loadImage(filename, (void**)output, width, height, IMAGE_RGBA32F, stream);
}
// loadImageRGBA
bool loadImageRGBA( const char* filename, float4** cpu, float4** gpu, int* width, int* height, cudaStream_t stream )
{
const bool result = loadImageRGBA(filename, gpu, width, height, stream);
if( !result )
return false;
if( cpu != NULL )
*cpu = *gpu;
return true;
}
// limit_pixel
/*static inline unsigned char limit_pixel( float pixel, float max_pixel )
{
if( pixel < 0 )
pixel = 0;
if( pixel > max_pixel )
pixel = max_pixel;
return (unsigned char)pixel;
}*/
// saveImage
bool saveImage( const char* filename, void* ptr, int width, int height, imageFormat format, int quality, const float2& pixel_range, bool sync, cudaStream_t stream )
{
// validate parameters
if( !filename || !ptr || width <= 0 || height <= 0 )
{
LogError(LOG_IMAGE "saveImageRGBA() - invalid parameter\n");
return false;
}
if( quality < 1 )
quality = 1;
if( quality > 100 )
quality = 100;
// check that the requested format is supported
if( !imageFormatIsRGB(format) && !imageFormatIsGray(format) )
{
LogError(LOG_IMAGE "saveImage() -- unsupported input image format (%s)\n", imageFormatToStr(format));
LogError(LOG_IMAGE " supported input image formats are:\n");
LogError(LOG_IMAGE " * rgb8\n");
LogError(LOG_IMAGE " * rgba8\n");
LogError(LOG_IMAGE " * rgb32f\n");
LogError(LOG_IMAGE " * rgba32f\n");
LogError(LOG_IMAGE " * gray8\n");
LogError(LOG_IMAGE " * gray32\n");
return false;
}
// allocate memory for the uint8 image
const size_t channels = imageFormatChannels(format);
const size_t stride = width * sizeof(unsigned char) * channels;
const size_t size = stride * height;
unsigned char* img = (unsigned char*)ptr;
// if needed, convert from float to uint8
const imageBaseType baseType = imageFormatBaseType(format);
if( baseType == IMAGE_FLOAT )
{
imageFormat outputFormat = IMAGE_UNKNOWN;
if( channels == 1 )
outputFormat = IMAGE_GRAY8;
else if( channels == 3 )
outputFormat = IMAGE_RGB8;
else if( channels == 4 )
outputFormat = IMAGE_RGBA8;
if( !cudaAllocMapped((void**)&img, size) )
{
LogError(LOG_IMAGE "saveImage() -- failed to allocate %zu bytes for image '%s'\n", size, filename);
return false;
}
if( CUDA_FAILED(cudaConvertColor(ptr, format, img, outputFormat, width, height, pixel_range, stream)) ) // TODO limit pixel
{
LogError(LOG_IMAGE "saveImage() -- failed to convert image from %s to %s ('%s')\n", imageFormatToStr(format), imageFormatToStr(outputFormat), filename);
return false;
}
sync = true;
}
if( sync )
{
if( stream != 0 )
CUDA(cudaStreamSynchronize(stream));
else
CUDA(cudaDeviceSynchronize());
}
#define release_return(x) \
if( baseType == IMAGE_FLOAT ) \
CUDA(cudaFreeHost(img)); \
return x;
// determine the file extension
const std::string ext = fileExtension(filename);
const char* extension = ext.c_str();
if( ext.size() == 0 )
{
LogError(LOG_IMAGE "invalid filename or extension, '%s'\n", filename);
release_return(false);
}
// save the image
int save_result = 0;
if( strcasecmp(extension, "jpg") == 0 || strcasecmp(extension, "jpeg") == 0 )
{
save_result = stbi_write_jpg(filename, width, height, channels, img, quality);
}
else if( strcasecmp(extension, "png") == 0 )
{
// convert quality from 1-100 to 0-9 (where 0 is high quality)
quality = (100 - quality) / 10;
if( quality < 0 )
quality = 0;
if( quality > 9 )
quality = 9;
stbi_write_png_compression_level = quality;
// write the PNG file
save_result = stbi_write_png(filename, width, height, channels, img, stride);
}
else if( strcasecmp(extension, "tga") == 0 )
{
save_result = stbi_write_tga(filename, width, height, channels, img);
}
else if( strcasecmp(extension, "bmp") == 0 )
{
save_result = stbi_write_bmp(filename, width, height, channels, img);
}
/*else if( strcasecmp(extension, "hdr") == 0 )
{
save_result = stbi_write_hdr(filename, width, height, channels, (float*)cpu);
}*/
else
{
LogError(LOG_IMAGE "invalid extension format '.%s' saving image '%s'\n", extension, filename);
LogError(LOG_IMAGE "valid extensions are: JPG/JPEG, PNG, TGA, BMP.\n");
release_return(false);
}
// check the return code
if( !save_result )
{
LogError(LOG_IMAGE "failed to save %ix%i image to '%s'\n", width, height, filename);
release_return(false);
}
LogVerbose(LOG_IMAGE "saved '%s' (%ix%i, %zu channels)\n", filename, width, height, channels);
release_return(true);
}
// saveImage
bool saveImage( const char* filename, void* ptr, int width, int height, imageFormat format, int quality, cudaStream_t stream )
{
return saveImage(filename, ptr, width, height, format, quality, make_float2(0,255), true, stream);
}
// saveImageRGBA
bool saveImageRGBA( const char* filename, float4* ptr, int width, int height, float max_pixel, int quality, cudaStream_t stream )
{
return saveImage(filename, ptr, width, height, IMAGE_RGBA32F, quality, make_float2(0, max_pixel), stream);
}