Esempio n. 1
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);
        }
Esempio n. 2
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));
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
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);
        }