Beispiel #1
0
        /**
         * Merges colors represented as 32-bit integers with an array of bits
         * from a given file.
         */
        private static int[] Merge(BitArray bits, Tour tour, int[] pixels)
        {
            //Clone pixels to prevent destruction.
            var newPixels = (int[]) pixels.Clone();

            Benchmark mark = new Benchmark("Time to Generate Tour", true);

            Square start = tour.CreateTour();
            Square current = start;

            mark.Time();

            mark = new Benchmark("Time to Modify Pixel Channels", true);

            for (int n = 0, length = bits.Length; n < length; n += 3)
            {
                //Calculate the bit depth to use.
                int depth = n/newPixels.Length;

                //Select the pixel to operate on and get the color channels.
                int index = Util.ToIndex(tour.Width, current.X, current.Y);
                int pixel = newPixels[index];

                //Get channel values and create a bool representation.
                int r = (pixel >> 16) & 0xFF;
                int g = (pixel >> 8) & 0xFF;
                int b = pixel & 0xFF;

                bool red = bits[n];
                bool green = n + 1 < length && bits[n + 1];
                bool blue = n + 2 < length && bits[n + 2];

                //Set pixel channel values based on the above bools.
                if (red) r = r | 1 << depth;
                else r = r & ~(1 << depth);

                if (green) g = g | 1 << depth;
                else g = g & ~(1 << depth);

                if (blue) b = b | 1 << depth;
                else b = b & ~(1 << depth);

                newPixels[index] = Util.BitsToColor(r, g, b);

                //Select the next pixel to manipulate.
                current = current.Next ?? start;
            }

            mark.Time();

            return newPixels;
        }