Minor code clean-up for WMFImageReader

This commit is contained in:
Harald Kuhr 2022-06-10 17:01:55 +02:00
parent 2b04f7205c
commit 00aec2c90e

View File

@ -1,141 +1,143 @@
/* /*
* Copyright (c) 2008, Harald Kuhr * Copyright (c) 2008, Harald Kuhr
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* * Redistributions of source code must retain the above copyright notice, this * * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. * list of conditions and the following disclaimer.
* *
* * Redistributions in binary form must reproduce the above copyright notice, * * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* *
* * Neither the name of the copyright holder nor the names of its * * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from * contributors may be used to endorse or promote products derived from
* this software without specific prior written permission. * this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.twelvemonkeys.imageio.plugins.wmf; package com.twelvemonkeys.imageio.plugins.wmf;
import com.twelvemonkeys.imageio.ImageReaderBase; import com.twelvemonkeys.imageio.ImageReaderBase;
import com.twelvemonkeys.imageio.plugins.svg.SVGImageReader; import com.twelvemonkeys.imageio.plugins.svg.SVGImageReader;
import com.twelvemonkeys.imageio.plugins.svg.SVGReadParam; import com.twelvemonkeys.imageio.plugins.svg.SVGReadParam;
import com.twelvemonkeys.imageio.util.IIOUtil; import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import org.apache.batik.transcoder.TranscoderException; import com.twelvemonkeys.imageio.util.IIOUtil;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.wmf.tosvg.WMFTranscoder; import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import javax.imageio.IIOException; import org.apache.batik.transcoder.wmf.tosvg.WMFTranscoder;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam; import javax.imageio.IIOException;
import javax.imageio.ImageTypeSpecifier; import javax.imageio.ImageReadParam;
import javax.imageio.spi.ImageReaderSpi; import javax.imageio.ImageTypeSpecifier;
import java.awt.image.BufferedImage; import javax.imageio.spi.ImageReaderSpi;
import java.io.*; import java.awt.image.*;
import java.util.Iterator; import java.io.ByteArrayOutputStream;
import java.io.IOException;
/** import java.io.OutputStreamWriter;
* WMFImageReader class description. import java.io.Writer;
* import java.nio.charset.StandardCharsets;
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a> import java.util.Iterator;
* @author last modified by $Author: haku $
* @version $Id: WMFImageReader.java,v 1.0 29.jul.2004 13:00:59 haku Exp $ /**
*/ * WMFImageReader class description.
// TODO: Probably possible to do less wrapping/unwrapping of data... *
// TODO: Consider using temp file instead of in-memory stream * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
public final class WMFImageReader extends ImageReaderBase { * @author last modified by $Author: haku $
* @version $Id: WMFImageReader.java,v 1.0 29.jul.2004 13:00:59 haku Exp $
private SVGImageReader reader = null; */
// TODO: Probably possible to do less wrapping/unwrapping of data...
public WMFImageReader(final ImageReaderSpi pProvider) { public final class WMFImageReader extends ImageReaderBase {
super(pProvider);
} private SVGImageReader reader = null;
protected void resetMembers() { public WMFImageReader(final ImageReaderSpi pProvider) {
if (reader != null) { super(pProvider);
reader.dispose(); }
}
protected void resetMembers() {
reader = null; if (reader != null) {
} reader.dispose();
}
public BufferedImage read(int pIndex, ImageReadParam pParam) throws IOException {
init(); reader = null;
}
processImageStarted(pIndex);
public BufferedImage read(int pIndex, ImageReadParam pParam) throws IOException {
BufferedImage image = reader.read(pIndex, pParam); init();
if (abortRequested()) {
processReadAborted(); processImageStarted(pIndex);
return image;
} BufferedImage image = reader.read(pIndex, pParam);
processImageProgress(100f); if (abortRequested()) {
processReadAborted();
processImageComplete(); return image;
}
return image; processImageProgress(100f);
}
processImageComplete();
private synchronized void init() throws IOException {
// Need the extra test, to avoid throwing an IOException from the Transcoder return image;
if (imageInput == null) { }
throw new IllegalStateException("input == null");
} private void init() throws IOException {
// Need the extra test, to avoid throwing an IOException from the Transcoder
if (reader == null) { if (imageInput == null) {
WMFTranscoder transcoder = new WMFTranscoder(); throw new IllegalStateException("input == null");
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(output, "UTF8"); if (reader == null) {
try { WMFTranscoder transcoder = new WMFTranscoder();
TranscoderInput in = new TranscoderInput(IIOUtil.createStreamAdapter(imageInput)); ByteArrayOutputStream output = new ByteArrayOutputStream(8192);
TranscoderOutput out = new TranscoderOutput(writer);
try (Writer writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
// TODO: Transcodinghints? TranscoderInput in = new TranscoderInput(IIOUtil.createStreamAdapter(imageInput));
TranscoderOutput out = new TranscoderOutput(writer);
transcoder.transcode(in, out);
} // TODO: Transcodinghints?
catch (TranscoderException e) {
throw new IIOException(e.getMessage(), e); transcoder.transcode(in, out);
} }
catch (TranscoderException e) {
reader = new SVGImageReader(getOriginatingProvider()); throw new IIOException(e.getMessage(), e);
reader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(output.toByteArray()))); }
}
} reader = new SVGImageReader(getOriginatingProvider());
reader.setInput(new ByteArrayImageInputStream(output.toByteArray()));
@Override }
public ImageReadParam getDefaultReadParam() { }
return new SVGReadParam();
} @Override
public ImageReadParam getDefaultReadParam() {
public int getWidth(int pIndex) throws IOException { return new SVGReadParam();
init(); }
return reader.getWidth(pIndex);
} public int getWidth(int pIndex) throws IOException {
init();
public int getHeight(int pIndex) throws IOException { return reader.getWidth(pIndex);
init(); }
return reader.getHeight(pIndex);
} public int getHeight(int pIndex) throws IOException {
init();
public Iterator<ImageTypeSpecifier> getImageTypes(final int pImageIndex) throws IOException { return reader.getHeight(pIndex);
init(); }
return reader.getImageTypes(pImageIndex);
} public Iterator<ImageTypeSpecifier> getImageTypes(final int pImageIndex) throws IOException {
init();
} return reader.getImageTypes(pImageIndex);
}
}