Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling pixel art with Qt Quick

I have a Qt Quick game that uses pixel art. For example:

import QtQuick 2.2
import QtQuick.Controls 1.1

ApplicationWindow {
    id: window
    visible: true
    width: 300
    height: 300
    title: qsTr("PixelArt")

    Image {
        source: "http://upload.wikimedia.org/wikipedia/commons/f/f0/Pixelart-tv-iso.png"
        anchors.centerIn: parent
    }
}

original size

I want to scale the art, so I increase the size:

import QtQuick 2.2
import QtQuick.Controls 1.1

ApplicationWindow {
    id: window
    visible: true
    width: 300
    height: 300
    title: qsTr("PixelArt")

    Image {
        source: "http://upload.wikimedia.org/wikipedia/commons/f/f0/Pixelart-tv-iso.png"
        anchors.centerIn: parent
        width: 256
        height: 256
    }
}

bad scaled

The image becomes blurry. How can I scale the image while preserving its "sharpness", so that it looks like this:

good scaled

like image 977
Mitch Avatar asked Dec 14 '22 23:12

Mitch


1 Answers

The image is blurry when scaled because the smooth property is true by default.

Primarily used in image based items to decide if the item should use smooth sampling or not. Smooth sampling is performed using linear interpolation, while non-smooth is performed using nearest neighbor.

Set it to false to stop this from happening:

import QtQuick 2.2
import QtQuick.Controls 1.1

ApplicationWindow {
    id: window
    visible: true
    width: 300
    height: 300
    title: qsTr("PixelArt")

    Image {
        source: "http://upload.wikimedia.org/wikipedia/commons/f/f0/Pixelart-tv-iso.png"
        anchors.centerIn: parent
        width: 256
        height: 256
        smooth: false
    }
}

For more information on scaling, see:

http://en.wikipedia.org/wiki/Image_scaling

like image 165
Mitch Avatar answered Dec 27 '22 03:12

Mitch