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

David Hägele (Jira) <[email protected]> Thu, 10 Oct 2019 15:22:00 +0000 (UTC)
Newsgroups gmane.text.xml.batik.devel
Message-ID <[email protected]>
David H=C3=A4gele created BATIK-1269:
-----------------------------------

             Summary: AbstractGraphics2D drawImage(img, dx1,dy1,dx2,dy2, sx=
1,sy1,sx2, sy2, obs) causes RasterFormatException when flipping source coor=
dinates updside down
                 Key: BATIK-1269
                 URL: https://issues.apache.org/jira/browse/BATIK-1269
             Project: Batik
          Issue Type: Bug
          Components: SVGGraphics2D
    Affects Versions: 1.11, 1.10, 1.9, 1.8, 1.7
            Reporter: David H=C3=A4gele


The AbstractGraphics2D class (org.apache.batik.ext.awt.g2d.AbstractGraphics=
2D) 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,}}
 {{=C2=A0=C2=A0 int dx1, int dy1, int dx2, int dy2,}}
 {{=C2=A0=C2=A0 int sx1, int sy1, int sx2, int sy2,}}
 {{=C2=A0=C2=A0 ImageObserver observer)}}

The implementation uses the BufferedImage.getSubimage() method to extract t=
he specified source pixels, See here: [github src|https://github.com/apache=
/xmlgraphics-batik/blob/9de3c8ea26ad7b91fec1ea00eac39236e000cfdb/batik-awt-=
util/src/main/java/org/apache/batik/ext/awt/g2d/AbstractGraphics2D.java#L77=
4]. This causes a=C2=A0RasterFormatException in case the specified source r=
ectangle is not from upper left corner to lower right corner but for exampl=
e upside down (e.g. in order to flip the image).

=C2=A0

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

=C2=A0

Here is a proposal for a fix:

=C2=A0

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

{{=C2=A0 if(dx2 < dx1){}}

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

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

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

=C2=A0



--
This message was sent by Atlassian Jira
(v8.3.4#803005)