Exemplo n.º 1
0
        private JB2Image ReadCompressedImage()
        {
            using (DjvuReader reader = Reader.CloneReader(_dataLocation, Length))
            {
                JB2Image image = new JB2Image();

                JB2.JB2Dictionary includedDictionary = null;

                if (Parent is FormChunk)
                {
                    InclChunk[] includes = ((FormChunk)Parent).IncludedItems;

                    if (includes != null && includes.Count() > 0)
                    {
                        string includeID   = includes.FirstOrDefault().IncludeID;
                        var    includeItem = Document.GetChunkByID <DjbzChunk>(includeID);

                        if (includeItem != null)
                        {
                            includedDictionary = includeItem.ShapeDictionary;
                        }
                    }
                }

                image.Decode(reader, includedDictionary);

                return(image);
            }
        }
Exemplo n.º 2
0
        internal bool IsLegalCompound()
        {
            int width = Width;
            int height = Height;

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

            JB2Image fgJb2 = ForegroundJB2Image;

            if (fgJb2 == null || fgJb2.Width != width || fgJb2.Height != height)
                return false;

            // There is no need to synchronize since we won't access data which could be updated.
            IInterWavePixelMap bgIWPixmap = (IInterWavePixelMap)BackgroundIWPixelMap;
            int bgred = 0;

            if (bgIWPixmap != null)
                bgred = ComputeRed(width, height, bgIWPixmap.Width, bgIWPixmap.Height);

            if ((bgred < 1) || (bgred > 12))
                return false;

            int fgred = 0;

            if (ForegroundIWPixelMap != null)
            {
                GPixmap fgPixmap = ForegroundPixelMap;
                fgred = ComputeRed(width, height, fgPixmap.Width, fgPixmap.Height);
            }

            return ((fgred >= 1) && (fgred <= 12));
        }
Exemplo n.º 3
0
        public GBitmap GetBitmapList(GRect rect, int subsample, int align, List <int> components)
        {
            if (rect.Empty)
            {
                return(new Graphics.Bitmap());
            }

            if (Info != null)
            {
                int width  = Info.Width;
                int height = Info.Height;

                JB2Image fgJb2 = ForegroundJB2Image;

                if (
                    (width != 0) &&
                    (height != 0) &&
                    (fgJb2 != null) &&
                    (fgJb2.Width == width) &&
                    (fgJb2.Height == height))
                {
                    return(fgJb2.GetBitmap(rect, subsample, align, 0, components));
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        private bool IsLegalBilevel()
        {
            if (Info == null)
            {
                return(false);
            }

            int width  = Info.Width;
            int height = Info.Height;

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

            JB2Image fgJb2 = ForegroundJB2Image;

            if ((fgJb2 == null) || (fgJb2.Width != width) || (fgJb2.Height != height))
            {
                return(false);
            }

            return(!(BackgroundIWPixelMap != null || ForegroundIWPixelMap != null ||
                     ForegroundPalette != null));
        }
Exemplo n.º 5
0
        internal DjvuNet.Graphics.Map GetForegroundMap()
        {
            lock (LoadingLock)
            {
                DjvuNet.Graphics.Map result     = null;
                JB2Image             jb2image   = null;
                IInterWavePixelMap   iwPixelMap = _Page.ForegroundIWPixelMap;

                if (iwPixelMap != null)
                {
                    result = _Page.ForegroundIWPixelMap.GetPixelMap();
                }
                else if ((jb2image = _Page.ForegroundJB2Image) != null)
                {
                    if (_Page.ForegroundPalette == null)
                    {
                        result = jb2image.GetBitmap(1, GBitmap.BorderSize);
                    }
                    else
                    {
                        result = jb2image.GetPixelMap(_Page.ForegroundPalette, 1, 16);
                    }
                }
                else if (iwPixelMap == null && jb2image == null)
                {
                    result = new GBitmap(_Page.Height, _Page.Width, GBitmap.BorderSize);
                }

                return(result);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the foreground image for the page
        /// </summary>
        /// <param name="resizeToPage"></param>
        /// <returns></returns>
        internal System.Drawing.Bitmap GetForegroundImage(int subsample, bool resizeImage = false)
        {
            Verify.SubsampleRange(subsample);

            lock (LoadingLock)
            {
                Bitmap result = null;

                JB2Image           jb2image   = null;
                IInterWavePixelMap iwPixelMap = _Page.ForegroundIWPixelMap;

                if (iwPixelMap != null)
                {
                    result = _Page.ForegroundIWPixelMap.GetPixelMap().ToImage();
                }
                else if ((jb2image = _Page.ForegroundJB2Image) != null)
                {
                    if (_Page.ForegroundPalette == null)
                    {
                        result = jb2image.GetBitmap(1, GBitmap.BorderSize).ToImage();
                    }
                    else
                    {
                        result = jb2image.GetPixelMap(_Page.ForegroundPalette, 1, 16).ToImage();
                    }
                }
                else if (iwPixelMap == null && jb2image == null)
                {
                    result = CreateBlankImage(Brushes.Black, _Page.Width / subsample, _Page.Height / subsample);
                }

                return(resizeImage ? ResizeImage(result, _Page.Width / subsample, _Page.Height / subsample) : result);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the foreground image for the page.
        /// </summary>
        /// <param name="resizeToPage"></param>
        /// <returns></returns>
        public static Bitmap GetForegroundImage(this DjvuImage image, int subsample, bool resizeImage = false)
        {
            Verify.SubsampleRange(subsample);

            lock (image.LoadingLock)
            {
                Bitmap result = null;

                JB2Image           jb2image   = null;
                IInterWavePixelMap iwPixelMap = image.Page.ForegroundIWPixelMap;

                if (iwPixelMap != null)
                {
                    result = image.Page.ForegroundIWPixelMap.GetPixelMap().ToImage();
                }
                else if ((jb2image = image.Page.ForegroundJB2Image) != null)
                {
                    if (image.Page.ForegroundPalette == null)
                    {
                        result = jb2image.GetBitmap(1, GBitmap.BorderSize).ToImage();
                    }
                    else
                    {
                        result = jb2image.GetPixelMap(image.Page.ForegroundPalette, 1, 16).ToImage();
                    }
                }
                else if (iwPixelMap == null && jb2image == null)
                {
                    result = CreateBlankImage(Brushes.Black, image.Page.Width / subsample, image.Page.Height / subsample);
                }

                return(resizeImage ? ResizeImage(result, image.Page.Width / subsample, image.Page.Height / subsample) : result);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the foreground image for the page
        /// </summary>
        /// <param name="resizeToPage"></param>
        /// <returns></returns>
        internal System.Drawing.Bitmap GetForegroundImage(int subsample, bool resizeImage = false)
        {
            Verify.SubsampleRange(subsample);

            lock (_LoadingLock)
            {
                Bitmap result = null;

                JB2Image jb2image   = null;
                var      iwPixelMap = _Page.ForegroundIWPixelMap;
                if (iwPixelMap != null)
                {
                    result = _Page.ForegroundIWPixelMap.GetPixelMap().ToImage();
                }
                else if ((jb2image = _Page.ForegroundJB2Image) != null)
                {
                    result = jb2image.GetBitmap().ToImage();
                }
                else if (iwPixelMap == null && jb2image == null)
                {
                    result = DjvuImage.CreateBlankImage(Brushes.Black, _Page.Width / subsample, _Page.Height / subsample);
                }

                return(resizeImage == true?DjvuImage.ResizeImage(result, _Page.Width / subsample, _Page.Height / subsample) : result);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Reads the image which is used to remove the watermark
        /// </summary>
        /// <returns></returns>
        private JB2Image ReadCompressedWatermarkImage()
        {
            using (DjvuReader reader = Reader.CloneReader(_dataLocation, Length))
            {
                JB2Image image = new JB2Image();
                image.Decode(reader);

                return(image);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Reads the image which is used to remove the watermark
        /// </summary>
        /// <returns></returns>
        internal JB2Image ReadCompressedWatermarkImage()
        {
            using (IDjvuReader reader = Reader.CloneReaderToMemory(DataOffset, Length))
            {
                JB2Image image = new JB2Image();
                image.Decode(reader);

                return(image);
            }
        }
Exemplo n.º 11
0
        private bool IsLegalCompound()
        {
            if (Info == null)
            {
                return(false);
            }

            int width  = Info.Width;
            int height = Info.Height;

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

            JB2Image fgJb2 = ForegroundJB2Image;

            if ((fgJb2 == null) || (fgJb2.Width != width) || (fgJb2.Height != height))
            {
                return(false);
            }

            // There is no need to synchronize since we won't access data which could be updated.
            IWPixelMap bgIWPixmap = (IWPixelMap)BackgroundIWPixelMap;
            int        bgred      = 0;

            if (bgIWPixmap != null)
            {
                bgred =
                    ComputeRed(
                        width,
                        height,
                        bgIWPixmap.Width,
                        bgIWPixmap.Height);
            }

            if ((bgred < 1) || (bgred > 12))
            {
                return(false);
            }

            int fgred = 0;

            if (ForegroundIWPixelMap != null)
            {
                GPixmap fgPixmap = ForegroundPixelMap;
                fgred = ComputeRed(
                    width,
                    height,
                    fgPixmap.ImageWidth,
                    fgPixmap.ImageHeight);
            }

            return((fgred >= 1) && (fgred <= 12));
        }
Exemplo n.º 12
0
        internal bool IsLegalBilevel()
        {
            int width = Width;
            int height = Height;

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

            JB2Image fgJb2 = ForegroundJB2Image;

            if (fgJb2 == null || fgJb2.Width != width || fgJb2.Height != height)
                return false;

            return !(BackgroundIWPixelMap != null || ForegroundIWPixelMap != null
                     || ForegroundPalette != null);
        }
Exemplo n.º 13
0
        public GBitmap GetBitmapList(GRect rect, int subsample, int align, List<int> components)
        {
            Verify.SubsampleRange(subsample);

            if (rect.Empty)
                return new Graphics.Bitmap();

            int width = Width;
            int height = Height;

            JB2Image fgJb2 = ForegroundJB2Image;

            if (width != 0 && height != 0 && fgJb2 != null && fgJb2.Width == width && fgJb2.Height == height)
                return fgJb2.GetBitmap(rect, subsample, align, 0, components);

            return null;
        }
Exemplo n.º 14
0
        internal JB2Image ReadCompressedImage()
        {
            using (IDjvuReader reader = Reader.CloneReaderToMemory(DataOffset, Length))
            {
                JB2Image          image = new JB2Image();
                JB2.JB2Dictionary includedDictionary = null;

                if (Parent is DjvuChunk djvuChunk)
                {
                    IReadOnlyList <InclChunk> includes = djvuChunk.IncludedItems;

                    if (includes?.Count > 0)
                    {
                        var includeIDs = includes
                                         .Where <InclChunk>(x => x.ChunkType == ChunkType.Incl);
                        var       root     = Document.RootForm as DjvmChunk;
                        DjbzChunk djbzItem = null;

                        foreach (InclChunk iChunk in includeIDs)
                        {
                            DirmComponent component = root?.Dirm.Components
                                                      .Where <DirmComponent>(x => x.ID == iChunk.IncludeID).FirstOrDefault();

                            var includeForm = root.Includes
                                              .FirstOrDefault <IDjviChunk>(x => x.DataOffset == (component.Offset + 12));

                            djbzItem = includeForm?.Children
                                       .FirstOrDefault(x => x.ChunkType == ChunkType.Djbz) as DjbzChunk;

                            if (djbzItem != null)
                            {
                                break;
                            }
                        }

                        includedDictionary = djbzItem?.ShapeDictionary;
                    }
                }

                image.Decode(reader, includedDictionary);

                return(image);
            }
        }
Exemplo n.º 15
0
        internal JB2Image ReadCompressedImage()
        {
            using (IDjvuReader reader = Reader.CloneReaderToMemory(DataOffset, Length))
            {
                JB2Image          image = new JB2Image();
                JB2.JB2Dictionary includedDictionary = null;
                DjvuChunk         djvuChunk          = Parent as DjvuChunk;

                if (djvuChunk != null)
                {
                    var includes = djvuChunk.IncludedItems;

                    if (includes?.Count > 0)
                    {
                        string includeID = includes
                                           .FirstOrDefault <InclChunk>(x => x.ChunkType == ChunkType.Incl)?.IncludeID;
                        DjvmChunk     root      = Document.RootForm as DjvmChunk;
                        DirmComponent component = root?.Dirm.Components
                                                  .Where <DirmComponent>(x => x.ID == includeID).FirstOrDefault();

                        var includeForm =
                            root.Includes
                            .Where(x => x.DataOffset == (component.Offset + 12))
                            .FirstOrDefault <IDjviChunk>();

                        var djbzItem = includeForm?.Children
                                       .Where <IDjvuNode>(x => x.ChunkType == ChunkType.Djbz).FirstOrDefault() as DjbzChunk;

                        includedDictionary = djbzItem?.ShapeDictionary;
                    }
                }

                image.Decode(reader, includedDictionary);

                return(image);
            }
        }
Exemplo n.º 16
0
        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;
        }