Module: HexaPDF::ImageLoader::JPEG

Defined in:
lib/hexapdf/image_loader/jpeg.rb

Overview

This module is used for loading images in the JPEG format from files or IO streams.

See: PDF2.0 s7.4.8, ITU T.81 Annex B, ITU T.872

Constant Summary collapse

MAGIC_FILE_MARKER =

The magic marker that tells us if the file/IO contains an image in JPEG format.

"\xFF\xD8\xFF".b
SOF_MARKERS =

The various start-of-frame markers that tell us which kind of JPEG it is. The marker segment itself contains all the needed information needed for creating the PDF image object.

See: ITU T.81 B1.1.3

[
  0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF
].freeze
APP14_MARKER =

Adobe uses the marker 0xEE (APPE or APP14) for its purposes. We need to use it for determinig whether we have a CMYK or YCCK image.

0xEE
APP14_TRANSFORM_CMYK =

Value of the 12th byte in an APP14 marker specifying that the image uses CMYK color encoding, with all four colors complemented.

0
EOI_MARKER =

End-of-image marker

0xD9
SOS_MARKER =

Start-of-scan marker

0xDA

Class Method Summary collapse

Class Method Details

.handles?(file_or_io) ⇒ Boolean

:call-seq:

JPEG.handles?(filename)     -> true or false
JPGE.handles?(io)           -> true or false

Returns true if the given file or IO stream can be handled, ie. if it contains an image in JPEG format.



79
80
81
82
83
84
85
86
# File 'lib/hexapdf/image_loader/jpeg.rb', line 79

def self.handles?(file_or_io)
  if file_or_io.kind_of?(String)
    File.read(file_or_io, 3, mode: 'rb') == MAGIC_FILE_MARKER
  else
    file_or_io.rewind
    file_or_io.read(3) == MAGIC_FILE_MARKER
  end
end

.load(document, file_or_io) ⇒ Object

:call-seq:

JPEG.load(document, filename)    -> image_obj
JPEG.load(document, io)          -> image_obj

Creates a PDF image object from the JPEG file or IO stream.



93
94
95
96
97
98
99
100
# File 'lib/hexapdf/image_loader/jpeg.rb', line 93

def self.load(document, file_or_io)
  dict = if file_or_io.kind_of?(String)
           File.open(file_or_io, 'rb') {|io| image_data_from_io(io) }
         else
           image_data_from_io(file_or_io)
         end
  document.add(dict, stream: HexaPDF::StreamData.new(file_or_io))
end