Debian Codegraph @

contrib/tiledTranscoder/TiledImageTranscoder.java

raw
1 /*
2
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 */
19 package org.apache.batik.apps.tiledTranscoder;
20
21 import java.awt.image.renderable.*;
22 import java.awt.image.*;
23 import java.io.*;
24
25 import org.apache.batik.transcoder.*;
26 import org.apache.batik.transcoder.image.*;
27
28 import org.apache.batik.ext.awt.image.*;
29 import org.apache.batik.ext.awt.image.codec.*;
30 import org.apache.batik.ext.awt.image.codec.tiff.*;
31 import org.apache.batik.ext.awt.image.rendered.*;
32 import org.apache.batik.ext.awt.image.renderable.*;
33
34 import org.w3c.dom.Document;
35
36 public class TiledImageTranscoder extends SVGAbstractTranscoder {
37
38 /**
39 * Constructs a new <code>ImageTranscoder</code>.
40 */
41 protected TiledImageTranscoder() {
42 }
43
44
45 /**
46 * Transcodes the specified Document as an image in the specified output.
47 *
48 * @param document the document to transcode
49 * @param uri the uri of the document or null if any
50 * @param output the ouput where to transcode
51 * @exception TranscoderException if an error occured while transcoding
52 */
53 protected void transcode(Document document,
54 String uri,
55 TranscoderOutput output)
56 throws TranscoderException {
57
58 // Sets up root, curTxf & curAoi
59 super.transcode(document, uri, output);
60
61 Filter f = this.root.getGraphicsNodeRable(true);
62
63 RenderContext rc = new RenderContext(curTxf, null, null);
64 RenderedImage img = f.createRendering(rc);
65
66 // prepare the image to be painted
67 int w = img.getWidth();
68 int h = img.getHeight();
69
70 try {
71 int bands = img.getSampleModel().getNumBands();
72 int [] off = new int[bands];
73 for (int i=0; i<bands; i++)
74 off[i] = i;
75 SampleModel sm = new PixelInterleavedSampleModel
76 (DataBuffer.TYPE_BYTE,
77 w, (100000+w-1)/w,
78 bands, w*bands, off);
79
80 RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img), sm);
81
82 TIFFImageEncoder enc = new TIFFImageEncoder
83 (output.getOutputStream(), null);
84 enc.encode(rimg);
85 } catch (IOException ioe) {
86 ioe.printStackTrace();
87 }
88 }
89
90
91 public static void main (String [] args) {
92 try {
93 FileOutputStream fos = new FileOutputStream(args[1]);
94 TiledImageTranscoder tit = new TiledImageTranscoder();
95 tit.addTranscodingHint(KEY_WIDTH, Float.valueOf(10240));
96 tit.transcode(new TranscoderInput("file:" + args[0]),
97 new TranscoderOutput(fos));
98 } catch (IOException ioe) {
99 ioe.printStackTrace();
100 } catch (TranscoderException te) {
101 te.printStackTrace();
102 }
103 }
104 }