View Javadoc

1   // Copyright 2002-2003, FreeHEP.
2   package org.freehep.graphicsio.emf.gdi;
3   
4   import java.awt.Color;
5   import java.awt.Rectangle;
6   import java.awt.geom.AffineTransform;
7   import java.awt.image.RenderedImage;
8   import java.awt.image.BufferedImage;
9   import java.io.IOException;
10  
11  import org.freehep.graphicsio.ImageGraphics2D;
12  import org.freehep.graphicsio.emf.EMFConstants;
13  import org.freehep.graphicsio.emf.EMFInputStream;
14  import org.freehep.graphicsio.emf.EMFOutputStream;
15  import org.freehep.graphicsio.emf.EMFTag;
16  import org.freehep.graphicsio.emf.EMFImageLoader;
17  import org.freehep.graphicsio.emf.EMFRenderer;
18  import org.freehep.graphicsio.raw.RawImageWriteParam;
19  import org.freehep.util.UserProperties;
20  import org.freehep.util.io.NoCloseOutputStream;
21  
22  /**
23   * BitBlt TAG. Encoded as plain RGB rather than the not-yet-working PNG format.
24   * The BI_code for BI_PNG and BI_JPG seems to be missing from the WINGDI.H file
25   * of visual C++.
26   * 
27   * @author Mark Donszelmann
28   * @version $Id: BitBlt.java 10367 2007-01-22 19:26:48Z duns $
29   */
30  public class BitBlt extends EMFTag implements EMFConstants {
31  
32      private final static int size = 100;
33  
34      private Rectangle bounds;
35  
36      private int x, y, width, height;
37  
38      private int dwROP;
39  
40      private int xSrc, ySrc;
41  
42      private AffineTransform transform;
43  
44      private Color bkg;
45  
46      private int usage;
47  
48      private BitmapInfo bmi;
49  
50      private BufferedImage image;
51  
52      public BitBlt() {
53          super(76, 1);
54      }
55  
56      public BitBlt(Rectangle bounds, int x, int y, int width, int height,
57              AffineTransform transform, BufferedImage image, Color bkg) {
58          this();
59          this.bounds = bounds;
60          this.x = x;
61          this.y = y;
62          this.width = width;
63          this.height = height;
64          this.dwROP = SRCCOPY;
65          this.xSrc = 0;
66          this.ySrc = 0;
67          this.transform = transform;
68          this.bkg = bkg;
69          this.usage = DIB_RGB_COLORS;
70          this.image = image;
71          this.bmi = null;
72      }
73  
74      public EMFTag read(int tagID, EMFInputStream emf, int len)
75              throws IOException {
76  
77          BitBlt tag = new BitBlt();
78  
79          tag.bounds = emf.readRECTL(); // 16
80          tag.x = emf.readLONG(); // 20
81          tag.y = emf.readLONG(); // 24
82          tag.width = emf.readLONG(); // 28
83          tag.height = emf.readLONG(); // 32
84          tag.dwROP = emf.readDWORD(); // 36
85          tag.xSrc = emf.readLONG(); // 40
86          tag.ySrc = emf.readLONG(); // 44
87          tag.transform = emf.readXFORM(); // 68
88          tag.bkg = emf.readCOLORREF(); // 72
89          tag.usage = emf.readDWORD(); // 76
90  
91          // ignored
92          /* int bmiOffset = */ emf.readDWORD(); // 80
93          int bmiSize = emf.readDWORD(); // 84
94          /* int bitmapOffset = */ emf.readDWORD(); // 88
95          int bitmapSize = emf.readDWORD(); // 92
96  
97          // read bmi
98          if (bmiSize > 0) {
99              tag.bmi = new BitmapInfo(emf);
100         } else {
101             tag.bmi = null;
102         }
103 
104         if (bitmapSize > 0 && tag.bmi != null) {
105             tag.image = EMFImageLoader.readImage(
106                 tag.bmi.getHeader(),
107                 tag.width,
108                 tag.height,
109                 emf,
110                 bitmapSize, null);
111         } else {
112             tag.image = null;
113         }
114 
115         return tag;
116     }
117 
118     public void write(int tagID, EMFOutputStream emf) throws IOException {
119         emf.writeRECTL(bounds);
120         emf.writeLONG(x);
121         emf.writeLONG(y);
122         emf.writeLONG(width);
123         emf.writeLONG(height);
124         emf.writeDWORD(dwROP);
125         emf.writeLONG(xSrc);
126         emf.writeLONG(ySrc);
127         emf.writeXFORM(transform);
128         emf.writeCOLORREF(bkg);
129         emf.writeDWORD(usage);
130         emf.writeDWORD(size); // bmi follows this record immediately
131         emf.writeDWORD(BitmapInfoHeader.size);
132         emf.writeDWORD(size + BitmapInfoHeader.size); // bitmap follows bmi
133 
134         emf.pushBuffer();
135 
136         UserProperties properties = new UserProperties();
137         properties.setProperty(RawImageWriteParam.BACKGROUND, bkg);
138         properties.setProperty(RawImageWriteParam.CODE, "BGR");
139         properties.setProperty(RawImageWriteParam.PAD, 4);
140         ImageGraphics2D.writeImage((RenderedImage)image, "raw", properties,
141                 new NoCloseOutputStream(emf));
142 
143         // emf.writeImage(image, bkg, "BGR", 4);
144         int length = emf.popBuffer();
145 
146         BitmapInfoHeader header = new BitmapInfoHeader(
147             image.getWidth(),
148             image.getHeight(), 24, BI_RGB, length, 0, 0, 0, 0);
149         bmi = new BitmapInfo(header);
150         bmi.write(emf);
151 
152         emf.writeDWORD(length);
153 
154         emf.append();
155     }
156 
157     public String toString() {
158         return super.toString() +
159             "\n  bounds: " + bounds +
160             "\n  x, y, w, h: " + x + " " + y + " " + width + " " + height +
161             "\n  dwROP: 0x" + Integer.toHexString(dwROP) +
162             "\n  xSrc, ySrc: " + xSrc + " " + ySrc +
163             "\n  transform: " + transform +
164             "\n  bkg: " + bkg +
165             "\n  usage: " + usage +
166             "\n" + ((bmi != null) ? bmi.toString() : "  bitmap: null");
167     }
168 
169     /**
170      * displays the tag using the renderer
171      *
172      * @param renderer EMFRenderer storing the drawing session data
173      */
174     public void render(EMFRenderer renderer) {
175         if (image != null) {
176             renderer.drawImage(image, transform);
177         }
178     }
179 }