3 * $Date: 2007-06-02 12:14:13 -0500 (Sat, 02 Jun 2007) $
6 * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
7 * for use in SwingJS via transpilation into JavaScript using Java2Script.
9 * Copyright (C) 2000-2005 The Jmol Development Team
11 * Contact: jmol-developers@lists.sf.net
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 // ImageEncoder - abstract class for writing out an image
30 // Copyright (C) 1996 by Jef Poskanzer <jef@mail.acme.com>. All rights reserved.
32 // Redistribution and use in source and binary forms, with or without
33 // modification, are permitted provided that the following conditions
35 // 1. Redistributions of source code must retain the above copyright
36 // notice, this list of conditions and the following disclaimer.
37 // 2. Redistributions in binary form must reproduce the above copyright
38 // notice, this list of conditions and the following disclaimer in the
39 // documentation and/or other materials provided with the distribution.
41 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 // Visit the ACME Labs Java page for up-to-date versions of this and other
54 // fine Java utilities: http://www.acme.com/java/
60 import javajs.api.GenericImageEncoder;
61 import javajs.util.OC;
66 * Generic abstract image creator:
70 * (2) encode the image bytes, if necessary
72 * (3) generate the image
73 * @author Bob Hanson hansonr@stolaf.edu
76 public abstract class ImageEncoder implements GenericImageEncoder {
80 protected int width = -1;
81 protected int height = -1;
82 protected int quality = -1;
83 protected String date;
84 protected boolean logging;
85 protected boolean doClose = true;
93 public boolean createImage(String type, OC out, Map<String, Object> params)
96 logging = (Boolean.TRUE == params.get("logging"));
97 width = ((Integer) params.get("imageWidth")).intValue();
98 height = ((Integer) params.get("imageHeight")).intValue();
99 pixels = (int[]) params.get("imagePixels");
100 date = (String) params.get("date");
101 Integer q = (Integer) params.get("quality");
102 quality = (q == null ? -1 : q.intValue());
105 close(); // GIF will override this and not close
109 abstract protected void setParams(Map<String, Object> params);
110 abstract protected void generate() throws Exception;
112 protected int[] pixels;
114 protected void putString(String s) {
115 byte[] b = s.getBytes();
116 out.write(b, 0, b.length);
119 protected void putByte(int b) {
120 out.writeByteAsInt(b);
123 protected void close() {
124 // your responsibility to close the output channel