Example #1
0
        // Verifies a test file. Returns true on success, false on failure. In the failure case,
        // a difference bitmap is written out.
        static bool VerifyTestFile(string filename, bool testLightenedColor, bool roundtripToOcadFile, int minOcadVersion, int maxOcadVersion)
        {
            string pngFileName;
            string mapFileName;
            string geodeFileName;
            string ocadFileName;
            string directoryName;
            string newBitmapName;
            RectangleF mapArea;
            int bmWidth, bmHeight;

            // Read the test file, and get the other file names and the area.
            using (StreamReader reader = new StreamReader(filename)) {
                mapFileName = reader.ReadLine();
                pngFileName = reader.ReadLine();
                float left, right, top, bottom;
                string area = reader.ReadLine();
                string[] coords = area.Split(',');
                left = float.Parse(coords[0]); bottom = float.Parse(coords[1]); right = float.Parse(coords[2]); top = float.Parse(coords[3]);
                mapArea = new RectangleF(left, top, right - left, bottom - top);
                string sizeLine = reader.ReadLine();
                coords = sizeLine.Split(',');
                bmWidth = int.Parse(coords[0]);
                bmHeight = int.Parse(coords[1]);
            }

            // Convert to absolute paths.
            directoryName = Path.GetDirectoryName(filename);
            mapFileName = Path.Combine(directoryName, mapFileName);
            pngFileName = Path.Combine(directoryName, pngFileName);
            geodeFileName = Path.Combine(directoryName,
                                         Path.GetFileNameWithoutExtension(mapFileName) + "_temp.geode");
            ocadFileName = Path.Combine(directoryName,
                                         Path.GetFileNameWithoutExtension(mapFileName) + "_temp.ocd");
            newBitmapName = Path.Combine(directoryName,
                                        Path.GetFileNameWithoutExtension(pngFileName) + "_new.png");

            File.Delete(geodeFileName);
            File.Delete(ocadFileName);
            File.Delete(newBitmapName);

            // Create and open the map file.
            Map map = new Map();
            InputOutput.ReadFile(mapFileName, map);

            // Draw into a new bitmap.
            BitmapSource bitmapNew = RenderBitmap(map, bmWidth, bmHeight, mapArea);
            WritePng(bitmapNew, newBitmapName);
            TestUtil.CompareBitmapBaseline(newBitmapName, pngFileName);

            if (testLightenedColor) {
                using (map.Write()) {
                    ColorMatrix colorMatrix = new ColorMatrix(new float[][] {
                           new float[] {0.4F,  0,  0,  0, 0},
                           new float[] {0,  0.4F,  0,  0, 0},
                           new float[] {0,  0,  0.4F,  0, 0},
                           new float[] {0,  0,  0,  1, 0},
                           new float[] {0.6F, 0.6F, 0.6F, 0, 1}
                    });
                    map.ColorMatrix = colorMatrix;
                }

                string lightenedPngFileName = Path.Combine(Path.GetDirectoryName(pngFileName), Path.GetFileNameWithoutExtension(pngFileName) + "_light.png");
                BitmapSource bitmapLight = RenderBitmap(map, bmWidth, bmHeight, mapArea);
                WritePng(bitmapLight, newBitmapName);
                TestUtil.CompareBitmapBaseline(newBitmapName, lightenedPngFileName);
            }

            if (roundtripToOcadFile) {
                for (int version = minOcadVersion; version <= maxOcadVersion; ++version) {
                    // Save and load to a temp file name.
                    InputOutput.WriteFile(ocadFileName, map, version);

                    // Create and open the map file.
                    map = new Map();
                    InputOutput.ReadFile(ocadFileName, map);

                    // Draw into a new bitmap.
                    bitmapNew = RenderBitmap(map, bmWidth, bmHeight, mapArea);
                    WritePng(bitmapNew, newBitmapName);
                    TestUtil.CompareBitmapBaseline(newBitmapName, pngFileName);

                    File.Delete(ocadFileName);
                }
            }

            return true;
        }
Example #2
0
        public bool IntersectsWith(RectangleF rect)
        {
            float right = Right;
            float bottom = Bottom;
            float left = this.left;
            float top = this.top;
            left = Math.Max(left, rect.Left);
            top = Math.Max(top, rect.Top);
            right = Math.Min(right, rect.Right);
            bottom = Math.Min(bottom, rect.Bottom);

            return left < right && top < bottom;
        }
Example #3
0
        // Render part of a map to a bitmap.
        static BitmapSource RenderBitmap(Map map, int bmWidth, int bmHeight, RectangleF mapArea)
        {
            // Calculate the transform matrix.
            Point midpoint = new Point(bmWidth / 2.0F, bmHeight / 2.0F);
            double scaleFactor = bmWidth / mapArea.Width;
            PointF centerPoint = new PointF((mapArea.Left + mapArea.Right) / 2, (mapArea.Top + mapArea.Bottom) / 2);
            Matrix matrix = Matrix.Identity;
            matrix.TranslatePrepend(midpoint.X, midpoint.Y);
            matrix.ScalePrepend(scaleFactor, -scaleFactor);  // y scale is negative to get to cartesian orientation.
            matrix.TranslatePrepend(-centerPoint.X, -centerPoint.Y);

            // Get the render options.
            RenderOptions renderOpts = new RenderOptions();
            renderOpts.usePatternBitmaps = false;
            renderOpts.minResolution = (float) (1 / scaleFactor);

            // Create a drawing of the map.
            DrawingVisual visual = new DrawingVisual();
            DrawingContext dc = visual.RenderOpen();

            // Clear the bitmap
            dc.DrawRectangle(Brushes.White, null, new Rect(-1, -1, bmWidth + 2, bmHeight + 2));  // clear background.

            // Transform to map coords.
            dc.PushTransform(new MatrixTransform(matrix));

            // Draw the map.
            using (map.Read())
                map.Draw(dc, mapArea, renderOpts);
            dc.Close();

            // Draw into a new bitmap.
            RenderTargetBitmap bitmapNew = new RenderTargetBitmap(bmWidth, bmHeight, 96.0, 96.0, PixelFormats.Pbgra32);
            bitmapNew.Render(visual);
            bitmapNew.Freeze();

            return bitmapNew;
        }
Example #4
0
        public void Intersect(RectangleF rect)
        {
            float right = Right;
            float bottom = Bottom;
            left = Math.Max(left, rect.Left);
            top = Math.Max(top, rect.Top);
            right = Math.Min(right, rect.Right);
            bottom = Math.Min(bottom, rect.Bottom);

            if (left >= right || top >= bottom)
                left = top = width = height = 0;
            else {
                width = right - left;
                height = bottom - top;
            }
        }
Example #5
0
        public bool Contains(RectangleF rect)
        {
            float rectL = rect.Left, rectR = rect.Right, rectT = rect.Top, rectB = rect.Bottom;

            return (rectL >= left && rectL <= Right &&
                        rectR >= left && rectR >= Right &&
                        rectT >= top && rectT <= Bottom &&
                        rectB >= top && rectB <= Bottom);
        }
Example #6
0
 public static RectangleF Union(RectangleF rect1, RectangleF rect2)
 {
     return RectangleF.FromLTRB(Math.Min(rect1.Left, rect2.Left),
                                                  Math.Min(rect1.Top, rect2.Top),
                                                  Math.Max(rect1.Right, rect2.Right),
                                                  Math.Max(rect1.Bottom, rect2.Bottom));
 }
Example #7
0
        public static RectangleF Intersect(RectangleF rect1, RectangleF rect2)
        {
            float left = Math.Max(rect1.Left, rect2.Left);
            float top = Math.Max(rect1.Top, rect2.Top);
            float right = Math.Min(rect1.Right, rect2.Right);
            float bottom = Math.Min(rect1.Bottom, rect2.Bottom);

            if (left > right || top > bottom)
                return new RectangleF(0, 0, 0, 0);
            else
                return RectangleF.FromLTRB(left, top, right, bottom);
        }
Example #8
0
 public static RectangleF Inflate(RectangleF rect, float x, float y)
 {
     return new RectangleF(rect.left - x, rect.top - y, rect.width + x, rect.height + y);
 }