0t3ch / aforge

Automatically exported from code.google.com/p/aforge
Other
0 stars 0 forks source link

16-bit greyscale/48-bit RGB support in CanvasMove #229

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Describe the fix or new feature which you would like to contribute to the 
project ...

Addition of 16-bit greyscale/48-bit RGB support in CanvasMove

Provide SVN patch for a code change or source file if it is something new ...

Index: AForge.NET Framework-2.1.5/Sources/Imaging/Filters/Other/CanvasMove.cs
===================================================================
--- AForge.NET 
Framework-2.1.5/Sources/Imaging/Filters/Other/CanvasMove.cs (revision 4359)
+++ AForge.NET 
Framework-2.1.5/Sources/Imaging/Filters/Other/CanvasMove.cs (revision 4360)
@@ -9,6 +9,8 @@
 // volodymyr.goncharov@gmail.com
 //

+using System;
+
 namespace AForge.Imaging.Filters
 {
     using System.Drawing;
@@ -112,6 +114,7 @@
             formatTranslations[PixelFormat.Format24bppRgb]    = PixelFormat.Format24bppRgb;
             formatTranslations[PixelFormat.Format32bppArgb]   = PixelFormat.Format32bppArgb;
             formatTranslations[PixelFormat.Format32bppRgb]    = PixelFormat.Format32bppRgb;
+            formatTranslations[PixelFormat.Format48bppRgb]    = 
PixelFormat.Format48bppRgb;
         }

         /// <summary>
@@ -173,14 +176,39 @@
             this.fillGray   = fillColorGray;
         }

-        /// <summary>
+                /// <summary>
         /// Process the filter on the specified image.
         /// </summary>
         /// 
         /// <param name="image">Source image data.</param>
         ///
-        protected override unsafe void ProcessFilter( UnmanagedImage image )
+        protected override void ProcessFilter( UnmanagedImage image )
         {
+            int pixelSize = Image.GetPixelFormatSize(image.PixelFormat)/8;
+            switch(pixelSize)
+            {
+                case 1:
+                case 3:
+                case 4:
+                    ProcessFilter8bpc(image); break;
+                case 2:
+                case 6:
+                    ProcessFilter16bpc(image); break;
+                default:
+                    throw new NotImplementedException();
+
+            }
+        }
+
+        /// <summary>
+        /// Process the filter on the specified image.
+        /// </summary>
+        /// <remarks>
+        /// This method handles images with 8 bits per channel.
+        /// </remarks>
+        /// <param name="image">Source image data.</param>
+        private unsafe void ProcessFilter8bpc( UnmanagedImage image )
+        {
             int pixelSize = Image.GetPixelFormatSize( image.PixelFormat ) / 8;

             // get image width and height
@@ -272,5 +300,110 @@
                 }
             }
         }
+
+        /// <summary>
+        /// Process the filter on the specified image.
+        /// </summary>
+        /// <remarks>
+        /// This method handles images with 16 bits per channel.
+        /// </remarks>
+        /// <param name="image">Source image data.</param>
+        private unsafe void ProcessFilter16bpc(UnmanagedImage image)
+        {
+            const int bytesPerChannel = 2;
+            int bytesPerPixel = Image.GetPixelFormatSize(image.PixelFormat)/8;
+            int valuesPerPixel = bytesPerPixel/bytesPerChannel;
+
+            // pad fill colours to 16-bits
+            ushort fillRed = (ushort)(this.fillRed << 8);
+            ushort fillGreen = (ushort)(this.fillGreen << 8);
+            ushort fillBlue = (ushort)(this.fillBlue << 8);
+
+            // get image width and height
+            int width = image.Width;
+            int height = image.Height;
+            int stride = image.Stride;
+
+            int movePointX = movePoint.X;
+            int movePointY = movePoint.Y;
+
+            // intersection rectangle
+            Rectangle intersect = Rectangle.Intersect(
+                new Rectangle(0, 0, width, height),
+                new Rectangle(movePointX, movePointY, width, height));
+
+            // start, stop and step for X and Y
+            int yStart = 0;
+            int yStop = height;
+            int yStep = 1;
+            int xStart = 0;
+            int xStop = width;
+            int xStep = 1;
+
+            if (movePointY > 0)
+            {
+                yStart = height - 1;
+                yStop = -1;
+                yStep = -1;
+            }
+            if (movePointX > 0)
+            {
+                xStart = width - 1;
+                xStop = -1;
+                xStep = -1;
+            }
+
+            // do the job
+            ushort* src = (ushort*)image.ImageData.ToPointer();
+            ushort* pixel, moved;
+
+            if (image.PixelFormat == PixelFormat.Format16bppGrayScale)
+            {
+                // grayscale image
+                for (int y = yStart; y != yStop; y += yStep)
+                {
+                    for (int x = xStart; x != xStop; x += xStep)
+                    {
+                        // current pixel
+                        pixel = src + y*(stride >> 1) + x;
+
+                        if (intersect.Contains(x, y))
+                        {
+                            moved = src + (y - movePointY) * (stride >> 1) + 
(x - movePointX);
+                            *pixel = *moved;
+                        }
+                        else
+                            *pixel = fillGray;
+                    }
+                }
+            }
+            else
+            {
+                // color image
+                for (int y = yStart; y != yStop; y += yStep)
+                {
+                    for (int x = xStart; x != xStop; x += xStep)
+                    {
+                        // current pixel
+                        pixel = src + y * (stride / bytesPerChannel) + x * 
valuesPerPixel;
+
+                        if (intersect.Contains(x, y))
+                        {
+                            moved = src + (y - movePointY) * (stride / 
bytesPerChannel) + (x - movePointX) * valuesPerPixel;
+
+                            pixel[RGB.R] = moved[RGB.R];
+                            pixel[RGB.G] = moved[RGB.G];
+                            pixel[RGB.B] = moved[RGB.B];
+                        }
+                        else
+                        {
+                            pixel[RGB.R] = fillRed;
+                            pixel[RGB.G] = fillGreen;
+                            pixel[RGB.B] = fillBlue;
+                        }
+                    }
+                }
+            }
+        }
     }
 }
\ No newline at end of file

Original issue reported on code.google.com by yvan.rod...@gmail.com on 21 Jun 2011 at 6:28

Attachments:

GoogleCodeExporter commented 9 years ago
CanvasMove is extended to support 16 bpp grayscale and 48/64 bpp color images.

Committed in revision 1535. Will be released in version 2.2.0.

Original comment by andrew.k...@gmail.com on 27 Jun 2011 at 10:22

GoogleCodeExporter commented 9 years ago

Original comment by andrew.k...@gmail.com on 28 Jul 2011 at 9:49

GoogleCodeExporter commented 9 years ago

Original comment by andrew.k...@gmail.com on 10 Aug 2011 at 9:39