Streamline standalone thumbnailer
This commit is contained in:
+53
-35
@@ -1,62 +1,80 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QCommandLineParser>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "../rawimage.h"
|
||||
#include "../loadimage.h"
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "stb_image_write.h"
|
||||
|
||||
bool OpenGLES = false;
|
||||
bool loadXISF(const LibXISF::ByteArray &data, std::shared_ptr<RawImage> &rawImage);
|
||||
bool loadFITS(const LibXISF::ByteArray &data, std::shared_ptr<RawImage> &rawImage);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
std::vector<std::string> args;
|
||||
for(int i=0; i<argc; i++)
|
||||
args.push_back(argv[i]);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.addOption({{"s", "size"}, "Size of the thumbnail in pixels (default: 128)", "size", "128"});
|
||||
parser.addPositionalArgument("input", "Input image file");
|
||||
parser.addPositionalArgument("output", "Output image file");
|
||||
parser.addHelpOption();
|
||||
|
||||
parser.process(a);
|
||||
|
||||
QStringList args = parser.positionalArguments();
|
||||
|
||||
if(args.size() < 2)
|
||||
if(args.size() < 3)
|
||||
return 1;
|
||||
|
||||
QString input = args[0];
|
||||
QString output = args[1];
|
||||
std::string input = args[1];
|
||||
std::string output = args[2];
|
||||
|
||||
ImageInfoData info;
|
||||
std::shared_ptr<RawImage> rawImage;
|
||||
if(!loadImage(input, info, rawImage))
|
||||
|
||||
LibXISF::ByteArray data;
|
||||
std::ifstream fr;
|
||||
fr.open(input, std::ios_base::in | std::ios_base::binary);
|
||||
if(!fr.is_open())
|
||||
return 2;
|
||||
|
||||
if(!rawImage)
|
||||
fr.seekg(0, std::ios_base::end);
|
||||
size_t len = fr.tellg();
|
||||
fr.seekg(0, std::ios_base::beg);
|
||||
data.resize(len);
|
||||
fr.read(data.data(), len);
|
||||
if(fr.bad())
|
||||
return 3;
|
||||
|
||||
bool ok;
|
||||
int size = parser.value("s").toInt(&ok);
|
||||
if(!ok)
|
||||
size = 128;
|
||||
if(input.find(".xisf") != std::string::npos)
|
||||
{
|
||||
if(!loadXISF(data, rawImage))
|
||||
return 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!loadFITS(data, rawImage))
|
||||
return 4;
|
||||
}
|
||||
|
||||
if(!rawImage)
|
||||
return 5;
|
||||
|
||||
uint32_t thumbSize = 256;
|
||||
|
||||
uint32_t w = rawImage->width();
|
||||
uint32_t h = rawImage->height();
|
||||
|
||||
uint32_t cw = thumbSize;
|
||||
uint32_t ch = thumbSize;
|
||||
if (w > h)
|
||||
ch = h * thumbSize / w;
|
||||
else
|
||||
cw = w * thumbSize / h;
|
||||
|
||||
QSize rect(rawImage->width(), rawImage->height());
|
||||
rect.scale(size, size, Qt::KeepAspectRatio);
|
||||
rawImage->calcStats();
|
||||
rawImage->resize(rect.width(), rect.height());
|
||||
if(rawImage->imageStats().m_median[0] < rawImage->norm() * 0.2f)
|
||||
rawImage->resize(cw, ch);
|
||||
if(rawImage->imageStats().m_median[0] < rawImage->norm() * 0.1f)
|
||||
{
|
||||
MTFParam mtfParams = rawImage->calcMTFParams(true);
|
||||
rawImage->applySTF(mtfParams);
|
||||
}
|
||||
rawImage->convertToType(RawImage::UINT8);
|
||||
|
||||
QImage img;
|
||||
if(rawImage->channels() == 1)
|
||||
img = QImage((const uchar*)rawImage->data(), rawImage->width(), rawImage->height(), rawImage->widthBytes(), QImage::Format_Grayscale8);
|
||||
stbi_write_png(output.c_str(), cw, ch, 1, rawImage->data(), rawImage->widthBytes());
|
||||
else
|
||||
img = QImage((const uchar*)rawImage->data(), rawImage->width(), rawImage->height(), rawImage->widthBytes(), QImage::Format_RGBA8888);
|
||||
|
||||
if(!img.save(output, "png"))
|
||||
return 4;
|
||||
stbi_write_png(output.c_str(), cw, ch, 4, rawImage->data(), rawImage->widthBytes());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user