Control over Image scaling algorithm
I am trying to rewrite a Swing image viewer in JavaFX, however the scaling algorithm used by the JavaFX Image constructor is of very low quality. Unfortunately I have to rely on resizing the image in the constructor, because some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing).
Here's a comparison. On the left, using BufferedImage.drawImage
to downscale a picture and rendering it with Graphics.drawImage
in paintComponent
(using antialias rendering hint) and on the right the same picture loaded at the same downscaled resolution through the JavaFX Image constructor (smooth is set to true).
Comparison
As you can see the JavaFX downscaling algorithm introduces a form of sawtooth effect. How could I remedy this? I imagine the problem comes from the fact that the image is not fully loaded in memory but it seems I have no other choice.
Here's a code that will generate the comparison. Using the following picture as an example.
public static BufferedImage scale(BufferedImage img, int width, int height) {
int imageType = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaled = new BufferedImage(width, height, imageType);
Graphics2D context = scaled.createGraphics();
context.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
context.drawImage(img, 0, 0, width, height, null);
context.dispose();
return scaled;
}
@Override
public void start(Stage stage) {
// Load full image in memory, downscale using BufferedImage
Image image1 = new Image("file:4000x3600.jpg");
BufferedImage bimg = SwingFXUtils.fromFXImage(image1, null);
bimg = scale(bimg, 600, 600);
image1 = SwingFXUtils.toFXImage(bimg, null);
// Downscale in the Image constructor, saves memory
Image image2 = new Image("file:4000x3600.jpg", 600, 600, false, true);
AnchorPane root = new AnchorPane();
ImageView view1 = new ImageView(image1);
view1.setLayoutX(0.0);
view1.setLayoutY(0.0);
ImageView view2 = new ImageView(image2);
view2.setLayoutX(600.0);
view2.setLayoutY(0.0);
root.getChildren().add(view1);
root.getChildren().add(view2);
Scene scene = new Scene(root, 1200, 600);
stage.setScene(scene);
stage.show();
}
java image javafx java-8
|
show 2 more comments
I am trying to rewrite a Swing image viewer in JavaFX, however the scaling algorithm used by the JavaFX Image constructor is of very low quality. Unfortunately I have to rely on resizing the image in the constructor, because some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing).
Here's a comparison. On the left, using BufferedImage.drawImage
to downscale a picture and rendering it with Graphics.drawImage
in paintComponent
(using antialias rendering hint) and on the right the same picture loaded at the same downscaled resolution through the JavaFX Image constructor (smooth is set to true).
Comparison
As you can see the JavaFX downscaling algorithm introduces a form of sawtooth effect. How could I remedy this? I imagine the problem comes from the fact that the image is not fully loaded in memory but it seems I have no other choice.
Here's a code that will generate the comparison. Using the following picture as an example.
public static BufferedImage scale(BufferedImage img, int width, int height) {
int imageType = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaled = new BufferedImage(width, height, imageType);
Graphics2D context = scaled.createGraphics();
context.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
context.drawImage(img, 0, 0, width, height, null);
context.dispose();
return scaled;
}
@Override
public void start(Stage stage) {
// Load full image in memory, downscale using BufferedImage
Image image1 = new Image("file:4000x3600.jpg");
BufferedImage bimg = SwingFXUtils.fromFXImage(image1, null);
bimg = scale(bimg, 600, 600);
image1 = SwingFXUtils.toFXImage(bimg, null);
// Downscale in the Image constructor, saves memory
Image image2 = new Image("file:4000x3600.jpg", 600, 600, false, true);
AnchorPane root = new AnchorPane();
ImageView view1 = new ImageView(image1);
view1.setLayoutX(0.0);
view1.setLayoutY(0.0);
ImageView view2 = new ImageView(image2);
view2.setLayoutX(600.0);
view2.setLayoutY(0.0);
root.getChildren().add(view1);
root.getChildren().add(view2);
Scene scene = new Scene(root, 1200, 600);
stage.setScene(scene);
stage.show();
}
java image javafx java-8
2
Can you create a Minimal, Complete, and Verifiable example showing the two approaches?
– Slaw
Jan 2 at 23:27
Question edited with example code and comparison.
– Xunkar
Jan 3 at 11:36
"some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing)" Can you provide these pictures?
– user1803551
Jan 4 at 15:58
Unfortunately not (they are copyrighted) but the root of this memory issue is the inability to allocate enough heap space with JavaFX. Running a Swing application using -Xmx2g works correctly, but with JavaFX that much memory cannot be allocated by the JVM (x64 JVM using 16G RAM).
– Xunkar
Jan 4 at 18:44
I don't see why there would be a difference in the memory allocation of the JVM between Swing and JavaFX. Sounds like a bug, I would submit it. If your image is 4000x3600 = 14.4Mil pixels and each pixel takes 4 bytes (rgba), then that's 57.6MB, which is really not a lot in any case. Are you getting an OOM exception?
– user1803551
Jan 6 at 6:06
|
show 2 more comments
I am trying to rewrite a Swing image viewer in JavaFX, however the scaling algorithm used by the JavaFX Image constructor is of very low quality. Unfortunately I have to rely on resizing the image in the constructor, because some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing).
Here's a comparison. On the left, using BufferedImage.drawImage
to downscale a picture and rendering it with Graphics.drawImage
in paintComponent
(using antialias rendering hint) and on the right the same picture loaded at the same downscaled resolution through the JavaFX Image constructor (smooth is set to true).
Comparison
As you can see the JavaFX downscaling algorithm introduces a form of sawtooth effect. How could I remedy this? I imagine the problem comes from the fact that the image is not fully loaded in memory but it seems I have no other choice.
Here's a code that will generate the comparison. Using the following picture as an example.
public static BufferedImage scale(BufferedImage img, int width, int height) {
int imageType = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaled = new BufferedImage(width, height, imageType);
Graphics2D context = scaled.createGraphics();
context.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
context.drawImage(img, 0, 0, width, height, null);
context.dispose();
return scaled;
}
@Override
public void start(Stage stage) {
// Load full image in memory, downscale using BufferedImage
Image image1 = new Image("file:4000x3600.jpg");
BufferedImage bimg = SwingFXUtils.fromFXImage(image1, null);
bimg = scale(bimg, 600, 600);
image1 = SwingFXUtils.toFXImage(bimg, null);
// Downscale in the Image constructor, saves memory
Image image2 = new Image("file:4000x3600.jpg", 600, 600, false, true);
AnchorPane root = new AnchorPane();
ImageView view1 = new ImageView(image1);
view1.setLayoutX(0.0);
view1.setLayoutY(0.0);
ImageView view2 = new ImageView(image2);
view2.setLayoutX(600.0);
view2.setLayoutY(0.0);
root.getChildren().add(view1);
root.getChildren().add(view2);
Scene scene = new Scene(root, 1200, 600);
stage.setScene(scene);
stage.show();
}
java image javafx java-8
I am trying to rewrite a Swing image viewer in JavaFX, however the scaling algorithm used by the JavaFX Image constructor is of very low quality. Unfortunately I have to rely on resizing the image in the constructor, because some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing).
Here's a comparison. On the left, using BufferedImage.drawImage
to downscale a picture and rendering it with Graphics.drawImage
in paintComponent
(using antialias rendering hint) and on the right the same picture loaded at the same downscaled resolution through the JavaFX Image constructor (smooth is set to true).
Comparison
As you can see the JavaFX downscaling algorithm introduces a form of sawtooth effect. How could I remedy this? I imagine the problem comes from the fact that the image is not fully loaded in memory but it seems I have no other choice.
Here's a code that will generate the comparison. Using the following picture as an example.
public static BufferedImage scale(BufferedImage img, int width, int height) {
int imageType = (img.getTransparency() == Transparency.OPAQUE)
? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage scaled = new BufferedImage(width, height, imageType);
Graphics2D context = scaled.createGraphics();
context.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
context.drawImage(img, 0, 0, width, height, null);
context.dispose();
return scaled;
}
@Override
public void start(Stage stage) {
// Load full image in memory, downscale using BufferedImage
Image image1 = new Image("file:4000x3600.jpg");
BufferedImage bimg = SwingFXUtils.fromFXImage(image1, null);
bimg = scale(bimg, 600, 600);
image1 = SwingFXUtils.toFXImage(bimg, null);
// Downscale in the Image constructor, saves memory
Image image2 = new Image("file:4000x3600.jpg", 600, 600, false, true);
AnchorPane root = new AnchorPane();
ImageView view1 = new ImageView(image1);
view1.setLayoutX(0.0);
view1.setLayoutY(0.0);
ImageView view2 = new ImageView(image2);
view2.setLayoutX(600.0);
view2.setLayoutY(0.0);
root.getChildren().add(view1);
root.getChildren().add(view2);
Scene scene = new Scene(root, 1200, 600);
stage.setScene(scene);
stage.show();
}
java image javafx java-8
java image javafx java-8
edited Jan 3 at 11:35
Xunkar
asked Jan 2 at 23:16
XunkarXunkar
407
407
2
Can you create a Minimal, Complete, and Verifiable example showing the two approaches?
– Slaw
Jan 2 at 23:27
Question edited with example code and comparison.
– Xunkar
Jan 3 at 11:36
"some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing)" Can you provide these pictures?
– user1803551
Jan 4 at 15:58
Unfortunately not (they are copyrighted) but the root of this memory issue is the inability to allocate enough heap space with JavaFX. Running a Swing application using -Xmx2g works correctly, but with JavaFX that much memory cannot be allocated by the JVM (x64 JVM using 16G RAM).
– Xunkar
Jan 4 at 18:44
I don't see why there would be a difference in the memory allocation of the JVM between Swing and JavaFX. Sounds like a bug, I would submit it. If your image is 4000x3600 = 14.4Mil pixels and each pixel takes 4 bytes (rgba), then that's 57.6MB, which is really not a lot in any case. Are you getting an OOM exception?
– user1803551
Jan 6 at 6:06
|
show 2 more comments
2
Can you create a Minimal, Complete, and Verifiable example showing the two approaches?
– Slaw
Jan 2 at 23:27
Question edited with example code and comparison.
– Xunkar
Jan 3 at 11:36
"some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing)" Can you provide these pictures?
– user1803551
Jan 4 at 15:58
Unfortunately not (they are copyrighted) but the root of this memory issue is the inability to allocate enough heap space with JavaFX. Running a Swing application using -Xmx2g works correctly, but with JavaFX that much memory cannot be allocated by the JVM (x64 JVM using 16G RAM).
– Xunkar
Jan 4 at 18:44
I don't see why there would be a difference in the memory allocation of the JVM between Swing and JavaFX. Sounds like a bug, I would submit it. If your image is 4000x3600 = 14.4Mil pixels and each pixel takes 4 bytes (rgba), then that's 57.6MB, which is really not a lot in any case. Are you getting an OOM exception?
– user1803551
Jan 6 at 6:06
2
2
Can you create a Minimal, Complete, and Verifiable example showing the two approaches?
– Slaw
Jan 2 at 23:27
Can you create a Minimal, Complete, and Verifiable example showing the two approaches?
– Slaw
Jan 2 at 23:27
Question edited with example code and comparison.
– Xunkar
Jan 3 at 11:36
Question edited with example code and comparison.
– Xunkar
Jan 3 at 11:36
"some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing)" Can you provide these pictures?
– user1803551
Jan 4 at 15:58
"some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing)" Can you provide these pictures?
– user1803551
Jan 4 at 15:58
Unfortunately not (they are copyrighted) but the root of this memory issue is the inability to allocate enough heap space with JavaFX. Running a Swing application using -Xmx2g works correctly, but with JavaFX that much memory cannot be allocated by the JVM (x64 JVM using 16G RAM).
– Xunkar
Jan 4 at 18:44
Unfortunately not (they are copyrighted) but the root of this memory issue is the inability to allocate enough heap space with JavaFX. Running a Swing application using -Xmx2g works correctly, but with JavaFX that much memory cannot be allocated by the JVM (x64 JVM using 16G RAM).
– Xunkar
Jan 4 at 18:44
I don't see why there would be a difference in the memory allocation of the JVM between Swing and JavaFX. Sounds like a bug, I would submit it. If your image is 4000x3600 = 14.4Mil pixels and each pixel takes 4 bytes (rgba), then that's 57.6MB, which is really not a lot in any case. Are you getting an OOM exception?
– user1803551
Jan 6 at 6:06
I don't see why there would be a difference in the memory allocation of the JVM between Swing and JavaFX. Sounds like a bug, I would submit it. If your image is 4000x3600 = 14.4Mil pixels and each pixel takes 4 bytes (rgba), then that's 57.6MB, which is really not a lot in any case. Are you getting an OOM exception?
– user1803551
Jan 6 at 6:06
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014424%2fcontrol-over-image-scaling-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014424%2fcontrol-over-image-scaling-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you create a Minimal, Complete, and Verifiable example showing the two approaches?
– Slaw
Jan 2 at 23:27
Question edited with example code and comparison.
– Xunkar
Jan 3 at 11:36
"some of the pictures are too big to be loaded fully in memory for JavaFX (even though it used to work in Swing)" Can you provide these pictures?
– user1803551
Jan 4 at 15:58
Unfortunately not (they are copyrighted) but the root of this memory issue is the inability to allocate enough heap space with JavaFX. Running a Swing application using -Xmx2g works correctly, but with JavaFX that much memory cannot be allocated by the JVM (x64 JVM using 16G RAM).
– Xunkar
Jan 4 at 18:44
I don't see why there would be a difference in the memory allocation of the JVM between Swing and JavaFX. Sounds like a bug, I would submit it. If your image is 4000x3600 = 14.4Mil pixels and each pixel takes 4 bytes (rgba), then that's 57.6MB, which is really not a lot in any case. Are you getting an OOM exception?
– user1803551
Jan 6 at 6:06