001/* 002 * $RCSfile: TIFFT6Compressor.java,v $ 003 * 004 * 005 * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without 008 * modification, are permitted provided that the following conditions 009 * are met: 010 * 011 * - Redistribution of source code must retain the above copyright 012 * notice, this list of conditions and the following disclaimer. 013 * 014 * - Redistribution in binary form must reproduce the above copyright 015 * notice, this list of conditions and the following disclaimer in 016 * the documentation and/or other materials provided with the 017 * distribution. 018 * 019 * Neither the name of Sun Microsystems, Inc. or the names of 020 * contributors may be used to endorse or promote products derived 021 * from this software without specific prior written permission. 022 * 023 * This software is provided "AS IS," without a warranty of any 024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS 030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, 032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND 033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR 034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 035 * POSSIBILITY OF SUCH DAMAGES. 036 * 037 * You acknowledge that this software is not designed or intended for 038 * use in the design, construction, operation or maintenance of any 039 * nuclear facility. 040 * 041 * $Revision: 1.3 $ 042 * $Date: 2006/04/11 22:10:37 $ 043 * $State: Exp $ 044 */ 045package com.github.jaiimageio.impl.plugins.tiff; 046 047import java.io.IOException; 048 049import javax.imageio.IIOException; 050 051import com.github.jaiimageio.plugins.tiff.BaselineTIFFTagSet; 052import com.github.jaiimageio.plugins.tiff.TIFFField; 053import com.github.jaiimageio.plugins.tiff.TIFFTag; 054 055/** 056 * 057 */ 058public class TIFFT6Compressor extends TIFFFaxCompressor { 059 060 public TIFFT6Compressor() { 061 super("CCITT T.6", BaselineTIFFTagSet.COMPRESSION_CCITT_T_6, true); 062 } 063 064 /** 065 * Encode a buffer of data using CCITT T.6 Compression also known as 066 * Group 4 facsimile compression. 067 * 068 * @param data The row of data to compress. 069 * @param lineStride Byte step between the same sample in different rows. 070 * @param colOffset Bit offset within first <code>data[rowOffset]</code>. 071 * @param width Number of bits in the row. 072 * @param height Number of rows in the buffer. 073 * @param compData The compressed data. 074 * 075 * @return The number of bytes saved in the compressed data array. 076 */ 077 public synchronized int encodeT6(byte[] data, 078 int lineStride, 079 int colOffset, 080 int width, 081 int height, 082 byte[] compData) 083 { 084 // 085 // ao, a1, a2 are bit indices in the current line 086 // b1 and b2 are bit indices in the reference line (line above) 087 // color is the current color (WHITE or BLACK) 088 // 089 byte[] refData = null; 090 int refAddr = 0; 091 int lineAddr = 0; 092 int outIndex = 0; 093 094 initBitBuf(); 095 096 // 097 // Iterate over all lines 098 // 099 while(height-- != 0) { 100 int a0 = colOffset; 101 int last = a0 + width; 102 103 int testbit = 104 ((data[lineAddr + (a0>>>3)]&0xff) >>> 105 (7-(a0 & 0x7))) & 0x1; 106 int a1 = testbit != 0 ? 107 a0 : nextState(data, lineAddr, a0, last); 108 109 testbit = refData == null ? 110 0: ((refData[refAddr + (a0>>>3)]&0xff) >>> 111 (7-(a0 & 0x7))) & 0x1; 112 int b1 = testbit != 0 ? 113 a0 : nextState(refData, refAddr, a0, last); 114 115 // 116 // The current color is set to WHITE at line start 117 // 118 int color = WHITE; 119 120 while(true) { 121 int b2 = nextState(refData, refAddr, b1, last); 122 if(b2 < a1) { // pass mode 123 outIndex += add2DBits(compData, outIndex, pass, 0); 124 a0 = b2; 125 } else { 126 int tmp = b1 - a1 + 3; 127 if((tmp <= 6) && (tmp >= 0)) { // vertical mode 128 outIndex += add2DBits(compData, outIndex, vert, tmp); 129 a0 = a1; 130 } else { // horizontal mode 131 int a2 = nextState(data, lineAddr, a1, last); 132 outIndex += add2DBits(compData, outIndex, horz, 0); 133 outIndex += add1DBits(compData, outIndex, a1-a0, color); 134 outIndex += add1DBits(compData, outIndex, a2-a1, color^1); 135 a0 = a2; 136 } 137 } 138 if(a0 >= last) { 139 break; 140 } 141 color = ((data[lineAddr + (a0>>>3)]&0xff) >>> 142 (7-(a0 & 0x7))) & 0x1; 143 a1 = nextState(data, lineAddr, a0, last); 144 b1 = nextState(refData, refAddr, a0, last); 145 testbit = refData == null ? 146 0: ((refData[refAddr + (b1>>>3)]&0xff) >>> 147 (7-(b1 & 0x7))) & 0x1; 148 if(testbit == color) { 149 b1 = nextState(refData, refAddr, b1, last); 150 } 151 } 152 153 refData = data; 154 refAddr = lineAddr; 155 lineAddr += lineStride; 156 157 } // End while(height--) 158 159 // 160 // append eofb 161 // 162 outIndex += addEOFB(compData, outIndex); 163 164 // Flip the bytes if inverse fill was requested. 165 if(inverseFill) { 166 for(int i = 0; i < outIndex; i++) { 167 compData[i] = TIFFFaxDecompressor.flipTable[compData[i]&0xff]; 168 } 169 } 170 171 return outIndex; 172 } 173 174 public int encode(byte[] b, int off, 175 int width, int height, 176 int[] bitsPerSample, 177 int scanlineStride) throws IOException { 178 if (bitsPerSample.length != 1 || bitsPerSample[0] != 1) { 179 throw new IIOException( 180 "Bits per sample must be 1 for T6 compression!"); 181 } 182 183 184 if (metadata instanceof TIFFImageMetadata) { 185 TIFFImageMetadata tim = (TIFFImageMetadata)metadata; 186 187 long[] options = new long[1]; 188 options[0] = 0; 189 190 BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance(); 191 TIFFField T6Options = 192 new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_T6_OPTIONS), 193 TIFFTag.TIFF_LONG, 194 1, 195 options); 196 tim.rootIFD.addTIFFField(T6Options); 197 } 198 199 // See comment in TIFFT4Compressor 200 int maxBits = 12*((width + 1)/2) + 2; 201 int bufSize = (maxBits + 7)/8; 202 bufSize = height*(bufSize + 2) + 12; 203 204 byte[] compData = new byte[bufSize]; 205 int bytes = encodeT6(b, scanlineStride, 8*off, width, height, 206 compData); 207 stream.write(compData, 0, bytes); 208 return bytes; 209 } 210}