Beispiel #1
0
 /// <summary>
 /// Finds the smallest and largest Y coordinates of the pixels whose alpha channel
 /// exceeds the specified threshold. The X coordinates of the result cover the entire image.
 /// </summary>
 public PixelRect PreciseHeight(int alphaThreshold = 0, int left = 0, int width = -1)
 {
     if (width < 0)
     {
         width = Width;
     }
     using (UseRead())
         return(PixelRect.FromTopBottom(PreciseTop(alphaThreshold, left, width), PreciseBottom(alphaThreshold, left, width)));
 }
Beispiel #2
0
 /// <summary>
 /// Finds the smallest and largest X and Y coordinates of the pixels whose alpha channel exceeds the specified threshold.
 /// </summary>
 public PixelRect PreciseSize(int alphaThreshold = 0)
 {
     using (UseRead())
     {
         int left   = PreciseLeft(alphaThreshold);
         int right  = PreciseRight(alphaThreshold);
         int top    = PreciseTop(alphaThreshold, left, right + 1);
         int bottom = PreciseBottom(alphaThreshold, left, right + 1);
         return(PixelRect.FromBounds(left, top, right, bottom));
     }
 }
Beispiel #3
0
 /// <summary>
 /// Finds the smallest and largest X coordinates of the pixels whose alpha channel
 /// exceeds the specified threshold. The Y coordinates of the result cover the entire image.
 /// </summary>
 public PixelRect PreciseWidth(int alphaThreshold = 0)
 {
     using (UseRead())
         return(PixelRect.FromLeftRight(PreciseLeft(alphaThreshold), PreciseRight(alphaThreshold)));
 }
Beispiel #4
0
 public PixelRect WithTopBottom(PixelRect height)
 {
     return(FromBounds(Left, height.Top, Right, height.Bottom));
 }
Beispiel #5
0
 public PixelRect WithLeftRight(PixelRect width)
 {
     return(FromBounds(width.Left, Top, width.Right, Bottom));
 }
Beispiel #6
0
 public PixelRect WithTopBottom(PixelRect height)
 {
     return FromBounds(Left, height.Top, Right, height.Bottom);
 }
Beispiel #7
0
 public PixelRect WithLeftRight(PixelRect width)
 {
     return FromBounds(width.Left, Top, width.Right, Bottom);
 }
        public static unsafe BitmapBase SizePos(BitmapBase source, double scaleWidth, double scaleHeight, int inX, int inY, int outX, int outY, int maxWidth = 0, int maxHeight = 0, Filter filter = null)
        {
            if (source.Width <= 0 || source.Height <= 0)
            {
                return(source.ToBitmapSame());
            }

            PixelRect pureImg = source.PreciseSize(0);

            if (pureImg.Width <= 0 || pureImg.Height <= 0)
            {
                return(source.ToBitmapSame());
            }

            int outWidth  = (int)Math.Round(pureImg.Width * scaleWidth);
            int outHeight = (int)Math.Round(pureImg.Height * scaleHeight);

            if (scaleWidth == 1 && scaleHeight == 1)
            {
                //no resize needed
                if (inX != outX || inY != outY)
                {
                    BitmapBase result;
                    if (maxWidth == 0 && maxHeight == 0)
                    {
                        result = new BitmapRam(outX - inX + source.Width, outY - inY + source.Height);
                    }
                    else
                    {
                        result = new BitmapRam(Math.Min(outX - inX + source.Width, maxWidth), Math.Min(outY - inY + source.Height, maxHeight));
                    }

                    result.DrawImage(source, outX - inX, outY - inY);
                    return(result);
                }
                else
                {
                    return(source.ToBitmapSame());
                }
            }

            if (filter == null)
            {
                if (scaleWidth < 1)
                {
                    filter = new LanczosFilter();
                }
                else
                {
                    filter = new MitchellFilter();
                }
            }

            int transparentOffset;

            if (pureImg.Left != 0 || pureImg.Top != 0)
            {
                transparentOffset = pureImg.Left * 4 + pureImg.Top * source.Stride;
                // Resample looks better if transprent pixels is cropped. Especially if the image is square
                // Data+DataOffset, pureImg.Width, pureImg.Height instead of Data, Width, Height works like left-top cropping
            }
            else
            {
                transparentOffset = 0;
            }

            BitmapBase afterHorzResample, afterVertResample;

            // Horizontal resampling
            if (scaleWidth == 1)
            {
                afterHorzResample = source;
            }
            else
            {
                afterHorzResample = new BitmapRam(outWidth, pureImg.Height);
                ContributorEntry[] contrib = filter.PrecomputeResample(scaleWidth, pureImg.Width, outWidth);
                Resample1D(afterHorzResample, source, transparentOffset, contrib, outWidth, pureImg.Height, true);
                transparentOffset = 0;
            }

            // Vertical resampling
            if (scaleHeight == 1)
            {
                afterVertResample = afterHorzResample;
            }
            else
            {
                afterVertResample = new BitmapRam(outWidth, outHeight);
                ContributorEntry[] contrib = filter.PrecomputeResample(scaleHeight, pureImg.Height, outHeight);
                Resample1D(afterVertResample, afterHorzResample, transparentOffset, contrib, outHeight, outWidth, false);
            }

            BitmapBase final;
            //At this point image will be resized and moved to another BitmapBase anyway
            int drawX = outX - (int)Math.Round((inX - pureImg.Left) * scaleWidth);
            int drawY = outY - (int)Math.Round((inY - pureImg.Top) * scaleHeight);

            if (maxWidth == 0 && maxHeight == 0)
            {
                final = new BitmapRam(Math.Max(drawX + outWidth, maxWidth), Math.Max(drawY + outHeight, maxHeight));
            }
            else
            {
                final = new BitmapRam(Math.Max(drawX + outWidth, maxWidth), Math.Max(drawY + outHeight, maxHeight));
            }
            final.DrawImage(afterVertResample, drawX, drawY);
            return(final);
        }