2019-11-28 01:12:26 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace PluginCommon {
|
|
|
|
|
/// <summary>
|
2019-12-05 18:21:07 +00:00
|
|
|
|
/// Bitmap with 8-bit palette indices, for use with visualization generators.
|
2019-11-28 01:12:26 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class VisBitmap8 : IVisualization2d {
|
|
|
|
|
public const int MAX_DIMENSION = 4096;
|
|
|
|
|
|
2019-12-05 02:05:17 +00:00
|
|
|
|
// IVisualization2d
|
2019-11-28 01:12:26 +00:00
|
|
|
|
public int Width { get; private set; }
|
2019-12-05 02:05:17 +00:00
|
|
|
|
|
|
|
|
|
// IVisualization2d
|
2019-11-28 01:12:26 +00:00
|
|
|
|
public int Height { get; private set; }
|
|
|
|
|
|
|
|
|
|
private byte[] mData;
|
|
|
|
|
private int[] mPalette;
|
2019-12-07 17:11:25 +00:00
|
|
|
|
private int mNextColorIdx;
|
2019-11-28 01:12:26 +00:00
|
|
|
|
|
2019-12-05 02:05:17 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="width">Bitmap width, in pixels.</param>
|
|
|
|
|
/// <param name="height">Bitmap height, in pixels.</param>
|
2019-11-28 01:12:26 +00:00
|
|
|
|
public VisBitmap8(int width, int height) {
|
2019-12-07 01:19:27 +00:00
|
|
|
|
if (width <= 0 || width > MAX_DIMENSION || height <= 0 || height > MAX_DIMENSION) {
|
|
|
|
|
throw new ArgumentException("Bad bitmap width/height " + width + "," + height);
|
|
|
|
|
}
|
2019-11-28 01:12:26 +00:00
|
|
|
|
|
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
|
|
|
|
|
|
|
|
|
mData = new byte[width * height];
|
|
|
|
|
mPalette = new int[256];
|
2019-12-07 17:11:25 +00:00
|
|
|
|
mNextColorIdx = 0;
|
2019-11-28 01:12:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetPixel(int x, int y) {
|
|
|
|
|
byte pix = mData[x + y * Width];
|
|
|
|
|
return mPalette[pix];
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 17:11:25 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the color for a single pixel.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X coordinate.</param>
|
|
|
|
|
/// <param name="y">Y coordinate.</param>
|
|
|
|
|
/// <param name="colorIndex">Color index.</param>
|
2019-11-28 01:12:26 +00:00
|
|
|
|
public void SetPixelIndex(int x, int y, byte colorIndex) {
|
2019-12-01 01:52:33 +00:00
|
|
|
|
if (x < 0 || x >= Width || y < 0 || y >= Height) {
|
2019-11-28 01:12:26 +00:00
|
|
|
|
throw new ArgumentException("Bad x/y: " + x + "," + y + " (width=" + Width +
|
|
|
|
|
" height=" + Height + ")");
|
|
|
|
|
}
|
2019-12-07 17:11:25 +00:00
|
|
|
|
if (colorIndex < 0 || colorIndex >= mNextColorIdx) {
|
2019-11-28 01:12:26 +00:00
|
|
|
|
throw new ArgumentException("Bad color: " + colorIndex + " (nextCol=" +
|
2019-12-07 17:11:25 +00:00
|
|
|
|
mNextColorIdx + ")");
|
2019-11-28 01:12:26 +00:00
|
|
|
|
}
|
|
|
|
|
mData[x + y * Width] = colorIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 17:11:25 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the color for all pixels.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="colorIndex">Color index.</param>
|
2019-12-07 05:25:31 +00:00
|
|
|
|
public void SetAllPixelIndices(byte colorIndex) {
|
2019-12-07 17:11:25 +00:00
|
|
|
|
if (colorIndex < 0 || colorIndex >= mNextColorIdx) {
|
2019-12-07 05:25:31 +00:00
|
|
|
|
throw new ArgumentException("Bad color: " + colorIndex + " (nextCol=" +
|
2019-12-07 17:11:25 +00:00
|
|
|
|
mNextColorIdx + ")");
|
2019-12-07 05:25:31 +00:00
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < mData.Length; i++) {
|
|
|
|
|
mData[i] = colorIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 01:12:26 +00:00
|
|
|
|
// IVisualization2d
|
|
|
|
|
public byte[] GetPixels() {
|
|
|
|
|
return mData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IVisualization2d
|
|
|
|
|
public int[] GetPalette() {
|
2019-12-07 17:11:25 +00:00
|
|
|
|
int[] pal = new int[mNextColorIdx];
|
|
|
|
|
for (int i = 0; i < mNextColorIdx; i++) {
|
2019-11-28 01:12:26 +00:00
|
|
|
|
pal[i] = mPalette[i];
|
|
|
|
|
}
|
|
|
|
|
return pal;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 02:05:17 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a new color to the palette. If the color already exists, the call has no
|
|
|
|
|
/// effect.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="color">32-bit ARGB color value.</param>
|
2019-11-28 01:12:26 +00:00
|
|
|
|
public void AddColor(int color) {
|
2019-12-07 17:11:25 +00:00
|
|
|
|
if (mNextColorIdx == 256) {
|
2019-11-28 01:12:26 +00:00
|
|
|
|
Debug.WriteLine("Palette is full");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-07 17:11:25 +00:00
|
|
|
|
// I'm expecting palettes to only have a few colors, so O(n^2) is fine for now.
|
|
|
|
|
for (int i = 0; i < mNextColorIdx; i++) {
|
2019-12-01 01:52:33 +00:00
|
|
|
|
if (mPalette[i] == color) {
|
|
|
|
|
Debug.WriteLine("Color " + color.ToString("x6") +
|
|
|
|
|
" already exists in palette (" + i + ")");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-07 17:11:25 +00:00
|
|
|
|
mPalette[mNextColorIdx++] = color;
|
2019-11-28 01:12:26 +00:00
|
|
|
|
}
|
2019-12-01 01:52:33 +00:00
|
|
|
|
|
2019-12-05 02:05:17 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a new color to the palette. If the color already exists, the call has no
|
|
|
|
|
/// effect.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="a">Alpha value.</param>
|
|
|
|
|
/// <param name="r">Red value.</param>
|
|
|
|
|
/// <param name="g">Green value.</param>
|
|
|
|
|
/// <param name="b">Blue value.</param>
|
2019-12-01 01:52:33 +00:00
|
|
|
|
public void AddColor(byte a, byte r, byte g, byte b) {
|
|
|
|
|
AddColor(Util.MakeARGB(a, r, g, b));
|
|
|
|
|
}
|
2019-11-28 01:12:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|