-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageWriterWindow.cpp
174 lines (146 loc) · 5.76 KB
/
ImageWriterWindow.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
#include "ImageWriterWindow.h"
#include "MainWindow.h"
#include "FileDialog.h"
#include "Serializer.h"
#include "ImGuiHelpers.h"
#include "./../Core/bitmap.h"
#include "./../Codecs/imageencoder.h"
#include <sstream>
ACMB_GUI_NAMESPACE_BEGIN
static std::string GetFilters()
{
const auto extensions = ImageEncoder::GetAllExtensions();
std::ostringstream ss;
ss << ".*";
for ( const auto& extension : extensions )
{
ss << "," << extension;
}
return ss.str();
}
static std::string GetFormatList()
{
const auto extensions = ImageEncoder::GetAllExtensions();
std::ostringstream ss;
for ( const auto& extension : extensions )
{
ss << extension << '\0';
}
const auto res = ss.str();
return { res.begin(), std::prev( res.end() ) };
}
ImageWriterWindow::ImageWriterWindow( const Point& gridPos )
: PipelineElementWindow( "Export Images", gridPos, PEFlags_StrictlyOneInput | PEFlags_NoOutput )
{
_formatList = GetFormatList();
}
void ImageWriterWindow::DrawPipelineElementControls()
{
UI::Checkbox( "Keep Original Name", &_keepOriginalFileName,
"If checked then the images will be saved with their original names. Otherwise the custom name will be used (with number postfixes for multiple images)" );
ImGui::Separator();
const float itemWidth = 100.0f * cMenuScaling;
auto fileDialog = FileDialog::Instance();
if ( _keepOriginalFileName )
{
ImGui::Text( "Directory Path" );
ImGui::SetNextItemWidth( itemWidth );
ImGui::InputText( "##Directory", ( char* ) _workingDirectory.c_str(), 1024, ImGuiInputTextFlags_ReadOnly );
UI::Button( "Select Directory", { itemWidth, 0 }, [&]
{
fileDialog.OpenDialog( "SelectOutputFile", "Select Directory", nullptr, _workingDirectory.c_str(), 0 );
}, "Choose a directory to export the results" );
UI::Combo( "Format", &_formatId, _formatList, "Choose a file format to export the results");
}
else
{
ImGui::Text( "File Name" );
ImGui::SetNextItemWidth( itemWidth );
const size_t lastSlash = _fileName.find_last_of( "\\/" );
ImGui::InputText( "##File Name", ( char* ) _fileName.substr( lastSlash + 1).c_str(), 1024, ImGuiInputTextFlags_ReadOnly );
UI::Button( "Select File", { itemWidth, 0 }, [&]
{
static auto filters = GetFilters();
fileDialog.OpenDialog( "SelectOutputFile", "Select File", filters.c_str(), _workingDirectory.c_str(), 0 );
}, "Choose a file format to export the results" );
}
if ( fileDialog.Display( "SelectOutputFile", {}, { 300 * cMenuScaling, 200 * cMenuScaling } ) )
{
// action if OK
if ( fileDialog.IsOk() )
{
_workingDirectory = fileDialog.GetCurrentPath();
_fileName = fileDialog.GetFilePathName();
}
// close
fileDialog.Close();
}
}
void ImageWriterWindow::Serialize( std::ostream& out ) const
{
PipelineElementWindow::Serialize( out );
gui::Serialize( _workingDirectory, out );
gui::Serialize( _fileName, out );
gui::Serialize( _formatId, out );
gui::Serialize( _keepOriginalFileName, out );
}
void ImageWriterWindow::Deserialize( std::istream& in )
{
PipelineElementWindow::Deserialize( in );
_workingDirectory = gui::Deserialize<std::string>( in, _remainingBytes );
_fileName = gui::Deserialize<std::string>( in, _remainingBytes );
_formatId = gui::Deserialize<int>( in, _remainingBytes );
_keepOriginalFileName = gui::Deserialize<bool>( in, _remainingBytes );
}
int ImageWriterWindow::GetSerializedStringSize() const
{
return PipelineElementWindow::GetSerializedStringSize()
+ gui::GetSerializedStringSize( _workingDirectory )
+ gui::GetSerializedStringSize( _fileName )
+ gui::GetSerializedStringSize( _formatId )
+ gui::GetSerializedStringSize( _keepOriginalFileName );
}
IBitmapPtr ImageWriterWindow::ProcessBitmapFromPrimaryInput( IBitmapPtr pSource, size_t taskNumber )
{
std::string finalName;
if ( _keepOriginalFileName )
{
auto taskName = GetTaskName( taskNumber );
const size_t lastSlashPos = taskName.find_last_of( "\\/" );
const size_t lastDotPos = taskName.find_last_of( '.' );
static const auto extensions = ImageEncoder::GetAllExtensions();
auto extensionIt = extensions.begin();
std::advance( extensionIt, _formatId );
finalName = _workingDirectory + "/" + taskName.substr( lastSlashPos + 1, lastDotPos - lastSlashPos - 1 ) + *extensionIt;
}
else
{
const size_t dotPos = _fileName.find_last_of( '.' );
finalName = (taskNumber == 0) ? _fileName : (_fileName.substr( 0, dotPos ) + "_" + std::to_string( taskNumber ) + _fileName.substr( dotPos ) );
}
IBitmap::Save( pSource, finalName );
return nullptr;
}
std::vector<std::string> ImageWriterWindow::RunAllTasks()
{
auto pPrimaryInput = GetPrimaryInput();
if ( !pPrimaryInput )
return { "No primary input for the'" + _name + "' element" };
_taskCount = pPrimaryInput->GetTaskCount();
if ( _taskCount == 0 )
return { "No input frames for the'" + _name + "' element" };
if ( _keepOriginalFileName && _workingDirectory.empty() )
return { "Working directory for the'" + _name + "' element is not specified" };
if ( !_keepOriginalFileName && _fileName.empty() )
return { "File name for the'" + _name + "' element is not specified" };
std::vector<std::string> res;
for ( size_t i = 0; i < _taskCount; ++i )
{
const auto taskRes = RunTaskAndReportProgress( i );
if ( !taskRes.has_value() )
res.push_back( taskRes.error() );
}
return res;
}
REGISTER_TOOLS_ITEM( ImageWriterWindow )
ACMB_GUI_NAMESPACE_END