// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace Spludlow.MetSat { public class CLM { // 2 bit colour source -> Format8bppIndexed PNG public static void ProcessGRIB(byte[] gribData, string targetFilename) { Spludlow.MetSat.GRIB grib = new GRIB(gribData); ProcessGRIB(grib, targetFilename); } public static void ProcessGRIB(Spludlow.MetSat.GRIB grib, string targetFilename) { System.Drawing.Imaging.PixelFormat pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed; int width = 3712; int height = 3712; Color[] pallete = new Color[256]; for (int index = 0; index < 256; ++index) pallete[index] = Color.Yellow; pallete[0] = Color.Black; pallete[1] = Color.White; pallete[2] = Color.Green; pallete[3] = Color.Blue; using (Bitmap bitmap = new Bitmap(width, height, pixelFormat)) { Spludlow.Drawing.Bitmaps.SetPalette(bitmap, pallete); byte[] bitmapData = Spludlow.Drawing.Bitmaps.ReadBitmapData(bitmap, pixelFormat); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int offset = y * width + x; int bitIndex = offset * 2; int byteIndex = bitIndex / 8; int bitOffset = bitIndex % 8; bitOffset /= 2; int mask = 3 << ((3 - bitOffset) * 2); int value = (int)grib.Data[byteIndex]; value &= mask; value = value >> ((3 - bitOffset) * 2); value = 3 - value; int targetOffset = (((width - 1) - y) * width) + ((width - 1) - x); // ? first width should be height ??? bitmapData[targetOffset] = (byte)value; } } Spludlow.Drawing.Bitmaps.WriteBitmapData(bitmap, bitmapData, pixelFormat); bitmap.Save(targetFilename, System.Drawing.Imaging.ImageFormat.Png); } } } }