Exemple #1
0
        internal unsafe static Bitmap BlendImages(Bitmap start, Bitmap end, double blend, bool parallel, out TimeSpan time)
        {
            // Validate parameters
            if (start.Width != end.Width || start.Height != end.Height) 
                throw new ArgumentException("The sizes of images do not match.");
            if (blend < 0 || blend > 1) 
                throw new ArgumentOutOfRangeException("blend", blend, "Must be in the range [0.0,1.1].");

            // Create the output image
            int width = start.Width, height = start.Height;
            Bitmap output = new Bitmap(width, height);
            var sw = new Stopwatch();

            // Blend the input images into the output
            using (FastBitmap fastOut = new FastBitmap(output))
            using (FastBitmap fastStart = new FastBitmap(start))
            using (FastBitmap fastEnd = new FastBitmap(end))
            {
                if (parallel)
                {
                    // Blend the images in parallel
                    sw.Restart();
                    Parallel.For(0, height, j =>
                    {
                        PixelData* outPixel = fastOut.GetInitialPixelForRow(j);
                        PixelData* startPixel = fastStart.GetInitialPixelForRow(j);
                        PixelData* endPixel = fastEnd.GetInitialPixelForRow(j);

                        for (int i = 0; i < width; i++)
                        {
                            // Blend the input pixels into the output pixel
                            outPixel->R = (byte)((startPixel->R * blend) + .5 + (endPixel->R * (1 - blend))); // .5 for rounding
                            outPixel->G = (byte)((startPixel->G * blend) + .5 + (endPixel->G * (1 - blend)));
                            outPixel->B = (byte)((startPixel->B * blend) + .5 + (endPixel->B * (1 - blend)));

                            outPixel++;
                            startPixel++;
                            endPixel++;
                        }
                    });
                    sw.Stop();
                }
                else
                {
                    // Blend the images sequentially
                    sw.Restart();
                    for(int j=0; j<height; j++)
                    {
                        PixelData* outPixel = fastOut.GetInitialPixelForRow(j);
                        PixelData* startPixel = fastStart.GetInitialPixelForRow(j);
                        PixelData* endPixel = fastEnd.GetInitialPixelForRow(j);

                        for (int i = 0; i < width; i++)
                        {
                            // Blend the input pixels into the output pixel
                            outPixel->R = (byte)((startPixel->R * blend) + .5 + (endPixel->R * (1 - blend))); // .5 for rounding
                            outPixel->G = (byte)((startPixel->G * blend) + .5 + (endPixel->G * (1 - blend)));
                            outPixel->B = (byte)((startPixel->B * blend) + .5 + (endPixel->B * (1 - blend)));

                            outPixel++;
                            startPixel++;
                            endPixel++;
                        }
                    }
                    sw.Stop();
                }
            }

            // Return the new image
            time = sw.Elapsed;
            return output;
        }
Exemple #2
0
        internal unsafe Bitmap BlendImages(Bitmap start, Bitmap end, double blend, bool parallel)
        {
            // Validate parameters
            if (start.Width != end.Width || start.Height != end.Height)
            {
                throw new ArgumentException("The sizes of images do not match.");
            }
            if (blend < 0 || blend > 1)
            {
                throw new ArgumentOutOfRangeException("blend", blend, "Must be in the range [0.0,1.1].");
            }

            // Create the output image
            int    width = start.Width, height = start.Height;
            Bitmap output = new Bitmap(width, height);

            // Blend the input images into the output
            using (FastBitmap fastOut = new FastBitmap(output))
                using (FastBitmap fastStart = new FastBitmap(start))
                    using (FastBitmap fastEnd = new FastBitmap(end))
                    {
                        if (parallel)
                        {
                            // Blend the images in parallel
                            Parallel.For(0, height, j =>
                            {
                                PixelData *outPixel   = fastOut.GetInitialPixelForRow(j);
                                PixelData *startPixel = fastStart.GetInitialPixelForRow(j);
                                PixelData *endPixel   = fastEnd.GetInitialPixelForRow(j);

                                for (int i = 0; i < width; i++)
                                {
                                    // Blend the input pixels into the output pixel
                                    outPixel->R = (byte)((startPixel->R * blend) + .5 + (endPixel->R * (1 - blend))); // .5 for rounding
                                    outPixel->G = (byte)((startPixel->G * blend) + .5 + (endPixel->G * (1 - blend)));
                                    outPixel->B = (byte)((startPixel->B * blend) + .5 + (endPixel->B * (1 - blend)));

                                    outPixel++;
                                    startPixel++;
                                    endPixel++;
                                }
                            });
                        }
                        else
                        {
                            // Blend the images sequentially
                            for (int j = 0; j < height; j++)
                            {
                                PixelData *outPixel   = fastOut.GetInitialPixelForRow(j);
                                PixelData *startPixel = fastStart.GetInitialPixelForRow(j);
                                PixelData *endPixel   = fastEnd.GetInitialPixelForRow(j);

                                for (int i = 0; i < width; i++)
                                {
                                    // Blend the input pixels into the output pixel
                                    outPixel->R = (byte)((startPixel->R * blend) + .5 + (endPixel->R * (1 - blend))); // .5 for rounding
                                    outPixel->G = (byte)((startPixel->G * blend) + .5 + (endPixel->G * (1 - blend)));
                                    outPixel->B = (byte)((startPixel->B * blend) + .5 + (endPixel->B * (1 - blend)));

                                    outPixel++;
                                    startPixel++;
                                    endPixel++;
                                }
                            }
                        }
                    }
            return(output);
        }