Beispiel #1
0
        public virtual void SRGBImageTest()
        {
            ImageData img = ImageDataFactory.Create(sourceFolder + "sRGBImage.png");

            NUnit.Framework.Assert.AreEqual(ImageType.PNG, img.GetOriginalType());
            NUnit.Framework.Assert.AreEqual(50, img.GetWidth(), 0);
            NUnit.Framework.Assert.AreEqual(50, img.GetHeight(), 0);
            NUnit.Framework.Assert.AreEqual(96, img.GetDpiX());
            NUnit.Framework.Assert.AreEqual(96, img.GetDpiY());
            NUnit.Framework.Assert.AreEqual(2.2, ((PngImageData)img).GetGamma(), 0.0001f);
            PngChromaticities pngChromaticities = ((PngImageData)img).GetPngChromaticities();

            NUnit.Framework.Assert.AreEqual(0.3127f, pngChromaticities.GetXW(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.329f, pngChromaticities.GetYW(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.64f, pngChromaticities.GetXR(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.33f, pngChromaticities.GetYR(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.3f, pngChromaticities.GetXG(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.6f, pngChromaticities.GetYG(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.15f, pngChromaticities.GetXB(), 0.0001f);
            NUnit.Framework.Assert.AreEqual(0.06f, pngChromaticities.GetYB(), 0.0001f);
        }
        /// <summary>
        /// Gets an instance of an Image from a System.Drwaing.Image.
        /// </summary>
        /// <param name="image">the System.Drawing.Image to convert</param>
        /// <param name="color">
        /// if different from null the transparency
        /// pixels are replaced by this color
        /// </param>
        /// <param name="forceBW">if true the image is treated as black and white</param>
        /// <returns>an object of type ImgRaw</returns>
        public static ImageData GetImage(System.Drawing.Image image, Color?color, bool forceBW)
        {
            System.Drawing.Bitmap bm = (System.Drawing.Bitmap)image;
            int w   = bm.Width;
            int h   = bm.Height;
            int pxv = 0;

            if (forceBW)
            {
                int    byteWidth  = (w / 8) + ((w & 7) != 0 ? 1 : 0);
                byte[] pixelsByte = new byte[byteWidth * h];

                int index      = 0;
                int transColor = 1;
                if (color != null)
                {
                    transColor = (color.Value.R + color.Value.G + color.Value.B < 384) ? 0 : 1;
                }
                int[] transparency = null;
                int   cbyte        = 0x80;
                int   wMarker      = 0;
                int   currByte     = 0;
                if (color != null)
                {
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            int alpha = bm.GetPixel(i, j).A;
                            if (alpha < 250)
                            {
                                if (transColor == 1)
                                {
                                    currByte |= cbyte;
                                }
                            }
                            else
                            {
                                if ((bm.GetPixel(i, j).ToArgb() & 0x888) != 0)
                                {
                                    currByte |= cbyte;
                                }
                            }
                            cbyte >>= 1;
                            if (cbyte == 0 || wMarker + 1 >= w)
                            {
                                pixelsByte[index++] = (byte)currByte;
                                cbyte    = 0x80;
                                currByte = 0;
                            }
                            ++wMarker;
                            if (wMarker >= w)
                            {
                                wMarker = 0;
                            }
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            if (transparency == null)
                            {
                                int alpha = bm.GetPixel(i, j).A;
                                if (alpha == 0)
                                {
                                    transparency        = new int[2];
                                    transparency[0]     =
                                        transparency[1] = ((bm.GetPixel(i, j).ToArgb() & 0x888) != 0) ? 1 : 0;
                                }
                            }
                            if ((bm.GetPixel(i, j).ToArgb() & 0x888) != 0)
                            {
                                currByte |= cbyte;
                            }
                            cbyte >>= 1;
                            if (cbyte == 0 || wMarker + 1 >= w)
                            {
                                pixelsByte[index++] = (byte)currByte;
                                cbyte    = 0x80;
                                currByte = 0;
                            }
                            ++wMarker;
                            if (wMarker >= w)
                            {
                                wMarker = 0;
                            }
                        }
                    }
                }

                return(ImageDataFactory.Create(w, h, 1, 1, pixelsByte, transparency));
            }
            else
            {
                byte[] pixelsByte = new byte[w * h * 3];
                byte[] smask      = null;

                int index = 0;
                int red   = 255;
                int green = 255;
                int blue  = 255;
                if (color != null)
                {
                    red   = color.Value.R;
                    green = color.Value.G;
                    blue  = color.Value.B;
                }
                int[] transparency = null;
                if (color != null)
                {
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            int alpha = (bm.GetPixel(i, j).ToArgb() >> 24) & 0xff;
                            if (alpha < 250)
                            {
                                pixelsByte[index++] = (byte)red;
                                pixelsByte[index++] = (byte)green;
                                pixelsByte[index++] = (byte)blue;
                            }
                            else
                            {
                                pxv = bm.GetPixel(i, j).ToArgb();
                                pixelsByte[index++] = (byte)((pxv >> 16) & 0xff);
                                pixelsByte[index++] = (byte)((pxv >> 8) & 0xff);
                                pixelsByte[index++] = (byte)((pxv) & 0xff);
                            }
                        }
                    }
                }
                else
                {
                    int transparentPixel = 0;
                    smask = new byte[w * h];
                    bool shades   = false;
                    int  smaskPtr = 0;
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            pxv = bm.GetPixel(i, j).ToArgb();
                            byte alpha = smask[smaskPtr++] = (byte)((pxv >> 24) & 0xff);
                            /* bugfix by Chris Nokleberg */
                            if (!shades)
                            {
                                if (alpha != 0 && alpha != 255)
                                {
                                    shades = true;
                                }
                                else if (transparency == null)
                                {
                                    if (alpha == 0)
                                    {
                                        transparentPixel = pxv & 0xffffff;
                                        transparency     = new int[6];
                                        transparency[0]  = transparency[1] = (transparentPixel >> 16) & 0xff;
                                        transparency[2]  = transparency[3] = (transparentPixel >> 8) & 0xff;
                                        transparency[4]  = transparency[5] = transparentPixel & 0xff;
                                        // Added by Michael Klink
                                        // Check whether this value for transparent pixels
                                        // has already been used for a non-transparent one
                                        // before this position
                                        for (int prevPixelI = 0; prevPixelI <= i; prevPixelI++)
                                        {
                                            for (int prevPixelJ = 0;
                                                 prevPixelJ < (prevPixelI == i ? j : h);
                                                 prevPixelJ++)
                                            {
                                                int prevPxV = bm.GetPixel(prevPixelI, prevPixelJ).ToArgb();
                                                if ((prevPxV & 0xffffff) == transparentPixel)
                                                {
                                                    // found a prior use of the transparentPixel color
                                                    // and, therefore, cannot make use of this color
                                                    // for transparency; we could still use an image
                                                    // mask but for simplicity let's use a soft mask
                                                    // which already is implemented here
                                                    shades = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (((pxv & 0xffffff) != transparentPixel) && (alpha == 0))
                                {
                                    shades = true;
                                }
                                else if (((pxv & 0xffffff) == transparentPixel) && (alpha != 0))
                                {
                                    shades = true;
                                }
                            }
                            pixelsByte[index++] = (byte)((pxv >> 16) & 0xff);
                            pixelsByte[index++] = (byte)((pxv >> 8) & 0xff);
                            pixelsByte[index++] = (byte)(pxv & 0xff);
                        }
                    }
                    if (shades)
                    {
                        transparency = null;
                    }
                    else
                    {
                        smask = null;
                    }
                }
                ImageData img = ImageDataFactory.Create(w, h, 3, 8, pixelsByte, transparency);
                if (smask != null)
                {
                    ImageData sm = ImageDataFactory.Create(w, h, 1, 8, smask, null);
                    sm.MakeMask();
                    img.imageMask = sm;
                }
                return(img);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets an instance of an Image from a System.Drwaing.Image.
        /// </summary>
        /// <param name="image">the System.Drawing.Image to convert</param>
        /// <param name="color">
        /// if different from null the transparency
        /// pixels are replaced by this color
        /// </param>
        /// <param name="forceBW">if true the image is treated as black and white</param>
        /// <returns>an object of type ImgRaw</returns>
        public static ImageData GetImage(System.Drawing.Image image, Color?color, bool forceBW)
        {
            System.Drawing.Bitmap bm = (System.Drawing.Bitmap)image;
            int w   = bm.Width;
            int h   = bm.Height;
            int pxv = 0;

            if (forceBW)
            {
                int    byteWidth  = (w / 8) + ((w & 7) != 0 ? 1 : 0);
                byte[] pixelsByte = new byte[byteWidth * h];

                int index      = 0;
                int transColor = 1;
                if (color != null)
                {
                    transColor = (color.Value.R + color.Value.G + color.Value.B < 384) ? 0 : 1;
                }
                int[] transparency = null;
                int   cbyte        = 0x80;
                int   wMarker      = 0;
                int   currByte     = 0;
                if (color != null)
                {
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            int alpha = bm.GetPixel(i, j).A;
                            if (alpha < 250)
                            {
                                if (transColor == 1)
                                {
                                    currByte |= cbyte;
                                }
                            }
                            else
                            {
                                if ((bm.GetPixel(i, j).ToArgb() & 0x888) != 0)
                                {
                                    currByte |= cbyte;
                                }
                            }
                            cbyte >>= 1;
                            if (cbyte == 0 || wMarker + 1 >= w)
                            {
                                pixelsByte[index++] = (byte)currByte;
                                cbyte    = 0x80;
                                currByte = 0;
                            }
                            ++wMarker;
                            if (wMarker >= w)
                            {
                                wMarker = 0;
                            }
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            if (transparency == null)
                            {
                                int alpha = bm.GetPixel(i, j).A;
                                if (alpha == 0)
                                {
                                    transparency    = new int[2];
                                    transparency[0] = transparency[1] = ((bm.GetPixel(i, j).ToArgb() & 0x888) != 0) ? 1 : 0;
                                }
                            }
                            if ((bm.GetPixel(i, j).ToArgb() & 0x888) != 0)
                            {
                                currByte |= cbyte;
                            }
                            cbyte >>= 1;
                            if (cbyte == 0 || wMarker + 1 >= w)
                            {
                                pixelsByte[index++] = (byte)currByte;
                                cbyte    = 0x80;
                                currByte = 0;
                            }
                            ++wMarker;
                            if (wMarker >= w)
                            {
                                wMarker = 0;
                            }
                        }
                    }
                }

                return(ImageDataFactory.Create(w, h, 1, 1, pixelsByte, transparency));
            }
            else
            {
                byte[] pixelsByte = new byte[w * h * 3];
                byte[] smask      = null;

                int index = 0;
                int red   = 255;
                int green = 255;
                int blue  = 255;
                if (color != null)
                {
                    red   = color.Value.R;
                    green = color.Value.G;
                    blue  = color.Value.B;
                }
                int[] transparency = null;
                if (color != null)
                {
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            int alpha = (bm.GetPixel(i, j).ToArgb() >> 24) & 0xff;
                            if (alpha < 250)
                            {
                                pixelsByte[index++] = (byte)red;
                                pixelsByte[index++] = (byte)green;
                                pixelsByte[index++] = (byte)blue;
                            }
                            else
                            {
                                pxv = bm.GetPixel(i, j).ToArgb();
                                pixelsByte[index++] = (byte)((pxv >> 16) & 0xff);
                                pixelsByte[index++] = (byte)((pxv >> 8) & 0xff);
                                pixelsByte[index++] = (byte)((pxv) & 0xff);
                            }
                        }
                    }
                }
                else
                {
                    int transparentPixel = 0;
                    smask = new byte[w * h];
                    bool shades   = false;
                    int  smaskPtr = 0;
                    for (int j = 0; j < h; j++)
                    {
                        for (int i = 0; i < w; i++)
                        {
                            pxv = bm.GetPixel(i, j).ToArgb();
                            byte alpha = smask[smaskPtr++] = (byte)((pxv >> 24) & 0xff);
                            /* bugfix by Chris Nokleberg */
                            if (!shades)
                            {
                                if (alpha != 0 && alpha != 255)
                                {
                                    shades = true;
                                }
                                else if (transparency == null)
                                {
                                    if (alpha == 0)
                                    {
                                        transparentPixel = pxv & 0xffffff;
                                        transparency     = new int[6];
                                        transparency[0]  = transparency[1] = (transparentPixel >> 16) & 0xff;
                                        transparency[2]  = transparency[3] = (transparentPixel >> 8) & 0xff;
                                        transparency[4]  = transparency[5] = transparentPixel & 0xff;
                                    }
                                }
                                else if ((pxv & 0xffffff) != transparentPixel)
                                {
                                    shades = true;
                                }
                            }
                            pixelsByte[index++] = (byte)((pxv >> 16) & 0xff);
                            pixelsByte[index++] = (byte)((pxv >> 8) & 0xff);
                            pixelsByte[index++] = (byte)(pxv & 0xff);
                        }
                    }
                    if (shades)
                    {
                        transparency = null;
                    }
                    else
                    {
                        smask = null;
                    }
                }
                ImageData img = ImageDataFactory.Create(w, h, 3, 8, pixelsByte, transparency);
                if (smask != null)
                {
                    ImageData sm = ImageDataFactory.Create(w, h, 1, 8, smask, null);
                    sm.MakeMask();
                    img.imageMask = sm;
                }
                return(img);
            }
        }