Exemplo n.º 1
0
        public GPixmap GetPixelMap(GRect rect, int subsample, double gamma, GPixmap retval)
        {
            Verify.SubsampleRange(subsample);

            if (rect?.Empty != false)
            {
                return((retval == null) ? (new PixelMap()) : retval.Init(0, 0, null));
            }

            GPixmap bg = GetBgPixmap(rect, subsample, gamma, retval);

            if (ForegroundJB2Image != null)
            {
                if (bg == null)
                {
                    bg = (retval == null) ? new PixelMap() : retval;
                    bg.Init(rect.Height, rect.Width, _IsInverted ? Pixel.BlackPixel : Pixel.WhitePixel);
                }

                if (Stencil(bg, rect, subsample, gamma))
                {
                    retval = bg;
                }
            }
            else
            {
                retval = bg;
            }

            return(retval);
        }
Exemplo n.º 2
0
        public GMap GetMap(GRect segment, int subsample, GMap retval)
        {
            Verify.SubsampleRange(subsample);

            if (IsColor)
                retval = GetPixelMap(segment, subsample, 0.0D,
                    (retval is GPixmap) ? (GPixmap)retval : null);
            else
                retval = GetBitmap(segment, subsample, 1,
                        (retval is GBitmap) ? (GBitmap)retval : null);

            return retval;
        }
Exemplo n.º 3
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.º 4
0
        public Map GetMap(GRect segment, int subsample, GMap retval)
        {
            retval =
               IsColor
              ? (GMap)GetPixelMap(
                segment,
                subsample,
                0.0D,
                (retval is GPixmap)
                ? (GPixmap)retval
                : null)
              : (GMap)GetBitmap(
                segment,
                subsample,
                1,
                ((retval is GBitmap)
                ? (GBitmap)retval
                : null));

            return retval;
        }
Exemplo n.º 5
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.º 6
0
        public virtual Graphics.PixelMap GetPixmap(int subsample, Rectangle rect, Graphics.PixelMap retval)
        {
            if (_ymap == null)
            {
                return null;
            }

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

            int w = rect.Width;
            int h = rect.Height;
            int pixsep = 3;
            int rowsep = w * pixsep;
            sbyte[] bytes = retval.Init(h, w, 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);
            }

            PixelReference pixel = retval.CreateGPixelReference(0);

            for (int i = 0; i < h; )
            {
                pixel.SetOffset(i++, 0);

                if ((_crmap != null) && (_cbmap != null) && (_crcbDelay >= 0))
                {
                    pixel.YCC_to_RGB(w);
                }
                else
                {
                    for (int x = w; x-- > 0; pixel.IncOffset())
                    {
                        pixel.SetGray((sbyte)(127 - pixel.Blue));
                    }
                }
            }

            return retval;
        }
Exemplo n.º 7
0
 public GBitmap GetBitmap(GRect rect, int subsample, int align, GBitmap retval)
 {
     Verify.SubsampleRange(subsample);
     return GetBitmapList(rect, 1, 1, null);
 }
Exemplo n.º 8
0
        public Rectangle CreateRectangles(Rectangle desired, Rectangle red)
        {
            Rectangle inp = new Rectangle();

            // Parameter validation
            if (
              (desired.XMin < 0)
              || (desired.YMin < 0)
              || (desired.XMax > destWidth)
              || (desired.YMax > destHeight))
            {
                throw new Exception(
                  "desired rectangle too big: " + desired.XMin + "," + desired.YMin
                  + "," + desired.XMax + "," + desired.YMax + "," + destWidth + ","
                  + destHeight);
            }

            // Compute ratio (if not done yet)
            if (vcoord == null)
            {
                SetVertRatio(0, 0);
            }

            if (hcoord == null)
            {
                SetHorzRatio(0, 0);
            }

            // Compute reduced bounds
            red.XMin = (hcoord[desired.XMin]) >> FRACBITS;
            red.YMin = (vcoord[desired.YMin]) >> FRACBITS;
            red.XMax = ((hcoord[desired.XMax - 1] + FRACSIZE) - 1) >> FRACBITS;
            red.YMax = ((vcoord[desired.YMax - 1] + FRACSIZE) - 1) >> FRACBITS;

            // Borders
            red.XMin = (red.XMin > 0)
              ? red.XMin
              : 0;
            red.XMax = (red.XMax < redw)
              ? (red.XMax + 1)
              : redw;
            red.YMin = (red.YMin > 0)
              ? red.YMin
              : 0;
            red.YMax = (red.YMax < redh)
              ? (red.YMax + 1)
              : redh;

            // Input
            inp.XMin = red.XMin << xshift;

            if (inp.XMin < 0)
            {
                inp.XMin = 0;
            }

            inp.XMax = red.XMax << xshift;

            if (inp.XMax > srcWidth)
            {
                inp.XMax = srcWidth;
            }

            inp.YMin = red.YMin << yshift;

            if (inp.YMin < 0)
            {
                inp.YMin = 0;
            }

            inp.YMax = red.YMax << yshift;

            if (inp.YMax > srcHeight)
            {
                inp.YMax = srcHeight;
            }

            return inp;
        }
Exemplo n.º 9
0
        public void Scale(Rectangle provided_input, PixelMap sourceMap, Rectangle targetRect, PixelMap output)
        {
            // Compute rectangles
            Rectangle required_red = new Rectangle();
            Rectangle sourceRect = CreateRectangles(targetRect, required_red);

            // Parameter validation
            if (
              (provided_input.Width != sourceMap.ImageWidth)
              || (provided_input.Height != sourceMap.ImageHeight))
            {
                throw new Exception("invalid rectangle");
            }

            if (
              (provided_input.XMin > sourceRect.XMin)
              || (provided_input.YMin > sourceRect.YMin)
              || (provided_input.XMax < sourceRect.XMax)
              || (provided_input.YMax < sourceRect.YMax))
            {
                throw new Exception("invalid rectangle");
            }

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

            // Prepare temp stuff
            int bufw = required_red.Width;
            Pixel[] lbuffer = new Pixel[bufw + 2];

            for (int i = 0; i < lbuffer.Length; )
            {
                lbuffer[i++] = new Pixel();
            }

            try
            {
                if ((xshift > 0) || (yshift > 0))
                {
                    p1 = new PixelMap().Init(1, bufw, null);
                    p2 = new PixelMap().Init(2, bufw, null);
                    l1 = l2 = -1;
                }

                // 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;
                        PixelReference upper;
                        PixelReference lower;

                        // Obtain upper and lower line in reduced image
                        if ((xshift > 0) || (yshift > 0))
                        {
                            lower = GetLine(fy1, required_red, provided_input, sourceMap);
                            upper = GetLine(fy2, required_red, provided_input, sourceMap);
                        }
                        else
                        {
                            int dx = required_red.XMin - provided_input.XMin;

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

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

                            lower =
                              sourceMap.CreateGPixelReference(fy1 - provided_input.YMin, dx);
                            upper =
                              sourceMap.CreateGPixelReference(fy2 - provided_input.YMin, dx);
                        }

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

                        for (
                          int edest = idest + bufw;
                          idest < edest;
                          upper.IncOffset(), lower.IncOffset())
                        {
                            Pixel dest = lbuffer[idest++];
                            int lower_r = lower.Red;
                            int delta_r = deltas[(256 + upper.Red) - lower_r];
                            int lower_g = lower.Green;
                            int delta_g = deltas[(256 + upper.Green) - lower_g];
                            int lower_b = lower.Blue;
                            int delta_b = deltas[(256 + upper.Blue) - lower_b];
                            dest.SetBGR(lower_b + delta_b, lower_g + delta_g, lower_r + delta_r);
                        }
                    }

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

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

                        // Loop horizontally
                        for (int x = targetRect.XMin; x < targetRect.XMax; x++)
                        {
                            int n = hcoord[x];
                            int lower = line + (n >> FRACBITS);
                            Pixel lower0 = lbuffer[lower];
                            Pixel lower1 = lbuffer[lower + 1];
                            short[] deltas = interp[n & FRACMASK];
                            int lower_r = lower0.Red;
                            int delta_r = deltas[(256 + lower1.Red) - lower_r];
                            int lower_g = lower0.Green;
                            int delta_g = deltas[(256 + lower1.Green) - lower_g];
                            int lower_b = lower0.Blue;
                            int delta_b = deltas[(256 + lower1.Blue) - lower_b];
                            dest.SetBGR(lower_b + delta_b, lower_g + delta_g, lower_r + delta_r);
                            dest.IncOffset();
                        }
                    }
                }
            }
            finally
            {
                p1 = null;
                p2 = null;
            }
        }
Exemplo n.º 10
0
 public Bitmap GetBitmap(Rectangle rect, int subsample, int align)
 {
     return GetBitmap(rect, subsample, align, 0);
 }
Exemplo n.º 11
0
 public Bitmap GetBitmap(Rectangle rect, int subsample)
 {
     return GetBitmap(rect, subsample, 1);
 }
Exemplo n.º 12
0
 public Bitmap GetBitmap(Rectangle rect)
 {
     return GetBitmap(rect, 1);
 }
Exemplo n.º 13
0
        /// <summary>
        /// Set this rectangle as the union of two rectangles.
        /// </summary>
        /// <param name="rect1">rectangle to union
        /// </param>
        /// <param name="rect2">rectangle to union
        ///
        /// </param>
        /// <returns> true if the results are non-empty
        /// </returns>
        public virtual bool Recthull(Rectangle rect1, Rectangle rect2)
        {
            if (rect1.Empty)
            {
                Right = rect2.Right;
                Left = rect2.Left;
                Bottom = rect2.Bottom;
                Top = rect2.Top;

                return !Empty;
            }

            if (rect2.Empty)
            {
                Right = rect1.Right;
                Left = rect1.Left;
                Bottom = rect1.Bottom;
                Top = rect1.Top;

                return !Empty;
            }
            Right = Math.Min(rect1.Right, rect2.Right);
            Left = Math.Max(rect1.Left, rect2.Left);
            Bottom = Math.Min(rect1.Bottom, rect2.Bottom);
            Top = Math.Max(rect1.Top, rect2.Top);
            return true;
        }
Exemplo n.º 14
0
        /// <summary> Set this rectangle as the intersection of two rectangles.
        ///
        /// </summary>
        /// <param name="rect1">rectangle to intersect
        /// </param>
        /// <param name="rect2">rectangle to intersect
        ///
        /// </param>
        /// <returns> true if the intersection is not empty
        /// </returns>
        public virtual bool Intersect(Rectangle rect1, Rectangle rect2)
        {
            Right = Math.Max(rect1.Right, rect2.Right);
            Left = Math.Min(rect1.Left, rect2.Left);
            Bottom = Math.Max(rect1.Bottom, rect2.Bottom);
            Top = Math.Min(rect1.Top, rect2.Top);

            if (Empty)
            {
                Right = Bottom = Left = Top = 0;

                return false;
            }

            return true;
        }
Exemplo n.º 15
0
 /// <summary> Test if a rectangle is contained within this rectangle.
 ///
 /// </summary>
 /// <param name="rect">rectangle to test
 ///
 /// </param>
 /// <returns> true if the rectangle is contained
 /// </returns>
 public virtual bool Contains(Rectangle rect)
 {
     return rect.Empty || (Contains(rect.Right, rect.Bottom) && Contains(rect.Left - 1, rect.Top - 1));
 }
Exemplo n.º 16
0
        public GPixmap GetPixelMap(GRect rect, int subsample, double gamma, PixelMap retval)
        {
            if (rect.Empty)
            {
                return (retval == null)
                ? (new PixelMap())
                : retval.Init(0, 0, null);
            }

            GPixmap bg = GetBgPixmap(rect, subsample, gamma, retval);
            if (ForegroundJB2Image != null)
            {
                if (bg == null)
                {
                    bg = (retval == null) ? new PixelMap() : retval;
                    bg.Init(
                     rect.Height,
                     rect.Width, GPixel.WhitePixel);
                }
                if (Stencil(bg, rect, subsample, gamma))
                {
                    retval = bg;
                }
            }
            else
            {
                retval = bg;
            }

            return retval;
        }
Exemplo n.º 17
0
        public Rectangle GetRequiredRect(Rectangle targetRect)
        {
            Rectangle red = new Rectangle();

            return CreateRectangles(targetRect, red);
        }
Exemplo n.º 18
0
        public Bitmap GetBitmap(Rectangle rect, int subsample, int align, int dispy, List<int> components)
        {
            if (components == null)
            {
                return GetBitmap(rect, subsample, align, dispy);
            }

            if ((Width == 0) || (Height == 0))
            {
                throw new SystemException("Image can not create bitmap");
            }

            int rxmin = rect.Right * subsample;
            int rymin = rect.Bottom * subsample;
            int swidth = rect.Width;
            int sheight = rect.Height;
            int border = (((swidth + align) - 1) & ~(align - 1)) - swidth;
            Bitmap bm = new Bitmap();
            bm.Init(sheight, swidth, border);
            bm.Grays = (1 + (subsample * subsample));

            for (int blitno = 0; blitno < Blits.Count; )
            {
                JB2Blit pblit = GetBlit(blitno++);
                JB2Shape pshape = GetShape(pblit.ShapeNumber);

                if (pshape.Bitmap != null)
                {
                    if (bm.Blit(pshape.Bitmap, pblit.Left - rxmin, (dispy + pblit.Bottom) - rymin, subsample))
                    {
                        components.Add((blitno - 1));
                    }
                }
            }

            return bm;
        }
Exemplo n.º 19
0
        private PixelReference GetLine(int fy, Rectangle required_red, Rectangle provided_input, PixelMap input)
        {
            if (fy < required_red.YMin)
            {
                fy = required_red.YMin;
            }
            else if (fy >= required_red.YMax)
            {
                fy = required_red.YMax - 1;
            }

            // Cached line
            if (fy == l2)
            {
                return p2.CreateGPixelReference(0);
            }

            if (fy == l1)
            {
                return p1.CreateGPixelReference(0);
            }

            // Shift
            PixelMap p = p1;
            p1 = p2;
            l1 = l2;
            p2 = p;
            l2 = fy;

            // Compute location of line
            Rectangle line = new Rectangle();
            line.XMin = required_red.XMin << xshift;
            line.XMax = required_red.XMax << xshift;
            line.YMin = fy << yshift;
            line.YMax = (fy + 1) << yshift;
            line.Intersect(line, provided_input);
            line.Translate(-provided_input.XMin, -provided_input.YMin);

            // Prepare variables
            int botline = input.RowOffset(line.YMin);
            int rowsize = input.GetRowSize();
            int sw = 1 << xshift;
            int div = xshift + yshift;
            int rnd = 1 << (div - 1);
            int rnd2 = rnd + rnd;

            PixelReference inp1 = input.CreateGPixelReference(0);
            PixelReference 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
            return p2.CreateGPixelReference(0);
        }
Exemplo n.º 20
0
        public void Image(int subsample, Rectangle rect, int index, sbyte[] img8, int rowsize, int pixsep, bool fast)
        {
            int nlevel = 0;

            while ((nlevel < 5) && ((32 >> nlevel) > subsample))
            {
                nlevel++;
            }

            int boxsize = 1 << nlevel;

            if (subsample != (32 >> nlevel))
            {
                throw new ArgumentException("(Map::image) Unsupported subsampling factor");
            }

            if (rect.Empty)
            {
                throw new ArgumentException("(Map::image) Rectangle is empty");
            }

            Rectangle irect = new Rectangle(0, 0, ((Iw + subsample) - 1) / subsample, ((Ih + subsample) - 1) / subsample);

            if ((rect.Right < 0) || (rect.Bottom < 0) || (rect.Left > irect.Left) || (rect.Top > irect.Top))
            {
                throw new ArgumentException("(Map::image) Rectangle is out of bounds: " + rect.Right + "," + rect.Bottom +
                                            "," + rect.Left + "," + rect.Top + "," + irect.Left + "," + irect.Top);
            }

            Rectangle[] needed = new Rectangle[8];
            Rectangle[] recomp = new Rectangle[8];

            for (int i = 0; i < 8; )
            {
                needed[i] = new Rectangle();
                recomp[i++] = new Rectangle();
            }

            int r = 1;
            needed[nlevel] = (Rectangle)rect.Duplicate();
            recomp[nlevel] = (Rectangle)rect.Duplicate();

            for (int i = nlevel - 1; i >= 0; i--)
            {
                needed[i] = recomp[i + 1];
                needed[i].Inflate(3 * r, 3 * r);
                needed[i].Intersect(needed[i], irect);
                r += r;
                recomp[i].Right = ((needed[i].Right + r) - 1) & ~(r - 1);
                recomp[i].Left = needed[i].Left & ~(r - 1);
                recomp[i].Bottom = ((needed[i].Bottom + r) - 1) & ~(r - 1);
                recomp[i].Top = needed[i].Top & ~(r - 1);
            }

            Rectangle work = new Rectangle();
            work.Right = needed[0].Right & ~(boxsize - 1);
            work.Bottom = needed[0].Bottom & ~(boxsize - 1);
            work.Left = ((needed[0].Left - 1) & ~(boxsize - 1)) + boxsize;
            work.Top = ((needed[0].Top - 1) & ~(boxsize - 1)) + boxsize;

            int dataw = work.Width;
            short[] data = new short[dataw * work.Height];
            int blkw = Bw >> 5;
            int lblock = ((work.Bottom >> nlevel) * blkw) + (work.Right >> nlevel);

            short[] liftblock = new short[1024];

            for (int by = work.Bottom, ldata = 0;
                 by < work.Top;
                 by += boxsize, ldata += (dataw << nlevel), lblock += blkw)
            {
                for (int bx = work.Right, bidx = lblock, rdata = ldata;
                     bx < work.Left;
                     bx += boxsize, bidx++, rdata += boxsize)
                {
                    IWBlock block = Blocks[bidx];
                    int mlevel = nlevel;

                    if ((nlevel > 2) &&
                        (((bx + 31) < needed[2].Right) || (bx > needed[2].Left) || ((by + 31) < needed[2].Bottom) ||
                         (by > needed[2].Top)))
                    {
                        mlevel = 2;
                    }

                    int bmax = ((1 << (mlevel + mlevel)) + 15) >> 4;
                    int ppinc = 1 << (nlevel - mlevel);
                    int ppmod1 = dataw << (nlevel - mlevel);
                    int ttmod0 = 32 >> mlevel;
                    int ttmod1 = ttmod0 << 5;
                    block.WriteLiftBlock(liftblock, 0, bmax);

                    for (int ii = 0, tt = 0, pp = rdata; ii < boxsize; ii += ppinc, pp += ppmod1, tt += (ttmod1 - 32))
                    {
                        for (int jj = 0; jj < boxsize; jj += ppinc, tt += ttmod0)
                        {
                            data[pp + jj] = liftblock[tt];
                        }
                    }
                }
            }

            r = boxsize;

            for (int i = 0; i < nlevel; i++)
            {
                Rectangle comp = needed[i];
                comp.Right = comp.Right & ~(r - 1);
                comp.Bottom = comp.Bottom & ~(r - 1);
                comp.Translate(-work.Right, -work.Bottom);

                if (fast && (i >= 4))
                {
                    for (int ii = comp.Bottom, pp = (comp.Bottom * dataw); ii < comp.Top; ii += 2, pp += (dataw + dataw))
                    {
                        for (int jj = comp.Right; jj < comp.Left; jj += 2)
                        {
                            data[pp + jj + dataw] = data[pp + jj + dataw + 1] = data[pp + jj + 1] = data[pp + jj];
                        }
                    }

                    break;
                }

                Backward(data, (comp.Bottom * dataw) + comp.Right, comp.Width, comp.Height, dataw, r, r >> 1);
                r >>= 1;
            }

            Rectangle nrect = (Rectangle)rect.Duplicate();
            nrect.Translate(-work.Right, -work.Bottom);

            for (int i = nrect.Bottom, pidx = (nrect.Bottom * dataw), ridx = index;
                 i++ < nrect.Top;
                 ridx += rowsize, pidx += dataw)
            {
                for (int j = nrect.Right, pixidx = ridx; j < nrect.Left; j++, pixidx += pixsep)
                {
                    int x = (data[pidx + j] + 32) >> 6;

                    if (x < -128)
                    {
                        x = -128;
                    }
                    else if (x > 127)
                    {
                        x = 127;
                    }

                    img8[pixidx] = (sbyte)x;
                }
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Gets the background pixmap
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="subSample"></param>
        /// <param name="gamma"></param>
        /// <param name="retval"></param>
        /// <returns></returns>
        public GPixmap GetBgPixmap(GRect rect, int subsample, double gamma, GPixmap retval)
        {
            Verify.SubsampleRange(subsample);

            GPixmap pMap = null;
            int width = Width;
            int height = Height;

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

            double gammaCorr = 1.0D;

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

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

            IInterWavePixelMap bgIWPixmap = BackgroundIWPixelMap;

            if (bgIWPixmap != null)
            {
                int iwWidth = bgIWPixmap.Width;
                int iwHeight = bgIWPixmap.Height;

                if (iwWidth == 0 || iwHeight == 0)
                    return null;

                int red = ComputeRed(width, height, iwWidth, iwHeight);

                if (red < 1 || red > 12)
                    return null;

                if (subsample == red)
                {
                    pMap = bgIWPixmap.GetPixelMap(1, rect, retval);
                }
                else if (subsample == (2 * red))
                {
                    pMap = bgIWPixmap.GetPixelMap(2, rect, retval);
                }
                else if (subsample == (4 * red))
                {
                    pMap = bgIWPixmap.GetPixelMap(4, rect, retval);
                }
                else if (subsample == (8 * red))
                {
                    pMap = bgIWPixmap.GetPixelMap(8, rect, retval);
                }
                else if ((red * 4) == (subsample * 3))
                {
                    GRect xrect = new GRect();
                    xrect.Right = (int)Math.Floor(rect.Right * 4D / 3D);
                    xrect.Bottom = (int)Math.Floor(rect.Bottom * 4D / 3D);
                    xrect.Left = (int)Math.Ceiling((double)rect.Left * 4D / 3D);
                    xrect.Top = (int)Math.Ceiling((double)rect.Top * 4D / 3D);

                    GRect nrect = new GRect(0, 0, rect.Width, rect.Height);

                    if (xrect.Left > iwWidth)
                        xrect.Left = iwWidth;

                    if (xrect.Top > iwHeight)
                        xrect.Top = iwHeight;

                    GPixmap iwPMap = bgIWPixmap.GetPixelMap(1, xrect, null);
                    pMap = (retval != null) ? retval : new PixelMap();
                    pMap.Downsample43(iwPMap, nrect);
                }
                else
                {
                    int po2 = 16;

                    while (po2 > 1 && subsample < po2 * red)
                        po2 >>= 1;

                    int inw = ((iwWidth + po2) - 1) / po2;
                    int inh = ((iwHeight + po2) - 1) / po2;
                    int outw = ((width + subsample) - 1) / subsample;
                    int outh = ((height + subsample) - 1) / subsample;
                    PixelMapScaler mapScaler = new PixelMapScaler(inw, inh, outw, outh);

                    mapScaler.SetHorzRatio(red * po2, subsample);
                    mapScaler.SetVertRatio(red * po2, subsample);

                    GRect xrect = mapScaler.GetRequiredRect(rect);
                    GPixmap iwPMap = bgIWPixmap.GetPixelMap(po2, xrect, null);
                    pMap = (retval != null) ? retval : new PixelMap();

                    mapScaler.Scale(xrect, iwPMap, rect, pMap);
                }

                if (pMap != null && gammaCorr != 1.0D)
                {
                    pMap.ApplyGammaCorrection(gammaCorr);

                    for (int i = 0; i < 9; i++)
                        pMap.ApplyGammaCorrection(gammaCorr);
                }

                return pMap;
            }
            else
                return null;
        }
Exemplo n.º 22
0
        private bool Stencil(PixelMap pm, Graphics.Rectangle rect, int subsample, double gamma)
        {
            if (Info == null)
            {
                return false;
            }

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

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

            double gamma_correction = 1.0D;

            if (gamma > 0.0D)
            {
                gamma_correction = gamma / Info.Gamma;
            }

            if (gamma_correction < 0.10000000000000001D)
            {
                gamma_correction = 0.10000000000000001D;
            }
            else if (gamma_correction > 10D)
            {
                gamma_correction = 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 GPixmap().Init(
                        1,
                        fgPalette.PaletteColors.Length,
                        null);
                    GPixelReference color = colors.CreateGPixelReference(0);

                    for (int i = 0; i < colors.ImageWidth; color.IncOffset())
                    {
                        fgPalette.index_to_color(i++, color);
                    }

                    colors.ApplyGammaCorrection(gamma_correction);

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

                    while (components.Count > 0)
                    {
                        int lastx = 0;
                        int colorindex = fgPalette.BlitColors[((int)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.ImageWidth,
                                    pshape.Bitmap.ImageHeight);
                                comprect.Recthull(comprect, xrect);
                                compset.Add(components[pos]);
                                components.Remove(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   = getBitmap(comprect, subsample, 1);
                        bm = new GBitmap();
                        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.
                IWPixelMap fgIWPixmap = ForegroundIWPixelMap;

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

                    if ((bm != null) && (pm != null))
                    {
                        GPixmap fgPixmap = ForegroundPixelMap;
                        int w = fgPixmap.ImageWidth;
                        int h = fgPixmap.ImageHeight;
                        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, gamma_correction);
                        return true;
                    }
                }
            }

            return false;
        }
Exemplo n.º 23
0
        /// <summary>
        /// Gets the background pixmap
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="subSample"></param>
        /// <param name="gamma"></param>
        /// <param name="retval"></param>
        /// <returns></returns>
        public GPixmap GetBgPixmap(GRect rect, int subsample, double gamma, GPixmap retval)
        {
            GPixmap pm = null;
            int width = (Info == null)
                            ? 0
                            : Info.Width;
            int height = (Info == null)
                             ? 0
                             : Info.Height;

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

            double gamma_correction = 1.0D;

            if ((gamma > 0.0D) && (Info != null))
            {
                gamma_correction = gamma / Info.Gamma;
            }

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

            IWPixelMap bgIWPixmap = BackgroundIWPixelMap;

            if (bgIWPixmap != null)
            {
                int w = bgIWPixmap.Width;
                int h = bgIWPixmap.Height;

                if ((w == 0) || (h == 0) || (width == 0) || (height == 0))
                {
                    return null;
                }

                int red = ComputeRed(width, height, w, h);

                if ((red < 1) || (red > 12))
                {
                    return null;
                }

                if (subsample == red)
                {
                    pm = bgIWPixmap.GetPixmap(1, rect, retval);
                }
                else if (subsample == (2 * red))
                {
                    pm = bgIWPixmap.GetPixmap(2, rect, retval);
                }
                else if (subsample == (4 * red))
                {
                    pm = bgIWPixmap.GetPixmap(4, rect, retval);
                }
                else if (subsample == (8 * red))
                {
                    pm = bgIWPixmap.GetPixmap(8, rect, retval);
                }
                else if ((red * 4) == (subsample * 3))
                {
                    GRect xrect = new GRect();
                    xrect.Right = (int)Math.Floor(rect.Right * 4D / 3D);
                    xrect.Bottom = (int)Math.Floor(rect.Bottom * 4D / 3D);
                    xrect.Left = (int)Math.Ceiling((double)rect.Left * 4D / 3D);
                    xrect.Top = (int)Math.Ceiling((double)rect.Top * 4D / 3D);

                    GRect nrect = new GRect(0, 0, rect.Width, rect.Height);
                    if (xrect.Left > w)
                    {
                        xrect.Left = w;
                    }

                    if (xrect.Top > h)
                    {
                        xrect.Top = h;
                    }

                    GPixmap ipm = bgIWPixmap.GetPixmap(1, xrect, null);
                    pm = (retval != null)
                             ? retval
                             : new GPixmap();
                    pm.Downsample43(ipm, nrect);
                }
                else
                {
                    int po2 = 16;

                    while ((po2 > 1) && (subsample < (po2 * red)))
                    {
                        po2 >>= 1;
                    }

                    int inw = ((w + po2) - 1) / po2;
                    int inh = ((h + po2) - 1) / po2;
                    int outw = ((width + subsample) - 1) / subsample;
                    int outh = ((height + subsample) - 1) / subsample;
                    PixelMapScaler ps = new PixelMapScaler(inw, inh, outw, outh);
                    ps.SetHorzRatio(red * po2, subsample);
                    ps.SetVertRatio(red * po2, subsample);

                    GRect xrect = ps.GetRequiredRect(rect);
                    GPixmap ipm = bgIWPixmap.GetPixmap(po2, xrect, null);
                    pm = (retval != null)
                             ? retval
                             : new GPixmap();
                    ps.Scale(xrect, ipm, rect, pm);
                }

                if ((pm != null) && (gamma_correction != 1.0D))
                {
                    pm.ApplyGammaCorrection(gamma_correction);

                    for (int i = 0; i < 9; i++)
                    {
                        pm.ApplyGammaCorrection(gamma_correction);
                    }
                }

                return pm;
            }
            else
            {
                return null;
            }
        }
Exemplo n.º 24
0
 public GBitmap GetBitmap(GRect rect, int subsample, int align, GBitmap retval)
 {
     return GetBitmapList(rect, 1, 1, null);
 }
Exemplo n.º 25
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;
        }
Exemplo n.º 26
0
        /// <summary>
        /// Decodes the data for the zone
        /// </summary>
        /// <param name="reader"></param>
        private void DecodeZoneData(DjvuReader reader, TextZone sibling, TextChunk chunkParent)
        {
            _zoneType = (ZoneTypes)reader.ReadByte();
            _x = reader.ReadUInt16MSB() - 0x8000;
            _y = reader.ReadUInt16MSB() - 0x8000;
            _width = reader.ReadUInt16MSB() - 0x8000;
            _height = reader.ReadUInt16MSB() - 0x8000;

            _textOffset = reader.ReadUInt16MSB() - 0x8000;
            _textLength = reader.ReadInt24MSB();

            ResolveOffsets(_parent, sibling);

            _rectangle = new Rectangle(_x, _y, _width, _height);

            int childrenZones = reader.ReadInt24MSB();
            List<TextZone> children = new List<TextZone>();

            TextZone childrenSibling = null;

            for (int x = 0; x < childrenZones; x++)
            {
                TextZone newZone = new TextZone(reader, this, childrenSibling, chunkParent);
                childrenSibling = newZone;

                children.Add(newZone);
            }

            _children = children.ToArray();
        }