Here is example code how to do palette manipulation without touching the pixels with Java1.1:
import java.awt.*;
import java.awt.image.*;
public class paletteManipulation extends java.applet.Applet implements Runnable {
private final static int width = 640;
private final static int height = 480;
private final static int speed = 2;
Thread tr;
public MemoryImageSource mis;
private static Image image;
Graphics screen;
int[] jpgPixels = new int[width*height];
byte[] pixels = new byte[width*height];
public void run() {
byte[] R = new byte[256];
byte[] G = new byte[256];
byte[] B = new byte[256];
for(int i = 0; i < 256; i++) {
R[i] = (byte) i;
G[i] = (byte) i;
B[i] = (byte) i;
}
try {
MediaTracker track = new MediaTracker (this);
image = getImage(getDocumentBase(), "test.jpg");
track.addImage(image,666);
track.waitForAll();
PixelGrabber ap = new PixelGrabber (image, 0, 0, width, height, jpgPixels, 0, width);
while (ap.grabPixels() && ((ap.status() & ImageObserver.ALLBITS) == 0)) { }
}
catch (Exception e) {
e.printStackTrace ();
}
for(int i = 0; i < width*height; i++) {
pixels[i] = (byte) (jpgPixels[i] & 0xff);
}
screen = getGraphics();
mis = new MemoryImageSource (width, height, new IndexColorModel(8, 256, R, G, B), pixels, 0, width, null);
mis.setAnimated(true);
mis.setFullBufferUpdates(true);
image = createImage(mis);
boolean reset = false;
while(true) {
if((R[0] & 0xff) == 0xff) {
reset = true;
}
else {
reset = false;
}
for(int i = 0; i < 256; i++) {
int color = (R[i] & 0xff);
if(reset == true) {
color = i;
}
else {
color += speed;
}
if(color > 255) {
color = 255;
}
R[i] = (byte) (color & 0xff);
G[i] = (byte) (color & 0xff);
B[i] = (byte) (color & 0xff);
}
mis.newPixels(pixels, new IndexColorModel(8, 256, R, G, B), 0, width);
screen.drawImage(image, 0, 0, null);
}
}
public void start() {
if (tr == null) {
tr = new Thread(this);
tr.start();
}
}
public void stop() {
if (tr != null) {
tr.stop();
tr = null;
}
}
}
The code loads a grayscale jpg picture and then fades the palette until the picture is all white and then resets the palette and starts from the beginning. Basically you can do exactly the same “effect” as in the video with code similar to this one.
If you are working with large palettes (such as 24bit palette), realtime manipulation of the palette might be too slow. In this case, you can simply precalculate couple of palettes, such as “night palette” and “day palette” and then switch the palette very fast when needed without touching the pixels. Of course if you need a nice fading effect, you would need to precalculate a lot of palettes…