www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/13/04:59:27

Message-ID: <D1FB30BBA491D1118E6D006097BCAE390A4852@Probe-nt-2a.Probe.co.uk>
From: Shawn Hargreaves <ShawnH AT Probe DOT co DOT uk>
To: djgpp AT delorie DOT com
Subject: Re: Allegro BMP Color Depth Conversion
Date: Fri, 13 Feb 1998 09:59:11 -0000
MIME-Version: 1.0

Keith Bonawitz writes:
> How can I convert these 24-bit images to 256 color images?  
> I understand that an palette has to be created that matches
> most of the colors reasonably well, but how would I go about 
> doing this?

The simplest method is:

    set_color_depth(8);
    set_color_conversion(COLORCONV_REDUCE_TO_256);
    BITMAP *bmp = load_bitmap(...);

Allegro will then automatically reduce the color depth while loading 
the file. In fact this color conversion mode is the default, so 
unless you are changing this setting somewhere in your code, the 
data will already be reduced without you having to do a thing!

If you want more control over the details of this process, try
something like:

    BITMAP *bmp24, *bmp8;
    PALETTE pal;

    set_color_conversion(COLORCONV_NONE);
    bmp24 = load_bitmap(...);
    bmp8 = create_bitmap_ex(8, bmp24->w, bmp24->h);
    generate_optimized_palette(bmp24, pal, NULL);
    select_palette(pal);
    blit(bmp24, bmp8, 0, 0, 0, 0, bmp24->w, bmp24->h);

The palette generation code was written by Michal Mertl, and can
be found in src/quantize.c. You will have to talk to him if you 
want details of the algorithm: I haven't fully grokked it myself,
but it seems to work pretty well :-)

	Shawn Hargreaves.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019