File indexing completed on 2024-12-22 04:15:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_gif_import.h"
0008 
0009 #include <QCheckBox>
0010 #include <QBuffer>
0011 #include <QSlider>
0012 #include <QApplication>
0013 
0014 #include <kpluginfactory.h>
0015 #include <QFileInfo>
0016 
0017 #include <KoColorSpace.h>
0018 #include <KoColorSpaceRegistry.h>
0019 
0020 #include <kis_transaction.h>
0021 #include <kis_paint_device.h>
0022 #include <kis_image.h>
0023 #include <kis_paint_layer.h>
0024 #include <kis_node.h>
0025 #include <kis_group_layer.h>
0026 #include <KisDocument.h>
0027 #include <KisImportExportAdditionalChecks.h>
0028 
0029 #include "qgiflibhandler.h"
0030 
0031 K_PLUGIN_FACTORY_WITH_JSON(KisGIFImportFactory, "krita_gif_import.json", registerPlugin<KisGIFImport>();)
0032 
0033 KisGIFImport::KisGIFImport(QObject *parent, const QVariantList &)
0034     : KisImportExportFilter(parent)
0035 {
0036 }
0037 
0038 KisGIFImport::~KisGIFImport()
0039 {
0040 }
0041 
0042 KisImportExportErrorCode KisGIFImport::convert(KisDocument *document, QIODevice *io,  KisPropertiesConfigurationSP configuration)
0043 {
0044     Q_UNUSED(configuration);
0045 
0046     QImage img;
0047     bool result = false;
0048     QGIFLibHandler handler;
0049 
0050 
0051     handler.setDevice(io);
0052 
0053     if (!io->isReadable()) {
0054         return ImportExportCodes::NoAccessToRead;
0055     }
0056 
0057     if (handler.canRead()) {
0058         result = handler.read(&img);
0059     } else {
0060         // handler.canRead() checks for the flag in the file; if it can't read it, maybe the format is incorrect
0061         return ImportExportCodes::FileFormatIncorrect;
0062     }
0063 
0064     if (result == false) {
0065         return ImportExportCodes::FileFormatIncorrect;
0066     }
0067 
0068     const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->rgb8();
0069     KisImageSP image = new KisImage(document->createUndoStore(), img.width(), img.height(), colorSpace, "imported from gif");
0070 
0071     KisPaintLayerSP layer = new KisPaintLayer(image, image->nextLayerName(), 255);
0072     layer->paintDevice()->convertFromQImage(img, 0, 0, 0);
0073     image->addNode(layer.data(), image->rootLayer().data());
0074 
0075     document->setCurrentImage(image);
0076     return ImportExportCodes::OK;
0077 
0078 }
0079 
0080 #include "kis_gif_import.moc"
0081