简体   繁体   中英

Lockbits stride on 1bpp Indexed image byte boundaries

I am cutting and pasting from one 1bpp indexed image to a new image.

All works well until the starting pixel is a divisor of 8. In the code below stride is equal to a value relative to the width of the rectangle until I hit a byte boundary. Then the stride is equal to the width of the entire page.

var croppedRect = new Rectangle((int)left, (int)top, (int)width, (int)height);
BitmapData croppedSource = _bitmapImage.LockBits(croppedRect, ImageLockMode.ReadWrite, BitmapImage.PixelFormat);
int stride = croppedSource.Stride;

This is a problem because rather than pasting my selected area into the new image, the Marshal copies a cross section, the height of the selected area, of the entire width of the page.

int numBytes = stride * (int)height;
var srcData = new byte[numBytes];

Marshal.Copy(croppedSource.Scan0, srcData, 0, numBytes);
Marshal.Copy(srcData, 0, croppedDest.Scan0, numBytes);
destBmp.UnlockBits(croppedDest);

Here's my code for anyone who's interested. There may be a more optimal solution but this works. I am creating an entire page in white and duplicating the selected area in the new page as I pass over it. Thanks to Bob Powell for the SetIndexedPixel routine.

protected int GetIndexedPixel(int x, int y, BitmapData bmd)
{
    var index = y * bmd.Stride + (x >> 3);
    var p = Marshal.ReadByte(bmd.Scan0, index);
    var mask = (byte)(0x80 >> (x & 0x7));
    return p &= mask;
}

protected void SetIndexedPixel(int x, int y, BitmapData bmd, bool pixel)
{
    int index = y * bmd.Stride + (x >> 3);
    byte p = Marshal.ReadByte(bmd.Scan0, index);
    byte mask = (byte)(0x80 >> (x & 0x7));
    if (pixel)
        p &= (byte)(mask ^ 0xff);
    else
        p |= mask;
    Marshal.WriteByte(bmd.Scan0, index, p);
}

public DocAppImage CutToNew(int left, int top, int width, int height, int pageWidth, int pageHeight)
{
    var destBmp = new Bitmap(pageWidth, pageHeight, BitmapImage.PixelFormat);
    var pageRect = new Rectangle(0, 0, pageWidth, pageHeight);

    var pageData = destBmp.LockBits(pageRect, ImageLockMode.WriteOnly, BitmapImage.PixelFormat);
    var croppedRect = new Rectangle(left, top, width, height);
    var croppedSource = BitmapImage.LockBits(croppedRect, ImageLockMode.ReadWrite, BitmapImage.PixelFormat);

    for (var y = 0; y < pageHeight; y++)
        for (var x = 0; x < pageWidth; x++)
        {
            if (y >= top && y <= top + height && x >= left && x <= width + left)
            {
                SetIndexedPixel(x, y, pageData,
                                GetIndexedPixel(x - left, y - top, croppedSource) == 0 ? true : false);
                SetIndexedPixel(x - left, y - top, croppedSource, false); //Blank area in original
            }
            else
                SetIndexedPixel(x, y, pageData, false);  //Fill the remainder of the page with white.
        }

    destBmp.UnlockBits(pageData);

    var retVal = new DocAppImage { BitmapImage = destBmp };
    destBmp.Dispose();

    BitmapImage.UnlockBits(croppedSource);
    SaveBitmapToFileImage(BitmapImage);

    return retVal;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM