[Interest] QImage::Format_Grayscale8 work working

Nikolai Tasev nikolai at nand.bg
Fri Mar 16 09:12:09 CET 2018


Format_Indexed8 and Format_Grayscale8 both have 8bit per pixel. The 
difference is that to convert from the 8bit pixel to RGB values 
(QImage::pixel() method)
for Indexed8u you need to set the Palette for conversion and for 
Grayscale8 you don't (it just assumes that R=G=B=gray value)

Are you trying to extract a channel from a multichannel image and put it 
into a grayscale image? The code seems unnecessary complex
and inefficient for such a task. You just need to get the data pointer 
and skip the uneeded channels and keep watch for the end of the row.


On 3/15/2018 10:21 PM, Jason H wrote:
> Given the following functions, I should be able to create a non-all black image (assuming input is good)? The only success I have is using Indexed8
>
> QHash<QRgb, qint32 (*)(QRgb)> colorFuncs {
> 	{ Qt::red,   qRed},
> 	{ Qt::green, qGreen},
> 	{ Qt::blue,  qBlue},
> 	{ Qt::gray,  qGray},
> };
>
> QImage color8(const QImage &image, int channel) { // Channel is one of Qt::red Qt::green Qt::blue or Qt::gray
> 	QImage out(image.width(), image.height(), QImage::Format_Indexed8);  // Change to Format_Grayscale8, and get nothing
> // for indexed8
> 	QVector<QRgb> values;
> 	values.reserve(256);
> 	if (channel==Qt::gray) { for (int c=0; c<256; c++) values.append(qRgb(c,c,c)); }
> 	if (channel==Qt::red)  { for (int c=0; c<256; c++) values.append(qRgb(c,0,0)); }
> 	if (channel==Qt::green){ for (int c=0; c<256; c++) values.append(qRgb(0,c,0)); }
> 	if (channel==Qt::blue) { for (int c=0; c<256; c++) values.append(qRgb(0,0,c)); }
> 	out.setColorTable(values);
> // end for indexed8
>
> 	int (*colorFunc)(QRgb rgb) = colorFuncs[channel];
> 	for (int y=0; y < image.height(); y++) {
> 		for (int x=0; x < image.width(); x++) {
> 			out.setPixel(x,y, colorFunc(image.pixel(x,y)));
> 		}
> 	}
>
> 	return out;
> }
>
> I'm not familar with Qt and Grayscale8... Anyone know what is going wrong?
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>




More information about the Interest mailing list