Beispiel #1
0
 public static void Blit(LockedBitmap source, LockedBitmap target, System.Drawing.Rectangle srcRect, int destX, int destY)
 {
     for (int sx = srcRect.X, dx = destX; sx < srcRect.X + srcRect.Width; sx++, dx++)
     {
         for (int sy = srcRect.Y, dy = destY; sy < srcRect.Y + srcRect.Height; sy++, dy++)
         {
             Color sourcePixel = source.GetPixel(sx, sy);
             target.SetPixel(dx, dy, sourcePixel);
         }
     }
 }
Beispiel #2
0
        public static void BlitMask(LockedBitmap source, LockedBitmap target, System.Drawing.Rectangle srcRect, int destX, int destY)
        {
            if (source.Depth != 24 || target.Depth != 32)
            {
                throw new Exception("BlitMask requires the source to be 24-bit and the target to be 32-bit");
            }

            int targetStartX = Math.Max(destX, 0);
            int targetEndX   = Math.Min(destX + srcRect.Width, target.Width);
            int targetStartY = Math.Max(destY, 0);
            int targetEndY   = Math.Min(destY + srcRect.Height, target.Height);

            int copyW = targetEndX - targetStartX;
            int copyH = targetEndY - targetStartY;

            if (copyW < 0 || copyH < 0)
            {
                return;
            }

            int sourceStartX = srcRect.X + targetStartX - destX;
            int sourceStartY = srcRect.Y + targetStartY - destY;

            for (int sx = sourceStartX, dx = targetStartX; dx < targetEndX; sx++, dx++)
            {
                for (int sy = sourceStartY, dy = targetStartY; dy < targetEndY; sy++, dy++)
                {
                    Color sourcePixel = source.GetPixel(sx, sy);
                    int   lume        = sourcePixel.R + sourcePixel.G + sourcePixel.B;
                    lume /= 3;

                    if (lume > 255)
                    {
                        lume = 255;
                    }

                    target.SetComponent(dx, dy, 3, (byte)lume);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns true when the pixel specified is black.
        /// </summary>
        public static bool EmptyPixelAt(LockedBitmap lockedBitmap, int x, int y)
        {
            Color color = lockedBitmap.GetPixel(x, y);

            return(color.R == 0 && color.G == 0 && color.B == 0);
        }
Beispiel #4
0
 /// <summary>
 /// Returns true when the pixel specified has an alpha
 /// lower than or equal to the specified tolerance.
 /// </summary>
 public static bool EmptyAlphaPixelAt(LockedBitmap lockedBitmap, int x, int y, byte tolerance)
 {
     return(lockedBitmap.GetComponent(x, y, 3) <= tolerance);
 }
Beispiel #5
0
        public LockedBitmap DownScale(int newWidth, int newHeight)
        {
            LockedBitmap newBitmap = new LockedBitmap(new Bitmap(newWidth, newHeight, Source.PixelFormat));

            if (Source.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new Exception("DownsScale32 only works on 32 bit images");
            }

            float xscale = (float)Data.Width / newWidth;
            float yscale = (float)Data.Height / newHeight;

            byte  r = 0, g = 0, b = 0, a = 0;
            float summedR = 0f;
            float summedG = 0f;
            float summedB = 0f;
            float summedA = 0f;

            int left, right, top, bottom; //the area of old pixels covered by the new bitmap

            float targetStartX, targetEndX;
            float targetStartY, targetEndY;

            float leftF, rightF, topF, bottomF; //edges of new pixel in old pixel coords
            float weight;
            float weightScale       = xscale * yscale;
            float totalColourWeight = 0f;

            for (int m = 0; m < newHeight; m++)
            {
                for (int n = 0; n < newWidth; n++)
                {
                    leftF  = n * xscale;
                    rightF = (n + 1) * xscale;

                    topF    = m * yscale;
                    bottomF = (m + 1) * yscale;

                    left  = (int)leftF;
                    right = (int)rightF;

                    top    = (int)topF;
                    bottom = (int)bottomF;

                    if (left < 0)
                    {
                        left = 0;
                    }
                    if (top < 0)
                    {
                        top = 0;
                    }
                    if (right >= Data.Width)
                    {
                        right = Data.Width - 1;
                    }
                    if (bottom >= Data.Height)
                    {
                        bottom = Data.Height - 1;
                    }

                    summedR           = 0f;
                    summedG           = 0f;
                    summedB           = 0f;
                    summedA           = 0f;
                    totalColourWeight = 0f;

                    for (int j = top; j <= bottom; j++)
                    {
                        for (int i = left; i <= right; i++)
                        {
                            targetStartX = Math.Max(leftF, i);
                            targetEndX   = Math.Min(rightF, i + 1);

                            targetStartY = Math.Max(topF, j);
                            targetEndY   = Math.Min(bottomF, j + 1);

                            weight = (targetEndX - targetStartX) * (targetEndY - targetStartY);

                            Color newColors = GetPixel(i, j);
                            r = newColors.R;
                            g = newColors.G;
                            b = newColors.B;
                            a = newColors.A;

                            summedA += weight * a;

                            if (a != 0)
                            {
                                summedR           += weight * r;
                                summedG           += weight * g;
                                summedB           += weight * b;
                                totalColourWeight += weight;
                            }
                        }
                    }

                    summedR /= totalColourWeight;
                    summedG /= totalColourWeight;
                    summedB /= totalColourWeight;
                    summedA /= weightScale;

                    if (summedR < 0)
                    {
                        summedR = 0f;
                    }
                    if (summedG < 0)
                    {
                        summedG = 0f;
                    }
                    if (summedB < 0)
                    {
                        summedB = 0f;
                    }
                    if (summedA < 0)
                    {
                        summedA = 0f;
                    }

                    if (summedR >= 256)
                    {
                        summedR = 255;
                    }
                    if (summedG >= 256)
                    {
                        summedG = 255;
                    }
                    if (summedB >= 256)
                    {
                        summedB = 255;
                    }
                    if (summedA >= 256)
                    {
                        summedA = 255;
                    }

                    newBitmap.SetPixel(n, m, new Color((int)summedR, (int)summedG, (int)summedB, (int)summedA));
                }
            }

            // Unlock old bitmap
            UnlockBits();
            return(newBitmap);
        }