B4J Question From 16.7 Mil.colors (RGB) to Indexed Color (256colors) ..Lower Quality Color Palette of Image...

Magma

Expert
Licensed User
Longtime User
Well sometimes,

to speed up the things or to have smaller files you need to change the color palette -> at Photoshop for example you can change it from RGB -> to Indexed Colored (Diffused...)

is that possible to make it at any image -> not a file at image / byte and how (b4j source code will help) ?

ps: at BitmapCreatorEffects - Erel has examples how to make it Grey Scale... but 256 colors will help more :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will create a 256 colors image:

1616568950684.png


B4X:
Private Sub CreateBC(bmp As B4XBitmap) As BitmapCreator
    Dim bc As BitmapCreator
    bc.Initialize(bmp.Width / bmp.Scale, bmp.Height / bmp.Scale)
    bc.CopyPixelsFromBitmap(bmp)
    Return bc
End Sub

Public Sub BinColors (bmp As B4XBitmap) As B4XBitmap
    Dim bc As BitmapCreator = CreateBC(bmp)
    Dim argb As ARGBColor
    For x = 0 To bc.mWidth - 1
        For y = 0 To bc.mHeight - 1
            bc.GetARGB(x, y, argb)
            argb.r = argb.r - (argb.r Mod 64)
            argb.g = argb.g - (argb.g Mod 64)
            argb.b = argb.b - (argb.b Mod 64)
            Log(argb)
            bc.SetARGB(x, y, argb)
        Next
    Next
    Return bc.Bitmap
End Sub

This is relevant if you want to process the image pixels, not if you want to save it with smaller size as the ssaved image format will not be affected by this.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
@Erel ...if you compress to jpeg bytes... the result of full color and the result of that you get from BinColors ----> it has big different... more than 15% !!!

is it possible to make it 16 colors ?

ps: it is extremely amazing - how fast is the loop in loop with b4j :) (i remember vb6/quickbasic - waiting... + limits of quickbasic ...64KB)
 
Upvote 0

emexes

Expert
Licensed User
The above code will actually make it 4 * 4 * 4 = 64 colors.
Change the modulus number to 32 for 256 colors. Change it to 128 for 8 colors. Making it 16 colors will be a bit more complicated.
Change the modulus number to 32 for the red and green channels (ie 3 bits red and green, 2 bits blue) for 256 colors (8 bits).
 
Upvote 0

emexes

Expert
Licensed User
So to see if correct:
...Mod 64 ----> give 64 colors image
...Mod 32 ----> give 256 colors image
...Mod 128 ---> give 8 colors image

correct >?
2/3rds correct.

...Mod 32 ----> give 512 colors image

3 bits each of red, green and blue = 9 bits total = 2^9 = 512 colors
 
Upvote 0

emexes

Expert
Licensed User
B4X:
            '252 distinct colors, with slightly more resolution for green
            argb.r = argb.r - (argb.r Mod 43) + 21    '6 distinct levels
            argb.g = argb.g - (argb.g Mod 37) + 18    '7 distinct levels
            argb.b = argb.b - (argb.b Mod 43) + 21    '6 distinct levels
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Upvote 0
Top