开发者

any can explain the function of stride in bitmapdata?

        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 );
        Bit开发者_运维百科mapData 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


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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜