Skip to content

Commit

Permalink
Issue jai-imageio#37 optimization for fill order lsb.
Browse files Browse the repository at this point in the history
  • Loading branch information
lbellonda committed May 24, 2017
1 parent 0d6b4ca commit a36bf00
Show file tree
Hide file tree
Showing 3 changed files with 321 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package com.github.jaiimageio.impl.plugins.tiff;

public class TIFFFillOrder {

public static final int reverseTable[] = {
0x00, //0 00000000 -> 00000000
0x80, //1 00000001 -> 10000000
0x40, //2 00000010 -> 01000000
0xC0, //3 00000011 -> 11000000
0x20, //4 00000100 -> 00100000
0xA0, //5 00000101 -> 10100000
0x60, //6 00000110 -> 01100000
0xE0, //7 00000111 -> 11100000
0x10, //8 00001000 -> 00010000
0x90, //9 00001001 -> 10010000
0x50, //10 00001010 -> 01010000
0xD0, //11 00001011 -> 11010000
0x30, //12 00001100 -> 00110000
0xB0, //13 00001101 -> 10110000
0x70, //14 00001110 -> 01110000
0xF0, //15 00001111 -> 11110000
0x08, //16 00010000 -> 00001000
0x88, //17 00010001 -> 10001000
0x48, //18 00010010 -> 01001000
0xC8, //19 00010011 -> 11001000
0x28, //20 00010100 -> 00101000
0xA8, //21 00010101 -> 10101000
0x68, //22 00010110 -> 01101000
0xE8, //23 00010111 -> 11101000
0x18, //24 00011000 -> 00011000
0x98, //25 00011001 -> 10011000
0x58, //26 00011010 -> 01011000
0xD8, //27 00011011 -> 11011000
0x38, //28 00011100 -> 00111000
0xB8, //29 00011101 -> 10111000
0x78, //30 00011110 -> 01111000
0xF8, //31 00011111 -> 11111000
0x04, //32 00100000 -> 00000100
0x84, //33 00100001 -> 10000100
0x44, //34 00100010 -> 01000100
0xC4, //35 00100011 -> 11000100
0x24, //36 00100100 -> 00100100
0xA4, //37 00100101 -> 10100100
0x64, //38 00100110 -> 01100100
0xE4, //39 00100111 -> 11100100
0x14, //40 00101000 -> 00010100
0x94, //41 00101001 -> 10010100
0x54, //42 00101010 -> 01010100
0xD4, //43 00101011 -> 11010100
0x34, //44 00101100 -> 00110100
0xB4, //45 00101101 -> 10110100
0x74, //46 00101110 -> 01110100
0xF4, //47 00101111 -> 11110100
0x0C, //48 00110000 -> 00001100
0x8C, //49 00110001 -> 10001100
0x4C, //50 00110010 -> 01001100
0xCC, //51 00110011 -> 11001100
0x2C, //52 00110100 -> 00101100
0xAC, //53 00110101 -> 10101100
0x6C, //54 00110110 -> 01101100
0xEC, //55 00110111 -> 11101100
0x1C, //56 00111000 -> 00011100
0x9C, //57 00111001 -> 10011100
0x5C, //58 00111010 -> 01011100
0xDC, //59 00111011 -> 11011100
0x3C, //60 00111100 -> 00111100
0xBC, //61 00111101 -> 10111100
0x7C, //62 00111110 -> 01111100
0xFC, //63 00111111 -> 11111100
0x02, //64 01000000 -> 00000010
0x82, //65 01000001 -> 10000010
0x42, //66 01000010 -> 01000010
0xC2, //67 01000011 -> 11000010
0x22, //68 01000100 -> 00100010
0xA2, //69 01000101 -> 10100010
0x62, //70 01000110 -> 01100010
0xE2, //71 01000111 -> 11100010
0x12, //72 01001000 -> 00010010
0x92, //73 01001001 -> 10010010
0x52, //74 01001010 -> 01010010
0xD2, //75 01001011 -> 11010010
0x32, //76 01001100 -> 00110010
0xB2, //77 01001101 -> 10110010
0x72, //78 01001110 -> 01110010
0xF2, //79 01001111 -> 11110010
0x0A, //80 01010000 -> 00001010
0x8A, //81 01010001 -> 10001010
0x4A, //82 01010010 -> 01001010
0xCA, //83 01010011 -> 11001010
0x2A, //84 01010100 -> 00101010
0xAA, //85 01010101 -> 10101010
0x6A, //86 01010110 -> 01101010
0xEA, //87 01010111 -> 11101010
0x1A, //88 01011000 -> 00011010
0x9A, //89 01011001 -> 10011010
0x5A, //90 01011010 -> 01011010
0xDA, //91 01011011 -> 11011010
0x3A, //92 01011100 -> 00111010
0xBA, //93 01011101 -> 10111010
0x7A, //94 01011110 -> 01111010
0xFA, //95 01011111 -> 11111010
0x06, //96 01100000 -> 00000110
0x86, //97 01100001 -> 10000110
0x46, //98 01100010 -> 01000110
0xC6, //99 01100011 -> 11000110
0x26, //100 01100100 -> 00100110
0xA6, //101 01100101 -> 10100110
0x66, //102 01100110 -> 01100110
0xE6, //103 01100111 -> 11100110
0x16, //104 01101000 -> 00010110
0x96, //105 01101001 -> 10010110
0x56, //106 01101010 -> 01010110
0xD6, //107 01101011 -> 11010110
0x36, //108 01101100 -> 00110110
0xB6, //109 01101101 -> 10110110
0x76, //110 01101110 -> 01110110
0xF6, //111 01101111 -> 11110110
0x0E, //112 01110000 -> 00001110
0x8E, //113 01110001 -> 10001110
0x4E, //114 01110010 -> 01001110
0xCE, //115 01110011 -> 11001110
0x2E, //116 01110100 -> 00101110
0xAE, //117 01110101 -> 10101110
0x6E, //118 01110110 -> 01101110
0xEE, //119 01110111 -> 11101110
0x1E, //120 01111000 -> 00011110
0x9E, //121 01111001 -> 10011110
0x5E, //122 01111010 -> 01011110
0xDE, //123 01111011 -> 11011110
0x3E, //124 01111100 -> 00111110
0xBE, //125 01111101 -> 10111110
0x7E, //126 01111110 -> 01111110
0xFE, //127 01111111 -> 11111110
0x01, //128 10000000 -> 00000001
0x81, //129 10000001 -> 10000001
0x41, //130 10000010 -> 01000001
0xC1, //131 10000011 -> 11000001
0x21, //132 10000100 -> 00100001
0xA1, //133 10000101 -> 10100001
0x61, //134 10000110 -> 01100001
0xE1, //135 10000111 -> 11100001
0x11, //136 10001000 -> 00010001
0x91, //137 10001001 -> 10010001
0x51, //138 10001010 -> 01010001
0xD1, //139 10001011 -> 11010001
0x31, //140 10001100 -> 00110001
0xB1, //141 10001101 -> 10110001
0x71, //142 10001110 -> 01110001
0xF1, //143 10001111 -> 11110001
0x09, //144 10010000 -> 00001001
0x89, //145 10010001 -> 10001001
0x49, //146 10010010 -> 01001001
0xC9, //147 10010011 -> 11001001
0x29, //148 10010100 -> 00101001
0xA9, //149 10010101 -> 10101001
0x69, //150 10010110 -> 01101001
0xE9, //151 10010111 -> 11101001
0x19, //152 10011000 -> 00011001
0x99, //153 10011001 -> 10011001
0x59, //154 10011010 -> 01011001
0xD9, //155 10011011 -> 11011001
0x39, //156 10011100 -> 00111001
0xB9, //157 10011101 -> 10111001
0x79, //158 10011110 -> 01111001
0xF9, //159 10011111 -> 11111001
0x05, //160 10100000 -> 00000101
0x85, //161 10100001 -> 10000101
0x45, //162 10100010 -> 01000101
0xC5, //163 10100011 -> 11000101
0x25, //164 10100100 -> 00100101
0xA5, //165 10100101 -> 10100101
0x65, //166 10100110 -> 01100101
0xE5, //167 10100111 -> 11100101
0x15, //168 10101000 -> 00010101
0x95, //169 10101001 -> 10010101
0x55, //170 10101010 -> 01010101
0xD5, //171 10101011 -> 11010101
0x35, //172 10101100 -> 00110101
0xB5, //173 10101101 -> 10110101
0x75, //174 10101110 -> 01110101
0xF5, //175 10101111 -> 11110101
0x0D, //176 10110000 -> 00001101
0x8D, //177 10110001 -> 10001101
0x4D, //178 10110010 -> 01001101
0xCD, //179 10110011 -> 11001101
0x2D, //180 10110100 -> 00101101
0xAD, //181 10110101 -> 10101101
0x6D, //182 10110110 -> 01101101
0xED, //183 10110111 -> 11101101
0x1D, //184 10111000 -> 00011101
0x9D, //185 10111001 -> 10011101
0x5D, //186 10111010 -> 01011101
0xDD, //187 10111011 -> 11011101
0x3D, //188 10111100 -> 00111101
0xBD, //189 10111101 -> 10111101
0x7D, //190 10111110 -> 01111101
0xFD, //191 10111111 -> 11111101
0x03, //192 11000000 -> 00000011
0x83, //193 11000001 -> 10000011
0x43, //194 11000010 -> 01000011
0xC3, //195 11000011 -> 11000011
0x23, //196 11000100 -> 00100011
0xA3, //197 11000101 -> 10100011
0x63, //198 11000110 -> 01100011
0xE3, //199 11000111 -> 11100011
0x13, //200 11001000 -> 00010011
0x93, //201 11001001 -> 10010011
0x53, //202 11001010 -> 01010011
0xD3, //203 11001011 -> 11010011
0x33, //204 11001100 -> 00110011
0xB3, //205 11001101 -> 10110011
0x73, //206 11001110 -> 01110011
0xF3, //207 11001111 -> 11110011
0x0B, //208 11010000 -> 00001011
0x8B, //209 11010001 -> 10001011
0x4B, //210 11010010 -> 01001011
0xCB, //211 11010011 -> 11001011
0x2B, //212 11010100 -> 00101011
0xAB, //213 11010101 -> 10101011
0x6B, //214 11010110 -> 01101011
0xEB, //215 11010111 -> 11101011
0x1B, //216 11011000 -> 00011011
0x9B, //217 11011001 -> 10011011
0x5B, //218 11011010 -> 01011011
0xDB, //219 11011011 -> 11011011
0x3B, //220 11011100 -> 00111011
0xBB, //221 11011101 -> 10111011
0x7B, //222 11011110 -> 01111011
0xFB, //223 11011111 -> 11111011
0x07, //224 11100000 -> 00000111
0x87, //225 11100001 -> 10000111
0x47, //226 11100010 -> 01000111
0xC7, //227 11100011 -> 11000111
0x27, //228 11100100 -> 00100111
0xA7, //229 11100101 -> 10100111
0x67, //230 11100110 -> 01100111
0xE7, //231 11100111 -> 11100111
0x17, //232 11101000 -> 00010111
0x97, //233 11101001 -> 10010111
0x57, //234 11101010 -> 01010111
0xD7, //235 11101011 -> 11010111
0x37, //236 11101100 -> 00110111
0xB7, //237 11101101 -> 10110111
0x77, //238 11101110 -> 01110111
0xF7, //239 11101111 -> 11110111
0x0F, //240 11110000 -> 00001111
0x8F, //241 11110001 -> 10001111
0x4F, //242 11110010 -> 01001111
0xCF, //243 11110011 -> 11001111
0x2F, //244 11110100 -> 00101111
0xAF, //245 11110101 -> 10101111
0x6F, //246 11110110 -> 01101111
0xEF, //247 11110111 -> 11101111
0x1F, //248 11111000 -> 00011111
0x9F, //249 11111001 -> 10011111
0x5F, //250 11111010 -> 01011111
0xDF, //251 11111011 -> 11011111
0x3F, //252 11111100 -> 00111111
0xBF, //253 11111101 -> 10111111
0x7F, //254 11111110 -> 01111111
0xFF, //255 11111111 -> 11111111
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,32 @@ public byte[] composeString(byte oldString[], byte newString) {
return string;
}

private int reverseBits(int inp)
public int reverseBits(int inp)
{
int iz = 0 ;
int po2 = 1;
int rev = 0x80;
for( int i = 0 ; i < 8 ; i++) {
if( (inp & po2 ) != 0 ) {
iz += rev;
return TIFFFillOrder.reverseTable[inp] ;
}

// This code to build the fillOrder table
public static void generateBitsreverseBits()
{
for( int iOuter = 0 ; iOuter < 256 ; iOuter ++ ) {
int iz = 0 ;
int po2 = 1;
int rev = 0x80;
for( int i = 0 ; i < 8 ; i++) {
if( (iOuter & po2 ) != 0 ) {
iz += rev;
}
po2 <<= 1 ;
rev >>= 1;
}
po2 <<= 1 ;
rev >>= 1;

}
return iz;
byte b = (byte)iz ;
String value = String.format("0x%02X,", b & 0x00FF);
System.out.println(value+" //"+ iOuter+" "
+String.format("%8s", Integer.toBinaryString(iOuter)).replace(" ", "0")
+" -> "
+String.format("%8s", Integer.toBinaryString(iz)).replace(" ", "0"));
}
}

// Returns the next 9, 10, 11 or 12 bits
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/github/jaiimageio/TiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import javax.imageio.stream.ImageOutputStream;

import com.github.jaiimageio.impl.plugins.tiff.TIFFImageWriter;
import com.github.jaiimageio.impl.plugins.tiff.TIFFLZWDecompressor;
import com.github.jaiimageio.impl.plugins.tiff.TIFFT6Compressor;
import com.github.jaiimageio.plugins.tiff.BaselineTIFFTagSet;
import com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam;

import org.junit.Test;
Expand All @@ -35,6 +37,37 @@ public void testLSB() throws Exception {
//--
URL file = getClass().getResource("/lsbtifflzw/export_gimp.tiff");
BufferedImage bufferedImage = ImageIO.read(file);
File fx = File.createTempFile("imageio-test", "." + "tiff");
bufferedImage = ImageIO.read(file);
ImageIO.write(bufferedImage, "tiff", fx);
System.out.println(fx);

}

private int reverseBits(int inp)
{
int iz = 0 ;
int po2 = 1;
int rev = 0x80;
for( int i = 0 ; i < 8 ; i++) {
if( (inp & po2 ) != 0 ) {
iz += rev;
}
po2 <<= 1 ;
rev >>= 1;

}
return iz;
}

@Test
public void testFillOrderReverseTable() throws Exception {
TIFFLZWDecompressor compressor = new TIFFLZWDecompressor(BaselineTIFFTagSet.PREDICTOR_NONE);
for( int i = 0 ; i < 256 ; i ++ ) {
int reversed = reverseBits(i);
int calculated = compressor.reverseBits(i);
assertEquals("Index:"+i, reversed, calculated);
}
}

private void checkAndCompare(final String fileName) throws Exception {
Expand Down

0 comments on commit a36bf00

Please sign in to comment.