Skip to content

Commit

Permalink
higher quality for previews
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-sita committed Nov 25, 2018
1 parent ee392c8 commit 0621f7f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void filterAndSavePress(ActionEvent event) {

new Thread(() -> {
Platform.runLater(() -> UserFeedback.popup("Poup will show up once PDF is filtered"));
PdfFilter.filter(openPdf, savePdf, false, scaleBrightness.isSelected(), brightnessSlider.getValue()/100.0, finalBlurPasses);
PdfFilter.filter(openPdf, savePdf, scaleBrightness.isSelected(), brightnessSlider.getValue()/100.0, finalBlurPasses);
Platform.runLater(() -> UserFeedback.popup("Finished filtering pdf"));
}).start();
}
Expand Down Expand Up @@ -123,7 +123,7 @@ void previewPress(ActionEvent event) {
lastImage = inputImage[0];
}
UserFeedback.reportProgress("Filtering image...");
var output = HighPassFilterConverter.convert(inputImage[0], finalBlurPasses, scaleBrightness.isSelected(), brightnessSlider.getValue()/100.0, false);
var output = HighPassFilterConverter.convert(inputImage[0], finalBlurPasses, scaleBrightness.isSelected(), brightnessSlider.getValue()/100.0);

if (higherQualityPreview.isSelected()) {
semaphore = new Semaphore(0);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/sections/automatedfilter/PdfFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import java.io.File;

public class PdfFilter {
public static void filter(File inputFile, File outputFile, boolean highQuality, boolean scaleBrightness, double scaleBrightnessVal, int blurPasses) {
public static void filter(File inputFile, File outputFile, boolean scaleBrightness, double scaleBrightnessVal, int blurPasses) {
int size = PdfIO.getNumberOfPages(inputFile);
var document = PdfIO.createDocument();
for (int i = 0; i < size; i++) {
UserFeedback.reportProgress((1.0*i)/size);
UserFeedback.reportProgress("Progress: page " + (i + 1) + "/" + size + ".");
var image = PdfIO.getPdfAsImage(inputFile, i);
var filteredImage = HighPassFilterConverter.convert(image, blurPasses, scaleBrightness, scaleBrightnessVal, highQuality);
var filteredImage = HighPassFilterConverter.convert(image, blurPasses, scaleBrightness, scaleBrightnessVal);
PdfIO.addImage(document, filteredImage);
}
PdfIO.saveDocumentAndClose(outputFile, document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import sections.UserFeedback;
import sections.main.MainViewController;
import toolset.imagetools.BufferedImageConvert;
import toolset.imagetools.BufferedImageScale;
import toolset.io.GuiFileIO;

import java.awt.image.BufferedImage;
import java.util.concurrent.Semaphore;

public final class HighPassFilterController {

Expand All @@ -31,7 +33,7 @@ public final class HighPassFilterController {
private CheckBox scaleBrightness;

@FXML
private CheckBox higherQuality;
private CheckBox higherQualityPreview;


@FXML
Expand Down Expand Up @@ -82,18 +84,41 @@ void runButton(ActionEvent event) {
//failed parsing
}
UserFeedback.reportProgress("Converting...");
var output = HighPassFilterConverter.convert(inputImage, blurValue, scaleBrightness.isSelected(), brightnessSlider.getValue()/100.0, higherQuality.isSelected());
var output = HighPassFilterConverter.convert(inputImage, blurValue, scaleBrightness.isSelected(), brightnessSlider.getValue()/100.0);
processedImage = output;
UserFeedback.reportProgress("Converted image!");
Platform.runLater(() -> setNewImage(output));

if (higherQualityPreview.isSelected()) {
semaphore = new Semaphore(0);
Platform.runLater(() -> setNewImage(output));
UserFeedback.reportProgress("Generated preview. Scaling to fit window... Waiting for permit.");
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
UserFeedback.reportProgress("Generated preview. Scaling to fit window...");
double width = imagePreview.getFitWidth();
double height = imagePreview.getFitHeight();
var scaledOutput = BufferedImageScale.getScaledImage(output, width, height);
Platform.runLater(() -> setNewImage(scaledOutput));
UserFeedback.reportProgress("Generated scaled preview.");
} else {
Platform.runLater(() -> setNewImage(output));
UserFeedback.reportProgress("Generated preview.");
}
}).start();

}

Semaphore semaphore;

private void setNewImage(BufferedImage bufferedImage) {
imagePreview.setImage(BufferedImageConvert.toFxImage(bufferedImage));
reAddNotifier();
MainViewController.forceOnWindowSizeChange();
if (semaphore != null) semaphore.release();
}

private void reAddNotifier() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package sections.highpassfilter;

import toolset.imagetools.*;
import toolset.imagetools.BufferedImageBlur;
import toolset.imagetools.BufferedImageColorPalette;
import toolset.imagetools.BufferedImageLayers;
import toolset.imagetools.BufferedImageVarious;

import java.awt.image.BufferedImage;
import java.awt.image.Kernel;

public final class HighPassFilterConverter {

public static BufferedImage convert(BufferedImage bufferedImage, int blurPasses, boolean scaleBrightness, double scaleBrightnessVal, boolean higherQuality) {

if (higherQuality) blurPasses *= 2;

if (higherQuality) bufferedImage = BufferedImageScale.getScaledImage(bufferedImage, 2);
public static BufferedImage convert(BufferedImage bufferedImage, int blurPasses, boolean scaleBrightness, double scaleBrightnessVal) {

var blurredImage = BufferedImageVarious.copyImage(bufferedImage);

Expand All @@ -23,7 +22,6 @@ public static BufferedImage convert(BufferedImage bufferedImage, int blurPasses,
BufferedImageVarious.copyImage(bufferedImage), blurredImage);

if (scaleBrightness) BufferedImageColorPalette.scaleAndCutoffBrightness(output, scaleBrightnessVal);
if (higherQuality) output = BufferedImageScale.getScaledImage(output, 0.5);

return output;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sections/highpassfilter/highPassFilter.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ImageView fx:id="imagePreview" fitHeight="510.0" fitWidth="475.0" layoutX="7.0" layoutY="136.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="7.0" AnchorPane.rightAnchor="718.0" AnchorPane.topAnchor="80.0" />
<Separator layoutX="12.0" layoutY="73.0" prefWidth="200.0" AnchorPane.leftAnchor="12.0" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="73.0" />
<CheckBox fx:id="scaleBrightness" layoutX="173.0" layoutY="11.0" mnemonicParsing="false" selected="true" text="correct brightness" />
<CheckBox fx:id="higherQuality" layoutX="198.0" layoutY="44.0" mnemonicParsing="false" text="higher quality" />
<CheckBox fx:id="higherQualityPreview" layoutX="198.0" layoutY="44.0" mnemonicParsing="false" selected="true" text="higher quality preview" />
<Slider fx:id="brightnessSlider" layoutX="298.0" layoutY="1.0" majorTickUnit="10.0" minorTickCount="1" prefHeight="24.0" prefWidth="241.0" showTickLabels="true" showTickMarks="true" value="70.0" />
</children>
</AnchorPane>
22 changes: 0 additions & 22 deletions src/main/java/toolset/imagetools/BufferedImageScale.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@

public final class BufferedImageScale {

/**
* Scales down image
*
* @param source input BufferedImage
* @param SIZE size of scaled down image
* @return scaled down image
*/

public static BufferedImage getComparableScaledDownImage(BufferedImage source, final int SIZE) {
if (source == null) return null;
Expand All @@ -35,21 +28,6 @@ public static BufferedImage getComparableScaledDownImage(BufferedImage source, f
return newImage;
}

public static BufferedImage getScaledImage(BufferedImage source, double scale) {
if (source == null) return null;
BufferedImage newImage = new BufferedImage((int) (source.getWidth() * scale), (int) (source.getHeight() * scale), BufferedImage.TYPE_INT_ARGB);

Graphics2D graphics2D = (Graphics2D) newImage.getGraphics();

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scale, scale);
graphics2D.drawImage(source, affineTransform, null);
graphics2D.dispose();

return newImage;
}

public static BufferedImage getScaledImage(BufferedImage source, double width, double height) {
if (source == null) return null;
Expand Down

0 comments on commit 0621f7f

Please sign in to comment.