Ejemplo n.º 1
0
        public void RGBToYCbCrPerformanceTest()
        {
            byte[]    rgba       = GetRgba(1);
            const int runs       = 20;
            const int iterations = 100;
            var       slowPerf   = new PerformanceMonitor("TheoreticallySlower", runs);
            var       fastPerf   = new PerformanceMonitor("TheoreticallyFaster", runs);

            for (int run = 0; run < runs; run++)
            {
                slowPerf.Start();
                for (int i = 0; i < iterations; i++)
                {
                    for (int p = 0; p < rgba.Length; p++)
                    {
                        byte b = rgba[p++];
                        byte g = rgba[p++];
                        byte r = rgba[p++];
                        YCbCr.fromRGBSlow(ref r, ref g, ref b);
                    }
                }
                slowPerf.Stop();

                fastPerf.Start();
                for (int i = 0; i < iterations; i++)
                {
                    for (int p = 0; p < rgba.Length; p++)
                    {
                        byte b = rgba[p++];
                        byte g = rgba[p++];
                        byte r = rgba[p++];
                        YCbCr.fromRGB(ref r, ref g, ref b);
                    }
                }
                fastPerf.Stop();
            }
#if !DEBUG
            // This is only true when running with the optimized JIT.
            Assert.IsTrue(fastPerf.AverageCompletionTimeInMs < slowPerf.AverageCompletionTimeInMs,
                          "The fast conversion method took {0:0.00} ms, while the slow took {1:0.00}", fastPerf.AverageCompletionTimeInMs, slowPerf.AverageCompletionTimeInMs);
#endif
        }
Ejemplo n.º 2
0
        public void RGBToYCbCrTest()
        {
            const int maxVariance = 1;

            byte[] rgba = GetRgba(1);
            for (int i = 0; i < rgba.Length; i++)
            {
                byte b2 = rgba[i];
                byte b1 = rgba[i++];
                byte g2 = rgba[i];
                byte g1 = rgba[i++];
                byte r2 = rgba[i];
                byte r1 = rgba[i++];
                YCbCr.fromRGBSlow(ref r1, ref g1, ref b1);
                YCbCr.fromRGB(ref r2, ref g2, ref b2);
                Assert.IsTrue(Math.Abs(r1 - r2) <= maxVariance);
                Assert.IsTrue(Math.Abs(g1 - g2) <= maxVariance);
                Assert.IsTrue(Math.Abs(b1 - b2) <= maxVariance);
            }
        }
Ejemplo n.º 3
0
        private void LoadSubsampledRaster(byte[] rgbaBuffer)
        {
            int rgbaPos = 0;

            for (short y = 0; y < height; y++)
            {
                int yy = y / vSampleFactor[yIndex];
                int cby = y / vSampleFactor[cbIndex];
                int cry = y / vSampleFactor[crIndex];
                int yx = 0, cbx = 0, crx = 0;
                for (short x = 0; x < width; x++)
                {
                    // Convert to YCbCr colorspace.
                    // The order of bytes is (oddly enough) BGRA
                    byte b = rgbaBuffer[rgbaPos++];
                    byte g = rgbaBuffer[rgbaPos++];
                    byte r = rgbaBuffer[rgbaPos++];
                    YCbCr.fromRGB(ref r, ref g, ref b);

                    // Only include the byte in question in the raster if it matches the appropriate sampling factor.
                    if (IncludeInSample(yIndex, x, y))
                    {
                        rasterBuffer[yIndex][yx++][yy] = r;
                    }
                    if (IncludeInSample(cbIndex, x, y))
                    {
                        rasterBuffer[cbIndex][cbx++][cby] = g;
                    }
                    if (IncludeInSample(crIndex, x, y))
                    {
                        rasterBuffer[crIndex][crx++][cry] = b;
                    }

                    // For YCbCr, we ignore the Alpha byte of the RGBA byte structure, so advance beyond it.
                    rgbaPos++;
                }
            }
        }