KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > io > TargaRLEInputStream


1 package com.imagero.uio.io;
2 import java.io.IOException JavaDoc;
3 import java.io.InputStream JavaDoc;
4
5 /**
6  * @author Andrey Kuznetsov
7  */

8 public class TargaRLEInputStream extends RLEInputStream {
9
10     int numSamples;
11     byte [] value;
12     boolean rawPacket;
13     int pixelSize;
14     int vindex;
15
16     public TargaRLEInputStream(InputStream JavaDoc in, int pixelSize) {
17         super(in);
18         this.pixelSize = pixelSize;
19         value = new byte[pixelSize];
20     }
21
22     public int read() throws IOException JavaDoc {
23         if (numSamples == 0) {
24             int v = in.read();
25             if(v == -1) {
26                 return -1;
27             }
28             if ((v >> 7) == 1) {
29                 for (int i = 0; i < value.length; i++) {
30                     value[i] = (byte) in.read();
31                 }
32                 numSamples = ((v & 0x7F) + 1) * pixelSize;
33                 rawPacket = false;
34             }
35             else {
36                 numSamples = (v + 1) * pixelSize;
37                 rawPacket = true;
38             }
39         }
40         numSamples--;
41         if (rawPacket) {
42             return in.read();
43         }
44         else {
45             int b = value[vindex++] & 0xFF;
46             if(vindex == pixelSize) {
47                 vindex = 0;
48             }
49             return b;
50         }
51     }
52 }
53
Popular Tags