View Javadoc

1   /**
2    * $Id$
3    *
4    * Copyright (c) 1998-2006
5    * semture GmbH
6    *
7    * Alle Rechte vorbehalten
8    */
9   package org.freehep.graphicsio.emf.gdi;
10  
11  import org.freehep.graphicsio.emf.EMFTag;
12  import org.freehep.graphicsio.emf.EMFInputStream;
13  import org.freehep.graphicsio.emf.EMFImageLoader;
14  import org.freehep.graphicsio.emf.EMFOutputStream;
15  import org.freehep.graphicsio.emf.EMFRenderer;
16  
17  import java.io.IOException;
18  import java.awt.image.BufferedImage;
19  import java.awt.TexturePaint;
20  import java.awt.Rectangle;
21  
22  /**
23   * @author Steffen Greiffenberg
24   * @version $Revision$
25   */
26  public class CreateDIBPatternBrushPt extends EMFTag {
27  
28      private int usage;
29  
30      private BitmapInfo bmi;
31  
32      private BufferedImage image;
33  
34      private int index;
35  
36      public CreateDIBPatternBrushPt() {
37          super(94, 1);
38      }
39  
40      public EMFTag read(int tagID, EMFInputStream emf, int len)
41              throws IOException {
42  
43          CreateDIBPatternBrushPt tag = new CreateDIBPatternBrushPt();
44  
45          // read the index for storing the GDIObject
46          tag.index = emf.readDWORD();
47  
48          // read whatever
49          /*byte[] bytes =*/ emf.readByte(24);
50  
51          tag.bmi = new BitmapInfo(emf);
52  
53          // not used but read:
54          // DIB_PAL_COLORS 	A color table is provided and
55          // consists of an array of 16-bit indexes into the
56          // logical palette of the device context into which
57          // the brush is to be selected.
58          // DIB_RGB_COLORS 	A color table is provided and
59          // contains literal RGB values.
60          tag.usage = emf.readDWORD();
61  
62          tag.image = EMFImageLoader.readImage(
63              tag.bmi.getHeader(),
64              tag.bmi.getHeader().getWidth(),
65              tag.bmi.getHeader().getHeight(),
66              emf,
67              len - 4 - 24 - BitmapInfoHeader.size - 4, null);
68  
69          return tag;
70      }
71  
72      public void write(int tagID, EMFOutputStream emf) throws IOException {
73          logger.warning("not implemented");
74      }
75  
76      public String toString() {
77          return super.toString() +
78              "\n  usage: " + usage +
79              "\n" + bmi.toString();
80      }
81  
82      /**
83       * displays the tag using the renderer
84       *
85       * @param renderer EMFRenderer storing the drawing session data
86       */
87      public void render(EMFRenderer renderer) {
88          // The StretchDIBits function copies the color data for a rectangle of pixels in a
89          // DIB to the specified destination rectangle. If the destination rectangle is larger
90          // than the source rectangle, this function stretches the rows and columns of color
91          // data to fit the destination rectangle. If the destination rectangle is smaller
92          // than the source rectangle, this function compresses the rows and columns by using
93          // the specified raster operation.
94          renderer.storeGDIObject(index, new GDIObject() {
95              public void render(EMFRenderer renderer) {
96                  if (image != null) {
97                      renderer.setBrushPaint(new TexturePaint(image, new Rectangle(0, 0, 16, 16)));
98                  }
99              }
100         });
101     }
102 }