PDA

View Full Version : QImage pixelIndex resize color Table prolem



johny333
6th March 2018, 22:33
Hi everybody.

Sorry for my language. Please, I have problem, when load image and convert to index format:

*image = image->convertToFormat( QImage::Format::Format_Indexed8 );

then reduce color table:

image->setColorCount( 10 );

and storage all pixel index to array data:

for( int posIndex = 0 ; posIndex < 320 * 200 ; posIndex++ )
{
int index= image->pixelIndex( posIndex % 320 ,posIndex / 320 );
image->setPixel( posIndex % 320 ,posIndex / 320 , index /*% colTable.size()*/ );
data[ posIndex ] = ( char )index;
}
Maximum number get from function pixelIndex have be 10, because in color table is 10 items, but I have in array data item above that number 10.

When check count of items in color Table:

QVector<QRgb> colTable = image->colorTable();
cout << "size col table: " << colTable.count() << endl;
get

When image put to display:

item->setPixmap( QPixmap::fromImage( *image ));

image have correct number of collors. Who is problem? Why function pixelIndex get number above that 10? Is this error of QT?

Thanks for answer.

^NyAw^
7th March 2018, 10:03
Hi,

I don't understand what you want to do but I think that you are a little bit confused.

Color Table is a table that contains QRgb values so a Color Table like:

ColorTable[0] -> (0,0,0) Black
ColorTable[1] -> (255,255,255) White
ColorTable[2] -> (255,0,0) Red
...

means that if the pixel value of the image is 1, the output that you will see on the screen is White pixel. If the value of the pixel of the image is 2, the output on the screen will be a Red pixel.

Do you understand how it works? It's a index table. The image values are INDEXES to the Color Table

johny333
7th March 2018, 17:10
Thanks, but I understand how work image with color palette. My problem is other. When I reduce color palette from default 255 items for example to 10:

image->setColorCount( 10 );

it will be possible adressing only 10 items in in colorTable:

index = mage->pixelIndex( x_pos , y_pos ); // index must bee in range 0 to 9

My problem is, that index is in range 0 to 255, that is wrong. Why index is not in range 0 to 9? Is this bug of QT?

^NyAw^
8th March 2018, 09:45
Thanks, but I understand how work image with color palette


Sure?



My problem is, that index is in range 0 to 255, that is wrong. Why index is not in range 0 to 9? Is this bug of QT?


You are assuming that changing the color table of an image will change the pixel values of the image(that are indexes in this case) and it is not true. Changing the color table only changes the color table not the pixel values.

If you want to change the pixel values(color indexes) you have to change the image pixel values. Do you know what is a LUT(Look Up Table)? An image with a color palette use the color table as LUT for display.

johny333
8th March 2018, 11:09
You are assuming that changing the color table of an image will change the pixel values of the image(that are indexes in this case) and it is not true.

Yes I assumed, that image index change automatic, when change color table. I understand, that color table and pixel index in image is not the same. I assumed, that QT this operation make in background calling inner function.


An image with a color palette use the color table as LUT for display.

Just I find how use color table to next conversion:

imageLoad->setColorCount( 10 );
QVector<QRgb> colTable;
colTable = imageLoad->colorTable();
*imageLoad = imageLoad->convertToFormat( QImage::Format::Format_Indexed8 , colTable );

, but this have no effect to change index of image. How is possible that? Which functions should I call? Or make I something wrong?

^NyAw^
8th March 2018, 11:32
Hi,

How would you expect that it will change automatically?

A RGB Pixel value (230,100,100) wich color table value must have?

Could you explain us what you are trying to do?

johny333
8th March 2018, 14:26
I trying convert image from format, that use full 256 colors to format, that use count color by me. I convert image by my own algorithm. When Image use full 256 index, compress data is for example 1000B. When Image use for example 10 index of colors, compress data is for example 200B. I will have possibility change size of compress data. This is possible when resize count index, that image use.

^NyAw^
8th March 2018, 16:47
Hi,

Let's imagine that your image is a 8 bit image. You can create a color table like this:



for (int i=0; i<255; i++)
colortable[i] = QRgb(i,i,i);


Displaying this image you will see a gray scale image.

Using this color table


for (int i=0; i<255; i++)
colortable[i] = QRgb(i,0,0);


You will see a "red scale" image.

If you want a color table with only 10 indexes you have to create it as nobody knows wich output colors you expect to get. Finally, from Qt Docs:

"When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined"

As your image is still having pixel values higher than 10, the output on the screen is undefined.

johny333
8th March 2018, 22:08
Hi,

Ok, so is not possible reduce color table and indexes in image. This is answer for my question.

Thanks.

Saurian
9th March 2018, 08:37
Hi,

Not necessarily. You could map all the colours from an image in these 10 colours. For example, you can set colours in the form (i, i, i) which have values between 0 and 100 to be mapped to the colour defined as (50, 50, 50). Something like this:




for(int index = 0; index < 255) {
if(index < 100) {
colortable[i] = QRgb(50, 50, 50);
}
if(index >= 100 && index < 255) {
colortable[i] = QRgb(125, 125, 125);
}
}



So what you need to do is to decide the RGB values for these 10 colours and map all the shades of these colours to their equivalent in your 10 colours palette. Of course, this will have to be done manually (I mean you decide which shade is equivalent to which colour).

Best regards,
Sorin