The results of a TrigradDecompression, containing both the output and debug bitmaps.
Exemplo n.º 1
0
        /// <summary> Decompresses a trigrad compressed bitmap. </summary>
        /// <param name="compressionData"> The TrigradCompressed data.</param>
        /// <param name="original"> The original image to determine the most effect fill mode.</param>
        /// <param name="options"> Options dictating the decompression.</param>
        public static TrigradDecompressed DecompressBitmap(TrigradCompressed compressionData, TrigradOptions options)
        {
            TrigradDecompressed decompressed = new TrigradDecompressed(compressionData.Width, compressionData.Height);

            for (int x = 0; x < compressionData.Width; x++)
                for (int y = 0; y < compressionData.Height; y++)
                    decompressed.Output[x, y] = new Pixel(Color.HotPink);

            drawMesh(compressionData.Mesh, decompressed.Output, options);

            fillGaps(decompressed.Output);

            return decompressed;
        }
        /// <summary> Decompresses a trigrad compressed bitmap. </summary>
        /// <param name="compressionData"> The TrigradCompressed data.</param>
        /// <param name="colorGrader"> The color grader that will be used to fill the output bitmap.</param>
        /// <param name="debug"> Bool specifying whether a debug output will be produced.</param>
        public static TrigradDecompressed DecompressBitmap(TrigradCompressed compressionData, IGrader colorGrader = null,bool debug = false)
        {
            if (colorGrader == null)
                colorGrader = new BarycentricGrader();

            TrigradDecompressed decompressed = new TrigradDecompressed(compressionData.Width, compressionData.Height);

            //build the triangle mesh
            decompressed.Mesh = buildMesh(compressionData.SampleTable);

            Parallel.ForEach(decompressed.Mesh.Triangles, triangle =>
            {
                var vU = triangle.GetVertex(0);
                var vV = triangle.GetVertex(1);
                var vW = triangle.GetVertex(2);

                //find sample points of triangle
                Color cU = compressionData.SampleTable[vU.Point()];
                Color cV = compressionData.SampleTable[vV.Point()];
                Color cW = compressionData.SampleTable[vW.Point()];

                //rasterize triangle to find points to fill
                var points = TriangleRasterization.PointsInTriangle(vU.Point(), vV.Point(), vW.Point());

                foreach (var point in points)
                {
                    var coords = Barycentric.GetCoordinates(point, vU, vV, vW);

                    Color gradedColor = colorGrader.Grade(cU, cV, cW, coords.U, coords.V, coords.W, point.X, point.Y);

                    lock (decompressed.Output)
                        decompressed.Output.SetPixel(point.X, point.Y, gradedColor);

                    if(debug)
                    lock (decompressed.DebugOutput)
                        decompressed.DebugOutput.SetPixel(point.X, point.Y, Color.FromArgb((byte)(coords.U * 255), (byte)(coords.V * 255), (byte)(coords.W * 255)));

                }
            });

            return decompressed;
        }