1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
* Copyright (c) 2011 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "stroke_testing_utils.h"
#include <QtTest>
#include <QDir>
#include <KoColor.h>
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
#include <KoCompositeOpRegistry.h>
#include <brushengine/kis_paintop_preset.h>
#include <resources/KoPattern.h>
#include "kis_canvas_resource_provider.h"
#include "kis_image.h"
#include "kis_paint_device.h"
#include "kis_paint_layer.h"
#include "kis_group_layer.h"
#include "testutil.h"
KisImageSP utils::createImage(KisUndoStore *undoStore, const QSize &imageSize) {
QRect imageRect(0,0,imageSize.width(),imageSize.height());
const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
KisImageSP image = new KisImage(undoStore, imageRect.width(), imageRect.height(), cs, "stroke test");
KisPaintLayerSP paintLayer1 = new KisPaintLayer(image, "paint1", OPACITY_OPAQUE_U8);
KisPaintLayerSP paintLayer2 = new KisPaintLayer(image, "paint2", OPACITY_OPAQUE_U8);
KisPaintLayerSP paintLayer3 = new KisPaintLayer(image, "paint3", OPACITY_OPAQUE_U8);
KisPaintLayerSP paintLayer4 = new KisPaintLayer(image, "paint4", OPACITY_OPAQUE_U8);
KisPaintLayerSP paintLayer5 = new KisPaintLayer(image, "paint5", OPACITY_OPAQUE_U8);
image->lock();
image->addNode(paintLayer1);
image->addNode(paintLayer2);
image->addNode(paintLayer3);
image->addNode(paintLayer4);
image->addNode(paintLayer5);
image->unlock();
return image;
}
KoCanvasResourceManager* utils::createResourceManager(KisImageWSP image,
KisNodeSP node,
const QString &presetFileName)
{
KoCanvasResourceManager *manager = new KoCanvasResourceManager();
QVariant i;
i.setValue(KoColor(Qt::black, image->colorSpace()));
manager->setResource(KoCanvasResourceManager::ForegroundColor, i);
i.setValue(KoColor(Qt::white, image->colorSpace()));
manager->setResource(KoCanvasResourceManager::BackgroundColor, i);
i.setValue(static_cast<void*>(0));
manager->setResource(KisCanvasResourceProvider::CurrentPattern, i);
manager->setResource(KisCanvasResourceProvider::CurrentGradient, i);
manager->setResource(KisCanvasResourceProvider::CurrentGeneratorConfiguration, i);
if(!node) {
node = image->root();
while(node && !dynamic_cast<KisPaintLayer*>(node.data())) {
node = node->firstChild();
}
Q_ASSERT(node && dynamic_cast<KisPaintLayer*>(node.data()));
}
i.setValue(node);
manager->setResource(KisCanvasResourceProvider::CurrentKritaNode, i);
KisPaintOpPresetSP preset;
if (!presetFileName.isEmpty()) {
QString fullFileName = TestUtil::fetchDataFileLazy(presetFileName);
preset = new KisPaintOpPreset(fullFileName);
bool presetValid = preset->load();
Q_ASSERT(presetValid); Q_UNUSED(presetValid);
i.setValue(preset);
manager->setResource(KisCanvasResourceProvider::CurrentPaintOpPreset, i);
}
i.setValue(COMPOSITE_OVER);
manager->setResource(KisCanvasResourceProvider::CurrentCompositeOp, i);
i.setValue(false);
manager->setResource(KisCanvasResourceProvider::MirrorHorizontal, i);
i.setValue(false);
manager->setResource(KisCanvasResourceProvider::MirrorVertical, i);
i.setValue(1.0);
manager->setResource(KisCanvasResourceProvider::Opacity, i);
i.setValue(1.0);
manager->setResource(KisCanvasResourceProvider::HdrExposure, i);
return manager;
}
utils::StrokeTester::StrokeTester(const QString &name, const QSize &imageSize, const QString &presetFilename)
: m_name(name),
m_imageSize(imageSize),
m_presetFilename(presetFilename),
m_numIterations(1),
m_baseFuzziness(1)
{
}
utils::StrokeTester::~StrokeTester()
{
}
void utils::StrokeTester::setNumIterations(int value)
{
m_numIterations = value;
}
void utils::StrokeTester::setBaseFuzziness(int value)
{
m_baseFuzziness = value;
}
void utils::StrokeTester::testSimpleStroke()
{
testOneStroke(false, true, false, true);
}
void utils::StrokeTester::test()
{
testOneStroke(false, false, false);
testOneStroke(false, true, false);
testOneStroke(true, false, false);
testOneStroke(true, true, false);
// The same but with updates (compare against projection)
testOneStroke(false, false, false, true);
testOneStroke(false, true, false, true);
testOneStroke(true, false, false, true);
testOneStroke(true, true, false, true);
// The same, but with an external layer
testOneStroke(false, false, true);
testOneStroke(false, true, true);
testOneStroke(true, false, true);
testOneStroke(true, true, true);
}
void utils::StrokeTester::benchmark()
{
// not cancelled, indirect painting, internal, no updates, no qimage
doStroke(false, true, false, false, false);
}
void utils::StrokeTester::testOneStroke(bool cancelled,
bool indirectPainting,
bool externalLayer,
bool testUpdates)
{
QString testName = formatTestName(m_name,
cancelled,
indirectPainting,
externalLayer);
dbgKrita << "Testcase:" << testName
<< "(comare against " << (testUpdates ? "projection" : "layer") << ")";
QImage resultImage;
resultImage = doStroke(cancelled, indirectPainting, externalLayer, testUpdates);
QImage refImage;
refImage.load(referenceFile(testName));
QPoint temp;
if(!TestUtil::compareQImages(temp, refImage, resultImage, m_baseFuzziness, m_baseFuzziness)) {
refImage.save(dumpReferenceFile(testName));
resultImage.save(resultFile(testName));
QFAIL("Images do not coincide");
}
}
QString utils::StrokeTester::formatTestName(const QString &baseName,
bool cancelled,
bool indirectPainting,
bool externalLayer)
{
QString result = baseName;
result += "_" + m_presetFilename;
result += indirectPainting ? "_indirect" : "_incremental";
result += cancelled ? "_cancelled" : "_finished";
result += externalLayer ? "_external" : "_internal";
return result;
}
QString utils::StrokeTester::referenceFile(const QString &testName)
{
QString path =
QString(FILES_DATA_DIR) + QDir::separator() +
m_name + QDir::separator();
path += testName;
path += ".png";
return path;
}
QString utils::StrokeTester::dumpReferenceFile(const QString &testName)
{
QString path = QString(FILES_OUTPUT_DIR) + QDir::separator();
path += testName;
path += "_expected";
path += ".png";
return path;
}
QString utils::StrokeTester::resultFile(const QString &testName)
{
QString path = QString(FILES_OUTPUT_DIR) + QDir::separator();
path += testName;
path += ".png";
return path;
}
QImage utils::StrokeTester::doStroke(bool cancelled,
bool indirectPainting,
bool externalLayer,
bool testUpdates,
bool needQImage)
{
KisImageSP image = utils::createImage(0, m_imageSize);
KoCanvasResourceManager *manager = utils::createResourceManager(image, 0, m_presetFilename);
KisNodeSP currentNode;
for (int i = 0; i < m_numIterations; i++) {
modifyResourceManager(manager, image, i);
KisResourcesSnapshotSP resources =
new KisResourcesSnapshot(image,
image->rootLayer()->firstChild(),
manager);
if(externalLayer) {
KisNodeSP externalNode = new KisPaintLayer(0, "extlyr", OPACITY_OPAQUE_U8, image->colorSpace());
resources->setCurrentNode(externalNode);
Q_ASSERT(resources->currentNode() == externalNode);
}
initImage(image, resources->currentNode(), i);
KisStrokeStrategy *stroke = createStroke(indirectPainting, resources, image);
m_strokeId = image->startStroke(stroke);
addPaintingJobs(image, resources, i);
if(!cancelled) {
image->endStroke(m_strokeId);
}
else {
image->cancelStroke(m_strokeId);
}
image->waitForDone();
currentNode = resources->currentNode();
}
beforeCheckingResult(image, currentNode);
QImage resultImage;
if(needQImage) {
KisPaintDeviceSP device = testUpdates ?
image->projection() :
currentNode->paintDevice();
resultImage = device->convertToQImage(0, 0, 0, image->width(), image->height());
}
image = 0;
delete manager;
return resultImage;
}
void utils::StrokeTester::modifyResourceManager(KoCanvasResourceManager *manager,
KisImageWSP image, int iteration)
{
Q_UNUSED(iteration);
modifyResourceManager(manager, image);
}
void utils::StrokeTester::modifyResourceManager(KoCanvasResourceManager *manager,
KisImageWSP image)
{
Q_UNUSED(manager);
Q_UNUSED(image);
}
void utils::StrokeTester::initImage(KisImageWSP image, KisNodeSP activeNode, int iteration)
{
Q_UNUSED(iteration);
initImage(image, activeNode);
}
void utils::StrokeTester::initImage(KisImageWSP image, KisNodeSP activeNode)
{
Q_UNUSED(image);
Q_UNUSED(activeNode);
}
void utils::StrokeTester::addPaintingJobs(KisImageWSP image,
KisResourcesSnapshotSP resources,
int iteration)
{
Q_UNUSED(iteration);
addPaintingJobs(image, resources);
}
void utils::StrokeTester::addPaintingJobs(KisImageWSP image,
KisResourcesSnapshotSP resources)
{
Q_UNUSED(image);
Q_UNUSED(resources);
}
void utils::StrokeTester::beforeCheckingResult(KisImageWSP image, KisNodeSP activeNode)
{
Q_UNUSED(image);
Q_UNUSED(activeNode);
}
|