JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / img / ImageEncoder.java
1 /* $RCSfile$
2  * $Author: hansonr $
3  * $Date: 2007-06-02 12:14:13 -0500 (Sat, 02 Jun 2007) $
4  * $Revision: 7831 $
5  *
6  * Copyright (C) 2000-2005  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 // ImageEncoder - abstract class for writing out an image
26 //
27 // Copyright (C) 1996 by Jef Poskanzer <jef@mail.acme.com>.  All rights reserved.
28 //
29 // Redistribution and use in source and binary forms, with or without
30 // modification, are permitted provided that the following conditions
31 // are met:
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.
37 //
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
48 // SUCH DAMAGE.
49 //
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/
52
53 package javajs.img;
54
55 import java.util.Map;
56
57 import javajs.api.GenericImageEncoder;
58 import javajs.util.OC;
59
60
61
62 /**
63  * Generic abstract image creator:
64  * 
65  *   (1) set parameters
66  *   
67  *   (2) encode the image bytes, if necessary
68  *   
69  *   (3) generate the image 
70  * @author Bob Hanson hansonr@stolaf.edu
71  */
72
73 public abstract class ImageEncoder implements GenericImageEncoder {
74
75   protected OC out;
76
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;
83
84   /**
85    * @param type
86    * @param out
87    * @param params
88    */
89   @Override
90   public boolean createImage(String type, OC out, Map<String, Object> params)
91       throws Exception {
92     this.out = out;
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());
100     setParams(params);
101     generate();
102     close(); // GIF will override this and not close
103     return doClose;
104   }
105
106   abstract protected void setParams(Map<String, Object> params);
107   abstract protected void generate() throws Exception;
108
109   protected int[] pixels;
110
111   protected void putString(String s) {
112     byte[] b = s.getBytes();
113     out.write(b, 0, b.length);
114   }
115
116   protected void putByte(int b) {
117     out.writeByteAsInt(b);
118   }
119   
120   protected void close() {
121     // your responsibility to close the output channel
122   }
123
124 }