[Interest] QImage::Format_Grayscale8 work working

Nikolai Tasev nikolai at nand.bg
Fri Mar 16 15:25:48 CET 2018


Yes that confirms my suspicions that setPixel for Format_Grayscale8 
expect different things ( as I wrote in my previous email).
You need to pass qRgb(qRed(rgbVal), 0, 0) for red, not qRed(rgbVal).

On 3/16/2018 4:09 PM, Jason H wrote:
> 5.10.1
>
> Yes, that's a good idea. So I wrote some code:
>
> 	QImage gray8 = color8(image, Qt::gray, QImage::Format_Grayscale8); // modded my function to take the format.
> 	QImage index = color8(image, Qt::gray, QImage::Format_Indexed8);
>
> 	int differences = 0;
> 	int same = 0;
> 	for (int y=0; y < gray8.height(); y++) {
> 		for (int x=0; x < gray8.width(); x++) {
> 			qDebug() << "gray" << gray8.pixel(x,y) << "index" << index.pixel(x,y);
> 			if (gray8.pixel(x,y) != index.pixel(x,y) )
> 				differences++;
> 			else
> 				same++;
> 		}
> 	}
>
> 	qDebug() << "differences" <<  differences;
> 	qDebug() << "same" <<  same;
>
> With output:
> differences 8048393
> same 72951
>
> It seems every pixel from "gray8" is 4278190080 == 0xFF000000.
> So it seems that setPixel in my color8 function never sets the pixel.
>
> I'm thinking this is a bug, unless someone has an idea?
>
>
>> Sent: Friday, March 16, 2018 at 9:52 AM
>> From: "Nikolai Tasev" <nikolai at nand.bg>
>> To: "Jason H" <jhihn at gmx.com>
>> Cc: "Interestqt-project.org" <Interest at qt-project.org>
>> Subject: Re: [Interest] QImage::Format_Grayscale8 work working
>>
>> I am not sure if there any difference in how the Indexed8 and Grayscale8
>> are saved. It also depends on the
>> file format. As far as I remember many formats always save as 32bits.
>>
>> First look at the pixel data as raw unsigned char values to see if the
>> conversion is correct, maybe the problem comes
>> later when saving. Then you can also try with some small synthetic
>> images for specialized tests.
>>
>> Grayscale8 is a most recent development is there any chance that you use
>> a newer header and older library?
>>
>> On 3/16/2018 3:14 PM, Jason H wrote:
>>> It returns black. All black. Hence the issue :-)
>>> The original file is an 8MP 4:3 image as png it saves to ~10mb.
>>> - as a Grayscale8 it saves as 8 KB-kilo. (should be 8,121,344)
>>> - as Indexed8 it saves as 5.5mb
>>>
>>> I'd expect Greyscale8 to be about the same size on disk as Indexed8 +/- color table.
>>>
>>>> Sent: Friday, March 16, 2018 at 8:38 AM
>>>> From: "Nikolai Tasev" <nikolai at nand.bg>
>>>> To: "Jason H" <jhihn at gmx.com>
>>>> Cc: "Interestqt-project.org" <Interest at qt-project.org>
>>>> Subject: Re: [Interest] QImage::Format_Grayscale8 work working
>>>>
>>>> What is not working for Grayscale? Looking at the code it will do
>>>> different things when you are passing Qt::red, Qt::blue, Qt::green.
>>>> It will not return a color image but a grayscale image of the
>>>> corresponding channel.
>>>>
>>>> On 3/16/2018 2:08 PM, Jason H wrote:
>>>>>> Sent: Friday, March 16, 2018 at 9:12 AM
>>>>>> From: "Nikolai Tasev" <nikolai at nand.bg>
>>>>>> To: "Jason H" <jhihn at gmx.com>, "Interestqt-project.org" <Interest at qt-project.org>
>>>>>> Subject: Re: [Interest] QImage::Format_Grayscale8 work working
>>>>>>
>>>>>>
>>>>>> 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)
>>>>> I am aware of that.
>>>>>
>>>>>> 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.
>>>>> That assumes I be want to do the math for every Format. ARGB is easy, RGB888 is not, etc. Pixel() alerts need to not care. Yes, I could use scan lines, but then each format requires code. Which I haven't got time to write now.
>>>>>
>>>>> Anyway, I be still believe my code is correct, and it's still not working.
>>>>>
>>>>>> 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