File indexing completed on 2025-01-05 04:01:16

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "raster_format.hpp"
0008 
0009 #include <QFileInfo>
0010 
0011 #include "io/raster/raster_mime.hpp"
0012 #include "utils/trace_wrapper.hpp"
0013 
0014 glaxnimate::io::Autoreg<glaxnimate::io::raster::RasterMime> glaxnimate::io::raster::RasterMime::autoreg;
0015 glaxnimate::io::Autoreg<glaxnimate::io::raster::RasterFormat> glaxnimate::io::raster::RasterFormat::autoreg;
0016 
0017 
0018 QStringList glaxnimate::io::raster::RasterFormat::extensions() const
0019 {
0020     QStringList formats;
0021     for ( const auto& fmt : QImageReader::supportedImageFormats() )
0022         if ( fmt != "gif" && fmt != "webp" && fmt != "svg" )
0023             formats << QString::fromUtf8(fmt);
0024     return formats;
0025 }
0026 
0027 bool glaxnimate::io::raster::RasterFormat::on_open(QIODevice& dev, const QString& filename, model::Document* document, const QVariantMap& settings)
0028 {
0029     auto main = document->assets()->add_comp_no_undo();
0030 
0031     main->animation->last_frame.set(main->fps.get());
0032 
0033 
0034     model::FrameTime default_time = settings["default_time"].toFloat();
0035     main->animation->last_frame.set(default_time == 0 ? default_time : 180);
0036 
0037 #ifndef WITHOUT_POTRACE
0038     if ( settings.value("trace", {}).toBool() )
0039     {
0040         QImageReader reader;
0041         reader.setDevice(&dev);
0042         QImage image = reader.read();
0043         if ( image.isNull() )
0044             return false;
0045 
0046         utils::trace::TraceWrapper trace(main, image, filename);
0047         std::vector<QRgb> colors;
0048         std::vector<utils::trace::TraceWrapper::TraceResult> result;
0049         auto preset = trace.preset_suggestion();
0050         trace.trace_preset(preset, 16, colors, result);
0051         trace.apply(result, preset == utils::trace::TraceWrapper::PixelPreset ? 0 : 1);
0052 
0053         return true;
0054     }
0055 #endif
0056 
0057     auto bmp = document->assets()->images->values.insert(std::make_unique<model::Bitmap>(document));
0058     if ( auto file = qobject_cast<QFile*>(&dev) )
0059         bmp->filename.set(file->fileName());
0060     else
0061         bmp->data.set(dev.readAll());
0062     auto img = std::make_unique<model::Image>(document);
0063     img->image.set(bmp);
0064     QPointF p(bmp->pixmap().width() / 2.0, bmp->pixmap().height() / 2.0);
0065     if ( !filename.isEmpty() )
0066         img->name.set(QFileInfo(filename).baseName());
0067     img->transform->anchor_point.set(p);
0068     img->transform->position.set(p);
0069     main->shapes.insert(std::move(img));
0070     main->width.set(bmp->pixmap().width());
0071     main->height.set(bmp->pixmap().height());
0072     return !bmp->pixmap().isNull();
0073 }