private bool encryptionFeasible(FileModel fileModel, IImageWrapper container) { var spread = this.getByteSpread(); var numberOfChannels = Enum.GetValues(typeof(LosslessFileMuxxer.ByteOrder)).GetLength(0); var totalBytesInImage = container.Width * container.Height * numberOfChannels; var bytesAvailableForData = totalBytesInImage - EncryptionHeader.GetHeaderLength(); var bytesNeededForData = fileModel.FileContents.Length * spread; return(bytesAvailableForData >= bytesNeededForData); }
private FileModel demuxxImage(Pixel[,] source) { FileModel fileModel = null; var numberOfChannels = Enum.GetValues(typeof(LosslessFileMuxxer.ByteOrder)).GetLength(0); var sourceWidth = source.GetLength(0); var sourceHeight = source.GetLength(1); int byteSpread = this.getByteSpread(); int bitShiftCount = (BITS_PER_CHANNEL - BITS_USED_PER_CHANNEL); int maxValue = (0x01 << BITS_PER_CHANNEL) - 0x01; int partMask = maxValue >> bitShiftCount; var spreadBytes = new byte[sourceWidth * sourceHeight * byteSpread]; var spreadIndex = 0; var channelIndex = 0; for (var x = 0; x < source.GetLength(0); ++x) { for (var y = 0; y < source.GetLength(1); ++y) { Pixel p = source [x, y]; for (var c = 0; c < numberOfChannels; ++c) { var channel = (ByteOrder)channelIndex; byte valueToUse; switch (channel) { case ByteOrder.Red: valueToUse = (byte)(p.R & partMask); break; case ByteOrder.Green: valueToUse = (byte)(p.G & partMask); break; case ByteOrder.Blue: valueToUse = (byte)(p.B & partMask); break; default: throw new NotImplementedException(); } spreadBytes [spreadIndex] = valueToUse; ++spreadIndex; channelIndex = channelIndex == numberOfChannels - 1 ? 0 : channelIndex + 1; } } } var unspreadBytes = new byte[sourceWidth * sourceHeight]; // don't yet know how many of these are useful yet var unspreadIndex = 0; spreadIndex = 0; do { byte b = 0x00; for (var i = 0; i < byteSpread; ++i) { var s = spreadBytes[spreadIndex]; var spreadShift = i * BITS_USED_PER_CHANNEL; var shifted = s << spreadShift; b += (byte)shifted; ++spreadIndex; } unspreadBytes[unspreadIndex] = b; ++unspreadIndex; }while(spreadIndex < spreadBytes.Length); var headerBytes = new byte[EncryptionHeader.GetHeaderLength()]; for (var i = 0; i < headerBytes.Length; ++i) { var h = unspreadBytes[i]; headerBytes [i] = h; } var header = new EncryptionHeader(headerBytes); var fileBytes = unspreadBytes.Skip(headerBytes.Length).Take(header.FileSize).ToArray(); var ms = new MemoryStream(fileBytes); ms.Position = 0; fileModel = new FileModel(ms, header.FileName); return(fileModel); }