void KisImageViewConverterTest::testViewToDocument()
{
    const KoColorSpace * colorSpace = KoColorSpaceRegistry::instance()->rgb8();
    KisImageSP image = new KisImage(0, 512, 512, colorSpace, "test");
    KisImageViewConverter viewConverter(image);

    image->setResolution(1.38888888, 1.38888888);

    QVERIFY(viewConverter.viewToDocument(QPointF(0.0, 0.0))
            == QPointF(0.0, 0.0));

    QPointF f = viewConverter.viewToDocument(QPointF(5, 5));
    QVERIFY(f.x() < 3.7 && f.x() > 3.5 && f.y() < 3.7 && f.y() > 3.5);

    QRectF r = viewConverter.viewToDocument(QRectF(0.0, 0.0, 5, 5));
    QVERIFY(r.width() < 3.7 && r.width() > 3.5
            && r.height() < 3.7 && r.height() > 3.5);

    QSizeF s = viewConverter.viewToDocument(QSizeF(1.0, 1.0));
    QVERIFY(s.width() < 0.721 && s.width() > 0.719
            && s.height() < 0.721 && s.height() > 0.719);

    double x = viewConverter.viewToDocumentX(1.0);
    QVERIFY(x < 0.721 && x > 0.719);

    double y = viewConverter.viewToDocumentY(1.0);
    QVERIFY(y < 0.721 && y > 0.719);

}
void KisImageViewConverterTest::testDocumentToView()
{
    const KoColorSpace * colorSpace = KoColorSpaceRegistry::instance()->rgb8();
    KisImageSP image = new KisImage(0, 512, 512, colorSpace, "test");
    KisImageViewConverter viewConverter(image);

    image->setResolution(1.38888888, 1.38888888);

    QVERIFY(viewConverter.documentToView(QPointF(0.0, 0.0))
            == QPointF(0.0, 0.0));

    QPointF f = viewConverter.documentToView(QPointF(3.2, 5.2));
    QVERIFY(f.x() < 4.44445 && f.x() > 4.44443 && f.y() < 7.22223 && f.y() > 7.22221);

    QRectF r = viewConverter.documentToView(QRectF(0.0, 0.0, 10.0, 10.0));
    QVERIFY(r.width() < 13.889 && r.width() > 13.8888
            && r.height() < 13.889 && r.height() > 13.8888);

    QSizeF s = viewConverter.documentToView(QSizeF(1.0, 1.0));
    QVERIFY(s.width() < 1.3888889 && s.width() > 1.388887
            && s.height() < 1.3888889 && s.height() > 1.388887);

    double x = viewConverter.documentToViewX(1.0);
    QVERIFY(x < 1.3888889 && x > 1.388887);

    double y = viewConverter.documentToViewY(1.0);
    QVERIFY(y < 1.3888889 && y > 1.388887);

}
void KisPrescaledProjectionTest::testScalingUndeferredSmoothing()
{
    // Set up a nice image
    QImage qimage(QString(FILES_DATA_DIR) + QDir::separator() + "lena.png");

    // Undo adapter not necessary
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisImageSP image = new KisImage(0, qimage.width(), qimage.height(), cs, "projection test");

    // 300 dpi recalculated to pixels per point (of which there are 72
    // to the inch)
    image->setResolution(100, 100);

    KisPaintLayerSP layer = new KisPaintLayer(image, "test", OPACITY_OPAQUE_U8, cs);
    image->addNode(layer.data(), image->rootLayer(), 0);
    layer->paintDevice()->convertFromQImage(qimage, 0);

    KisPrescaledProjection projection;
    KisCoordinatesConverter converter;
    
    converter.setImage(image);
    projection.setCoordinatesConverter(&converter);
    projection.setMonitorProfile(0,
                                 KoColorConversionTransformation::internalRenderingIntent(),
                                 KoColorConversionTransformation::internalConversionFlags());
    projection.setImage(image);

    testProjectionScenario(projection, &converter, "120dpi");

}
void KisPrescaledProjectionTest::benchmarkUpdate()
{
    QImage referenceImage(QString(FILES_DATA_DIR) + QDir::separator() + "lena.png");
    QRect imageRect = QRect(QPoint(0,0), referenceImage.size());

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisImageSP image = new KisImage(0, imageRect.width(), imageRect.height(), cs, "projection test");

    // set up 300dpi
    image->setResolution(300 / 72 , 300 / 72);

    KisPaintLayerSP layer = new KisPaintLayer(image, "paint1", OPACITY_OPAQUE_U8, cs);
    layer->paintDevice()->convertFromQImage(referenceImage, 0);

    image->addNode(layer, image->rootLayer(), 0);

    KisPrescaledProjection projection;

    KisCoordinatesConverter converter;
    converter.setImage(image);
    projection.setCoordinatesConverter(&converter);
    projection.setMonitorProfile(0,
                                 KoColorConversionTransformation::internalRenderingIntent(),
                                 KoColorConversionTransformation::internalConversionFlags());
    projection.setImage(image);

    // Emulate "Use same aspect as pixels"
    converter.setResolution(image->xRes(), image->yRes());

    converter.setZoom(1.0);

    KisUpdateInfoSP info = projection.updateCache(image->bounds());
    projection.recalculateCache(info);

    QCOMPARE(imageRect, QRect(0,0,512,512));

    QRect dirtyRect(0,0,20,20);
    const qint32 numShifts = 25;
    const QPoint offset(dirtyRect.width(),dirtyRect.height());

    //CALLGRIND_START_INSTRUMENTATION;

    QBENCHMARK {
        for(qint32 i = 0; i < numShifts; i++) {
            KisUpdateInfoSP tempInfo = projection.updateCache(dirtyRect);
            projection.recalculateCache(tempInfo);

            dirtyRect.translate(offset);
        }
    }

    //CALLGRIND_STOP_INSTRUMENTATION;

}
void KisImageViewConverterTest::testZoom()
{
    const KoColorSpace * colorSpace = KoColorSpaceRegistry::instance()->rgb8();
    KisImageSP image = new KisImage(0, 512, 512, colorSpace, "test");
    KisImageViewConverter viewConverter(image);

    image->setResolution(1.38888888, 5.38888888);

    qreal zoomX, zoomY;

    viewConverter.zoom(&zoomX, &zoomY);
    QVERIFY(zoomX < 1.388889 && zoomX > 1.3888887);
    QVERIFY(zoomY < 5.388889 && zoomY > 5.3888887);
}