You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ROP3Table.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.hwmf;
  16. import java.awt.AlphaComposite;
  17. import java.awt.Color;
  18. import java.awt.Font;
  19. import java.awt.Graphics2D;
  20. import java.awt.Rectangle;
  21. import java.awt.Shape;
  22. import java.awt.TexturePaint;
  23. import java.awt.font.TextLayout;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.geom.Ellipse2D;
  26. import java.awt.geom.Rectangle2D;
  27. import java.awt.image.BufferedImage;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import javax.imageio.ImageIO;
  31. import org.apache.poi.hwmf.draw.HwmfGraphics;
  32. import org.apache.poi.hwmf.draw.HwmfROP3Composite;
  33. import org.apache.poi.hwmf.record.HwmfTernaryRasterOp;
  34. /**
  35. * Generates an image table describing the various ternary raster operations
  36. *
  37. * inspired from http://www.evmsoft.net/en/roptest.html
  38. */
  39. public final class ROP3Table {
  40. private ROP3Table() {}
  41. private static final long PATTERN = 0xAADDAA55AADDAA55L;
  42. private static final HwmfTernaryRasterOp[] OPS = HwmfTernaryRasterOp.values();
  43. private static final int COLS = 16, BOX = 100;
  44. private static final double SCALE = 2, HEADER = 1.1;
  45. private static final Rectangle2D RECT = new Rectangle2D.Double(0.05*BOX, 0.05*BOX, 0.90*BOX, 0.90*BOX);
  46. private static final Shape CIRCLE_BIG = new Ellipse2D.Double(0.15*BOX, 0.15*BOX, 0.70*BOX, 0.70*BOX);
  47. private static final Shape CIRCLE_SMALL = new Ellipse2D.Double(0.40*BOX, 0.40*BOX, 0.20*BOX, 0.20*BOX);
  48. private static final Shape LABEL_BOX = new Rectangle.Double(0.06*BOX, 0.85*BOX, 0.88*BOX, 0.10*BOX);
  49. private static final AlphaComposite SRC_OVER = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
  50. private static final AffineTransform INIT_AT = AffineTransform.getScaleInstance(SCALE, SCALE);
  51. public static void main(String[] args) throws IOException {
  52. BufferedImage pattern = getPattern();
  53. BufferedImage source = getSource();
  54. BufferedImage dest = new BufferedImage(
  55. (int)(BOX * COLS * SCALE),
  56. (int)(BOX * (Math.max(OPS.length/COLS,1) + HEADER) * SCALE),
  57. BufferedImage.TYPE_INT_ARGB);
  58. Graphics2D g = dest.createGraphics();
  59. g.setTransform(INIT_AT);
  60. g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10));
  61. g.translate(BOX * (COLS-5) / 2., 0);
  62. g.setColor(Color.BLACK);
  63. fillDest(g);
  64. fillLabel(g, "Dest");
  65. g.translate(2*BOX, 0);
  66. g.drawImage(source, 0, 0, null);
  67. fillLabel(g, "Source");
  68. g.translate(2*BOX, 0);
  69. g.setPaint(new TexturePaint(pattern, RECT));
  70. g.fill(RECT);
  71. fillLabel(g, "Pattern");
  72. int idx=0;
  73. for (HwmfTernaryRasterOp op : OPS) {
  74. g.setTransform(INIT_AT);
  75. g.translate(0, HEADER * BOX);
  76. g.translate(BOX*(idx%COLS), BOX*(idx/COLS));
  77. fillDest(g);
  78. fillPattern(g, op, pattern, source);
  79. fillLabel(g, op.name());
  80. idx++;
  81. }
  82. g.dispose();
  83. ImageIO.write(dest, "PNG", new File("rop3.png"));
  84. }
  85. private static BufferedImage getPattern() {
  86. return HwmfGraphics.getPatternFromLong(PATTERN, Color.BLACK, Color.WHITE, false);
  87. }
  88. private static BufferedImage getSource() {
  89. BufferedImage checker = new BufferedImage(BOX, BOX, BufferedImage.TYPE_INT_ARGB);
  90. Graphics2D cg = checker.createGraphics();
  91. cg.setColor(Color.PINK);
  92. cg.fill(RECT);
  93. cg.setColor(new Color(0xE6E6FA, false));
  94. cg.fill(new Rectangle2D.Double(0.05*BOX, 0.05*BOX, 0.45*BOX, 0.45*BOX));
  95. cg.fill(new Rectangle2D.Double(0.50*BOX, 0.50*BOX, 0.45*BOX, 0.45*BOX));
  96. cg.dispose();
  97. return checker;
  98. }
  99. private static void fillDest(Graphics2D g) {
  100. g.setComposite(SRC_OVER);
  101. g.setColor(Color.LIGHT_GRAY);
  102. g.fill(RECT);
  103. g.setColor(new Color(0xDAA520, false));
  104. g.fill(CIRCLE_BIG);
  105. g.setColor(Color.RED);
  106. g.fill(CIRCLE_SMALL);
  107. }
  108. private static void fillPattern(Graphics2D g, HwmfTernaryRasterOp op, BufferedImage pattern, BufferedImage source) {
  109. g.setComposite(new HwmfROP3Composite(g.getTransform(), RECT, op, pattern, Color.YELLOW, Color.BLUE));
  110. g.setClip(RECT);
  111. g.drawImage(source, 0, 0, null);
  112. g.setClip(null);
  113. g.setComposite(SRC_OVER);
  114. }
  115. private static void fillLabel(Graphics2D g, String str) {
  116. g.setColor(Color.WHITE);
  117. g.fill(LABEL_BOX);
  118. g.setColor(Color.BLACK);
  119. TextLayout t = new TextLayout(str, g.getFont(), g.getFontRenderContext());
  120. Rectangle2D b = t.getBounds();
  121. g.drawString(str, (float)((BOX -b.getWidth())/2.), (float)(0.94*BOX));
  122. }
  123. }