Class: Prawn::Images::JPG
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
Constant Summary collapse
- JPEG_SOF_BLOCKS =
[0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF]
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#scaled_height ⇒ Object
Returns the value of attribute scaled_height.
-
#scaled_width ⇒ Object
Returns the value of attribute scaled_width.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_pdf_object(document) ⇒ Object
Build a PDF object representing this image in
document
, and return a Reference to it. -
#initialize(data) ⇒ JPG
constructor
Process a new JPG image.
Methods inherited from Image
Constructor Details
#initialize(data) ⇒ JPG
Process a new JPG image
:data
-
A binary string of JPEG data
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/prawn/images/jpg.rb', line 30 def initialize(data) @data = data d = StringIO.new(@data) d.binmode c_marker = 0xff # Section marker. d.seek(2) # Skip the first two bytes of JPEG identifier. loop do marker, code, length = d.read(4).unpack('CCn') raise "JPEG marker not found!" if marker != c_marker if JPEG_SOF_BLOCKS.include?(code) @bits, @height, @width, @channels = d.read(6).unpack("CnnC") break end d.seek(length - 2, IO::SEEK_CUR) end end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
17 18 19 |
# File 'lib/prawn/images/jpg.rb', line 17 def bits @bits end |
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
17 18 19 |
# File 'lib/prawn/images/jpg.rb', line 17 def channels @channels end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
17 18 19 |
# File 'lib/prawn/images/jpg.rb', line 17 def height @height end |
#scaled_height ⇒ Object
Returns the value of attribute scaled_height.
18 19 20 |
# File 'lib/prawn/images/jpg.rb', line 18 def scaled_height @scaled_height end |
#scaled_width ⇒ Object
Returns the value of attribute scaled_width.
18 19 20 |
# File 'lib/prawn/images/jpg.rb', line 18 def scaled_width @scaled_width end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
17 18 19 |
# File 'lib/prawn/images/jpg.rb', line 17 def width @width end |
Class Method Details
.can_render?(image_blob) ⇒ Boolean
22 23 24 |
# File 'lib/prawn/images/jpg.rb', line 22 def self.can_render?(image_blob) image_blob[0, 3].unpack("C*") == [255, 216, 255] end |
Instance Method Details
#build_pdf_object(document) ⇒ Object
Build a PDF object representing this image in document
, and return a Reference to it.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/prawn/images/jpg.rb', line 53 def build_pdf_object(document) color_space = case channels when 1 :DeviceGray when 3 :DeviceRGB when 4 :DeviceCMYK else raise ArgumentError, 'JPG uses an unsupported number of channels' end obj = document.ref!( :Type => :XObject, :Subtype => :Image, :ColorSpace => color_space, :BitsPerComponent => bits, :Width => width, :Height => height ) # add extra decode params for CMYK images. By swapping the # min and max values from the default, we invert the colours. See # section 4.8.4 of the spec. if color_space == :DeviceCMYK obj.data[:Decode] = [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ] end obj.stream << @data obj.stream.filters << :DCTDecode obj end |