void KisCoordinatesConverterTest::testMirroring() { KisImageSP image; KisCoordinatesConverter converter; initImage(&image, &converter); QSize widgetSize(500,400); QSize flakeSize(1000,1000); QRectF testRect(300, 100, 200, 200); converter.setImage(image); converter.setDocumentOffset(QPoint(200,100)); converter.setCanvasWidgetSize(widgetSize); QTransform imageToWidget; QTransform documentToWidget; QTransform flakeToWidget; QTransform viewportToWidget; converter.mirror(converter.imageCenterInWidgetPixel(), true, false); converter.setZoom(1.); // image pixels == flake pixels QRectF viewportRect = converter.imageToViewport(testRect); QRectF widgetRect = converter.viewportToWidget(viewportRect); QCOMPARE(widgetRect, QRectF(300,0,200,200)); QCOMPARE(viewportRect, QRectF(0,0,200,200)); QRectF roundTrip = converter.viewportToWidget(widgetRect); QCOMPARE(roundTrip, viewportRect); }
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 KisCoordinatesConverterTest::testRotation() { KisImageSP image; KisCoordinatesConverter converter; initImage(&image, &converter); QSize widgetSize(1000,500); QRectF testRect(800, 100, 300, 300); converter.setImage(image); converter.setDocumentOffset(QPoint(0,0)); converter.setCanvasWidgetSize(widgetSize); converter.rotate(converter.widgetCenterPoint(), 30); converter.setZoom(1.); QTransform viewportToWidget = converter.viewportToWidgetTransform(); QRectF boundingRect = viewportToWidget.mapRect(testRect); QRectF directRect = converter.viewportToWidget(testRect); QCOMPARE(boundingRect, directRect); QRectF referenceRect(QPointF(742.82,53.5898), QSizeF(409.808,409.808)); #define FUZZY(a,b) ((a)-(b) < 0.01) QVERIFY(FUZZY(boundingRect.top(), referenceRect.top())); QVERIFY(FUZZY(boundingRect.left(), referenceRect.left())); QVERIFY(FUZZY(boundingRect.width(), referenceRect.width())); QVERIFY(FUZZY(boundingRect.height(), referenceRect.height())); }
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 KisCoordinatesConverterTest::testImageCropping() { KisImageSP image; KisCoordinatesConverter converter; initImage(&image, &converter); converter.setImage(image); converter.setDocumentOffset(QPoint(0,0)); converter.setCanvasWidgetSize(QSize(500,500)); converter.setZoom(1.); // we do NOT crop here QCOMPARE(converter.viewportToImage(QRectF(900,900,200,200)), QRectF(900,900,200,200)); }
void KisCoordinatesConverterTest::testConversion() { KisImageSP image; KisCoordinatesConverter converter; initImage(&image, &converter); converter.setImage(image); converter.setDocumentOffset(QPoint(20,20)); converter.setCanvasWidgetSize(QSize(500,500)); converter.setZoom(1.); QRectF testRect(100,100,100,100); QCOMPARE(converter.imageToViewport(testRect), QRectF(80,80,100,100)); QCOMPARE(converter.viewportToImage(testRect), QRectF(120,120,100,100)); QCOMPARE(converter.widgetToViewport(testRect), QRectF(100,100,100,100)); QCOMPARE(converter.viewportToWidget(testRect), QRectF(100,100,100,100)); QCOMPARE(converter.widgetToDocument(testRect), QRectF(1.20,1.20,1,1)); QCOMPARE(converter.documentToWidget(testRect), QRectF(9980,9980,10000,10000)); QCOMPARE(converter.imageToDocument(testRect), QRectF(1,1,1,1)); QCOMPARE(converter.documentToImage(testRect), QRectF(10000,10000,10000,10000)); converter.setZoom(0.5); QCOMPARE(converter.imageToViewport(testRect), QRectF(30,30,50,50)); QCOMPARE(converter.viewportToImage(testRect), QRectF(240,240,200,200)); QCOMPARE(converter.widgetToViewport(testRect), QRectF(100,100,100,100)); QCOMPARE(converter.viewportToWidget(testRect), QRectF(100,100,100,100)); QCOMPARE(converter.widgetToDocument(testRect), QRectF(2.4,2.4,2,2)); QCOMPARE(converter.documentToWidget(testRect), QRectF(4980,4980,5000,5000)); QCOMPARE(converter.imageToDocument(testRect), QRectF(1,1,1,1)); QCOMPARE(converter.documentToImage(testRect), QRectF(10000,10000,10000,10000)); }
void KisCoordinatesConverterTest::testConsistency() { KisImageSP image; KisCoordinatesConverter converter; initImage(&image, &converter); converter.setImage(image); converter.setDocumentOffset(QPoint(20,30)); converter.setCanvasWidgetSize(QSize(500,500)); QRectF testRect(100,100,100,100); QTransform imageToWidget; QTransform documentToWidget; QTransform viewportToWidget; converter.setZoom(0.5); imageToWidget = converter.imageToWidgetTransform(); documentToWidget = converter.documentToWidgetTransform(); viewportToWidget = converter.viewportToWidgetTransform(); QRectF fromImage = converter.viewportToWidget(converter.imageToViewport(testRect)); QRectF fromDocument = converter.documentToWidget(testRect); QRectF fromViewport = converter.viewportToWidget(testRect); CHECK_TRANSFORM(imageToWidget, testRect, fromImage); CHECK_TRANSFORM(documentToWidget, testRect, fromDocument); CHECK_TRANSFORM(viewportToWidget, testRect, fromViewport); }
void KisCoordinatesConverterTest::testTransformations() { KisImageSP image; KisCoordinatesConverter converter; initImage(&image, &converter); converter.setImage(image); converter.setDocumentOffset(QPoint(20,30)); converter.setCanvasWidgetSize(QSize(500,500)); QRectF testRect(100,100,100,100); QTransform imageToWidget; QTransform documentToWidget; QTransform flakeToWidget; QTransform viewportToWidget; converter.setZoom(1.); imageToWidget = converter.imageToWidgetTransform(); documentToWidget = converter.documentToWidgetTransform(); flakeToWidget = converter.flakeToWidgetTransform(); viewportToWidget = converter.viewportToWidgetTransform(); CHECK_TRANSFORM(imageToWidget, testRect, QRectF(80,70,100,100)); CHECK_TRANSFORM(documentToWidget, testRect, QRectF(9980,9970,10000,10000)); CHECK_TRANSFORM(flakeToWidget, testRect, QRectF(80,70,100,100)); CHECK_TRANSFORM(viewportToWidget, testRect, QRectF(100,100,100,100)); converter.setZoom(0.5); imageToWidget = converter.imageToWidgetTransform(); documentToWidget = converter.documentToWidgetTransform(); flakeToWidget = converter.flakeToWidgetTransform(); viewportToWidget = converter.viewportToWidgetTransform(); CHECK_TRANSFORM(imageToWidget, testRect, QRectF(30,20,50,50)); CHECK_TRANSFORM(documentToWidget, testRect, QRectF(4980,4970,5000,5000)); CHECK_TRANSFORM(flakeToWidget, testRect, QRectF(80,70,100,100)); CHECK_TRANSFORM(viewportToWidget, testRect, QRectF(100,100,100,100)); }
void KisQPainterCanvas::paintEvent(QPaintEvent * ev) { KisImageWSP image = canvas()->image(); if (image == 0) return; setAutoFillBackground(false); if (m_buffer.size() != size()) { m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied); } QPainter gc(&m_buffer); // we double buffer, so we paint on an image first, then from the image onto the canvas, // so copy the clip region since otherwise we're filling the whole buffer every time with // the background color _and_ the transparent squares. gc.setClipRegion(ev->region()); KisCoordinatesConverter *converter = coordinatesConverter(); QTransform imageTransform = converter->viewportToWidgetTransform(); gc.save(); gc.setCompositionMode(QPainter::CompositionMode_Source); gc.fillRect(QRect(QPoint(0, 0), size()), borderColor()); QTransform checkersTransform; QPointF brushOrigin; QPolygonF polygon; converter->getQPainterCheckersInfo(&checkersTransform, &brushOrigin, &polygon); gc.setPen(Qt::NoPen); gc.setBrush(m_d->checkBrush); gc.setBrushOrigin(brushOrigin); gc.setTransform(checkersTransform); gc.drawPolygon(polygon); gc.setTransform(imageTransform); gc.setRenderHint(QPainter::SmoothPixmapTransform, true); QRectF viewportRect = converter->widgetToViewport(ev->rect()); gc.setCompositionMode(QPainter::CompositionMode_SourceOver); gc.drawImage(viewportRect, m_d->prescaledProjection->prescaledQImage(), viewportRect); gc.restore(); #ifdef DEBUG_REPAINT QColor color = QColor(random() % 255, random() % 255, random() % 255, 150); gc.fillRect(ev->rect(), color); #endif drawDecorations(gc, ev->rect()); gc.end(); QPainter painter(this); painter.drawImage(ev->rect(), m_buffer, ev->rect()); }
void KisOpenGLCanvas2::drawImage() { if (!d->displayShader) { return; } glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); KisCoordinatesConverter *converter = coordinatesConverter(); d->displayShader->bind(); QMatrix4x4 projectionMatrix; projectionMatrix.setToIdentity(); projectionMatrix.ortho(0, width(), height(), 0, NEAR_VAL, FAR_VAL); // Set view/projection matrices QMatrix4x4 modelMatrix(coordinatesConverter()->imageToWidgetTransform()); modelMatrix.optimize(); modelMatrix = projectionMatrix * modelMatrix; d->displayShader->setUniformValue(d->displayShader->location(Uniform::ModelViewProjection), modelMatrix); QMatrix4x4 textureMatrix; textureMatrix.setToIdentity(); d->displayShader->setUniformValue(d->displayShader->location(Uniform::TextureMatrix), textureMatrix); QRectF widgetRect(0,0, width(), height()); QRectF widgetRectInImagePixels = converter->documentToImage(converter->widgetToDocument(widgetRect)); qreal scaleX, scaleY; converter->imageScale(&scaleX, &scaleY); d->displayShader->setUniformValue(d->displayShader->location(Uniform::ViewportScale), (GLfloat) scaleX); d->displayShader->setUniformValue(d->displayShader->location(Uniform::TexelSize), (GLfloat) d->openGLImageTextures->texelSize()); QRect ir = d->openGLImageTextures->storedImageBounds(); QRect wr = widgetRectInImagePixels.toAlignedRect(); if (!d->wrapAroundMode) { // if we don't want to paint wrapping images, just limit the // processing area, and the code will handle all the rest wr &= ir; } int firstColumn = d->xToColWithWrapCompensation(wr.left(), ir); int lastColumn = d->xToColWithWrapCompensation(wr.right(), ir); int firstRow = d->yToRowWithWrapCompensation(wr.top(), ir); int lastRow = d->yToRowWithWrapCompensation(wr.bottom(), ir); int minColumn = d->openGLImageTextures->xToCol(ir.left()); int maxColumn = d->openGLImageTextures->xToCol(ir.right()); int minRow = d->openGLImageTextures->yToRow(ir.top()); int maxRow = d->openGLImageTextures->yToRow(ir.bottom()); int imageColumns = maxColumn - minColumn + 1; int imageRows = maxRow - minRow + 1; for (int col = firstColumn; col <= lastColumn; col++) { for (int row = firstRow; row <= lastRow; row++) { int effectiveCol = col; int effectiveRow = row; QPointF tileWrappingTranslation; if (effectiveCol > maxColumn || effectiveCol < minColumn) { int translationStep = floor(qreal(col) / imageColumns); int originCol = translationStep * imageColumns; effectiveCol = col - originCol; tileWrappingTranslation.rx() = translationStep * ir.width(); } if (effectiveRow > maxRow || effectiveRow < minRow) { int translationStep = floor(qreal(row) / imageRows); int originRow = translationStep * imageRows; effectiveRow = row - originRow; tileWrappingTranslation.ry() = translationStep * ir.height(); } KisTextureTile *tile = d->openGLImageTextures->getTextureTileCR(effectiveCol, effectiveRow); if (!tile) { warnUI << "OpenGL: Trying to paint texture tile but it has not been created yet."; continue; } /* * We create a float rect here to workaround Qt's * "history reasons" in calculation of right() * and bottom() coordinates of integer rects. */ QRectF textureRect(tile->tileRectInTexturePixels()); QRectF modelRect(tile->tileRectInImagePixels().translated(tileWrappingTranslation.x(), tileWrappingTranslation.y())); //Setup the geometry for rendering if (KisOpenGL::hasOpenGL3()) { rectToVertices(d->vertices, modelRect); d->quadBuffers[0].bind(); d->quadBuffers[0].write(0, d->vertices, 3 * 6 * sizeof(float)); rectToTexCoords(d->texCoords, textureRect); d->quadBuffers[1].bind(); d->quadBuffers[1].write(0, d->texCoords, 2 * 6 * sizeof(float)); } else { rectToVertices(d->vertices, modelRect); d->displayShader->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); d->displayShader->setAttributeArray(PROGRAM_VERTEX_ATTRIBUTE, d->vertices); rectToTexCoords(d->texCoords, textureRect); d->displayShader->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE); d->displayShader->setAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE, d->texCoords); } if (d->displayFilter) { glActiveTexture(GL_TEXTURE0 + 1); glBindTexture(GL_TEXTURE_3D, d->displayFilter->lutTexture()); d->displayShader->setUniformValue(d->displayShader->location(Uniform::Texture1), 1); } int currentLodPlane = tile->currentLodPlane(); if (d->displayShader->location(Uniform::FixedLodLevel) >= 0) { d->displayShader->setUniformValue(d->displayShader->location(Uniform::FixedLodLevel), (GLfloat) currentLodPlane); } glActiveTexture(GL_TEXTURE0); tile->bindToActiveTexture(); if (currentLodPlane > 0) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); } else if (SCALE_MORE_OR_EQUAL_TO(scaleX, scaleY, 2.0)) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); switch(d->filterMode) { case KisOpenGL::NearestFilterMode: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); break; case KisOpenGL::BilinearFilterMode: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); break; case KisOpenGL::TrilinearFilterMode: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); break; case KisOpenGL::HighQualityFiltering: if (SCALE_LESS_THAN(scaleX, scaleY, 0.5)) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } break; } } glDrawArrays(GL_TRIANGLES, 0, 6); } } glBindTexture(GL_TEXTURE_2D, 0); d->displayShader->release(); glBindBuffer(GL_ARRAY_BUFFER, 0); }
void KisOpenGLCanvas2::drawCheckers() { if (!d->checkerShader) { return; } KisCoordinatesConverter *converter = coordinatesConverter(); QTransform textureTransform; QTransform modelTransform; QRectF textureRect; QRectF modelRect; QRectF viewportRect = !d->wrapAroundMode ? converter->imageRectInViewportPixels() : converter->widgetToViewport(this->rect()); converter->getOpenGLCheckersInfo(viewportRect, &textureTransform, &modelTransform, &textureRect, &modelRect, d->scrollCheckers); textureTransform *= QTransform::fromScale(d->checkSizeScale / KisOpenGLImageTextures::BACKGROUND_TEXTURE_SIZE, d->checkSizeScale / KisOpenGLImageTextures::BACKGROUND_TEXTURE_SIZE); if (!d->checkerShader->bind()) { qWarning() << "Could not bind checker shader"; return; } QMatrix4x4 projectionMatrix; projectionMatrix.setToIdentity(); projectionMatrix.ortho(0, width(), height(), 0, NEAR_VAL, FAR_VAL); // Set view/projection matrices QMatrix4x4 modelMatrix(modelTransform); modelMatrix.optimize(); modelMatrix = projectionMatrix * modelMatrix; d->checkerShader->setUniformValue(d->checkerShader->location(Uniform::ModelViewProjection), modelMatrix); QMatrix4x4 textureMatrix(textureTransform); d->checkerShader->setUniformValue(d->checkerShader->location(Uniform::TextureMatrix), textureMatrix); //Setup the geometry for rendering if (KisOpenGL::hasOpenGL3()) { rectToVertices(d->vertices, modelRect); d->quadBuffers[0].bind(); d->quadBuffers[0].write(0, d->vertices, 3 * 6 * sizeof(float)); rectToTexCoords(d->texCoords, textureRect); d->quadBuffers[1].bind(); d->quadBuffers[1].write(0, d->texCoords, 2 * 6 * sizeof(float)); } else { rectToVertices(d->vertices, modelRect); d->checkerShader->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); d->checkerShader->setAttributeArray(PROGRAM_VERTEX_ATTRIBUTE, d->vertices); rectToTexCoords(d->texCoords, textureRect); d->checkerShader->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE); d->checkerShader->setAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE, d->texCoords); } // render checkers glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, d->openGLImageTextures->checkerTexture()); glDrawArrays(GL_TRIANGLES, 0, 6); glBindTexture(GL_TEXTURE_2D, 0); d->checkerShader->release(); glBindBuffer(GL_ARRAY_BUFFER, 0); }
void KisOpenGLCanvas2::paintToolOutline(const QPainterPath &path) { d->cursorShader->bind(); // setup the mvp transformation KisCoordinatesConverter *converter = coordinatesConverter(); QMatrix4x4 projectionMatrix; projectionMatrix.setToIdentity(); projectionMatrix.ortho(0, width(), height(), 0, NEAR_VAL, FAR_VAL); // Set view/projection matrices QMatrix4x4 modelMatrix(converter->flakeToWidgetTransform()); modelMatrix.optimize(); modelMatrix = projectionMatrix * modelMatrix; d->cursorShader->setUniformValue(d->cursorShader->location(Uniform::ModelViewProjection), modelMatrix); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // XXX: glLogicOp not in ES 2.0 -- it would be better to use another method. // It is defined in 3.1 core profile onward. // Actually, https://www.opengl.org/sdk/docs/man/html/glLogicOp.xhtml says it's in 2.0 onwards, // only not in ES, but we don't care about ES, so we could use the function directly. glEnable(GL_COLOR_LOGIC_OP); if (ptr_glLogicOp) { ptr_glLogicOp(GL_XOR); } // Paint the tool outline if (KisOpenGL::hasOpenGL3()) { d->outlineVAO.bind(); d->lineBuffer.bind(); } // Convert every disjointed subpath to a polygon and draw that polygon QList<QPolygonF> subPathPolygons = path.toSubpathPolygons(); for (int i = 0; i < subPathPolygons.size(); i++) { const QPolygonF& polygon = subPathPolygons.at(i); QVector<QVector3D> vertices; vertices.resize(polygon.count()); for (int j = 0; j < polygon.count(); j++) { QPointF p = polygon.at(j); vertices[j].setX(p.x()); vertices[j].setY(p.y()); } if (KisOpenGL::hasOpenGL3()) { d->lineBuffer.allocate(vertices.constData(), 3 * vertices.size() * sizeof(float)); } else { d->cursorShader->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); d->cursorShader->setAttributeArray(PROGRAM_VERTEX_ATTRIBUTE, vertices.constData()); } glDrawArrays(GL_LINE_STRIP, 0, vertices.size()); } if (KisOpenGL::hasOpenGL3()) { d->lineBuffer.release(); d->outlineVAO.release(); } glDisable(GL_COLOR_LOGIC_OP); d->cursorShader->release(); }