public static HeaderStruct FromBytes(byte[] bytearray) { HeaderStruct obj = new HeaderStruct(); int len = Marshal.SizeOf(obj); IntPtr i = Marshal.AllocHGlobal(len); Marshal.Copy(bytearray, 0, i, len); obj = (HeaderStruct)Marshal.PtrToStructure(i, obj.GetType()); Marshal.FreeHGlobal(i); return(obj); }
public HeaderStruct ReadHeader(BitArray pixelData, out int pixelPtr) { var dataPtr = 0; int pixelDataPtr = NextWritableBitHeader(-1); BitArray data = new BitArray(HeaderStruct.BitSize); while (dataPtr < data.Length) { data[dataPtr++] = pixelData[pixelDataPtr]; pixelDataPtr = NextWritableBitHeader(pixelDataPtr); } // finish off this pixel, then go to next pixel pixelPtr = (pixelDataPtr / 32) + 1 + 1; var bytes = new byte[HeaderStruct.BitSize / 8]; data.CopyTo(bytes, 0); return(HeaderStruct.FromBytes(bytes)); }
// Use this to ensure we only write to a rectangle within the image public bool IsWritablePixel(HeaderStruct header, int pixelPtr, int width, int height) { var x = pixelPtr % width; var y = pixelPtr / width; int minX = header.MinX; int maxX = header.MaxX; int minY = header.MinY; int maxY = header.MaxY; if (minX == maxX && minY == maxY) { return(true); // if not set, dont constrain at all } bool xInRange = (minX <= x) && (x <= maxX); bool yInRange = (minY <= y) && (y <= maxY); return(xInRange && yInRange); }
public BitArray WriteHeader(BitArray pixelData, HeaderStruct header, out int pixelPtr) { var bytes = header.ToBytes(); BitArray headerBits = new BitArray(bytes); var dataPtr = 0; int pixelDataPtr = NextWritableBitHeader(-1); while (dataPtr < headerBits.Length) { pixelData[pixelDataPtr] = headerBits[dataPtr++]; pixelDataPtr = NextWritableBitHeader(pixelDataPtr); } // finish off this pixel, then go to next pixel pixelPtr = (pixelDataPtr / 32) + 1 + 1; //Dump(pixelData, "Wrote header"); return(pixelData); }