示例#1
0
        public virtual int AddBlit(JB2Blit jb2Blit)
        {
            if (jb2Blit.ShapeNumber >= ShapeCount)
            {
                throw new ArgumentException("Image bad shape");
            }

            int retval = _blits.Count;
            _blits.Add(jb2Blit);

            return retval;
        }
示例#2
0
        protected override void CodeAbsoluteLocation(JB2Blit jblt, int rows, int columns)
        {
            if (!GotStartRecordP)
            {
                throw new SystemException("Image no start");
            }

            int left = CodeNum(1, ImageColumns, AbsLocX);
            int top = CodeNum(1, ImageRows, AbsLocY);

            jblt.Bottom = top - rows;
            jblt.Left = left - 1;
        }
示例#3
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;
        }
示例#4
0
        protected virtual void CodeRelativeLocation(JB2Blit jblt, int rows, int columns)
        {
            if (!GotStartRecordP)
            {
                throw new SystemException("Image no start");
            }

            int bottom = 0;
            int left = 0;
            int top = 0;
            int right = 0;

            if (_encoding)
            {
                left = jblt.Left + 1;
                bottom = jblt.Bottom + 1;
                right = (left + columns) - 1;
                top = (bottom + rows) - 1;
            }

            bool new_row = CodeBit((left < _lastLeft), _offsetTypeDist);

            if (new_row)
            {
                int x_diff = GetDiff(left - _lastRowLeft, rel_loc_x_last);
                int y_diff = GetDiff(top - _lastRowBottom, rel_loc_y_last);

                if (!_encoding)
                {
                    left = _lastRowLeft + x_diff;
                    top = _lastRowBottom + y_diff;
                    right = (left + columns) - 1;
                    bottom = (top - rows) + 1;
                }

                _lastLeft = _lastRowLeft = left;
                _lastRight = right;
                _lastBottom = _lastRowBottom = bottom;
                FillShortList(bottom);
            }
            else
            {
                int x_diff = GetDiff(left - _lastRight, rel_loc_x_current);
                int y_diff = GetDiff(bottom - _lastBottom, rel_loc_y_current);

                if (!_encoding)
                {
                    left = _lastRight + x_diff;
                    bottom = _lastBottom + y_diff;
                    right = (left + columns) - 1;
                    top = (bottom + rows) - 1;
                }

                _lastLeft = left;
                _lastRight = right;
                _lastBottom = UpdateShortList(bottom);
            }

            if (!_encoding)
            {
                jblt.Bottom = bottom - 1;
                jblt.Left = left - 1;
            }
        }
示例#5
0
        protected virtual int CodeRecordB(int rectype, JB2Image jim, JB2Shape xjshp, JB2Blit xjblt)
        {
            Bitmap bm = null;
            int shapeno = -1;
            JB2Shape jshp = xjshp;
            JB2Blit jblt = xjblt;
            rectype = CodeRecordType(rectype);

            switch (rectype)
            {
                case NewMark:
                case NewMarkImageOnly:
                case MatchedRefine:
                case MatchedRefineImageOnly:
                case NonMarkData:
                    {
                        if (jblt == null)
                        {
                            jblt = new JB2Blit();
                        }

                        // fall through
                    }
                    goto case NewMarkLibraryOnly;

                case NewMarkLibraryOnly:
                case MatchedRefineLibraryOnly:
                    {
                        if (!_encoding)
                        {
                            jshp = new JB2Shape().Init((rectype == NonMarkData) ? (-2) : (-1));
                        }
                        else if (jshp == null)
                        {
                            jshp = new JB2Shape();
                        }

                        bm = jshp.Bitmap;

                        break;
                    }

                case MatchedCopy:
                    {
                        if (jblt == null)
                        {
                            jblt = new JB2Blit();
                        }

                        break;
                    }
            }

            bool needAddLibrary = false;
            bool needAddBlit = false;

            switch (rectype)
            {
                case StartOfData:
                    {
                        CodeImageSize(jim);
                        CodeEventualLosslessRefinement();

                        if (!_encoding)
                        {
                            InitLibrary(jim);
                        }

                        break;
                    }

                case NewMark:
                    {
                        needAddBlit = needAddLibrary = true;
                        CodeAbsoluteMarkSize(bm, 4);
                        CodeBitmapDirectly(bm);
                        CodeRelativeLocation(jblt, bm.ImageHeight, bm.ImageWidth);

                        break;
                    }

                case NewMarkLibraryOnly:
                    {
                        needAddLibrary = true;
                        CodeAbsoluteMarkSize(bm, 4);
                        CodeBitmapDirectly(bm);

                        break;
                    }

                case NewMarkImageOnly:
                    {
                        needAddBlit = true;
                        CodeAbsoluteMarkSize(bm, 3);
                        CodeBitmapDirectly(bm);
                        CodeRelativeLocation(jblt, bm.ImageHeight, bm.ImageWidth);

                        break;
                    }

                case MatchedRefine:
                    {
                        needAddBlit = true;
                        needAddLibrary = true;

                        int match = CodeMatchIndex(jshp.Parent, jim);

                        if (!_encoding)
                        {
                            jshp.Parent = Convert.ToInt32((Lib2Shape[match]));
                        }

                        Bitmap cbm = jim.GetShape(jshp.Parent).Bitmap;
                        Rectangle lmatch = (Rectangle)_libinfo[match];
                        CodeRelativeMarkSize(bm, (1 + lmatch.Left) - lmatch.Right, (1 + lmatch.Top) - lmatch.Bottom, 4);

                        //          verbose("2.d time="+System.currentTimeMillis()+",rectype="+rectype);
                        CodeBitmapByCrossCoding(bm, cbm, match);

                        //          verbose("2.e time="+System.currentTimeMillis()+",rectype="+rectype);
                        CodeRelativeLocation(jblt, bm.ImageHeight, bm.ImageWidth);

                        break;
                    }

                case MatchedRefineLibraryOnly:
                    {
                        needAddLibrary = true;

                        int match = CodeMatchIndex(jshp.Parent, jim);

                        if (!_encoding)
                        {
                            jshp.Parent = Convert.ToInt32((Lib2Shape[match]));
                        }

                        Bitmap cbm = jim.GetShape(jshp.Parent).Bitmap;
                        Rectangle lmatch = (Rectangle)_libinfo[match];
                        CodeRelativeMarkSize(bm, (1 + lmatch.Left) - lmatch.Right, (1 + lmatch.Top) - lmatch.Bottom, 4);

                        break;
                    }

                case MatchedRefineImageOnly:
                    {
                        needAddBlit = true;

                        int match = CodeMatchIndex(jshp.Parent, jim);

                        if (!_encoding)
                        {
                            jshp.Parent = Convert.ToInt32((Lib2Shape[match]));
                        }

                        Bitmap cbm = jim.GetShape(jshp.Parent).Bitmap;
                        Rectangle lmatch = (Rectangle)_libinfo[match];
                        CodeRelativeMarkSize(bm, (1 + lmatch.Left) - lmatch.Right, (1 + lmatch.Top) - lmatch.Bottom, 4);
                        CodeBitmapByCrossCoding(bm, cbm, match);
                        CodeRelativeLocation(jblt, bm.ImageHeight, bm.ImageWidth);

                        break;
                    }

                case MatchedCopy:
                    {
                        int match = CodeMatchIndex(jblt.ShapeNumber, jim);

                        if (!_encoding)
                        {
                            jblt.ShapeNumber = Convert.ToInt32((Lib2Shape[match]));
                        }

                        bm = jim.GetShape(jblt.ShapeNumber).Bitmap;

                        Rectangle lmatch = (Rectangle)_libinfo[match];
                        jblt.Left += lmatch.Right;
                        jblt.Bottom += lmatch.Bottom;

                        CodeRelativeLocation(jblt, (1 + lmatch.Top) - lmatch.Bottom,
                                               (1 + lmatch.Left) - lmatch.Right);

                        jblt.Left -= lmatch.Right;
                        jblt.Bottom -= lmatch.Bottom;

                        break;
                    }

                case NonMarkData:
                    {
                        needAddBlit = true;
                        CodeAbsoluteMarkSize(bm, 3);
                        CodeBitmapDirectly(bm);
                        CodeAbsoluteLocation(jblt, bm.ImageHeight, bm.ImageWidth);

                        break;
                    }

                case PreservedComment:
                    {
                        jim.Comment = CodeComment(jim.Comment);

                        break;
                    }

                case RequiredDictOrReset:
                    {
                        if (!GotStartRecordP)
                        {
                            CodeInheritedShapeCount(jim);
                        }
                        else
                        {
                            ResetNumcoder();
                        }

                        break;
                    }

                case EndOfData:
                    break;

                default:
                    throw new ArgumentException("Image unknown type");
            }

            if (!_encoding)
            {
                switch (rectype)
                {
                    case NewMark:
                    case NewMarkLibraryOnly:
                    case MatchedRefine:
                    case MatchedRefineLibraryOnly:
                    case NewMarkImageOnly:
                    case MatchedRefineImageOnly:
                    case NonMarkData:
                        {
                            if (xjshp != null)
                            {
                                jshp = jshp.Duplicate();
                            }

                            shapeno = jim.AddShape(jshp);
                            Shape2Lib(shapeno, MinusOneObject);

                            if (needAddLibrary)
                            {
                                AddLibrary(shapeno, jshp);
                            }

                            if (needAddBlit)
                            {
                                jblt.ShapeNumber = shapeno;

                                if (xjblt != null)
                                {
                                    jblt = (JB2Blit)xjblt.Duplicate();
                                }

                                jim.AddBlit(jblt);
                            }

                            break;
                        }

                    case MatchedCopy:
                        {
                            if (xjblt != null)
                            {
                                jblt = (JB2Blit)xjblt.Duplicate();
                            }

                            jim.AddBlit(jblt);

                            break;
                        }
                }
            }

            return rectype;
        }
示例#6
0
 protected abstract void CodeAbsoluteLocation(JB2Blit jblt, int rows, int columns);