private Bitmap generate() { if (R == null) { return(null); } int cols = R.GetLength(0); int rows = R.GetLength(1); Bitmap dst = new Bitmap(cols * blockSize, rows * blockSize); BitmapData dstData = dst.LockBits(MyF.bound(dst), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); double Rcount = cols * rows - 1;// to let grogress% > 1/2 double progressSize = cols * rows * 2; for (int j = 0; j < rows; j++) { for (int i = 0; i < cols; i++) { Rcount++; if (monitor != null) { monitor.OnValueChanged(new ValueEventArgs() { value = Rcount / progressSize }); } R[i, j].draw(dstData, i * blockSize, j * blockSize); } } dst.UnlockBits(dstData); return(dst); }
public Bitmap merge(CodingMethod method) { int width = 0; int height = 0; Bitmap dst = null; for (int k = 7; k >= 0; k--) {// try to find output size if (this[k] != null) { width = Math.Max(width, this[k].Width); height = Math.Max(height, this[k].Height); } } if ((width == 0) || (height == 0)) { return(dst); } dst = new Bitmap(width, height); BitmapData[] srcDatas = bitPlanes.ToArray().Select(x => lockBimap(x, ImageLockMode.ReadOnly)).ToArray(); BitmapData dstData = dst.LockBits(MyF.bound(dst), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); unsafe { byte *dstPtr = (byte *)dstData.Scan0; int skipByte = dstData.Stride - dstData.Width * 3; for (int j = 0; j < dstData.Height; j++) { for (int i = 0; i < dstData.Width; i++) { byte srcByte = getByte(srcDatas, i, j); switch (method) { case CodingMethod.GrayCode: srcByte = gray2Bin(srcByte); break; case CodingMethod.Binary: default: break; } dstPtr[0] = srcByte; dstPtr[1] = dstPtr[0]; dstPtr[2] = dstPtr[0]; dstPtr += 3; } dstPtr += skipByte; } } dst.UnlockBits(dstData); for (int i = 0; i < 8; i++) { if (bitPlanes[i] == null) { continue; } bitPlanes[i].UnlockBits(srcDatas[i]); } return(dst); }
public Bitmap decode(Bitmap src) { if (file == null) { return(null); } if (src == null) { return(null); } if (monitor != null) { monitor.start(); } BitmapData srcData = src.LockBits(MyF.bound(src), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); buildD(srcData); src.UnlockBits(srcData); Bitmap output = map(); if (monitor != null) { monitor.fine(); } return(output); }
private BitmapData lockBimap(Bitmap src, ImageLockMode mode) { if (src == null) { return(null); } else { return(src.LockBits(MyF.bound(src), mode, PixelFormat.Format24bppRgb)); } }
public void encode(Bitmap src) { if (src == null) { return; } if (activeFrom != null) { activeFrom.BeginInvoke(new formChange(setEnable), false); } if (monitor != null) { monitor.start(); } BitmapData srcData = src.LockBits(MyF.bound(src), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); Thread threadR = new Thread(new ThreadStart(new Action(() => { buildR(srcData); }))); Thread threadD = new Thread(new ThreadStart(new Action(() => { buildD(srcData); }))); threadD.Start(); threadR.Start(); threadR.Join(); threadD.Join(); src.UnlockBits(srcData); if (monitor != null) { monitor.OnValueChanged(new ValueEventArgs() { value = 0.1 }); } findMatch(out file); if (monitor != null) { monitor.fine(); } if (activeFrom != null) { activeFrom.BeginInvoke(new formChange(saveFile), true); } }
public static void setPixel(BitmapData data, int x, int y, double[] value) { byte[] input = MyF.boundP(value); if (x < 0 || x >= data.Width || y < 0 || y >= data.Height) { return; } else { unsafe { byte *target = (byte *)data.Scan0; target += y * data.Stride + x * 3; target[0] = input[0]; target[1] = input[1]; target[2] = input[2]; } return; } }
public static Bitmap[] getBitPlane(Bitmap src, CodingMethod method) { Bitmap[] planes = new Bitmap[8]; if (src == null) { return(planes); } for (int i = 0; i < 8; i++) { planes[i] = new Bitmap(src.Width, src.Height); //Debug.Print(BitMask[i].ToString() + " "); } BitmapData srcData = src.LockBits(MyF.bound(src), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData[] dstDatas = planes.ToArray().Select(x => x.LockBits(MyF.bound(x), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb)).ToArray(); if (srcData == null) { Debug.Print("no srcData"); return(planes); } unsafe { int skipByte = srcData.Stride - srcData.Width * 3; //Debug.Print(" " + skipByte + " " + srcData.Stride + " " + srcData.Width); //int skipBit = dstDatas[0].Stride - dstDatas[0].Width; byte * srcPtr = (byte *)srcData.Scan0; byte *[] dstPtrs = new byte *[8]; for (int i = 0; i < 8; i++) { dstPtrs[i] = (byte *)dstDatas[i].Scan0; } for (int j = 0; j < srcData.Height; j++) { for (int i = 0; i < srcData.Width; i++) { for (int k = 7; k >= 0; k--) { //Debug.Print(" " + i + " " + j + " " + k); byte srcByte = srcPtr[0]; switch (method) { case CodingMethod.GrayCode: srcByte = bin2Gray(srcByte); break; case CodingMethod.Binary: default: break; } if ((BitMask[k] & srcByte) != 0) { byte *target = dstPtrs[k]; target[0] = 255; target[1] = 255; target[2] = 255; } dstPtrs[k] += 3; } srcPtr += 3; } srcPtr += skipByte; for (int k = 7; k >= 0; k--) { dstPtrs[k] += skipByte; } } } src.UnlockBits(srcData); for (int i = 0; i < 8; i++) { planes[i].UnlockBits(dstDatas[i]); } return(planes); }