PaperForge.Image (PaperForge v0.2.0)

Copy Markdown View Source

Represents an image registered inside a PDF document.

PaperForge supports JPEG and PNG images embedded as PDF image XObjects.

A registered image contains:

  • its content hash;
  • original width and height;
  • color space;
  • bits per component;
  • raw image data;
  • internal PDF resource name;
  • indirect object reference.

Example

%PaperForge.Image{
  hash: <<...>>,
  format: :png,
  width: 1200,
  height: 800,
  color_space: :device_rgb,
  bits_per_component: 8,
  data: image_binary,
  resource_name: "Im1",
  reference: %PaperForge.Reference{
    object_id: 8,
    generation: 0
  }
}

Summary

Functions

Calculates the display size while preserving aspect ratio when only one dimension is provided.

Returns a SHA-256 hash for image binary data.

Returns a hexadecimal representation of an image hash.

Returns the PDF name associated with the image color space.

Types

color_space()

@type color_space() :: :device_gray | :device_rgb | :device_cmyk

format()

@type format() :: :jpeg | :png

t()

@type t() :: %PaperForge.Image{
  bits_per_component: pos_integer(),
  color_space: color_space(),
  data: binary(),
  format: format(),
  hash: binary(),
  height: pos_integer(),
  reference: PaperForge.Reference.t(),
  resource_name: binary(),
  width: pos_integer()
}

Functions

display_size(image, options \\ [])

@spec display_size(
  t(),
  keyword()
) :: {number(), number()}

Calculates the display size while preserving aspect ratio when only one dimension is provided.

Supported combinations:

  • neither width nor height: use original dimensions;
  • only width: calculate height;
  • only height: calculate width;
  • both width and height: use both values directly.

Examples

PaperForge.Image.display_size(
  image,
  width: 200
)

PaperForge.Image.display_size(
  image,
  height: 100
)

hash(data)

@spec hash(binary()) :: binary()

Returns a SHA-256 hash for image binary data.

This hash can be used by PaperForge.ImageRegistry to avoid storing the same image more than once.

hash_hex(hash)

@spec hash_hex(t() | binary()) :: binary()

Returns a hexadecimal representation of an image hash.

This is mainly useful for debugging and tests.

new(hash, format, metadata, data, resource_name, reference)

@spec new(
  binary(),
  format(),
  map(),
  binary(),
  binary(),
  PaperForge.Reference.t()
) :: t()

Creates a registered image.

The metadata map must contain:

  • :width
  • :height
  • :color_space
  • :bits_per_component

pdf_color_space(arg1)

@spec pdf_color_space(t() | color_space()) :: {:name, binary()}

Returns the PDF name associated with the image color space.