Merge branch 'Jalview-BH/JAL-3026-JAL-3063-JAXB' of
[jalview.git] / unused / 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  * 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.
8  *
9  * Copyright (C) 2000-2005  The Jmol Development Team
10  *
11  * Contact: jmol-developers@lists.sf.net
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 // ImageEncoder - abstract class for writing out an image
29 //
30 // Copyright (C) 1996 by Jef Poskanzer <jef@mail.acme.com>.  All rights reserved.
31 //
32 // Redistribution and use in source and binary forms, with or without
33 // modification, are permitted provided that the following conditions
34 // are met:
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.
40 //
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
51 // SUCH DAMAGE.
52 //
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/
55
56 package javajs.img;
57
58 import java.util.Map;
59
60 import javajs.api.GenericImageEncoder;
61 import javajs.util.OC;
62
63
64
65 /**
66  * Generic abstract image creator:
67  * 
68  *   (1) set parameters
69  *   
70  *   (2) encode the image bytes, if necessary
71  *   
72  *   (3) generate the image 
73  * @author Bob Hanson hansonr@stolaf.edu
74  */
75
76 public abstract class ImageEncoder implements GenericImageEncoder {
77
78   protected OC out;
79
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;
86
87   /**
88    * @param type
89    * @param out
90    * @param params
91    */
92   @Override
93   public boolean createImage(String type, OC out, Map<String, Object> params)
94       throws Exception {
95     this.out = out;
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());
103     setParams(params);
104     generate();
105     close(); // GIF will override this and not close
106     return doClose;
107   }
108
109   abstract protected void setParams(Map<String, Object> params);
110   abstract protected void generate() throws Exception;
111
112   protected int[] pixels;
113
114   protected void putString(String s) {
115     byte[] b = s.getBytes();
116     out.write(b, 0, b.length);
117   }
118
119   protected void putByte(int b) {
120     out.writeByteAsInt(b);
121   }
122   
123   protected void close() {
124     // your responsibility to close the output channel
125   }
126
127 }