public BinaryPixelBitmap(UnsafeBitmap bm, int xStart, int yStart, int xWidth, int yHeight, BinPixelConvertType convertType, Color?refColor) { // If refColor==null then sets convertType to ColorIsOne and refColor to black // Otherwise if convertType is ColorIsOne then if color matches refColor it is a 1 and all others are 0 // If convertType is ColorIsZero then if color matches refColor then it is a 0 and all others are 1 height = yHeight; columns = new int[xWidth]; int temp; if (refColor == null) { convertType = BinPixelConvertType.ColorIsOne; refColor = Color.FromArgb(0, 0, 0); // Black } if (convertType == BinPixelConvertType.ColorIsOne) { for (int x = xStart; x < xStart + xWidth; x++) { temp = 0; for (int y = yStart; y < (yStart + yHeight) && y < (yStart + 16); y++) { if (bm.GetPixel(x, y) == refColor) { temp |= 1 << (y - yStart); } } columns[x - xStart] = temp; } } else { for (int x = xStart; x < xStart + xWidth; x++) { temp = 0; for (int y = yStart; y < (yStart + yHeight) && y < (yStart + 16); y++) { if (bm.GetPixel(x, y) != refColor) { temp |= 1 << (y - yStart); } } columns[x - xStart] = temp; } } }
public int[] CloneAsBin(Rectangle rect, BinPixelConvertType convertType, Color?refColor) { int yHeight = rect.Height; if (yHeight > 32) { return(null); } int xWidth = rect.Width; int xStart = rect.Left; int yStart = rect.Top; // If refColor==null then sets convertType to ColorIsOne and refColor to black // Otherwise if convertType is ColorIsOne then if color matches refColor it is a 1 and all others are 0 // If convertType is ColorIsZero then if color matches refColor then it is a 0 and all others are 1 int[] columns = new int[xWidth]; if (refColor == null) { convertType = BinPixelConvertType.ColorIsOne; refColor = Color.FromArgb(0, 0, 0); // Black } PixelData refColorPD; refColorPD.red = refColor.Value.R; refColorPD.blue = refColor.Value.B; refColorPD.green = refColor.Value.G; byte *pBaseTemp; if (convertType == BinPixelConvertType.ColorIsOne) { for (int y = 0; y < yHeight; y++) { pBaseTemp = pBase + (y + yStart) * width + xStart * bytesPerPixel; for (int x = 0; x < xWidth; x++) { if (*pBaseTemp == refColorPD.blue && *(pBaseTemp + 1) == refColorPD.green && *(pBaseTemp + 2) == refColorPD.red) { columns[x] |= 1 << y; } pBaseTemp += bytesPerPixel; } } } else { for (int y = 0; y < yHeight; y++) { pBaseTemp = pBase + (y + yStart) * width + xStart * bytesPerPixel; for (int x = 0; x < xWidth; x++) { if (*pBaseTemp != refColorPD.blue && *(pBaseTemp + 1) != refColorPD.green && *(pBaseTemp + 2) != refColorPD.red) { columns[x] |= 1 << y; } pBaseTemp += bytesPerPixel; } } } return(columns); }