1 // Copyright 2002, FreeHEP.
2 package org.freehep.graphicsio.emf.gdi;
3
4 import java.io.IOException;
5 import java.awt.Image;
6
7 import org.freehep.graphicsio.emf.EMFConstants;
8 import org.freehep.graphicsio.emf.EMFInputStream;
9 import org.freehep.graphicsio.emf.EMFOutputStream;
10 import org.freehep.graphicsio.emf.EMFTag;
11 import org.freehep.graphicsio.emf.EMFRenderer;
12
13 /**
14 * SetStretchBltMode TAG.
15 *
16 * @author Mark Donszelmann
17 * @version $Id: SetStretchBltMode.java 10367 2007-01-22 19:26:48Z duns $
18 */
19 public class SetStretchBltMode extends EMFTag implements EMFConstants {
20
21 private int mode;
22
23 public SetStretchBltMode() {
24 super(21, 1);
25 }
26
27 public SetStretchBltMode(int mode) {
28 this();
29 this.mode = mode;
30 }
31
32 public EMFTag read(int tagID, EMFInputStream emf, int len)
33 throws IOException {
34
35 return new SetStretchBltMode(emf.readDWORD());
36 }
37
38 public void write(int tagID, EMFOutputStream emf) throws IOException {
39 emf.writeDWORD(mode);
40 }
41
42 public String toString() {
43 return super.toString() + "\n mode: " + mode;
44 }
45
46 /**
47 * displays the tag using the renderer
48 *
49 * @param renderer EMFRenderer storing the drawing session data
50 */
51 public void render(EMFRenderer renderer) {
52 // The stretching mode defines how the system combines rows or columns of a
53 // bitmap with existing pixels on a display device when an application calls
54 // the StretchBlt function.
55 renderer.setScaleMode(
56 getScaleMode(mode));
57 }
58
59 /**
60 * converts a SetStretchBltMode to a TWIP_SCALE constat of class Image
61 * @return e.g. {@link java.awt.Image#SCALE_FAST}
62 * @param mode EMFTag SetStretchBltMode
63 */
64 private int getScaleMode(int mode) {
65 // COLORONCOLOR Deletes the pixels. This mode deletes all
66 // eliminated lines of pixels without trying to preserve their information.
67 if (
68 mode == EMFConstants.COLORONCOLOR /*||
69 mode == EMFConstants.STRETCH_DELETESCANS*/) {
70 return Image.SCALE_FAST;
71 }
72 // HALFTONE Maps pixels from the source rectangle into blocks
73 // of pixels in the destination rectangle. The average color over the
74 // destination block of pixels approximates the color of the source pixels.
75 else if (
76 mode == EMFConstants.HALFTONE /*||
77 mode == EMFConstants.STRETCH_HALFTONE*/) {
78 return Image.SCALE_SMOOTH;
79 }
80 // BLACKONWHITE Performs a Boolean AND operation using the
81 // color values for the eliminated and existing pixels. If the bitmap
82 // is a monochrome bitmap, this mode preserves black pixels at the
83 // expense of white pixels.
84 else if (
85 mode == EMFConstants.BLACKONWHITE /*||
86 mode == EMFConstants.STRETCH_ANDSCANS*/) {
87 // TODO not sure
88 return Image.SCALE_REPLICATE;
89 }
90 // WHITEONBLACK Performs a Boolean OR operation using the
91 // color values for the eliminated and existing pixels. If the bitmap
92 // is a monochrome bitmap, this mode preserves white pixels at the
93 // expense of black pixels.
94 else if (
95 mode == EMFConstants.WHITEONBLACK /*||
96 mode == EMFConstants.STRETCH_ORSCANS*/) {
97 // TODO not sure
98 return Image.SCALE_REPLICATE;
99 } else {
100 logger.warning("got unsupported SetStretchBltMode " + mode);
101 return Image.SCALE_DEFAULT;
102 }
103 }
104 }