KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > common > BitFile


1 /*
2  * @(#)BitFile.java 1.2 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.common;
9
10 import java.io.IOException JavaDoc;
11 import javax.imageio.stream.ImageOutputStream JavaDoc;
12
13 /*
14  * Came from GIFEncoder initially.
15  * Modified - to allow for output compressed data without the block counts
16  * which breakup the compressed data stream for GIF.
17  */

18 public class BitFile {
19     ImageOutputStream JavaDoc output;
20     byte buffer[];
21     int index;
22     int bitsLeft; // bits left at current index that are avail.
23

24     /** note this also indicates gif format BITFile. **/
25     boolean blocks = false;
26
27     /*
28      * @param output destination for output data
29      * @param blocks GIF LZW requires block counts for output data
30      */

31     public BitFile(ImageOutputStream JavaDoc output, boolean blocks) {
32         this.output = output;
33         this.blocks = blocks;
34         buffer = new byte[256];
35         index = 0;
36         bitsLeft = 8;
37     }
38     
39     public void flush() throws IOException JavaDoc {
40         int numBytes = index + (bitsLeft == 8 ? 0 : 1);
41         if (numBytes > 0) {
42             if (blocks) {
43                 output.write(numBytes);
44             }
45             output.write(buffer, 0, numBytes);
46             buffer[0] = 0;
47             index = 0;
48             bitsLeft = 8;
49         }
50     }
51
52     public void writeBits(int bits, int numbits) throws IOException JavaDoc {
53         int bitsWritten = 0;
54         int numBytes = 255; // gif block count
55
do {
56             // This handles the GIF block count stuff
57
if ((index == 254 && bitsLeft == 0) || index > 254) {
58                 if (blocks) {
59                     output.write(numBytes);
60                 }
61                 
62                 output.write(buffer, 0, numBytes);
63                 
64                 buffer[0] = 0;
65                 index = 0;
66                 bitsLeft = 8;
67             }
68             
69             if (numbits <= bitsLeft) { // bits contents fit in current index byte
70
if (blocks) { // GIF
71
buffer[index] |= (bits & ((1 << numbits) - 1)) << (8 - bitsLeft);
72                     bitsWritten += numbits;
73                     bitsLeft -= numbits;
74                     numbits = 0;
75                 } else {
76                     buffer[index] |= (bits & ((1 << numbits) - 1)) << (bitsLeft - numbits);
77                     bitsWritten += numbits;
78                     bitsLeft -= numbits;
79                     numbits = 0;
80                 }
81             } else { // bits overflow from current byte to next.
82
if (blocks) { // GIF
83
// if bits > space left in current byte then the lowest order bits
84
// of code are taken and put in current byte and rest put in next.
85
buffer[index] |= (bits & ((1 << bitsLeft) - 1)) << (8 - bitsLeft);
86                     bitsWritten += bitsLeft;
87                     bits >>= bitsLeft;
88                     numbits -= bitsLeft;
89                     buffer[++index] = 0;
90                     bitsLeft = 8;
91                 } else {
92                     // if bits > space left in current byte then the highest order bits
93
// of code are taken and put in current byte and rest put in next.
94
// at highest order bit location !!
95
int topbits = (bits >>> (numbits - bitsLeft)) & ((1 << bitsLeft) - 1);
96                     buffer[index] |= topbits;
97                     numbits -= bitsLeft; // ok this many bits gone off the top
98
bitsWritten += bitsLeft;
99                     buffer[++index] = 0; // next index
100
bitsLeft = 8;
101                 }
102             }
103         } while (numbits != 0);
104     }
105 }
106
Popular Tags