public static bool BoundingBoxesAreCrossed(ConvexPolygon p1, ConvexPolygon p2) { if (p1.mXMin > p2.mXMax) { return(false); } if (p2.mXMin > p1.mXMax) { return(false); } if (p1.mYMin > p2.mYMax) { return(false); } if (p2.mYMin > p1.mYMax) { return(false); } return(true); }
public bool Resize(int targetWidth, int targetHeight, int quality, ProgressReporter callback) { // Calculating new picture's real dimensions double kx = 1, ky = 1; // Scaling coefficients: kx = (double)targetWidth / mWidth; ky = (double)targetHeight / mHeight; // Creating new image float[,] newr = new float[targetWidth, targetHeight]; float[,] newg = new float[targetWidth, targetHeight]; float[,] newb = new float[targetWidth, targetHeight]; float[,] newhl = new float[targetWidth, targetHeight]; // Going thru new pixels. Calculating influence from source pixel // colors to new pixel colors for (int n = 0; n < mHeight; n++) { if (n % REPORT_EVERY_NTH_LINE == 0 && callback != null) { if (!callback((double)n / mHeight)) return false; } for (int m = 0; m < mWidth; m++) { // Transformed source matrix squares CatEye.Core.Point[] src_tr_pts = new CatEye.Core.Point[] { new CatEye.Core.Point(kx * m, ky * n), new CatEye.Core.Point(kx * (m + 1), ky * n), new CatEye.Core.Point(kx * (m + 1), ky * (n + 1)), new CatEye.Core.Point(kx * m, ky * (n + 1)) }; ConvexPolygon cp_src_tr = new ConvexPolygon(src_tr_pts); int xmin = Math.Max((int)cp_src_tr.XMin, 0); int ymin = Math.Max((int)cp_src_tr.YMin, 0); int xmax = Math.Min((int)cp_src_tr.XMax + 1, targetWidth); int ymax = Math.Min((int)cp_src_tr.YMax + 1, targetHeight); for (int j = ymin; j < ymax; j++) { for (int i = xmin; i < xmax; i++) { double part = cp_src_tr.CalcProjectionToPixel(i, j, quality); // Adding colors part newr[i, j] += (float)(r_chan[m, n] * part); newg[i, j] += (float)(g_chan[m, n] * part); newb[i, j] += (float)(b_chan[m, n] * part); newhl[i, j] += (float)(hl_chan[m, n] * part); } } } } lock (this) { r_chan = newr; g_chan = newg; b_chan = newb; hl_chan = newhl; mWidth = targetWidth; mHeight = targetHeight; } return true; }
public void Crotate(double beta, Point c, int crop_w, int crop_h, int quality, ProgressReporter callback) { beta *= Math.PI / 180.0; float[,] oldr = r_chan; float[,] oldg = g_chan; float[,] oldb = b_chan; float[,] oldhl = hl_chan; int oldW = mWidth, oldH = mHeight; // Creating new image lock (this) { r_chan = new float[crop_w, crop_h]; g_chan = new float[crop_w, crop_h]; b_chan = new float[crop_w, crop_h]; hl_chan = new float[crop_w, crop_h]; mWidth = crop_w; mHeight = crop_h; } // Full progress int full_n = 0; object full_n_lock = new object(); // Initializing threads int threads_num = 6; Thread[] threads = new Thread[threads_num]; bool user_cancel = false; for (int q = 0; q < threads_num; q++) { threads[q] = new Thread(delegate (object obj) { try { int n1 = ((thread_data)obj).i1; int n2 = ((thread_data)obj).i2; for (int n = n1; n < n2; n++) { if (n % REPORT_EVERY_NTH_LINE == 0 && callback != null) { if (!callback((double)full_n / oldH)) throw new UserCancelException(); } for (int m = 0; m < oldW; m++) { // Rotated source matrix squares CatEye.Core.Point[] src_tr_pts = new CatEye.Core.Point[] { Point.Rotate(new CatEye.Core.Point(m, n ), -beta, c), Point.Rotate(new CatEye.Core.Point((m + 1), n ), -beta, c), Point.Rotate(new CatEye.Core.Point((m + 1), (n + 1)), -beta, c), Point.Rotate(new CatEye.Core.Point(m, (n + 1)), -beta, c) }; // Rotated and translated source matrix squares CatEye.Core.Point[] src_tr_pts2 = new CatEye.Core.Point[] { new Point(src_tr_pts[0].X - c.X + (double)crop_w / 2, src_tr_pts[0].Y - c.Y + (double)crop_h / 2), new Point(src_tr_pts[1].X - c.X + (double)crop_w / 2, src_tr_pts[1].Y - c.Y + (double)crop_h / 2), new Point(src_tr_pts[2].X - c.X + (double)crop_w / 2, src_tr_pts[2].Y - c.Y + (double)crop_h / 2), new Point(src_tr_pts[3].X - c.X + (double)crop_w / 2, src_tr_pts[3].Y - c.Y + (double)crop_h / 2), }; ConvexPolygon cp_src_tr = new ConvexPolygon(src_tr_pts2); int xmin = Math.Max((int)cp_src_tr.XMin, 0); int ymin = Math.Max((int)cp_src_tr.YMin, 0); int xmax = Math.Min((int)cp_src_tr.XMax + 1, crop_w); int ymax = Math.Min((int)cp_src_tr.YMax + 1, crop_h); for (int j = ymin; j < ymax; j++) { for (int i = xmin; i < xmax; i++) { double part = cp_src_tr.CalcProjectionToPixel(i, j, quality); // Adding colors part lock (this) { r_chan[i, j] += (float)(oldr[m, n] * part); g_chan[i, j] += (float)(oldg[m, n] * part); b_chan[i, j] += (float)(oldb[m, n] * part); hl_chan[i, j] += (float)(oldhl[m, n] * part); } } } } lock (full_n_lock) full_n ++; } } catch (UserCancelException) { user_cancel = true; } }); } // Starting threads for (int q = 0; q < threads_num; q++) { thread_data td = new thread_data(); td.i1 = (oldH / threads_num) * q; if (q < threads_num - 1) { td.i2 = (oldH / threads_num) * (q + 1); } else { td.i2 = oldH; } threads[q].Start(td); } // Waiting for threads for (int q = 0; q < threads_num; q++) { threads[q].Join(); } if (user_cancel) throw new UserCancelException(); }
public static bool BoundingBoxesAreCrossed(ConvexPolygon p1, ConvexPolygon p2) { if (p1.mXMin > p2.mXMax) return false; if (p2.mXMin > p1.mXMax) return false; if (p1.mYMin > p2.mYMax) return false; if (p2.mYMin > p1.mYMax) return false; return true; }