コード例 #1
0
        private static bool RecolorImage(IcImage img, float fHue)
        {
            const int cbHeader = NativeMethods.BITMAPINFOHEADERSize;

            if (Marshal.SizeOf(typeof(BITMAPINFOHEADER)) != cbHeader)
            {
                Debug.Assert(false);
                return(false);
            }

            byte[] pb = img.Data;
            if (pb.Length < cbHeader)
            {
                Debug.Assert(false); return(false);
            }

            BITMAPINFOHEADER h = MemUtil.BytesToStruct <BITMAPINFOHEADER>(pb, 0);

            if (h.biSize != cbHeader)
            {
                Debug.Assert(h.biSize == 0x474E5089);                 // PNG
                return(false);
            }
            Debug.Assert(h.biPlanes == 1);
            Debug.Assert((h.biClrUsed == 0) && (h.biClrImportant == 0));
            if (h.biCompression != 0)
            {
                Debug.Assert(false); return(false);
            }

            int w = h.biWidth;

            if (w <= 0)
            {
                Debug.Assert(false); return(false);
            }
            int hAbs = Math.Abs(h.biHeight);

            if ((hAbs != w) && (hAbs != (w << 1)))
            {
                Debug.Assert(false); return(false);
            }

            ushort uBpp = h.biBitCount;

            if ((uBpp == 1) || (uBpp == 4) || (uBpp == 8))
            {
                RecolorPalette(pb, cbHeader, 1 << uBpp, fHue);
            }
            else if ((uBpp == 24) || (uBpp == 32))
            {
                if (!RecolorPixelData(pb, cbHeader, w, w, uBpp, fHue))
                {
                    return(false);
                }
            }
            else
            {
                Debug.Assert(false); return(false);
            }

            Debug.Assert(((w < 256) && (img.Entry.bWidth == w)) ||
                         ((w >= 256) && (img.Entry.bWidth == 0)));
            Debug.Assert(((w < 256) && (img.Entry.bHeight == w)) ||
                         ((w >= 256) && (img.Entry.bHeight == 0)));
            Debug.Assert(((uBpp < 8) && (img.Entry.bColorCount == (1 << uBpp))) ||
                         ((uBpp >= 8) && (img.Entry.bColorCount == 0)));
            Debug.Assert(img.Entry.bReserved == 0);
            Debug.Assert(img.Entry.wPlanes == h.biPlanes);
            Debug.Assert(img.Entry.wBitCount == uBpp);

            return(true);
        }
コード例 #2
0
ファイル: IconColorizer.cs プロジェクト: Gallimathias/KeePass
        private static List <IcImage> GetImages(Icon ico)
        {
            List <IcImage> l = new List <IcImage>();

            byte[] pb;
            using (MemoryStream ms = new MemoryStream())
            {
                ico.Save(ms);
                pb = ms.ToArray();
            }

            const int cbDir = NativeMethods.ICONDIRSize;

            if (Marshal.SizeOf(typeof(ICONDIR)) != cbDir)
            {
                Debug.Assert(false); return(l);
            }
            const int cbEntry = NativeMethods.ICONDIRENTRYSize;

            if (Marshal.SizeOf(typeof(ICONDIRENTRY)) != cbEntry)
            {
                Debug.Assert(false); return(l);
            }

            int iPos = 0;

            ICONDIR d = MemUtil.BytesToStruct <ICONDIR>(pb, iPos);

            iPos += cbDir;

            if (d.idReserved != 0)
            {
                Debug.Assert(false); return(l);
            }
            if (d.idType != 1)
            {
                Debug.Assert(false); return(l);
            }

            for (uint u = 0; u < d.idCount; ++u)
            {
                IcImage img = new IcImage();

                img.Entry = MemUtil.BytesToStruct <ICONDIRENTRY>(pb, iPos);
                iPos     += cbEntry;

                img.Data = MemUtil.Mid(pb, (int)img.Entry.dwImageOffset,
                                       (int)img.Entry.dwBytesInRes);

                l.Add(img);
            }

#if DEBUG
            int cbSum = cbDir + (l.Count * cbEntry);
            foreach (IcImage img in l)
            {
                cbSum += img.Data.Length;
            }
            Debug.Assert(cbSum == pb.Length);             // No extra data
#endif

            return(l);
        }