Exemple #1
0
        public void BuildForegroundImage_Theory(int docNumber)
        {
            int pageCount = 0;

            using (DjvuDocument document = Util.GetTestDocument(docNumber, out pageCount))
            {
                Util.VerifyDjvuDocument(pageCount, document);
                IDjvuPage page          = document.FirstPage;
                var       testImagePath = Path.Combine(Util.RepoRoot, "artifacts", "data", $"test{docNumber:00#}CFgnd.png");

                DjvuImage djvuImage = page.Image as DjvuImage;
                using (Bitmap image = djvuImage.GetForegroundImage(1, true))
                    using (Bitmap testImage = new Bitmap(testImagePath))
                    {
                        Assert.NotNull(image);
                        Assert.IsType <Bitmap>(image);

                        if (image.Width != testImage.Width || image.Height != testImage.Height)
                        {
                            Assert.True(false, $"Unexpected image size differences. Width image: {image.Width} | testImage: {testImage.Width}, Height: image: {image.Height} | testImage {testImage.Height}");
                        }

                        bool result = Util.CompareImagesForBinarySimilarity(testImage, image, 0.025, true, $"Testing Djvu foreground: \ttest{docNumber:00#}C.png, ");

#if DUMP_IMAGES
                        DumpImage(docNumber, image, "Fgnd");
#endif

                        Assert.True(result);
                    }
            }
        }
Exemple #2
0
        public void GetForegroundImage000()
        {
            var page = new DjvuPage();

            page.Height = 128;
            page.Width  = 128;
            DjvuImage djvuImage = page.Image as DjvuImage;

            using (var bitmap = djvuImage.GetForegroundImage(1))
            {
                Assert.NotNull(bitmap);
                Assert.NotEqual(0, page.Width);
                Assert.Equal(page.Width, bitmap.Width);
                Assert.Equal(page.Height, bitmap.Height);
                var pixel = bitmap.GetPixel(0, 0);
                Assert.Equal(0, pixel.R);
                Assert.Equal(0, pixel.G);
                Assert.Equal(0, pixel.B);
            }
        }
Exemple #3
0
        public void GetForegroundImage078()
        {
            string file = Path.Combine(Util.ArtifactsPath, "test078C.djvu");

            using (DjvuDocument doc = new DjvuDocument(file))
            {
                var page = (DjvuPage)doc.Pages[0];
                page.Height = 128;
                page.Width  = 128;
                DjvuImage djvuImage = page.Image as DjvuImage;
                using (var bitmap = djvuImage.GetForegroundImage(1))
                {
                    Assert.NotNull(bitmap);
                    Assert.NotEqual(0, page.Width);
                    Assert.Equal(page.Width, bitmap.Width);
                    Assert.Equal(page.Height, bitmap.Height);
                    var pixel = bitmap.GetPixel(0, 0);
                    Assert.Equal(0, pixel.R);
                    Assert.Equal(0, pixel.G);
                    Assert.Equal(0, pixel.B);
                }
            }
        }
Exemple #4
0
        public void GetForegroundImage003()
        {
            int pageCount = 0;

            using (DjvuDocument document = Util.GetTestDocument(3, out pageCount))
            {
                Util.VerifyDjvuDocument(pageCount, document);

                DjvuPage page = document.FirstPage as DjvuPage;
                Assert.NotNull(page);

                DjvuImage djvuImage = page.Image as DjvuImage;
                using (var image = djvuImage.GetForegroundImage(1))
                {
                    Assert.NotNull(image);
                    Assert.IsType <Bitmap>(image);
#if DUMP_IMAGES
                    string file = Path.Combine(Util.ArtifactsDataPath, "dumps", "test003CFn.png");
                    using (FileStream stream = new FileStream(file, FileMode.Create))
                        image.Save(stream, ImageFormat.Png);
#endif
                }
            }
        }
Exemple #5
0
        public static unsafe Bitmap BuildImage(this DjvuImage image, int subsample = 1)
        {
            Verify.SubsampleRange(subsample);

            lock (image.LoadingLock)
            {
                Bitmap background = image.GetBackgroundImage(subsample, true);

                // TODO ETW logging goes here

                using (Bitmap foreground = image.GetForegroundImage(subsample, true))
                {
                    using (Bitmap mask = image.GetMaskImage(subsample, true))
                    {
                        image.HasLoaded = true;

                        BitmapData backgroundData =
                            background.LockBits(new Rectangle(0, 0, background.Width, background.Height),
                                                ImageLockMode.ReadWrite, background.PixelFormat);
                        int backgroundPixelSize = GetPixelSize(backgroundData.PixelFormat);

                        BitmapData foregroundData =
                            foreground.LockBits(new Rectangle(0, 0, foreground.Width, foreground.Height),
                                                ImageLockMode.ReadOnly, foreground.PixelFormat);
                        int foregroundPixelSize = GetPixelSize(foregroundData.PixelFormat);

                        BitmapData maskData = mask.LockBits(new Rectangle(0, 0, mask.Width, mask.Height),
                                                            ImageLockMode.ReadOnly, mask.PixelFormat);

                        //int maskPixelSize = GetPixelSize(maskData);

                        int bgndHeight = background.Height;
                        int bgndWidth  = background.Width;

                        int fgndHeight = foreground.Height;
                        int fgndWidth  = foreground.Width;

                        int maskHeight = mask.Height;
                        int maskWidth  = mask.Width;

                        int maskbgnH = maskHeight / bgndHeight;
                        int maskfgnH = maskHeight / fgndHeight;

                        int maskbgnW = maskWidth / bgndWidth;
                        int maskfgnW = maskWidth / fgndWidth;

                        //Parallel.For(
                        //    0,
                        //    height,
                        //    y =>
                        //    {

                        for (int y = 0, yf = 0, yb = 0; y < maskHeight && yb < bgndHeight && yf < fgndHeight; ++y, yf = yb = y)
                        {
                            byte *maskRow = (byte *)maskData.Scan0 + (y * maskData.Stride);
                            DjvuNet.Graphics.Pixel *backgroundRow = (DjvuNet.Graphics.Pixel *)(backgroundData.Scan0 + (yb * backgroundData.Stride));
                            DjvuNet.Graphics.Pixel *foregroundRow = (DjvuNet.Graphics.Pixel *)(foregroundData.Scan0 + (yf * foregroundData.Stride));

                            for (int x = 0, xf = 0, xb = 0; x < bgndWidth && xb < maskWidth && xf < fgndWidth; x++)
                            {
                                // Check if the mask byte is set
                                if (maskRow[x] > 0)
                                {
                                    DjvuNet.Graphics.Pixel xF = foregroundRow[xf];

                                    if (image.IsInverted)
                                    {
                                        backgroundRow[xb] = InvertColor(xF);
                                    }
                                    else
                                    {
                                        backgroundRow[xb] = xF;
                                    }
                                }
                                else if (image.IsInverted)
                                {
                                    backgroundRow[xb] = InvertColor(backgroundRow[xb]);
                                }

                                if (x >= 0)
                                {
                                    if (x % maskbgnW == 0)
                                    {
                                        xb++;
                                    }

                                    if (x % maskfgnW == 0)
                                    {
                                        xf++;
                                    }
                                }
                            }

                            if (y >= 0)
                            {
                                if (y % maskbgnH == 0)
                                {
                                    yb++;
                                }

                                if (y % maskfgnH == 0)
                                {
                                    yf++;
                                }
                            }
                        }
                        //});

                        mask.UnlockBits(maskData);
                        foreground.UnlockBits(foregroundData);
                        background.UnlockBits(backgroundData);

                        return(background);
                    }
                }
            }
        }