Skip to content

Commit 877e911

Browse files
committed
Grayscale images fix
1 parent 2954668 commit 877e911

File tree

4 files changed

+84
-28
lines changed

4 files changed

+84
-28
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ Check console to progress information.
1111

1212
![Screenshot](https://github.com/ssh4net/Solidify/assets/3924000/3b8562f6-ca73-49f6-a3b1-b9e1f4cbc8ac)
1313

14-
Tool expecting RGBA image file. Output files will be write in same folder as a source files with a name **Source_file_name_fill.ext** as RGB without alpha channel.
14+
Tool expecting RGBA or Grayscale with alpha channel image file. Output files will be write in same folder as a source files with a name **Source_file_name_fill.ext** as RGB without alpha channel.
1515

16-
From v1.1 possible to use external alpha channel as a single channel file with **_mask.ext** or **_alpha.ext** in name (case insencitive).
17-
Tool will read external alpha channel file only once per batch.
16+
From v1.2 possible to use external alpha channel as a single channel file with **_mask.ext** or **_alpha.ext** in name (case insencitive).
17+
Supporting for RGB and Grayscale images only with external alpha/mask image file.
18+
19+
Too optimize performance Solidify app reading external alpha channel file once per batch.
1820

1921
![Solidify 1 1](https://github.com/ssh4net/Solidify/assets/3924000/24dc9382-e554-44d0-8ed1-2465752a4752)
2022

@@ -27,7 +29,8 @@ Dependencies
2729

2830
Changelog
2931
---------
30-
* 1.1 - External alpha/mask channel support. Drag&Drop window always on top.
32+
* 1.2 - Grayscale textures fix
33+
* 1.1 - External alpha/mask channel support. Drag&Drop window always on top
3134
* 1.0 - Initial release
3235

3336
License

src/Solidify.cpp

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

3939
std::pair<ImageBuf, ImageBuf> mask_load(const std::string& mask_file) {
40-
ImageBuf mask_buf(mask_file);
40+
ImageBuf alpha_buf(mask_file);
4141
std::cout << "Reading " << mask_file << std::endl;
42-
bool read_ok = mask_buf.read(0, 0, 0, 1, true, TypeDesc::FLOAT, nullptr, nullptr);
42+
bool read_ok = alpha_buf.read(0, 0, 0, 1, true, TypeDesc::FLOAT, nullptr, nullptr);
4343
if (!read_ok) {
4444
std::cerr << "Error: Could not read mask image\n";
45+
system("pause");
4546
exit(-1);
4647
}
47-
48+
49+
// rename channel to alpha and set it as an alpha channel
50+
alpha_buf.specmod().channelnames[0] = "A";
51+
alpha_buf.specmod().alpha_channel = 0;
52+
4853
ImageBuf rgb_alpha, temp_buff;
4954

50-
bool ok = ImageBufAlgo::channel_append(temp_buff, mask_buf, mask_buf);
51-
ok = ok && ImageBufAlgo::channel_append(rgb_alpha, temp_buff, mask_buf);
55+
bool ok = ImageBufAlgo::channel_append(temp_buff, alpha_buf, alpha_buf);
56+
ok = ok && ImageBufAlgo::channel_append(rgb_alpha, temp_buff, alpha_buf);
5257
if (!ok) {
5358
std::cerr << "Error: Could not append channels\n";
59+
system("pause");
5460
exit(-1);
5561
}
5662

5763
temp_buff.clear();
5864

59-
return { mask_buf, rgb_alpha };
65+
return { alpha_buf, rgb_alpha };
6066
}
6167

6268
bool solidify_main(const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair) {
@@ -66,6 +72,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
6672
ImageBuf input_buf(inputFileName);
6773

6874
bool external_alpha = false;
75+
bool grayscale = false;
6976

7077
if (mask_pair.first.initialized() && mask_pair.second.initialized()) {
7178
external_alpha = true;
@@ -82,7 +89,55 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
8289
std::cerr << "Error: Could not read input image\n";
8390
return false;
8491
}
92+
8593
std::cout << std::endl;
94+
std::cout << "channels: " << input_buf.nchannels() << std::endl;
95+
96+
int alpha_channel = input_buf.spec().alpha_channel;
97+
std::cout << "alpha channel: " << alpha_channel << std::endl;
98+
99+
bool isValid = true;
100+
int inputCh = input_buf.nchannels();
101+
//
102+
// Valid with and without external alpha:
103+
// RGBA - 4 channels
104+
// Grayscale with alpha - 2 channels
105+
// RGB with alpha - 4 channels
106+
//
107+
// Valid only with external alpha:
108+
// Grayscale - 1 channel
109+
// RGB - 3 channels
110+
//
111+
switch (inputCh)
112+
{
113+
case 1:
114+
isValid = external_alpha ? true : false;
115+
grayscale = true;
116+
break;
117+
case 2:
118+
isValid = true;
119+
input_buf.specmod().channelnames[0] = "Y";
120+
input_buf.specmod().channelnames[0] = "A";
121+
input_buf.specmod().alpha_channel = 1;
122+
grayscale = true;
123+
break;
124+
case 3:
125+
isValid = external_alpha ? true : false;
126+
grayscale = false;
127+
break;
128+
case 4:
129+
isValid = true;
130+
input_buf.specmod().channelnames[3] = "A";
131+
input_buf.specmod().alpha_channel = 3;
132+
grayscale = false;
133+
break;
134+
default:
135+
isValid = false;
136+
std::cerr << "Error: Only Grayscale, RGB and RGBA images are supported" << std::endl;
137+
std::cerr << "Grayscale and RGB images must have an external alpha channel or use external alpha file" << std::endl;
138+
return false;
139+
break;
140+
}
86141

87142
// Create an ImageBuf object to store the result
88143
ImageBuf result_buf, rgba_buf;
@@ -92,16 +147,28 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
92147
Timer pushpull_timer;
93148

94149
if (external_alpha) {
95-
bool ok = ImageBufAlgo::mul(input_buf, input_buf, mask_pair.second);
150+
bool ok = ImageBufAlgo::mul(input_buf, input_buf, grayscale ? mask_pair.first : mask_pair.second);
96151
ok = ok && ImageBufAlgo::channel_append(rgba_buf, input_buf, mask_pair.first);
97152
if (!ok) {
98153
std::cerr << "Error: " << rgba_buf.geterror() << std::endl;
99154
return false;
100155
}
156+
// rename last channel to alpha and set alpha channel
157+
rgba_buf.specmod().channelnames[rgba_buf.nchannels() - 1] = "A";
158+
rgba_buf.specmod().alpha_channel = rgba_buf.nchannels() - 1;
101159
}
102160

103161
ImageBuf* input_buf_ptr = external_alpha ? &rgba_buf : &input_buf; // Use the multiplied RGBA buffer if have an external alpha
104162

163+
#if 0
164+
std::cout << "channels: " << input_buf_ptr->nchannels() << std::endl;
165+
//std::vector<std::string> chnames = input_buf_ptr->spec().channelnames;
166+
//for (int i = 0; i < input_buf_ptr->nchannels(); ++i)
167+
// std::cout << "channel " << i << " : " << chnames[i] << std::endl;
168+
169+
std::cout << "alpha channel: " << input_buf_ptr->spec().alpha_channel << std::endl;
170+
#endif
171+
105172
// Call fillholes_pushpull
106173
bool ok = ImageBufAlgo::fillholes_pushpull(result_buf, *input_buf_ptr);
107174

@@ -119,7 +186,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
119186
}
120187

121188
ImageSpec spec = result_buf.spec();
122-
spec.nchannels = 3; // Only write RGB channels
189+
spec.nchannels = grayscale ? 1 : 3; // Only write RGB channels
123190
spec.alpha_channel = -1; // No alpha channel
124191

125192
out->open(outputFileName, spec, ImageOutput::Create);
@@ -131,21 +198,6 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
131198
*progress_callback, nullptr);
132199
out->close();
133200

134-
#if 0
135-
136-
// Write RGBA buffer for debug
137-
spec = rgba_buf.spec();
138-
spec.nchannels = 4;
139-
140-
out->open("debug.exr", spec, ImageOutput::Create);
141-
142-
out->write_image(rgba_buf.spec().format, rgba_buf.localpixels(),
143-
rgba_buf.pixel_stride(), rgba_buf.scanline_stride(), rgba_buf.z_stride(),
144-
*progress_callback, nullptr);
145-
out->close();
146-
147-
#endif
148-
149201
std::cout << std::endl << "Total processing time : " << g_timer.nowText() << std::endl;
150202

151203
return true;

src/processing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ bool doProcessing(QList<QUrl> urls) {
8282

8383
// Call the solidify_main function
8484
if (!solidify_main(fileNames[i].toStdString(), outName.toStdString(), mask_pair)) {
85+
system("pause");
8586
exit(-1);
8687
};
8788
}

src/ui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DropArea : public QLabel {
3333
setMinimumSize(400, 400);
3434
setMaximumSize(400, 400);
3535
setWindowFlags(Qt::WindowStaysOnTopHint);
36-
setWindowTitle("Solidify 1.1");
36+
setWindowTitle("Solidify 1.2");
3737
setText("Drag & drop files here"); // Set text
3838
setAlignment(Qt::AlignCenter); // Set alignment to center
3939

0 commit comments

Comments
 (0)