首先谈谈 GIF 文件和您的原始解决方案。
让它运行得非常快很简单,但是为了好玩,我只是笑了 - 现在我知道你说的质量下降是什么意思了..!!
让我解释一下:GIF 文件中有几个选项,但这里重要的是它是否抖动。
我已经提到它们只有 256 种颜色;为了看起来不错,至少从远处看,普通的 GIF 文件使用了一个技巧:它们抖动像素块来显示混合颜色!这很好用,但它完全禁止使用调色板进行颜色旋转..
当然可以关闭抖动,但结果不仅看起来很粗糙而且像素化;使用合适的调色板可以进行旋转,但结果充其量是平庸的。我已经在函数palRotate 中附加了执行此操作的代码。
这给我们留下了我最初的建议:忘记 GIF 并使用 ARGB 颜色旋转!这也允许您使用已计算的所有 Ks 范围。但您需要紧凑的代码以使其快速运行。
这是一个完整的测试平台,使用 LockBits 加载数据并使用它们进行全彩色循环。
您需要在表单中添加 PictureBox、Button 和 Timer。
请注意,您需要保持数据的大小等于PictureBox 的大小。
我已经包含了一个 500x500 的测试数据文件。
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
//..
// the data array
int[,] Ks;
// the offset for cycling
int koffset = 0;
// a list of colors
List<Color> colors = new List<Color>();
public void paintKs()
{
if (Ks == null) return;
Size s1 = pb_image.ClientSize;
pb_image.Image = new Bitmap(s1.Width, s1.Height);
Bitmap bmp = new Bitmap(pb_image.Image);
PixelFormat fmt1 = bmp.PixelFormat;
byte bpp1 = 4;
Rectangle rect = new Rectangle(Point.Empty, s1);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, fmt1);
int size1 = bmpData.Stride * bmpData.Height;
byte[] data = new byte[size1];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, data, 0, size1);
for (int y = 0; y < s1.Height; y++)
{
for (int x = 0; x < s1.Width; x++)
{
int index = y * bmpData.Stride + x * bpp1;
Color c = colors[(Ks[x, y] + koffset) % (colors.Count)];
if (Ks[x, y] == 0) c = Color.Black;
data[index + 0] = c.B;
data[index + 1] = c.G;
data[index + 2] = c.R;
data[index + 3] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
bmp.UnlockBits(bmpData);
pb_image.Image = bmp;
}
void saveKs(string dataFile)
{
using (BinaryWriter writer = new BinaryWriter(File.Open(dataFile, FileMode.Create)))
{
for (int y = 0; y < Ks.GetLength(0); y++)
for (int x = 0; x < Ks.GetLength(1); x++)
writer.Write((Int16)Ks[x, y]);
}
}
void loadKs(string dataFile)
{
int w = pb_image.ClientSize.Width;
if (Ks == null) Ks = new int[w, w];
using (BinaryReader reader = new BinaryReader(File.Open(dataFile, FileMode.Open)))
{
for (int y = 0; y < Ks.GetLength(0); y++)
for (int x = 0; x < Ks.GetLength(1); x++)
Ks[x, y] = reader.ReadInt16();
}
}
private void Test_Click(object sender, EventArgs e)
{
loadKs("fractalData021.dat");
for (int i = 0; i < 256; i++)
{
// a very simple and rather awful palette!
for (int i = 0; i < 256; i++)
colors.Add(Color.FromArgb(255, i, i, 255 - i));
for (int i = 0; i < 100; i++)
colors.Add(Color.FromArgb(255, i + 100, 255 -i, 155 - i));
for (int i = 0; i < 100; i++)
colors.Add(Color.FromArgb(255, i + i+ 50, 255 - i - i, 155 - i/2));
}
paintKs();
timer1.Intervall = 33; // 30 fps
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
koffset++;
if (koffset >= colors.Count) koffset = 0;;
paintKs();
}
这里有一些带有测试数据的文件;测试文件的大小为 500x500 像素:
http://www.file-upload.net/download-9796723/fractalData021.dat.html
http://www.file-upload.net/download-9796722/fractalData021.jpg.html
http://www.file-upload.net/download-9796721/fractalData021.txt.html
更新:
这是在非抖动 GIF 文件上执行 plaette 旋转的代码。
void palRotate()
{
Bitmap bmp = (Bitmap)pb_image.Image;
var pal = bmp.Palette;
for (int i = 0; i < 256; i++) pal.Entries[(i + koffset) % 256] = colors[i];
bmp.Palette = pal;
pb_image.Image = bmp;
}
为了准备,这些调用会将原始调色板颜色提取到颜色列表中:
pb_image.Image = new Bitmap("d:\\fff.gif");
Bitmap bmp = (Bitmap)pb_image.Image;
var pal = bmp.Palette;
for (int i = 0; i < 256; i++) colors.Add(pal.Entries[i]);
为了让它看起来一点也不糟糕,托盘必须有某种顺序;但即便如此,像素化图像看起来也很可悲..
它的调用方式与 Timer 中的其他轮换代码类似,后者推进 koffset 变量。