コード例 #1
0
        public static IPixelMap PixelMapFromBitmap(System.Drawing.Bitmap bmp)
        {
            IPixelMap  pixMap = PixelMapTests.CreateInitVerifyPixelMap(bmp.Width, bmp.Height, Pixel.WhitePixel);
            BitmapData data   = null;

            try
            {
                data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                unsafe
                {
                    // TODO: Fix me!
                    fixed(sbyte *dest = pixMap.Data)
                    MemoryUtilities.MoveMemory(dest, (void *)data.Scan0, pixMap.Data.Length);
                }
            }
            catch (Exception ex)
            {
                throw new DjvuAggregateException(
                          $"Error with bitmap. Width: {bmp.Width}, Height: {bmp.Height}, PixelFormat: {bmp.PixelFormat}", ex);
            }
            finally
            {
                if (data != null)
                {
                    bmp.UnlockBits(data);
                }
            }
            return(pixMap);
        }
コード例 #2
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void OpInequalityTest001()
        {
            int   width  = 16;
            int   height = 16;
            Pixel color  = Pixel.BlackPixel;
            Pixel spixel = new Pixel(127, 127, 127);
            sbyte scolor = 127;

            Bitmap bmp = new Bitmap();

            bmp.Init(height, width, 0);
            bmp.Fill(scolor);

            IPixelMap map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.BlackPixel);
            IPixelMap map2 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.GreenPixel);
            IPixelMap map3 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.RedPixel);

            PixelReference pix1  = (PixelReference)map1.CreateGPixelReference(0);
            PixelReference pix2  = (PixelReference)map2.CreateGPixelReference(0);
            PixelReference pix3  = (PixelReference)map3.CreateGPixelReference(0);
            PixelReference pix4  = (PixelReference)bmp.CreateGPixelReference(0);
            PixelReference pix11 = (PixelReference)pix1.Duplicate();
            PixelReference pix21 = (PixelReference)pix2.Duplicate();
            PixelReference pix31 = (PixelReference)pix3.Duplicate();

            Assert.True(pix1 != pix2);
            Assert.False(pix1 != pix11);
            Assert.True(pix3 != pix2);
            Assert.False(pix2 != pix21);
            Assert.True(pix3 != pix1);
            Assert.False(pix3 != pix31);
            Assert.True(pix3 != (PixelReference)null);
            Assert.True((PixelReference)null != pix3);
        }
コード例 #3
0
        public void ScaleTest012()
        {
            int srcWidth     = 48;
            int srcHeight    = 27;
            int targetWidth  = 16;
            int targetHeight = 9;

            var sclr = CreateVerifyPixelMapScaler(srcWidth, srcHeight, targetWidth, targetHeight);

            Rectangle srcRect = new Rectangle
            {
                Right  = 0,
                Left   = srcWidth,
                Bottom = -1,
                Top    = srcHeight - 1
            };

            Rectangle targetRect = new Rectangle
            {
                Right  = 0,
                Left   = targetWidth,
                Bottom = 0,
                Top    = targetHeight
            };

            IPixelMap srcMap  = PixelMapTests.CreateInitVerifyPixelMap(srcWidth, srcHeight, Pixel.RedPixel);
            IPixelMap destMap = PixelMapTests.CreateInitVerifyPixelMap(targetWidth, targetHeight, Pixel.WhitePixel);

            Assert.Throws <DjvuArgumentException>(() => sclr.Scale(srcRect, srcMap, targetRect, destMap));
        }
コード例 #4
0
        public void ScaleTest007()
        {
            int srcWidth     = 48;
            int srcHeight    = 27;
            int targetWidth  = 32;
            int targetHeight = 18;

            var sclr = CreateVerifyPixelMapScaler(srcWidth, srcHeight, targetWidth, targetHeight);

            Rectangle srcRect = new Rectangle
            {
                Right  = 0,
                Left   = 48,
                Bottom = 0,
                Top    = 27
            };

            Rectangle targetRect = new Rectangle
            {
                Right  = 0,
                Left   = 32,
                Bottom = 0,
                Top    = 18
            };

            IPixelMap srcMap  = PixelMapTests.CreateInitVerifyPixelMap(srcWidth, srcHeight, Pixel.RedPixel);
            IPixelMap destMap = PixelMapTests.CreateInitVerifyPixelMap(targetWidth, targetHeight, Pixel.WhitePixel);

            sclr.SetHorzRatio(32, 48);
            sclr.SetVertRatio(18, 27);

            sclr.Scale(srcRect, srcMap, targetRect, destMap);
        }
コード例 #5
0
        public void ScaleTest008()
        {
            int srcWidth     = 48;
            int srcHeight    = 27;
            int targetWidth  = 16;
            int targetHeight = 9;

            var sclr = CreateVerifyPixelMapScaler(srcWidth, srcHeight, targetWidth, targetHeight);

            Rectangle srcRect = new Rectangle
            {
                Right  = 0,
                Left   = srcWidth,
                Bottom = 0,
                Top    = srcHeight
            };

            Rectangle targetRect = new Rectangle
            {
                Right  = 0,
                Left   = targetWidth,
                Bottom = 0,
                Top    = targetHeight
            };

            IPixelMap srcMap  = PixelMapTests.CreateInitVerifyPixelMap(srcWidth, srcHeight, Pixel.RedPixel);
            IPixelMap destMap = PixelMapTests.CreateInitVerifyPixelMap(targetWidth, targetHeight, Pixel.WhitePixel);

            sclr.SetHorzRatio(targetWidth, srcWidth);
            sclr.SetVertRatio(targetHeight, srcHeight);

            sclr.Scale(srcRect, srcMap, targetRect, destMap);
        }
コード例 #6
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void SetPixelsTest001()
        {
            int   width  = 16;
            int   height = 16;
            Pixel color  = Pixel.BluePixel;
            sbyte scolor = 127;

            Bitmap bmp = new Bitmap();

            bmp.Init(height, width, 0);
            bmp.Fill(scolor);

            IPixelMap map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            IPixelMap map2 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.GreenPixel);
            IPixelMap map3 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.RedPixel);

            var pix1 = map1.CreateGPixelReference(0);
            var pix2 = map2.CreateGPixelReference(0);
            var pix3 = map3.CreateGPixelReference(0);
            var pix4 = bmp.CreateGPixelReference(0);

            Assert.Equal(scolor, bmp.GetByteAt(8));

            pix4.SetPixels(pix1, 16);
            Assert.Equal(255, bmp.GetByteAt(8));
            Assert.Equal(scolor, bmp.GetByteAt(24));

            pix4.SetPixels(pix2, 16);
            Assert.Equal(0, bmp.GetByteAt(24));
            Assert.Equal(scolor, bmp.GetByteAt(40));

            pix4.SetPixels(pix3, 16);
            Assert.Equal(0, bmp.GetByteAt(40));
            Assert.Equal(scolor, bmp.GetByteAt(56));
        }
コード例 #7
0
        public void ScaleTest006()
        {
            int srcWidth     = 48;
            int srcHeight    = 27;
            int targetWidth  = 32;
            int targetHeight = 18;

            var sclr = CreateVerifyPixelMapScaler(srcWidth, srcHeight, targetWidth, targetHeight);

            Rectangle srcRect = new Rectangle
            {
                Right  = 0,
                Left   = 48,
                Bottom = 0,
                Top    = 27
            };

            srcRect.YMax -= 1;

            Rectangle targetRect = new Rectangle
            {
                Right  = 0,
                Left   = 32,
                Bottom = 0,
                Top    = 18
            };

            IPixelMap srcMap  = PixelMapTests.CreateInitVerifyPixelMap(srcWidth, srcHeight, Pixel.RedPixel);
            IPixelMap destMap = PixelMapTests.CreateInitVerifyPixelMap(targetWidth, targetHeight, Pixel.WhitePixel);

            Assert.Throws <DjvuArgumentException>(() => sclr.Scale(srcRect, srcMap, targetRect, destMap));
        }
コード例 #8
0
        public void ToImageTest005()
        {
            int   width  = 32;
            int   height = 32;
            Pixel color  = Pixel.BluePixel;

            IPixelMap map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);

            map1.BytesPerPixel = 5;
            Assert.Throws <DjvuFormatException>(() => map1.ToImage());
        }
コード例 #9
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void OffsetTest001()
        {
            int   width  = 32;
            int   height = 32;
            Pixel color  = Pixel.BluePixel;

            IPixelMap      map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            PixelReference pix  = (PixelReference)map1.CreateGPixelReference(0);

            pix.Offset = 3 * width;
            Assert.Equal(3 * width, pix.Offset);
        }
コード例 #10
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void RedOffsetTest()
        {
            int   width  = 256;
            int   height = 256;
            Pixel color  = Pixel.BluePixel;

            IPixelMap      map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            PixelReference pix  = (PixelReference)map1.CreateGPixelReference(0);

            Assert.Equal(2, pix.RedOffset);
            pix.RedOffset = 0;
            Assert.Equal(0, pix.RedOffset);
        }
コード例 #11
0
ファイル: InterWavePixelMap.cs プロジェクト: sahwar/DjvuNet
        public IPixelMap GetPixelMap(int subsample, Rectangle rect, IPixelMap retval)
        {
            if (_YMap == null)
            {
                return(null);
            }

            if (retval == null)
            {
                retval = new PixelMap();
            }

            int width  = rect.Width;
            int height = rect.Height;
            int pixsep = 3;
            int rowsep = width * pixsep;

            sbyte[] bytes = retval.Init(height, width, null).Data;

            _YMap.Image(subsample, rect, 0, bytes, rowsep, pixsep, false);

            if ((_CrMap != null) && (_CbMap != null) && (_CrCbDelay >= 0))
            {
                _CbMap.Image(subsample, rect, 1, bytes, rowsep, pixsep, _CrCbHalf);
                _CrMap.Image(subsample, rect, 2, bytes, rowsep, pixsep, _CrCbHalf);
            }

            if ((_CrMap != null) && (_CbMap != null) && (_CrCbDelay >= 0))
            {
                unsafe
                {
                    fixed(sbyte *pBuffer = bytes)
                    {
                        Pixel *pPix = (Pixel *)pBuffer;

                        InterWaveTransform.YCbCr2Rgb(pPix, width, height);
                    }
                }
            }
            else
            {
                IPixelReference pixel = retval.CreateGPixelReference(0);
                for (int x = width * height; x-- > 0; pixel.IncOffset())
                {
                    pixel.SetGray((sbyte)(127 - pixel.Blue));
                }
            }

            return(retval);
        }
コード例 #12
0
ファイル: PixelMapTests.cs プロジェクト: sahwar/DjvuNet
        public static IPixelMap CreateInitVerifyPixelMap(int width, int height, IPixel color)
        {
            IPixelMap map = CreateVerifyPixelMap();

            map.Init(height, width, color);
            Assert.Equal(width, map.Width);
            Assert.Equal(height, map.Height);
            Assert.Equal <IPixel>(color, map.CreateGPixelReference(width / 2).ToPixel());

            var pix = map.CreateGPixelReference(width / 2);

            Assert.True(color.Equals(pix.ToPixel()));
            return(map);
        }
コード例 #13
0
ファイル: PixelMapTests.cs プロジェクト: sahwar/DjvuNet
        public void StencilTest001()
        {
            int       width  = 128;
            int       height = 128;
            Pixel     color  = Pixel.WhitePixel;
            Rectangle rect   = new Rectangle {
                Left = -10, Bottom = -10, Right = 200, Top = 200
            };

            IBitmap   bmp  = BitmapTests.CreateIntiFillVerifyBitmap(width, height, 0, -1);
            IPixelMap map  = CreateInitVerifyPixelMap(width, height, color);
            IPixelMap map2 = CreateInitVerifyPixelMap(width, height, Pixel.BlackPixel);

            Assert.Throws <DjvuArgumentOutOfRangeException>("bounds", () => map.Stencil(bmp, map2, 1, 1, rect, 2.2));
        }
コード例 #14
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void ParentTest()
        {
            int   width  = 256;
            int   height = 256;
            Pixel color  = Pixel.BluePixel;

            IPixelMap      map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            IPixelMap      map2 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            PixelReference pix  = (PixelReference)map1.CreateGPixelReference(0);

            Assert.Same(pix.Parent, map1);
            pix.Parent = map2;
            Assert.NotSame(map1, map2);
            Assert.NotSame(pix.Parent, map1);
            Assert.Same(pix.Parent, map2);
        }
コード例 #15
0
        public void ToImageTest002()
        {
            int   width  = 32;
            int   height = 32;
            Pixel color  = Pixel.BluePixel;

            IPixelMap map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);

            map1.BytesPerPixel = 2;
            using (System.Drawing.Bitmap bmp = map1.ToImage())
            {
                Assert.NotNull(bmp);
                Assert.IsType <System.Drawing.Bitmap>(bmp);
                Assert.Equal(width, bmp.Width);
                Assert.Equal(height, bmp.Height);
            }
        }
コード例 #16
0
ファイル: PixelMapTests.cs プロジェクト: sahwar/DjvuNet
        public void StencilTest003()
        {
            int       width  = 128;
            int       height = 128;
            Pixel     color  = new Pixel(107, 125, 93);
            Pixel     color2 = new Pixel(-77, -77, -77);
            Rectangle rect   = new Rectangle {
                Left = 100, Bottom = 0, Right = 0, Top = 100
            };

            IBitmap bmp = BitmapTests.CreateIntiFillVerifyBitmap(width / 2, height / 2, 0, 127);

            bmp.Grays = 256;
            IPixelMap map  = CreateInitVerifyPixelMap(width, height, color);
            IPixelMap map2 = CreateInitVerifyPixelMap(width, height, color2);

            map.Stencil(bmp, map2, 1, 1, rect, 2.2);
        }
コード例 #17
0
        private static void TestVerifyEncoderInitialization(IPixelMap pixMap, InterWavePixelMapEncoder map)
        {
            Assert.Null(map._YEncoder);
            Assert.Null(map._YMap);
            Assert.Null(map._CbEncoder);
            Assert.Null(map._CbMap);
            Assert.Null(map._CrEncoder);
            Assert.Null(map._CrMap);

            map.InitializeEncoder(pixMap);

            Assert.Null(map._YEncoder);
            Assert.NotNull(map._YMap);
            Assert.Null(map._CbEncoder);
            Assert.NotNull(map._CbMap);
            Assert.Null(map._CrEncoder);
            Assert.NotNull(map._CrMap);
        }
コード例 #18
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void EqualsIPixelReference()
        {
            int   width  = 16;
            int   height = 16;
            Pixel color  = Pixel.BluePixel;

            IPixelMap map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            IPixelMap map2 = PixelMapTests.CreateInitVerifyPixelMap(width, height, color);
            IPixelMap map3 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.BlackPixel);

            IPixelReference pix1 = map1.CreateGPixelReference(0);
            IPixelReference pix2 = map2.CreateGPixelReference(0);
            IPixelReference pix3 = map3.CreateGPixelReference(0);

            Assert.True(pix1.Equals(pix2));
            Assert.False(pix1.Equals(pix3));
            Assert.False(pix1.Equals((IPixelReference)null));
        }
コード例 #19
0
        public void GetLineTest004()
        {
            int srcWidth     = 48;
            int srcHeight    = 27;
            int targetWidth  = 16;
            int targetHeight = 9;

            var sclr = CreateVerifyPixelMapScaler(srcWidth, srcHeight, targetWidth, targetHeight);

            Rectangle srcRect = new Rectangle
            {
                Right  = 0,
                Left   = srcWidth,
                Bottom = 0,
                Top    = srcHeight
            };

            Rectangle redRect = new Rectangle
            {
                Right  = 0,
                Left   = targetWidth,
                Bottom = 0,
                Top    = targetHeight
            };

            IPixelMap srcMap  = PixelMapTests.CreateInitVerifyPixelMap(srcWidth, srcHeight, Pixel.RedPixel);
            IPixelMap destMap = PixelMapTests.CreateInitVerifyPixelMap(targetWidth, targetHeight, Pixel.WhitePixel);

            sclr._PixelMap1 = srcMap;
            sclr._PixelMap2 = destMap;

            int fy = sclr._L2;

            var line = sclr.GetLine(fy, redRect, srcRect, srcMap);

            Assert.NotNull(line);
        }
コード例 #20
0
ファイル: PixelMapTests.cs プロジェクト: sahwar/DjvuNet
        public static void WritePixelMap(int width, int height, IPixelMap bmp)
        {
            string formatIndex = "x4";
            string formatPixel = "x2";
            var    pix         = bmp.CreateGPixelReference(0);
            var    colorNum    = pix.ColorNumber;

            for (int i = (height - 1); i >= 0; i--)
            {
                Console.Write($"{(i * width).ToString(formatIndex)}  ");
                for (int k = 0; k < width; k++)
                {
                    int rowOffset    = i * width;
                    int columnOffset = k;
                    pix.SetOffset(rowOffset + columnOffset);
                    Console.Write($"{pix.Blue.ToString(formatPixel)}");
                    Console.Write($"{pix.Green.ToString(formatPixel)}");
                    Console.Write($"{pix.Red.ToString(formatPixel)}  ");
                }

                Console.WriteLine();
            }
            Console.WriteLine();
        }
コード例 #21
0
ファイル: PixelReferenceTests.cs プロジェクト: sahwar/DjvuNet
        public void SetPixelsTest002()
        {
            int   width  = 16;
            int   height = 16;
            Pixel color  = Pixel.BlackPixel;
            Pixel spixel = new Pixel(127, 127, 127);
            sbyte scolor = 127;

            Bitmap bmp = new Bitmap();

            bmp.Init(height, width, 0);
            bmp.Fill(scolor);

            IPixelMap map1 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.BlackPixel);
            IPixelMap map2 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.GreenPixel);
            IPixelMap map3 = PixelMapTests.CreateInitVerifyPixelMap(width, height, Pixel.RedPixel);

            var pix1 = map1.CreateGPixelReference(0);
            var pix2 = map2.CreateGPixelReference(0);
            var pix3 = map3.CreateGPixelReference(0);
            var pix4 = bmp.CreateGPixelReference(0);

            Assert.Equal(color, map1.GetPixelAt(0, 8));

            pix1.SetPixels(pix2, 16);
            Assert.Equal(Pixel.GreenPixel, map1.GetPixelAt(0, 8));
            Assert.Equal(color, map1.GetPixelAt(1, 8));

            pix1.SetPixels(pix3, 16);
            Assert.Equal(Pixel.RedPixel, map1.GetPixelAt(1, 8));
            Assert.Equal(color, map1.GetPixelAt(2, 8));

            pix1.SetPixels(pix4, 16);
            Assert.Equal(spixel, map1.GetPixelAt(2, 8));
            Assert.Equal(color, map1.GetPixelAt(3, 8));
        }
コード例 #22
0
        public void EncodeImage_Theory(string fileName)
        {
            string file    = Path.Combine(Util.ArtifactsPath, fileName);
            string outFile = Path.Combine(Util.ArtifactsDataPath, fileName + ".djvu");

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(file))
            {
                bmp.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX);
                int       width  = bmp.Width;
                int       height = bmp.Height;
                IPixelMap pixMap = PixelMapFromBitmap(bmp);

                var map = new InterWavePixelMapEncoder();
                map.InitializeEncoder(pixMap, null, YCrCbMode.Full);

                const int nchunks = 4;
                int[]     slices  = new int[] { 74, 90, 98, 103 };
                //float[] decibel = new float[] { 5.0f, 10.0f, 15.0f, 20.0f };

                InterWaveEncoderSettings[] settings = new InterWaveEncoderSettings[nchunks];
                if (fileName != "")
                {
                    for (int i = 0; i < nchunks; i++)
                    {
                        settings[i] = new InterWaveEncoderSettings {
                            Slices = slices[i]
                        };
                    }
                }
                //else
                //{
                //    for (int i = 0; i < nchunks; i++)
                //    {
                //        settings[i] = new InterWaveEncoderSettings
                //        {
                //            Decibels = decibel[i]
                //        };
                //    }
                //}

                DjvuFormElement form = null;
                using (MemoryStream stream = new MemoryStream())
                    using (IDjvuWriter writer = new DjvuWriter(stream))
                    {
                        form = map.EncodeImage(writer, nchunks, settings);
                    }

                using (IDjvuWriter writer = new DjvuWriter(outFile))
                {
                    form.WriteData(writer);
                }

                using (DjvuDocument doc = new DjvuDocument(outFile))
                {
                    IDjvuPage page     = doc.Pages[0];
                    PM44Form  pageForm = (PM44Form)page.PageForm;

                    if (nchunks != pageForm.Children.Count)
                    {
                        Assert.True(false);
                    }

                    if (nchunks != form.Children.Count)
                    {
                        Assert.True(false);
                    }

                    Assert.IsType <PM44Form>(form);

                    for (int i = 0; i < nchunks; i++)
                    {
                        IDjvuNode c = form.Children[i];

                        if (c == null)
                        {
                            Assert.True(false);
                        }

                        byte[] chunkDataBuffer = null;
                        if (!(c is PM44Chunk chunk))
                        {
                            Assert.True(false);
                        }
コード例 #23
0
ファイル: DjvuPage.cs プロジェクト: sahwar/DjvuNet
        internal bool Stencil(IPixelMap pm, Graphics.Rectangle rect, int subsample, double gamma)
        {
            Verify.SubsampleRange(subsample);

            int width = Width;
            int height = Height;

            if (width <= 0 || height <= 0)
                return false;

            double gammaCorr = 1.0D;

            if (gamma > 0.0D)
            {
                gammaCorr = gamma / Gamma;
            }

            if (gammaCorr < 0.10000000000000001D)
            {
                gammaCorr = 0.10000000000000001D;
            }
            else if (gammaCorr > 10D)
            {
                gammaCorr = 10D;
            }

            JB2Image fgJb2 = ForegroundJB2Image;

            if (fgJb2 != null)
            {
                ColorPalette fgPalette = ForegroundPalette;

                if (fgPalette != null)
                {
                    List<int> components = new List<int>();
                    GBitmap bm = GetBitmapList(rect, subsample, 1, components);

                    if (fgJb2.Blits.Count != fgPalette.BlitColors?.Length)
                    {
                        pm.Attenuate(bm, 0, 0);

                        return false;
                    }

                    GPixmap colors =
                      new PixelMap().Init(1, fgPalette.PaletteColors.Length, null);

                    GPixelReference color = colors.CreateGPixelReference(0);

                    for (int i = 0; i < colors.Width; color.IncOffset())
                        fgPalette.IndexToColor(i++, color);

                    colors.ApplyGammaCorrection(gammaCorr);

                    List<int> compset = new List<int>();

                    while (components.Count > 0)
                    {
                        int lastx = 0;
                        int colorindex = fgPalette.BlitColors[components[0]];
                        GRect comprect = new GRect();
                        compset = new List<int>();

                        for (int pos = 0; pos < components.Count; )
                        {
                            int blitno = ((int)components[pos]);
                            JB2Blit pblit = fgJb2.Blits[blitno];

                            if (pblit.Left < lastx)
                            {
                                break;
                            }

                            lastx = pblit.Left;

                            if (fgPalette.BlitColors[blitno] == colorindex)
                            {
                                JB2Shape pshape = fgJb2.GetShape(pblit.ShapeNumber);
                                GRect xrect = new GRect(pblit.Left, pblit.Bottom,
                                    pshape.Bitmap.Width, pshape.Bitmap.Height);

                                comprect.Recthull(comprect, xrect);
                                compset.Add(components[pos]);
                                components.RemoveAt(pos);
                            }
                            else
                            {
                                pos++;
                            }
                        }

                        comprect.XMin /= subsample;
                        comprect.YMin /= subsample;
                        comprect.XMax = ((comprect.XMax + subsample) - 1) / subsample;
                        comprect.YMax = ((comprect.YMax + subsample) - 1) / subsample;
                        comprect.Intersect(comprect, rect);

                        if (comprect.Empty)
                        {
                            continue;
                        }

                        bm = new DjvuNet.Graphics.Bitmap();
                        bm.Init(comprect.Height, comprect.Width, 0);
                        bm.Grays = 1 + (subsample * subsample);

                        int rxmin = comprect.XMin * subsample;
                        int rymin = comprect.YMin * subsample;

                        for (int pos = 0; pos < compset.Count; ++pos)
                        {
                            int blitno = ((int)compset[pos]);
                            JB2Blit pblit = fgJb2.Blits[blitno];
                            JB2Shape pshape = fgJb2.GetShape(pblit.ShapeNumber);
                            bm.Blit(
                              pshape.Bitmap,
                              pblit.Left - rxmin,
                              pblit.Bottom - rymin,
                              subsample);
                        }

                        color.SetOffset(colorindex);
                        pm.Blit(
                          bm,
                          comprect.XMin - rect.XMin,
                          comprect.YMin - rect.YMin,
                          color);
                    }

                    return true;
                }

                // Three layer model.
                IInterWavePixelMap fgIWPixmap = ForegroundIWPixelMap;

                if (fgIWPixmap != null)
                {
                    GBitmap bm = GetBitmap(rect, subsample, 1, null);

                    if (bm != null && pm != null)
                    {
                        GPixmap fgPixmap = ForegroundPixelMap;
                        int w = fgPixmap.Width;
                        int h = fgPixmap.Height;
                        int red = ComputeRed(width, height, w, h);

                        //          if((red < 1) || (red > 12))
                        if (red < 1 || red > 16)
                            return false;
                        //
                        //          int supersample = (red <= subsample)
                        //            ? 1
                        //            : (red / subsample);
                        //          int wantedred = supersample * subsample;
                        //
                        //          if(red == wantedred)
                        //          {
                        //            pm.stencil(bm, fgPixmap, supersample, rect, gamma_correction);
                        //
                        //            return 1;
                        //          }
                        pm.Stencil(bm, fgPixmap, red, subsample, rect, gammaCorr);
                        return true;
                    }
                }
            }

            return false;
        }
コード例 #24
0
        internal IPixelReference GetLine(int fy, Rectangle redRect, Rectangle srcRect, IPixelMap srcMap)
        {
            if (fy < redRect.YMin)
            {
                fy = redRect.YMin;
            }
            else if (fy >= redRect.YMax)
            {
                fy = redRect.YMax - 1;
            }

            // Cached line
            if (fy == _L2)
            {
                return(_PixelMap2.CreateGPixelReference(0));
            }

            if (fy == _L1)
            {
                return(_PixelMap1.CreateGPixelReference(0));
            }

            // Shift
            IPixelMap p = _PixelMap1;

            _PixelMap1 = _PixelMap2;
            _L1        = _L2;
            _PixelMap2 = p;
            _L2        = fy;

            // Compute location of line
            Rectangle line = new Rectangle();

            line.XMin = redRect.XMin << _XShift;
            line.XMax = redRect.XMax << _XShift;
            line.YMin = fy << _YShift;
            line.YMax = (fy + 1) << _YShift;
            line.Intersect(line, srcRect);
            line.Translate(-srcRect.XMin, -srcRect.YMin);

            // Prepare variables
            int botline = srcMap.RowOffset(line.YMin);
            int rowsize = srcMap.GetRowSize();
            int sw      = 1 << _XShift;
            int div     = _XShift + _YShift;
            int rnd     = 1 << (div - 1);
            int rnd2    = rnd + rnd;

            var inp1 = srcMap.CreateGPixelReference(0);
            var ip   = p.CreateGPixelReference(0);

            // Compute averages
            for (int x = line.XMin; x < line.XMax; x += sw, ip.IncOffset())
            {
                int r    = 0;
                int g    = 0;
                int b    = 0;
                int s    = 0;
                int inp0 = botline + x;
                int sy2  = line.Height;
                int sy1  = (1 << _YShift);

                if (sy1 > sy2)
                {
                    sy1 = sy2;
                }

                for (int sy = 0; sy < sy1; sy++, inp0 += rowsize)
                {
                    int sx1 = x + sw;
                    inp1.SetOffset(inp0);

                    if (sx1 > line.XMax)
                    {
                        sx1 = line.XMax;
                    }

                    for (int sx = sx1 - x; sx-- > 0; s++, inp1.IncOffset())
                    {
                        r += inp1.Red;
                        g += inp1.Green;
                        b += inp1.Blue;
                    }
                }

                if (s == rnd2)
                {
                    ip.SetBGR((b + rnd) >> div, (g + r) >> div, (r + rnd) >> div);
                }
                else
                {
                    ip.SetBGR((b + (s / 2)) / 2, (g + (s / 2)) / s, (r + (s / 2)) / s);
                }
            }
            return(_PixelMap2.CreateGPixelReference(0));
        }
コード例 #25
0
 public static InterWaveImage CreateEncoder(IPixelMap map, IBitmap mask = null, YCrCbMode mode = YCrCbMode.Normal)
 {
     throw new NotImplementedException();
 }
コード例 #26
0
        /// <summary>
        /// Initializes an InterWavePixelMap with color image #bm#.  This constructor
        /// performs the wavelet decomposition of image #bm# and records the
        /// corresponding wavelet coefficients. Argument #mask# is an optional
        /// bilevel image specifying the masked pixels(see \Ref{ IW44Image.h}).
        /// Argument #crcbmode# specifies how the chrominance information should be
        /// encoded(see \Ref{ CRCBMode}).
        /// </summary>
        /// <param name="bm"></param>
        /// <param name="mask"></param>
        /// <param name="mode"></param>
        public unsafe void InitializeEncoder(IPixelMap pm, Bitmap gmask = null, YCrCbMode crcbmode = YCrCbMode.Normal)
        {
            /* Free */
            CloseEncoder();

            // Handle CRCB mode
            switch (crcbmode)
            {
            case YCrCbMode.None:
                _CrCbHalf  = true;
                _CrCbDelay = -1;
                break;

            case YCrCbMode.Half:
                _CrCbHalf  = true;
                _CrCbDelay = 10;
                break;

            case YCrCbMode.Normal:
                _CrCbHalf  = false;
                _CrCbDelay = 10;
                break;

            case YCrCbMode.Full:
                _CrCbHalf  = false;
                _CrCbDelay = 0;
                break;
            }

            // Prepare mask information
            sbyte *msk8       = (sbyte *)IntPtr.Zero;
            int    mskrowsize = 0;

            Bitmap   mask  = gmask;
            GCHandle hMask = default(GCHandle);

            if (mask != null)
            {
                hMask      = GCHandle.Alloc(mask.Data, GCHandleType.Pinned);
                msk8       = (sbyte *)hMask.AddrOfPinnedObject();
                mskrowsize = mask.GetRowSize();
            }

            /* Create */
            int width  = pm.Width;
            int height = pm.Height;

            sbyte[]  sYBuffer = new sbyte[width * height];
            GCHandle hYBuffer = GCHandle.Alloc(sYBuffer, GCHandleType.Pinned);
            sbyte *  yBuffer  = (sbyte *)hYBuffer.AddrOfPinnedObject();

            // Create maps
            InterWaveMapEncoder eymap = new InterWaveMapEncoder(width, height);

            _YMap = eymap;

            GCHandle hData = GCHandle.Alloc(pm.Data, GCHandleType.Pinned);
            Pixel *  pData = (Pixel *)hData.AddrOfPinnedObject();

            // Create chrominance maps
            if (_CrCbDelay >= 0)
            {
                sbyte[]  sCbBuffer = new sbyte[width * height];
                GCHandle hCbBuffer = GCHandle.Alloc(sCbBuffer, GCHandleType.Pinned);
                sbyte *  cbBuffer  = (sbyte *)hCbBuffer.AddrOfPinnedObject();

                sbyte[]  sCrBuffer = new sbyte[width * height];
                GCHandle hCrBuffer = GCHandle.Alloc(sCrBuffer, GCHandleType.Pinned);
                sbyte *  crBuffer  = (sbyte *)hCrBuffer.AddrOfPinnedObject();

                InterWaveMapEncoder ecbmap = new InterWaveMapEncoder(width, height);
                _CbMap = ecbmap;

                InterWaveMapEncoder ecrmap = new InterWaveMapEncoder(width, height);
                _CrMap = ecrmap;

                // Color space conversion from RGB to YCbCr and channel separation
                InterWaveTransform.Rgb2YCbCr(pData, width, height, width * 3, yBuffer, cbBuffer, crBuffer, width);

                // Create YMap
                eymap.Create(yBuffer, width, msk8, mskrowsize);
                // Create CbMap
                ecbmap.Create(cbBuffer, width, msk8, mskrowsize);
                // Create CrMap
                ecrmap.Create(crBuffer, width, msk8, mskrowsize);

                // Perform chrominance reduction (CrCbHalf)
                if (_CrCbHalf)
                {
                    ecbmap.Slashres(2);
                    ecrmap.Slashres(2);
                }

                if (hCbBuffer.IsAllocated)
                {
                    hCbBuffer.Free();
                }

                if (hCrBuffer.IsAllocated)
                {
                    hCrBuffer.Free();
                }
            }
            else
            {
                // Fill buffer with luminance information
                InterWaveTransform.Rgb2Y(pData, width, height, pm.GetRowSize(), yBuffer, width);
                // Create YMAP
                eymap.Create(yBuffer, width, msk8, mskrowsize);

                // Inversion for gray images
                sbyte *e = yBuffer + width * height;
                for (sbyte *b = yBuffer; b < e; b++)
                {
                    *b = (sbyte)(255 - *b);
                }
            }

            if (hMask.IsAllocated)
            {
                hMask.Free();
            }

            hData.Free();
            hYBuffer.Free();
        }
コード例 #27
0
        public void Scale(Rectangle srcRect, IPixelMap srcMap, Rectangle targetRect, IPixelMap targetMap)
        {
            // Parameter validation
            if ((srcRect.Width != srcMap.Width) || (srcRect.Height != srcMap.Height))
            {
                throw new DjvuArgumentException("Invalid rectangle", nameof(srcRect));
            }

            // Compute rectangles
            Rectangle required_red = new Rectangle();
            Rectangle sourceRect   = CreateRectangles(targetRect, required_red);

            if ((srcRect.XMin > sourceRect.XMin) || (srcRect.YMin > sourceRect.YMin) ||
                (srcRect.XMax < sourceRect.XMax) || (srcRect.YMax < sourceRect.YMax))
            {
                throw new DjvuArgumentException("Invalid rectangle", nameof(srcRect));
            }

            // Adjust output pixmap
            if ((targetRect.Width != (int)targetMap.Width) || (targetRect.Height != (int)targetMap.Height))
            {
                targetMap.Init(targetRect.Height, targetRect.Width, null);
            }

            // Prepare temp stuff
            int bufw = required_red.Width;

            Pixel[] lbuffer = new Pixel[bufw + 2];

            try
            {
                if ((_XShift > 0) || (_YShift > 0))
                {
                    _PixelMap1 = new PixelMap().Init(1, bufw, null);
                    _PixelMap2 = new PixelMap().Init(2, bufw, null);
                    _L1        = _L2 = -1;
                }

                IPixelReference upper = srcMap.CreateGPixelReference(0, 0);
                IPixelReference lower = srcMap.CreateGPixelReference(0, 0);
                IPixelReference dest  = targetMap.CreateGPixelReference(0, 0);

                // Loop on output lines
                for (int y = targetRect.YMin; y < targetRect.YMax; y++)
                {
                    // Perform vertical interpolation
                    {
                        int fy  = _VCoord[y];
                        int fy1 = fy >> FRACBITS;
                        int fy2 = fy1 + 1;

                        // Obtain upper and lower line in reduced image
                        if ((_XShift > 0) || (_YShift > 0))
                        {
                            lower = GetLine(fy1, required_red, srcRect, srcMap);
                            upper = GetLine(fy2, required_red, srcRect, srcMap);
                        }
                        else
                        {
                            int dx = required_red.XMin - srcRect.XMin;

                            if (required_red.YMin > fy1)
                            {
                                fy1 = required_red.YMin;
                            }

                            if (required_red.YMax <= fy2)
                            {
                                fy2 = required_red.YMax - 1;
                            }

                            lower.SetOffset(fy1 - srcRect.YMin, dx);
                            // srcMap.CreateGPixelReference(fy1 - srcRect.YMin, dx);
                            upper.SetOffset(fy2 - srcRect.YMin, dx);
                            // srcMap.CreateGPixelReference(fy2 - srcRect.YMin, dx);
                        }

                        // Compute line
                        int     idest  = 1;
                        short[] deltas = interp[fy & FRACMASK];

                        unsafe
                        {
                            for (int edest = idest + bufw; idest < edest; upper.IncOffset(), lower.IncOffset())
                            {
                                Pixel destPix = lbuffer[idest++];

                                int    color    = 0;
                                sbyte *colorPtr = (sbyte *)&color;
                                // Skip alpha and set pointer to Blue
                                colorPtr++;
                                *colorPtr  = lower.Blue;
                                *colorPtr += (sbyte)deltas[(256 + upper.Blue) - *colorPtr];

                                // Set pointer to Green
                                colorPtr++;
                                *colorPtr  = lower.Green;
                                *colorPtr += (sbyte)deltas[(256 + upper.Green) - *colorPtr];

                                // Set pointer to Red
                                colorPtr++;
                                *colorPtr  = lower.Red;
                                *colorPtr += (sbyte)deltas[(256 + upper.Red) - *colorPtr];

                                //Pixel d = (Pixel) lower.ToPixel();
                                //destPix.SetBGR(d);
                                destPix.SetBGR(*colorPtr);
                            }
                        }
                    }

                    // Perform horizontal interpolation
                    {
                        // Prepare for side effects
                        lbuffer[0] = lbuffer[1];

                        // lbuffer[bufw] = lbuffer[bufw];
                        int line = 1 - required_red.XMin;
                        dest.SetOffset(y - targetRect.YMin, 0);
                        //= targetMap.CreateGPixelReference(y - targetRect.YMin, 0);

                        // Loop horizontally
                        unsafe
                        {
                            for (int x = targetRect.XMin; x < targetRect.XMax; x++)
                            {
                                int     n      = _HCoord[x];
                                int     lowerl = line + (n >> FRACBITS);
                                Pixel   lower0 = lbuffer[lowerl];
                                Pixel   lower1 = lbuffer[lowerl + 1];
                                short[] deltas = interp[n & FRACMASK];

                                int    color    = 0;
                                sbyte *colorPtr = (sbyte *)&color;
                                // Skip alpha and set pointer to Blue
                                colorPtr++;
                                *colorPtr  = lower0.Blue;
                                *colorPtr += (sbyte)deltas[(256 + lower1.Blue) - *colorPtr];

                                // Set pointer to Green
                                colorPtr++;
                                *colorPtr  = lower0.Green;
                                *colorPtr += (sbyte)deltas[(256 + lower1.Green) - *colorPtr];

                                // Set pointer to Red
                                colorPtr++;
                                *colorPtr  = lower0.Red;
                                *colorPtr += (sbyte)deltas[(256 + lower1.Red) - *colorPtr];

                                dest.SetBGR(*colorPtr);
                                dest.IncOffset();
                            }
                        }
                    }
                }
            }
            finally
            {
                _PixelMap1 = null;
                _PixelMap2 = null;
            }
        }