Skip to main content
4 of 4
added 4 characters in body
LittleBit
  • 1.2k
  • 7
  • 17

What is the hidden quote?

"Three may keep a secret, if two of them are dead."

Which object is on the line?

Probably the Key

https://postimg.cc/YGbnLCFt

C# Code

ImageProcessing

internal static class ImageProcessing
{
    public static IReadOnlyList<bool> GetBits(Bitmap image)
    {
        var bits = new List<bool>();

        // row scan
        for (int y = 0; y < image.Height; y++)
        {
            for (int x = 0; x < image.Width; x++)
            {
                var pixel = image.GetPixel(x, y);
                bits.Add((pixel.R & 1) != 0);
                bits.Add((pixel.G & 1) != 0);
                bits.Add((pixel.B & 1) != 0);
            }
        }

        return bits;
    }

    public static Bitmap GetImage(IEnumerable<bool> bits, int width, int height)
    {
        var image = new Bitmap(width, height);
        var queue = new Queue<bool>(bits);

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                var pixel = queue.Dequeue() ? 255 : 0;
                image.SetPixel(x, y, Color.FromArgb(pixel, pixel, pixel));
            }
        }

        return image;
    }
}

DeSerialization

internal static class DeSerializer
{
    public static string BitsToText(IReadOnlyList<bool> bits)
    {
        var length = bits
            .Skip(2)
            .GetBytes(2)
            .GetUshort();

        var bytes = bits
            .Skip(2 + 16) // skip header
            .GetBytes(length);

        return Encoding.ASCII.GetString(bytes);
    }

    public static Bitmap BitsToImage(IReadOnlyList<bool> bits)
    {
        var width = bits
            .Skip(2)
            .GetBytes(2)
            .GetUshort();

        var height = bits
            .Skip(2 + 16)
            .GetBytes(2)
            .GetUshort();

        return ImageProcessing.GetImage(bits.Skip(2 + 16 + 16), width, height);
    }

    private static byte[] GetBytes(this IEnumerable<bool> bits, int length)
    {
        return bits
        .Take(length * 8)
        .Chunk(8)
        .Select(values =>
        {
            return (byte)values
            .Reverse() // MSB is first, reverse to LSB first
            .Select((bit, index) => bit ? (int)Math.Pow(2, index) : 0)
            .Sum();
        })
        .ToArray();
    }

    private static int GetUshort(this byte[] bytes)
    {
        return bytes
            .Reverse() // MSByte is first, revrse for LSByte first
            .Select((value, index) => value * (int)Math.Pow(2, index * 8))
            .Sum();
    }
}