KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > apps > tiledTranscoder > TiledImageTranscoder


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.apps.tiledTranscoder;
19
20 import java.awt.image.renderable.*;
21 import java.awt.image.*;
22 import java.io.*;
23
24 import org.apache.batik.transcoder.*;
25 import org.apache.batik.transcoder.image.*;
26
27 import org.apache.batik.ext.awt.image.*;
28 import org.apache.batik.ext.awt.image.codec.*;
29 import org.apache.batik.ext.awt.image.codec.tiff.*;
30 import org.apache.batik.ext.awt.image.rendered.*;
31 import org.apache.batik.ext.awt.image.renderable.*;
32
33 import org.w3c.dom.Document JavaDoc;
34
35 public class TiledImageTranscoder extends SVGAbstractTranscoder {
36     
37     /**
38      * Constructs a new <tt>ImageTranscoder</tt>.
39      */

40     protected TiledImageTranscoder() {
41     }
42
43
44     /**
45      * Transcodes the specified Document as an image in the specified output.
46      *
47      * @param document the document to transcode
48      * @param uri the uri of the document or null if any
49      * @param output the ouput where to transcode
50      * @exception TranscoderException if an error occured while transcoding
51      */

52     protected void transcode(Document JavaDoc document,
53                              String JavaDoc uri,
54                              TranscoderOutput output)
55             throws TranscoderException {
56
57         // Sets up root, curTxf & curAoi
58
super.transcode(document, uri, output);
59
60         Filter f = this.root.getGraphicsNodeRable(true);
61         
62         RenderContext rc = new RenderContext(curTxf, null, null);
63         RenderedImage img = f.createRendering(rc);
64
65         // prepare the image to be painted
66
int w = img.getWidth();
67         int h = img.getHeight();
68
69         try {
70             int bands = img.getSampleModel().getNumBands();
71             int [] off = new int[bands];
72             for (int i=0; i<bands; i++)
73                 off[i] = i;
74             SampleModel sm = new PixelInterleavedSampleModel
75                 (DataBuffer.TYPE_BYTE,
76                  w, (100000+w-1)/w,
77                  bands, w*bands, off);
78             
79             RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img), sm);
80
81             TIFFImageEncoder enc = new TIFFImageEncoder
82                 (output.getOutputStream(), null);
83             enc.encode(rimg);
84         } catch (IOException ioe) {
85             ioe.printStackTrace();
86         }
87     }
88
89
90     public static void main (String JavaDoc [] args) {
91         try {
92             FileOutputStream fos = new FileOutputStream(args[1]);
93             TiledImageTranscoder tit = new TiledImageTranscoder();
94             tit.addTranscodingHint(KEY_WIDTH, new Float JavaDoc(10240));
95             tit.transcode(new TranscoderInput("file:" + args[0]),
96                           new TranscoderOutput(fos));
97         } catch (IOException ioe) {
98             ioe.printStackTrace();
99         } catch (TranscoderException te) {
100             te.printStackTrace();
101         }
102     }
103 }
104
Popular Tags