Content of file WangAnnotations.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>WangAnnotations.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Mistral Core</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.image.metadata</a> &gt; <span class="el_source">WangAnnotations.java</span></div><h1>WangAnnotations.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * Mistral: open source imaging engine
 * http://tidalwave.it/projects/mistral
 *
 * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/mistral-src
 * git clone https://github.com/tidalwave-it/mistral-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.image.metadata;

import java.util.ArrayList;
import java.util.List;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteOrder;
import javax.imageio.ImageIO;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import it.tidalwave.image.EditableImage;
import it.tidalwave.image.op.DrawOp;

/***********************************************************************************************************************
 *
 * http://support.global360.com/content/I4W/documentation/annospec.htm
 *
 * @author Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc bnc" id="L49" title="All 2 branches missed.">public class WangAnnotations</span>
  {
    public static class Attributes
      {
        // http://msdn2.microsoft.com/en-us/library/ms533931(VS.85).aspx
        public static class LogFont
          {
            private final int height;
            private final int width;
            private final int escapement;
            private final int orientation;
            private final int weight;
            private final byte italic;
            private final byte underline;
            private final byte strikeout;
            private final byte charset;
            private final byte outprec;
            private final byte clipprec;
            private final byte quality;
            private final byte pitch;
            private final String faceName;

            protected LogFont (final byte[] buffer)
                    throws IOException
<span class="nc" id="L73">              {</span>
<span class="nc" id="L74">                final var iis = ImageIO.createImageInputStream(new ByteArrayInputStream(buffer));</span>
<span class="nc" id="L75">                iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);</span>
<span class="nc" id="L76">                height = iis.readInt();</span>
<span class="nc" id="L77">                width = iis.readInt();</span>
<span class="nc" id="L78">                escapement = iis.readInt();</span>
<span class="nc" id="L79">                orientation = iis.readInt();</span>
<span class="nc" id="L80">                weight = iis.readInt();</span>
<span class="nc" id="L81">                italic = iis.readByte();</span>
<span class="nc" id="L82">                underline = iis.readByte();</span>
<span class="nc" id="L83">                strikeout = iis.readByte();</span>
<span class="nc" id="L84">                charset = iis.readByte();</span>
<span class="nc" id="L85">                outprec = iis.readByte();</span>
<span class="nc" id="L86">                clipprec = iis.readByte();</span>
<span class="nc" id="L87">                quality = iis.readByte();</span>
<span class="nc" id="L88">                pitch = iis.readByte();</span>
<span class="nc" id="L89">                final var bytes = new byte[16]; // FIXME</span>
<span class="nc" id="L90"> iis.read(bytes);</span> <span class="nc" id="L91"> faceName = new String(bytes);</span> <span class="nc" id="L92"> iis.close();</span> <span class="nc" id="L93"> }</span> public Font createFont() { <span class="nc bnc" id="L97" title="All 2 branches missed."> return new Font(faceName, (weight &lt; 700) ? Font.PLAIN : Font.BOLD, 40 * height / 15); // FIXME</span> } @Override public String toString() { <span class="nc" id="L103"> return String.format(&quot;LogFont[height: %d, weight: %d, face: %s]&quot;, height, weight, faceName);</span> } } private final int type; private final int x1; private final int y1; private final int x2; private final int y2; private final byte highlight; private final byte transparent; private final int lineSize; private final LogFont logFont; private final int timeStamp; private final byte visible; protected Attributes (final byte[] buffer) throws IOException <span class="nc" id="L121"> {</span> <span class="nc" id="L122"> final var iis = ImageIO.createImageInputStream(new ByteArrayInputStream(buffer));</span> <span class="nc" id="L123"> iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);</span> <span class="nc" id="L124"> type = iis.readInt();</span> <span class="nc" id="L125"> x1 = iis.readInt();</span> <span class="nc" id="L126"> y1 = iis.readInt();</span> <span class="nc" id="L127"> x2 = iis.readInt();</span> <span class="nc" id="L128"> y2 = iis.readInt();</span> <span class="nc" id="L130"> iis.skipBytes(3); // color1</span> <span class="nc" id="L131"> iis.skipBytes(3); // color2</span> <span class="nc" id="L132"> highlight = iis.readByte();</span> <span class="nc" id="L133"> transparent = iis.readByte();</span> <span class="nc" id="L134"> lineSize = iis.readInt();</span> <span class="nc" id="L136"> iis.readLong(); // reserved 1</span> <span class="nc" id="L137"> iis.readLong(); // reserved 2</span> <span class="nc" id="L138"> final var logFontBuffer = new byte[64];</span> <span class="nc" id="L139"> iis.read(logFontBuffer);</span> <span class="nc" id="L140"> logFont = new LogFont(logFontBuffer);</span> <span class="nc" id="L141"> timeStamp = iis.readInt();</span> <span class="nc" id="L142"> visible = iis.readByte();</span> <span class="nc" id="L143"> iis.skipBytes(8); // reserved 4</span> <span class="nc" id="L144"> iis.close();</span> <span class="nc" id="L145"> }</span> public int getType() { <span class="nc" id="L149"> return type;</span> } public void drawString (final Graphics2D g, final EditableImage image, final String text) { <span class="nc" id="L154"> g.setColor(Color.BLACK);</span> // g.setStroke(new BasicStroke(3)); // drawRectangle(g, image); <span class="nc" id="L157"> g.setFont(logFont.createFont());</span> <span class="nc" id="L158"> g.drawString(text, (float)x1, (float)y1 + g.getFontMetrics().getAscent());</span> // System.err.println(String.format(&quot;Rendered %s at %d, %d&quot;, text, xx, yy)); <span class="nc" id="L160"> }</span> public void drawRectangle (final Graphics2D g, final EditableImage image) { <span class="nc" id="L164"> final var ww = Math.abs(x2 - x1);</span> <span class="nc" id="L165"> final var hh = Math.abs(y2 - y1);</span> // g.setColor(Color.WHITE); // g.fillRect(xx2, yy2, ww, hh); <span class="nc" id="L168"> g.setStroke(new BasicStroke(3));</span> <span class="nc" id="L169"> g.setColor(Color.BLACK);</span> <span class="nc" id="L170"> g.drawRect(x1, y1, ww, hh);</span> <span class="nc" id="L171"> System.err.println(String.format(&quot;&gt;&gt;&gt;&gt; Rendered rectangle: %d,%d,%d,%d - %dx%d&quot;, x1, y1, x2, y2, ww, hh));</span> // System.err.println(String.format(&quot;Rendered %s at %f, %f&quot;, text, xx, yy)); <span class="nc" id="L173"> }</span> @Override public String toString() { <span class="nc" id="L178"> return String.format(&quot;Attributes [%d %d,%d,%d,%d visible: %d font: %s]&quot;,</span> <span class="nc" id="L179"> type,</span> <span class="nc" id="L180"> x1,</span> <span class="nc" id="L181"> y1,</span> <span class="nc" id="L182"> x2,</span> <span class="nc" id="L183"> y2,</span> <span class="nc" id="L184"> visible,</span> logFont); } } public abstract static class Internal { protected final Attributes attributes; public Internal (final Attributes attributes) <span class="nc" id="L194"> {</span> <span class="nc" id="L195"> this.attributes = attributes;</span> <span class="nc" id="L196"> }</span> public abstract void render (EditableImage image); } public static class OiAnText extends Internal { private final double orientation; private final double resolution; private final String text; protected OiAnText (final Attributes attributes, final byte[] buffer) throws IOException { <span class="nc" id="L210"> super(attributes);</span> <span class="nc" id="L211"> final var iis = ImageIO.createImageInputStream(new ByteArrayInputStream(buffer));</span> <span class="nc" id="L212"> iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);</span> <span class="nc" id="L213"> orientation = iis.readInt() / 10.0;</span> <span class="nc" id="L214"> final var d1000 = iis.readInt();</span> <span class="nc" id="L215"> resolution = 72000.0 / iis.readInt();</span> <span class="nc" id="L216"> final var len = iis.readInt();</span> <span class="nc" id="L217"> final var bytes = new byte[len];</span> <span class="nc" id="L218"> iis.read(bytes);</span> <span class="nc bnc" id="L219" title="All 2 branches missed."> final var stringLength = (bytes[len - 1]) == 0 ? len - 1 : len;</span> <span class="nc" id="L220"> text = new String(bytes, 0, stringLength, &quot;CP1252&quot;);</span> <span class="nc" id="L221"> iis.close();</span> <span class="nc" id="L222"> }</span> @Override public void render (final EditableImage image) { <span class="nc" id="L227"> image.execute(new DrawOp((g, image1) -&gt; attributes.drawString(g, image1, text)));</span> <span class="nc" id="L228"> }</span> @Override public String toString() { <span class="nc" id="L233"> return String.format(&quot;OiAnText[angle: %f resolution: %f text: %s, %s]&quot;,</span> <span class="nc" id="L234"> orientation,</span> <span class="nc" id="L235"> resolution,</span> text, attributes); } } public static class OiHilite extends Internal { protected OiHilite (final Attributes attributes) { <span class="nc" id="L245"> super(attributes);</span> <span class="nc" id="L246"> System.err.println(String.format(&quot;OiHilite&quot;));</span> <span class="nc" id="L247"> }</span> @Override public void render (final EditableImage image) { <span class="nc" id="L252"> image.execute(new DrawOp(attributes::drawRectangle));</span> <span class="nc" id="L253"> }</span> @Override public String toString() { <span class="nc" id="L258"> return String.format(&quot;OiHilite[%s]&quot;, attributes);</span> } } <span class="nc" id="L262"> private final List&lt;Internal&gt; internals = new ArrayList&lt;&gt;();</span> public WangAnnotations (final byte[] buffer) throws IOException <span class="nc" id="L266"> {</span> <span class="nc" id="L267"> final var bais = new ByteArrayInputStream(buffer);</span> <span class="nc" id="L268"> final var iis = ImageIO.createImageInputStream(bais);</span> <span class="nc" id="L269"> iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);</span> <span class="nc" id="L271"> final var header = iis.readInt();</span> <span class="nc" id="L272"> final var intSize = iis.readInt();</span> <span class="nc bnc" id="L273" title="All 4 branches missed."> assert intSize == 1;</span> <span class="nc" id="L274"> Attributes attributes = null;</span> <span class="nc bnc" id="L276" title="All 2 branches missed."> while (iis.getStreamPosition() &lt; buffer.length)</span> { <span class="nc" id="L278"> final var dataType = iis.readInt();</span> <span class="nc" id="L279"> final var dataSize = iis.readInt();</span> // System.err.println(String.format(&quot;dataType: %d size: %d&quot;, dataType, dataSize)); <span class="nc bnc" id="L282" title="All 4 branches missed."> if ((dataType == 2) || (dataType == 6))</span> { <span class="nc bnc" id="L284" title="All 2 branches missed."> System.err.println((dataType == 2) ? &quot;DEFAULT NAMED BLOCK&quot; : &quot;PART OF PRECEDING&quot;);</span> <span class="nc" id="L285"> final var bytes = new byte[8];</span> <span class="nc" id="L286"> iis.read(bytes);</span> <span class="nc" id="L287"> final var name = new String(bytes);</span> // System.err.println(String.format(&quot;named block: %s&quot;, name)); <span class="nc" id="L289"> final var size = iis.readInt();</span> <span class="nc" id="L290"> final var block = new byte[size];</span> <span class="nc" id="L291"> iis.read(block);</span> <span class="nc bnc" id="L293" title="All 2 branches missed."> if (name.startsWith(&quot;OiAnText&quot;))</span> { <span class="nc" id="L295"> internals.add(new OiAnText(attributes, block));</span> } <span class="nc bnc" id="L297" title="All 2 branches missed."> if (name.startsWith(&quot;OiHilite&quot;))</span> { <span class="nc" id="L299"> internals.add(new OiHilite(attributes));</span> } else { <span class="nc" id="L303"> System.err.println(String.format(&quot;unmanaged named block: %s&quot;, name));</span> } <span class="nc" id="L305"> }</span> <span class="nc bnc" id="L307" title="All 2 branches missed."> else if (dataType == 5)</span> { <span class="nc" id="L309"> final var block = new byte[dataSize];</span> <span class="nc" id="L310"> iis.read(block);</span> <span class="nc" id="L311"> attributes = new Attributes(block);</span> } <span class="nc" id="L313"> }</span> <span class="nc" id="L315"> System.err.println(internals);</span> <span class="nc" id="L316"> }</span> public void render (final EditableImage image) { <span class="nc bnc" id="L320" title="All 2 branches missed."> for (final var internal : internals)</span> { <span class="nc" id="L322"> internal.render(image);</span> <span class="nc" id="L323"> }</span> <span class="nc" id="L324"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>