Re: Palette Problem
- Posted by David Cuny <dcuny at LANSET.COM> May 13, 1999
- 468 views
Gwenakl Joret wrote: >I've a little problem using palette in Eu using Read_bitmpap() : How >to Change a palette of a Bmp ? : The routine readBitmapFromFile() returns the bitmap portion (or an error code) of the requested bitmap. It reads the requested bitmap, then maps the bitmap's palette to the current palette. Finally, it replaces the references in the bitmap from it's original pal to the current pal. Some obvious points: 1. You need to set the graphics_mode before calling the function. 2. Resolutions with more colors take longer to convert 3. It works in all graphics modes, but higher modes have more colors, so map better To speed things up, I can suggest the following: 1. Pete has an assembly version of this on his home page - runs faster! 2. Reduce the colors in the bitmap you are reading, if you can. 3. If you always use the same bitmaps, map them all to the same palette: - read all the bitmaps mapped to the same palette - save the bitmaps with the new palette Once they are converted over, you can use the regular Euphoria tools again. Hope this helps! -- David Cuny include image.e procedure abortErr( sequence msg, sequence args ) -- display an error message and abort printf( 1, msg, args ) abort( 0 ) end procedure function abs( atom a ) -- return absolute value if a > 0 then return a else return -a end if end function function convertToPalette( sequence sourcePal ) -- convert the color list to palette indexes atom err, diff, best sequence destPal, colorIndex -- get palette destPal = get_all_palette() -- adjust source sourcePal = floor( sourcePal/4 ) -- build holder for results colorIndex = repeat( 0, length( sourcePal ) ) -- look at each entry for i = 1 to length( sourcePal ) do -- set the last best match as impossibly bad best = #FFFFFF -- look through the palette for entry = 1 to length( destPal ) do -- error amount for this entry err = 0 -- for each {rgb} for rgb = 1 to 3 do -- error is difference between the two diff = abs( sourcePal[i][rgb] - destPal[entry][rgb] ) err = err + diff end for -- better match than prior best match? if err < best then -- save error amount best = err -- store the palette index colorIndex[i] = entry end if end for end for return colorIndex end function function readBitmapFromFile( sequence fName ) -- load a bitmap from the current file, and map to current palette integer index sequence replace object res -- attempt to read bitmap res = read_bitmap( fName ) if integer( res ) then -- return the error message return res end if -- adjust the current palette replace = convertToPalette(res[1]) -- replace values with references to new palette for i = 1 to length( res[2] ) do for j = 1 to length( res[2][i] ) do -- get index index = res[2][i][j] -- get actual color index = replace[index+1]-1 -- replace res[2][i][j] = index end for end for -- return bitmap return res[2] end function