Skip to content

Set original type to images within ImageLoader for OpenRTF support #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions openpdf/src/main/java/com/lowagie/text/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
Expand All @@ -73,7 +74,7 @@ public class ImageLoader {
public static Image getPngImage(URL url) {
try (InputStream is = url.openStream()) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);
return getImage(bufferedImage, Image.ORIGINAL_PNG);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand All @@ -82,7 +83,7 @@ public static Image getPngImage(URL url) {
public static Image getGifImage(URL url) {
try (InputStream is = url.openStream()) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);
return getImage(bufferedImage, Image.ORIGINAL_GIF);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand All @@ -91,7 +92,7 @@ public static Image getGifImage(URL url) {
public static Image getTiffImage(URL url) {
try (InputStream is = url.openStream()) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);
return getImage(bufferedImage, Image.ORIGINAL_TIFF);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand All @@ -101,7 +102,7 @@ public static Image getTiffImage(URL url) {
public static Image getBmpImage(URL url) {
try (InputStream is = url.openStream()) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);
return getImage(bufferedImage, Image.ORIGINAL_BMP);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand Down Expand Up @@ -134,7 +135,7 @@ public static Image getJpeg2000Image(URL url) {
public static Image getGifImage(byte[] imageData) {
try (InputStream is = new ByteArrayInputStream(imageData)) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);
return getImage(bufferedImage, Image.ORIGINAL_GIF);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand All @@ -143,8 +144,7 @@ public static Image getGifImage(byte[] imageData) {
public static Image getPngImage(byte[] imageData) {
try (InputStream is = new ByteArrayInputStream(imageData)) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);

return getImage(bufferedImage, Image.ORIGINAL_PNG);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand All @@ -153,8 +153,7 @@ public static Image getPngImage(byte[] imageData) {
public static Image getBmpImage(byte[] imageData) {
try (InputStream is = new ByteArrayInputStream(imageData)) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);

return getImage(bufferedImage, Image.ORIGINAL_BMP);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand All @@ -169,8 +168,7 @@ public static Image getBmpImage(byte[] imageData) {
public static Image getTiffImage(byte[] imageData) {
try (InputStream is = new ByteArrayInputStream(imageData)) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);

return getImage(bufferedImage, Image.ORIGINAL_TIFF);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
Expand Down Expand Up @@ -200,4 +198,9 @@ public static Image getJpeg2000Image(byte[] imageData) {
}
}

private static Image getImage(BufferedImage bufferedImage, int originalType) throws IOException {
Image image = Image.getInstance(bufferedImage, null, false);
image.setOriginalType(originalType);
return image;
}
}