Class: Writexlsx::ImageProperty
- Inherits:
-
Object
- Object
- Writexlsx::ImageProperty
- Defined in:
- lib/write_xlsx/image_property.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#decorative ⇒ Object
readonly
Returns the value of attribute decorative.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#md5 ⇒ Object
readonly
Returns the value of attribute md5.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#position ⇒ Object
Returns the value of attribute position.
-
#ref_id ⇒ Object
Returns the value of attribute ref_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#x_dpi ⇒ Object
readonly
Returns the value of attribute x_dpi.
-
#y_dpi ⇒ Object
readonly
Returns the value of attribute y_dpi.
Instance Method Summary collapse
-
#initialize(filename, options = {}) ⇒ ImageProperty
constructor
A new instance of ImageProperty.
-
#process_bmp(data) ⇒ Object
Extract width and height information from a BMP file.
-
#process_gif(data) ⇒ Object
Extract width and height information from a GIF file.
- #process_jpg(data) ⇒ Object
-
#process_png(data) ⇒ Object
Extract width and height information from a PNG file.
Constructor Details
#initialize(filename, options = {}) ⇒ ImageProperty
Returns a new instance of ImageProperty.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/write_xlsx/image_property.rb', line 12 def initialize(filename, = {}) @filename = filename @description = [:description] @decorative = [:decorative] @position = [:position] @name = File.basename(filename) # Open the image file and import the data. data = File.binread(filename) @md5 = Digest::MD5.hexdigest(data) if data.unpack1('x A3') == 'PNG' process_png(data) elsif data.unpack1('n') == 0xFFD8 process_jpg(data) elsif data.unpack1('A4') == 'GIF8' process_gif(data) elsif data.unpack1('A2') == 'BM' process_bmp(data) else # TODO. Add Image::Size to support other types. raise "Unsupported image format for file: #{filename}\n" end # Set a default dpi for images with 0 dpi. @x_dpi = 96 if @x_dpi == 0 @y_dpi = 96 if @y_dpi == 0 end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
10 11 12 |
# File 'lib/write_xlsx/image_property.rb', line 10 def body @body end |
#decorative ⇒ Object (readonly)
Returns the value of attribute decorative.
9 10 11 |
# File 'lib/write_xlsx/image_property.rb', line 9 def decorative @decorative end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
9 10 11 |
# File 'lib/write_xlsx/image_property.rb', line 9 def description @description end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
9 10 11 |
# File 'lib/write_xlsx/image_property.rb', line 9 def filename @filename end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def height @height end |
#md5 ⇒ Object (readonly)
Returns the value of attribute md5.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def md5 @md5 end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def name @name end |
#position ⇒ Object
Returns the value of attribute position.
10 11 12 |
# File 'lib/write_xlsx/image_property.rb', line 10 def position @position end |
#ref_id ⇒ Object
Returns the value of attribute ref_id.
10 11 12 |
# File 'lib/write_xlsx/image_property.rb', line 10 def ref_id @ref_id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def type @type end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def width @width end |
#x_dpi ⇒ Object (readonly)
Returns the value of attribute x_dpi.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def x_dpi @x_dpi end |
#y_dpi ⇒ Object (readonly)
Returns the value of attribute y_dpi.
8 9 10 |
# File 'lib/write_xlsx/image_property.rb', line 8 def y_dpi @y_dpi end |
Instance Method Details
#process_bmp(data) ⇒ Object
Extract width and height information from a BMP file.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/write_xlsx/image_property.rb', line 140 def process_bmp(data) # :nodoc: @type = 'bmp' # Check that the file is big enough to be a bitmap. raise "#{@filename} doesn't contain enough data." if data.bytesize <= 0x36 # Read the bitmap width and height. Verify the sizes. @width, @height = data.unpack("x18 V2") raise "#{@filename}: largest image width #{width} supported is 65k." if width > 0xFFFF raise "#{@filename}: largest image height supported is 65k." if @height > 0xFFFF # Read the bitmap planes and bpp data. Verify them. planes, bitcount = data.unpack("x26 v2") raise "#{@filename} isn't a 24bit true color bitmap." unless bitcount == 24 raise "#{@filename}: only 1 plane supported in bitmap image." unless planes == 1 # Read the bitmap compression. Verify compression. compression = data.unpack1("x30 V") raise "#{@filename}: compression not supported in bitmap image." unless compression == 0 @x_dpi = @y_dpi = 96 end |
#process_gif(data) ⇒ Object
Extract width and height information from a GIF file.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/write_xlsx/image_property.rb', line 128 def process_gif(data) @type = 'gif' @x_dpi = 96 @y_dpi = 96 @width = data[6, 2].unpack1("v") @height = data[8, 2].unpack1("v") raise "#{@filename}: no size data found in gif image.\n" if @height.nil? end |
#process_jpg(data) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 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 |
# File 'lib/write_xlsx/image_property.rb', line 82 def process_jpg(data) @type = 'jpeg' @x_dpi = 96 @y_dpi = 96 offset = 2 data_length = data.bytesize # Search through the image data to read the JPEG markers. while offset < data_length marker = data[offset + 0, 2].unpack1("n") length = data[offset + 2, 2].unpack1("n") # Read the height and width in the 0xFFCn elements # (Except C4, C8 and CC which aren't SOF markers). if (marker & 0xFFF0) == 0xFFC0 && marker != 0xFFC4 && marker != 0xFFCC @height = data[offset + 5, 2].unpack1("n") @width = data[offset + 7, 2].unpack1("n") end # Read the DPI in the 0xFFE0 element. if marker == 0xFFE0 units = data[offset + 11, 1].unpack1("C") x_density = data[offset + 12, 2].unpack1("n") y_density = data[offset + 14, 2].unpack1("n") if units == 1 @x_dpi = x_density @y_dpi = y_density elsif units == 2 @x_dpi = x_density * 2.54 @y_dpi = y_density * 2.54 end end offset += length + 2 break if marker == 0xFFDA end raise "#{@filename}: no size data found in jpeg image.\n" unless @height end |
#process_png(data) ⇒ Object
Extract width and height information from a PNG file.
43 44 45 46 47 48 49 50 51 52 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 |
# File 'lib/write_xlsx/image_property.rb', line 43 def process_png(data) @type = 'png' @width = 0 @height = 0 @x_dpi = 96 @y_dpi = 96 offset = 8 data_length = data.size # Search through the image data to read the height and width in th the # IHDR element. Also read the DPI in the pHYs element. while offset < data_length length = data[offset + 0, 4].unpack1("N") png_type = data[offset + 4, 4].unpack1("A4") case png_type when "IHDR" @width = data[offset + 8, 4].unpack1("N") @height = data[offset + 12, 4].unpack1("N") when "pHYs" x_ppu = data[offset + 8, 4].unpack1("N") y_ppu = data[offset + 12, 4].unpack1("N") units = data[offset + 16, 1].unpack1("C") if units == 1 @x_dpi = x_ppu * 0.0254 @y_dpi = y_ppu * 0.0254 end end offset = offset + length + 12 break if png_type == "IEND" end raise "#{@filename}: no size data found in png image.\n" unless @height end |