My current routine for screen 2 conversion is the following: (can you read matlab code ?)
function [ OUT ] = quantize( T )
%QUANTIZE Summary of this function goes here
%
% T is NVx192 matrix encoding NV 8x8 blocks
% each row vector correspondes to a 8x8 block in RGB - i.e. 8x8x3=192
% columns 1:64 are red, columns 65:128 are blue, columns 129:192 blue
% the 8x8 block is scanned by row
%
% OUT has the same structure of T and returns the NV 8x8 blocks
% encoded in screen 2 of a TMS9918
% RGB palette of the TMS9918
pal = [ 0, 0, 0
0, 0, 0
0, 241, 20
68, 249, 86
85, 79, 255
128, 111, 255
250, 80, 51
12, 255, 255
255, 81, 52
255, 115, 86
226, 210, 4
242, 217, 71
4, 212, 19
231, 80, 229
208, 208, 208
255, 255, 255 ];
% number of blocks to be converted
NV = size(T,1);
C = uint8(zeros(size(T)));
R = T(:,1:64);
G = T(:,65:128);
B = T(:,129:192);
% encode each block individually
for j=1:NV
r = uint8(zeros(1,64));
g = uint8(zeros(1,64));
b = uint8(zeros(1,64));
% encode each block line by line
for n=0:7
t = [(n*8):(n*8+7)] + 1;
p = [ R(j,t)' G(j,t)' B(j,t)']; % this is 8x3 matrix representing a line
% scan all possible couples of colors for each line
% the couples of backgound-foreground colors are numbere
% avoiding duplications - there are 105 possibilities
dold=1e41;
for c1=2:16;
for c2=(c1+1):16;
c = [pal(c1,:) ; pal(c2,:)]; % this is 2x3 matrix with the 2 tentative colors
[x,i]=min(distfun(p,c)'); % associate the 8 points to the closest of the 2 colors
d=sum(x);
if d<dold
cold=c;
dold=d;
iold=i;
end
end
end
r(t) = uint8(cold(iold,1));
g(t) = uint8(cold(iold,2));
b(t) = uint8(cold(iold,3));
end
C(j,:) = [r g b];
end
OUT=C;
%%%%%%%%%%%%
function D = distfun(X, C)
%DISTFUN Calculate point to cluster centroid distances.
[n,p] = size(X);
D = zeros(n,size(C,1));
clusts = 1:size(C,1);
for i = clusts
D(:,i) = sum((double(X) - double(C(repmat(i,n,1),:))).^2, 2);
end
It does flat conversion: this implies that the scr2 color clash and the color quantization
due to msx1 palette affect the result drammatically.
I would like to introduce Floyd dithering in the conversion, like in Jannone's converter.
The results on the webpage VDPENC2 have been obtained using his converter on the
full color 24bit tiles and they look fine. Dithering improves a lot the final result.
Moreover, having a fast routine, dithering could be included in the kmeans algorithm
used for tile optimization. This could improve the result even further - but here more
tests are required.
Unfortunately Jannone's tool can be only used by converting the tiles as they are a
single image 256x192. This implies that errors on colors diffuse across boundaries of the
tiles that are displayed in adjacent positions.This should be avoided, as each tile can be
placed in any position of the screen by the player.
Can anyone help me in implementing a fast color dithering to be applied
within the 8x8 block during conversion to scr2 ?