-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphaIfier.cpp
More file actions
136 lines (119 loc) · 4.65 KB
/
alphaIfier.cpp
File metadata and controls
136 lines (119 loc) · 4.65 KB
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
// AlphaIfier.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <algorithm> // For min/max functions
namespace fs = std::filesystem;
using namespace std;
vector<vector<unsigned char>> createAlphaMask(int width, int height, int featherWidth) {
vector<vector<unsigned char>> mask(height, vector<unsigned char>(width));
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int distanceToEdge = min({ x, y, width - 1 - x, height - 1 - y });
if (distanceToEdge < featherWidth) {
float alphaFactor = static_cast<float>(distanceToEdge) / featherWidth;
mask[y][x] = static_cast<unsigned char>(255 * alphaFactor);
}
else {
mask[y][x] = 255; // Fully opaque
}
}
}
return mask;
}
vector<string> verifyDimensionsAndListFiles(const string& path, int& commonWidth, int& commonHeight) {
vector<string> files;
bool first = true;
for (const auto& entry : fs::directory_iterator(path)) {
if (entry.path().extension() == ".png") {
int width, height, channels;
stbi_info(entry.path().string().c_str(), &width, &height, &channels);
if (first) {
commonWidth = width;
commonHeight = height;
first = false;
}
else if (width != commonWidth || height != commonHeight) {
cout << "Dimension mismatch found in file: " << entry.path().filename() << endl;
continue;
}
files.push_back(entry.path().string());
}
}
return files;
}
void applyAlphaFeathering(const string& filePath, const vector<vector<unsigned char>>& alphaMask) {
int width, height, channels;
unsigned char* image = stbi_load(filePath.c_str(), &width, &height, &channels, 4); // Force RGBA
if (!image) {
cout << "Failed to load image: " << filePath << endl;
return;
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
unsigned char* pixel = image + (y * width + x) * 4;
// Apply precomputed alpha value from the mask
pixel[3] = alphaMask[y][x];
}
}
// Ensure "output" directory exists
fs::create_directory("output");
string outputPath = "output/" + fs::path(filePath).filename().string();
// Write the modified image with applied alpha feathering to the output path
stbi_write_png(outputPath.c_str(), width, height, 4, image, width * 4);
stbi_image_free(image);
}
int main(int argc, char* argv[]) {
int featherWidth = 2; // Default value for feathering width
string path;
bool pathSet = false;
// Parse command line arguments
for (int i = 1; i < argc; ++i) {
string arg = argv[i];
if (arg == "-featherwidth" && i + 1 < argc) {
featherWidth = std::max(0, std::atoi(argv[++i])); // Ensure non-negative
}
else {
// Assume any non-flag argument is the path
path = arg;
pathSet = true;
}
}
// Validate path
if (!pathSet) {
cout << "Usage: " << argv[0] << " [-featherwidth N] <path to PNG or folder>" << endl;
return 1;
}
if (fs::is_directory(path)) {
int commonWidth = 0, commonHeight = 0;
auto files = verifyDimensionsAndListFiles(path, commonWidth, commonHeight);
if (!files.empty()) {
// Create alpha mask based on the first image's dimensions and the specified featherWidth
auto alphaMask = createAlphaMask(commonWidth, commonHeight, featherWidth);
for (const auto& file : files) {
cout << "Processing file: " << file << endl; // Log the file being processed
applyAlphaFeathering(file, alphaMask);
}
}
}
else if (fs::path(path).extension() == ".png") {
int width, height, channels;
// Use stbi_info to get the image dimensions
stbi_info(path.c_str(), &width, &height, &channels);
auto alphaMask = createAlphaMask(width, height, featherWidth);
cout << "Processing file: " << path << endl; // Log the file being processed
applyAlphaFeathering(path, alphaMask);
}
else {
cout << "Invalid input. Please provide a path to a PNG file or a directory." << endl;
return 1;
}
cout << "Processing completed." << endl;
return 0;
}