Hi. First, I post the code that finally I have implemented using the code by Cesar. After I comment a problem with this code:
void FotoEditorFotos
::paintRect(const QColor &color,
const QRect &rectAPintar
) {
int xMin = rectAPintar.x();
int xMax = xMin + rectAPintar.width() - 1;
int yMin = rectAPintar.y();
int yMax = yMin + rectAPintar.height() - 1;
if (!imatge.valid(xMin, yMin) || !imatge.valid(xMax, yMax)) return;
QRgb colorRgb = color.rgba();
quint8 *pixelsLiniaFoto;
quint8 color8, colorVermell, colorVerd, colorBlau, colorAlpha;
int bytesPixel = (imatge.depth()) >> 3;
if (bytesPixel == 1)
color8 = (imatge.isGrayScale()) ? qGray(colorRgb) : obtenirIndexColor(valorColor);
else
{
colorVermell = qRed(colorRgb);
colorVerd = qGreen(colorRgb);
colorBlau = qBlue(colorRgb);
colorAlpha = qAlpha(colorRgb);
}
for (int j = yMin; j <= yMax; j++)
{
pixelsLiniaFoto = static_cast<quint8 *>(imatge.scanLine(j)) + (xMin * bytesPixel);
for (int i = xMin; i <= xMax; i++)
{
switch (bytesPixel)
{
// Qt only manages 1, 8 or 32bpp
case 1:
*(pixelsLiniaFoto++) = color8;
break;
case 4:
*(pixelsLiniaFoto++) = colorVermell;
*(pixelsLiniaFoto++) = colorVerd;
*(pixelsLiniaFoto++) = colorBlau;
*(pixelsLiniaFoto++) = colorAlpha;
break;
}
}
}
update();
}
void FotoEditorFotos::paintRect(const QColor &color, const QRect &rectAPintar)
{
int xMin = rectAPintar.x();
int xMax = xMin + rectAPintar.width() - 1;
int yMin = rectAPintar.y();
int yMax = yMin + rectAPintar.height() - 1;
if (!imatge.valid(xMin, yMin) || !imatge.valid(xMax, yMax)) return;
QRgb colorRgb = color.rgba();
quint8 *pixelsLiniaFoto;
quint8 color8, colorVermell, colorVerd, colorBlau, colorAlpha;
int bytesPixel = (imatge.depth()) >> 3;
if (bytesPixel == 1)
color8 = (imatge.isGrayScale()) ? qGray(colorRgb) : obtenirIndexColor(valorColor);
else
{
colorVermell = qRed(colorRgb);
colorVerd = qGreen(colorRgb);
colorBlau = qBlue(colorRgb);
colorAlpha = qAlpha(colorRgb);
}
for (int j = yMin; j <= yMax; j++)
{
pixelsLiniaFoto = static_cast<quint8 *>(imatge.scanLine(j)) + (xMin * bytesPixel);
for (int i = xMin; i <= xMax; i++)
{
switch (bytesPixel)
{
// Qt only manages 1, 8 or 32bpp
case 1:
*(pixelsLiniaFoto++) = color8;
break;
case 4:
*(pixelsLiniaFoto++) = colorVermell;
*(pixelsLiniaFoto++) = colorVerd;
*(pixelsLiniaFoto++) = colorBlau;
*(pixelsLiniaFoto++) = colorAlpha;
break;
}
}
}
update();
}
To copy to clipboard, switch view to plain text mode
The problem is that with images with 32bpp it paints the area that I want but with an invalid color, different from the color that I pass to the function. Anoybody could explain where's the mistake?
Bookmarks