private void TestButtonRadioButton_Click(object sender, RoutedEventArgs e) { var tab1 = new float[2]; var tab2 = new float[2]; var tab3 = new float[2]; for (int i = 0; i < tab1.Length; i++) { if (i == 0) { tab1[i] = 1f; tab2[i] = 1f; tab3[i] = 1f; } else { tab1[i] = 2f; tab2[i] = 2f; tab3[i] = 2f; } } var inttab1 = new int[10]; var inttab2 = new int[10]; for (int i = 0; i < inttab1.Length; i++) { inttab1[i] = 3; inttab2[i] = 7; } DoubleColor[,] LABImageArray = new DoubleColor[SourceImageColorArray.GetLength(0), SourceImageColorArray.GetLength(1)]; int from = SourceColorSpaceComboBox.SelectedIndex; ColorProfileConverter.ConvertImageToLAB(SourceImageColorArray, LABImageArray, (ColorProfileEnum)from); int rows = LABImageArray.GetLength(0); int cols = LABImageArray.GetLength(1); var vector_x = new float[rows * cols]; var vector_y = new float[rows * cols]; var vector_z = new float[rows * cols]; for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { vector_x[y + x * cols] = (float)LABImageArray[x, y].R; vector_y[y + x * cols] = (float)LABImageArray[x, y].G; vector_z[y + x * cols] = (float)LABImageArray[x, y].B; } } using (var wrapper = new Logic()) { //var result = wrapper.addParallelVectors(inttab1, inttab2, inttab1.Length); //for (int i = 0; i < result.Length; i++) //{ // Debug.Write($"{result[i]}, "); //} //Debug.WriteLine(""); var iters = wrapper.KMeansGather(tab1, tab2, tab3, tab1.Length, 1, MaxIter); Debug.WriteLine($"KMEANS: Iters: {iters}"); for (int i = 0; i < tab1.Length; i++) { Debug.WriteLine($"X: {tab1[i]} Y: {tab2[i]} Z: {tab3[i]}"); } var img_iters = wrapper.KMeansGather(vector_x, vector_y, vector_z, vector_x.Length, KMeansParam, MaxIter); Debug.WriteLine($"Image iterations: {img_iters}"); } for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { LABImageArray[x, y].R = vector_x[y + x * cols]; LABImageArray[x, y].G = vector_y[y + x * cols]; LABImageArray[x, y].B = vector_z[y + x * cols]; } } ColorProfileConverter.ConvertImageFromLAB(LABImageArray, DestImageColorArray, (ColorProfileEnum)from); Paint.CopyToWriteableBitmap(DestImageWB, DestImageColorArray); }
//param 0-gather, 1-scatter, 2-reduce_by_key private void KMeansGPULaunch(int param) { var start1 = DateTime.Now; int rows = SourceImageColorArray.GetLength(0); int cols = SourceImageColorArray.GetLength(1); var colors_array = new int[rows * cols]; for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { colors_array[y + x * cols] = SourceImageColorArray[x, y].ToInt(); } } var RGBtoXYZMatrix = SourceImageCP.RGBtoXYZ.toFloatMatrix(); var XYZtoRGBMatrix = SourceImageCP.XYZtoRGB.toFloatMatrix(); float YR = 100f; double XR_double = SourceImageCP.White_X * YR / SourceImageCP.White_Y; double ZR_double = SourceImageCP.White_Z * YR / SourceImageCP.White_Y; float XR = (float)XR_double; float ZR = (float)ZR_double; float gamma = (float)SourceImageCP.Gamma; var start2 = DateTime.Now; using (var wrapper = new Logic()) { try { var img_iters = wrapper.KMeansImage(colors_array, colors_array.Length, XR, YR, ZR, gamma, RGBtoXYZMatrix, XYZtoRGBMatrix, KMeansParam, MaxIter, param); Debug.WriteLine($"Image iterations: {img_iters}"); } catch (Exception e) { Debug.WriteLine($"Unexpected error: {e.Message}"); return; } } var end = DateTime.Now; for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { DestImageColorArray[x, y] = new SimpleColor(colors_array[y + x * cols]); } } Dispatcher.Invoke(() => { Paint.CopyToWriteableBitmap(DestImageWB, colors_array, rows, cols); }); string method = "NONE"; if (param == 0) { method = "GATHER"; } else if (param == 1) { method = "SCATTER"; } else if (param == 2) { method = "RBK"; } Debug.WriteLine($"GPU TIME[{method}]: {end - start2} , with memory allocation: {end - start1}"); }
private bool TryGenerate(bool paint = true) { var isValid = ValidateTextBoxes(); if (isValid.IsSourceValid == false && isValid.IsDestValid == false) { var info = new StringBuilder(); info.AppendLine("Error in source and destination color space!"); MessageBox.Show(info.ToString(), Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } else if (isValid.IsSourceValid == false) { var info = new StringBuilder(); info.AppendLine("Error in source color space!"); MessageBox.Show(info.ToString(), Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } else if (isValid.IsDestValid == false) { var info = new StringBuilder(); info.AppendLine("Error in destination color space!"); MessageBox.Show(info.ToString(), Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } if (SourceImageColorProfile.HasChanged == false && DestImageColorProfile.HasChanged == false) { if (paint == true) { int from = SourceColorSpaceComboBox.SelectedIndex; int to = DestColorSpaceComboBox.SelectedIndex; ColorProfileConverter.ConvertImage(SourceImageColorArray, DestImageColorArray, (ColorProfileEnum)from, (ColorProfileEnum)to); Paint.CopyToWriteableBitmap(DestImageWB, DestImageColorArray); } } else { var sourceValidation = SourceImageColorProfile.Validate(); var destValidation = DestImageColorProfile.Validate(); if (sourceValidation.isValid == false && destValidation.isValid == false) { //error var info = new StringBuilder(); info.AppendLine("Error in source and destination color space:"); info.AppendLine("Source: " + sourceValidation.info); info.Append("Destination: " + destValidation.info); MessageBox.Show(info.ToString(), Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } else if (sourceValidation.isValid == false) { //error var info = new StringBuilder(); info.AppendLine("Error in source color space:"); info.Append(sourceValidation.info); MessageBox.Show(info.ToString(), Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } else if (destValidation.isValid == false) { //error var info = new StringBuilder(); info.AppendLine("Error in destination color space:"); info.Append(destValidation.info); MessageBox.Show(info.ToString(), Globals.WindowName, MessageBoxButton.OK, MessageBoxImage.Error); return(false); } else { if (paint == true) { ColorProfileConverter.ConvertImage(SourceImageColorArray, DestImageColorArray, SourceImageColorProfile, DestImageColorProfile); Paint.CopyToWriteableBitmap(DestImageWB, DestImageColorArray); } } } return(true); }