Перемещение пикселей в отсортированном 4-битном растровом изображении - PullRequest
0 голосов
/ 02 марта 2020

Я получил задание от своего учителя преобразовать изображение в 4-битное растровое изображение на пиксель и отсортировать его с помощью вставки, а затем переместить пиксели по определенному шаблону. Мне удалось преобразовать его, но я не могу придумать способ сделать шаблон с ним.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Images
{
    class Program
    {
        static void Main(string[] args)
        {
            //Converting img from JPG to BMP
            var name = Path.GetFileNameWithoutExtension(args[0]);
            Bitmap image = new Bitmap(args[0]);
            image.Save(name + "_24bit.bmp", ImageFormat.Bmp);

            //BMP reading
            using (FileStream file = new FileStream(name + "_24bit.bmp", FileMode.Open, FileAccess.Read))
            {
                byte[] b = new byte[file.Length];
                file.Read(b, 0, (int)file.Length);
                int width = BitConverter.ToInt32(b, 0x0012); //width
                int height = BitConverter.ToInt32(b, 0x0016); //height
                int points = width * height;

                //Points to colour codes
                int[] bs = new int[points];
                int j = 54;
                for (int i = 0; i < bs.Length; i++)
                {
                    bs[i] = (((b[j + 2] << 8) + b[j + 1]) << 8) + b[j];
                    j += 3;
                }
                //




                bs = InsertionSort(bs);

                j = 54;
                for (int i = 0; i < bs.Length; i++)
                {
                    byte[] p = BitConverter.GetBytes(bs[i]);
                    b[j] = p[0];
                    b[j + 1] = p[1];
                    b[j + 2] = p[2];
                    j += 3;
                }
                using (FileStream file2 = new FileStream(name + "_sorted.bmp", FileMode.Create, FileAccess.Write))
                {
                    file2.Seek(0, SeekOrigin.Begin);
                    file2.Write(b);
                    file2.Close();
                }
                //

                var a = new byte[points * 2];               




                //Converting to 4bit
                //https://en.wikipedia.org/wiki/8-bit_color
                Array.Copy(BitConverter.GetBytes(54 + 4 * 16 + points), 0, b, 0x0002, 4); //File size in bytes
                Array.Copy(BitConverter.GetBytes(54 + 16 * 4), 0, b, 0x000A, 4); //Offset from beginning of file to the beginning of the bitmap data
                Array.Copy(BitConverter.GetBytes(4), 0, b, 0x001C, 2);    //Bits per Pixel used to store palette entry information. This also identifies in an indirect way the number of possible colors. Possible values are:
                Array.Copy(BitConverter.GetBytes(0x100), 0, b, 0x002E, 4);  //Number of actually used colors. For a 8-bit / pixel bitmap this will be 100h or 256.

                //16 color table
                for (int i = 0; i < 4; i++) //red
                {
                    for (j = 0; j < 2; j++) //green
                    {
                        for (int k = 0; k < 2; k++) //blue
                        {
                            int l = ((i << 2) + (j << 1) + k) * 4;
                            a[l + 0] = (byte)(k << 7); //blue
                            a[l + 1] = (byte)(j << 7); //green
                            a[l + 2] = (byte)(i << 6); //red
                            a[l + 3] = 0;
                        }
                    }
                }
                //Recalculating points
                j = points / 2 + 16 * 4;
                for (int i = b.Length - 6; i > 53; i -= 6)
                {
                    j--;
                    a[j] =
                        (byte)(
                        ((b[i + 2] >> 6) << 2) | //red
                        ((b[i + 1] >> 7) << 1) | //green
                        ((b[i + 0] >> 7) << 0) | //blue
                        ((b[i + 5] >> 6) << 6) | //red
                        ((b[i + 4] >> 7) << 5) | //green
                        ((b[i + 3] >> 7) << 4) //red
                        );
                }

                using (FileStream file2 = new FileStream(name + "_4bit.bmp", FileMode.Create, FileAccess.Write))
                {
                    file2.Seek(0, SeekOrigin.Begin);
                    file2.Write(b, 0, 54);
                    file2.Write(a, 0, points / 2 + 16 * 4);
                    file2.Close();
                }
                //
                file.Close();
                //
            }

            static int[] InsertionSort(int[] inputArray)
            {
                for (int i = 0; i < inputArray.Length - 1; i++)
                {
                    for (int j = i + 1; j > 0; j--)
                    {
                        if (inputArray[j - 1] > inputArray[j])
                        {
                            int temp = inputArray[j - 1];
                            inputArray[j - 1] = inputArray[j];
                            inputArray[j] = temp;
                        }
                    }
                }
                return inputArray;
            }
        }
    }
}

Мне нужен шаблон, подобный следующему: i.gyazo.com/2aae9ac67225597a56bbae0ce1e621e8.png

Я получаю это: i.gyazo.com/d433edea1dcdb5463e7869dd0ddd0762.png*100

...