Package renderer

Source Code of renderer.Renderer$Writer

package renderer;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.LinkedList;

import plugin.typeinterfaces.DataStreamFilter;

import skomp.io.ColorInputStream;
import skomp.io.ColorMode;
import skomp.io.ColorOutputStream;

public class Renderer extends Thread {

  private LinkedList<DataStreamFilter> filters = new LinkedList<DataStreamFilter>();

  private static Renderer instance_ = new Renderer();

  private Renderer() {
  }

  public static Renderer getInstance() {
    return instance_;
  }

  protected void export(File target, BufferedImage img)
      throws FileNotFoundException {
    ColorOutputStream cos = new ColorOutputStream(new FileOutputStream(
        target));
  }

  protected BufferedImage filter(BufferedImage image,
      LinkedList<DataStreamFilter> filterchain) {
    BufferedImage ret = new BufferedImage(image.getWidth(), image
        .getHeight(), BufferedImage.TYPE_INT_ARGB);
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = null;
    try {
      out = new PipedOutputStream(in);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
    ColorInputStream cis = new ColorInputStream(in,
        ColorMode.TRUECOLOR32BIT, true);
    cis.addFilterChain(filterchain);
    ColorOutputStream cos = new ColorOutputStream(out);
    Writer babbler = new Writer(cos, image);
    Reader reader = new Reader(cis, ret);
    reader.start();
    babbler.start();
    synchronized (this) {
      try {
        this.wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    return ret;
  }

  @Override
  public void run() {
  }

  class Writer extends Thread {

    private ColorOutputStream cos;

    private IOException lastException;

    private BufferedImage img;

    protected Writer(ColorOutputStream cos, BufferedImage img) {
      this.cos = cos;
      this.img = img;
    }

    @Override
    public void run() {
      int read = 0;
      for (int i = 0; i < img.getHeight(); i++) {
        for (int j = 0; j < img.getWidth(); j++) {
          try {
            if ((read = img.getRGB(i, j)) != -1)
              cos.write(read);
          } catch (IOException e) {
            e.printStackTrace();
            lastException = e;
            return;
          }
        }
      }
      try {
        cos.write(-1);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  class Reader extends Thread {
    private ColorInputStream cis;

    private BufferedImage img;

    protected Reader(ColorInputStream cis, BufferedImage img) {
      super();
      this.cis = cis;
      this.img = img;
    }

    @Override
    public void run() {
      Color read;
      try {
        for (int i = 0; i < img.getHeight(); i++) {
          for (int j = 0; j < img.getWidth(); j++) {
            read = cis.readNextColor();
            if (read == null || interrupted()) {
              synchronized (this) {
                this.notifyAll();
              }
            }
            img.setRGB(i, j, read.getRGB());
          }
        }
      } catch (IOException e) {
        this.notifyAll();
        e.printStackTrace();
      } catch (ArrayIndexOutOfBoundsException e) {
      }
    }
  }
}
TOP

Related Classes of renderer.Renderer$Writer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.