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