r←n optimal img;bmp;bin;rgb
;i;p;v;b;euclid;most
;m;⎕IO
⍝ compute optimal palette
⍝ it is recomended using
⍝ a shrinked image
⎕IO←0
r←⍬
⍝ 12996=114*2=(⌊0.45×255)*2, 0.45 is the threshold constant from the paper.
euclid←{∧/12996≤+⌿×⍨r(-⍤1 0⍤¯1)⍥((3⍴256)∘⊤)⍵}
most←(((⊢⍳⌈/)⊢/)⌷⊣/){⍺(+/⍵)}⌸
⍝ 16*3 binning map, instead of 10*3 bins used in the paper.
bmp←16⊥⌊img÷16
p←,rgb←256⊥img
⍝ vote
i←0
:Repeat
v←euclid rgb
⍝ count bins
b←bmp most⍥,v
r,←p[⊃⍒,(⊢×{+/,⍵}⌺21 21)bmp=b]
i+←1
:Until i=n ⍝ User has to specify the number of colors.
⍝ fin, translate the compressed palette
r←(3 1,≢r)⍴(3⍴256)⊤r
The above APL code is an altered implementation of https://v-sense.scss.tcd.ie/wp-content/uploads/2018/09/IMVIP18_palette.pdf
The input is an array of 3 Y X describes the RGB image of size X×Y.
The output is an array of 3 1 X describes the palette of length X.
The max value of these arrays are assumed to be 255.
I'd like to have comments on APL coding style, performance, and space efficiency.