public static Point TransformPoint(PointD[] pos3, int idiolCenterX, int idiolCenterY, Point srcpos) { var srcPos = new AffinePos(pos3[0], pos3[1], pos3[2], pos3[3]); var reslist = new List <PartialData>(); var trsData = srcPos.ToTrnsData(1, 1); var xshift = (int)(trsData.XSrcCnter - idiolCenterX); var yshift = (int)(trsData.YSrcCnter - idiolCenterY); double radian = trsData.Angle; double cosRadian = Cos(radian); double sinRadian = Sin(radian); var x = srcpos.X - xshift; var y = srcpos.Y - yshift; var sourceX = (x - idiolCenterX) * cosRadian + (y - idiolCenterY) * (sinRadian); sourceX += (idiolCenterX); var sourceY = (x - idiolCenterX) * -sinRadian + (y - idiolCenterY) * cosRadian; sourceY += (idiolCenterY); // 이부분은 ok var isourceX = (int)sourceX; var isourceY = (int)sourceY; return(new Point(isourceX, isourceY)); }
public static PointD GetCenter(this AffinePos pos) { var xCenter = Abs(pos.LT.X + pos.RB.X) / 2; var yCenter = Abs(pos.LB.Y + pos.RT.Y) / 2; return(new PointD(xCenter, yCenter)); }
public static TrnsData ToTrnsData (this AffinePos srcPos, int w, int h) { //inner w h var innerh = PosL2(srcPos.LB, srcPos.LT); var innerw = PosL2(srcPos.RT, srcPos.LT); var trgPos = srcPos.MoveToCenter(new PointD(w / 2.0, h / 2.0)); // ok var fcenterX = srcPos.GetCenter().X; var fcenterY = srcPos.GetCenter().Y; var lcenterX = trgPos.GetCenter().X; var lcenterY = trgPos.GetCenter().Y; var xshift = lcenterX - fcenterX; var yshift = lcenterY - fcenterY; var dx = srcPos.LT.X - srcPos.RT.X; var dy = srcPos.LT.Y - srcPos.RT.Y; double radian; if (Abs(dx) < 0.0001) { radian = 1.5708; } else if (Abs(dy) < 0.0001) { radian = 3.14159; } else { radian = Math.Atan(dy / dx); } //var angle_degree = 360 - Math.Atan2(dy, dx) * 180 / Math.PI; //double radian; //angle_degree %= 360; //radian = PI / 180.0 * angle_degree; return(new TrnsData() { H = h, W = w, XShift = (int)xshift, YShift = (int)yshift, Innerw = innerw, Innterh = innerh, XSrcCnter = (int)fcenterX, YSrcCnter = (int)fcenterY, dX = (int)dx, dY = (int)dy, Angle = radian, }); }
public static AffinePos MoveToCenter(this AffinePos pos, PointD center) { var wHalf = Math.Sqrt(Math.Pow((pos.LT.X - pos.RT.X), 2) + Math.Pow((pos.LT.Y - pos.RT.Y), 2)); var hHalf = Math.Sqrt(Math.Pow((pos.LT.X - pos.LB.X), 2) + Math.Pow((pos.LT.Y - pos.LB.Y), 2)); var x1 = center.X - wHalf; var x2 = center.X - wHalf; var x3 = center.X + wHalf; var x4 = center.X + wHalf; var y1 = center.Y - hHalf; var y3 = center.Y + hHalf; var y2 = center.Y + hHalf; var y4 = center.Y - hHalf; return(new AffinePos(x1, y1, x2, y2, x3, y3, x4, y4)); }
public static AffinePos GetCentered(this AffinePos pos, PointD center) { var wHalf = Abs(pos.LT.X - pos.RT.X) / 2; var hHalf = Abs(pos.LT.Y - pos.RT.Y) / 2; var x1 = center.X - wHalf; var x2 = center.X - wHalf; var x3 = center.X + wHalf; var x4 = center.X + wHalf; var y1 = center.Y - hHalf; var y3 = center.Y + hHalf; var y2 = center.Y + hHalf; var y4 = center.Y - hHalf; return(new AffinePos(x1, y1, x2, y2, x3, y3, x4, y4)); }
public static int[,] xytransform(AffinePos srcPos, AffinePos trgPos, int[,] src) { var h = src.GetLength(0); var w = src.GetLength(1); var fcenterX = srcPos.GetCenter().X; var fcenterY = srcPos.GetCenter().Y; var lcenterX = trgPos.GetCenter().X; var lcenterY = trgPos.GetCenter().Y; var xshift = lcenterX - fcenterX; var yshift = lcenterY - fcenterY; var dx = Abs(srcPos.LT.X - srcPos.RT.X); var dy = Abs(srcPos.LT.Y - srcPos.RT.Y); if (dx == 0) { return(null); } var angle_degree = 360 - Math.Atan2(dy, dx) * 180 / Math.PI; int[,] R = new int[(int)(h + Abs(yshift)), (int)(w + Abs(xshift))]; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { if (i - xshift >= 0 && j - yshift >= 0 && i - xshift < R.GetLength(1) && j - yshift < R.GetLength(0)) { R[(int)(j - yshift), (int)(i - xshift)] = src[j, i]; } } } return(R); }
//public static Func< byte[,] ,TrnsData, byte[,],int , byte[,]> Translation // => (trg,tData, src,h) // => xytransform(trg,tData, src , h ); // //public static Func<byte[,],TrnsData, byte[,] , int , byte[,]> Rotation // => (trg,tData, src,h) // => rotateGeneral(trg,tData, src , h ); public static Func <TrnsData> CreateTrnsData (AffinePos srcPos, int w, int h) => new Func <TrnsData>(() => srcPos.ToTrnsData(w, h));
public static Bitmap Run_Memory(MemoryStream bytestream, int w, int h, PointD[] pos3) { var srcPos = new AffinePos(pos3[0], pos3[1], pos3[2], pos3[3]); var reslist = new List <PartialData>(); var trsData = srcPos.ToTrnsData(w, h); var xc = trsData.XSrcCnter; var yc = trsData.YSrcCnter; var xmax = Math.Max(w - xc, xc) * 2; var ymax = Math.Max(h - yc, yc) * 2; byte[][] R1 = CreateJagged(ymax, xmax); // -- for (int i = 0; i < h; i++) { var data = new byte[w]; bytestream.Seek(i * w, SeekOrigin.Begin); bytestream.Read(data, 0, w); var reshaped = data.Reshape(1, w).ToJagged(); xytransform(ref R1, trsData, reshaped, i, xmax, ymax); } var w1 = R1[0].Length; var h1 = R1.Length; var degree = trsData.Angle * 180 / Math.PI; double radian = trsData.Angle; //double radian = 0.785398; double cosRadian = Cos(radian); double sinRadian = Sin(radian); int newWidth = (int)(w1 * Abs(cosRadian) + h1 * Abs(sinRadian)); int newHeight = (int)(h1 * Abs(cosRadian) + w1 * Abs(sinRadian)); // ok byte[][] R2 = CreateJagged(newWidth, newHeight); for (int i = 0; i < R1.Length; i++) { //bytestream.Seek(i * w, SeekOrigin.Begin); //bytestream.Read(data, 0, w); try { var reshaped = R1[i].Reshape(1, w1).ToJagged(); rotateGeneral(ref R2, trsData, reshaped, i, w1, h1); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } return(R2.ToMat().ToBitmap_SetPixel()); }