Class PDF::Writer::External::Image
In: lib/pdf/writer/object/image.rb
Parent: PDF::Writer::External
TechBook Transaction::Simple SimpleTable Complex Action FontDescriptor Procset Catalog FontEncoding Pages Destination Info Encryption Annotation Contents Outline Page Outlines Font ViewerPreferences Image Hash OHash QuickRef StdDev FontMetrics StrokeStyle ARC4 PolygonPoint ImageInfo lib/pdf/simpletable.rb lib/pdf/writer.rb lib/pdf/techbook.rb lib/pdf/quickref.rb lib/pdf/charts/stddev.rb Charts Math lib/pdf/writer/ohash.rb lib/pdf/writer/fontmetrics.rb lib/pdf/writer/strokestyle.rb lib/pdf/writer/arc4.rb lib/pdf/writer/graphics.rb lib/pdf/writer/object.rb lib/pdf/writer/object/image.rb External lib/pdf/writer/object/font.rb lib/pdf/writer/object/outlines.rb lib/pdf/writer/object/contents.rb lib/pdf/writer/object/annotation.rb lib/pdf/writer/object/destination.rb lib/pdf/writer/object/viewerpreferences.rb lib/pdf/writer/object/info.rb lib/pdf/writer/object/fontencoding.rb lib/pdf/writer/object/page.rb lib/pdf/writer/object/catalog.rb lib/pdf/writer/object/outline.rb lib/pdf/writer/object/encryption.rb lib/pdf/writer/object/procset.rb lib/pdf/writer/object/action.rb lib/pdf/writer/object/pages.rb lib/pdf/writer/object/fontdescriptor.rb Object OffsetReader EN Lang lib/pdf/writer/graphics/imageinfo.rb Graphics Writer PDF dot/m_33_0.png

An image object. This will be an /XObject dictionary in the document. This includes description and data. The diectionary includes:

Type:(name, Optional) The type of PDF object that this dictionary describes; if present, must be /XObject for an image XObject.
Subtype:(name, Required) The type of XObject that this dictionary describes; must be /Image for an image /XObject.
Width:(integer, Required) The width of the image, in samples.
Height:(integer, Required) The height of the image, in samples.
ColorSpace:(name or array, Required for images, except those that use the /JPXDecode filter; not allowed for image masks) The color space in which image samples are specified; it can be any type of color space except /Pattern. If the image uses the JPXDecode filter, this entry is optional: * If /ColorSpace is present, any color space specifications in the JPEG2000 data are ignored. * If /ColorSpace is absent, the color space specifications in the JPEG2000 data are used. The /Decode array is also ignored unless /ImageMask is true.
BitsPerComponent:(integer, Required except for image masks and images that use the JPXDecode filter) The number of bits used to represent each color component. Only a single value may be specified; the number of bits is the same for all color components. Valid values are 1, 2, 4, 8, and (in PDF 1.5) 16. If /ImageMask is true, this entry is optional, and if specified, its value must be 1. If the image stream uses a filter, the value of BitsPerComponent must be consistent with the size of the data samples that the filter delivers. In particular, a CCITTFaxDecode or JBIG2Decode filter always delivers 1-bit samples, a RunLengthDecode or DCTDecode filter delivers 8-bit samples, and an LZWDecode or FlateDecode filter delivers samples of a specified size if a predictor function is used. If the image stream uses the JPXDecode filter, this entry is optional and ignored if present. The bit depth is determined in the process of decoding the JPEG2000 image.
Intent:(name, Optional; PDF 1.1) The name of a color rendering intent to be used in rendering the image (see “Rendering Intents” on page 230). Default value: the current rendering intent in the graphics state.
ImageMask:(boolean, Optional) A flag indicating whether the image is to be treated as an image mask (see Section 4.8.5, “Masked Images”). If this flag is true, the value of /BitsPerComponent must be 1 and Mask and /ColorSpace should not be specified; unmasked areas are painted using the current nonstroking color. Default value: false.
Mask:(stream or array, Optional except for image masks; not allowed for image masks; PDF 1.3) An image XObject defining an image mask to be applied to this image (see “Explicit Masking” on page 321), or an array specifying a range of colors to be applied to it as a color key mask (see “Color Key Masking” on page 321). If ImageMask is true, this entry must not be present. (See implementation note 51 in Appendix H.)
Decode:(array, Optional) An array of numbers describing how to map image samples into the range of values appropriate for the image’s color space (see “Decode Arrays” on page 314). If ImageMask is true, the array must be either [0 1] or [1 0]; otherwise, its length must be twice the number of color components required by ColorSpace. If the image uses the JPXDecode filter and ImageMask is false, Decode is ignored. Default value: see “Decode Arrays” on page 314.
Interpolate:(boolean, Optional) A flag indicating whether image interpolation is to be performed (see “Image Interpolation” on page 316). Default value: false.
Alternates:(array, Optional; PDF 1.3) An array of alternate image dictionaries for this image (see “Alternate Images” on page 317). The order of elements within the array has no significance. This entry may not be present in an image XObject that is itself an alternate image.
SMask:(stream, Optional; PDF 1.4) A subsidiary image XObject defining a soft-mask image (see “Soft-Mask Images” on page 522) to be used as a source of mask shape or mask opacity values in the transparent imaging model. The alpha source parameter in the graphics state determines whether the mask values are interpreted as shape or opacity. If present, this entry overrides the current soft mask in the graphics state, as well as the image’s Mask entry, if any. (However, the other transparency related graphics state parameters—blend mode and alpha constant—remain in effect.) If SMask is absent, the image has no associated soft mask (although the current soft mask in the graphics state may still apply).
SMaskInData:(integer, Optional for images that use the JPXDecode filter, meaningless otherwise; PDF 1.5) A code specifying how soft-mask information (see “Soft-Mask Images” on page 522) encoded with image samples should be used: (0) If present, encoded soft-mask image information should be ignored. (1) The image’s data stream includes encoded soft-mask values. An application can create a soft-mask image from the information to be used as a source of mask shape or mask opacity in the transparency imaging model. (2) The image’s data stream includes color channels that have been preblended with a background; the image data also includes an opacity channel. An application can create a soft-mask image with a Matte entry from the opacity channel information to be used as a source of mask shape or mask opacity in the transparency model.
  • If this entry has a nonzero value, SMask should not

be specified. See also Section 3.3.8, “JPXDecode Filter.” Default value: 0.

Name:(name, Required in PDF 1.0; optional otherwise) The name by which this image XObject is referenced in the XObject subdictionary of the current resource dictionary (see Section 3.7.2, “Resource Dictionaries”). Note: This entry is obsolescent and its use is no longer recommended. (See implementation note 52 in Appendix H.)
StructParent:(integer, Required if the image is a structural content item; PDF 1.3) The integer key of the image’s entry in the structural parent tree (see “Finding Structure Elements from Content Items” on page 797).
ID:(string, Optional; PDF 1.3; indirect reference preferred) The digital identifier of the image’s parent Web Capture content set (see Section 10.9.5, “Object Attributes Related to Web Capture”).
OPI:(dictionary, Optional; PDF 1.2) An OPI version dictionary for the image (see Section 10.10.6, “Open Prepress Interface (OPI)”). If ImageMask is true, this entry is ignored.
Metadata:(stream, Optional; PDF 1.4) A metadata stream containing metadata for the image (see Section 10.2.2, “Metadata Streams”).
OC:(dictionary, Optional; PDF 1.5) An optional content group or optional content membership dictionary (see Section 4.10, “Optional Content”), specifying the optional content properties for this image XObject. Before the image is processed, its visibility is determined based on this entry. If it is determined to be invisible, the entire image is skipped, as if there were no Do operator to invoke it.

Methods

new   to_s  

Attributes

image_info  [R] 
label  [R] 

Public Class methods

[Source]

     # File lib/pdf/writer/object/image.rb, line 157
157:   def initialize(parent, data, image, label)
158:     super(parent)
159: 
160:     @data = data
161: 
162:     @image_info = image
163: 
164:     @info = {
165:       'Type'    => '/XObject',
166:       'Subtype' => '/Image',
167:       'Width'   => image.width,
168:       'Height'  => image.height
169:     }
170: 
171:     case image.format
172:     when "JPEG"
173:       case image.channels
174:       when 1
175:         @info['ColorSpace'] = '/DeviceGray'
176:       when 4
177:         @info['ColorSpace'] = '/DeviceCMYK'
178:           # This should fix problems with CMYK JPEG colours inverted in
179:           # Adobe Acrobat. Enable only if appropriate.
180: #       @info['Decode'] = '[1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0]'
181:       else
182:         @info['ColorSpace'] = '/DeviceRGB'
183:       end
184:       @info['Filter'] = '/DCTDecode'
185:       @info['BitsPerComponent'] = 8
186:     when "PNG"
187:       if image.info[:compression_method] != 0
188:         raise TypeError, PDF::Writer::Lang[:png_unsupp_compres]
189:       end
190: 
191:       if image.info[:filter_method] != 0
192:         raise TypeError, PDF::Writer::Lang[:png_unsupp_filter]
193:       end
194: 
195:       data = data.dup
196:       data.extend(PDF::Writer::OffsetReader)
197: 
198:       data.read_o(8)  # Skip the default header
199: 
200:       ok      = true
201:       length  = data.size
202:       palette = ""
203:       idat    = ""
204: 
205:       while ok
206:         chunk_size  = data.read_o(4).unpack("N")[0]
207:         section     = data.read_o(4)
208:         case section
209:         when 'PLTE'
210:           palette << data.read_o(chunk_size)
211:         when 'IDAT'
212:           idat << data.read_o(chunk_size)
213:         when 'tRNS'
214:             # This chunk can only occur once and it must occur after the
215:             # PLTE chunk and before the IDAT chunk
216:           trans = {}
217:           case image.info[:color_type]
218:           when 3
219:               # Indexed colour, RGB. Each byte in this chunk is an alpha for
220:               # the palette index in the PLTE ("palette") chunk up until the
221:               # last non-opaque entry. Set up an array, stretching over all
222:               # palette entries which will be 0 (opaque) or 1 (transparent).
223:             trans[:type]  = 'indexed'
224:             trans[:data]  = data.read_o(chunk_size).unpack("C*")
225:           when 0
226:               # Greyscale. Corresponding to entries in the PLTE chunk.
227:               # Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1
228:             trans[:grayscale] = data.read_o(2).unpack("n")
229:             trans[:type]      = 'indexed'
230: #           trans[:data]      = data.read_o.unpack("C")
231:           when 2
232:               # True colour with proper alpha channel.
233:             trans[:rgb] = data.read_o(6).unpack("nnn")
234:           end
235:         else
236:           data.offset += chunk_size
237:         end
238: 
239:         ok = (section != "IEND")
240: 
241:         data.read_o(4)  # Skip the CRC
242:       end
243: 
244:       if image.bits > 8
245:         raise TypeError, PDF::Writer::Lang[:png_8bit_colour]
246:       end
247:       if image.info[:interlace_method] != 0
248:         raise TypeError, PDF::Writer::Lang[:png_interlace]
249:       end
250: 
251:       ncolor  = 1
252:       colour  = 'DeviceRGB'
253:       case image.info[:color_type]
254:       when 3
255:         nil
256:       when 2
257:         ncolor = 3
258:       when 0
259:         colour = 'DeviceGray'
260:       else
261:         raise TypeError, PDF::Writer::Lang[:png_alpha_trans]
262:       end
263: 
264:       @info['Filter'] = '[/FlateDecode]'
265:       @info['DecodeParms'] = "[<</Predictor 15 /Colors #{ncolor} /Columns #{image.width}>>]"
266:       @info['BitsPerComponent'] = image.bits.to_s
267: 
268:       unless palette.empty?
269:         @info['ColorSpace']  = " [ /Indexed /DeviceRGB #{(palette.size / 3) - 1} "
270:         contents            = PDF::Writer::Object::Contents.new(parent,
271:                                                                 self)
272:         contents.data       = palette
273:         @info['ColorSpace'] << "#{contents.oid} 0 R ]"
274: 
275:         if trans
276:           case trans[:type]
277:           when 'indexed'
278:             @info['Mask']   = " [ #{trans[:data].join(' ')} ] "
279:           end
280:         end
281:       else
282:         @info['ColorSpace'] = "/#{colour}"
283:       end
284: 
285:       @data = idat
286:     end
287: 
288:     @label = label
289: 
290:       # assign it a place in the named resource dictionary as an external
291:       # object, according to the label passed in with it.
292:     @parent.pages << self
293:       # also make sure that we have the right procset object for it.
294:     @parent.procset << 'ImageC'
295:   end

Public Instance methods

[Source]

     # File lib/pdf/writer/object/image.rb, line 297
297:   def to_s
298:     tmp = @data.dup
299:     res = "\n#{@oid} 0 obj\n<<"
300:     @info.each { |k, v| res << "\n/#{k} #{v}"}
301:     res << "\n/Length #{tmp.size} >>\nstream\n#{tmp}\nendstream\nendobj\n"
302:     res
303:   end

[Validate]