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: PDF1.7 s7.4.8, ITU T.81 Annex B

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
ADOBE_MARKER =

Adobe uses the marker 0xEE (APPE) for its purposes. We need to use it for determinig whether to invert the colors for CMYK/YCCK images or not (Adobe does this…).

The marker also let’s us distinguish between YCCK and CMYK images. However, we don’t actually need this information (and we don’t need to set the /ColorTransform value) because if the image has this information it is automically used.

0xEE
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.

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# File 'lib/hexapdf/image_loader/jpeg.rb', line 74

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.



88
89
90
91
92
93
94
95
# File 'lib/hexapdf/image_loader/jpeg.rb', line 88

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