Exemplo n.º 1
0
        /// <summary>
        /// Fills a whole image with a color while preserving the alpha channel
        /// </summary>
        /// <param name="image">The image to fill</param>
        /// <param name="color">The color to fill with</param>
        /// <returns>The result image</returns>
        public static Bitmap FillAlphaPreserve(Bitmap image, Color color)
        {
            int width = image.Width;
            int height = image.Height;
            Rectangle bounds = new Rectangle(0, 0, width, height);

            Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            Bitmap output = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            using(Graphics g = Graphics.FromImage(copy))
            {
                g.PageUnit = GraphicsUnit.Pixel;
                g.DrawImageUnscaled(image, 0, 0);
            }

            BitmapData outData = null;
            BitmapData srcData = null;
            try
            {
                outData = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
                srcData = copy.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                byte* outRow = (byte*)outData.Scan0.ToPointer();
                Int32* outPxl;

                byte* srcRow = (byte*)srcData.Scan0.ToPointer();
                Int32* srcPxl;

                for(int row = 0; row < height; ++row)
                {
                    outPxl = (Int32*)outRow;
                    srcPxl = (Int32*)srcRow;
                    for(int col = 0; col < width; ++col, ++outPxl, ++srcPxl)
                    {
                        XColor xc = new XColor((Color32*)srcPxl);
                        xc.rgb = color;
                        ((Color32*)outPxl)->ARGB = xc.col32.ARGB;
                    }

                    outRow += outData.Stride;
                    srcRow += srcData.Stride;
                }
            }
            finally
            {
                output.UnlockBits(outData);
                copy.UnlockBits(srcData);
            }

            copy.Dispose();
            return output;
        }
Exemplo n.º 2
0
    public void RealizeBrush(XBrush brush, PdfColorMode colorMode)
    {
      if (brush is XSolidBrush)
      {
        XColor color = ((XSolidBrush)brush).Color;
        color = ColorSpaceHelper.EnsureColorMode(colorMode, color);

        if (colorMode != PdfColorMode.Cmyk)
        {
          if (this.realizedFillColor.Rgb != color.Rgb)
          {
            this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
            this.renderer.Append(" rg\n");
          }
        }
        else
        {
          if (!ColorSpaceHelper.IsEqualCmyk(this.realizedFillColor, color))
          {
            this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
            this.renderer.Append(" k\n");
          }
        }

        if (this.renderer.Owner.Version >= 14 && this.realizedFillColor.A != color.A)
        {
          PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.A);
          string gs = this.renderer.Resources.AddExtGState(extGState);
          this.renderer.AppendFormat("{0} gs\n", gs);

          // Must create transparany group
          if (this.renderer.page != null && color.A < 1)
            this.renderer.page.transparencyUsed = true;
        }
        this.realizedFillColor = color;
      }
      else if (brush is XLinearGradientBrush)
      {
        XMatrix matrix = this.renderer.defaultViewMatrix;
        matrix.Prepend(this.Transform);
        PdfShadingPattern pattern = new PdfShadingPattern(this.renderer.Owner);
        pattern.SetupFromBrush((XLinearGradientBrush)brush, matrix);
        string name = this.renderer.Resources.AddPattern(pattern);
        this.renderer.AppendFormat("/Pattern cs\n", name);
        this.renderer.AppendFormat("{0} scn\n", name);

        // Invalidate fill color
        this.realizedFillColor = XColor.Empty;
      }
    }
Exemplo n.º 3
0
    public void RealizePen(XPen pen, PdfColorMode colorMode)
    {
      XColor color = pen.Color;
      color = ColorSpaceHelper.EnsureColorMode(colorMode, color);

      if (this.realizedLineWith != pen.width)
      {
        this.renderer.AppendFormat("{0:0.###} w\n", pen.width);
        this.realizedLineWith = pen.width;
      }

      if (this.realizedLineCap != (int)pen.lineCap)
      {
        this.renderer.AppendFormat("{0} J\n", (int)pen.lineCap);
        this.realizedLineCap = (int)pen.lineCap;
      }

      if (this.realizedLineJoin != (int)pen.lineJoin)
      {
        this.renderer.AppendFormat("{0} j\n", (int)pen.lineJoin);
        this.realizedLineJoin = (int)pen.lineJoin;
      }

      if (this.realizedLineCap == (int)XLineJoin.Miter)
      {
        if (this.realizedMiterLimit != (int)pen.miterLimit && (int)pen.miterLimit != 0)
        {
          this.renderer.AppendFormat("{0} M\n", (int)pen.miterLimit);
          this.realizedMiterLimit = (int)pen.miterLimit;
        }
      }

      if (this.realizedDashStyle != pen.dashStyle || pen.dashStyle == XDashStyle.Custom)
      {
        double dot = pen.Width;
        double dash = 3 * dot;

        // Line width 0 is not recommended but valid
        XDashStyle dashStyle = pen.DashStyle;
        if (dot == 0)
          dashStyle = XDashStyle.Solid;

        switch (dashStyle)
        {
          case XDashStyle.Solid:
            this.renderer.Append("[]0 d\n");
            break;

          case XDashStyle.Dash:
            this.renderer.AppendFormat("[{0:0.##} {1:0.##}]0 d\n", dash, dot);
            break;

          case XDashStyle.Dot:
            this.renderer.AppendFormat("[{0:0.##}]0 d\n", dot);
            break;

          case XDashStyle.DashDot:
            this.renderer.AppendFormat("[{0:0.##} {1:0.##} {1:0.##} {1:0.##}]0 d\n", dash, dot);
            break;

          case XDashStyle.DashDotDot:
            this.renderer.AppendFormat("[{0:0.##} {1:0.##} {1:0.##} {1:0.##} {1:0.##} {1:0.##}]0 d\n", dash, dot);
            break;

          case XDashStyle.Custom:
            {
              StringBuilder pdf = new StringBuilder("[", 256);
              int len = pen.dashPattern == null ? 0 : pen.dashPattern.Length;
              for (int idx = 0; idx < len; idx++)
              {
                if (idx > 0)
                  pdf.Append(' ');
                pdf.Append(PdfEncoders.ToString(pen.dashPattern[idx] * pen.width));
              }
              // Make an even number of values look like in GDI+
              if (len > 0 && len % 2 == 1)
              {
                pdf.Append(' ');
                pdf.Append(PdfEncoders.ToString(0.2 * pen.width));
              }
              pdf.AppendFormat(CultureInfo.InvariantCulture, "]{0:0.###} d\n", pen.dashOffset * pen.width);
              string pattern = pdf.ToString();

              // BUG: [email protected] reported a realizing problem
              // HACK: I romove the if clause
              //if (this.realizedDashPattern != pattern)
              {
                this.realizedDashPattern = pattern;
                this.renderer.Append(pattern);
              }
            }
            break;
        }
        this.realizedDashStyle = dashStyle;
      }

      if (colorMode != PdfColorMode.Cmyk)
      {
        if (this.realizedStrokeColor.Rgb != color.Rgb)
        {
          this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
          this.renderer.Append(" RG\n");
        }
      }
      else
      {
        if (!ColorSpaceHelper.IsEqualCmyk(this.realizedStrokeColor, color))
        {
          this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
          this.renderer.Append(" K\n");
        }
      }

      if (this.renderer.Owner.Version >= 14 && this.realizedStrokeColor.A != color.A)
      {
        PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateStroke(color.A);
        string gs = this.renderer.Resources.AddExtGState(extGState);
        this.renderer.AppendFormat("{0} gs\n", gs);

        // Must create transparany group
        if (this.renderer.page != null && color.A < 1)
          this.renderer.page.transparencyUsed = true;
      }
      this.realizedStrokeColor = color;
    }
Exemplo n.º 4
0
    // --------------------------------------------------------------------------------------------

    #region  Drawing

    //void SetPageLayout(down, point(0, 0), unit

    // ----- Clear --------------------------------------------------------------------------------

    public void Clear(XColor color)
    {
      if (!this.gfx.transform.IsIdentity)
        throw new NotImplementedException("Transform must be identity to clear the canvas.");

      // TODO: this is implementation is bogus. Reset transformation to identity an then fill
      XBrush brush = new XSolidBrush(color);
      DrawRectangle(null, brush, 0, 0, Size.Width, Size.Height);
    }
Exemplo n.º 5
0
    public void RealizeBrush(XBrush brush)
    {
      if (brush is XSolidBrush)
      {
        XColor color = ((XSolidBrush)brush).Color;
        if (this.realizedFillColor.Argb != color.Argb)
        {
          if (this.realizedFillColor.Rgb != color.Rgb)
          {
            this.renderer.AppendFormat("{0} {1} {2} rg\n",
              PdfEncoders.ToString(color.R / 255.0), PdfEncoders.ToString(color.G / 255.0), PdfEncoders.ToString(color.B / 255.0));
          }
          if (this.renderer.Owner.Version >= 14 && this.realizedFillColor.A != color.A)
          {
            PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.A);
            string gs = this.renderer.Resources.AddExtGState(extGState);
            this.renderer.AppendFormat("{0} gs\n", gs);

            // Must create transparany group
            if (this.renderer.page != null && color.A < 1)
              this.renderer.page.transparencyUsed = true;
          }
          this.realizedFillColor = color;
        }
      }
      else if (brush is XLinearGradientBrush)
      {
        XMatrix matrix = this.renderer.defaultViewMatrix;
        matrix.Multiply(this.Transform);
        PdfShadingPattern pattern = new PdfShadingPattern(this.renderer.Owner);
        pattern.SetupFromBrush((XLinearGradientBrush)brush, matrix);
        string name = this.renderer.Resources.AddPattern(pattern);
        this.renderer.AppendFormat("/Pattern cs\n", name);
        this.renderer.AppendFormat("{0} scn\n", name);

        // Invalidate fill color
        this.realizedFillColor = XColor.Empty;
      }
    }
Exemplo n.º 6
0
        private void RealizeFillColor(XColor color, bool overPrint, PdfColorMode colorMode)
        {
            color = ColorSpaceHelper.EnsureColorMode(colorMode, color);

            if (colorMode != PdfColorMode.Cmyk)
            {
                if (_realizedFillColor.IsEmpty || _realizedFillColor.Rgb != color.Rgb)
                {
                    _renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
                    _renderer.Append(" rg\n");
                }
            }
            else
            {
                Debug.Assert(colorMode == PdfColorMode.Cmyk);

                if (_realizedFillColor.IsEmpty || !ColorSpaceHelper.IsEqualCmyk(_realizedFillColor, color))
                {
                    _renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
                    _renderer.Append(" k\n");
                }
            }

            if (_renderer.Owner.Version >= 14 && (_realizedFillColor.A != color.A || _realizedNonStrokeOverPrint != overPrint))
            {

                PdfExtGState extGState = _renderer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.A, overPrint);
                string gs = _renderer.Resources.AddExtGState(extGState);
                _renderer.AppendFormatString("{0} gs\n", gs);

                // Must create transparency group.
                if (_renderer._page != null && color.A < 1)
                    _renderer._page.TransparencyUsed = true;
            }
            _realizedFillColor = color;
            _realizedNonStrokeOverPrint = overPrint;
        }
Exemplo n.º 7
0
        public void RealizeBrush(XBrush brush, PdfColorMode colorMode, int renderingMode, double fontEmSize)
        {
            // Rendering mode 2 is used for bold simulation.
            // Reference: TABLE 5.3  Text rendering modes / Page 402

            XSolidBrush solidBrush = brush as XSolidBrush;
            if (solidBrush != null)
            {
                XColor color = solidBrush.Color;
                bool overPrint = solidBrush.Overprint;

                if (renderingMode == 0)
                {
                    RealizeFillColor(color, overPrint, colorMode);
                }
                else if (renderingMode == 2)
                {
                    // Come here in case of bold simulation.
                    RealizeFillColor(color, false, colorMode);
                    //color = XColors.Green;
                    RealizePen(new XPen(color, fontEmSize * Const.BoldEmphasis), colorMode);
                }
                else
                    throw new InvalidOperationException("Only rendering modes 0 and 2 are currently supported.");
            }
            else
            {
                if (renderingMode != 0)
                    throw new InvalidOperationException("Rendering modes other than 0 can only be used with solid color brushes.");

                XLinearGradientBrush gradientBrush = brush as XLinearGradientBrush;
                if (gradientBrush != null)
                {
                    Debug.Assert(UnrealizedCtm.IsIdentity, "Must realize ctm first.");
                    XMatrix matrix = _renderer.DefaultViewMatrix;
                    matrix.Prepend(EffectiveCtm);
                    PdfShadingPattern pattern = new PdfShadingPattern(_renderer.Owner);
                    pattern.SetupFromBrush(gradientBrush, matrix, _renderer);
                    string name = _renderer.Resources.AddPattern(pattern);
                    _renderer.AppendFormatString("/Pattern cs\n", name);
                    _renderer.AppendFormatString("{0} scn\n", name);

                    // Invalidate fill color.
                    _realizedFillColor = XColor.Empty;
                }
            }
        }
Exemplo n.º 8
0
        public void RealizePen(XPen pen, PdfColorMode colorMode)
        {
            const string frmt2 = Config.SignificantFigures2;
            const string format = Config.SignificantFigures3;
            XColor color = pen.Color;
            bool overPrint = pen.Overprint;
            color = ColorSpaceHelper.EnsureColorMode(colorMode, color);

            if (_realizedLineWith != pen._width)
            {
                _renderer.AppendFormatArgs("{0:" + format + "} w\n", pen._width);
                _realizedLineWith = pen._width;
            }

            if (_realizedLineCap != (int)pen._lineCap)
            {
                _renderer.AppendFormatArgs("{0} J\n", (int)pen._lineCap);
                _realizedLineCap = (int)pen._lineCap;
            }

            if (_realizedLineJoin != (int)pen._lineJoin)
            {
                _renderer.AppendFormatArgs("{0} j\n", (int)pen._lineJoin);
                _realizedLineJoin = (int)pen._lineJoin;
            }

            if (_realizedLineCap == (int)XLineJoin.Miter)
            {
                if (_realizedMiterLimit != (int)pen._miterLimit && (int)pen._miterLimit != 0)
                {
                    _renderer.AppendFormatInt("{0} M\n", (int)pen._miterLimit);
                    _realizedMiterLimit = (int)pen._miterLimit;
                }
            }

            if (_realizedDashStyle != pen._dashStyle || pen._dashStyle == XDashStyle.Custom)
            {
                double dot = pen.Width;
                double dash = 3 * dot;

                // Line width 0 is not recommended but valid.
                XDashStyle dashStyle = pen.DashStyle;
                if (dot == 0)
                    dashStyle = XDashStyle.Solid;

                switch (dashStyle)
                {
                    case XDashStyle.Solid:
                        _renderer.Append("[]0 d\n");
                        break;

                    case XDashStyle.Dash:
                        _renderer.AppendFormatArgs("[{0:" + frmt2 + "} {1:" + frmt2 + "}]0 d\n", dash, dot);
                        break;

                    case XDashStyle.Dot:
                        _renderer.AppendFormatArgs("[{0:" + frmt2 + "}]0 d\n", dot);
                        break;

                    case XDashStyle.DashDot:
                        _renderer.AppendFormatArgs("[{0:" + frmt2 + "} {1:" + frmt2 + "} {1:" + frmt2 + "} {1:" + frmt2 + "}]0 d\n", dash, dot);
                        break;

                    case XDashStyle.DashDotDot:
                        _renderer.AppendFormatArgs("[{0:" + frmt2 + "} {1:" + frmt2 + "} {1:" + frmt2 + "} {1:" + frmt2 + "} {1:" + frmt2 + "} {1:" + frmt2 + "}]0 d\n", dash, dot);
                        break;

                    case XDashStyle.Custom:
                        {
                            StringBuilder pdf = new StringBuilder("[", 256);
                            int len = pen._dashPattern == null ? 0 : pen._dashPattern.Length;
                            for (int idx = 0; idx < len; idx++)
                            {
                                if (idx > 0)
                                    pdf.Append(' ');
                                pdf.Append(PdfEncoders.ToString(pen._dashPattern[idx] * pen._width));
                            }
                            // Make an even number of values look like in GDI+
                            if (len > 0 && len % 2 == 1)
                            {
                                pdf.Append(' ');
                                pdf.Append(PdfEncoders.ToString(0.2 * pen._width));
                            }
                            pdf.AppendFormat(CultureInfo.InvariantCulture, "]{0:" + format + "} d\n", pen._dashOffset * pen._width);
                            string pattern = pdf.ToString();

                            // BUG: [email protected] reported a realizing problem
                            // HACK: I remove the if clause
                            //if (_realizedDashPattern != pattern)
                            {
                                _realizedDashPattern = pattern;
                                _renderer.Append(pattern);
                            }
                        }
                        break;
                }
                _realizedDashStyle = dashStyle;
            }

            if (colorMode != PdfColorMode.Cmyk)
            {
                if (_realizedStrokeColor.Rgb != color.Rgb)
                {
                    _renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
                    _renderer.Append(" RG\n");
                }
            }
            else
            {
                if (!ColorSpaceHelper.IsEqualCmyk(_realizedStrokeColor, color))
                {
                    _renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
                    _renderer.Append(" K\n");
                }
            }

            if (_renderer.Owner.Version >= 14 && (_realizedStrokeColor.A != color.A || _realizedStrokeOverPrint != overPrint))
            {
                PdfExtGState extGState = _renderer.Owner.ExtGStateTable.GetExtGStateStroke(color.A, overPrint);
                string gs = _renderer.Resources.AddExtGState(extGState);
                _renderer.AppendFormatString("{0} gs\n", gs);

                // Must create transparency group.
                if (_renderer._page != null && color.A < 1)
                    _renderer._page.TransparencyUsed = true;
            }
            _realizedStrokeColor = color;
            _realizedStrokeOverPrint = overPrint;
        }
Exemplo n.º 9
0
 public ColorResourceInfo(XKnownColor knownColor, XColor color, uint argb, string name, string nameDE)
 {
     KnownColor = knownColor;
     Color = color;
     Argb = argb;
     Name = name;
     NameDE = nameDE;
 }