private void LoadImage(WriteableBitmap bitmap) { if (bitmap.PixelWidth != 0 && bitmap.PixelHeight != 0) { this.loadedBitmap = bitmap; PixelateGenTaskData task_data = new PixelateGenTaskData(); task_data.pix_denom = this.pixDenom; task_data.width = bitmap.PixelWidth; task_data.height = bitmap.PixelHeight; task_data.pixels = bitmap.Pixels; this.pixelateGeneratorWorker.RunWorkerAsync(task_data); this.GenerationProgressIndicator.IsVisible = true; } }
private void StartPixelateGenerator() { if (this.scaledBitmap != null) { try { PixelateGenTaskData task_data = new PixelateGenTaskData(); task_data.pix_denom = this.pixDenom; task_data.width = this.scaledBitmap.PixelWidth; task_data.height = this.scaledBitmap.PixelHeight; task_data.pixels = this.scaledBitmap.Pixels; this.pixelateGeneratorWorker.RunWorkerAsync(task_data); this.GenerationProgressIndicator.IsVisible = true; } catch (Exception) { // Ignore } } }
private void pixelateGeneratorWorker_DoWork(object sender, DoWorkEventArgs e) { int pix_denom = (e.Argument as PixelateGenTaskData).pix_denom; int width = (e.Argument as PixelateGenTaskData).width; int height = (e.Argument as PixelateGenTaskData).height; int[] pixels = (e.Argument as PixelateGenTaskData).pixels; int[] pixelated_pixels = pixels.Clone() as int[]; int pix_size = width > height ? width / pix_denom : height / pix_denom; if (pix_size != 0) { for (int i = 0; i < width / pix_size + 1; i++) { for (int j = 0; j < height / pix_size + 1; j++) { int avg_r = 0; int avg_g = 0; int avg_b = 0; int pixcnt = 0; for (int x = i * pix_size; x < (i + 1) * pix_size && x < width; x++) { for (int y = j * pix_size; y < (j + 1) * pix_size && y < height; y++) { byte[] color = BitConverter.GetBytes(pixelated_pixels[y * width + x]); avg_r += color[0]; avg_g += color[1]; avg_b += color[2]; pixcnt++; } } if (pixcnt != 0) { avg_r = avg_r / pixcnt; avg_g = avg_g / pixcnt; avg_b = avg_b / pixcnt; for (int x = i * pix_size; x < (i + 1) * pix_size && x < width; x++) { for (int y = j * pix_size; y < (j + 1) * pix_size && y < height; y++) { byte[] color = BitConverter.GetBytes(pixelated_pixels[y * width + x]); pixelated_pixels[y * width + x] = (color[3] << 24) | (avg_b << 16) | (avg_g << 8) | avg_r; } } } } } } PixelateGenTaskData task_data = new PixelateGenTaskData(); task_data.pix_denom = pix_denom; task_data.width = width; task_data.height = height; task_data.pixels = pixelated_pixels; e.Result = task_data; }