Module: Pdfrb::ImageLoader::PNG
- Defined in:
- lib/pdfrb/image_loader/png.rb
Overview
PNG loader. Parses IHDR/IDAT/IEND chunks. Stores the
zlib-compressed IDAT bytes verbatim with a PNG-predictor
DecodeParms dict so the reader re-applies the PNG unfilter
via FlateDecode.
Supported color types: 0 (gray), 2 (RGB), 3 (indexed), 4 (gray+alpha), 6 (RGBA). For RGBA/gray+alpha, alpha is split into a separate /SMask Image XObject (also FlateDecode + PNG predictor).
Class Method Summary collapse
- .build_opaque_image(document, width, height, bit_depth, color_type, decoded, chunks) ⇒ Object
- .build_rgba_image(document, width, height, bit_depth, color_type, decoded, _chunks) ⇒ Object
- .call(document, bytes, **_opts) ⇒ Object
Class Method Details
.build_opaque_image(document, width, height, bit_depth, color_type, decoded, chunks) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pdfrb/image_loader/png.rb', line 78 def build_opaque_image(document, width, height, bit_depth, color_type, decoded, chunks) predictor_parms = predictor_parms(width, color_type[:channels], bit_depth) dict = { Type: :XObject, Subtype: :Image, Width: width, Height: height, BitsPerComponent: bit_depth, Filter: :FlateDecode, DecodeParms: predictor_parms } dict[:ColorSpace] = color_space_for(color_type, bit_depth, chunks) image = document.add(dict, type: Pdfrb::Model::Type::XObjectImage) image.stream = deflate_with_filter_bytes(decoded, width, color_type[:channels], height) image end |
.build_rgba_image(document, width, height, bit_depth, color_type, decoded, _chunks) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pdfrb/image_loader/png.rb', line 95 def build_rgba_image(document, width, height, bit_depth, color_type, decoded, _chunks) # De-filter PNG rows, split into RGB + alpha channels, # re-compress each separately. rgb, alpha = split_channels(decoded, width, height, color_type[:channels], bit_depth) channels = color_type[:channels] - 1 # alpha removed predictor_parms = predictor_parms(width, channels, bit_depth) rgb_bytes = with_filter_bytes(rgb, width, channels, height) alpha_bytes = with_filter_bytes(alpha, width, 1, height) smask = document.add( { Type: :XObject, Subtype: :Image, Width: width, Height: height, BitsPerComponent: bit_depth, ColorSpace: :DeviceGray, Filter: :FlateDecode, DecodeParms: predictor_parms(width, 1, bit_depth) }, type: Pdfrb::Model::Type::XObjectImage ) smask.stream = Zlib::Deflate.deflate(alpha_bytes) image = document.add( { Type: :XObject, Subtype: :Image, Width: width, Height: height, BitsPerComponent: bit_depth, ColorSpace: color_type[:name] == :rgba ? :DeviceRGB : :DeviceGray, Filter: :FlateDecode, DecodeParms: predictor_parms, SMask: Pdfrb::Model::Reference.new(smask.oid, smask.gen) }, type: Pdfrb::Model::Type::XObjectImage ) image.stream = Zlib::Deflate.deflate(rgb_bytes) image end |
.call(document, bytes, **_opts) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pdfrb/image_loader/png.rb', line 28 def call(document, bytes, **_opts) return nil unless bytes.is_a?(::String) && bytes.start_with?(SIGNATURE) chunks = parse_chunks(bytes) ihdr = chunks[:IHDR] width = unpack_uint(ihdr, 0, 4) height = unpack_uint(ihdr, 4, 4) bit_depth = ihdr.getbyte(8) color_type_id = ihdr.getbyte(9) color_type = COLOR_TYPES[color_type_id] || raise(Pdfrb::Error, "PNG: unsupported color type #{color_type_id}") idat = chunks[:IDATs].map { |c| c }.join decoded = Zlib::Inflate.inflate(idat) if color_type[:has_alpha] build_rgba_image(document, width, height, bit_depth, color_type, decoded, chunks) else build_opaque_image(document, width, height, bit_depth, color_type, decoded, chunks) end end |