Showing the changes to java/pkg/com/sun/imageio/plugins/common/LZWCompressor.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,146 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package com.sun.imageio.plugins.common; | ||
| 27 | |||
| 28 | import java.io.IOException; | ||
| 29 | import java.io.PrintStream; | ||
| 30 | import javax.imageio.stream.ImageOutputStream; | ||
| 31 | |||
| 32 | /* | ||
| 33 | * Modified from original LZWCompressor to change interface to passing a | ||
| 34 | * buffer of data to be compressed. | ||
| 35 | */ | ||
| 36 | public class LZWCompressor { | ||
| 37 | /** base underlying code size of data being compressed 8 for TIFF, 1 to 8 for GIF **/ | ||
| 38 | int codeSize; | ||
| 39 | |||
| 40 | /** reserved clear code based on code size **/ | ||
| 41 | int clearCode; | ||
| 42 | |||
| 43 | /** reserved end of data code based on code size **/ | ||
| 44 | int endOfInfo; | ||
| 45 | |||
| 46 | /** current number bits output for each code **/ | ||
| 47 | int numBits; | ||
| 48 | |||
| 49 | /** limit at which current number of bits code size has to be increased **/ | ||
| 50 | int limit; | ||
| 51 | |||
| 52 | /** the prefix code which represents the predecessor string to current input point **/ | ||
| 53 | short prefix; | ||
| 54 | |||
| 55 | /** output destination for bit codes **/ | ||
| 56 | BitFile bf; | ||
| 57 | |||
| 58 | /** general purpose LZW string table **/ | ||
| 59 | LZWStringTable lzss; | ||
| 60 | |||
| 61 | /** modify the limits of the code values in LZW encoding due to TIFF bug / feature **/ | ||
| 62 | boolean tiffFudge; | ||
| 63 | |||
| 64 | /** | ||
| 65 | * @param out destination for compressed data | ||
| 66 | * @param codeSize the initial code size for the LZW compressor | ||
| 67 | * @param TIFF flag indicating that TIFF lzw fudge needs to be applied | ||
| 68 | * @exception IOException if underlying output stream error | ||
| 69 | **/ | ||
| 70 | public LZWCompressor(ImageOutputStream out, int codeSize, boolean TIFF) | ||
| 71 | throws IOException | ||
| 72 | { | ||
| 73 | bf = new BitFile(out, !TIFF); // set flag for GIF as NOT tiff | ||
| 74 | this.codeSize = codeSize; | ||
| 75 | tiffFudge = TIFF; | ||
| 76 | clearCode = 1 << codeSize; | ||
| 77 | endOfInfo = clearCode + 1; | ||
| 78 | numBits = codeSize + 1; | ||
| 79 | |||
| 80 | limit = (1 << numBits) - 1; | ||
| 81 | if (tiffFudge) { | ||
| 82 | --limit; | ||
| 83 | } | ||
| 84 | |||
| 85 | prefix = (short)0xFFFF; | ||
| 86 | lzss = new LZWStringTable(); | ||
| 87 | lzss.clearTable(codeSize); | ||
| 88 | bf.writeBits(clearCode, numBits); | ||
| 89 | } | ||
| 90 | |||
| 91 | /** | ||
| 92 | * @param buf data to be compressed to output stream | ||
| 93 | * @exception IOException if underlying output stream error | ||
| 94 | **/ | ||
| 95 | public void compress(byte[] buf, int offset, int length) | ||
| 96 | throws IOException | ||
| 97 | { | ||
| 98 | int idx; | ||
| 99 | byte c; | ||
| 100 | short index; | ||
| 101 | |||
| 102 | int maxOffset = offset + length; | ||
| 103 | for (idx = offset; idx < maxOffset; ++idx) { | ||
| 104 | c = buf[idx]; | ||
| 105 | if ((index = lzss.findCharString(prefix, c)) != -1) { | ||
| 106 | prefix = index; | ||
| 107 | } else { | ||
| 108 | bf.writeBits(prefix, numBits); | ||
| 109 | if (lzss.addCharString(prefix, c) > limit) { | ||
| 110 | if (numBits == 12) { | ||
| 111 | bf.writeBits(clearCode, numBits); | ||
| 112 | lzss.clearTable(codeSize); | ||
| 113 | numBits = codeSize + 1; | ||
| 114 | } else { | ||
| 115 | ++numBits; | ||
| 116 | } | ||
| 117 | |||
| 118 | limit = (1 << numBits) - 1; | ||
| 119 | if (tiffFudge) { | ||
| 120 | --limit; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | prefix = (short)((short)c & 0xFF); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | /* | ||
| 129 | * Indicate to compressor that no more data to go so write out | ||
| 130 | * any remaining buffered data. | ||
| 131 | * | ||
| 132 | * @exception IOException if underlying output stream error | ||
| 133 | */ | ||
| 134 | public void flush() throws IOException { | ||
| 135 | if (prefix != -1) { | ||
| 136 | bf.writeBits(prefix, numBits); | ||
| 137 | } | ||
| 138 | |||
| 139 | bf.writeBits(endOfInfo, numBits); | ||
| 140 | bf.flush(); | ||
| 141 | } | ||
| 142 | |||
| 143 | public void dump(PrintStream out) { | ||
| 144 | lzss.dump(out); | ||
| 145 | } | ||
| 146 | } | ||