1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-07-30 15:29:01 +00:00
6502bench/PluginCommon/VisBitmap8.cs
Andy McFadden 365864ccdf More progress on visualization
Implemented Apple II hi-res bitmap conversion.  Supports B&W and
color.  Uses essentially the same algorithm as CiderPress.

Experimented with displaying non-text items in ListView.  I assumed
it would work, since it's the sort of thing WPF is designed to do,
but it's always wise to approach with caution.  Visualization Sets
now show a 64x64 button as a placeholder for the eventual thumbnail.

Some things were being flaky, which turned out to be because I
wasn't Prepare()ing the plugins before using them from Edit
Visualization.  To make this a deterministic failure I added an
Unprepare() call that tells the plugin that we're all done.

NOTE: this breaks all existing plugins.
2019-11-30 18:02:03 -08:00

100 lines
3.1 KiB
C#

/*
* 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.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace PluginCommon {
/// <summary>
/// Bitmap with 8-bit palette indices, for use with plugin visualizers.
/// </summary>
[Serializable]
public class VisBitmap8 : IVisualization2d {
public const int MAX_DIMENSION = 4096;
public int Width { get; private set; }
public int Height { get; private set; }
private byte[] mData;
private int[] mPalette;
private int mNextColor;
public VisBitmap8(int width, int height) {
Debug.Assert(width > 0 && width <= MAX_DIMENSION);
Debug.Assert(height > 0 && height <= MAX_DIMENSION);
Width = width;
Height = height;
mData = new byte[width * height];
mPalette = new int[256];
mNextColor = 0;
}
// IVisualization2d
public int GetPixel(int x, int y) {
byte pix = mData[x + y * Width];
return mPalette[pix];
}
public void SetPixelIndex(int x, int y, byte colorIndex) {
if (x < 0 || x >= Width || y < 0 || y >= Height) {
throw new ArgumentException("Bad x/y: " + x + "," + y + " (width=" + Width +
" height=" + Height + ")");
}
if (colorIndex < 0 || colorIndex >= mNextColor) {
throw new ArgumentException("Bad color: " + colorIndex + " (nextCol=" +
mNextColor + ")");
}
mData[x + y * Width] = colorIndex;
}
// IVisualization2d
public byte[] GetPixels() {
return mData;
}
// IVisualization2d
public int[] GetPalette() {
int[] pal = new int[mNextColor];
for (int i = 0; i < mNextColor; i++) {
pal[i] = mPalette[i];
}
return pal;
}
public void AddColor(int color) {
if (mNextColor == 256) {
Debug.WriteLine("Palette is full");
return;
}
for (int i = 0; i < mNextColor; i++) {
if (mPalette[i] == color) {
Debug.WriteLine("Color " + color.ToString("x6") +
" already exists in palette (" + i + ")");
return;
}
}
mPalette[mNextColor++] = color;
}
public void AddColor(byte a, byte r, byte g, byte b) {
AddColor(Util.MakeARGB(a, r, g, b));
}
}
}