Skip to content

Commit

Permalink
Merge pull request #1 from kamil-sita/compression
Browse files Browse the repository at this point in the history
Compression
  • Loading branch information
kamil-sita authored Feb 18, 2019
2 parents 7ad1b1a + 4bdbbc9 commit 2b61b08
Show file tree
Hide file tree
Showing 24 changed files with 1,414 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/main/java/sections/Interruptible.java
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);
}
}
31 changes: 31 additions & 0 deletions src/main/java/sections/MockInterruptible.java
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) {
}

}
16 changes: 16 additions & 0 deletions src/main/java/sections/UserFeedback.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextInputDialog;
import sections.main.MainViewController;
import toolset.io.BufferedImageIO;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Optional;


Expand Down Expand Up @@ -34,4 +38,16 @@ public static Optional<String> getText(String title, String header, String conte
textInputDialog.setContentText(context);
return textInputDialog.showAndWait();
}

public static void openInDefault(BufferedImage bufferedImage) {
var file = BufferedImageIO.saveImage(bufferedImage, null);
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
e.printStackTrace();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class AutomatedFilterController {

@FXML
void filterAndSavePress(ActionEvent event) {
var optionalSavePdf = GuiFileIO.getSaveDirectory();
var optionalSavePdf = GuiFileIO.getSaveDirectory("*.pdf");
if (!optionalSavePdf.isPresent()) {
UserFeedback.popup("Wrong save file!");
return;
Expand Down
137 changes: 137 additions & 0 deletions src/main/java/sections/compression/BitSequence.java
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 src/main/java/sections/compression/CompressedAndPreview.java
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;
}
}
Loading

0 comments on commit 2b61b08

Please sign in to comment.