Skip to content

Commit

Permalink
Add guards to prevent making BufferedImage with custom image type.
Browse files Browse the repository at this point in the history
Previous commit caused certain unit tests to fail in Java 5, as PNGs
were loaded on `BufferedImage` with `.TYPE_CUSTOM`.
  • Loading branch information
coobird committed Oct 17, 2020
1 parent a8155d1 commit ce5b459
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,15 @@ public BufferedImage build() {
* Sets the type of the image of the {@link BufferedImage}.
*
* @param imageType The image type to use.
* If {@link BufferedImage#TYPE_CUSTOM} is used, it
* be substituted by {@link BufferedImage#TYPE_INT_ARGB}.
* @return This {@link BufferedImageBuilder} instance.
*/
public BufferedImageBuilder imageType(int imageType) {
if (imageType == BufferedImage.TYPE_CUSTOM) {
imageType = DEFAULT_TYPE;
}

this.imageType = imageType;
return this;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/net/coobird/thumbnailator/filters/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.awt.Point;
import java.awt.image.BufferedImage;

import net.coobird.thumbnailator.builders.BufferedImageBuilder;
import net.coobird.thumbnailator.geometry.Position;

/**
Expand Down Expand Up @@ -171,8 +172,11 @@ public BufferedImage apply(BufferedImage img) {
0, 0, 0, 0
);

BufferedImage finalImage =
new BufferedImage(widthToUse, heightToUse, img.getType());
BufferedImage finalImage = new BufferedImageBuilder(
widthToUse,
heightToUse,
img.getType()
).build();

Graphics g = finalImage.getGraphics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package net.coobird.thumbnailator.filters;

import net.coobird.thumbnailator.builders.BufferedImageBuilder;
import net.coobird.thumbnailator.util.BufferedImages;

import java.awt.AlphaComposite;
Expand Down Expand Up @@ -85,8 +86,11 @@ public BufferedImage apply(BufferedImage img) {
int width = img.getWidth();
int height = img.getHeight();

BufferedImage finalImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
BufferedImage finalImage = new BufferedImageBuilder(
width,
height,
BufferedImage.TYPE_INT_ARGB
).build();

Graphics2D g = finalImage.createGraphics();
g.setComposite(composite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package net.coobird.thumbnailator.resizers;

import net.coobird.thumbnailator.builders.BufferedImageBuilder;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
Expand Down Expand Up @@ -97,11 +99,11 @@ public void resize(BufferedImage srcImage, BufferedImage destImage)
}

// Temporary image used for in-place resizing of image.
BufferedImage tempImage = new BufferedImage(
BufferedImage tempImage = new BufferedImageBuilder(
currentWidth,
currentHeight,
destImage.getType()
);
).build();

Graphics2D g = createGraphics(tempImage);
g.setComposite(AlphaComposite.Src);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package net.coobird.thumbnailator.util;

import net.coobird.thumbnailator.builders.BufferedImageBuilder;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

Expand Down Expand Up @@ -61,12 +63,11 @@ public static BufferedImage copy(BufferedImage img) {
public static BufferedImage copy(BufferedImage img, int imageType) {
int width = img.getWidth();
int height = img.getHeight();

BufferedImage newImage = new BufferedImage(width, height, imageType);

BufferedImage newImage = new BufferedImageBuilder(width, height, imageType).build();

Graphics g = newImage.createGraphics();

g.drawImage(img, 0, 0, null);

g.dispose();

return newImage;
Expand Down

0 comments on commit ce5b459

Please sign in to comment.