Debian Codegraph @

src/dr/util/TIFFWriter.java

raw symbols SCIP
1 /*
2 * TIFFWriter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST 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
18 * GNU 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 BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 /**
29 * @author Marc A. Suchard
30 * @author Liya Thomas -- most code taken from Liya's November 2001 free source code
31 */
32
33
34 //import java.awt.*;
35
36 import dr.geo.color.ChannelColorScheme;
37 import dr.geo.color.ColorScheme;
38
39 import java.awt.image.BufferedImage;
40 import java.io.DataOutputStream;
41 import java.io.File;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 //import java.awt.image.IndexColorModel;
47
48 public class TIFFWriter {
49
50 public static final short MAXROWS = 6000; // maximum # of rows
51 public static final short MAXCOLUMNS = 3000; // maximum # of columns
52
53 public static void writeDoubleArray(String fileName, double[][] inputImageInt) {
54 writeDoubleArray(fileName, inputImageInt, "png", ColorScheme.HEATMAP);
55 }
56
57 public static void writeDoubleArrayMultiChannel(String fileName, List<double[][]> inputImageIntList, String format,
58 ChannelColorScheme scheme) {
59 // Get size, assumes the same for all matrix in list
60 int dim1 = inputImageIntList.get(0).length;
61 int dim2 = (inputImageIntList.get(0))[0].length;
62 BufferedImage image = new BufferedImage(dim1, dim2, BufferedImage.TYPE_INT_ARGB);
63
64 List<Double> max = new ArrayList<Double>();
65 List<Double> min = new ArrayList<Double>();
66
67 final int channels = inputImageIntList.size();
68
69 for (int c = 0; c < channels; ++c) {
70 double[][] inputImageInt = inputImageIntList.get(c);
71 double tmax = Double.NEGATIVE_INFINITY;
72 double tmin = Double.POSITIVE_INFINITY;
73 for (int i = 0; i < dim1; ++i) {
74 for (int j = 0; j < dim2; ++j) {
75 double value = inputImageInt[i][j];
76 if (value > tmax) tmax = value;
77 else if (value < tmin) tmin = value;
78 }
79 }
80 max.add(tmax);
81 min.add(tmin);
82 }
83
84 for (int i = 0; i < dim1; ++i) {
85 for (int j = 0; j < dim2; ++j) {
86 List<Double> input = new ArrayList<Double>();
87 for (int c = 0; c < channels; ++c) {
88 double value = (inputImageIntList.get(c))[i][j];
89 input.add(value);
90 }
91 image.setRGB(i, j, scheme.getColor(input, min, max).getRGB());
92 }
93 }
94
95 try {
96 javax.imageio.ImageIO.write(image, format, new File(fileName));
97 } catch (IOException e) {
98 e.printStackTrace();
99 }
100 }
101
102 public static void writeDoubleArray(String fileName, double[][] inputImageInt, String format, ColorScheme scheme) {
103 BufferedImage image = new BufferedImage(inputImageInt.length, inputImageInt[0].length, BufferedImage.TYPE_INT_ARGB);
104
105 double max = Double.NEGATIVE_INFINITY;
106 double min = Double.POSITIVE_INFINITY;
107 for (int i = 0; i < inputImageInt.length; ++i) {
108 for (int j = 0; j < inputImageInt[i].length; ++j) {
109 double value = inputImageInt[i][j];
110 if (value > max) max = value;
111 else if (value < min) min = value;
112 }
113 }
114
115 for (int i = 0; i < inputImageInt.length; ++i) {
116 for (int j = 0; j < inputImageInt[i].length; ++j) {
117 double value = inputImageInt[i][j];
118 image.setRGB(i, j, scheme.getColor(value, min, max).getRGB());
119 }
120 }
121
122 try {
123 javax.imageio.ImageIO.write(image, format, new File(fileName));
124 } catch (IOException e) {
125 e.printStackTrace();
126 }
127 }
128
129 // public static double getRampValue(double input, double min, double max) {
130 // double end = 1.0 / 6.0;
131 // double start = 0.0;
132 // return (input - min) / (max - min) * (end - start);
133 // }
134
135 // public static Color getColor(double input, double min, double max) {
136 // float hue = (float) getRampValue(input, min, max);
137 // float saturation = 0.85f;
138 // float alpha = 1.0f;
139 // return Color.getHSBColor(hue, saturation, alpha);
140 // }
141
142 // Create TIFF image of integer array
143 public static void writeDoubleArray(
144 DataOutputStream dataOut,
145 double[][] inputImageInt) {
146
147 final int rows = inputImageInt.length;
148 final int columns = inputImageInt[0].length;
149
150 if (rows < 0 || rows > MAXROWS || columns < 0 || columns > MAXCOLUMNS)
151 throw new RuntimeException("Invalid # rows and # columns");
152
153 // offset to the end of data (gray values) in file
154 int pos = 8 + rows * columns;
155
156 try {
157
158 /*
159 * Write the header
160 */
161 short i, j;
162 i = (short) 'I';
163 j = (short) (i * 256 + i);
164 fputword(dataOut, j);
165 fputword(dataOut, (short) 42);
166 fputlong(dataOut, pos);
167
168 /*
169 * Write the bitmap
170 */
171 for (i = 0; i < rows; i++)
172 for (j = 0; j < columns; j++) {
173 int datum = (int) inputImageInt[i][j];
174 dataOut.writeByte((byte) datum);
175 }
176
177 /*
178 * Write the tags
179 */
180
181 fputword(dataOut, (short) 8); // # of tags
182 writetiftag(dataOut, SubFileType, TIFFshort, 1, 1);
183 writetiftag(dataOut, ImageWidth, TIFFshort, 1, columns);
184 writetiftag(dataOut, ImageLength, TIFFshort, 1, rows);
185 writetiftag(dataOut, BitsPerSample, TIFFshort, 1, 8);
186 writetiftag(dataOut, Compression, TIFFshort, 1, 1);
187 writetiftag(dataOut, PhotoMetricInterp, TIFFshort, 1, 1); // for gray values only
188 writetiftag(dataOut, StripOffsets, TIFFlong, 1, 8); // beginning of image data
189 writetiftag(dataOut, PlanarConfiguration, TIFFshort, 1, 1);
190
191 fputlong(dataOut, 0);
192
193 } catch (java.io.IOException read) {
194 System.out.println("Error occured while writing output file.");
195 }
196
197 }
198
199 /*
200 * write one TIFF tag to the IFD
201 */
202 static void writetiftag(DataOutputStream dataOut, short tag, short type, int length, int offset) {
203 fputword(dataOut, tag);
204 fputword(dataOut, type);
205 fputlong(dataOut, length);
206 fputlong(dataOut, offset);
207 } /* writetiftag */
208
209 /*
210 * function: fputword
211 */
212 static void fputword(DataOutputStream dataOut, short n) {
213 try {
214 dataOut.writeByte((byte) n);
215 dataOut.writeByte((byte) (n >> 8));
216 } catch (java.io.IOException read) {
217 System.out.println("Error occured while writing output file.");
218 }
219
220 } /* fputword */
221
222 /*
223 * function: fputlong
224 */
225 static void fputlong(DataOutputStream dataOut, int n) {
226 try {
227 dataOut.writeByte((byte) n);
228 dataOut.writeByte((byte) (n >> 8));
229 dataOut.writeByte((byte) (n >> 16));
230 dataOut.writeByte((byte) (n >> 24));
231 } catch (java.io.IOException read) {
232 System.out.println("Error occured while writing output file.");
233 }
234
235 } /* fputlong */
236
237
238 public static final short GOOD_WRITE = 0;
239 public static final short BAD_WRITE = 1;
240 public static final short BAD_READ = 2;
241 public static final short MEMORY_ERROR = 3;
242 public static final short WRONG_BITS = 4;
243
244 public static final short RGB_RED = 0;
245 public static final short RGB_GREEN = 1;
246 public static final short RGB_BLUE = 2;
247 public static final short RGB_SIZE = 3;
248
249 /*
250 * TIFF object sizes
251 */
252 public static final short TIFFbyte = 1;
253 public static final short TIFFascii = 2;
254 public static final short TIFFshort = 3;
255 public static final short TIFFlong = 4;
256 public static final short TIFFrational = 5;
257
258 /*
259 * TIFF tag names
260 */
261 public static final short NewSubFile = 254;
262 public static final short SubFileType = 255;
263 public static final short ImageWidth = 256;
264 public static final short ImageLength = 257;
265 public static final short RowsPerStrip = 278;
266 public static final short StripOffsets = 273;
267 public static final short StripByteCounts = 279;
268 public static final short SamplesPerPixel = 277;
269 public static final short BitsPerSample = 258;
270 public static final short Compression = 259;
271 public static final short PlanarConfiguration = 284;
272 public static final short Group3Options = 292;
273 public static final short Group4Options = 293;
274 public static final short FillOrder = 266;
275 public static final short Threshholding = 263;
276 public static final short CellWidth = 264;
277 public static final short CellLength = 265;
278 public static final short MinSampleValue = 280;
279 public static final short MaxSampleValue = 281;
280 public static final short PhotoMetricInterp = 262;
281 public static final short GrayResponseUnit = 290;
282 public static final short GrayResponseCurve = 291;
283 public static final short ColorResponseUnit = 300;
284 public static final short ColorResponseCurves = 301;
285 public static final short XResolution = 282;
286 public static final short YResolution = 283;
287 public static final short ResolutionUnit = 296;
288 public static final short Orientation = 274;
289 public static final short DocumentName = 269;
290 public static final short PageName = 285;
291 public static final short XPosition = 286;
292 public static final short YPosition = 287;
293 public static final short PageNumber = 297;
294 public static final short ImageDescription = 270;
295 public static final short Make = 271;
296 public static final short Model = 272;
297 public static final short FreeOffsets = 288;
298 public static final short FreeByteCounts = 289;
299 public static final short ColorMap = 320;
300 public static final short Artist = 315;
301 public static final short DateTime = 306;
302 public static final short HostComputer = 316;
303 public static final short Software = 305;
304
305 }
306
307
308
309