Jump to content

Convert 256 colors to 16 colors - BMP Image

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
spyrytus

spyrytus

    Newbie

  • Members
  • Pip
  • 4 posts
Hello.
I write class, which converts 256 colors image to 16 colors image. But I don't know, how I can do it. I know this formule: Delta=(R1-R2)^2 + (G1-G2)^2+ (B1-B2)^2, but I don't understand how use it ?
So, please explain me how convert image ? :crying:
I write following code:

#define DELTA(RGB1, RGB2) ( \

                sqrt(pow(RGB1.rgbRed-RGB2.rgbRed, 2) + \

                pow(RGB1.rgbGreen-RGB2.rgbGreen, 2) + \

                pow(RGB1.rgbBlue-RGB2.rgbBlue, 2)))

...

for ( int i = 0; i < 255; i++ )

{

  RGBQUAD rgb1 = ColorTable256[i];

  RGBQUAD rgb2 = ColorTable256[i + 1];

  int index = DELTA( rgb1, rgb2 );

  rgb1.rgbRed = ( ( rgb1.rgbRed + rgb2.rgbRed ) / 2 );

  rgb1.rgbGreen = ( ( rgb1.rgbGreen + rgb2.rgbGreen ) / 2 );

  rgb1.rgbBlue = ( ( rgb1.rgbBlue + rgb2.rgbBlue ) / 2 ); 

  if ( index < 16 ) ColorTable16[index] = RGB( rgb1.rgbRed, rgb1.rgbGreen, rgb1.rgbBlue );

}

But this code don't too well works ...
If anyone knows how does it - please tell me.
It's very important for me ...
TIA.

#2
brightmatter

brightmatter

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
Try using a modulus. I could be wrong but take your 256 % 16.

#3
spyrytus

spyrytus

    Newbie

  • Members
  • Pip
  • 4 posts

brightmatter said:

Try using a modulus. I could be wrong but take your 256 % 16.
No, it didn't helps, here need to do a bit another... but I don't know what ... :cursing:
I tried all, but in any case picture becomes "breakup picture" ... :crying:

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think the purpose of Delta is to determine which color it is "closest" to. Do you have a description of the algorithm you are supposed to implement?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
spyrytus

spyrytus

    Newbie

  • Members
  • Pip
  • 4 posts

WingedPanther said:

Do you have a description of the algorithm you are supposed to implement?
No, I don't have a description of the algorithm that's why I ask help from your ...
All that I know about it, this is that need to find the closest colors 256 colors table for 16 colors table. In other words, find the closest maximum look like color for 16 colors table.

It's all: en[dot]wikipedia[dot]org[slash]wiki[slash]Color_quantization

TIA.

#6
sanfor

sanfor

    Newbie

  • Members
  • PipPip
  • 11 posts
i didn't know that
thanks

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
This may be useful: An Overview of Color Quantization Techniques
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
speculatius

speculatius

    Newbie

  • Members
  • PipPip
  • 25 posts
Hello,

here you can find RGB-values for all 16 colors. The easiest algorithm that I can imagine is:

1) prepare table with 16 shown RGB values
2) for every color in your 256-colored image, try to compare it to every color from table (use your delta-function)
3) select the closest one and write to color-mapping table
4) convert image

I know, it looks little bit slow. But RGB values for this 16 colors are not absolutely systematic, so I think that you dont have other choice. Let me know if you found better solution.

EDIT: Oh sorry, I see that you already have tables with colors. So step 1 is irrelevant :)

#9
spyrytus

spyrytus

    Newbie

  • Members
  • Pip
  • 4 posts

speculatius said:

EDIT: Oh sorry, I see that you already have tables with colors. So step 1 is irrelevant :)
Hello.
Yes, I have tables with colors, but it doesn't nothing gives me ...
Because photo becomes too horrible, and I want that my photo would be similar of "The Paint".
I know that "Paint" changes pallete - how he does it ?
It's very important for me, I don't know that do ... I was searching everywhere in the Internet and nothing found ...
So, below code, which is finding the closest color in pallete:
// find the closest color from table
int GetIndex( RGBQUAD &Color, LPDWORD ColorTable )
{
  // index of color
  int index = -1;
  // distance color (need for algorithm Euclid)
  int dis = 0x7FFFFFFF;
  // finding color
  for ( int i = 0; i < 16; i++ )
  {
    // get color from pallete
    RGBQUAD rgb = getColors( ColorTable[i] );
    // get MAX likeness
    int delta = DELTA( Color, rgb );
    // check ...
    if ( delta < dis )
    {
      // change data
      dis = delta;
      // set new color index
      index = i;
      // change color
      memcpy( &Color, &rgb, sizeof( RGBQUAD ) );
    }
  }
  // return color index
  return index;
}
Maybe someone knows - how to overcome this problem ... :w00t:
I need only algorithm for changes table of colors from 256 to 16 colors ... :crying:

EDIT:

speculatius said:

1) prepare table with 16 shown RGB values
How it doing ? Because it's very big problem for me ... :crying:
TIA.

#10
speculatius

speculatius

    Newbie

  • Members
  • PipPip
  • 25 posts
I tried it in my Paint and it gave me horrible image too (in exactly default 16 colors). But may be you are looking for dithering.

Simply take two colors and mix them. When you alternate them from pixel to pixel (like chessboard), you can optically create new color. In this way you can create new 16*15=240 colors. It should be enough, but if not, you can use more patterns to create much more colors.