2024-09-09 21:12:05 +00:00
|
|
|
#include "texturecrafter.h"
|
|
|
|
|
|
|
|
TextureCrafter::TextureCrafter(QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
{}
|
|
|
|
|
2024-09-10 22:07:51 +00:00
|
|
|
QUrl TextureCrafter::packChannels(QVector<QUrl> imagePaths) {
|
2024-09-10 14:11:13 +00:00
|
|
|
|
2024-09-09 21:12:05 +00:00
|
|
|
QVector<QImage> sourceImages;
|
|
|
|
int width = 0, height = 0;
|
|
|
|
QImage::Format format = QImage::Format_RGB888;
|
|
|
|
|
|
|
|
for (int i = 0; i < imagePaths.length(); i++) {
|
|
|
|
// if the ui layer did its job this will always be a file:// url
|
2024-09-10 14:11:13 +00:00
|
|
|
QString rawPath = imagePaths[i].toLocalFile();
|
2024-09-09 21:12:05 +00:00
|
|
|
QImage newImage(rawPath); // would this cause a use after free idk
|
|
|
|
|
|
|
|
// we just use the first image's dimensions and force the others to conform hehe
|
|
|
|
if (width == 0) {
|
|
|
|
width = newImage.width();
|
|
|
|
}
|
|
|
|
if (height == 0) {
|
|
|
|
height = newImage.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceImages.append(newImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage outImage(width, height, format);
|
|
|
|
|
2024-09-09 21:27:35 +00:00
|
|
|
// do the channel packing!!
|
2024-09-09 21:12:05 +00:00
|
|
|
// theres probably a faster way to do this but eh
|
|
|
|
for (int i = 0; i < width; i++) {
|
|
|
|
for (int j = 0; j < height; j++) {
|
|
|
|
int r, g, b;
|
|
|
|
|
|
|
|
// too eepy for dry
|
|
|
|
r = sourceImages.at(0).pixelColor(i, j).red();
|
|
|
|
g = sourceImages.at(1).pixelColor(i, j).green();
|
|
|
|
b = sourceImages.at(2).pixelColor(i, j).blue();
|
|
|
|
|
|
|
|
QColor newColor(r, g, b);
|
|
|
|
|
|
|
|
outImage.setPixelColor(i, j, newColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 21:27:35 +00:00
|
|
|
// write it to a temp file
|
|
|
|
if (outDir.isValid()) {
|
|
|
|
QString outFileName = outDir.path();
|
|
|
|
outFileName.append("/out.png");
|
|
|
|
|
2024-09-14 20:32:51 +00:00
|
|
|
printf("writing to %s\n", outFileName.toLatin1().data());
|
2024-09-10 14:11:13 +00:00
|
|
|
|
|
|
|
//TODO: use libpng and do progressive write to update the progress bar
|
|
|
|
if (outImage.save(outFileName)) {
|
|
|
|
printf("cool ^-^\n");
|
2024-09-10 22:07:51 +00:00
|
|
|
return QUrl::fromLocalFile(outFileName);
|
2024-09-10 14:11:13 +00:00
|
|
|
} else {
|
|
|
|
//TODO: return some error value to show the user
|
2024-09-14 20:32:51 +00:00
|
|
|
printf("that dream is fucked it is fucking fucked (write failed ;-;)\n");
|
2024-09-10 14:11:13 +00:00
|
|
|
}
|
2024-09-09 21:27:35 +00:00
|
|
|
} else {
|
2024-09-14 20:32:51 +00:00
|
|
|
printf("chat its so over (output dir invalid </3)\n");
|
2024-09-09 21:27:35 +00:00
|
|
|
}
|
2024-09-09 21:12:05 +00:00
|
|
|
|
2024-09-10 22:07:51 +00:00
|
|
|
return QUrl();
|
2024-09-09 21:12:05 +00:00
|
|
|
//do we need to close these??? idk lets hope qt handles it
|
|
|
|
}
|