News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Monochrome Palette  (Read 936 times)

Squall_FF8

  • Full Member
  • ***
  • Posts: 198
    • View Profile
Monochrome Palette
« on: February 21, 2018, 09:10:17 am »
Hey guys,

I have a simple task - to make a function that populate a palette with monochrome colors, depending on BPP (2, 3, 4, 8 ).

Could you share how you gonna wright that function in a program language of your choice? The function gets 2 parameters:
 - Pal - this is our palette. Think of it as array of integer (4 bytes per entry)
 - Num - number of possible colors. Num = 2 ^ BPP. So Num will be 4, 8, 16, 256.

:edit:
I have to specify that palettes entries are in the format ARGB, 8 bits each. For the sake of this task we don care about A, so you may assume it is 0.
« Last Edit: February 21, 2018, 09:27:01 am by Squall_FF8 »

weissvulf

  • Sr. Member
  • ****
  • Posts: 324
  • Good news! An anomaly solved the enigma.
    • View Profile
Re: Monochrome Palette
« Reply #1 on: February 21, 2018, 06:33:43 pm »
Is this to produce a surrogate palette for image data without a palette? I worked on something similar for that TIM tool and was never happy with the results. The problem being that the image data values in an indexed image have no relation to an 'appropriate' greyscale value.

In other words, in a 16 color image, a value of 0x01 in the image data could have originally represented black, white or any lightness/darkness of color. When I tried to generate a generic grayscale palette, things usually turned out very low-contrast compared to their original. I finally opted for a wildly-colored random palette which overall seemed to make the image more recognizable.

There are probably much better ways of doing it, but this was roughly what my attempt looked like, though I used a structure instead of an array. Also, note that the "step" variable does require floating point values for some color-counts:

Code: [Select]
Func _makePal ($pal, $num)

;calcualte distance between palette values, generates FloatingPoint
$step = 0xFF / ($num-1) ;example FF/4-1 color = 0x55 per step

;calculate additive value for stepping 3byte RGB channels: 0x55 to 0x555555
$step = $step * 0x10101

;set initial palette entry to black
$pal[0] = 0

;fill 1 palette entry per loop
For $i = 1 To $num-1

;Palette entry = previous palette entry plus 'step'
$pal[$i] = $pal[$i - 1] + $step
Next
Return $pal
EndFunc

Squall_FF8

  • Full Member
  • ***
  • Posts: 198
    • View Profile
Re: Monochrome Palette
« Reply #2 on: February 22, 2018, 03:17:22 am »
Is this to produce a surrogate palette for image data without a palette? ... generic grayscale palette
Exactly - generic grayscale palette! This is for the tool I started not long - based on LZ77Restructor2, but will give much more instruments and freedom to 'see' the image from any game ROM. I actually made a good progress implementing GBA 4BPP LZ10 compressed graphics (which is pretty much half of what the LZ77Restructor2 does). Now I want to expand to include 8BPP LZ10, then not compressed versions on both, then SNES 2, 3, 4 BPP uncompressed, then LZSS (Final Fantasy titles),...

Anyway I need a routine that can produce generic grayscale, that hit 0 in Pal[0] ans 255 and Pal[2^Bpp-1]. As you mentioned that usually is not the best generic palette because many titles use specific index for specific purpose. Its enough for me as far as the image is easily recognizable by the user. In all cases his next step should be finding/using the original colors.

I used 3 different approaches but got different results and most of the time, couldn't reach 255 in each BPP. That's why I posted the question here. Last night I was reminded about our conversation of colors 5b -> 8b, so I came to:
Code: [Select]
procedure MakeMonoPal(var Pal: tPalette; Num: integer);
  var i, r: integer;
begin
  for i := 0 to Num - 1 do begin
    r := (i * 255) div (Num - 1);
    Pal[i] := r + r shl 8 + r shl 16;
  end;
end;
Opening the browser this morning I saw your post and its dangerously similar  :laugh:  :beer:

weissvulf

  • Sr. Member
  • ****
  • Posts: 324
  • Good news! An anomaly solved the enigma.
    • View Profile
Re: Monochrome Palette
« Reply #3 on: February 23, 2018, 08:27:58 pm »
Yeah, very similar.  8) I suppose that's to be expected given the criteria.

A lot of PS1 graphics have a palette with black as 00 and white as 01. The greyscale method above usually makes these graphics render almost solid black. It's especially notable with text-graphics which are 16 color, but often only use the first 2 or 4 colors of the palette.

To account for this, I was going to try to use XOR bit flipping to calculate every other palette entry. In other words, pal[odd] = XOR of pal[even] and 0xFF etc.
If the above method would generate:
00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
It would instead become:
00 FF 22 DD 44 BB 66 99 88 77 AA 55 CC 33 EE 11

I haven't gotten around to testing if this actually produces any benefit, but it might be worth trying.  :thumbsup:

Squall_FF8

  • Full Member
  • ***
  • Posts: 198
    • View Profile
Re: Monochrome Palette
« Reply #4 on: February 25, 2018, 06:38:07 pm »
I haven't gotten around to testing if this actually produces any benefit, but it might be worth trying.  :thumbsup:
Probably a single palette useful for all is not going to work well. So probably something similar to YY-CHR - use couple of predefined ones and leave the user to try/experiment will be the best course of action.