Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * @(#)ColorCycle.java  1.0.1  2010-01-08
   * 
   * Copyright (c) 2009-2010 Werner Randelshofer, Goldau, Switzerland.
   * All rights reserved.
   *
   * You may not use, copy or modify this file, except in compliance with the
   * license agreement you entered into with Werner Randelshofer.
   * For details see accompanying license terms.
  */
 package org.monte.media.ilbm;
 
Implements DRNG color cycling for an IFF ILBM image.

 ILBM DRNG DPaint IV enhanced color cycle chunk
 --------------------------------------------

 set {
     active=1,DPReserved=4
 } drngFlags;

 /* True color cell * /
 typedef struct {
     UBYTE cell;
     UBYTE r;
     UBYTE g;
     UBYTE b;
 } ilbmDRNGDColor;

 /* Color register cell * /
 typedef struct {
     UBYTE cell;
     UBYTE index;
 } ilbmDRNGDIndex;

 /* DRNG chunk. * /
 typedef struct {
     UBYTE min; /* min cell value * /
     UBYTE max; /* max cell value * /
     UWORD rate; /* color cycling rate, 16384 = 60 steps/second * /
     UWORD set drngFlags flags; /* 1=RNG_ACTIVE, 4=RNG_DP_RESERVED * /
     UBYTE ntrue; /* number of DColorCell structs to follow * /
     UBYTE ntregs; /* number of DIndexCell structs to follow * /
     ilbmDRNGDColor[ntrue] trueColorCells;
     ilbmDRNGDIndex[ntregs] colorRegisterCells;
 } ilbmDRangeChunk;
 

Author(s):
Werner Randelshofer
Version:
1.0.1 2010-11-08 Fixed color cycling rate.
1.0 2009-12-23 Created.
 
 public class DRNGColorCycle extends ColorCycle {
 
     public abstract static class Cell implements Comparable<Cell> {
 
         protected int cell;
         protected int value;
 
         public Cell(int cell) {
             this. = cell;
         }

        
Reads the initial value of the cell which is either taken from the rgb palette or from an rgb value stored by the cell.

Parameters:
rgbs the palette.
isHalfbright whether the halfbright value shall be taken.
 
         public abstract void readValue(int[] rgbsboolean isHalfbright);

        
Writes the final value of the cell into the color palette - or does nothing
 
         public abstract void writeValue(int[] rgbsboolean isHalfbright);
 
         @Override
         public int compareTo(Cell that) {
             return this. - that.cell;
         }
 
         @Override
         public boolean equals(Object o) {
             if (o instanceof Cell) {
                 Cell that = (Cello;
                 return that.cell == this.;
             }
             return false;
         }
 
         @Override
         public int hashCode() {
             return ;
         }
    }

    
True color cell.
    public static class DColorCell extends Cell {
        private int rgb;
        public DColorCell(int cellint rgb) {
            super(cell);
            this. = rgb;
        }

        
Sets the initial value of the cell from its rgb instance variable.
        @Override
        public void readValue(int[] rgbsboolean isHalfbright) {
             = isHalfbright ?  & 0x0f0f0f : ;
        }

        
Does nothing.
        @Override
        public void writeValue(int[] rgbsboolean isHalfbright) {
            // nothing to do
        }
    }

    
Color register cell.
    public static class DIndexCell extends Cell {
        private int index;
        public DIndexCell(int cellint index) {
            super(cell);
            this. = index;
        }

        
Sets the initial value of the cell from the rgb palette.
        @Override
        public void readValue(int[] rgbsboolean isHalfbright) {
             = isHalfbright ? rgbs[ + 32] : rgbs[];
        }

        
Writes the final value of the cell into the color palette.
        @Override
        public void writeValue(int[] rgbsboolean isHalfbright) {
            rgbs[isHalfbright ?  + 32 : ] = ;
        }
    }
    
Lowest color register of the range.
    private int min;
    
Highest color register of the range.
    private int max;
    
Whether the image is in EHB mode.
    private boolean isEHB;
    
List with interpolated cells.
    private Cell[] ic;
    
Actual cells with values.
    private Cell[] cells;
    private boolean isReverse;

    

Parameters:
rate
timeScale
min
max
isActive
isEHB
cells
    public DRNGColorCycle(int rateint timeScaleint minint maxboolean isActiveboolean isEHBCell[] cells) {
        super(ratetimeScaleisActive);
        this. = min;
        this. = max;
        this. = isEHB;
        this. = cells;
    }
    public int getMin() {
        return ;
    }
    public int getMax() {
        return ;
    }
    private void interpolateCells(int[] rgbs) {
        //System.out.println("DRNGColorCycle " + min + ".." + max + " number of cells:" + cells.length);
         = new Cell[ -  + 1];
        Arrays.sort();
        for (int i = 0; i < .i++) {
            [[i]. - ] = [i];
            [i].readValue(rgbsfalse);
        }
        int left = . - 1;
        int right = 0;
        for (int i = 0; i < .i++) {
            if ([right]. == i) {
                left = right;
                right = (right == . - 1) ? 0 : right + 1;
            } else {
                //System.out.println("  interpolating cell "+i+"("+(i+min)+")"+" with values from "+cells[left].cell+" and "+cells[right].cell);
                int levels=[left].<[right].?[right].-[left].:-[left].+[right].+1;
                int blend=[right].>(i+)?[right].-(i+):-(i+)+[right].+1;
                int lrgb=[left].;
                int rrgb=[right].;
                [i]=new DColorCell(i,//
                       ( ((lrgb&0xff0000)*blend+(rrgb&0xff0000)*(levels-blend))/levels&0xff0000)|//
                       ( ((lrgb&0xff00)*blend+(rrgb&0xff00)*(levels-blend))/levels&0xff00)|//
                       ( ((lrgb&0xff)*blend+(rrgb&0xff)*(levels-blend))/levels)//
                       );
                //System.out.println("  levels:"+levels+" blend:"+blend+" lrgb:"+Integer.toHexString(lrgb)+" rrgb:"+Integer.toHexString(rrgb)+" blend:"+Integer.toHexString(((DColorCell)ic[i]).rgb));
            }
        }
    }
    @Override
    public void doCycle(int[] rgbslong time) {
        if () {
            if ( == null) {
                interpolateCells(rgbs);
            }
            int shift = (int) ((time *  /  / 1000) % (.));
            if () {
                for (int i = 0; i < .i++) {
                    [i].readValue(rgbsfalse);
                }
                for (int j = 0; j < shiftj++) {
                    int tmp = [0].;
                    for (int i = 1; i < .i++) {
                        [i - 1]. = [i].;
                    }
                    [. - 1]. = tmp;
                }
                for (int i = 0; i < .i++) {
                    [i].writeValue(rgbsfalse);
                }
                if () {
                    for (int i = 0; i < .i++) {
                        [i].readValue(rgbstrue);
                    }
                    for (int j = 0; j < shiftj++) {
                        int tmp = [0].;
                        for (int i = 1; i < .i++) {
                            [i - 1]. = [i].;
                        }
                        [. - 1]. = tmp;
                    }
                    for (int i = 0; i < .i++) {
                        [i].writeValue(rgbstrue);
                    }
                }
            } else {
                for (int i = 0; i < .i++) {
                    [i].readValue(rgbsfalse);
                }
                for (int j = 0; j < shiftj++) {
                    int tmp = [. - 1].;
                    for (int i = . - 1; i > 0; i--) {
                        [i]. = [i - 1].;
                    }
                    [0]. = tmp;
                }
                for (int i = 0; i < .i++) {
                    [i].writeValue(rgbsfalse);
                }
                if () {
                    for (int i = 0; i < .i++) {
                        [i].readValue(rgbstrue);
                    }
                    for (int j = 0; j < shiftj++) {
                        int tmp = [. - 1].;
                        for (int i = . - 1; i > 0; i--) {
                            [i]. = [i - 1].;
                        }
                        [0]. = tmp;
                    }
                    for (int i = 0; i < .i++) {
                        [i].writeValue(rgbstrue);
                    }
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X