GaugeUtil.java

package libsidutils.oscilloscope;

import libsidplay.common.SIDEmu;

import java.nio.ByteBuffer;

public abstract class GaugeUtil {

    private static final int ALPHA = 0xff000000;
    private static final int[] gaugeColors = new int[256];

    static {
        for (int i = 0; i < gaugeColors.length; i++) {
            int color = Math.round((float) Math.sqrt(i / 255f) * 255f);
            gaugeColors[i] = ALPHA | ((color & 0xff) << 8) | (color >> 8);
        }
    }

    private final int width;
    private final int height;

    public GaugeUtil(int width, int height) {
        this.width = width;
        this.height = height;
    }

    private TraceHistory history = TraceHistory.EMPTY;
    private ByteBuffer snapshot;
    private int snapshotOffset, snapshotWidth;

    public void setHistory(TraceHistory history) {
        this.history = history;
        snapshot = null;
    }

    public void setSnapshot(ByteBuffer frame, int offset, int columns) {
        snapshot = frame;
        snapshotOffset = offset;
        snapshotWidth = columns;
    }

    private float minimum(int x) {
        return snapshot == null ? history.minimum(width - x) :
                snapshot.getFloat(snapshotOffset + Math.min(snapshotWidth - 1, x * snapshotWidth / width) * 8);
    }

    private float maximum(int x) {
        return snapshot == null ? history.maximum(width - x) :
                snapshot.getFloat(snapshotOffset + Math.min(snapshotWidth - 1, x * snapshotWidth / width) * 8 + 4);
    }

    public void draw(SIDEmu sidemu) {
        final int shade = 255;
        for (int x = 0; x < width; x++) {
            float startPos = (1 - maximum(x)) * (height - 1);
            if (startPos < 0) {
                startPos = 0;
            }

            float endPos = (1 - minimum(x)) * (height - 1);
            if (endPos > height - 1) {
                endPos = height - 1;
            }

            int intStartPos = (int) Math.floor(startPos);
            int intEndPos = (int) Math.ceil(endPos);

            /* one pixel line? */
            if (intStartPos == intEndPos) {
                drawLine(x, intStartPos, x, intStartPos, gaugeColors[shade]);
                continue;
            }

            /* At least 1 pixel separating, calculate start and end colors */
            float firstPixel = startPos - intStartPos;
            firstPixel = 1 - firstPixel;
            if (firstPixel < 0) {
                firstPixel = 0;
            }
            if (firstPixel > 1) {
                firstPixel = 1;
            }

            float lastPixel = intEndPos - endPos;
            lastPixel = 1 - lastPixel;
            if (lastPixel < 0) {
                lastPixel = 0;
            }
            if (lastPixel > 1) {
                lastPixel = 1;
            }

            /* Draw end points */
            drawLine(x, intStartPos, x, intStartPos, gaugeColors[(int) (shade * firstPixel)]);

            drawLine(x, intEndPos, x, intEndPos, gaugeColors[(int) (shade * lastPixel)]);

            intStartPos += 1;
            intEndPos -= 1;

            /* Still space for the middle line? */
            if (intStartPos <= intEndPos) {
                drawLine(x, intStartPos, x, intEndPos, gaugeColors[shade]);
            }
        }
    }

    protected abstract void drawLine(int x, int y1, int x2, int y2, int color);
}