Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ImageWriter.java
1 package ij.io;
2 import java.io.*;
3 import ij.*; //??
4 
6 public class ImageWriter {
7  private FileInfo fi;
8  private boolean showProgressBar=true;
9 
10  public ImageWriter (FileInfo fi) {
11  this.fi = fi;
12  }
13 
14  private void showProgress(double progress) {
15  if (showProgressBar)
16  IJ.showProgress(progress);
17  }
18 
19  void write8BitImage(OutputStream out, byte[] pixels) throws IOException {
20  int bytesWritten = 0;
21  int size = fi.width*fi.height;
22  int count = 8192;
23 
24  while (bytesWritten<size) {
25  if ((bytesWritten + count)>size)
26  count = size - bytesWritten;
27  out.write(pixels, bytesWritten, count);
28  bytesWritten += count;
29  showProgress((double)bytesWritten/size);
30  }
31  }
32 
33  void write8BitStack(OutputStream out, Object[] stack) throws IOException {
34  showProgressBar = false;
35  for (int i=0; i<fi.nImages; i++) {
36  IJ.showStatus("Writing: " + (i+1) + "/" + fi.nImages);
37  write8BitImage(out, (byte[])stack[i]);
38  IJ.showProgress((double)(i+1)/fi.nImages);
39  }
40  }
41 
42  void write16BitImage(OutputStream out, short[] pixels) throws IOException {
43  int bytesWritten = 0;
44  int size = fi.width*fi.height*2;
45  int count = 8192;
46  byte[] buffer = new byte[count];
47 
48  while (bytesWritten<size) {
49  if ((bytesWritten + count)>size)
50  count = size - bytesWritten;
51  int j = bytesWritten/2;
52  int value;
53  if (fi.intelByteOrder)
54  for (int i=0; i < count; i+=2) {
55  value = pixels[j];
56  buffer[i] = (byte)value;
57  buffer[i+1] = (byte)(value>>>8);
58  j++;
59  }
60  else
61  for (int i=0; i < count; i+=2) {
62  value = pixels[j];
63  buffer[i] = (byte)(value>>>8);
64  buffer[i+1] = (byte)value;
65  j++;
66  }
67  out.write(buffer, 0, count);
68  bytesWritten += count;
69  showProgress((double)bytesWritten/size);
70  }
71  }
72 
73  void write16BitStack(OutputStream out, Object[] stack) throws IOException {
74  showProgressBar = false;
75  for (int i=0; i<fi.nImages; i++) {
76  IJ.showStatus("Writing: " + (i+1) + "/" + fi.nImages);
77  write16BitImage(out, (short[])stack[i]);
78  IJ.showProgress((double)(i+1)/fi.nImages);
79  }
80  }
81 
82  void writeFloatImage(OutputStream out, float[] pixels) throws IOException {
83  int bytesWritten = 0;
84  int size = fi.width*fi.height*4;
85  int count = 8192;
86  byte[] buffer = new byte[count];
87  int tmp;
88 
89  while (bytesWritten<size) {
90  if ((bytesWritten + count)>size)
91  count = size - bytesWritten;
92  int j = bytesWritten/4;
93  if (fi.intelByteOrder)
94  for (int i=0; i < count; i+=4) {
95  tmp = Float.floatToIntBits(pixels[j]);
96  buffer[i] = (byte)tmp;
97  buffer[i+1] = (byte)(tmp>>8);
98  buffer[i+2] = (byte)(tmp>>16);
99  buffer[i+3] = (byte)(tmp>>24);
100  j++;
101  }
102  else
103  for (int i=0; i < count; i+=4) {
104  tmp = Float.floatToIntBits(pixels[j]);
105  buffer[i] = (byte)(tmp>>24);
106  buffer[i+1] = (byte)(tmp>>16);
107  buffer[i+2] = (byte)(tmp>>8);
108  buffer[i+3] = (byte)tmp;
109  j++;
110  }
111  out.write(buffer, 0, count);
112  bytesWritten += count;
113  showProgress((double)bytesWritten/size);
114  }
115  }
116 
117  void writeFloatStack(OutputStream out, Object[] stack) throws IOException {
118  showProgressBar = false;
119  for (int i=0; i<fi.nImages; i++) {
120  IJ.showStatus("Writing: " + (i+1) + "/" + fi.nImages);
121  writeFloatImage(out, (float[])stack[i]);
122  IJ.showProgress((double)(i+1)/fi.nImages);
123  }
124  }
125 
126  void writeRGBImage(OutputStream out, int[] pixels) throws IOException {
127  int bytesWritten = 0;
128  int size = fi.width*fi.height*3;
129  int count = fi.width*24;
130  byte[] buffer = new byte[count];
131 
132  while (bytesWritten<size) {
133  if ((bytesWritten + count)>size)
134  count = size - bytesWritten;
135  int j = bytesWritten/3;
136  for (int i=0; i < count; i+=3) {
137  buffer[i] = (byte)(pixels[j]>>16); //red
138  buffer[i+1] = (byte)(pixels[j]>>8); //green
139  buffer[i+2] = (byte)pixels[j]; //blue
140  j++;
141  }
142  out.write(buffer, 0, count);
143  bytesWritten += count;
144  showProgress((double)bytesWritten/size);
145  }
146  }
147 
148  void writeRGBStack(OutputStream out, Object[] stack) throws IOException {
149  showProgressBar = false;
150  for (int i=0; i<fi.nImages; i++) {
151  IJ.showStatus("Writing: " + (i+1) + "/" + fi.nImages);
152  writeRGBImage(out, (int[])stack[i]);
153  IJ.showProgress((double)(i+1)/fi.nImages);
154  }
155  }
156 
163  public void write(OutputStream out) throws IOException {
164  if (fi.pixels==null)
165  throw new IOException("ImageWriter: fi.pixels==null");
166  if (fi.nImages>1 && !(fi.pixels instanceof Object[]))
167  throw new IOException("ImageWriter: fi.pixels not a stack");
168  switch (fi.fileType) {
169  case FileInfo.GRAY8:
170  case FileInfo.COLOR8:
171  if (fi.nImages>1)
172  write8BitStack(out, (Object[])fi.pixels);
173  else
174  write8BitImage(out, (byte[])fi.pixels);
175  break;
176  case FileInfo.GRAY16_SIGNED:
178  if (fi.nImages>1)
179  write16BitStack(out, (Object[])fi.pixels);
180  else
181  write16BitImage(out, (short[])fi.pixels);
182  break;
183  case FileInfo.GRAY32_FLOAT:
184  if (fi.nImages>1)
185  writeFloatStack(out, (Object[])fi.pixels);
186  else
187  writeFloatImage(out, (float[])fi.pixels);
188  break;
189  case FileInfo.RGB:
190  if (fi.nImages>1)
191  writeRGBStack(out, (Object[])fi.pixels);
192  else
193  writeRGBImage(out, (int[])fi.pixels);
194  break;
195  default:
196  }
197  }
198 
199 }
200