Is there a way to create binary diff for two byte arrays in C # .net?

I am trying to compare two byte arrays in memory and create a data structure to store the differences, so with the byte array B and the data structure containing the differences, you can recreate the byte array A.

The size of byte arrays is always the same and relatively small. Byte arrays represent raster images, which typically have sizes from 1000x1000x32 to 128x128x32.

The speed and efficiency (versus time pp cpu), at which data-structure-then-hold-the-difference and a second byte array can be combined / used to restore byte array A, are of the highest importance. It is not so important that the generation of a difference object is equally effective.

Currently, my solution will be to use the binary search hashing method + md5 to build a list of the smallest units of difference within the image and package the raw byte data with reference to its offset inside the byte array A.

those. I generate a hash for the Image A byte array and compare it with the Image B byte array hash, if it does not match, I horizontally divide the image in half and hashes each block, then compares these hashes between the images, this process then repeats recursively until all the blocks that do not match; minimum size achieved, such as 32x32x32 and / or maximum number of partitions. Once a block is marked as matching, it is no longer part of the recursive search. As soon as all differences are identified, they will be added to the list of changes, and this list will be a difference object.

Are there any built-in ways to effectively get the results I'm looking for? Or are there libraries that could do the job?


Notes:

  • This is a training project (for WCF, WPF and a number of other technologies),
  • VNC, .
  • , A , , , , / 30 + .
  • 3 fps + , .
+3
3

- , - . , , "" diff, , A. , , .

, , 1 , diff, 5 ( , Int32 ) 1 , B A. O (n) diff O (m) , m - . , , - .

, - :

Diff GetDiff(byte[] a, byte[] b)
{
    Diff diff = new Diff();
    for (int i = 0; i < a.Length; i++)
    {
        if (a[i] != b[i])
        {
            diff.Points.Add(new DiffPoint(i, a[i]));
        }
    }
    return diff;
}

// Mutator method - turns "b" into the original "a"
void ApplyDiff(byte[] b, Diff diff)
{
    foreach (DiffPoint point in diff.Points)
    {
        b[point.Index] = point.Value;
    }
}

// Copy method - recreates "a" leaving "b" intact
byte[] ApplyDiffCopy(byte[] b, Diff diff)
{
    byte[] a = new byte[b.Length];
    int startIndex = 0;
    foreach (DffPoint point in diff.Points)
    {
        for (int i = startIndex; i < point.Index; i++)
        {
            a[i] = b[i];
        }
        a[point.Index] = point.Value;
        startIndex = point.Index + 1;
    }
    for (int j = startIndex; j < b.Length; j++)
    {
        a[j] = b[j];
    }
    return a;
}

struct DiffPoint
{
    public int Index;
    public byte Value;

    public DiffPoint(int index, byte value) : this()
    {
        this.Index = index;
        this.Value = value;
    }
}

class Diff
{
    public Diff()
    {
        Points = new List<DiffPoint>();
    }

    public List<DiffPoint> Points { get; private set; }
}

ApplyDiffCopy, , , . , B, ApplyDiff , .

, , . ( ..).

, , ApplyDiff .

+7

Crikey! - , XOR - , , - , RLE.

+2

BitArray ( Reflector, , , , )

        byte[] s1 = new byte[] {0,1,2,3,4,5,6};
        byte[] s2 = new byte[] {0,1,2,4,4,6,6};
        var orig1 = new BitArray(s1);
        var orig2 = new BitArray(s2);
        var diff = orig1.Xor(orig2);
        byte[] diffArray = new byte[s1.Length];
        diff.CopyTo(diffArray, 0); // here we have a byte diff array of s1 and s2

        var reconstruct = orig2.Xor(diff);
        reconstruct.CopyTo(diffArray, 0); // diffArray is now the same as s1
0

Source: https://habr.com/ru/post/1725491/


All Articles