Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Manipulate image and apply RGB mask: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Sub-categorize)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:Snippets::Misc]]
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.
 
<code>
// img is the input original image
QRect rect = img->rect();
QImage *result = new QImage(wd, ht, img->format());
uint *output=0, *input = 0;
for (int y = rect.top(); y < rect.bottom(); y+'')
{
input = (uint*)img->scanLine(y - rect.top());
output = (uint*)result->scanLine(y - rect.top());
for (int x = rect.left(); x < rect.right(); x)
{
int col = *input;
if (col  Qt::transparent || col  Qt::color0)
{
*output''+ = *input+'';
continue;
}
QColor qcol = QColor::fromRgba(col);
if (ui->redBox->checkState()  0) qcol.setRed(0);
            if (ui->greenBox->checkState()  0) qcol.setGreen(0);
if (ui->blueBox->checkState()  0) qcol.setBlue(0);
            if (ui->alphaBox->checkState()  0) qcol.setAlpha(0);
*output''+ = qcol.rgba();
input+'';
}
}
// At this point result will have the modified QImage
</code>


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


===Categories:===
<code>
 
// img is the input original image
* [[:Category:snippets|snippets]]
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

Latest revision as of 11:58, 28 November 2016

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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.

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

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