Skip to content

Commit

Permalink
Add support for inline image data in content streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
deven committed Jul 24, 2024
1 parent 89f93bf commit 7e6b9f8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/PDF/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ sub generate_content_stream {
} elsif ($object->[1]{type} eq "array") {
# Serialize array.
$self->serialize_array(\$stream, $object->[0]);
} elsif ($object->[1]{type} eq "image") {
# Serialize inline image data.
$self->serialize_image(\$stream, $object->[0]);
} else {
# Serialize string or other token.
$self->serialize_object(\$stream, $object->[0]);
Expand Down Expand Up @@ -726,6 +729,14 @@ sub serialize_array {
${$stream} .= "]";
}

# Append the serialization of inline image data to the generated content stream.
sub serialize_image {
my ($self, $stream, $image) = @_;

# Append inline image data between ID (Image Data) and EI (End Image) operators.
${$stream} .= "\nID\n$image\nEI\n";
}

# Append the serialization of an object to the generated content stream.
sub serialize_object {
my ($self, $stream, $object) = @_;
Expand Down Expand Up @@ -835,6 +846,13 @@ sub parse_objects {
($token eq "R" ? \$new_id : $new_id),
{ type => $token, offset => $id->[1]{offset} }
];
} elsif ($token eq "ID") { # Inline image data: ID ... EI
s/\A$s(.*?)(?:\r\n|$s)?EI$s//s or croak join(": ", $self->{-file} || (), "Byte offset $offset: Invalid inline image data!\n");
my $image = $1;

# TODO: Apply encoding filters?

push @objects, [ $image, { type => "image" } ];
} elsif ($token eq "stream") { # Stream content: stream ... endstream
my ($id, $stream) = @objects[-2,-1];
$stream->[1]{type} eq "dict" or croak join(": ", $self->{-file} || (), "Byte offset $offset: Stream dictionary missing!\n");
Expand Down

0 comments on commit 7e6b9f8

Please sign in to comment.