srtm1 progress

This commit is contained in:
Arndt 2016-11-20 22:28:16 +01:00
parent 95209148b4
commit 561b60c5cb
9 changed files with 289 additions and 166 deletions

View file

@ -12,46 +12,74 @@ import java.io.InputStream;
public final class MixCoderDataInputStream extends DataInputStream
{
private long lastValue;
private long repCount;
private int lastValue;
private int repCount;
private int diffshift;
private int bm = 0x100;
private int b;
public MixCoderDataInputStream( InputStream is )
{
super( is );
}
public long readSigned() throws IOException
{
long v = readUnsigned();
return ( v & 1 ) == 0 ? v >> 1 : -(v >> 1 );
}
public long readUnsigned() throws IOException
{
long v = 0;
int shift = 0;
for(;;)
{
long i7 = readByte() & 0xff;
v |= (( i7 & 0x7f ) << shift);
if ( ( i7 & 0x80 ) == 0 ) break;
shift += 7;
}
return v;
}
public long readMixed() throws IOException
public int readMixed() throws IOException
{
if ( repCount == 0 )
{
long b = readByte() & 0xff;
long repCode = b >> 6;
long diffcode = b & 0x3f;
repCount = repCode == 0 ? readUnsigned() : repCode;
lastValue += diffcode == 0 ? readSigned() : diffcode - 32;
boolean negative = decodeBit();
int d = decodeVarBits() + diffshift;
repCount = decodeVarBits() + 1;
lastValue += negative ? -d : d;
diffshift = 1;
}
repCount--;
return lastValue;
}
public final boolean decodeBit() throws IOException
{
if ( bm == 0x100 )
{
bm = 1;
b = readByte();
}
boolean value = ( ( b & bm ) != 0 );
bm <<= 1;
return value;
}
public final int decodeVarBits() throws IOException
{
int range = 0;
int value = 0;
while (!decodeBit())
{
value += range + 1;
range = 2 * range + 1;
}
return value + decodeBounded( range );
}
public final int decodeBounded( int max ) throws IOException
{
int value = 0;
int im = 1; // integer mask
while (( value | im ) <= max)
{
if ( bm == 0x100 )
{
bm = 1;
b = readByte();
}
if ( ( b & bm ) != 0 )
value |= im;
bm <<= 1;
im <<= 1;
}
return value;
}
}

View file

@ -12,54 +12,41 @@ import java.io.OutputStream;
public final class MixCoderDataOutputStream extends DataOutputStream
{
private long lastValue;
private long lastLastValue;
private long repCount;
private boolean doFlush;
private int lastValue;
private int lastLastValue;
private int repCount;
private int diffshift;
private int bm = 1; // byte mask (write mode)
private int b = 0;
public static int[] diffs = new int[100];
public static int[] counts = new int[100];
public MixCoderDataOutputStream( OutputStream os )
{
super( os );
}
public void writeSigned( long v ) throws IOException
{
writeUnsigned( v < 0 ? ( (-v) << 1 ) | 1 : v << 1 );
}
public void writeUnsigned( long v ) throws IOException
{
if ( v < 0 ) throw new IllegalArgumentException( "writeUnsigned: " + v );
do
{
long i7 = v & 0x7f;
v >>= 7;
if ( v != 0 ) i7 |= 0x80;
writeByte( (byte)( i7 & 0xff ) );
}
while( v != 0 );
}
public void writeMixed( long v ) throws IOException
public void writeMixed( int v ) throws IOException
{
if ( v != lastValue && repCount > 0 )
{
long d = lastValue - lastLastValue;
int d = lastValue - lastLastValue;
lastLastValue = lastValue;
// if diff fits within 6 bits and rep-count < 4, write a single byte
int repCode = repCount < 4 ? (int)repCount : 0;
int diffcode = (int)(d > -32 && d < 32 ? d+32 : 0);
writeByte( (byte)( diffcode | repCode << 6 ) );
if ( repCode == 0)
encodeBit( d < 0 );
if ( d < 0 )
{
writeUnsigned( repCount );
}
if ( diffcode == 0)
{
writeSigned( d );
d = -d;
}
encodeVarBits( d-diffshift );
encodeVarBits( repCount-1 );
if ( d < 100 ) diffs[d]++;
if ( repCount < 100 ) counts[repCount]++;
diffshift = 1;
repCount = 0;
}
lastValue = v;
@ -69,11 +56,68 @@ public final class MixCoderDataOutputStream extends DataOutputStream
@Override
public void flush() throws IOException
{
// todo: does this keep stream consistency after flush ?
long v = lastValue;
int v = lastValue;
writeMixed( v+1 );
lastValue = v;
repCount = 0;
if ( bm > 1 )
{
writeByte( (byte)b ); // flush bit-coding
}
}
public final void encodeBit( boolean value ) throws IOException
{
if ( bm == 0x100 )
{
writeByte( (byte)b );
bm = 1;
b = 0;
}
if ( value )
{
b |= bm;
}
bm <<= 1;
}
public final void encodeVarBits( int value ) throws IOException
{
int range = 0;
while (value > range)
{
encodeBit( false );
value -= range + 1;
range = 2 * range + 1;
}
encodeBit( true );
encodeBounded( range, value );
}
public final void encodeBounded( int max, int value ) throws IOException
{
int im = 1; // integer mask
while (im <= max)
{
if ( bm == 0x100 )
{
writeByte( (byte)b );
bm = 1;
b = 0;
}
if ( ( value & im ) != 0 )
{
b |= bm;
max -= im;
}
bm <<= 1;
im <<= 1;
}
}
public static void stats()
{
for(int i=1; i<100;i++) System.out.println( "diff[" + i + "] = " + diffs[i] );
for(int i=1; i<100;i++) System.out.println( "counts[" + i + "] = " + counts[i] );
}
}

View file

@ -24,6 +24,14 @@ public final class ReducedMedianFilter
{
if ( weight > 0. )
{
for( int i=0; i<nsamples; i++ )
{
if ( values[i] == value )
{
weights[i] += weight;
return;
}
}
weights[nsamples] = weight;
values[nsamples] = value;
nsamples++;

View file

@ -45,7 +45,7 @@ public class MixCoderTest
}
}
private void checkEncodeDecode( long v, MixCoderDataOutputStream mco, MixCoderDataInputStream mci ) throws IOException
private void checkEncodeDecode( int v, MixCoderDataOutputStream mco, MixCoderDataInputStream mci ) throws IOException
{
if ( mco != null )
{