コード例 #1
0
        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));
            }
        }
コード例 #2
0
ファイル: PixData.cs プロジェクト: FSofTlpz/GarminCore
 /// <summary>
 /// erzeugt eine Kopie
 /// </summary>
 /// <param name="pix"></param>
 public PixData(PixData pix) :
     this(pix.Width, pix.Height, pix.BpP)
 {
     pix.rawimgdata.CopyTo(rawimgdata, 0);
 }
コード例 #3
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);
                }
            }
        }