Skip to content

Commit

Permalink
Merge pull request #5 from CherryPill/A-a-c_#2
Browse files Browse the repository at this point in the history
A a c #2
  • Loading branch information
CherryPill authored Feb 1, 2020
2 parents 99a661f + e0612fd commit 56201e5
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 105 deletions.
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,32 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.2.4</version>
<scope>test</scope>
</dependency>


</dependencies>
</project>
12 changes: 9 additions & 3 deletions src/main/java/application/Main.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package application;

import application.utility.Utility;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main extends Application {

private static final Logger logger = LogManager.getLogger(Main.class);

@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("layout/layout.fxml"));
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().add(getClass().getResource("/style/application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("ASCII Art creator");
primaryStage.setTitle(Utility.getProps().getProperty("ui.sys.app.name"));
primaryStage.show();

logger.info("Main app started. UI started.");
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
102 changes: 76 additions & 26 deletions src/main/java/application/converter/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import application.encoder.GifEncoder;
import application.utility.GifUtility;
import application.utility.Utility;
import javafx.concurrent.Task;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
Expand All @@ -16,19 +18,45 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Converter<V> extends Task<V> {

public Converter(List<File> inputFiles,
File outputDir,
Converter.UI_OUTFILE_CONVERSION_TYPE conversionType,
int blockSize,
Color background,
Color foreground, Stage progressWindow) {
this.outputDir = outputDir;
this.inputFiles = inputFiles;
this.blockSize = blockSize;
this.background = background;
this.foreground = foreground;
this.conversion = conversionType;
}

@Override
protected V call() throws Exception {
convertAllImages();
return null;
}

public enum CONV_TYPE {TXT, IMG_OTHER, IMG_GIF}

public class Converter {
;

public enum CONV_TYPE {TXT, IMG_OTHER, IMG_GIF};
public enum UI_OUTFILE_CONVERSION_TYPE {TEXT, IMG}

public enum UI_OUTFILE_CONVERSION_TYPE {TEXT, IMG};
;

private final float scaleFactor = 1.5F;

private static final String asciiChars = Utility.getProps().getProperty("logic.core.chars");

private final CONV_TYPE convType;
private CONV_TYPE type = null;

private static int gifDelayTime;

Expand All @@ -38,40 +66,57 @@ public enum UI_OUTFILE_CONVERSION_TYPE {TEXT, IMG};

private int blockCountForHeight;

private String outFile;

private File inputFile;
private File outputDir;

private Color background;

private Color foreground;

public void setOutFile(String outFile) {
this.outFile = outFile;
}
private int filesProcessed;

private int filesToProcessTotal;

private int blocksProcessed;

public Converter(File input,
int b,
Converter.CONV_TYPE convType,
Color back,
Color fore) {
this.inputFile = input;
this.blockSize = b;
this.convType = convType;
this.background = back;
this.foreground = fore;
private int blocksToProcessTotal;

private List<File> inputFiles;

Converter.UI_OUTFILE_CONVERSION_TYPE conversion;

private void convertAllImages() {
String outFileName;

filesProcessed = 1;
filesToProcessTotal = inputFiles.size();
for (File i : inputFiles) {
if (conversion == Converter.UI_OUTFILE_CONVERSION_TYPE.IMG) {
outFileName = Utility.omitExtension(i.getName()) + "_" + UUID.randomUUID() + "." + Utility.inferExtension(i.getName());
if (Utility.inferExtension(i.getName()).equals("gif")) {
this.type = Converter.CONV_TYPE.IMG_GIF;
} else {
type = Converter.CONV_TYPE.IMG_OTHER;
}
} else {
outFileName = Utility.omitExtension(i.getName()) + "_" + UUID.randomUUID() + ".txt";
type = Converter.CONV_TYPE.TXT;
}
convertSingleImage(i, outFileName);
this.filesProcessed++;
}
}

public void convert(File output) {
String format = Utility.inferExtension(this.inputFile.getName());
File modifiedOutPath = new File(output.getAbsolutePath() + "\\" + outFile);
//convert -> convertSingleImage
public void convertSingleImage(File inputFile, String outFileName) {
String format = Utility.inferExtension(inputFile.getName());
File modifiedOutPath = new File(outputDir.getAbsolutePath() + "\\" + outFileName);
GifEncoder ge = null;
ImageInputStream stream = null;
ImageOutputStream outStream = null;
try {
int charSize = 20;
ImageReader reader = ImageIO.getImageReadersByFormatName(format).next();
stream = ImageIO.createImageInputStream(this.inputFile);
stream = ImageIO.createImageInputStream(inputFile);
outStream = new FileImageOutputStream(modifiedOutPath);
reader.setInput(stream);

Expand All @@ -89,6 +134,9 @@ public void convert(File output) {
System.out.println(blockCountForWidth + " " + blockCountForHeight);
//get pixels of this block
//processing each block and computing grayscale
blocksToProcessTotal = blockCountForHeight * blockCountForWidth;
blocksProcessed = 0;

for (int x = 1; x <= blockCountForWidth; x++) {
for (int y = 1; y <= blockCountForHeight; y++) {
int endPointX = x * blockSize;
Expand All @@ -105,10 +153,12 @@ public void convert(File output) {
long avgGreyScaleForBlock = getAvgGS(startingPointX, startingPointY, endPointX, endPointY, frame);

charMatrix[y - 1][x - 1] = getCorrespondingChar((int) avgGreyScaleForBlock);
updateProgress(++this.blocksProcessed, this.blocksToProcessTotal);
updateMessage(inputFile.getName()+" ["+this.filesProcessed+" of "+this.filesToProcessTotal+"]");
}
}

if (this.convType == Converter.CONV_TYPE.TXT) {
if (type == Converter.CONV_TYPE.TXT) {
FileWriter fw = new FileWriter(modifiedOutPath);
BufferedWriter bw = new BufferedWriter(fw);
for (int y = 0; y < blockCountForHeight; y++) {
Expand Down Expand Up @@ -164,7 +214,7 @@ public void convert(File output) {

outImage = outImage.getSubimage(0, 0, g.getFontMetrics().stringWidth(lastLine), newImageHeight);

if (this.convType == Converter.CONV_TYPE.IMG_GIF) {
if (type == Converter.CONV_TYPE.IMG_GIF) {
if (ge == null) {
gifDelayTime = GifUtility.getGifDelay(reader);
ge = new GifEncoder(outStream, outImage.getType(), gifDelayTime);
Expand Down
66 changes: 41 additions & 25 deletions src/main/java/application/io/ConversionService.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
package application.io;

import application.converter.Converter;
import application.ui.WindowFactory;
import application.utility.MessageWrapper;
import application.utility.Utility;
import javafx.beans.binding.Bindings;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class ConversionService {

private WindowFactory windowFactory = new WindowFactory();

public void convertFiles(List<File> inputFiles,
File o,
File outputDir,
int blocks,
Converter.UI_OUTFILE_CONVERSION_TYPE conversion,
Converter.UI_OUTFILE_CONVERSION_TYPE conversionType,
Color b,
Color f) throws IOException {
String outFileName;
Converter.CONV_TYPE type = null;
for (File i : inputFiles) {
if (conversion == Converter.UI_OUTFILE_CONVERSION_TYPE.IMG) {
outFileName = Utility.omitExtension(i.getName()) + "_" + UUID.randomUUID() + "." + Utility.inferExtension(i.getName());
if (Utility.inferExtension(i.getName()).equals("gif")) {
type = Converter.CONV_TYPE.IMG_GIF;
} else {
type = Converter.CONV_TYPE.IMG_OTHER;
}
} else {
outFileName = Utility.omitExtension(i.getName()) + "_" + UUID.randomUUID() + ".txt";
type = Converter.CONV_TYPE.TXT;
}
Converter c = new Converter(i,
blocks,
type,
Utility.getAwtColorFromFXColor(b),
Utility.getAwtColorFromFXColor(f));
c.setOutFile(outFileName);
c.convert(o);
}
Stage progressBarWindow = windowFactory.createWindowFromFXML(
Modality.APPLICATION_MODAL);
progressBarWindow.show();
Converter conversionTask = new Converter(
inputFiles,
outputDir,
conversionType,
blocks,
Utility.getAwtColorFromFXColor(b),
Utility.getAwtColorFromFXColor(f),
progressBarWindow
);

ProgressBar pbNode = (ProgressBar) progressBarWindow.getScene().lookup("#perFrameProgressBar");
pbNode.progressProperty().bind(conversionTask.progressProperty());
Label individualProgressBarLabel = (Label) progressBarWindow.getScene().lookup("#individualProgressLabel");
individualProgressBarLabel.textProperty().bind(
Bindings.createStringBinding(
() -> conversionTask.getMessage(),
conversionTask.messageProperty()
)
);
conversionTask.setOnSucceeded(event -> {
progressBarWindow.close();
MessageWrapper.showMessage("Finished converting",
Alert.AlertType.INFORMATION);
});
new Thread(conversionTask).start();
}
}
24 changes: 21 additions & 3 deletions src/main/java/application/mvc/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public class MainController {

@FXML
public void initialize() {

comboBlocksCount.setItems(blocksCountComboValues);
comboConversionType.setItems(conversionTypeComboValues);
comboBlocksCount.getSelectionModel().select(0);
comboConversionType.getSelectionModel().select(0);
comboConversionType.getSelectionModel().select(1);
toggleImageOptions();
conversionService = new ConversionService();
}
Expand Down Expand Up @@ -108,6 +109,11 @@ private boolean validateFields(Converter.UI_OUTFILE_CONVERSION_TYPE selectedConv
MessageWrapper.showMessage(Utility.getProps().getProperty("ui.msg.err.no_color_chosen"), AlertType.ERROR);
return false;
}
} else if (selectedConversionType == Converter.UI_OUTFILE_CONVERSION_TYPE.TEXT) {
if (Utility.imageListContainsGif(chosenFiles)) {
MessageWrapper.showMessage(Utility.getProps().getProperty("ui.msg.err.gif_to_text"), AlertType.WARNING);
return false;
}
}
return true;
} else {
Expand All @@ -125,14 +131,26 @@ public void specifyInput() {
FileChooser fc = new FileChooser();
fc.setTitle(Utility.getProps().getProperty("ui.lbl.choose_img"));
fc.setInitialDirectory(new File(System.getProperty("user.home")));
fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JPG", "*.jpg"),
fc.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("All images", "*.jpg",
"*.png", "*.gif"),
new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png"),
new FileChooser.ExtensionFilter("GIF", "*.gif"));
List<File> chosenFiles = fc.showOpenMultipleDialog(null);
if (chosenFiles != null) {
labelInputFile.setText(Utility.concatListStrings(chosenFiles));
labelInputFile.setText(Utility.createFileListString(chosenFiles));
this.chosenFiles = chosenFiles;
}
if(Utility.imageListContainsGif(chosenFiles)){
comboConversionType.getSelectionModel().select(1);
comboConversionType.setDisable(true);
MessageWrapper.showMessage(Utility.getProps().getProperty("ui.msg.err.gif_to_text"), AlertType.WARNING);
}
else{
comboConversionType.getSelectionModel().select(0);
comboConversionType.setDisable(false);
}
}

@FXML
Expand Down
Loading

0 comments on commit 56201e5

Please sign in to comment.