-
Notifications
You must be signed in to change notification settings - Fork 289
/
imageWriter.cpp
134 lines (104 loc) · 3.45 KB
/
imageWriter.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
/*
* Copyright (c) 2020, 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 "imageWriter.h"
#include "imageIO.h"
#include "filesystem.h"
#include "logging.h"
#include <strings.h>
// supported image file extensions
const char* imageWriter::SupportedExtensions[] = { "jpg", "jpeg", "png",
"tga", "targa", "bmp",
NULL };
bool imageWriter::IsSupportedExtension( const char* ext )
{
if( !ext )
return false;
uint32_t extCount = 0;
while(true)
{
if( !SupportedExtensions[extCount] )
break;
if( strcasecmp(SupportedExtensions[extCount], ext) == 0 )
return true;
extCount++;
}
return false;
}
// constructor
imageWriter::imageWriter( const videoOptions& options ) : videoOutput(options)
{
mFileCount = 0;
mStreaming = true;
// replace wildcards with %i
const size_t wildcard = mOptions.resource.location.find("*");
if( wildcard != std::string::npos )
mOptions.resource.location.replace(wildcard, 1, "%i");
}
// destructor
imageWriter::~imageWriter()
{
}
// Create
imageWriter* imageWriter::Create( const videoOptions& options )
{
return new imageWriter(options);
}
// Create
imageWriter* imageWriter::Create( const char* resource, const videoOptions& options )
{
videoOptions opt = options;
opt.resource = resource;
return Create(opt);
}
// Render
bool imageWriter::Render( void* image, uint32_t width, uint32_t height, imageFormat format, cudaStream_t stream )
{
const bool substreams_success = videoOutput::Render(image, width, height, format);
if( mOptions.resource.location.find("%") != std::string::npos )
{
// path has a format (should be '%u' or '%i')
sprintf(mFileOut, mOptions.resource.location.c_str(), mFileCount);
}
else if( mOptions.resource.extension.size() == 0 )
{
// path is a dir, use default image numbering
sprintf(mFileOut, "%u.jpg", mFileCount);
const std::string path = pathJoin(mOptions.resource.location, mFileOut);
strcpy(mFileOut, path.c_str());
}
else
{
// path is a single file, use it as-is
strcpy(mFileOut, mOptions.resource.location.c_str());
}
//CUDA(cudaDeviceSynchronize()); // now done in saveImage()
// save the image
if( !saveImage(mFileOut, image, width, height, format, IMAGE_DEFAULT_SAVE_QUALITY, stream) )
{
LogError(LOG_IMAGE "imageWriter -- failed to save '%s'\n", mFileOut);
return false;
}
mOptions.width = width;
mOptions.height = height;
mFileCount++;
return substreams_success;
}