Lock() public method

public Lock ( ) : void
return void
        protected override Effect GetEffect(FastBitmap source)
        {
            // Get curves either from Curves collection or ACV file.
            CurveCollection curves = GetCurves();

            // Check that there are at least 4 curves.
            if (curves.Count < 4)
                throw new DynamicImageException(
                    "At least 4 curves (corresponding to Composite, Red, Green, Blue) must be specified.");

            // Convert mathematical curve definitions into 4x256 lookup texture (Composite, Red, Green, Blue are the 4 "columns").
            FastBitmap curvesLookup = new FastBitmap(4, 256);
            curvesLookup.Lock();
            for (int x = 0; x < 4; ++x)
            {
                IEnumerable<CurvePoint> points = curves[x].Points.Cast<CurvePoint>();
                float[] xValues = points.Select(p => (float) p.Input).ToArray();
                float[] yValues = points.Select(p => (float) p.Output).ToArray();
                float[] derivatives = CubicSplineUtility.CalculateSpline(xValues, yValues);
                for (int y = 0; y < 256; ++y)
                    curvesLookup[x, y] = Color.FromRgb((byte) CubicSplineUtility.InterpolateSpline(xValues, yValues, derivatives, y), 0, 0);
            }
            curvesLookup.Unlock();

            return new CurvesEffect
            {
                CurvesLookup = new ImageBrush(curvesLookup.InnerBitmap)
            };
        }
Example #2
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            Bitmap = new FastBitmap(Width, Height);
            Bitmap.Lock();

            for (int y = 0; y < Height; y++)
                for (int x = 0; x < Width; x++)
                {
                    ColorHsv colorHsv = CalculateFractalColor(x, y);
                    var color = (Color) colorHsv;
                    Bitmap[x, y] = color.ToWpfColor();
                }

            Bitmap.Unlock();
        }
Example #3
0
        protected override void CreateImage()
        {
            Bitmap = new FastBitmap(Width, Height);
            Bitmap.Lock();

            for (int y = 0; y < Height; y++)
                for (int x = 0; x < Width; x++)
                {
                    ColorHsv colourHsv = CalculateFractalColour(x, y);
                    Color colour = (Color) colourHsv;
                    Bitmap[x, y] = colour;
                }

            Bitmap.Unlock();
        }
Example #4
0
        protected override Effect GetEffect(FastBitmap source)
        {
            FastBitmap transferLookup = new FastBitmap(1, 256);
            transferLookup.Lock();
            for (int y = 0; y < 256; ++y)
            {
                byte colorComponent = (byte) (255 * GetTransferFunctionValue(y / 255.0f));
                transferLookup[0, y] = Color.FromRgb(colorComponent, colorComponent, colorComponent);
            }
            transferLookup.Unlock();

            return new TransferEffect
            {
                TransferLookup = new ImageBrush(transferLookup.InnerBitmap)
            };
        }
        public static void AssertEqual(FastBitmap expected, FastBitmap actual)
        {
            Assert.AreEqual(expected.Width, actual.Width);
            Assert.AreEqual(expected.Height, actual.Height);

            try
            {
                expected.Lock();
                actual.Lock();

                for (int y = 0, height = expected.Height; y < height; ++y)
                    for (int x = 0, width = expected.Width; x < width; ++x)
                        Assert.AreEqual(expected[x, y], actual[x, y]);
            }
            finally
            {
                actual.Unlock();
                expected.Unlock();
            }
        }
Example #6
0
        protected override Effect GetEffect(FastBitmap source)
        {
            Color transparentColor;
            if (UseFirstPixel)
            {
                source.Lock();
                transparentColor = source[0, 0];
                source.Unlock();
            }
            else
            {
                transparentColor = Color;
            }

            return new ColorKeyEffect
            {
                ColorTolerance = ColorTolerance / 255.0,
                TransparentColor = transparentColor
            };
        }
        protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
        {
            SWMColor transparentColor;
            if (UseFirstPixel)
            {
                source.Lock();
                transparentColor = source[0, 0];
                source.Unlock();
            }
            else
            {
                transparentColor = Color.ToWpfColor();
            }

            return new ColorKeyEffect
            {
                ColorTolerance = ColorTolerance / 255.0,
                TransparentColor = transparentColor
            };
        }
Example #8
0
        public override sealed void ApplyFilter(FastBitmap bitmap)
        {
            OnBeginApplyFilter(bitmap);

            // get destination dimensions
            int destWidth, destHeight;
            bool shouldContinue = GetDestinationDimensions(bitmap, out destWidth, out destHeight);
            if (!shouldContinue)
                return;

            FastBitmap destination = new FastBitmap(destWidth, destHeight);

            // copy metadata
            // TODO
            /*foreach (PropertyItem propertyItem in bitmap.InnerBitmap.PropertyItems)
                destination.InnerBitmap.SetPropertyItem(propertyItem);*/

            int width = bitmap.Width;
            int height = bitmap.Height;

            OriginalSpace = new Int32Rect(0, 0, width, height);
            Int32Rect transformedSpace = GetTransformedSpace(OriginalSpace);

            try
            {
                bitmap.Lock();
                destination.Lock();

                if (InterpolationMode == InterpolationMode.NearestNeighbor)
                    FilterPixelsNearestNeighbor(bitmap, destination, width, height, transformedSpace);
                else
                    FilterPixelsBilinear(bitmap, destination, width, height, transformedSpace);
            }
            finally
            {
                destination.Unlock();
                bitmap.Unlock();
            }

            bitmap.InnerBitmap = destination.InnerBitmap;
        }
Example #9
0
        private BitmapSource CreateImage()
        {
            ValidateParameters();

            // Set design-mode properties.
            foreach (Layer layer in this.VisibleLayers)
                {
                    layer.Site = this.Site;
                    layer.DesignMode = this.DesignMode;
                }

            // First, we process layers which have a specific size.
            foreach (Layer layer in this.VisibleLayers)
                if (layer.HasFixedSize)
                    layer.Process();

            // Second, for SizeMode = Auto, we calculate the output dimensions
            // based on the union of all layers' (which have an explicit size) dimensions.
            int outputWidth, outputHeight;
            if (this.AutoSize)
            {
                Int32Rect outputDimensions = Int32Rect.Empty;
                foreach (Layer layer in this.VisibleLayers)
                {
                    // Calculate dimensions of layers; the dimensions of some layers may be omitted
                    // at design time and are then derived from layers which have a fixed size.
                    // Only include layer in bounds calculation if the Anchor property is None.
                    if (layer.Anchor == AnchorStyles.None)
                    {
                        Int32Rect? bounds = layer.Bounds;
                        if (bounds != null)
                            outputDimensions = Int32RectUtility.Union(outputDimensions, bounds.Value);
                    }
                }
                outputWidth = outputDimensions.Width;
                outputHeight = outputDimensions.Height;
            }
            else
            {
                outputWidth = Width.Value;
                outputHeight = Height.Value;
            }

            // If at this point we don't have a valid size, return - this means that
            // layers without an explicit size will never force an image to be created.
            if (outputWidth == 0 && outputHeight == 0)
                return null;

            // Second, layers which don't have explicit sizes set, we now set their sizes
            // based on the overall composition size, and process the layer.
            foreach (Layer layer in this.VisibleLayers)
                if (!layer.HasFixedSize)
                {
                    layer.CalculatedWidth = outputWidth;
                    layer.CalculatedHeight = outputHeight;

                    layer.Process();
                }

            // If any of the layers are not present, we don't create the image
            foreach (Layer layer in this.VisibleLayers)
                if (layer.Bitmap == null)
                    return null;

            // Calculate X and Y for layers which are anchored.
            foreach (Layer layer in this.VisibleLayers)
            {
                if (layer.Bitmap != null && layer.Anchor != AnchorStyles.None)
                {
                    // Set X.
                    switch (layer.Anchor)
                    {
                        case AnchorStyles.BottomCenter :
                        case AnchorStyles.MiddleCenter :
                        case AnchorStyles.TopCenter :
                            layer.X = (outputWidth - layer.Size.Value.Width) / 2;
                            break;
                        case AnchorStyles.BottomLeft :
                        case AnchorStyles.MiddleLeft :
                        case AnchorStyles.TopLeft :
                            layer.X = layer.AnchorPadding;
                            break;
                        case AnchorStyles.BottomRight:
                        case AnchorStyles.MiddleRight:
                        case AnchorStyles.TopRight:
                            layer.X = outputWidth - layer.Size.Value.Width - layer.AnchorPadding;
                            break;
                    }

                    // Set Y.
                    switch (layer.Anchor)
                    {
                        case AnchorStyles.BottomCenter:
                        case AnchorStyles.BottomLeft:
                        case AnchorStyles.BottomRight:
                            layer.Y = outputHeight - layer.Size.Value.Height - layer.AnchorPadding;
                            break;
                        case AnchorStyles.MiddleCenter:
                        case AnchorStyles.MiddleLeft:
                        case AnchorStyles.MiddleRight:
                            layer.Y = (outputHeight - layer.Size.Value.Height) / 2;
                            break;
                        case AnchorStyles.TopCenter:
                        case AnchorStyles.TopLeft:
                        case AnchorStyles.TopRight:
                            layer.Y = layer.AnchorPadding;
                            break;
                    }
                }
            }

            // Apply fill.
            DrawingVisual dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();

            // Apply fill.
            Fill.Apply(dc, new Rect(0, 0, outputWidth, outputHeight));

            dc.Close();

            // create output bitmap and lock data
            RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(outputWidth, outputHeight);
            rtb.Render(dv);
            FastBitmap output = new FastBitmap(rtb);

            // Blend layers using specified blend mode.
            output = LayerBlender.BlendLayers(output, VisibleLayers);

            // Apply global filters.
            foreach (Filter filter in Filters)
                if (filter.Enabled)
                    filter.ApplyFilter(output);

            // If image format doesn't support transparency, make all transparent pixels totally opaque.
            // Otherwise WPF wants to save them as black.
            if (ImageFormat == DynamicImageFormat.Bmp || ImageFormat == DynamicImageFormat.Jpeg)
            {
                output.Lock();
                for (int y = 0; y < output.Height; ++y)
                    for (int x = 0; x < output.Width; ++x)
                    {
                        Color c = output[x, y];
                        //if (output[x, y].A == 0 && output[x, y].R == 0 && output[x, y].G == 0 && output[x, y].B == 0)
                        output[x, y] = Color.FromArgb(255, c.R, c.G, c.B);
                    }
                output.Unlock();
            }

            return output.InnerBitmap;
        }