8 private boolean showProgressBar=
true;
14 private void showProgress(
double progress) {
19 void write8BitImage(OutputStream out, byte[] pixels)
throws IOException {
21 int size = fi.width*fi.height;
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);
33 void write8BitStack(OutputStream out, Object[] stack)
throws IOException {
34 showProgressBar =
false;
35 for (
int i=0; i<fi.nImages; i++) {
37 write8BitImage(out, (byte[])stack[i]);
42 void write16BitImage(OutputStream out,
short[] pixels)
throws IOException {
44 int size = fi.width*fi.height*2;
46 byte[] buffer =
new byte[count];
48 while (bytesWritten<size) {
49 if ((bytesWritten + count)>size)
50 count = size - bytesWritten;
51 int j = bytesWritten/2;
53 if (fi.intelByteOrder)
54 for (
int i=0; i < count; i+=2) {
56 buffer[i] = (byte)value;
57 buffer[i+1] = (byte)(value>>>8);
61 for (
int i=0; i < count; i+=2) {
63 buffer[i] = (byte)(value>>>8);
64 buffer[i+1] = (byte)value;
67 out.write(buffer, 0, count);
68 bytesWritten += count;
69 showProgress((
double)bytesWritten/size);
73 void write16BitStack(OutputStream out, Object[] stack)
throws IOException {
74 showProgressBar =
false;
75 for (
int i=0; i<fi.nImages; i++) {
77 write16BitImage(out, (
short[])stack[i]);
82 void writeFloatImage(OutputStream out,
float[] pixels)
throws IOException {
84 int size = fi.width*fi.height*4;
86 byte[] buffer =
new byte[count];
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);
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;
111 out.write(buffer, 0, count);
112 bytesWritten += count;
113 showProgress((
double)bytesWritten/size);
117 void writeFloatStack(OutputStream out, Object[] stack)
throws IOException {
118 showProgressBar =
false;
119 for (
int i=0; i<fi.nImages; i++) {
121 writeFloatImage(out, (
float[])stack[i]);
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];
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);
138 buffer[i+1] = (byte)(pixels[j]>>8);
139 buffer[i+2] = (byte)pixels[j];
142 out.write(buffer, 0, count);
143 bytesWritten += count;
144 showProgress((
double)bytesWritten/size);
148 void writeRGBStack(OutputStream out, Object[] stack)
throws IOException {
149 showProgressBar =
false;
150 for (
int i=0; i<fi.nImages; i++) {
152 writeRGBImage(out, (
int[])stack[i]);
163 public void write(OutputStream out)
throws IOException {
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) {
172 write8BitStack(out, (Object[])fi.pixels);
174 write8BitImage(out, (byte[])fi.pixels);
179 write16BitStack(out, (Object[])fi.pixels);
181 write16BitImage(out, (
short[])fi.pixels);
185 writeFloatStack(out, (Object[])fi.pixels);
187 writeFloatImage(out, (
float[])fi.pixels);
191 writeRGBStack(out, (Object[])fi.pixels);
193 writeRGBImage(out, (
int[])fi.pixels);