Class: Prawn::Images::JPG

Inherits:
Image
  • Object
show all
Defined in:
lib/prawn/images/jpg.rb

Overview

A convenience class that wraps the logic for extracting the parts of a JPG image that we need to embed them in a PDF.

Defined Under Namespace

Classes: FormatError

Extension API collapse

Extension API collapse

Constructor Details

#initialize(data) ⇒ JPG

Process a new JPG image.

Parameters:

  • data (String)

    A binary string of JPEG data.

Source Code
lib/prawn/images/jpg.rb, line 58
58
def initialize(data)
59
  super()
60
  @data = data
61
  d = StringIO.new(@data)
62
  d.binmode
63
64
  c_marker = 0xff # Section marker.
65
  d.seek(2) # Skip the first two bytes of JPEG identifier.
66
  loop do
67
    marker, code, length = d.read(4).unpack('CCn')
68
    raise FormatError, 'JPEG marker not found!' if marker != c_marker
69
70
    if JPEG_SOF_BLOCKS.include?(code)
71
      @bits, @height, @width, @channels = d.read(6).unpack('CnnC')
72
      break
73
    end
74
75
    d.seek(length - 2, IO::SEEK_CUR)
76
  end
77
end

Instance Attribute Details

#bitsInteger (readonly)

Sample Precision in bits.

Returns:

  • (Integer)
Source Code
lib/prawn/images/jpg.rb, line 27
27
def bits
28
  @bits
29
end

#channelsInteger (readonly)

Number of image components (channels).

Returns:

  • (Integer)
Source Code
lib/prawn/images/jpg.rb, line 31
31
def channels
32
  @channels
33
end

#heightInteger (readonly)

Image height in pixels.

Returns:

  • (Integer)
Source Code
lib/prawn/images/jpg.rb, line 23
23
def height
24
  @height
25
end

#scaled_heightNumber

Scaled height of the image in PDF points.

Returns:

  • (Number)
Source Code
lib/prawn/images/jpg.rb, line 39
39
def scaled_height
40
  @scaled_height
41
end

#scaled_widthNumber

Scaled width of the image in PDF points.

Returns:

  • (Number)
Source Code
lib/prawn/images/jpg.rb, line 35
35
def scaled_width
36
  @scaled_width
37
end

#widthInteger (readonly)

Image width in pixels.

Returns:

  • (Integer)
Source Code
lib/prawn/images/jpg.rb, line 19
19
def width
20
  @width
21
end

Class Method Details

.can_render?(image_blob) ⇒ Boolean

Can this image handler process this image?

Parameters:

  • image_blob (String)

Returns:

  • (Boolean)
Source Code
lib/prawn/images/jpg.rb, line 51
51
def self.can_render?(image_blob)
52
  image_blob[0, 3].unpack('C*') == [255, 216, 255]
53
end

Instance Method Details

#build_pdf_object(document) ⇒ PDF::Core::Reference

Build a PDF object representing this image in document, and return a Reference to it.

Parameters:

Returns:

  • (PDF::Core::Reference)
Source Code
lib/prawn/images/jpg.rb, line 84
84
def build_pdf_object(document)
85
  color_space =
86
    case channels
87
    when 1
88
      :DeviceGray
89
    when 3
90
      :DeviceRGB
91
    when 4
92
      :DeviceCMYK
93
    else
94
      raise ArgumentError, 'JPG uses an unsupported number of channels'
95
    end
96
97
  obj = document.ref!(
98
    Type: :XObject,
99
    Subtype: :Image,
100
    ColorSpace: color_space,
101
    BitsPerComponent: bits,
102
    Width: width,
103
    Height: height,
104
  )
105
106
  # add extra decode params for CMYK images. By swapping the
107
  # min and max values from the default, we invert the colours. See
108
  # section 4.8.4 of the spec.
109
  if color_space == :DeviceCMYK
110
    obj.data[:Decode] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
111
  end
112
113
  obj.stream << @data
114
  obj.stream.filters << :DCTDecode
115
  obj
116
end