Skip to content

Webp image support #1295

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
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@
int c6 = is.read();
int c7 = is.read();
int c8 = is.read();
// webp
int c9 = is.read();
int c10 = is.read();
int c11 = is.read();
int c12 = is.read();
is.close();

is = null;
Expand Down Expand Up @@ -519,6 +524,15 @@
img = ImageLoader.getTiffImage(url);
return img;
}
if (c1 == 'R' && c2 == 'I'
&& c3 == 'F'
&& c4 == 'F'
&& c9 == 'W'
&& c10 == 'E'
&& c11 == 'B'
&& c12 == 'P') {
return ImageLoader.getWebpImage(imgb);
}
if (c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2' &&
c5 == '\r' && c6 == '\n' && c7 == 0x1a && c8 == '\n') {
throw new IOException(url.toString()
Expand Down Expand Up @@ -612,6 +626,21 @@
|| (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) {
return ImageLoader.getTiffImage(imgb);
}
if (c1 == 'R' && c2 == 'I'
&& c3 == 'F'
&& c4 == 'F') {
is = new java.io.ByteArrayInputStream(imgb);
long skipped = is.skip(8);
if(skipped == 8) {

Check notice on line 634 in openpdf/src/main/java/com/lowagie/text/Image.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

openpdf/src/main/java/com/lowagie/text/Image.java#L634

'if' is not followed by whitespace.

Check notice on line 634 in openpdf/src/main/java/com/lowagie/text/Image.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

openpdf/src/main/java/com/lowagie/text/Image.java#L634

WhitespaceAround: 'if' is not followed by whitespace. Empty blocks may only be represented as {} when not part of a multi-block statement (4.1.3)
final int c9 = is.read();
final int c10 = is.read();
final int c11 = is.read();
final int c12 = is.read();
if (c9 == 'W' && c10 == 'E' && c11 == 'B' && c12 == 'P') {
return ImageLoader.getWebpImage(imgb);
}
}
}

Check notice on line 643 in openpdf/src/main/java/com/lowagie/text/Image.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

openpdf/src/main/java/com/lowagie/text/Image.java#L643

Line contains a tab character.
if (c1 == 0x97 && c2 == 'J' && c3 == 'B' && c4 == '2') {
is = new java.io.ByteArrayInputStream(imgb);
is.skip(4);
Expand Down
17 changes: 17 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ public static Image getTiffImage(byte[] imageData) {
}
}

/**
* Creates an Image from an array of webp image bytes.
* https://datatracker.ietf.org/doc/rfc9649/
*
* @param imageData bytes of the webp image
* @return an objet of type <code>Image</code>
*/
public static Image getWebpImage(byte[] imageData) {
try (InputStream is = new ByteArrayInputStream(imageData)) {
BufferedImage bufferedImage = ImageIO.read(is);
return Image.getInstance(bufferedImage, null, false);

} catch (Exception e) {
throw new ExceptionConverter(e);
}
}

/**
* Creates an Image from a JPEG image file in a byte array.
*
Expand Down