private byte[] colorDiff; // delta-encoded change in color, then RL-encoded #endregion Fields #region Constructors /// <summary> /// Creates a FrameDiff object as the difference between two frames. /// </summary> /// <param name="oldFrame"></param> /// <param name="newFrame"></param> internal MifFrameDiff(MifFrame oldFrame, MifFrame newFrame) { if (oldFrame == null) throw new ArgumentNullException("oldFrame"); if (newFrame == null) throw new ArgumentNullException("newFrame"); if (oldFrame.colorData != null && newFrame.colorData != null) { this.colorDiff = MifDeltaEncoding.Encode( oldFrame.colorData, newFrame.colorData, true); memoryUsed1 += colorDiff.Length; this.colorDiff = MifRunLengthEncoding.Encode(colorDiff, 2); memoryUsed2 += colorDiff.Length; } if (oldFrame.alphaData != null && newFrame.alphaData != null) { this.alphaDiff = MifDeltaEncoding.Encode( oldFrame.alphaData, newFrame.alphaData, true); memoryUsed1 += alphaDiff.Length; this.alphaDiff = MifRunLengthEncoding.Encode(alphaDiff, 1); memoryUsed2 += alphaDiff.Length; } }
/// <summary> /// Read a frame. /// </summary> public MifFrame ReadFrame(MifHeader header, MifFrame prevFrame) { MifFrame frame = new MifFrame(); // Read Delay field if present. if (header.Flags.HasFlag(MifFlags.HasDelay)) { frame.delay = ReadInt32(); } // Read primary channels. frame.colorData = new byte[2 * header.ImageWidth * header.ImageHeight]; if (prevFrame != null && prevFrame.colorData != null) { prevFrame.colorData.CopyTo(frame.colorData, 0); ReadPixelData(header, frame.colorData, false); } else { ReadPixelData(header, frame.colorData, true); } // Read alpha channel if present. if (header.Flags.HasFlag(MifFlags.HasAlpha)) { frame.alphaData = new byte[header.ImageWidth * header.ImageHeight]; if (prevFrame != null && prevFrame.alphaData != null) { prevFrame.alphaData.CopyTo(frame.alphaData, 0); ReadPixelData(header, frame.alphaData, false); } else { ReadPixelData(header, frame.alphaData, true); } } return frame; }
internal void UpdateFrame(MifFrame frame) { if (frame == null) throw new ArgumentNullException("frame"); if (frame.colorData != null && this.colorDiff != null) { byte[] actualColorDiff = MifRunLengthEncoding.Decode(this.colorDiff, 2); MifDeltaEncoding.Decode(frame.colorData, actualColorDiff, true); } if (frame.alphaData != null && this.alphaDiff != null) { byte[] actualAlphaDiff = MifRunLengthEncoding.Decode(this.alphaDiff, 1); MifDeltaEncoding.Decode(frame.alphaData, actualAlphaDiff, true); } }