You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Robert Gash <ga...@gashalot.com> on 2003/06/03 17:50:13 UTC

[Batik 1.5b5] PNGTranscoder creating blank images

I am using the SVG generator to create a new SVG by drawing on a
Graphics2D object.  Once updates are complete, I need to rasterize
this image to a PNG.  When I do so, the PNGTranscoder (and
JPEGTranscoder) continuously produce appropriately sized, but blank
images (IE- a big image full of background color).

I have saved the SVG that I am attemting to rasterize, and Adobe's SVG
viewer displays is properly.  What might I be missing that could cause
problems like this?

Here is the code:

DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
Document doc =
domImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);

SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
Element svgRoot = doc.getDocumentElement();
cr.drawScope(svgGenerator, 400, 400);
svgGenerator.getRoot(svgRoot);
svgRoot.setAttributeNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "width", "400");
svgRoot.setAttributeNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "height", "400");

// prepare the input and output for the transcoding process
TranscoderInput ti = new TranscoderInput(doc);
OutputStream outFile = new
FileOutputStream("test/radar.png");
TranscoderOutput to = new TranscoderOutput(outFile);

// prep the PNG transcoder for output
Transcoder t = new PNGTranscoder();
t.addTranscodingHint(org.apache.batik.transcoder.image.ImageTranscoder.KEY_BACKGROUND_COLOR,
Color.WHITE);
t.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(400));
t.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new Float(400));

Writer out = new FileWriter("test/radar.xml");
XMLSerializer xs = new XMLSerializer(out, new OutputFormat(doc));
xs.serialize(doc);
           
// transcode
t.transcode(ti, to);

outFile.flush();
outFile.close();

-- 
Robert Gash, gashalot@gashalot.com
(Web) http://gashalot.com/
(PGP) http://gashalot.com/pgpkeys.txt


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: [Batik 1.5b5] PNGTranscoder creating blank images

Posted by Vincent Hardy <vi...@sun.com>.
Hi Robert,

In the code your provided in your first email, the Document was created 
with the proper namespace URI/qualified name for the Document element. 
Setting the prefix on the root element only changes the namespace URI 
prefix, but it does not change the fact that the element's namespace so 
that should not cause any difference in behavior. Fixing the 
width/height setting as Thomas had suggested would make a difference. 
Did you try not setting the prefix on svgRoot. If that does not work, I 
think we have a bug somewhere.

Vincent.

Robert Gash wrote:

>On Wed, 4 Jun 2003 09:27:20 -0400, Thomas E Deweese was overheard saying:
>|      I would at first blush guess a namespace issue of some kind.  If
>|  your root element is not an 'svg:svg' so to say then the rasterizer
>|  will ignore the whole document.  This is a common problem because in a
>|  sense the rules applied when parsing are different from the DOM rules
>|  - so writing the file out and re-reading it often 'fixes' the file.
>
>Thomas,
>
>You were correct in assuming that the problem was related to the root
>node not being an "svg:svg" element.  By default the root node was a
>plain "svg" element.  Manually setting the prefix of the root node to
>"svg" corrected the problem, and the transcoder now produces the
>expected images.
>
>For those interested parties, here is the relevant snippet of code to
>create a new SVGGraphics2D that you can draw on and then transcode
>immediately, without having to parse the document again:
>
>            SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
>            Element svgRoot = doc.getDocumentElement();
>            svgRoot.setPrefix("svg");
>
>            // draw on svgGenerator here
>            svgGenerator.getRoot(svgRoot);
>            svgRoot.setAttributeNS(null, "width", /* width */);
>            svgRoot.setAttributeNS(null, "height", /* height */);
>
>            TranscoderInput ti = new TranscoderInput(doc);
>            // do the rest of the transcode here...
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Re: [Batik 1.5b5] PNGTranscoder creating blank images

Posted by Robert Gash <ga...@gashalot.com>.
On Wed, 4 Jun 2003 09:27:20 -0400, Thomas E Deweese was overheard saying:
|      I would at first blush guess a namespace issue of some kind.  If
|  your root element is not an 'svg:svg' so to say then the rasterizer
|  will ignore the whole document.  This is a common problem because in a
|  sense the rules applied when parsing are different from the DOM rules
|  - so writing the file out and re-reading it often 'fixes' the file.

Thomas,

You were correct in assuming that the problem was related to the root
node not being an "svg:svg" element.  By default the root node was a
plain "svg" element.  Manually setting the prefix of the root node to
"svg" corrected the problem, and the transcoder now produces the
expected images.

For those interested parties, here is the relevant snippet of code to
create a new SVGGraphics2D that you can draw on and then transcode
immediately, without having to parse the document again:

            SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
            Element svgRoot = doc.getDocumentElement();
            svgRoot.setPrefix("svg");

            // draw on svgGenerator here
            svgGenerator.getRoot(svgRoot);
            svgRoot.setAttributeNS(null, "width", /* width */);
            svgRoot.setAttributeNS(null, "height", /* height */);

            TranscoderInput ti = new TranscoderInput(doc);
            // do the rest of the transcode here...

-- 
Robert Gash, gashalot@gashalot.com
(Web) http://gashalot.com/
(PGP) http://gashalot.com/pgpkeys.txt


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


RE: [Batik 1.5b5] PNGTranscoder creating blank images

Posted by Thomas E Deweese <th...@kodak.com>.
>>>>> "RG" == Robert Gash <ga...@gashalot.com> writes:

RG> I am using the SVG generator to create a new SVG by drawing on a
RG> Graphics2D object.  Once updates are complete, I need to rasterize
RG> this image to a PNG.  When I do so, the PNGTranscoder (and
RG> JPEGTranscoder) continuously produce appropriately sized, but
RG> blank images (IE- a big image full of background color).

RG> I have saved the SVG that I am attemting to rasterize, and Adobe's
RG> SVG viewer displays is properly.  What might I be missing that
RG> could cause problems like this?

    I would at first blush guess a namespace issue of some kind.  If
your root element is not an 'svg:svg' so to say then the rasterizer
will ignore the whole document.  This is a common problem because in a
sense the rules applied when parsing are different from the DOM rules
- so writing the file out and re-reading it often 'fixes' the file.

    That all said I'm not sure where the problem would occur since it
is mostly Batik code that is creating elements.  It would probably be
worth your while to print the namespaceURI and tag name for svgRoot
and the direct children of svgRoot.

    Also width and height should not be in the SVG namespace they
should be in the 'default' namespace (just use setAttribute or
setAttributeNS(null, ...) - I'll leave it to others to decide which is
truly correct but both work in Batik).

RG> Here is the code:

RG> DOMImplementation domImpl =
RG> SVGDOMImplementation.getDOMImplementation(); Document doc =
RG> domImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
RG> "svg", null);

RG> SVGGraphics2D svgGenerator = new SVGGraphics2D(doc); Element
RG> svgRoot = doc.getDocumentElement(); cr.drawScope(svgGenerator,
RG> 400, 400); svgGenerator.getRoot(svgRoot);
RG> svgRoot.setAttributeNS(SVGDOMImplementation.SVG_NAMESPACE_URI,
RG> "width", "400");
RG> svgRoot.setAttributeNS(SVGDOMImplementation.SVG_NAMESPACE_URI,
RG> "height", "400");

RG> // prepare the input and output for the transcoding process
RG> TranscoderInput ti = new TranscoderInput(doc); OutputStream
RG> outFile = new FileOutputStream("test/radar.png"); TranscoderOutput
RG> to = new TranscoderOutput(outFile);

RG> // prep the PNG transcoder for output Transcoder t = new
RG> PNGTranscoder();
RG> t.addTranscodingHint(org.apache.batik.transcoder.image.ImageTranscoder.KEY_BACKGROUND_COLOR,
RG> Color.WHITE);
RG> t.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new
RG> Float(400));
RG> t.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new
RG> Float(400));

RG> Writer out = new FileWriter("test/radar.xml"); XMLSerializer xs =
RG> new XMLSerializer(out, new OutputFormat(doc)); xs.serialize(doc);
           
RG> // transcode t.transcode(ti, to);

RG> outFile.flush(); outFile.close();

RG> -- Robert Gash, gashalot@gashalot.com (Web) http://gashalot.com/
RG> (PGP) http://gashalot.com/pgpkeys.txt


RG> ---------------------------------------------------------------------
RG> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org For
RG> additional commands, e-mail: batik-users-help@xml.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org