Skip to content

Commit ac82b54

Browse files
committed
external alpha/mask support
1 parent e369334 commit ac82b54

4 files changed

Lines changed: 88 additions & 17 deletions

File tree

src/Solidify.cpp

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,57 @@ bool progress_callback(void* opaque_data, float portion_done)
3737
}
3838

3939
//int main(int argc, char* argv[]) {
40-
int solidify_main(const std::string& inputFileName, const std::string& outputFileName) {
41-
//if (argc != 3) {
42-
// std::cerr << "Usage: " << argv[0] << " INPUT OUTPUT\n";
43-
// return 1;
44-
//}
45-
40+
int solidify_main(const std::string& mask_file, const std::string& inputFileName, const std::string& outputFileName) {
4641
Timer g_timer;
4742

48-
//const char* input_filename = argv[0];
49-
//const char* output_filename = argv[1];
50-
5143
// Create an ImageBuf object for the input file
5244
ImageBuf input_buf(inputFileName);
45+
ImageBuf mask_buf(mask_file);
5346

5447
// Read the image with a progress callback
48+
49+
int last_channel = -1;
50+
51+
if (mask_file != "") {
52+
last_channel = 3;
53+
std::cout << "Reading " << mask_file << std::endl;
54+
bool read_ok = mask_buf.read(0, 0, 0, 1, true, TypeDesc::FLOAT, nullptr, nullptr);
55+
if (!read_ok) {
56+
std::cerr << "Error: Could not read mask image\n";
57+
return 1;
58+
}
59+
}
5560
std::cout << "Reading " << inputFileName << std::endl;
56-
bool read_ok = input_buf.read(0, 0, 0, -1, true, TypeUnknown, *progress_callback, nullptr);
61+
62+
bool read_ok = input_buf.read(0, 0, 0, last_channel, true, TypeUnknown, *progress_callback, nullptr);
5763
if (!read_ok) {
5864
std::cerr << "Error: Could not read input image\n";
5965
return 1;
6066
}
6167
std::cout << std::endl;
6268

6369
// Create an ImageBuf object to store the result
64-
ImageBuf result_buf;
70+
ImageBuf result_buf, rgba_buf;
6571

66-
// Call fillholes_pushpull
6772
std::cout << "Filling holes in process...\n";
6873

6974
Timer pushpull_timer;
70-
bool ok = ImageBufAlgo::fillholes_pushpull(result_buf, input_buf);
75+
76+
if (mask_file != "") {
77+
ImageBuf alpha_ch, alpha_c3;
78+
bool ok = ImageBufAlgo::channel_append(alpha_ch, mask_buf, mask_buf);
79+
ok = ok && ImageBufAlgo::channel_append(alpha_c3, alpha_ch, mask_buf);
80+
ImageBufAlgo::mul(input_buf, input_buf, alpha_c3);
81+
ok = ok && ImageBufAlgo::channel_append(rgba_buf, input_buf, mask_buf);
82+
if (!ok) {
83+
std::cerr << "Error: " << rgba_buf.geterror() << std::endl;
84+
return 1;
85+
}
86+
}
87+
88+
// Call fillholes_pushpull
89+
90+
bool ok = ImageBufAlgo::fillholes_pushpull(result_buf, (mask_file == "") ? input_buf : rgba_buf);
7191

7292
if (!ok) {
7393
std::cerr << "Error: " << result_buf.geterror() << std::endl;
@@ -95,6 +115,19 @@ int solidify_main(const std::string& inputFileName, const std::string& outputFil
95115
*progress_callback, nullptr);
96116
out->close();
97117

118+
#if 0
119+
spec = rgba_buf.spec();
120+
spec.nchannels = 4;
121+
122+
out->open("debug.exr", spec, ImageOutput::Create);
123+
124+
out->write_image(rgba_buf.spec().format, rgba_buf.localpixels(),
125+
rgba_buf.pixel_stride(), rgba_buf.scanline_stride(), rgba_buf.z_stride(),
126+
*progress_callback, nullptr);
127+
out->close();
128+
129+
#endif
130+
98131
std::cout << std::endl << "Total processing time : " << g_timer.nowText() << std::endl;
99132

100133
return 0;

src/processing.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,61 @@
2424

2525
#include "solidify.h"
2626

27-
std::vector<QString> fileNames; // This will hold the names of all files
27+
QString checkAlpha(std::vector<QString> fileNames) {
28+
std::vector<QString>::iterator it;
29+
30+
// Find "_mask."
31+
it = std::find_if(fileNames.begin(), fileNames.end(), [](const QString& str) {
32+
return str.contains("_mask.", Qt::CaseInsensitive);
33+
});
34+
if (it != fileNames.end()) {
35+
qDebug() << "Found _mask. in: " << it->toStdString().c_str();
36+
return *it;
37+
}
38+
39+
// Find "_alpha."
40+
it = std::find_if(fileNames.begin(), fileNames.end(), [](const QString& str) {
41+
return str.contains("_alpha.", Qt::CaseInsensitive);
42+
});
43+
if (it != fileNames.end()) {
44+
qDebug() << "Found _alpha. in: " << it->toStdString().c_str();
45+
return *it;
46+
}
47+
48+
// If we reach this point, we did not find a match.
49+
return "";
50+
}
2851

2952
bool doProcessing(QList<QUrl> urls) {
53+
std::vector<QString> fileNames; // This will hold the names of all files
54+
3055
for (const QUrl& url : urls) {
3156
QString fileName = url.toLocalFile();
3257
if (!fileName.isEmpty()) {
3358
fileNames.push_back(fileName);
3459
}
3560
}
3661

62+
// Check if there is an alpha channel files
63+
QString mask_file = checkAlpha(fileNames);
64+
65+
if (mask_file != "") {
66+
qDebug() << "Mask file: " << mask_file << " will be used.";
67+
}
68+
3769
for (int i = 0; i < fileNames.size(); i++) {
3870
//qDebug() << "File name: " << fileName;
71+
if (fileNames[i] == mask_file) {
72+
continue;
73+
}
74+
3975
QFileInfo fileInfo(fileNames[i]);
4076
QString baseName = fileInfo.baseName();
4177
QString path = fileInfo.absolutePath();
4278
QString outName = path + "/" + baseName + "_fill." + fileInfo.completeSuffix();
4379

4480
// Call the solidify_main function
45-
if (solidify_main(fileNames[i].toStdString(), outName.toStdString())) {
81+
if (solidify_main(mask_file.toStdString(), fileNames[i].toStdString(), outName.toStdString())) {
4682
exit(-1);
4783
};
4884
}

src/processing.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
#pragma once
1919
#include <QtCore/QFileInfo>
2020

21-
bool doProcessing(QList<QUrl> urls);
21+
bool doProcessing(QList<QUrl> urls);
22+
23+
QString checkAlpha(std::vector<QString> fileNames);

src/solidify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
*/
1717
#pragma once
1818

19-
int solidify_main(const std::string& inputFileName, const std::string& outputFileName);
19+
int solidify_main(const std::string& mask_file, const std::string& inputFileName, const std::string& outputFileName);

0 commit comments

Comments
 (0)