/// <summary> /// erzeugt ein Bild aus dem Bitmap /// </summary> /// <param name="bm">Bitmap</param> /// <param name="cm"></param> /// <param name="bExtended">true für Bilder mit mehr 255 Farben</param> public PixMap(Bitmap bm, BitmapColorMode cm, bool bExtended = false) { Colormode = cm; bool bWithTransp, bWithAlpha; Color[] coltab = GraphicElement.GetBitmapColorInfo(bm, out bWithTransp, out bWithAlpha); CreateFromBitmap(bm, Colormode, bExtended, coltab, bWithTransp, bWithAlpha); }
/// <summary> /// erzeugt das Bild aus dem Stream mit vorgegebener Farbtabelle (es werden nur noch die Pixeldaten eingelesen) /// </summary> /// <param name="br"></param> /// <param name="col">Farbtabelle</param> /// <param name="iWidth"></param> /// <param name="iHeight"></param> public PixMap(uint iWidth, uint iHeight, Color[] col, BitmapColorMode cm, BinaryReaderWriter br = null) : this(iWidth, iHeight, col.Length, cm) { col.CopyTo(colColorTable, 0); if (br != null) { data.Read(br); } }
/// <summary> /// setzt die Bitmaps (falls nicht null) mit dem einfachstmöglichen BitmapColorMode /// </summary> /// <param name="bmday"></param> /// <param name="bmnight"></param> /// <param name="bOnlyGarminColors">Die jeweils nächstgelegene Garminfarbe verwenden?</param> public void SetBitmaps(Bitmap bmday, Bitmap bmnight, bool bOnlyGarminColors) { if (bmday == null) { throw new Exception("Wenigstens das Tag-Bitmap muß gesetzt werden."); } if (bmnight != null) { if (bmday.Width != bmnight.Width || bmday.Height != bmnight.Height) { throw new Exception("Beide Bitmaps müßen die gleiche Größe haben."); } } if ((bmday.Width > 255 || bmday.Height > 255) || (bmnight != null && (bmnight.Width > 255 || bmnight.Height > 255))) { throw new Exception("Das Bitmap darf höchstens 255x255 groß sein."); } BitmapColorMode cm = GetMinBitmapColorMode(GetMinBitmapColorMode(bmday), GetMinBitmapColorMode(bmnight)); ColormodeDay = cm; if (bmday != null) // sonst bleibt das alte bestehen { XBitmapDay = new PixMap(bmday, ColormodeDay); if (bOnlyGarminColors) { for (uint i = 0; i < XBitmapDay.Colors; i++) { XBitmapDay.SetNewColor(i, GraphicElement.GetNearestGarminColor(XBitmapDay.GetColor(i))); } } } if (bmnight != null) { XBitmapNight = new PixMap(bmnight, ColormodeDay); WithNightXpm = true; if (bOnlyGarminColors) { for (uint i = 0; i < XBitmapNight.Colors; i++) { XBitmapNight.SetNewColor(i, GraphicElement.GetNearestGarminColor(XBitmapNight.GetColor(i))); } } } else { XBitmapNight = null; WithNightXpm = false; } Width = XBitmapDay.Width; Height = XBitmapDay.Height; }
public void Read(BinaryReaderWriter br) { try { Options = br.ReadByte(); uint iWidth = br.ReadByte(); uint iHeight = br.ReadByte(); colsday = br.ReadByte(); ColormodeDay = (BitmapColorMode)br.ReadByte(); this.XBitmapDay = new PixMap(iWidth, iHeight, colsday, ColormodeDay, br); if (WithNightXpm) { colsnight = br.ReadByte(); ColormodeNight = (BitmapColorMode)br.ReadByte(); if (!NightXpmHasData) { Color[] col = BinaryColor.ReadColorTable(br, colsnight); XBitmapNight = new PixMap(XBitmapDay); XBitmapNight.SetNewColors(col); } else { XBitmapNight = new PixMap(Width, Height, colsnight, ColormodeNight, br); } } if (WithString) { Text = new MultiText(br); } if (WithExtendedOptions) { ExtOptions = br.ReadByte(); switch (FontColType) { case FontColours.Day: colFontColour[0] = BinaryColor.ReadColor(br); break; case FontColours.Night: colFontColour[1] = BinaryColor.ReadColor(br); break; case FontColours.DayAndNight: colFontColour = BinaryColor.ReadColorTable(br, 2); break; } } } catch (Exception ex) { throw new Exception(string.Format("Fehler beim Lesen des Punktes 0x{0:x} 0x{1:x}: {2}", Type, Subtype, ex.Message)); } }
private PixMap GetDummyXPixMap(BitmapColorMode bcm, bool b4Day, PixMap old) { if (old != null) { if (old.Colormode != bcm) { switch (old.Colormode) { case BitmapColorMode.POLY1TR: old.ChangeColorMode(BitmapColorMode.POLY2); old.InvertBits(); old.SetNewColors(new Color[] { old.GetColor(0), Color.White }); // 2. Farbe einfach Weiß break; case BitmapColorMode.POLY2: old.ChangeColorMode(BitmapColorMode.POLY1TR); old.InvertBits(); old.SetNewColors(new Color[] { old.GetColor(0) }); break; } } return(old); } PixMap pic = new PixMap(32, 32, 2, bcm); if (b4Day) { pic.SetNewColor(0, DayColor1); if (bcm == BitmapColorMode.POLY2) { pic.SetNewColor(1, DayColor2); } } else { pic.SetNewColor(0, NightColor1); if (bcm == BitmapColorMode.POLY2) { pic.SetNewColor(1, NightColor2); } } return(pic); }
/// <summary> /// setzt den Colormode neu (falls das entsprechend des BpP möglich ist) /// </summary> /// <param name="cm"></param> public void ChangeColorMode(BitmapColorMode cm) { switch (Colormode) { case BitmapColorMode.POI_SIMPLE: case BitmapColorMode.POI_TR: case BitmapColorMode.POI_ALPHA: if (BpP > 1 && (cm == BitmapColorMode.POLY1TR || cm == BitmapColorMode.POLY2)) { throw new Exception(string.Format("Der Colormode kann nicht von {0} in {1} geändert werden.", Colormode, cm)); } break; case BitmapColorMode.POLY1TR: case BitmapColorMode.POLY2: break; } Colormode = cm; }
/// <summary> /// liefert den min. möglichen Mode für 2 vorgegebene Modes /// </summary> /// <param name="cm1"></param> /// <param name="cm2"></param> /// <returns></returns> protected BitmapColorMode GetMinBitmapColorMode(BitmapColorMode cm1, BitmapColorMode cm2) { switch (cm2) { case BitmapColorMode.POI_SIMPLE: return(cm1); case BitmapColorMode.POI_TR: switch (cm1) { case BitmapColorMode.POI_SIMPLE: return(cm2); case BitmapColorMode.POI_TR: case BitmapColorMode.POI_ALPHA: return(cm1); } break; default: return(BitmapColorMode.POI_ALPHA); } return(BitmapColorMode.POI_ALPHA); }
/// <summary> /// liefert die Bits je Pixel in Abhängigkeit vom BitmapColorMode und der Farbanzahl (auch für 0 Farben) /// </summary> /// <param name="cm"></param> /// <param name="cols">Farbanzahl (ohne transparente Farbe bei POI_TR und POLY1TR)</param> /// <returns></returns> protected static uint BitsPerPixel4BitmapColorMode(BitmapColorMode cm, int cols) { // vgl. auch misch (http://ati.land.cz/) uint iBpp = 0; if (cols > 255) { throw new Exception("Zuviele Farben."); } switch (cm) { case BitmapColorMode.POI_SIMPLE: switch (cols) { case 0: iBpp = 24; break; // 16?, "Truecolor" case 1: iBpp = 1; break; case 2: case 3: iBpp = 2; break; default: if (cols <= 15) { iBpp = 4; } else { iBpp = 8; } break; } break; case BitmapColorMode.POI_TR: // Die transparente Farbe kommt noch dazu. //switch (cols) { // case 0: iBpp = 1; break; // "Truecolor" // case 1: // case 2: // case 3: iBpp = 2; break; // default: // if (cols <= 15) iBpp = 4; // else iBpp = 8; // break; //} switch (cols) { case 0: iBpp = 1; break; // "Truecolor" case 1: case 2: iBpp = 2; break; default: if (cols <= 14) { iBpp = 4; } else { iBpp = 8; } break; } break; case BitmapColorMode.POI_ALPHA: switch (cols) { case 0: iBpp = 32; break; // 16?, "Truecolor" case 1: iBpp = 1; break; case 2: case 3: iBpp = 2; break; default: if (cols <= 15) { iBpp = 4; } else { iBpp = 8; } break; } break; case BitmapColorMode.POLY1TR: if (cols > 2) { throw new Exception(string.Format("Zuviele Farben ({0}) für den BitmapColorMode {1}.", cols, cm)); } iBpp = 1; break; case BitmapColorMode.POLY2: if (cols > 2) { throw new Exception(string.Format("Zuviele Farben ({0}) für den BitmapColorMode {1}.", cols, cm)); } iBpp = 1; break; default: throw new Exception(string.Format("Unbekannter ColorMode für Bitmap ({0:x2}).", cm)); } return(iBpp); }
protected void CreateFromBitmap(Bitmap bm, BitmapColorMode cm, bool bExtended, Color[] coltab, bool bWithTransp, bool bWithAlpha) { if (Colormode == BitmapColorMode.unknown) { throw new Exception("Unsinniger BitmapColorMode."); } // diverse Fehler abweisen if (!bExtended && coltab.Length > 255) { throw new Exception("Es sind max. 255 Farben im Bild möglich."); } if (coltab.Length <= 255) { bExtended = false; } if (bWithAlpha && Colormode != BitmapColorMode.POI_ALPHA) { throw new Exception(string.Format("Der gewählte Farbmodus {0} ist für halbtransparente Farben nicht verwendbar.", Colormode)); } if (Colormode == BitmapColorMode.POLY2 && (coltab.Length > 2 || bWithTransp)) { throw new Exception(string.Format("Beim Farbmodus {0} sind max. 2 Farben im Bild möglich. Davon darf keine transparent sein.", Colormode)); } if (Colormode == BitmapColorMode.POLY1TR && ((!bWithTransp && coltab.Length > 1) || (bWithTransp && coltab.Length > 2))) { throw new Exception(string.Format("Beim Farbmodus {0} ist außer Transparenz nur 1 Farbe im Bild möglich.", Colormode)); } if (!bExtended) { // Farben in die Tabelle übernehmen switch (Colormode) { case BitmapColorMode.POLY1TR: colColorTable = new Color[1] { coltab[0] }; break; case BitmapColorMode.POI_TR: colColorTable = new Color[coltab.Length]; for (int i = 0; i < coltab.Length; i++) // die letzte Farbe (nicht in der Tabelle enthalten) ist immer transp. { colColorTable[i] = coltab[i]; } break; default: colColorTable = coltab; break; } // verwendete Farben "einsammeln" Dictionary <Color, int> Colors = new Dictionary <Color, int>(); // Farbindex setzen for (int i = 0; i < colColorTable.Length; i++) { Colors[colColorTable[i]] = i; } if (Colormode == BitmapColorMode.POLY2 && Colors.Count == 1) { Colors.Add(Color.White, Colors.Count); // es war nur 1 Farbe enthalten, deshalb zusätzlich eine Dummy-Farbe aufnehmen } int tr = 0; if (Colormode == BitmapColorMode.POLY1TR || Colormode == BitmapColorMode.POI_TR) { tr = 1; Colors[colColorTable[0]] = 1; Colors[TransparentColor] = 0; // transp. Farbe als 0 //Colors.Add(TransparentColor, Colors.Count); // als höchster Index die transp. Farbe } //data = new PixData(bm, BitsPerPixel4BitmapColorMode(Colormode, coltab.Length), Colors); data = new PixData(bm, BitsPerPixel4BitmapColorMode(Colormode, Colors.Count - tr), Colors); } else { colColorTable = new Color[0]; data = new PixData(bm, BitsPerPixel4BitmapColorMode(Colormode, 0)); } }
/// <summary> /// erzeugt ein Bild mit max. 256 Farben (auch 0 Farben); transparent ist die "Dummy"-Farbe; liest die Farbtabelle und die Daten ev. aus dem Stream /// </summary> /// <param name="iWidth"></param> /// <param name="iHeight"></param> /// <param name="iColors">Anzahl der einzulesenden Farben</param> /// <param name="cm"></param> /// <param name="br"></param> public PixMap(uint iWidth, uint iHeight, int iColors, BitmapColorMode cm, BinaryReaderWriter br = null) { data = new PixData(iWidth, iHeight, BitsPerPixel4BitmapColorMode(cm, iColors)); Colormode = cm; colColorTable = new Color[iColors]; for (int i = 0; i < iColors; i++) // Init. der Farbtabelle { colColorTable[i] = TransparentColor; } if (cm == BitmapColorMode.POLY1TR) // in diesem Spezialfall alle Pixel/Bits auf 1 setzen { data.SetAllBits(); } if (br != null) { if (iColors == 0) // keine Farbtabelle, d.h. Pixel sind direkt durch ihre Farben definiert { colColorTable = new Color[0]; switch (Colormode) { case BitmapColorMode.POI_SIMPLE: data = new PixData(iWidth, iHeight, BpP, br); break; case BitmapColorMode.POI_TR: /* Keine Ahnung warum. Eigentlich wären überhaupt keine Daten nötig. * Mit dem 1 Bit könnten 2 Farben gemeint sein: * 0 --> transparent * 1 --> ??? * In der Praxis scheint aber immer 0 darin zu stehen. */ data = new PixData(iWidth, iHeight, BpP, br); //for(int i=0;i<rawimgdata.Length;i++) // if (rawimgdata[i] != 0) { // Debug.WriteLine("Bitmap mit iColors==0 und NICHT alle Daten=0"); // break; // } break; case BitmapColorMode.POI_ALPHA: data = new PixData(iWidth, iHeight, BpP, br); break; case BitmapColorMode.POLY1TR: case BitmapColorMode.POLY2: throw new Exception(string.Format("Für den ColorMode {0} muß eine Farbtabelle mit eingelesen werden.", Colormode)); default: throw new Exception(string.Format("Unbekannter ColorMode für Bitmap ({0:x2}).", Colormode)); } } else { switch (Colormode) { case BitmapColorMode.POI_SIMPLE: case BitmapColorMode.POI_TR: colColorTable = BinaryColor.ReadColorTable(br, iColors, false); break; case BitmapColorMode.POI_ALPHA: colColorTable = BinaryColor.ReadColorTable(br, iColors, true); break; case BitmapColorMode.POLY1TR: if (iColors != 1) { throw new Exception(string.Format("Für den ColorMode {0} kann nur 1 Farbe (nicht {1}) eingelesen werden.", Colormode, iColors)); } colColorTable = BinaryColor.ReadColorTable(br, 1, false); break; case BitmapColorMode.POLY2: if (iColors != 2) { throw new Exception(string.Format("Für den ColorMode {0} können nur 2 Farben (nicht {1}) eingelesen werden.", Colormode, iColors)); } colColorTable = BinaryColor.ReadColorTable(br, 2, false); break; default: throw new Exception(string.Format("Unbekannter ColorMode für Bitmap ({0:x2}).", Colormode)); } data = new PixData(iWidth, iHeight, BpP, br); } } }
public POI(uint iTyp, uint iSubtyp, BitmapColorMode cm) : this(iTyp, iSubtyp) { ColormodeDay = cm; }