-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kamil-sita/compression
Compression
- Loading branch information
Showing
24 changed files
with
1,414 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
package sections; | ||
|
||
/** | ||
* | ||
* Abstract class for job interruption and JavaFX progress feedback. | ||
*/ | ||
public abstract class Interruptible { | ||
protected boolean isInterrupted = false; | ||
|
||
public abstract Runnable getRunnable(); | ||
public abstract Runnable onUninterruptedFinish(); | ||
|
||
public final boolean isInterrupted() { | ||
public boolean isInterrupted() { | ||
return isInterrupted; | ||
} | ||
|
||
public final void interrupt() { | ||
public void interrupt() { | ||
isInterrupted = true; | ||
} | ||
|
||
public void reportProgress(double percentProgress) { | ||
UserFeedback.reportProgress(percentProgress); | ||
} | ||
|
||
public void reportProgress (String message) { | ||
UserFeedback.reportProgress(message); | ||
} | ||
|
||
public void popup(String message) { | ||
UserFeedback.reportProgress(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sections; | ||
|
||
public final class MockInterruptible extends Interruptible { | ||
@Override | ||
public Runnable getRunnable() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Runnable onUninterruptedFinish() { | ||
return null; | ||
} | ||
|
||
public boolean isInterrupted() { | ||
return false; | ||
} | ||
|
||
public void interrupt() { | ||
|
||
} | ||
|
||
public void reportProgress(double percentProgress) { | ||
} | ||
|
||
public void reportProgress (String message) { | ||
} | ||
|
||
public void popup(String message) { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package sections.compression; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class BitSequence { | ||
|
||
private final int BYTE_SIZE = 8; //byte size in bits | ||
|
||
private ArrayList<Boolean> seq = new ArrayList<>(); | ||
int pointer = 0; | ||
|
||
public BitSequence() { | ||
|
||
} | ||
|
||
public BitSequence(byte[] data) { | ||
for (var bytef : data) { | ||
var bools = toBoolArray(bytef, BYTE_SIZE); | ||
for (var bool : bools) { | ||
seq.add(bool); | ||
} | ||
} | ||
} | ||
|
||
public void put(long value, int length) { | ||
boolean[] arr = toBoolArray(value, length); | ||
for (boolean b : arr) { | ||
seq.add(b); | ||
} | ||
} | ||
|
||
public void putOne(int value) { | ||
put(value, 1); | ||
} | ||
|
||
public boolean has(int size) { | ||
return seq.size() - pointer >= size; | ||
} | ||
|
||
public void addAll(BitSequence bitSequence) { | ||
this.seq.addAll(bitSequence.seq); | ||
} | ||
|
||
public void resetPointer() { | ||
pointer = 0; | ||
} | ||
|
||
public void consume(int size) { | ||
pointer += size; | ||
if (pointer > getSizeIgnoreConsumed()) throw new IllegalArgumentException("Consumed more than is available"); | ||
} | ||
|
||
public boolean getAt(int index) { | ||
return seq.get(index + pointer); | ||
} | ||
|
||
public int getNext(int count) { | ||
int out = 0; | ||
for (int i = 0; i < count; i++) { | ||
if (seq.get(i + pointer)) { | ||
int value = 1 << (count - i - 1); | ||
out = (value | out); | ||
} | ||
} | ||
return out; | ||
} | ||
|
||
public int getAndConsume(int count) { | ||
if (!has(count)) consume(count); //throwing error | ||
int v = getNext(count); | ||
consume(count); | ||
return v; | ||
} | ||
|
||
public ArrayList<Boolean> getSeq() { | ||
return seq; | ||
} | ||
|
||
public byte[] getSeqArray() { | ||
fitByteSize(); | ||
byte[] data = new byte[getSizeIgnoreConsumed() / BYTE_SIZE]; | ||
for (int i = 0; i < data.length; i += 1) { | ||
data[i] = fitBitsIntoByte(i); | ||
} | ||
return data; | ||
} | ||
|
||
public int getSize() { | ||
return seq.size() - pointer; | ||
} | ||
|
||
public int getSizeIgnoreConsumed() { | ||
return seq.size(); | ||
} | ||
|
||
public int getConsumedSize() { | ||
return pointer; | ||
} | ||
|
||
|
||
private boolean[] toBoolArray(long input, int length) { | ||
boolean[] arr = new boolean[length]; | ||
int i = 0; | ||
for (int j = length - 1; j >= 0; j--) { | ||
int pow = 1 << i; | ||
arr[j] = (input & pow) > 0; | ||
i++; | ||
} | ||
return arr; | ||
} | ||
|
||
private void fitByteSize() { | ||
while (getSize() % BYTE_SIZE != 0) { | ||
put(0, 1); | ||
} | ||
} | ||
|
||
private byte fitBitsIntoByte(int octaId) { | ||
byte out = 0; | ||
int j = 7; | ||
for (int i = octaId * 8; i < (octaId + 1) * 8; i++) { | ||
if (seq.get(i)) { | ||
int value = 1 << j; | ||
out = (byte) (value | out); | ||
} | ||
j--; | ||
} | ||
return out; | ||
} | ||
|
||
public void write() { | ||
for (int i = 0; i < seq.size(); i++) { | ||
System.out.print(seq.get(i) ? "1" : "0"); | ||
} | ||
System.out.println(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/sections/compression/CompressedAndPreview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sections.compression; | ||
|
||
import java.awt.image.BufferedImage; | ||
|
||
public class CompressedAndPreview { | ||
private BitSequence bitSequence; | ||
private BufferedImage bufferedImage; | ||
|
||
public CompressedAndPreview(BitSequence bitSequence, BufferedImage bufferedImage) { | ||
this.bitSequence = bitSequence; | ||
this.bufferedImage = bufferedImage; | ||
} | ||
|
||
public BitSequence getBitSequence() { | ||
return bitSequence; | ||
} | ||
|
||
public BufferedImage getBufferedImage() { | ||
return bufferedImage; | ||
} | ||
} |
Oops, something went wrong.