private void grayscale(ImageData image, ICanvasRenderingContext2D ctx) { ctx.drawImage(image, 0, 0); uint imgWidth = image.width; uint imgHeight = image.height; var imageData = (ImageData) ctx.getImageData(0, 0, imgWidth, imgHeight); for (ulong i = 0; i < imageData.height; i++) { for (ulong j = 0; j < imageData.width; j++) { ulong index = (i*4)*imageData.width + (j*4); var red = (byte) imageData.data[index]; var green = (byte) imageData.data[index + 1]; var blue = (byte) imageData.data[index + 2]; var alpha = (byte) imageData.data[index + 3]; var average = (byte) ((red + green + blue)/3); imageData.data[index] = average; imageData.data[index + 1] = average; imageData.data[index + 2] = average; imageData.data[index + 3] = alpha; } } ctx.putImageData(imageData, imageData.width + 5, 0, 0, 0, imageData.width, imageData.height); }