Could please somebody explain me (ASCII is really welcome) what the stride
argument stands for in Canvas.drawBitmap()
and in Bitmap.setPixels()/getPixels()
? I understand it's a way to skip elements in the colors array, but how?
In most cases the stride is the same as the width. The stride is useful if you are trying to copy/draw a sub-region of a Bitmap. For instance, if you have a 100x100 bitmap and you want to draw the 50x50 top-right corner, you can use a width of 50px and a stride of 100px.
Stride is number of bytes used for storing one image row.
Stride can be different from the image width.
Most of the images are 4 byte aligned.
For ex. a 24 bit (RGB) image with width of 50 pixels. The total bytes required will be 150 (3(RGB)*50). As image will be 4 byte aligned, in this case the byte required will become 152.
So you will see stride as 152, width 50 and image alignment as 4 byte.
In most cases the stride is the same as the width. The stride is useful if you are trying to copy/draw a sub-region of a Bitmap. For instance, if you have a 100x100 bitmap and you want to draw the 50x50 top-right corner, you can use a width of 50px and a stride of 100px.
I suppose the question is about Android, java, not windows! In this case, stride has nothing to do with "number of bytes used for storing one image row", that is a windows nomenclature.
Before you understand the parameter "stride", you need to know that getPixels is a function copying pixels from the source Bitmap to destination array ( which is typed int Pixels[]).
concerning copying, you need to know where is the source (to come from), and where is the destination (to come to), in function,
public void getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height) {
throw new RuntimeException("Stub!");
}
these 4 parameters control the source: int x, int y, int width, int height
these 3 parameters control the destination: int[] pixels, int offset, int stride
e.g. You have a sourceImage with width*height = 100*100Pixels, you make a destinationImage with width*height = 200*100Pixels,and You make the following codes,
sourceImage.getPixels(pixels, 0, 2*wd, 0, 0, wd, ht); // No.1 copying
sourceImage.getPixels(pixels, wd, 2*wd, 0, 0, wd, ht);// No.2 copying
destinationImage = Bitmap.createBitmap(pixels, 0, 2*wd, 2*wd, ht, Bitmap.Config.ARGB_8888); // make a big image twice the size of the original
Explanation is given as follows for No.1 copying getPixels,
1 line reading: with line width = wd, and put it into Pixels[0]~Pixels[wd-1];
2 line reading: put it into Pixels[stride+0]~Pixels[stride+wd-1];
nth line reading: put it into Pixels[(n-1)*stride]~Pixels[(n-1)*stride+wd-1].
That is pretty much of the getPixels.
Here is a good explanation from Microsoft about what stride generally is in images. So, in plain English, it defines for how many steps will the computer scan image data until it assumes that it is on a next line.
I also believe that @Romain Guy's example would also require to set x = 50 and height = 50 if I understand it correctly.
© 2022 - 2024 — McMap. All rights reserved.