VicePALRenderer.java
/*
* PAL colour tables and rendering arithmetic derived from VICE 3.10:
* video-color.c, render1x1pal.c (John Selck, Groepaz) and viciisc/vicii-color.c.
* The default x64sc measured 8565R2 even/odd palette is used without modification.
* Copyright and licence: GNU GPL version 2 or, at your option, any later version.
* https://sourceforge.net/projects/vice-emu/files/releases/vice-3.10.tar.gz/download
*/
package libsidplay.components.mos656x;
/**
* Fast, fixed VICE 3.10 default PAL preset: 50% horizontal blur, UV delay line,
* 125% contrast/saturation and gamma 1.00. One output pixel per VIC colour
* code, without interpolated half-pixels or artificial scanline rows.
* No carrier waveform, frame blending or per-pixel allocation.
* Pixels are packed as 0xAABBGGRR, giving RGBA bytes in little-endian buffers.
*
* The fixed tables retain x64sc's default MOS8565 palette and integer arithmetic;
* this colour preset deliberately does not vary with the emulated VIC.
*/
public final class VicePALRenderer {
private static final int[] Y_LOW = {0, 2097152, 641728, 1340064, 761248, 1048576, 497024, 1600096,
761248, 497024, 1048576, 641728, 966784, 1600096, 966784, 1340064};
private static final int[] Y_HIGH = {0, 12517376, 3830314, 7998507, 4543699, 6258688, 2966612, 9550573,
4543699, 2966612, 6258688, 3830314, 5770492, 9550573, 5770492, 7998507};
private static final int[][] U = {
{0, 0, -2150, 1869, 26000, -22240, 32527, -35130, -17526, -29368, -2150, 0, 0, -22240, 32527, 0},
{0, 0, -13090, 12329, 18224, -14493, 35228, -33533, -23899, -29162, -13090, 0, 0, -14493, 35228, 0}};
private static final int[][] V = {
{0, 0, 19759, -20049, 13628, -15828, -8293, 3624, 17063, 10926, 19759, 0, 0, -15828, -8293, 0},
{0, 0, 18679, -19041, 17048, -18042, -1871, 6637, 14919, 11265, 18679, 0, 0, -18042, -1871, 0}};
private static final int[] GAMMA = gamma();
private final int width;
private final byte[] padded;
private final int[] previousU, previousV;
private int previousRaster = Integer.MIN_VALUE;
public VicePALRenderer(int width) {
if (width <= 0 || width > VIC.MAX_WIDTH) throw new IllegalArgumentException("Invalid PAL line width");
this.width = width;
padded = new byte[width + 4];
previousU = new int[width];
previousV = new int[width];
}
/** Decode one source raster at native resolution, retaining the PAL chroma delay line. */
public void renderLine(byte[] colors, int raster, int[] line) {
System.arraycopy(colors, 0, padded, 2, width);
padded[0] = padded[1] = colors[0];
padded[width + 2] = padded[width + 3] = colors[width - 1];
if (previousRaster != raster - 1) {
initialiseChroma((raster ^ 1) & 1);
}
int parity = raster & 1;
int[] cb = U[parity], cr = V[parity];
int gain = parity == 0 ? 32 : 20; // VICE 3.10's default PALOddLineOffset=500.
int sumU = cb[padded[0]] + cb[padded[1]] + cb[padded[2]];
int sumV = cr[padded[0]] + cr[padded[1]] + cr[padded[2]];
for (int x = 0; x < width; x++) {
int c0 = padded[x], c1 = padded[x + 1], c2 = padded[x + 2], c3 = padded[x + 3];
sumU += cb[c3];
sumV += cr[c3];
int y = Y_LOW[c1] + Y_HIGH[c2] + Y_LOW[c3];
int u = (sumU + previousU[x]) * gain;
int v = (sumV + previousV[x]) * gain;
previousU[x] = sumU;
previousV[x] = sumV;
sumU -= cb[c0];
sumV -= cr[c0];
store(x, y, u, v, line);
}
previousRaster = raster;
}
private void initialiseChroma(int parity) {
int[] cb = U[parity], cr = V[parity];
int u = cb[padded[0]] + cb[padded[1]] + cb[padded[2]];
int v = cr[padded[0]] + cr[padded[1]] + cr[padded[2]];
for (int x = 0; x < width; x++) {
u += cb[padded[x + 3]];
v += cr[padded[x + 3]];
previousU[x] = u;
previousV[x] = v;
u -= cb[padded[x]];
v -= cr[padded[x]];
}
}
private void store(int x, int y, int u, int v, int[] line) {
int red = (y + v) >> 16;
int blue = (y + u) >> 16;
int green = (y - ((50 * u + 130 * v) >> 8)) >> 16;
line[x] = 0xff000000 | GAMMA[256 + red] | GAMMA[256 + green] << 8 | GAMMA[256 + blue] << 16;
}
public void reset() {
previousRaster = Integer.MIN_VALUE;
}
private static int[] gamma() {
int[] table = new int[768];
for (int i = 0; i < table.length; i++) {
// VICE 3.10 uses gamma 1.00 directly, without the older PAL / 2.8 conversion.
table[i] = Math.max(0, Math.min(255, (int) ((i - 256) * 1.25f)));
}
return table;
}
}