private byte[] pixel(out int bufferByteOffset)
        {
            int x = m_x;
            int y = m_y;

            unchecked
            {
                if ((uint)x >= (uint)m_SourceImage.Width)
                {
                    if (x < 0)
                    {
                        x = 0;
                    }
                    else
                    {
                        x = (int)m_SourceImage.Width - 1;
                    }
                }

                if ((uint)y >= (uint)m_SourceImage.Height)
                {
                    if (y < 0)
                    {
                        y = 0;
                    }
                    else
                    {
                        y = (int)m_SourceImage.Height - 1;
                    }
                }
            }

            bufferByteOffset = m_SourceImage.GetBufferOffsetXY(x, y);
            return(m_SourceImage.GetBuffer());
        }
Exemple #2
0
        //--------------------------------------------------------------------
        public byte pixel(int x, int y)
        {
            int bufferIndex = m_rbuf.GetBufferOffsetXY(x, y);

            byte[] buffer = m_rbuf.GetBuffer();
            return(buffer[bufferIndex]);
        }
Exemple #3
0
        //--------------------------------------------------------------------
        public byte pixel(int x, int y)
        {
            unchecked
            {
                if ((uint)x < (uint)m_rbuf.Width &&
                    (uint)y < (uint)m_rbuf.Height)
                {
                    int    bufferIndex = m_rbuf.GetBufferOffsetXY(x, y);
                    byte[] buffer      = m_rbuf.GetBuffer();
                    return(buffer[bufferIndex]);
                }
            }

            return(0);
        }
Exemple #4
0
        public static bool SaveImageData(string filename, IImageByte sourceImage)
        {
            try
            {
                using (var tgaSave = new MemoryStream())
                {
                    var source         = sourceImage.GetBuffer();
                    var invertedBuffer = new byte[source.Length];
                    int index          = 0;
                    for (int y = sourceImage.Height - 1; y >= 0; y--)
                    {
                        var line = sourceImage.GetBufferOffsetY(y);
                        for (int x = 0; x < sourceImage.Width; x++)
                        {
                            var pix = x * 4;
                            invertedBuffer[index++] = source[line + pix + 2];
                            invertedBuffer[index++] = source[line + pix + 1];
                            invertedBuffer[index++] = source[line + pix + 0];
                            invertedBuffer[index++] = source[line + pix + 3];
                        }
                    }

                    var image2 = SixLabors.ImageSharp.Image.LoadPixelData <Rgba32>(invertedBuffer,
                                                                                   sourceImage.Width,
                                                                                   sourceImage.Height);
                    image2.Save(filename);
                }

                return(true);
            }
            catch { }

            return(false);
        }
 private void attach(IImageByte pixf)
 {
     m_SourceImage = pixf;
     m_Buffer      = m_SourceImage.GetBuffer();
     m_Width       = m_SourceImage.Width;
     m_DistanceBetweenPixelsInclusive = m_SourceImage.GetBytesBetweenPixelsInclusive();
 }
Exemple #6
0
        private static Image <Rgba32> ImageBufferToImage32(IImageByte sourceImage)
        {
            var source         = sourceImage.GetBuffer();
            var invertedBuffer = new byte[source.Length];
            int index          = 0;

            for (int y = sourceImage.Height - 1; y >= 0; y--)
            {
                var line = sourceImage.GetBufferOffsetY(y);
                for (int x = 0; x < sourceImage.Width; x++)
                {
                    var pix = x * 4;
                    invertedBuffer[index++] = source[line + pix + 2];
                    invertedBuffer[index++] = source[line + pix + 1];
                    invertedBuffer[index++] = source[line + pix + 0];
                    invertedBuffer[index++] = source[line + pix + 3];
                }
            }

            var image2 = SixLabors.ImageSharp.Image.LoadPixelData <Rgba32>(invertedBuffer,
                                                                           sourceImage.Width,
                                                                           sourceImage.Height);

            return(image2);
        }
		private void attach(IImageByte pixf)
		{
			m_SourceImage = pixf;
			m_Buffer = m_SourceImage.GetBuffer();
			m_Width = m_SourceImage.Width;
			m_DistanceBetweenPixelsInclusive = m_SourceImage.GetBytesBetweenPixelsInclusive();
		}
Exemple #8
0
        public void Attach(IImageByte sourceImage, IRecieveBlenderByte recieveBlender, int distanceBetweenPixelsInclusive, int bufferOffset, int bitsPerPixel)
        {
            SetDimmensionAndFormat(sourceImage.Width, sourceImage.Height, sourceImage.StrideInBytes(), bitsPerPixel, distanceBetweenPixelsInclusive, false);
            int offset = sourceImage.GetBufferOffsetXY(0, 0);

            byte[] buffer = sourceImage.GetBuffer();
            SetBuffer(buffer, offset + bufferOffset);
            SetRecieveBlender(recieveBlender);
        }
Exemple #9
0
        public ImageBuffer(IImageByte sourceImage, IRecieveBlenderByte recieveBlender)
        {
            SetDimmensionAndFormat(sourceImage.Width, sourceImage.Height, sourceImage.StrideInBytes(), sourceImage.BitDepth, sourceImage.GetBytesBetweenPixelsInclusive(), true);
            int offset = sourceImage.GetBufferOffsetXY(0, 0);

            byte[] buffer    = sourceImage.GetBuffer();
            byte[] newBuffer = new byte[buffer.Length];
            agg_basics.memcpy(newBuffer, offset, buffer, offset, buffer.Length - offset);
            SetBuffer(newBuffer, offset);
            SetRecieveBlender(recieveBlender);
        }
Exemple #10
0
        public ImageBuffer(IImageByte sourceImageToCopy, IRecieveBlenderByte blender, int distanceBetweenPixelsInclusive, int bufferOffset, int bitsPerPixel)
        {
            SetDimmensionAndFormat(sourceImageToCopy.Width, sourceImageToCopy.Height, sourceImageToCopy.StrideInBytes(), bitsPerPixel, distanceBetweenPixelsInclusive, true);
            int offset = sourceImageToCopy.GetBufferOffsetXY(0, 0);

            byte[] buffer    = sourceImageToCopy.GetBuffer();
            byte[] newBuffer = new byte[buffer.Length];
            agg_basics.memcpy(newBuffer, offset, buffer, offset, buffer.Length - offset);
            SetBuffer(newBuffer, offset + bufferOffset);
            SetRecieveBlender(blender);
        }
Exemple #11
0
        /// <summary>
        /// This will create a new ImageBuffer that references the same memory as the image that you took the sub image from.
        /// It will modify the original main image when you draw to it.
        /// </summary>
        /// <param name="imageContainingSubImage"></param>
        /// <param name="subImageBounds"></param>
        /// <returns></returns>
        public static ImageBuffer NewSubImageReference(IImageByte imageContainingSubImage, RectangleDouble subImageBounds)
        {
            ImageBuffer subImage = new ImageBuffer();

            if (subImageBounds.Left < 0 || subImageBounds.Bottom < 0 || subImageBounds.Right > imageContainingSubImage.Width || subImageBounds.Top > imageContainingSubImage.Height ||
                subImageBounds.Left >= subImageBounds.Right || subImageBounds.Bottom >= subImageBounds.Top)
            {
                throw new ArgumentException("The subImageBounds must be on the image and valid.");
            }
            int left   = Math.Max(0, (int)Math.Floor(subImageBounds.Left));
            int bottom = Math.Max(0, (int)Math.Floor(subImageBounds.Bottom));
            int width  = Math.Min(imageContainingSubImage.Width - left, (int)subImageBounds.Width);
            int height = Math.Min(imageContainingSubImage.Height - bottom, (int)subImageBounds.Height);
            int bufferOffsetToFirstPixel = imageContainingSubImage.GetBufferOffsetXY(left, bottom);

            subImage.AttachBuffer(imageContainingSubImage.GetBuffer(), bufferOffsetToFirstPixel, width, height, imageContainingSubImage.StrideInBytes(), imageContainingSubImage.BitDepth, imageContainingSubImage.GetBytesBetweenPixelsInclusive());
            subImage.SetRecieveBlender(imageContainingSubImage.GetRecieveBlender());

            return(subImage);
        }
Exemple #12
0
        public bool Attach(IImageByte sourceImage, int x1, int y1, int x2, int y2)
        {
            m_ByteBuffer = null;
            DettachBuffer();

            if (x1 > x2 || y1 > y2)
            {
                throw new Exception("You need to have your x1 and y1 be the lower left corner of your sub image.");
            }
            RectangleInt boundsRect = new RectangleInt(x1, y1, x2, y2);

            if (boundsRect.clip(new RectangleInt(0, 0, (int)sourceImage.Width - 1, (int)sourceImage.Height - 1)))
            {
                SetDimmensionAndFormat(boundsRect.Width, boundsRect.Height, sourceImage.StrideInBytes(), sourceImage.BitDepth, sourceImage.GetBytesBetweenPixelsInclusive(), false);
                int    bufferOffset = sourceImage.GetBufferOffsetXY(boundsRect.Left, boundsRect.Bottom);
                byte[] buffer       = sourceImage.GetBuffer();
                SetBuffer(buffer, bufferOffset);
                return(true);
            }

            return(false);
        }
Exemple #13
0
        protected void CopyFromNoClipping(IImageByte sourceImage, RectangleInt clippedSourceImageRect, int destXOffset, int destYOffset)
        {
            if (GetBytesBetweenPixelsInclusive() != BitDepth / 8 ||
                sourceImage.GetBytesBetweenPixelsInclusive() != sourceImage.BitDepth / 8)
            {
                throw new Exception("WIP we only support packed pixel formats at this time.");
            }

            if (BitDepth == sourceImage.BitDepth)
            {
                int lengthInBytes = clippedSourceImageRect.Width * GetBytesBetweenPixelsInclusive();

                int    sourceOffset = sourceImage.GetBufferOffsetXY(clippedSourceImageRect.Left, clippedSourceImageRect.Bottom);
                byte[] sourceBuffer = sourceImage.GetBuffer();
                int    destOffset;
                byte[] destBuffer = GetPixelPointerXY(clippedSourceImageRect.Left + destXOffset, clippedSourceImageRect.Bottom + destYOffset, out destOffset);

                for (int i = 0; i < clippedSourceImageRect.Height; i++)
                {
                    agg_basics.memmove(destBuffer, destOffset, sourceBuffer, sourceOffset, lengthInBytes);
                    sourceOffset += sourceImage.StrideInBytes();
                    destOffset   += StrideInBytes();
                }
            }
            else
            {
                bool haveConversion = true;
                switch (sourceImage.BitDepth)
                {
                case 24:
                    switch (BitDepth)
                    {
                    case 32:
                    {
                        int numPixelsToCopy = clippedSourceImageRect.Width;
                        for (int i = clippedSourceImageRect.Bottom; i < clippedSourceImageRect.Top; i++)
                        {
                            int    sourceOffset = sourceImage.GetBufferOffsetXY(clippedSourceImageRect.Left, clippedSourceImageRect.Bottom + i);
                            byte[] sourceBuffer = sourceImage.GetBuffer();
                            int    destOffset;
                            byte[] destBuffer = GetPixelPointerXY(
                                clippedSourceImageRect.Left + destXOffset,
                                clippedSourceImageRect.Bottom + i + destYOffset,
                                out destOffset);
                            for (int x = 0; x < numPixelsToCopy; x++)
                            {
                                destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
                                destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
                                destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
                                destBuffer[destOffset++] = 255;
                            }
                        }
                    }
                    break;

                    default:
                        haveConversion = false;
                        break;
                    }
                    break;

                default:
                    haveConversion = false;
                    break;
                }

                if (!haveConversion)
                {
                    throw new NotImplementedException("You need to write the " + sourceImage.BitDepth.ToString() + " to " + BitDepth.ToString() + " conversion");
                }
            }
        }
Exemple #14
0
		public ImageBuffer(IImageByte sourceImage, IRecieveBlenderByte recieveBlender)
		{
			SetDimmensionAndFormat(sourceImage.Width, sourceImage.Height, sourceImage.StrideInBytes(), sourceImage.BitDepth, sourceImage.GetBytesBetweenPixelsInclusive(), true);
			int offset = sourceImage.GetBufferOffsetXY(0, 0);
			byte[] buffer = sourceImage.GetBuffer();
			byte[] newBuffer = new byte[buffer.Length];
			agg_basics.memcpy(newBuffer, offset, buffer, offset, buffer.Length - offset);
			SetBuffer(newBuffer, offset);
			SetRecieveBlender(recieveBlender);
		}
        public bool SaveImageData(string filename, IImageByte sourceImage)
        {
            // Get a lock index base on the hash of the file name
            int lockerIndex = Math.Abs(filename.GetHashCode()) % Lockers.Length;             // mod the hash code by the count to get an index

            // lock on the index that this file name selects
            lock (Lockers[lockerIndex])
            {
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }

                ImageFormat format = ImageFormat.Jpeg;
                if (filename.ToLower().EndsWith(".png"))
                {
                    format = ImageFormat.Png;
                }
                else if (!filename.ToLower().EndsWith(".jpg") && !filename.ToLower().EndsWith(".jpeg"))
                {
                    filename += ".jpg";
                }

                if (!File.Exists(filename))
                {
                    if (sourceImage.BitDepth == 32)
                    {
                        try
                        {
                            using (var bitmapToSave = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb))
                            {
                                BitmapData bitmapData = bitmapToSave.LockBits(new Rectangle(0, 0, bitmapToSave.Width, bitmapToSave.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmapToSave.PixelFormat);
                                int        destIndex  = 0;
                                unsafe
                                {
                                    byte[] sourceBuffer    = sourceImage.GetBuffer();
                                    byte * pDestBuffer     = (byte *)bitmapData.Scan0;
                                    int    scanlinePadding = bitmapData.Stride - bitmapData.Width * 4;
                                    for (int y = 0; y < sourceImage.Height; y++)
                                    {
                                        int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                                        for (int x = 0; x < sourceImage.Width; x++)
                                        {
                                            pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                            pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                            pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                            pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                        }

                                        destIndex += scanlinePadding;
                                    }
                                }

                                bitmapToSave.UnlockBits(bitmapData);
                                bitmapToSave.Save(filename, format);
                            }

                            return(true);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error saving file: " + ex.Message);
                            return(false);
                        }
                    }
                    else if (sourceImage.BitDepth == 8 && format == ImageFormat.Png)
                    {
                        using (Bitmap bitmapToSave = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format8bppIndexed))
                        {
                            ColorPalette palette = bitmapToSave.Palette;
                            for (int i = 0; i < palette.Entries.Length; i++)
                            {
                                palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
                            }
                            bitmapToSave.Palette = palette;
                            BitmapData bitmapData = bitmapToSave.LockBits(new Rectangle(0, 0, bitmapToSave.Width, bitmapToSave.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmapToSave.PixelFormat);
                            int        destIndex  = 0;
                            unsafe
                            {
                                byte[] sourceBuffer = sourceImage.GetBuffer();
                                byte * pDestBuffer  = (byte *)bitmapData.Scan0;
                                for (int y = 0; y < sourceImage.Height; y++)
                                {
                                    int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                                    for (int x = 0; x < sourceImage.Width; x++)
                                    {
                                        pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                    }
                                }
                            }
                            bitmapToSave.Save(filename, format);
                            bitmapToSave.UnlockBits(bitmapData);

                            return(true);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }

                return(false);
            }
        }
Exemple #16
0
        // Create
        //--------------------------------------------------------------------
        public void create(IImageByte src)
        {
            // we are going to create a dilated image for filtering
            // we add m_dilation pixels to every side of the image and then copy the image in the x
            // direction into each end so that we can sample into this image to get filtering on x repeating
            // if the original image look like this
            //
            // 123456
            //
            // the new image would look like this
            //
            // 0000000000
            // 0000000000
            // 5612345612
            // 0000000000
            // 0000000000

            m_height          = (int)agg_basics.uceil(src.Height);
            m_width           = (int)agg_basics.uceil(src.Width);
            m_width_hr        = (int)agg_basics.uround(src.Width * LineAABasics.line_subpixel_scale);
            m_half_height_hr  = (int)agg_basics.uround(src.Height * LineAABasics.line_subpixel_scale / 2);
            m_offset_y_hr     = m_dilation_hr + m_half_height_hr - LineAABasics.line_subpixel_scale / 2;
            m_half_height_hr += LineAABasics.line_subpixel_scale / 2;

            int bufferWidth    = m_width + m_dilation * 2;
            int bufferHeight   = m_height + m_dilation * 2;
            int bytesPerPixel  = src.BitDepth / 8;
            int NewSizeInBytes = bufferWidth * bufferHeight * bytesPerPixel;

            if (m_DataSizeInBytes < NewSizeInBytes)
            {
                m_DataSizeInBytes = NewSizeInBytes;
                m_data            = new byte[m_DataSizeInBytes];
            }

            m_buf.AttachBuffer(m_data, 0, bufferWidth, bufferHeight, bufferWidth * bytesPerPixel, src.BitDepth, bytesPerPixel);
            byte[] destBuffer   = m_buf.GetBuffer();
            byte[] sourceBuffer = src.GetBuffer();

            // copy the image into the middle of the dest
            for (int y = 0; y < m_height; y++)
            {
                for (int x = 0; x < m_width; x++)
                {
                    int sourceOffset = src.GetBufferOffsetXY(x, y);
                    int destOffset   = m_buf.GetBufferOffsetXY(m_dilation, y + m_dilation);
                    for (int channel = 0; channel < bytesPerPixel; channel++)
                    {
                        destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
                    }
                }
            }

            // copy the first two pixels form the end into the beginning and from the beginning into the end
            for (int y = 0; y < m_height; y++)
            {
                int s1Offset = src.GetBufferOffsetXY(0, y);
                int d1Offset = m_buf.GetBufferOffsetXY(m_dilation + m_width, y);

                int s2Offset = src.GetBufferOffsetXY(m_width - m_dilation, y);
                int d2Offset = m_buf.GetBufferOffsetXY(0, y);

                for (int x = 0; x < m_dilation; x++)
                {
                    for (int channel = 0; channel < bytesPerPixel; channel++)
                    {
                        destBuffer[d1Offset++] = sourceBuffer[s1Offset++];
                        destBuffer[d2Offset++] = sourceBuffer[s2Offset++];
                    }
                }
            }
        }
        public override bool SaveImageData(String filename, IImageByte sourceImage)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            ImageFormat format = ImageFormat.Jpeg;

            if (filename.ToLower().EndsWith(".png"))
            {
                format = ImageFormat.Png;
            }
            else if (!filename.ToLower().EndsWith(".jpg") && !filename.ToLower().EndsWith(".jpeg"))
            {
                filename += ".jpg";
            }

            if (!System.IO.File.Exists(filename))
            {
                if (sourceImage.BitDepth == 32)
                {
                    Bitmap     bitmapToSave = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
                    BitmapData bitmapData   = bitmapToSave.LockBits(new Rectangle(0, 0, bitmapToSave.Width, bitmapToSave.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmapToSave.PixelFormat);
                    int        destIndex    = 0;
                    unsafe
                    {
                        byte[] sourceBuffer    = sourceImage.GetBuffer();
                        byte * pDestBuffer     = (byte *)bitmapData.Scan0;
                        int    scanlinePadding = bitmapData.Stride - bitmapData.Width * 4;
                        for (int y = 0; y < sourceImage.Height; y++)
                        {
                            int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                            for (int x = 0; x < sourceImage.Width; x++)
                            {
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                            }
                            destIndex += scanlinePadding;
                        }
                    }
                    bitmapToSave.UnlockBits(bitmapData);
                    bitmapToSave.Save(filename, format);

                    return(true);
                }
                else if (sourceImage.BitDepth == 8 && format == ImageFormat.Png)
                {
                    Bitmap       bitmapToSave = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format8bppIndexed);
                    ColorPalette palette      = bitmapToSave.Palette;
                    for (int i = 0; i < palette.Entries.Length; i++)
                    {
                        palette.Entries[i] = Color.FromArgb(i, i, i);
                    }
                    bitmapToSave.Palette = palette;
                    BitmapData bitmapData = bitmapToSave.LockBits(new Rectangle(0, 0, bitmapToSave.Width, bitmapToSave.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmapToSave.PixelFormat);
                    int        destIndex  = 0;
                    unsafe
                    {
                        byte[] sourceBuffer = sourceImage.GetBuffer();
                        byte * pDestBuffer  = (byte *)bitmapData.Scan0;
                        for (int y = 0; y < sourceImage.Height; y++)
                        {
                            int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                            for (int x = 0; x < sourceImage.Width; x++)
                            {
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                            }
                        }
                    }
                    bitmapToSave.Save(filename, format);
                    bitmapToSave.UnlockBits(bitmapData);
                    return(true);
                }
                else
                {
                    throw new System.NotImplementedException();
                }
            }

            return(false);
        }
Exemple #18
0
		public void Attach(IImageByte sourceImage, IRecieveBlenderByte recieveBlender, int distanceBetweenPixelsInclusive, int bufferOffset, int bitsPerPixel)
		{
			SetDimmensionAndFormat(sourceImage.Width, sourceImage.Height, sourceImage.StrideInBytes(), bitsPerPixel, distanceBetweenPixelsInclusive, false);
			int offset = sourceImage.GetBufferOffsetXY(0, 0);
			byte[] buffer = sourceImage.GetBuffer();
			SetBuffer(buffer, offset + bufferOffset);
			SetRecieveBlender(recieveBlender);
		}
Exemple #19
0
		/// <summary>
		/// This will create a new ImageBuffer that references the same memory as the image that you took the sub image from.
		/// It will modify the original main image when you draw to it.
		/// </summary>
		/// <param name="imageContainingSubImage"></param>
		/// <param name="subImageBounds"></param>
		/// <returns></returns>
		public static ImageBuffer NewSubImageReference(IImageByte imageContainingSubImage, RectangleDouble subImageBounds)
		{
			ImageBuffer subImage = new ImageBuffer();
			if (subImageBounds.Left < 0 || subImageBounds.Bottom < 0 || subImageBounds.Right > imageContainingSubImage.Width || subImageBounds.Top > imageContainingSubImage.Height
				|| subImageBounds.Left >= subImageBounds.Right || subImageBounds.Bottom >= subImageBounds.Top)
			{
				throw new ArgumentException("The subImageBounds must be on the image and valid.");
			}
			int left = Math.Max(0, (int)Math.Floor(subImageBounds.Left));
			int bottom = Math.Max(0, (int)Math.Floor(subImageBounds.Bottom));
			int width = Math.Min(imageContainingSubImage.Width - left, (int)subImageBounds.Width);
			int height = Math.Min(imageContainingSubImage.Height - bottom, (int)subImageBounds.Height);
			int bufferOffsetToFirstPixel = imageContainingSubImage.GetBufferOffsetXY(left, bottom);
			subImage.AttachBuffer(imageContainingSubImage.GetBuffer(), bufferOffsetToFirstPixel, width, height, imageContainingSubImage.StrideInBytes(), imageContainingSubImage.BitDepth, imageContainingSubImage.GetBytesBetweenPixelsInclusive());
			subImage.SetRecieveBlender(imageContainingSubImage.GetRecieveBlender());

			return subImage;
		}
Exemple #20
0
		public ImageBuffer(IImageByte sourceImageToCopy, IRecieveBlenderByte blender, int distanceBetweenPixelsInclusive, int bufferOffset, int bitsPerPixel)
		{
			SetDimmensionAndFormat(sourceImageToCopy.Width, sourceImageToCopy.Height, sourceImageToCopy.StrideInBytes(), bitsPerPixel, distanceBetweenPixelsInclusive, true);
			int offset = sourceImageToCopy.GetBufferOffsetXY(0, 0);
			byte[] buffer = sourceImageToCopy.GetBuffer();
			byte[] newBuffer = new byte[buffer.Length];
			agg_basics.memcpy(newBuffer, offset, buffer, offset, buffer.Length - offset);
			SetBuffer(newBuffer, offset + bufferOffset);
			SetRecieveBlender(blender);
		}
        public override bool SaveImageData(String filename, IImageByte sourceImage)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            ImageFormat format = ImageFormat.Jpeg;
            if (filename.ToLower().EndsWith(".png"))
            {
                format = ImageFormat.Png;
            }
            else if (!filename.ToLower().EndsWith(".jpg") && !filename.ToLower().EndsWith(".jpeg"))
            {
                filename += ".jpg";
            }

            if (!System.IO.File.Exists(filename))
            {
                if (sourceImage.BitDepth == 32)
                {
                    Bitmap bitmapToSave = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
                    BitmapData bitmapData = bitmapToSave.LockBits(new Rectangle(0, 0, bitmapToSave.Width, bitmapToSave.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmapToSave.PixelFormat);
                    int destIndex = 0;
                    unsafe
                    {
                        byte[] sourceBuffer = sourceImage.GetBuffer();
                        byte* pDestBuffer = (byte*)bitmapData.Scan0;
                        int scanlinePadding = bitmapData.Stride - bitmapData.Width * 4;
                        for (int y = 0; y < sourceImage.Height; y++)
                        {
                            int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                            for (int x = 0; x < sourceImage.Width; x++)
                            {
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                            }
                            destIndex += scanlinePadding;
                        }
                    }
                    bitmapToSave.Save(filename, format);
                    bitmapToSave.UnlockBits(bitmapData);
                    return true;
                }
                else if (sourceImage.BitDepth == 8 && format == ImageFormat.Png)
                {
                    Bitmap bitmapToSave = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format8bppIndexed);
                    ColorPalette palette = bitmapToSave.Palette;
                    for (int i = 0; i < palette.Entries.Length; i++)
                    {
                        palette.Entries[i] = Color.FromArgb(i, i, i);
                    }
                    bitmapToSave.Palette = palette;
                    BitmapData bitmapData = bitmapToSave.LockBits(new Rectangle(0, 0, bitmapToSave.Width, bitmapToSave.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmapToSave.PixelFormat);
                    int destIndex = 0;
                    unsafe
                    {
                        byte[] sourceBuffer = sourceImage.GetBuffer();
                        byte* pDestBuffer = (byte*)bitmapData.Scan0;
                        for (int y = 0; y < sourceImage.Height; y++)
                        {
                            int sourceIndex = sourceImage.GetBufferOffsetXY(0, sourceImage.Height - 1 - y);
                            for (int x = 0; x < sourceImage.Width; x++)
                            {
                                pDestBuffer[destIndex++] = sourceBuffer[sourceIndex++];
                            }
                        }
                    }
                    bitmapToSave.Save(filename, format);
                    bitmapToSave.UnlockBits(bitmapData);
                    return true;
                }
                else
                {
                    throw new System.NotImplementedException();
                }
            }

            return false;
        }
Exemple #22
0
 public byte[] GetBuffer()
 {
     return(linkedImage.GetBuffer());
 }
Exemple #23
0
		public bool Attach(IImageByte sourceImage, int x1, int y1, int x2, int y2)
		{
			m_ByteBuffer = null;
			DettachBuffer();

			if (x1 > x2 || y1 > y2)
			{
				throw new Exception("You need to have your x1 and y1 be the lower left corner of your sub image.");
			}
			RectangleInt boundsRect = new RectangleInt(x1, y1, x2, y2);
			if (boundsRect.clip(new RectangleInt(0, 0, (int)sourceImage.Width - 1, (int)sourceImage.Height - 1)))
			{
				SetDimmensionAndFormat(boundsRect.Width, boundsRect.Height, sourceImage.StrideInBytes(), sourceImage.BitDepth, sourceImage.GetBytesBetweenPixelsInclusive(), false);
				int bufferOffset = sourceImage.GetBufferOffsetXY(boundsRect.Left, boundsRect.Bottom);
				byte[] buffer = sourceImage.GetBuffer();
				SetBuffer(buffer, bufferOffset);
				return true;
			}

			return false;
		}
		private void BlendInFilterPixel(int[] accumulatedColor, int back_r, int back_g, int back_b, int back_a, IImageByte SourceRenderingBuffer, int maxx, int maxy, int x_lr, int y_lr, int weight)
		{
			byte[] fg_ptr;
			unchecked
			{
				if ((uint)x_lr <= (uint)maxx && (uint)y_lr <= (uint)maxy)
				{
					int bufferIndex = SourceRenderingBuffer.GetBufferOffsetXY(x_lr, y_lr);
					fg_ptr = SourceRenderingBuffer.GetBuffer();

					accumulatedColor[0] += weight * fg_ptr[bufferIndex + ImageBuffer.OrderR];
					accumulatedColor[1] += weight * fg_ptr[bufferIndex + ImageBuffer.OrderG];
					accumulatedColor[2] += weight * fg_ptr[bufferIndex + ImageBuffer.OrderB];
					accumulatedColor[3] += weight * fg_ptr[bufferIndex + ImageBuffer.OrderA];
				}
				else
				{
					accumulatedColor[0] += back_r * weight;
					accumulatedColor[1] += back_g * weight;
					accumulatedColor[2] += back_b * weight;
					accumulatedColor[3] += back_a * weight;
				}
			}
		}
Exemple #25
0
		protected void CopyFromNoClipping(IImageByte sourceImage, RectangleInt clippedSourceImageRect, int destXOffset, int destYOffset)
		{
			if (GetBytesBetweenPixelsInclusive() != BitDepth / 8
				|| sourceImage.GetBytesBetweenPixelsInclusive() != sourceImage.BitDepth / 8)
			{
				throw new Exception("WIP we only support packed pixel formats at this time.");
			}

			if (BitDepth == sourceImage.BitDepth)
			{
				int lengthInBytes = clippedSourceImageRect.Width * GetBytesBetweenPixelsInclusive();

				int sourceOffset = sourceImage.GetBufferOffsetXY(clippedSourceImageRect.Left, clippedSourceImageRect.Bottom);
				byte[] sourceBuffer = sourceImage.GetBuffer();
				int destOffset;
				byte[] destBuffer = GetPixelPointerXY(clippedSourceImageRect.Left + destXOffset, clippedSourceImageRect.Bottom + destYOffset, out destOffset);

				for (int i = 0; i < clippedSourceImageRect.Height; i++)
				{
					agg_basics.memmove(destBuffer, destOffset, sourceBuffer, sourceOffset, lengthInBytes);
					sourceOffset += sourceImage.StrideInBytes();
					destOffset += StrideInBytes();
				}
			}
			else
			{
				bool haveConversion = true;
				switch (sourceImage.BitDepth)
				{
					case 24:
						switch (BitDepth)
						{
							case 32:
								{
									int numPixelsToCopy = clippedSourceImageRect.Width;
									for (int i = clippedSourceImageRect.Bottom; i < clippedSourceImageRect.Top; i++)
									{
										int sourceOffset = sourceImage.GetBufferOffsetXY(clippedSourceImageRect.Left, clippedSourceImageRect.Bottom + i);
										byte[] sourceBuffer = sourceImage.GetBuffer();
										int destOffset;
										byte[] destBuffer = GetPixelPointerXY(
											clippedSourceImageRect.Left + destXOffset,
											clippedSourceImageRect.Bottom + i + destYOffset,
											out destOffset);
										for (int x = 0; x < numPixelsToCopy; x++)
										{
											destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
											destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
											destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
											destBuffer[destOffset++] = 255;
										}
									}
								}
								break;

							default:
								haveConversion = false;
								break;
						}
						break;

					default:
						haveConversion = false;
						break;
				}

				if (!haveConversion)
				{
					throw new NotImplementedException("You need to write the " + sourceImage.BitDepth.ToString() + " to " + BitDepth.ToString() + " conversion");
				}
			}
		}
		// Create
		//--------------------------------------------------------------------
		public void create(IImageByte src)
		{
			// we are going to create a dilated image for filtering
			// we add m_dilation pixels to every side of the image and then copy the image in the x
			// direction into each end so that we can sample into this image to get filtering on x repeating
			// if the original image look like this
			//
			// 123456
			//
			// the new image would look like this
			//
			// 0000000000
			// 0000000000
			// 5612345612
			// 0000000000
			// 0000000000

			m_height = (int)agg_basics.uceil(src.Height);
			m_width = (int)agg_basics.uceil(src.Width);
			m_width_hr = (int)agg_basics.uround(src.Width * LineAABasics.line_subpixel_scale);
			m_half_height_hr = (int)agg_basics.uround(src.Height * LineAABasics.line_subpixel_scale / 2);
			m_offset_y_hr = m_dilation_hr + m_half_height_hr - LineAABasics.line_subpixel_scale / 2;
			m_half_height_hr += LineAABasics.line_subpixel_scale / 2;

			int bufferWidth = m_width + m_dilation * 2;
			int bufferHeight = m_height + m_dilation * 2;
			int bytesPerPixel = src.BitDepth / 8;
			int NewSizeInBytes = bufferWidth * bufferHeight * bytesPerPixel;
			if (m_DataSizeInBytes < NewSizeInBytes)
			{
				m_DataSizeInBytes = NewSizeInBytes;
				m_data = new byte[m_DataSizeInBytes];
			}

			m_buf.AttachBuffer(m_data, 0, bufferWidth, bufferHeight, bufferWidth * bytesPerPixel, src.BitDepth, bytesPerPixel);
			byte[] destBuffer = m_buf.GetBuffer();
			byte[] sourceBuffer = src.GetBuffer();

			// copy the image into the middle of the dest
			for (int y = 0; y < m_height; y++)
			{
				for (int x = 0; x < m_width; x++)
				{
					int sourceOffset = src.GetBufferOffsetXY(x, y);
					int destOffset = m_buf.GetBufferOffsetXY(m_dilation, y + m_dilation);
					for (int channel = 0; channel < bytesPerPixel; channel++)
					{
						destBuffer[destOffset++] = sourceBuffer[sourceOffset++];
					}
				}
			}

			// copy the first two pixels form the end into the beginning and from the beginning into the end
			for (int y = 0; y < m_height; y++)
			{
				int s1Offset = src.GetBufferOffsetXY(0, y);
				int d1Offset = m_buf.GetBufferOffsetXY(m_dilation + m_width, y);

				int s2Offset = src.GetBufferOffsetXY(m_width - m_dilation, y);
				int d2Offset = m_buf.GetBufferOffsetXY(0, y);

				for (int x = 0; x < m_dilation; x++)
				{
					for (int channel = 0; channel < bytesPerPixel; channel++)
					{
						destBuffer[d1Offset++] = sourceBuffer[s1Offset++];
						destBuffer[d2Offset++] = sourceBuffer[s2Offset++];
					}
				}
			}
		}