<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Weird QImage pixel manipulation issue.]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I've encountered a weird bug while inverting an image, editing it afterwards, converting it to a QPixmap and finally drawing it to screen.</p>
<p dir="auto">This is the code for manipulating the image:</p>
<pre><code>QPixmap modify(const QImage &amp;source) {
     QImage inv = source.copy();
     inv.invertPixels();
     
     uint *ptr = (uint *)inv.bits();
     uint *end = inv.bits() + (inv.byteCount() / 4);
     while (ptr &lt; end) {
          *ptr++ += 0x00333333;
     }

     return QPixmap::fromImage(inv);
}
</code></pre>
<p dir="auto">and this one draws the QPixmap to screen</p>
<pre><code>void XYZ::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    painter.drawPixmap(m_ImagePos, m_MyPixmap);
}
</code></pre>
<p dir="auto">results in</p>
<p dir="auto"><img src="http://imgur.com/mv467NX.png" alt="alt text" class=" img-fluid img-markdown" /></p>
<p dir="auto">The background is completely messed up, it seems to render random pixels there and whenever I hover it with the mouse, it seems to execute the "leaveEvent" of the underlying button. If I click into it, it focuses the QtCreator instance displayed below my application.</p>
<p dir="auto">Has anyone an idea what the issue could be?<br />
Thanks in advance.</p>
<p dir="auto">PS: It results in the same issue if I manipulate the image using scanlines and qRgba(...) for manipulating the pixel.</p>
]]></description><link>https://forum.qt.io/topic/73423/weird-qimage-pixel-manipulation-issue</link><generator>RSS for Node</generator><lastBuildDate>Fri, 15 May 2026 22:55:06 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/73423.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Nov 2016 05:04:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Mon, 06 Mar 2017 18:40:12 GMT]]></title><description><![CDATA[<p dir="auto">Sorry to put this back on top, but for anyone who has the same problem as I had:</p>
<p dir="auto">The reason for the messed-up background was that after copying the image, Qt somehow converted it to a premultiplied-alpha format. Hence attempting to edit the pixels was disastrous. A call to 'convertToFormat(QImage::Format_ARGB32)' after 'copy()' should do it :).</p>
]]></description><link>https://forum.qt.io/post/380159</link><guid isPermaLink="true">https://forum.qt.io/post/380159</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Mon, 06 Mar 2017 18:40:12 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Fri, 18 Nov 2016 21:11:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a> said in <a href="/post/360211">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto">We shouldn't abuse this thread for these kinds of discussions anymore.</p>
</blockquote>
<p dir="auto">I tend to wander off, so that happens to me a lot ... sorry.</p>
]]></description><link>https://forum.qt.io/post/360401</link><guid isPermaLink="true">https://forum.qt.io/post/360401</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Fri, 18 Nov 2016 21:11:48 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 23:22:00 GMT]]></title><description><![CDATA[<p dir="auto">Bitshifting is just plain beautiful &lt;3</p>
<p dir="auto">Jokes aside, of course you are totally correct. I implemented the same algorithm twice because I thought that my initial issue was caused by that. Anyways, now I am just using pixel and setPixel to interact with the data, because I agree on the readability reason with you.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> said in <a href="/topic/73423/weird-qimage-pixel-manipulation-issue/14">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto">Faster is a relative term - faster compared to what? Knuth's whole point is that you can spend months making a piece of code to run 10% faster, but if that piece of code carries 10% of the total execution time, in reality you've optimized only to remove a meager 1% of execution time. Ultimately, it boils down to profiling, finding the bottlenecks and finally removing them.</p>
</blockquote>
<p dir="auto">This is also a good point, but I guess it only applies to companies which actually only have spare development time. This is a hobby project, therefore I have plenty of time to try different stuff and play around with the features of Qt. Not to mention that I am 18 years old only and need to gain experience working with the Qt framework.</p>
<p dir="auto">We shouldn't abuse this thread for these kinds of discussions anymore. You can always send me a private message, if you want. :)</p>
]]></description><link>https://forum.qt.io/post/360211</link><guid isPermaLink="true">https://forum.qt.io/post/360211</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 23:22:00 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 23:10:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a> said in <a href="/post/360207">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto">I tested it now, both algorithms work and the method with 'scanline' and 'bits' is faster, so why wouldn't I take the faster one?</p>
</blockquote>
<p dir="auto">Few reasons (whether they're good is up to you to decide):</p>
<ol>
<li>It depends on the internal representation of the data inside <code>QImage</code> which isn't guaranteed to be compatible between versions, while the Qt API is binary compatible between minor versions (that translates to years).</li>
<li>It's a (tragic) fact of life that code is read much more than it's written, so one'd be wise to opt for more readable (and type-safe) code as every opportunity presents itself.</li>
<li>Faster is a relative term - faster compared to what? Knuth's whole point is that you can spend months making a piece of code to run 10% faster, but if that piece of code carries 10% of the total execution time, in reality you've optimized only to remove a meager 1% of execution time. Ultimately, it boils down to profiling, finding the bottlenecks and finally removing them.</li>
</ol>
<p dir="auto">PS.<br />
Talking about micro optimizations I'd suggest changing:</p>
<pre><code>inv.byteCount() / 4
</code></pre>
<p dir="auto">to:</p>
<pre><code>inv.byteCount() &gt;&gt; 2
</code></pre>
<p dir="auto">it isn't that clear and pretty though, is it?</p>
]]></description><link>https://forum.qt.io/post/360210</link><guid isPermaLink="true">https://forum.qt.io/post/360210</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Thu, 17 Nov 2016 23:10:08 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 22:27:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> said in <a href="/topic/73423/weird-qimage-pixel-manipulation-issue/12">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto">As one Donald Knuth once notably remarked:<br />
<em>"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."</em></p>
<p dir="auto">Which I happen to agree with. So before you know that the call to <code>setPixel()</code> is a bottleneck I advise you just forget this (really) tiny inefficiency.</p>
</blockquote>
<p dir="auto">I agree, but as Niklaus Wirth once stated:<br />
<em>'Software is getting slower more rapidly than hardware is getting faster.'</em><br />
I tested it now, both algorithms work and the method with 'scanline' and 'bits' is faster, so why wouldn't I take the faster one?</p>
<p dir="auto">btt:<br />
I finally found the issue (I felt like this could be it for a while) for the weird rendering, but I cannot really explain why it happened, given the fact that 'c' is copied in invertImage():</p>
<pre><code>void setImage(const QPixmap &amp;img) {
    QImage c = img.toImage();
    // ... do some pixel fetching (but not manipulating) ...
    m_pm = QPixmap::fromImage(invertImage(c));
}
</code></pre>
<p dir="auto">now with</p>
<pre><code>void setImage(const QImage &amp;c) {
    // ... do some pixel fetching ...
    m_pm = QPixmap::fromImage(invertImage(c));
}
</code></pre>
<p dir="auto">it just works fine.</p>
]]></description><link>https://forum.qt.io/post/360207</link><guid isPermaLink="true">https://forum.qt.io/post/360207</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 22:27:14 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 21:36:49 GMT]]></title><description><![CDATA[<p dir="auto">As one Donald Knuth once notably remarked:<br />
<em>"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."</em></p>
<p dir="auto">Which I happen to agree with. So before you know that the call to <code>setPixel()</code> is a bottleneck I advise you just forget this (really) tiny inefficiency.</p>
]]></description><link>https://forum.qt.io/post/360198</link><guid isPermaLink="true">https://forum.qt.io/post/360198</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Thu, 17 Nov 2016 21:36:49 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 19:44:48 GMT]]></title><description><![CDATA[<p dir="auto">We are not necessarily talking about small images here. I believe setPixel is costly when big images are involved. But thanks for that solution, too. May come in handy for small images later! :)</p>
]]></description><link>https://forum.qt.io/post/360184</link><guid isPermaLink="true">https://forum.qt.io/post/360184</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 19:44:48 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 19:06:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a> said in <a href="/post/360177">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto">isn't setPixel costly?</p>
</blockquote>
<p dir="auto">Depends on your definition of costly. A function call, couple of <code>if</code>s and a <code>switch</code> I don't consider to be much on the costly side.</p>
]]></description><link>https://forum.qt.io/post/360183</link><guid isPermaLink="true">https://forum.qt.io/post/360183</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Thu, 17 Nov 2016 19:06:50 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 18:49:02 GMT]]></title><description><![CDATA[<p dir="auto">That actually is a great idea VRonin, as flat-styled images will never really contain more than 256 colors!</p>
<p dir="auto">Solved :).</p>
]]></description><link>https://forum.qt.io/post/360181</link><guid isPermaLink="true">https://forum.qt.io/post/360181</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 18:49:02 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 18:54:33 GMT]]></title><description><![CDATA[<p dir="auto">If that is the only case then it's much more efficient to use the colour table</p>
<pre><code>inv.convertToFormat(QImage::Format_Indexed8);
const int colCount=inv.colorCount();
for(int i=0;i&lt;colCount;++i){
if(inv.color(i)==qRgba(0,0,0,0xFF)){
inv.setColor(i,qRgba(0x33,0x33,0x33,0xFF));
break;
}
}
</code></pre>
]]></description><link>https://forum.qt.io/post/360180</link><guid isPermaLink="true">https://forum.qt.io/post/360180</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 17 Nov 2016 18:54:33 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 18:22:13 GMT]]></title><description><![CDATA[<p dir="auto">Hello kshegunov,</p>
<p dir="auto">isn't setPixel costly? To answer your question: Only pure black images are passed to this function. The loop continuously fetches the RGBA data (i.e. 0xFF000000)  and adds 0x00333333 to it -&gt; 0xFF333333, results in a slightly brighter picture.</p>
<p dir="auto">I will try your method nevertheless and set this topic as "solved" if I don't find any better one. Thanks!</p>
]]></description><link>https://forum.qt.io/post/360177</link><guid isPermaLink="true">https://forum.qt.io/post/360177</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 18:22:13 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 16:05:55 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a><br />
Just iterate the image through the API instead of the internal data. E.g.:</p>
<pre><code>QPixmap modify(const QImage &amp;source)
{
     qint32 width = source.width(), height = source.height();
     QImage result(width, height, source.format());
     for (qint32 y = 0, y &lt; height; y++)  {
          for (qint32 x = 0; x &lt; width; x++)  {
              QRgb pixel = source.pixel(x, y);

              Q_ASSERT(0x132 - qRed(pixel) &lt;= 0xFF &amp;&amp; 0x132 - qGreen(pixel) &lt;= 0xFF &amp;&amp; 0x132 - qBlue(pixel) &lt;= 0xFF);
              pixel = qRgb(0x132 - qRed(pixel), 0x132 - qGreen(pixel), 0x132 - qBlue(pixel));
              result.setPixel(x, y, pixel);
          }
     }

     return QPixmap::fromImage(result);
}
</code></pre>
<p dir="auto">And I don't understand what this is supposed to do exactly:</p>
<pre><code>*ptr++ += 0x00333333;
</code></pre>
<p dir="auto">why add <code>0x00333333</code>, what's the significance? You do understand that there will be shifting if the value overflows ...?</p>
]]></description><link>https://forum.qt.io/post/360155</link><guid isPermaLink="true">https://forum.qt.io/post/360155</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Thu, 17 Nov 2016 16:05:55 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 07:23:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> said in <a href="/post/360035">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a> Does your pixmap have a transparent background?</p>
</blockquote>
<p dir="auto">Indeed. It needs to have a transparent background for the algorithm to work dynamically and in every situation possible. Is that an issue?</p>
<p dir="auto">I believe that semi-transparency is the issue by now, but forcing the background to be white (QPainter::setBackground &amp; QPainter::setBackgroundMode) in the paintEvent didn't help either.</p>
<p dir="auto">Of course, I could blit the QPixmap on a plain white image and draw that in the end, but this is kinda hacky and also quite costly when done multiple times or with larger images.</p>
]]></description><link>https://forum.qt.io/post/360046</link><guid isPermaLink="true">https://forum.qt.io/post/360046</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 07:23:24 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 05:51:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a> Does your pixmap have a transparent background?</p>
]]></description><link>https://forum.qt.io/post/360035</link><guid isPermaLink="true">https://forum.qt.io/post/360035</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 17 Nov 2016 05:51:36 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 05:43:01 GMT]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">sorry, my actual code was different. The image is assured to be 32bpp, therefore I actually used</p>
<pre><code>uint *end = ptr + (inv.byteCount() / 4);
</code></pre>
<p dir="auto">but the issue remains. I tried to save my QPixmap to file before rendering it and it looks just fine, that means my manipulation can not be the issue here. :(</p>
]]></description><link>https://forum.qt.io/post/360033</link><guid isPermaLink="true">https://forum.qt.io/post/360033</guid><dc:creator><![CDATA[Nicolas Kogler]]></dc:creator><pubDate>Thu, 17 Nov 2016 05:43:01 GMT</pubDate></item><item><title><![CDATA[Reply to Weird QImage pixel manipulation issue. on Thu, 17 Nov 2016 05:22:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nicolas-kogler">@<bdi>Nicolas-Kogler</bdi></a> said in <a href="/post/360027">Weird QImage pixel manipulation issue.</a>:</p>
<blockquote>
<p dir="auto">uint *end = (uint *)(inv.bits() + inv.byteCount());</p>
</blockquote>
<p dir="auto">You're trying to modify pixel data, but you use byteCount() - I think this is wrong. An RGBA pixmap with 100 pixels will consume 400 bytes. You're most probably writing outside of your pixmap which is undefined behaviour.<br />
You can use size() to calculate number of pixels.</p>
]]></description><link>https://forum.qt.io/post/360029</link><guid isPermaLink="true">https://forum.qt.io/post/360029</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 17 Nov 2016 05:22:45 GMT</pubDate></item></channel></rss>