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.
Defined Under Namespace
Classes: FormatError
Extension API collapse
-
#bits ⇒ Integer
readonly
Sample Precision in bits.
-
#channels ⇒ Integer
readonly
Number of image components (channels).
-
#height ⇒ Integer
readonly
Image height in pixels.
-
#scaled_height ⇒ Number
Scaled height of the image in PDF points.
-
#scaled_width ⇒ Number
Scaled width of the image in PDF points.
-
#width ⇒ Integer
readonly
Image width in pixels.
Extension API collapse
-
.can_render?(image_blob) ⇒ Boolean
Can this image handler process this image?.
-
#build_pdf_object(document) ⇒ PDF::Core::Reference
Build a PDF object representing this image in
document
, and return a Reference to it. -
#initialize(data) ⇒ JPG
constructor
Process a new JPG image.
Constructor Details
#initialize(data) ⇒ JPG
Process a new JPG image.
Source Code
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
#bits ⇒ Integer (readonly)
Sample Precision in bits.
Source Code
27 | def bits |
28 | @bits
|
29 | end
|
#channels ⇒ Integer (readonly)
Number of image components (channels).
Source Code
31 | def channels |
32 | @channels
|
33 | end
|
#height ⇒ Integer (readonly)
Image height in pixels.
Source Code
23 | def height |
24 | @height
|
25 | end
|
#scaled_height ⇒ Number
Scaled height of the image in PDF points.
Source Code
39 | def scaled_height |
40 | @scaled_height
|
41 | end
|
#scaled_width ⇒ Number
Scaled width of the image in PDF points.
Source Code
35 | def scaled_width |
36 | @scaled_width
|
37 | end
|
#width ⇒ Integer (readonly)
Image width in pixels.
Source Code
19 | def width |
20 | @width
|
21 | end
|
Class Method Details
.can_render?(image_blob) ⇒ Boolean
Can this image handler process this image?
Source Code
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.
Source Code
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
|