texcrafting/texturecrafter.cpp

70 lines
2.1 KiB
C++

#include "texturecrafter.h"
TextureCrafter::TextureCrafter(QObject *parent)
: QObject{parent}
{}
QUrl TextureCrafter::packChannels(QVector<QUrl> imagePaths) {
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
QString rawPath = imagePaths[i].toLocalFile();
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);
// do the channel packing!!
// 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);
}
}
// write it to a temp file
if (outDir.isValid()) {
QString outFileName = outDir.path();
outFileName.append("/out.png");
printf("writing to %s\n", outFileName.toLatin1().data());
//TODO: use libpng and do progressive write to update the progress bar
if (outImage.save(outFileName)) {
printf("cool ^-^\n");
return QUrl::fromLocalFile(outFileName);
} else {
//TODO: return some error value to show the user
printf("that dream is fucked it is fucking fucked (write failed ;-;)\n");
}
} else {
printf("chat its so over (output dir invalid </3)\n");
}
return QUrl();
//do we need to close these??? idk lets hope qt handles it
}