Manipulate image and apply RGB mask: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[[Category:snippets]]
Small snippet showing how to take a QImage, run through its pixels and modify them based on certain conditions.
Small snippet showing how to take a QImage, run through its pixels and modify them based on certain conditions.


Here img is an input image, the application form has 4 checkboxes, which define the component of the color (<span class="caps">ARGB</span>) that is to be masked.
Here img is an input image, the application form has 4 checkboxes, which define the component of the color (ARGB) that is to be masked.
 
Here is an alternative version which should be quite a bit faster, but is less readable, perhaps:
 
===Categories:===


* [[:Category:snippets|snippets]]
<code><br /> // img is the input original image<br /> QRect rect = img-&gt;rect();<br /> QImage *result = new QImage(wd, ht, img-&gt;format());<br /> uint *output=0, *input = 0;<br /> for (int y = rect.top(); y &lt; rect.bottom(); y+'')<br /> {<br /> input = (uint*)img-&gt;scanLine(y - rect.top());<br /> output = (uint*)result-&gt;scanLine(y - rect.top());<br /> for (int x = rect.left(); x &lt; rect.right(); x)<br /> {<br /> int col = *input;<br /> if (col  Qt::transparent || col  Qt::color0)<br /> {<br /> *output''+ = *input+'';<br /> continue;<br /> }<br /> QColor qcol = QColor::fromRgba(col);<br /> if (ui-&gt;redBox-&gt;checkState()  0) qcol.setRed(0);
            if (ui-&amp;gt;greenBox-&amp;gt;checkState()  0) qcol.setGreen(0);<br /> if (ui-&gt;blueBox-&gt;checkState()  0) qcol.setBlue(0);
            if (ui-&amp;gt;alphaBox-&amp;gt;checkState()  0) qcol.setAlpha(0);<br /> *output''+ = qcol.rgba();<br /> input+'';<br /> }<br /> }<br /> // At this point result will have the modified QImage<br /></code>
<br />Here is an alternative version which should be quite a bit faster, but is less readable, perhaps:
<br /><code><br /> // img is the input original image<br /> QRect rect = img-&gt;rect();<br /> QImage *result = new QImage(wd, ht, img-&gt;format());<br /> // Mask is suitable for colors in ARGB byte order.<br /> uint mask = 0;<br /> if (ui-&gt;redBox-&gt;checkState() != 0)<br /> mask = mask | 0x00ff0000;<br /> if (ui-&gt;greenBox-&gt;checkState() != 0)<br /> mask = mask | 0x0000ff00;<br /> if (ui-&gt;blueBox-&gt;checkState() != 0)<br /> mask = mask | 0x000000ff;<br /> if (ui-&gt;alphaBox-&gt;checkState() != 0)<br /> mask = mask | 0xff000000;<br /> for (int y = rect.top(); y &lt; rect.bottom(); y)<br /> {<br /> uint* input = (uint*)img-&gt;scanLine(y - rect.top());<br /> uint* output = (uint*)result-&gt;scanLine(y - rect.top());<br /> for (int x = rect.left(); x &lt; rect.right(); x)<br /> {<br /> *output = *input &amp; mask;<br /> output;<br /> input''+;<br /> }<br /> }<br /> // At this point result will have the modified QImage

Revision as of 10:52, 24 February 2015


Small snippet showing how to take a QImage, run through its pixels and modify them based on certain conditions.

Here img is an input image, the application form has 4 checkboxes, which define the component of the color (ARGB) that is to be masked.

<br /> // img is the input original image<br /> QRect rect = img-&gt;rect();<br /> QImage *result = new QImage(wd, ht, img-&gt;format());<br /> uint *output=0, *input = 0;<br /> for (int y = rect.top(); y &lt; rect.bottom(); y+'')<br /> {<br /> input = (uint*)img-&gt;scanLine(y - rect.top());<br /> output = (uint*)result-&gt;scanLine(y - rect.top());<br /> for (int x = rect.left(); x &lt; rect.right(); x)<br /> {<br /> int col = *input;<br /> if (col  Qt::transparent || col  Qt::color0)<br /> {<br /> *output''+ = *input+'';<br /> continue;<br /> }<br /> QColor qcol = QColor::fromRgba(col);<br /> if (ui-&gt;redBox-&gt;checkState()  0) qcol.setRed(0);
            if (ui-&amp;gt;greenBox-&amp;gt;checkState()  0) qcol.setGreen(0);<br /> if (ui-&gt;blueBox-&gt;checkState()  0) qcol.setBlue(0);
            if (ui-&amp;gt;alphaBox-&amp;gt;checkState()  0) qcol.setAlpha(0);<br /> *output''+ = qcol.rgba();<br /> input+'';<br /> }<br /> }<br /> // At this point result will have the modified QImage<br />


Here is an alternative version which should be quite a bit faster, but is less readable, perhaps:

// img is the input original image
QRect rect = img->rect();
QImage *result = new QImage(wd, ht, img->format());
// Mask is suitable for colors in ARGB byte order.
uint mask = 0;
if (ui->redBox->checkState() != 0)
mask = mask | 0x00ff0000;
if (ui->greenBox->checkState() != 0)
mask = mask | 0x0000ff00;
if (ui->blueBox->checkState() != 0)
mask = mask | 0x000000ff;
if (ui->alphaBox->checkState() != 0)
mask = mask | 0xff000000;
for (int y = rect.top(); y < rect.bottom(); y)
{
uint* input = (uint*)img->scanLine(y - rect.top());
uint* output = (uint*)result->scanLine(y - rect.top());
for (int x = rect.left(); x < rect.right(); x)
{
*output = *input & mask;
output;
input+;
}
}
// At this point result will have the modified QImage