Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawBitmap() and setPixels(): what's the stride?

Tags:

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?

like image 587
bigstones Avatar asked Jan 25 '11 18:01

bigstones


People also ask

What is stride in Android?

Stride is number of bytes used for storing one image row. Stride can be different from the image width.

How do I know if my Android BMP is empty?

You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.

What are bitmaps in Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.


3 Answers

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.

like image 158
Hoshin Avatar answered Sep 22 '22 03:09

Hoshin


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.

like image 35
Romain Guy Avatar answered Sep 20 '22 03:09

Romain Guy


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.

like image 1
SpaceSoftwares Avatar answered Sep 24 '22 03:09

SpaceSoftwares