image Color Cycling problem
in
Programming Questions
•
1 year ago
Hi, I am using the example given by Christophe Guebert for color cycling (
link and
images). It says that the the images are 8 bit with dynamic pallete. Actually I want to add different images than given in the example. but I am not able to figure out how to create the images which are used in the example ( auther says the images are in
scenes are actually Amiga IFF/ILBM
files, originally created with Deluxe Paint
in DOS). Is it possible to create similar images using Photoshop or any other software. below is the code. please help me
- CyclingImage[] images;
- void setup()
- {
- size(640,480);
- images = new CyclingImage[3];
- images[0] = new CyclingImage("C1.gz");
- images[1] = new CyclingImage("C2.gz");
- images[2] = new CyclingImage("C3.gz");
- }
- void draw()
- {
- // Choosing images to be displayed
- float f = constrain(mouseX / (float) width, 0, 1);
- f *= images.length - 1;
- int index = floor(f);
- f -= index;
- f = 3*f*f - 2*f*f*f; // smoother interpolation
- float t = millis() / 1000.0 / 280; // arbitrary coefficient for speed
- images[index].update(t);
- images[index].display();
- tint(color(255, f*255)); // using "tint" to go from one image to the next
- images[index+1].update(t);
- images[index+1].display();
- tint(color(255));
- //if(showFPS) text(frameRate, 0, 10);
- }
- // 8-bits image with an dynamic palette
- class CyclingImage
- {
- PImage img;
- private PImage _img;
- private color[] _originalColors;
- class Cycle { int reversed, rate, low, high; }
- private Cycle[] _cycles;
- private int[] _pixels, _cyclingPixels; // WTF : can't do unsigned byte in Java ?
- CyclingImage(String filename)
- {
- // Loading file
- // Using "createInput" to decompress the files
- DataInputStream in = new DataInputStream(createInput(filename));
- int w=0, h=0;
- try
- {
- // size of image
- w = in.readUnsignedByte() + in.readUnsignedByte()*256;
- h = in.readUnsignedByte() + in.readUnsignedByte()*256;
- // palette of colors
- _originalColors = new color[256];
- for(int i=0; i<256; i++)
- _originalColors[i] = color(in.readUnsignedByte(), in.readUnsignedByte(), in.readUnsignedByte());
- // information on cycling colors
- int nbCycles = in.readUnsignedByte();
- _cycles = new Cycle[nbCycles];
- for(int i=0; i<nbCycles; i++)
- {
- _cycles[i] = new Cycle();
- _cycles[i].reversed = in.readUnsignedByte() + in.readUnsignedByte()*256;
- if(_cycles[i].reversed != 0)
- println("reversed = " + _cycles[i].reversed);
- _cycles[i].rate = in.readUnsignedByte() + in.readUnsignedByte()*256;
- _cycles[i].low = in.readUnsignedByte();
- _cycles[i].high = in.readUnsignedByte();
- }
- // image data
- _pixels = new int[w*h];
- for(int i=0; i<w*h; i++)
- _pixels[i] = in.readUnsignedByte();
- } catch(Exception e) {}
- // Creating image
- if(w*h != 0)
- {
- _img = createImage(w, h, RGB);
- _img.loadPixels();
- for(int i=0; i<w*h; i++)
- _img.pixels[i] = _originalColors[_pixels[i]];
- _img.updatePixels();
- }
- // Extracting pixels that can change
- boolean[] cyclingColors = new boolean[256];
- for(int i=0; i<256; i++) cyclingColors[i] = false;
- for(int i=0; i<_cycles.length; i++)
- { // marking changing colors
- if(_cycles[i].rate == 0) continue;
- for(int j=_cycles[i].low; j<=_cycles[i].high; j++)
- cyclingColors[j] = true;
- }
- int nb = 0;
- for(int i=0; i<w*h; i++)
- if(cyclingColors[_pixels[i]]) nb++; // computing size of array
- // creating an index of the pixels that need to be updated
- _cyclingPixels = new int[nb];
- int j=0;
- for(int i=0; i<w*h; i++)
- if(cyclingColors[_pixels[i]])
- _cyclingPixels[j++] = i;
- }
- void update(float t)
- {
- color[] newColors = new color[256]; // (no need for initialization)
- // Cycling palette colors
- for(int i=0; i<_cycles.length; i++)
- {
- Cycle c = _cycles[i];
- if(c.rate == 0)
- continue;
- float amt = t * c.rate;
- int range = c.high - c.low + 1;
- int amount = floor(amt) % range;
- amt -= floor(amt);
- for(int j=0; j<range; j++) // cycling and blending colors
- newColors[c.low + (j + amount) % range] =
- lerpColor(_originalColors[c.low + (j + 1) % range],
- _originalColors[c.low + j], amt);
- }
- // Updating image
- _img.loadPixels();
- for(int i=0; i<_cyclingPixels.length; i++) // only the pixels that need to be updated
- _img.pixels[_cyclingPixels[i]] = newColors[_pixels[_cyclingPixels[i]]];
- _img.updatePixels();
- }
- void display()
- { // nothing fancy here
- image(_img, 0, 0);
- }
- }
1