any can explain the function of stride in bitmapdata?
Asked Answered
F

5

9
        Bitmap bit1 = new Bitmap( bmpimg , width , height );
        Bitmap bit2 = new Bitmap( bmp , width , height );

        Bitmap bmpresult = new Bitmap( width , height );

        BitmapData data1 = bit1.LockBits( new Rectangle( 0 , 0 , bit1.Width , bit1.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData data2 = bit2.LockBits( new Rectangle( 0 , 0 , bit2.Width , bit2.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );
        BitmapData data3 = bmpresult.LockBits( new Rectangle( 0 , 0 , bmpresult.Width , bmpresult.Height ) , ImageLockMode.ReadWrite , PixelFormat.Format24bppRgb );

unsafe
        {
            int remain1 = data1.Stride - data1.Width * 3;
            int remain2 = data2.Stride - data2.Width * 3;
            int remain3 = data3.Stride - data3.Width * 3;


            byte* ptr1 = ( byte* )data1.Scan0;
            byte* ptr2 = ( byte* )data2.Scan0;
            byte* ptr3 = ( byte* )data3.Scan0;

            for( int i = 0 ; i < height ; i ++ )
            {
                for( int j = 0 ; j < width * 3 ; j ++ )
                {
                    ptr3[ 0 ] = ( byte ) ( XOR_Operator( ptr1[ 0 ] , ptr2[ 0 ] ) );
                    ptr1 ++;
                    ptr2 ++;
                    ptr3 ++;
                }

                ptr1 += remain1;
                ptr2 += remain2;
                ptr3 += remain3;
            }


        }

        bit1.UnlockBits( data1 );
        bit2.UnlockBits( data2 );
        bmpresult.UnlockBits( data3 );

        return bmpresult;
    }

is it necessary to get remain for data objects

Fateful answered 5/1, 2010 at 7:34 Comment(1)
in the above given code is it necessary to use data.stride what is its basic purpose? i hope u can understand nowFateful
K
10

Stride is there due to hardware requirements that have unfortunately leaked through to the API layer.

It is important because Windows drivers sometimes require that scanlines (rows in the image) be memory aligned. That is why they are sometimes larger than they would strictly need to be.

See for example this MSDN article DIBs and Their Use.

Every scanline is DWORD-aligned. The scanline is buffered to alignment; the buffering is not necessarily 0.

Your handling seems to be sufficient.

Kerplunk answered 5/1, 2010 at 7:44 Comment(3)
I don't think it's a hardware requirement that's leaked through because stride existed before there were graphics hardware that could do acceleration (blitting). If I remember correctly it was more to do with optimising graphic performance on early PCs where all graphic operations were done by the CPU. The obvious reason is to make pixel indexing easier - make stride a power of 2 bytes and you eliminate a multiply (which used to be slow)Divider
Also, early graphics cards used bit planes to store the frame buffer which meant the easiest way to store images was to pad them to multiple of eight pixel wide images. Be thankful you no longer need to worry about bit plane based hardware.Divider
Woah. This is important information. So, the only way to correctly get the actual pixel data on it is to take the image width and multiply it by the bytes per pixel, then?Lafond
A
8

Stride is the number of bytes your code must iterate past to reach the next vertical pixel.

This may be different from the image's width * pixel size if hardware requires a width of a certain multiple.

Anana answered 5/1, 2010 at 7:47 Comment(1)
Surely that should be bytes to iterate instead of pixels since the offset from the start of one scanline to the next is not necessarily a multiple of the size of a pixel. For example, a 24bit image which is 3 pixels wide uses nine bytes per image row but it could be padded to ten (3 1/3 pixels) or twelve bytes (4 pixels).Divider
D
3

Scanlines are typically aligned on processor word boundaries.

http://javaboutique.internet.com/tutorials/rasters/index2.html has a nice diagram.

People implementing CPU-accessible bitmaps want to align their scanlines on processor word boundaries as the machine codes for accessing and manipulating processor words can be substantially faster than those for non-aligned addresses.

Disinfectant answered 5/1, 2010 at 7:38 Comment(1)
The diagram link is brokenImperator
S
1

Yes, it's necessary.

The stride value is the offset from the start of one scan line to the start of the next scan line. If the scan lines are padded, the value is a few bytes more than what's needed for the pixels in the scan line.

If the bitmap is stored upside down in memory (i.e. the bottom scan line first), the stride value is negative. If you would read such a bitmap without using the stride value, you would get just garbage after the first scan line, or a memory access error.

Sigismondo answered 5/1, 2010 at 8:14 Comment(0)
O
1

The stride value is the number of bytes that the bitmap takes to represent one row of pixels. so you could move a memory pointer forward by the stride to move down a row

Octopus answered 5/1, 2010 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.