Qpixmap scale to preserve the color mapping



  • Hello, I am wondering, if I can preserve the color mapping of given .png file, while scaling a QPixmap, or QImage. Let`s say we have 1200x800 png file, then scale it to 1/2 by both WxH. The color is changed of the different pixels in the scaled image. So, can I preserve it?


  • Moderators

    QPixmap doesn't provide such features, since it's optimized for screen display.
    QImage in contrast is made for raw image manipulation. Such as support for color tables.
    I can't give you an example right now, but you will have to go with QImage and make sure it has QImage::Format_Indexed8 in order to use QImage's colorTable methods. If i understood you correct.



  • Yes, kind of that. Lets say in a png with 1200x800 at pixel 100,100 its RGB 255,0,0, When I scale it to 800x600 can I preserve the pixel at let`s say 40,40, which was 100,100 to the same RGB?


  • Moderators

    i don't understand what could be your use case.
    Scaling (down) means that you need to merge several pixels with most probably different colors down to 1 pixel. So which color the "new" pixel gets is decided by the used algorithm.

    A simple algorithm for your desired downscaling would be to calculate the scaling factor first. Then map the pixel values with the factor to your scaled image.

    But i am pretty sure the result look beyond acceptable :)
    In some case not even recognizable from the original.



  • So, I have to use SVG to derive different sized QImage-s :(


  • Moderators

    you still haven't said for what you need this requirement.
    Why is Qt's offered scaling not acceptable for you?



  • Let me show you this....
    https://www.flickr.com/photos/heatblazer/15927159799/

    Consider that I am creating custom QGraphicsEllipseItems, when I found my custom mapped collor by iterating trough the image as shown. There are small red pixels in each circle. When I found them, I put invisible, clickable QGraphicsEllipseItem. Kind of mapping items by color. But when I scale, the so-called "Marker color" is lost.



  • So, just identify the positions of the markers before scaling, and then recalculate what the positions would be after scaling?


  • Moderators

    well such an approach is very error prone .. .as you see :)
    Why not checking programmatically? For example if you depend on analyzing the image data you can translate points (e.g. from mouse events) from your graphics item to the point in the image via the calculated scale factor.
    So that your logic still operates on the original sized image.



  • [quote author="Andre" date="1421844408"]So, just identify the positions of the markers before scaling, and then recalculate what the positions would be after scaling?[/quote]

    That is, actually, a not bad idea...



  • [quote author="raven-worx" date="1421844500"]well such an approach is very error prone .. .as you see :)
    Why not checking programmatically? For example if you depend on analyzing the image data you can translate points (e.g. from mouse events) from your graphics item to the point in the image via the calculated scale factor.
    So that your logic still operates on the original sized image.[/quote]

    Its more complicated than that. First, the items must be created before the mouse is clicked. Then they are connected with the game logic, they are connected with the graph logic too. The items are set before user knows it. It would be simple just to click and create item, but the code is already rolled that way and I dont have time to complete reverse engeneering it. So SVG scale? Any hints? I`d be grateful if you hint me abotu QSvgRenderer.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.