Uploaded image for project: 'Batik'
  1. Batik
  2. BATIK-1269

[PATCH] AbstractGraphics2D drawImage(img, dx1,dy1,dx2,dy2, sx1,sy1,sx2, sy2, obs) causes RasterFormatException when flipping source coordinates updside down

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 1.7, 1.8, 1.9, 1.10, 1.11
    • Fix Version/s: None
    • Component/s: SVGGraphics2D
    • Labels:
      None
    • Flags:
      Patch

      Description

      The AbstractGraphics2D class (org.apache.batik.ext.awt.g2d.AbstractGraphics2D) is extended by SVGGraphics2D and implements the following drawImage method which allows to specify start and end corners of a rectangle in the destination space as well as within the image source:

      public boolean drawImage(Image img,
         int dx1, int dy1, int dx2, int dy2,
         int sx1, int sy1, int sx2, int sy2,
         ImageObserver observer)

      The implementation uses the BufferedImage.getSubimage() method to extract the specified source pixels, See here: github src. This causes a RasterFormatException in case the specified source rectangle is not from upper left corner to lower right corner but for example upside down (e.g. in order to flip the image).

       

      Also the implementation assumes that the destination coordinates are from top left to bottom right as well, which may not be the case and results in the image not appearing in the dom.

       

      Here is a proposal for a fix:

       

      public boolean drawImage(Image img,
         int dx1, int dy1, int dx2, int dy2,
         int sx1, int sy1, int sx2, int sy2,
         ImageObserver observer)
      {{ {}}

        if(dx2 < dx1){

      {{    return drawImage(img, }}
          dx2, dy1, dx1, dy2,
          sx2, sy1, sx1, sy2,
          observer);
      {{  }}}
        if(dy2 < dy1){
      {{    return drawImage(img, }}
          dx1, dy2, dx2, dy1,
          sx1, sy2, sx2, sy1,
          observer);
      {{  }}}

        int srcW = Math.abs(sx2-sx1);
        int srcH = Math.abs(sy2-sy1);
        BufferedImage src = new BufferedImage(srcW, srcH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = src.createGraphics();
        g.drawImage(img, 0,0, srcW,srcH, sx1,sy1, sx2,sy2, null);
        g.dispose();

        return drawImage(src, dx1, dy1, dx2-dx1, dy2-dy1, observer);
      {{ }}}

       

        Attachments

        1. AbstractGraphics2D.diff
          2 kB
          David Hägele
        2. AbstractGraphics2D.diff
          1 kB
          David Hägele

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              hageldave David Hägele
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:

                Time Tracking

                Estimated:
                Original Estimate - 0.5h
                0.5h
                Remaining:
                Remaining Estimate - 0.5h
                0.5h
                Logged:
                Time Spent - Not Specified
                Not Specified