All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javajs.img.ImageEncoder Maven / Gradle / Ivy

There is a newer version: 14.31.10
Show newest version
/* $RCSfile$
 * $Author: hansonr $
 * $Date: 2007-06-02 12:14:13 -0500 (Sat, 02 Jun 2007) $
 * $Revision: 7831 $
 *
 * Copyright (C) 2000-2005  The Jmol Development Team
 *
 * Contact: [email protected]
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package javajs.img;

import java.util.Map;

import javajs.api.GenericImageEncoder;
import javajs.util.OC;



/**
 * Generic abstract image creator:
 * 
 *   (1) set parameters
 *   
 *   (2) encode the image bytes, if necessary
 *   
 *   (3) generate the image 
 * @author Bob Hanson [email protected]
 */

public abstract class ImageEncoder implements GenericImageEncoder {

  protected OC out;

  protected int width = -1;
  protected int height = -1;
  protected int quality = -1;
  protected String date;
  protected boolean logging;
  protected boolean doClose = true;

  /**
   * @param type
   * @param out
   * @param params
   */
  @Override
  public boolean createImage(String type, OC out, Map params)
      throws Exception {
    this.out = out;
    logging = (Boolean.TRUE == params.get("logging"));
    width = ((Integer) params.get("imageWidth")).intValue();
    height = ((Integer) params.get("imageHeight")).intValue();
    System.out.println("ImageEncoder width height " + width + " " + height);
    pixels = (int[]) params.get("imagePixels");
    date = (String) params.get("date");
    Integer q = (Integer) params.get("quality");
    quality = (q == null ? -1 : q.intValue());
    setParams(params);
    generate();
    close(); // GIF will override this and not close
    return doClose;
  }

  abstract protected void setParams(Map params);
  abstract protected void generate() throws Exception;

  protected int[] pixels;

  protected void putString(String s) {
    byte[] b = s.getBytes();
    out.write(b, 0, b.length);
  }

  protected void putByte(int b) {
    out.writeByteAsInt(b);
  }
  
  protected void close() {
    // your responsibility to close the output channel
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy