|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Windows.Media; |
| 4 | +using System.Windows.Media.Imaging; |
| 5 | + |
| 6 | +namespace PPMLib.WPF |
| 7 | +{ |
| 8 | + public static class PPMRenderer |
| 9 | + { |
| 10 | + public static readonly List<Color> ThumbnailPalette = new List<Color> |
| 11 | + { |
| 12 | + Color.FromRgb(0xFF,0xFF,0xFF), Color.FromRgb(0x52,0x52,0x52), Color.FromRgb(0xFF,0xFF,0xFF), Color.FromRgb(0x9C,0x9C,0x9C), |
| 13 | + Color.FromRgb(0xFF,0x48,0x44), Color.FromRgb(0xC8,0x51,0x4F), Color.FromRgb(0xFF,0xAD,0xAC), Color.FromRgb(0x00,0xFF,0x00), |
| 14 | + Color.FromRgb(0x48,0x40,0xFF), Color.FromRgb(0x51,0x4F,0xB8), Color.FromRgb(0xAD,0xAB,0xFF), Color.FromRgb(0x00,0xFF,0x00), |
| 15 | + Color.FromRgb(0xB6,0x57,0xB7), Color.FromRgb(0x00,0xFF,0x00), Color.FromRgb(0x00,0xFF,0x00), Color.FromRgb(0x00,0xFF,0x00) |
| 16 | + }; |
| 17 | + public static WriteableBitmap GetThumbnailBitmap(byte[] buffer) |
| 18 | + { |
| 19 | + if (buffer.Length != 1536) |
| 20 | + { |
| 21 | + throw new ArgumentException("Wrong thumbnail buffer size"); |
| 22 | + } |
| 23 | + |
| 24 | + byte[] bytes = new byte[32 * 48]; |
| 25 | + |
| 26 | + int offset = 0; |
| 27 | + for (int ty = 0; ty < 48; ty += 8) |
| 28 | + { |
| 29 | + for (int tx = 0; tx < 32; tx += 4) |
| 30 | + { |
| 31 | + for (int l = 0; l < 8; l++) |
| 32 | + { |
| 33 | + int line = (ty + l) << 5; |
| 34 | + for (int px = 0; px < 4; px++) |
| 35 | + { |
| 36 | + // Need to reverse nibbles : |
| 37 | + bytes[line + tx + px] = (byte)(((buffer[offset] & 0xF) << 4) | ((buffer[offset] & 0xF0) >> 4)); |
| 38 | + offset += 1; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + var palette = new BitmapPalette(ThumbnailPalette); |
| 45 | + // Directly set bitmap's 4-bit palette instead of using 32-bit colors |
| 46 | + var bmp = new WriteableBitmap(64, 48, 96, 96, PixelFormats.Indexed4, palette); |
| 47 | + bmp.WritePixels(new System.Windows.Int32Rect(0, 0, 64, 48), bytes, 32, 0); |
| 48 | + return bmp; |
| 49 | + } |
| 50 | + |
| 51 | + public static readonly List<Color> FramePalette = new List<Color> |
| 52 | + { |
| 53 | + Color.FromRgb(0x00,0x00,0x00), Color.FromRgb(0xFF,0xFF,0xFF), Color.FromRgb(0xFF,0x00,0x00), Color.FromRgb(0x00,0x00,0xFF) |
| 54 | + }; |
| 55 | + |
| 56 | + private static Color GetLayerColor(PenColor pc,PaperColor paper) |
| 57 | + { |
| 58 | + if (pc == PenColor.Inverted) return FramePalette[1 - (int)paper]; |
| 59 | + return FramePalette[(int)pc]; |
| 60 | + } |
| 61 | + |
| 62 | + public static WriteableBitmap GetFrameBitmap(PPMFrame frame) |
| 63 | + { |
| 64 | + var palette = new BitmapPalette(new List<Color> |
| 65 | + { |
| 66 | + FramePalette[(int)frame.PaperColor], |
| 67 | + GetLayerColor(frame.Layer1.PenColor,frame.PaperColor), |
| 68 | + GetLayerColor(frame.Layer2.PenColor,frame.PaperColor), |
| 69 | + }); |
| 70 | + var bmp = new WriteableBitmap(256, 192, 96, 96, PixelFormats.Indexed2, palette); |
| 71 | + |
| 72 | + int stride = 64; |
| 73 | + byte[] pixels = new byte[64 * 192]; |
| 74 | + for (int x = 0; x < 256; x++) |
| 75 | + { |
| 76 | + for (int y = 0; y < 192; y++) |
| 77 | + { |
| 78 | + if (frame.Layer1[y, x]) |
| 79 | + { |
| 80 | + int b = 256 * y + x; |
| 81 | + int p = 3 - b % 4; |
| 82 | + b /= 4; |
| 83 | + pixels[b] &= (byte)(~(0b11 << (2 * p))); |
| 84 | + pixels[b] |= (byte)(0b10 << (2 * p)); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + for (int x = 0; x < 256; x++) |
| 89 | + { |
| 90 | + for (int y = 0; y < 192; y++) |
| 91 | + { |
| 92 | + if (frame.Layer2[y, x]) |
| 93 | + { |
| 94 | + int b = 256 * y + x; |
| 95 | + int p = 3 - b % 4; |
| 96 | + b /= 4; |
| 97 | + pixels[b] &= (byte)(~(0b11 << (2 * p))); |
| 98 | + pixels[b] |= (byte)(0b01 << (2 * p)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + bmp.WritePixels(new System.Windows.Int32Rect(0, 0, 256, 192), pixels, stride, 0); |
| 103 | + return bmp; |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments