static Bitmap ConvertCompressedOcadIcon(byte[] bytes) { byte[] compressed = new byte[248]; byte[] uncompressed = new byte[22 * 22]; Array.Copy(bytes, 16, compressed, 0, 248); LZWCompression compressor = new LZWCompression(); compressor.Expand(compressed, uncompressed); return ConvertOcad9Icon(uncompressed); }
// Convert an image into an OCAD icon bits. byte[] ImageToOcadIcon(Image image, int version) { Bitmap bitmap = new Bitmap(24, 24, PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); g.DrawImage(image, new Rectangle(0, 0, 24, 24)); g.Dispose(); if (version <= 7) { // ocad 6, 7 -- 4 bit color byte[] array = new byte[264]; for (int row = 1; row <= 22; ++row) for (int col = 1; col <= 22; col += 2) { int colorIndex1 = NearestOcadColor(bitmap.GetPixel(col, row), false); int colorIndex2 = NearestOcadColor(bitmap.GetPixel(col + 1, row), false); array[(22 - row) * 12 + (col - 1) / 2] = (byte) ((colorIndex1 << 4) + colorIndex2); } return array; } else { // ocad 8/9 -- 8 bit color byte[] uncompressed = new byte[22 * 22]; for (int row = 1; row <= 22; ++row) for (int col = 1; col <= 22; ++col) { int colorIndex = NearestOcadColor(bitmap.GetPixel(col, row), true); uncompressed[(22-row) * 22 + (col - 1)] = colorIndex > 128 ? (byte)0 : (byte) colorIndex; } if (version == 8) { // OCAD 8 -- compressed. byte[] array = new byte[264]; byte[] compressed = new byte[264 - 16]; for (int i = 0; i < compressed.Length; ++i) compressed[i] = 0xFF; LZWCompression compressor = new LZWCompression(); compressor.Compress(uncompressed, compressed); Array.Copy(compressed, 0, array, 16, compressed.Length); for (int i = 0; i < 16; ++i) array[i] = 0xFF; return array; } else { // OCAD 9 -- uncompressed return uncompressed; } } }