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

0001 /*
0002  *  SPDX-FileCopyrightText: 2008 Boudewijn Rempt boud @valdyas.org
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef _UTIL_H_
0007 #define _UTIL_H_
0008 
0009 #include <simpletest.h>
0010 #include <QBitArray>
0011 
0012 #include <KisDocument.h>
0013 #include <KoDocumentInfo.h>
0014 #include <KoColorSpaceRegistry.h>
0015 #include <KoShapeContainer.h>
0016 #include <KoColorSpace.h>
0017 #include <KoPathShape.h>
0018 
0019 #include <kis_count_visitor.h>
0020 #include "kis_types.h"
0021 #include "filter/kis_filter_registry.h"
0022 #include "filter/kis_filter_configuration.h"
0023 #include "filter/kis_filter.h"
0024 #include "KisPart.h"
0025 #include "kis_image.h"
0026 #include "kis_pixel_selection.h"
0027 #include "kis_group_layer.h"
0028 #include "kis_paint_layer.h"
0029 #include "kis_clone_layer.h"
0030 #include "kis_adjustment_layer.h"
0031 #include "kis_shape_layer.h"
0032 #include "kis_filter_mask.h"
0033 #include "kis_transparency_mask.h"
0034 #include "kis_selection_mask.h"
0035 #include "kis_selection.h"
0036 #include "kis_fill_painter.h"
0037 #include "kis_shape_selection.h"
0038 #include "kis_default_bounds.h"
0039 #include "KisImageResolutionProxy.h"
0040 #include "KisDumbTransformMaskParams.h"
0041 #include <KisGlobalResourcesInterface.h>
0042 
0043 
0044 KisSelectionSP createPixelSelection(KisPaintDeviceSP paintDevice)
0045 {
0046     KisSelectionSP pixelSelection = new KisSelection(new KisSelectionDefaultBounds(paintDevice), KisImageResolutionProxy::identity());
0047 
0048     KisFillPainter gc(pixelSelection->pixelSelection());
0049     gc.fillRect(10, 10, 200, 200, KoColor(gc.device()->colorSpace()));
0050     gc.fillRect(150, 150, 200, 200, KoColor(QColor(100, 100, 100, 100), gc.device()->colorSpace()));
0051     gc.end();
0052 
0053     return pixelSelection;
0054 }
0055 
0056 KisSelectionSP createVectorSelection(KisPaintDeviceSP paintDevice, KisImageSP image, KoShapeControllerBase *shapeController)
0057 {
0058     KisSelectionSP selection = new KisSelection(new KisSelectionDefaultBounds(paintDevice), toQShared(new KisImageResolutionProxy(image)));
0059     KoPathShape* path = new KoPathShape();
0060     path->setShapeId(KoPathShapeId);
0061     path->moveTo(QPointF(10, 10));
0062     path->lineTo(QPointF(10, 10) + QPointF(100, 0));
0063     path->lineTo(QPointF(100, 100));
0064     path->lineTo(QPointF(10, 10) + QPointF(0, 100));
0065     path->close();
0066     path->normalize();
0067     KisShapeSelection* shapeSelection = new KisShapeSelection(shapeController, selection);
0068     shapeSelection->addShape(path);
0069     selection->convertToVectorSelectionNoUndo(shapeSelection);
0070 
0071     return selection;
0072 }
0073 
0074 QTransform createTestingTransform() {
0075     return QTransform(1,2,3,4,5,6,7,8,9);
0076 }
0077 
0078 KisDocument* createCompleteDocument()
0079 {
0080     KisImageSP image = new KisImage(0, 1024, 1024, KoColorSpaceRegistry::instance()->rgb8(), "test for roundtrip");
0081 
0082     KisDocument *doc = qobject_cast<KisDocument*>(KisPart::instance()->createDocument());
0083 
0084     doc->setCurrentImage(image);
0085     doc->documentInfo()->setAboutInfo("title", image->objectName());
0086 
0087     KisGroupLayerSP group1 = new KisGroupLayer(image, "group1", 50);
0088 
0089     KisGroupLayerSP group2 = new KisGroupLayer(image, "group2", 100);
0090 
0091     KisPaintLayerSP paintLayer1 = new KisPaintLayer(image, "paintlayer1", OPACITY_OPAQUE_U8);
0092     paintLayer1->setUserLocked(true);
0093     QBitArray channelFlags(4);
0094     channelFlags[0] = true;
0095     channelFlags[2] = true;
0096     paintLayer1->setChannelFlags(channelFlags);
0097 
0098     {
0099         KisFillPainter gc(paintLayer1->paintDevice());
0100         gc.fillRect(10, 10, 200, 200, KoColor(Qt::red, paintLayer1->paintDevice()->colorSpace()));
0101         gc.end();
0102     }
0103 
0104     KisPaintLayerSP paintLayer2 = new KisPaintLayer(image, "paintlayer2", OPACITY_TRANSPARENT_U8, KoColorSpaceRegistry::instance()->lab16());
0105     paintLayer2->setVisible(false);
0106     {
0107         KisFillPainter gc(paintLayer2->paintDevice());
0108         gc.fillRect(0, 0, 900, 1024, KoColor(QColor(10, 20, 30), paintLayer2->paintDevice()->colorSpace()));
0109         gc.end();
0110     }
0111 
0112 
0113     KisCloneLayerSP cloneLayer1 = new KisCloneLayer(group1, image, "clonelayer1", 150);
0114     cloneLayer1->setX(100);
0115     cloneLayer1->setY(100);
0116 
0117     KisSelectionSP pixelSelection = createPixelSelection(paintLayer1->paintDevice());
0118     KisFilterConfigurationSP kfc = KisFilterRegistry::instance()->get("pixelize")->defaultConfiguration(KisGlobalResourcesInterface::instance());
0119     Q_ASSERT(kfc);
0120     KisAdjustmentLayerSP adjustmentLayer1 = new KisAdjustmentLayer(image, "adjustmentLayer1", kfc->cloneWithResourcesSnapshot(), pixelSelection);
0121 
0122     KisSelectionSP vectorSelection = createVectorSelection(paintLayer2->paintDevice(), image, doc->shapeController());
0123     KisAdjustmentLayerSP adjustmentLayer2 = new KisAdjustmentLayer(image, "adjustmentLayer2", kfc->cloneWithResourcesSnapshot(), vectorSelection);
0124 
0125     image->addNode(paintLayer1);
0126     image->addNode(group1);
0127     image->addNode(paintLayer2, group1);
0128     image->addNode(group2);
0129     image->addNode(cloneLayer1, group2);
0130     image->addNode(adjustmentLayer1, group2);
0131 
0132 //    KoShapeContainer * parentContainer =
0133 //        dynamic_cast<KoShapeContainer*>(doc->shapeForNode(group1));
0134 
0135     KoPathShape* path = new KoPathShape();
0136     path->setShapeId(KoPathShapeId);
0137     path->moveTo(QPointF(10, 10));
0138     path->lineTo(QPointF(10, 10) + QPointF(100, 0));
0139     path->lineTo(QPointF(100, 100));
0140     path->lineTo(QPointF(10, 10) + QPointF(0, 100));
0141     path->close();
0142     path->normalize();
0143     KisShapeLayerSP shapeLayer = new KisShapeLayer(doc->shapeController(), image, "shapeLayer1", 75);
0144     shapeLayer->addShape(path);
0145     image->addNode(shapeLayer, group1);
0146     image->addNode(adjustmentLayer2, group1);
0147 
0148     KisFilterMaskSP filterMask1 = new KisFilterMask(image, "filterMask1");
0149 
0150     kfc = KisFilterRegistry::instance()->get("pixelize")->defaultConfiguration(KisGlobalResourcesInterface::instance());
0151     filterMask1->setFilter(kfc->cloneWithResourcesSnapshot());
0152     kfc = 0; // kfc cannot be shared!
0153 
0154     filterMask1->setSelection(createPixelSelection(paintLayer1->paintDevice()));
0155     image->addNode(filterMask1, paintLayer1);
0156 
0157     KisFilterMaskSP filterMask2 = new KisFilterMask(image, "filterMask2");
0158 
0159     kfc = KisFilterRegistry::instance()->get("pixelize")->defaultConfiguration(KisGlobalResourcesInterface::instance());
0160     filterMask2->setFilter(kfc->cloneWithResourcesSnapshot());
0161     kfc = 0; // kfc cannot be shared!
0162 
0163     filterMask2->setSelection(createVectorSelection(paintLayer2->paintDevice(), image, doc->shapeController()));
0164     image->addNode(filterMask2, paintLayer2);
0165 
0166     KisTransparencyMaskSP transparencyMask1 = new KisTransparencyMask(image, "transparencyMask1");
0167     transparencyMask1->setSelection(createPixelSelection(paintLayer1->paintDevice()));
0168     image->addNode(transparencyMask1, group1);
0169 
0170     KisTransparencyMaskSP transparencyMask2 = new KisTransparencyMask(image, "transparencyMask2");
0171     transparencyMask2->setSelection(createPixelSelection(paintLayer1->paintDevice()));
0172     image->addNode(transparencyMask2, group2);
0173 
0174     KisSelectionMaskSP selectionMask1 = new KisSelectionMask(image, "selectionMask1");
0175     image->addNode(selectionMask1, paintLayer1);
0176     selectionMask1->setSelection(createPixelSelection(paintLayer1->paintDevice()));
0177 
0178     KisSelectionMaskSP selectionMask2 = new KisSelectionMask(image, "selectionMask2");
0179     selectionMask2->setSelection(createPixelSelection(paintLayer2->paintDevice()));
0180     image->addNode(selectionMask2, paintLayer2);
0181 
0182     KisTransformMaskSP transformMask = new KisTransformMask(image, "testTransformMask");
0183     transformMask->setTransformParams(KisTransformMaskParamsInterfaceSP(
0184                                           new KisDumbTransformMaskParams(createTestingTransform())));
0185 
0186     image->addNode(transformMask, paintLayer2);
0187 
0188     return doc;
0189 }
0190 
0191 KisDocument *createEmptyDocument()
0192 {
0193     KisImageSP image = new KisImage(0, 1024, 1024, KoColorSpaceRegistry::instance()->rgb8(), "test for roundtrip");
0194 
0195     KisDocument *doc = qobject_cast<KisDocument*>(KisPart::instance()->createDocument());
0196 
0197     doc->setCurrentImage(image);
0198     doc->documentInfo()->setAboutInfo("title", image->objectName());
0199 
0200     return doc;
0201 }
0202 
0203 
0204 #endif