Esempio n. 1
0
 public void ShouldSetTheCorrectValueForTheOpacityChannel()
 {
     _settings.Format = MagickFormat.Rgbo;
     using (IMagickImage image = new MagickImage(_bytes, _settings))
     {
         using (IPixelCollection pixels = image.GetPixels())
         {
             Pixel pixel = pixels.GetPixel(0, 0);
             Assert.AreEqual(4, pixel.Channels);
             Assert.AreEqual(1, pixel.GetChannel(0));
             Assert.AreEqual(2, pixel.GetChannel(1));
             Assert.AreEqual(3, pixel.GetChannel(2));
             Assert.AreEqual(Quantum.Max - 4, pixel.GetChannel(3));
         }
     }
 }
Esempio n. 2
0
        public static Image LoadFromFile(string filepath)
        {
            var image = new MagickImage(filepath);
            IPixelCollection <byte> pixelCollection = image.GetPixels();
            ImageUInt8RGB           result          = new ImageUInt8RGB((uint)image.Width, (uint)image.Height);

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    IPixel <byte> pixel = pixelCollection.GetPixel(x, y);
                    result.R[x, y] = pixel.GetChannel(0);
                    result.G[x, y] = pixel.GetChannel(1);
                    result.B[x, y] = pixel.GetChannel(2);
                }
            }
            return(result);
        }
Esempio n. 3
0
        public void Test_Read_Bytes()
        {
#if Q8
            var bytes = new byte[] { 1, 2, 3, 4 };
#elif Q16 || Q16HDRI
            var bytes = new byte[] { 1, 0, 2, 0, 3, 0, 4, 0 };
#else
#error Not implemented!
#endif

            MagickReadSettings settings = new MagickReadSettings()
            {
                Width  = 1,
                Height = 1,
            };

            settings.Format = MagickFormat.Bgra;
            using (IMagickImage image = new MagickImage(bytes, settings))
            {
                using (IPixelCollection pixels = image.GetPixels())
                {
                    Pixel pixel = pixels.GetPixel(0, 0);
                    Assert.AreEqual(4, pixel.Channels);
                    Assert.AreEqual(3, pixel.GetChannel(0));
                    Assert.AreEqual(2, pixel.GetChannel(1));
                    Assert.AreEqual(1, pixel.GetChannel(2));
                    Assert.AreEqual(4, pixel.GetChannel(3));
                }
            }

            settings.Format = MagickFormat.Bgro;
            using (IMagickImage image = new MagickImage(bytes, settings))
            {
                using (IPixelCollection pixels = image.GetPixels())
                {
                    Pixel pixel = pixels.GetPixel(0, 0);
                    Assert.AreEqual(4, pixel.Channels);
                    Assert.AreEqual(3, pixel.GetChannel(0));
                    Assert.AreEqual(2, pixel.GetChannel(1));
                    Assert.AreEqual(1, pixel.GetChannel(2));
                    Assert.AreEqual(Quantum.Max - 4, pixel.GetChannel(3));
                }
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            using (MagickImage hdrImage =
                       new MagickImage(
                           @"d:\DEV_stuff\DEV\sharpexr\EXRViewer\bin\Debug\AmericanKitchen_OI2_2.VRayDiffuseFilterPS.exr ", new PixelReadSettings()))
            {
                Console.WriteLine("Reading image");
                IPixelCollection pixels = hdrImage.GetPixels();
                for (int y = 0; y < hdrImage.Height; y++)
                {
                    for (int x = 0; x < hdrImage.Width; x++)
                    {
                        MagickColor p = pixels.GetPixel(x, y).ToColor();
                    }
                }
            }

            Console.ReadKey();
        }
Esempio n. 5
0
            public void ShouldHaveTheCorrectColorspace()
            {
                using (IMagickImage image = new MagickImage(MagickColors.Black, 1, 1))
                {
                    image.ColorSpace = ColorSpace.CMYK;
                    image.Opaque(MagickColors.Black, new MagickColor("cmyk(128,23,250,156)"));

                    using (IPixelCollection pixels = image.GetPixelsUnsafe())
                    {
                        var color = pixels.GetPixel(0, 0).ToColor();
#if Q8
                        Assert.AreEqual("cmyka(128,23,250,156,1.0)", color.ToString());
#elif Q16 || Q16HDRI
                        Assert.AreEqual("cmyka(32896,5911,64250,40092,1.0)", color.ToString());
#else
#error Not implemented!
#endif
                    }
                }
            }
Esempio n. 6
0
            public void ShouldMergeTheImages()
            {
                using (IMagickImageCollection collection = new MagickImageCollection())
                {
                    collection.Read(Files.RoseSparkleGIF);

                    using (IPixelCollection pixels = collection[1].GetPixels())
                    {
                        MagickColor color = pixels.GetPixel(53, 3).ToColor();
                        Assert.AreEqual(0, color.A);
                    }

                    collection.Coalesce();

                    using (IPixelCollection pixels = collection[1].GetPixels())
                    {
                        MagickColor color = pixels.GetPixel(53, 3).ToColor();
                        Assert.AreEqual(Quantum.Max, color.A);
                    }
                }
            }
Esempio n. 7
0
        public void Draw(MagickImage map)
        {
            MainForm.ProgressStart("Rendering water ...");

            using (IPixelCollection heightmapPixels = zoneConfiguration.Heightmap.HeightmapScaled.GetPixels())
            {
                using (MagickImage water = new MagickImage(MagickColors.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize))
                {
                    int progressCounter = 0;

                    foreach (WaterConfiguration river in m_waterAreas)
                    {
                        MainForm.Log(river.Name + "...", MainForm.LogLevel.notice);

                        MagickColor fillColor;
                        if (m_useClientColors)
                        {
                            fillColor = river.Color;
                        }
                        else
                        {
                            fillColor = m_waterColor;
                        }
                        //water.FillColor = fillColor;

                        // Get the river coordinates and scale them to the targets size
                        List <PointD> riverCoordinates = river.GetCoordinates().Select(c => new PointD(c.X * zoneConfiguration.MapScale, c.Y * zoneConfiguration.MapScale)).ToList();

                        // Texture
                        using (MagickImage texture = new MagickImage((river.Type.ToLower() == "lava") ? GetLavaTexture() : GetWateryTexture()))
                        {
                            using (MagickImage pattern = new MagickImage(fillColor, texture.Width, texture.Height))
                            {
                                texture.Composite(pattern, 0, 0, CompositeOperator.DstIn);
                                texture.Composite(pattern, 0, 0, CompositeOperator.ColorDodge);

                                water.Settings.FillPattern = texture;
                                DrawablePolygon poly = new DrawablePolygon(riverCoordinates);
                                water.Draw(poly);
                            }
                        }

                        // get the min/max and just process them
                        int minX = Convert.ToInt32(riverCoordinates.Min(m => m.X)) - 10;
                        int maxX = Convert.ToInt32(riverCoordinates.Max(m => m.X)) + 10;
                        int minY = Convert.ToInt32(riverCoordinates.Min(m => m.Y)) - 10;
                        int maxY = Convert.ToInt32(riverCoordinates.Max(m => m.Y)) + 10;

                        using (IPixelCollection riverPixelCollection = water.GetPixels())
                        {
                            for (int x = minX; x < maxX; x++)
                            {
                                if (x < 0)
                                {
                                    continue;
                                }
                                if (x >= zoneConfiguration.TargetMapSize)
                                {
                                    continue;
                                }

                                for (int y = minY; y < maxY; y++)
                                {
                                    if (y < 0)
                                    {
                                        continue;
                                    }
                                    if (y >= zoneConfiguration.TargetMapSize)
                                    {
                                        continue;
                                    }

                                    ushort pixelHeight = heightmapPixels.GetPixel(x, y).GetChannel(0);
                                    if (pixelHeight > river.Height)
                                    {
                                        Pixel newPixel = new Pixel(x, y, new ushort[] { 0, 0, 0, ushort.MinValue });
                                        riverPixelCollection.SetPixel(newPixel);
                                    }
                                }
                            }
                        }

                        if (debug)
                        {
                            DebugRiver(progressCounter, river, riverCoordinates);
                        }

                        int percent = 100 * progressCounter / m_waterAreas.Count();
                        MainForm.ProgressUpdate(percent);
                        progressCounter++;
                    }

                    MainForm.ProgressStartMarquee("Merging...");

                    if (WaterTransparency != 0)
                    {
                        water.Alpha(AlphaOption.Set);
                        double divideValue = 100.0 / (100.0 - WaterTransparency);
                        water.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                    }

                    water.Blur();
                    map.Composite(water, 0, 0, CompositeOperator.SrcOver);
                }
            }

            MainForm.ProgressReset();
        }
Esempio n. 8
0
        public void Draw(MagickImage map)
        {
            MainForm.ProgressStart("Drawing lightmap ...");

            // Get the heightmap
            MagickImage heightmap = zoneConfiguration.Heightmap.Heightmap;

            using (MagickImage lightmap = new MagickImage(Color.Transparent, 256, 256))
            {
                using (IPixelCollection heightmapPixels = heightmap.GetPixels())
                {
                    using (IPixelCollection lightmapPixels = lightmap.GetPixels())
                    {
                        // z-component of surface normals
                        double nz   = 512d / zScale;
                        double nz_2 = nz * nz;
                        double nzlz = nz * lightVector[2];

                        int y1 = 0, y2 = 0;
                        for (int y = 0; y < lightmap.Height; y++)
                        {
                            if (y == 0)
                            {
                                y1 = 0;
                            }
                            else
                            {
                                y1 = y - 1;
                            }
                            if (y == 255)
                            {
                                y2 = 255;
                            }
                            else
                            {
                                y2 = y + 1;
                            }

                            int x1 = 0, x2 = 0;
                            for (int x = 0; x < lightmap.Width; x++)
                            {
                                if (x == 0)
                                {
                                    x1 = 0;
                                }
                                else
                                {
                                    x1 = x - 1;
                                }
                                if (x == 255)
                                {
                                    x2 = 255;
                                }
                                else
                                {
                                    x2 = x + 1;
                                }

                                double l = heightmapPixels.GetPixel(x1, y).GetChannel(0);
                                double r = heightmapPixels.GetPixel(x2, y).GetChannel(0);
                                double u = heightmapPixels.GetPixel(x, y1).GetChannel(0);
                                double d = heightmapPixels.GetPixel(x, y2).GetChannel(0);

                                double nx = l - r;
                                double ny = u - d;

                                double m_normal = Math.Sqrt(nx * nx + ny * ny + nz_2);
                                double ndotl    = (nx * lightVector[0] + ny * lightVector[1] + nzlz) / m_normal;

                                double pixelValue = lightBase - ndotl * lightScale * 256d;

                                ushort pixelValueDiff = 0;
                                ushort alphaValue     = ushort.MaxValue;
                                if (pixelValue < 0)
                                {
                                    pixelValueDiff = 0;
                                    alphaValue     = (ushort)pixelValue;
                                }
                                else
                                {
                                    pixelValueDiff = (ushort)pixelValue;
                                }

                                // ColorDodge map
                                // white lightens areas where black does nothing
                                // alpha darkens areas
                                lightmapPixels.SetPixel(x, y, new ushort[] { pixelValueDiff, pixelValueDiff, pixelValueDiff, alphaValue });
                            }

                            int percent = 100 * y / lightmap.Height;
                            MainForm.ProgressUpdate(percent);
                        }
                    }
                }

                MainForm.ProgressStartMarquee("Merging...");
                lightmap.Blur(0.0, 0.5);

                lightmap.VirtualPixelMethod = VirtualPixelMethod.Transparent;
                lightmap.FilterType         = FilterType.Gaussian;
                lightmap.Resize(zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize);

                // Apply the bumpmap using ColorDodge
                map.Composite(lightmap, 0, 0, CompositeOperator.ColorDodge);

                MainForm.ProgressReset();
            }
        }
Esempio n. 9
0
        private bool IsBorderColor(IPixelCollection pixels, int x, int y, MagickColor borderColor)
        {
            var color = pixels.GetPixel(x, y).ToColor();

            return(color.FuzzyEquals(borderColor, ColorFuzz));
        }
Esempio n. 10
0
        static MagickImage Method2_Magick(MagickImage working, MagickImage target)
        {
            MagickImage newImage = (MagickImage)working.Clone(/*working.Width, working.Height*/);

            int max_num_matches_per_frame = (int)(target.Height * target.Width * (1 / 1.0));

            //Console.WriteLine($"max_matches: {max_num_matches_per_frame}");

            IPixelCollection newImage_pixels = newImage.GetPixels();
            IPixelCollection target_pixels   = target.GetPixels();

            /*for (int x = 0; x < newImage.Width; x++)
             * {
             *  for (int y = 0; y < newImage.Height; y++)
             *  {
             *      Pixel p = newImage_pixels.GetPixel(x, y);
             *      Console.WriteLine($"p: {p.GetChannel(0)}, {p.GetChannel(1)}, {p.GetChannel(2)}");
             *  }
             * }*/

            for (int i = 0; i < max_num_matches_per_frame; i++)
            {
                Random rand = new Random();

                int x = rand.Next(0, newImage.Width);
                int y = rand.Next(0, newImage.Height);

                Pixel p_targ = target_pixels.GetPixel(x, y);
                Pixel p_work = newImage_pixels.GetPixel(x, y);



                //Console.WriteLine($"x, y: {x}, {y}");
                //Console.WriteLine($"r,g,b: {p_work.ToColor().R}, {p_work.ToColor().G}, {p_work.ToColor().B}");

                int randR = rand.Next(0, 255);
                int randG = rand.Next(0, 255);
                int randB = rand.Next(0, 255);

                int newR = p_work.GetChannel(0);
                int newG = p_work.GetChannel(1);
                int newB = p_work.GetChannel(2);

                if (Math.Abs(randR - p_targ.GetChannel(0)) < Math.Abs(randR - p_work.GetChannel(0)))
                {
                    newR = randR;
                }

                if (Math.Abs(randG - p_targ.GetChannel(1)) < Math.Abs(randG - p_work.GetChannel(1)))
                {
                    newG = randG;
                }

                if (Math.Abs(randB - p_targ.GetChannel(2)) < Math.Abs(randB - p_work.GetChannel(2)))
                {
                    newB = randB;
                }

                //if (newR != c_work.R || newG != c_work.G || newB != c_work.B)
                //    num_matches++;

                p_work.SetChannel(0, (byte)newR);
                p_work.SetChannel(1, (byte)newG);
                p_work.SetChannel(2, (byte)newB);

                /*p_work.SetChannel(0, 255);
                *  p_work.SetChannel(1, 255);
                *  p_work.SetChannel(2, 255);*/

                //Console.WriteLine($"orig: {newImage_pixels.GetPixel(x, y).GetChannel(0)}, new: {p_work.GetChannel(0)}");

                newImage_pixels.SetPixel(p_work);
                //newImage_pixels.SetArea(x, y, 1, 1, new[] { newR, newG, newB });



                //working.SetPixel(x, y, Color.FromArgb(newR, newG, newB));
            }

            return(newImage);
        }
Esempio n. 11
0
        public string[,] PixelIntensity(string filePath)
        {
            int width  = 0;
            int height = 0;

            string[,] intensity = new string [width, height];

            using (MagickImage image = new MagickImage())
            {
                image.Read(filePath);
                width     = image.Width - 1;
                height    = image.Height - 1;
                intensity = new string [width, height];
                int blue            = 0;
                int green           = 0;
                int red             = 0;
                IPixelCollection pc = image.GetPixels();

                for (int i = 0; i < width - 1; i++)
                {
                    for (int j = 0; j < height - 1; j++)
                    {
                        var col = pc.GetPixel(i, j).ToColor();
                        ;

                        if (col.B <= 255)
                        {
                            // don't divide, thake the value as is
                            blue = col.B;
                        }
                        else if (col.B >= 256 && col.B <= 65280)
                        {
                            // divide by 256
                            blue = col.B / 256;
                        }
                        else if (col.B < 65280)
                        {
                            // divide by 65536
                            blue = col.B / 65536;
                        }

                        if (col.G <= 255)
                        {
                            // don't divide, take the value as is
                            green = col.G;
                        }
                        else if (col.G >= 256 && col.G <= 65280)
                        {
                            // divide by 256
                            green = col.G / 256;
                        }
                        else if (col.G < 65280)
                        {
                            // divide by 65536
                            green = col.G / 65536;
                        }

                        if (col.R <= 255)
                        {
                            // don't divide, thake the value as is
                            red = col.R;
                        }
                        else if (col.R >= 256 && col.R <= 65280)
                        {
                            // divide by 256
                            red = col.R / 256;
                        }
                        else if (col.R < 65280)
                        {
                            // divide by 65536
                            red = col.R / 65536;
                        }

                        //Decimal spectrum
                        //Blue divide value: 0 - 255
                        //Green divide value: 256 - 65280
                        //Red divide value: 65536 - 16711680

                        int    pixVal     = 0;
                        string asciiShade = string.Empty;

                        if (red == 0 && green == 0 && blue == 0)
                        {
                            //don't divide just use 0
                            pixVal = 0;
                        }
                        else
                        {
                            pixVal = (red + green + blue) / 3;
                        }

                        asciiShade = AsciiShadeChar(pixVal);

                        intensity[i, j] = asciiShade;
                    }
                }
            }

            return(intensity);
        }