コード例 #1
0
        private static void Decode1BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            int position = 0;
            int bitsLeft = 0;
            int value    = 0;

            for (int y = 0; y < imageType.Height; y++)
            {
                for (int x = 0; x < imageType.Width; x++)
                {
                    if (bitsLeft == 0)
                    {
                        value    = 0xff & imageData[position++];
                        bitsLeft = 8;
                    }

                    uint argb;
                    argb    = (value & 0x80u) != 0 ? 0xff000000u : 0xffffffffu;
                    value <<= 1;
                    bitsLeft--;

                    SetPixel(image, x, y, argb);
                }
            }
        }
コード例 #2
0
        private static void Decode8BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            for (int y = 0; y < imageType.Height; y++)
            {
                for (int x = 0; x < imageType.Width; x++)
                {
                    int index = 0xff & imageData[y * imageType.Width + x];

                    SetPixel(image, x, y, PALETTE_8BPP[index]);
                }
            }
        }
コード例 #3
0
        private static void Decode32BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            for (int y = 0; y < imageType.Height; y++)
            {
                for (int x = 0; x < imageType.Width; x++)
                {
                    uint argb = (0xff000000u /* the "alpha" is ignored */ |
                                 ((0xffu & imageData[4 * (y * imageType.Width + x) + 1]) << 16) |
                                 ((0xffu & imageData[4 * (y * imageType.Width + x) + 2]) << 8) |
                                 (0xffu & imageData[4 * (y * imageType.Width + x) + 3]));

                    SetPixel(image, x, y, argb);
                }
            }
        }
コード例 #4
0
        private static void Decode32BPPImageARGB(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            for (int y = 0; y < imageType.Height; y++)
            {
                for (int x = 0; x < imageType.Width; x++)
                {
                    uint argb = (((0xffu & imageData[4 * (y * imageType.Width + x) + 0]) << 24) |
                                 ((0xffu & imageData[4 * (y * imageType.Width + x) + 1]) << 16) |
                                 ((0xffu & imageData[4 * (y * imageType.Width + x) + 2]) << 8) |
                                 (0xffu & imageData[4 * (y * imageType.Width + x) + 3]));

                    SetPixel(image, x, y, argb);
                }
            }
        }
コード例 #5
0
        public static IcnsType FindType(int width, int height, int bpp, TypeDetails details)
        {
            for (int i = 0; i < ALL_TYPES.Length; i++)
            {
                IcnsType type = ALL_TYPES[i];
                if (type.width == width &&
                    type.height == height &&
                    type.bitsPerPixel == bpp &&
                    type.details == details)
                {
                    return(type);
                }
            }

            return(null);
        }
コード例 #6
0
 private static IcnsImage TryDecodingPng(IcnsImageParser.IcnsElement element, IcnsType imageType)
 {
     if (element.data.Length < PNG_SIGNATURE.Length)
     {
         return(null); // definitely not a valid png
     }
     for (int i = 0; i < PNG_SIGNATURE.Length; i++)
     {
         if (element.data[i] != PNG_SIGNATURE[i])
         {
             return(null); // not a png
         }
     }
     using (MemoryStream ms = new MemoryStream(element.data))
         return(new IcnsImage((Bitmap)Image.FromStream(ms), imageType)); // cast is valid, for PNG it will be Bitmap
 }
コード例 #7
0
        public static void WriteImage(Bitmap src, Stream stream)
        {
            IcnsType imageType = IcnsType.FindType(src.Width, src.Height, 32, IcnsType.TypeDetails.None);

            if (imageType == null)
            {
                throw new NotSupportedException($"Invalid/unsupported source: {src.Width}x{src.Height}");
            }

            Write4Bytes(stream, ICNS_MAGIC);
            Write4Bytes(stream, 4 + 4 + 4 + 4 + 4 * imageType.Width * imageType.Height + 4 + 4 + imageType.Width * imageType.Height);

            Write4Bytes(stream, imageType.Type);
            Write4Bytes(stream, 4 + 4 + 4 * imageType.Width * imageType.Height);

            BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            // the image
            for (int y = 0; y < src.Height; y++)
            {
                for (int x = 0; x < src.Width; x++)
                {
                    uint argb = IcnsDecoder.GetPixel(bitmapData, x, y);
                    stream.WriteByte(0);
                    stream.WriteByte((byte)((argb & 0x00ff0000) >> 16));
                    stream.WriteByte((byte)((argb & 0x0000ff00) >> 8));
                    stream.WriteByte((byte)((argb & 0x000000ff) >> 0));
                }
            }

            // mask
            IcnsType maskType = IcnsType.FindType(src.Width, src.Height, 8, IcnsType.TypeDetails.Mask);

            Write4Bytes(stream, maskType.Type);
            Write4Bytes(stream, 4 + 4 + imageType.Width * imageType.Width);

            for (int y = 0; y < src.Height; y++)
            {
                for (int x = 0; x < src.Width; x++)
                {
                    uint argb = IcnsDecoder.GetPixel(bitmapData, x, y);
                    stream.WriteByte((byte)((argb & 0xff000000) >> 24));
                }
            }
        }
コード例 #8
0
        private static void Decode4BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            int  i       = 0;
            bool visited = false;

            for (int y = 0; y < imageType.Height; y++)
            {
                for (int x = 0; x < imageType.Width; x++)
                {
                    int index;
                    if (!visited)
                    {
                        index = 0xf & (imageData[i] >> 4);
                    }
                    else
                    {
                        index = 0xf & imageData[i++];
                    }
                    visited = !visited;

                    SetPixel(image, x, y, PALETE_4BPP[index]);
                }
            }
        }
コード例 #9
0
 public IcnsImage(Bitmap bitmap, IcnsType type)
 {
     this.bitmap = bitmap;
     this.type   = type;
 }
コード例 #10
0
ファイル: IcnsImage.cs プロジェクト: BrokenEvent/CsIcnsReader
 public IcnsImage(Bitmap bitmap, IcnsType type)
 {
     this.bitmap = bitmap;
       this.type = type;
 }
コード例 #11
0
        private static IcnsImage DecodeImage(IcnsImageParser.IcnsElement imageElement, IcnsImageParser.IcnsElement[] icnsElements)
        {
            IcnsType imageType = IcnsType.FindType(imageElement.type, IcnsType.TypeDetails.Mask);

            if (imageType == null)
            {
                return(null);
            }

            IcnsType maskType = null;

            IcnsImageParser.IcnsElement maskElement = null;

            if (imageType.Details == IcnsType.TypeDetails.HasMask)
            {
                maskType    = imageType;
                maskElement = imageElement;
            }
            else if (imageType.Details == IcnsType.TypeDetails.None)
            {
                maskType = IcnsType.FindType(imageType.Width, imageType.Height, 8, IcnsType.TypeDetails.Mask);
                if (maskType != null)
                {
                    maskElement = FindElement(icnsElements, maskType.Type);
                }

                if (maskElement == null)
                {
                    maskType = IcnsType.FindType(imageType.Width, imageType.Height, 1, IcnsType.TypeDetails.Mask);
                    if (maskType != null)
                    {
                        maskElement = FindElement(icnsElements, maskType.Type);
                    }
                }
            }

            if (imageType.Details == IcnsType.TypeDetails.Compressed ||
                imageType.Details == IcnsType.TypeDetails.Retina)
            {
                IcnsImage result = TryDecodingPng(imageElement, imageType);
                if (result != null)
                {
                    return(result); // png
                }
                if (LoadJ2kImage == null)
                {
                    return(null); // couldn't be loaded
                }
                return(LoadJ2kImage(imageElement, imageType));
            }

            int expectedSize = (imageType.Width * imageType.Height * imageType.BitsPerPixel + 7) / 8;

            byte[] imageData;

            if (imageElement.data.Length < expectedSize)
            {
                if (imageType.BitsPerPixel == 32)
                {
                    imageData = Rle24Compression.Decompress(imageType.Width, imageType.Height, imageElement.data);
                }
                else
                {
                    throw new Exception("Short image data but not a 32 bit compressed type");
                }
            }
            else
            {
                imageData = imageElement.data;
            }

            Bitmap     image      = new Bitmap(imageType.Width, imageType.Height, PixelFormat.Format32bppArgb);
            BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            switch (imageType.BitsPerPixel)
            {
            case 1:
                Decode1BPPImage(imageType, imageData, bitmapData);
                break;

            case 4:
                Decode4BPPImage(imageType, imageData, bitmapData);
                break;

            case 8:
                Decode8BPPImage(imageType, imageData, bitmapData);
                break;

            case 32:
                if (imageType.Details == IcnsType.TypeDetails.ARGB)
                {
                    Decode32BPPImageARGB(imageType, imageData, bitmapData);
                }
                else
                {
                    Decode32BPPImage(imageType, imageData, bitmapData);
                }
                break;

            default:
                image.UnlockBits(bitmapData);
                image.Dispose();
                throw new NotSupportedException("Unsupported bit depth " + imageType.BitsPerPixel);
            }

            if (maskElement != null)
            {
                switch (maskType.BitsPerPixel)
                {
                case 1:
                    Apply1BPPMask(maskElement.data, bitmapData);
                    break;

                case 8:
                    Apply8BPPMask(maskElement.data, bitmapData);
                    break;

                default:
                    image.UnlockBits(bitmapData);
                    image.Dispose();
                    throw new NotSupportedException("Unsupport mask bit depth " + maskType.BitsPerPixel);
                }
            }

            image.UnlockBits(bitmapData);
            return(new IcnsImage(image, imageType));
        }
コード例 #12
0
        private static void Decode1BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            int position = 0;
              int bitsLeft = 0;
              int value = 0;
              for (int y = 0; y < imageType.Height; y++)
            for (int x = 0; x < imageType.Width; x++)
            {
              if (bitsLeft == 0)
              {
            value = 0xff & imageData[position++];
            bitsLeft = 8;
              }

              uint argb;
              argb = (value & 0x80u) != 0 ? 0xff000000u : 0xffffffffu;
              value <<= 1;
              bitsLeft--;

              SetPixel(image, x, y, argb);
            }
        }
コード例 #13
0
        private static void Decode32BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            for (int y = 0; y < imageType.Height; y++)
            for (int x = 0; x < imageType.Width; x++)
            {
              uint argb = (0xff000000u /* the "alpha" is ignored */|
                       ((0xffu & imageData[4 * (y * imageType.Width + x) + 1]) << 16) |
                       ((0xffu & imageData[4 * (y * imageType.Width + x) + 2]) << 8) |
                       (0xffu & imageData[4 * (y * imageType.Width + x) + 3]));

              SetPixel(image, x, y, argb);
            }
        }
コード例 #14
0
        private static void Decode4BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            int i = 0;
              bool visited = false;
              for (int y = 0; y < imageType.Height; y++)
            for (int x = 0; x < imageType.Width; x++)
            {
              int index;
              if (!visited)
            index = 0xf & (imageData[i] >> 4);
              else
            index = 0xf & imageData[i++];
              visited = !visited;

              SetPixel(image, x, y, PALETE_4BPP[index]);
            }
        }
コード例 #15
0
        private static void Decode8BPPImage(IcnsType imageType, byte[] imageData, BitmapData image)
        {
            for (int y = 0; y < imageType.Height; y++)
            for (int x = 0; x < imageType.Width; x++)
            {
              int index = 0xff & imageData[y * imageType.Width + x];

              SetPixel(image, x, y, PALETTE_8BPP[index]);
            }
        }
コード例 #16
0
ファイル: IcnsType.cs プロジェクト: BrokenEvent/CsIcnsReader
        public static IcnsType Find8BPPMaskType(IcnsType imageType)
        {
            for (int i = 0; i < allMaskTypes.Length; i++)
            if (allMaskTypes[i].BitsPerPixel == 8 &&
            allMaskTypes[i].Width == imageType.Width &&
            allMaskTypes[i].Height == imageType.Height)
              return allMaskTypes[i];

              return null;
        }
コード例 #17
0
        private static IcnsImage LoadWithFreeImage(IcnsImageParser.IcnsElement element, IcnsType imageType)
        {
            using (MemoryStream ms = new MemoryStream(element.data))
            {
                FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_JP2;
                FIBITMAP          fib    = FreeImage.LoadFromStream(ms, ref format);
                Bitmap            bmp    = FreeImage.GetBitmap(fib);
                FreeImage.Unload(fib);

                return(new IcnsImage(bmp, imageType));
            }
        }