I have opened an image in my app and getting the pixels data of image using the following piece of code.
using (IRandomAccessStream fileStreams = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStreams);
BitmapTransform transform = new BitmapTransform()
{
ScaledWidth = Convert.ToUInt32(bitmapImage.PixelWidth),
ScaledHeight = Convert.ToUInt32(bitmapImage.PixelHeight)
};
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage
);
byte[] sourcePixels = pixelData.DetachPixelData();
}
Here I have got an array of all the pixels of the image. The total number of pixels in this array are (width * height * 4). After analyzing this array I came to know that index numbers 0, 1, 2 and 3 contains the red, green blue and alpha values of the first pixel, index numbers 4, 5, 6 and 7 contains the red, green, blue and alpha values of second pixel of the image and so on.
Now I want to apply my 3x3 filter to this image, how can I do it with this 1-D array? I know how to do it if I have 2-D array of the image.
Right now, I have one idea in my mind.
- Store red pixels in one 2D array, green in other and so on
- Apply filter on each 2d array.
- Combine all of these to make a 1-D array again and return the result.
Is it a good solution? Help me if there is a better solution to do it.