public RawImage Resample(int factor) { switch (factor) { case 2: case 3: case 4: break; default: throw new ArgumentException("Invalid resample factor"); } int w = this.Width / factor; int h = this.Height / factor; int[] dest = new int[w * h]; int p = 0; int[] pp = new int[4]; double l; double a; double b; for (int i = 0; i < h; i++) { int x = (i * factor) * this.Width; for (int k = 0; k < factor; k++) { pp[k] = x; x += this.Width; } for (int j = 0; j < w; j++) { int count = 0; l = a = b = 0f; for (int k = 0; k < factor; k++) { for (int m = 0; m < factor; m++) { LabColor lab = ColorTransform.RgbToLab(Color.FromArgb(this.Pixels[pp[k]])); l += lab.L; a += lab.A; b += lab.B; pp[k]++; ++count; } } dest[p++] = ColorTransform.LabToRgb(new LabColor( l / (double)count, a / (double)count, b / (double)count)).ToArgb() & 0xffffff; } } return(new RawImage(w, h, dest)); }
private static Color[] LabSort(Color[] colors, Color origin) { List <LabColor> l = new List <LabColor>(colors.Length); for (int i = 0; i < colors.Length; i++) { l.Add(ColorTransform.RgbToLab(colors[i])); } l.Sort(new PaletteColorComparer(origin)); Color[] c = new Color[l.Count]; for (int i = 0; i < l.Count; i++) { c[i] = ColorTransform.LabToRgb(l[i]); } return(c); }
public void UpdatePaletteClusters(ClusteringPoint[] clusters, ClusteringPoint[] points) { if (clusters == null) { return; } using (SqlConnection conn = this.GetConnection()) { SqlCommand cmd = new SqlCommand(@"DELETE FROM PALETTE_COLOR WHERE PALETTE_ID < 0", conn); cmd.ExecuteNonQuery(); cmd = null; cmd = new SqlCommand(@"UPDATE PALETTE SET CLUSTER = NULL", conn); cmd.ExecuteNonQuery(); cmd = null; for (int i = 0; i < clusters.Length; i++) { ClusteringPoint p = clusters[i]; int j = 0; int order = 0; while (j < p.Size) { int rgb = ColorTransform.LabToRgb(new LabColor(p[j++], p[j++], p[j++])).ToArgb() & 0xffffff; ++order; cmd = new SqlCommand(@"INSERT INTO [PALETTE_COLOR] ([PALETTE_ID],[ORDER],[RGB]) VALUES (-" + (i + 1).ToString() + "," + order.ToString() + "," + rgb.ToString() + ")", conn); cmd.ExecuteNonQuery(); cmd = null; } } for (int i = 0; i < points.Length; i++) { int id = points[i].Id; int cluster = -(points[i].Index + 1); cmd = new SqlCommand(@"UPDATE PALETTE SET CLUSTER = " + cluster.ToString() + " WHERE ID = " + id.ToString(), conn); cmd.ExecuteNonQuery(); cmd = null; } } }
public Color FromColor3(Color3 color) { return(ColorTransform.LabToRgb(new LabColor(color.A, color.B, color.C))); }
public RawImage ResizeTo(int maxDimension) { if ((this.Width <= maxDimension) && (this.Height <= maxDimension)) { return(this); } double scaleX = (double)this.Width / (double)maxDimension; double scaleY = (double)this.Height / (double)maxDimension; scaleX = scaleX > scaleY ? scaleX : scaleY; int w = (int)((double)this.Width / scaleX); int h = (int)((double)this.Height / scaleX); scaleX = (double)this.Width / (double)(w + 1); int[] mapX = new int[w + 1]; for (int i = 0; i <= w; i++) { mapX[i] = (int)(scaleX * (double)i); } mapX[w] = this.Width; scaleY = (double)this.Height / (double)(h + 1); int[] mapY = new int[h + 1]; for (int i = 0; i <= h; i++) { mapY[i] = (int)(scaleY * (double)i); } mapY[h] = this.Height; int p = 0; double l; double a; double b; int[] dest = new int[w * h]; for (int i = 0; i < h; i++) { int y0 = mapY[i]; int y1 = mapY[i + 1]; for (int j = 0; j < w; j++) { int x0 = mapX[j]; int x1 = mapX[j + 1]; int count = 0; l = a = b = 0f; for (int ii = y0; ii < y1; ii++) { for (int jj = x0; jj < x1; jj++) { int c = this.Pixels[(ii * this.Width) + jj]; LabColor lab = ColorTransform.RgbToLab(Color.FromArgb(c)); l += lab.L; a += lab.A; b += lab.B; ++count; } } if (count > 0) { dest[p] = ColorTransform.LabToRgb(new LabColor( l / (double)count, a / (double)count, b / (double)count)).ToArgb() & 0xffffff; } ++p; } } return(new RawImage(w, h, dest)); }