get_section_bytes: Get bytes for section of jpeg file

Description Usage Arguments Details Value See Also Examples

View source: R/get_section_bytes.R

Description

This package mainly focuses on parsing Exif data but there may be other metadata in the file. This function allows you to extract the bytes for a section identified by scan_jpeg().

Usage

1
get_section_bytes(section, input = NULL, include_marker = FALSE)

Arguments

section

An object of class "jpeg_section"

input

A character vector or seek-able binary connection

include_marker

By default the bytes are returned without the two-byte marker identifier. Set to TRUE to include this value

Details

Note that raw bytes for each section are not saved by default. So you will need to pass the same path used during the scan to the input= parameter.

Value

A raw vector

See Also

scan_jpeg()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
path <- system.file("extdata", "LookUp.jpg", package="readexif")
toc <- scan_jpeg(path)
toc # note the 4th block contains adobe XMP data
bytes <- get_section_bytes(toc[[4]], path, include_marker=TRUE)

read_xmp <- function(bytes) {
  bytes <- c(bytes, as.raw(0L)) # pad with null byte
  # Use connection to keep track of bytes
  rawcon <- rawConnection(bytes)
  on.exit(close(rawcon))
  # Read section length
  len <- readBin(rawcon, integer(), n=1, size=2, signed=FALSE, endian = "big")
  # Read section ID
  id <- readBin(rawcon, character())
  # Read body. XMP happens to just use UTF-8 encoded text
  body <- readBin(rawcon, character())
  Encoding(body) <- "UTF-8" # (per the XMP spec)
  body <- gsub("\\s{2,}", "", body)  # trim excess whitespace
  return(list(len=len, id=id, body=body))
}

read_xmp(bytes)

MrFlick/readexif documentation built on Dec. 17, 2021, 4:22 a.m.