Class: Sketchup::ImageRep

Inherits:
Object
  • Object
show all

Overview

References an image representation object.

Examples:

# Get the color of the center of the first material texture found in model.
texture = Sketchup.active_model.materials.map(&:texture).compact.first
image_rep = texture.image_rep
color = image_rep.color_at_uv(0.5, 0.5)

Version:

  • SketchUp 2018

Instance Method Summary # collapse

Constructor Details

#initializeSketchup::ImageRep #initialize(filepath) ⇒ Sketchup::ImageRep

The #initialize method creates a new image object. The image object will have no data if a path to the image is not provided.

Examples:

Default constructor

image_rep = Sketchup::ImageRep.new
# Use #set_data or #load_file to add image data.

Construct from file

image_rep = Sketchup::ImageRep.new("/path/to/image.jpg")

Overloads:

Raises:

  • (ArgumentError)

    if the file path or image is invalid.

Version:

  • SketchUp 2018

Instance Method Details

#bits_per_pixelInteger

The #bits_per_pixel method gets the number of bits per pixel in the image.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
bpp = image_rep.bits_per_pixel

Returns:

  • (Integer)

Version:

  • SketchUp 2018

#color_at_uv(u, v, bilinear = false) ⇒ Sketchup::Color?

The #color_at_uv method returns a color corresponding to the UV texture coordinates. 0.0, 0.0 maps to the bottom left and 1.0, 1.0 to the top right of the image.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
color = image_rep.color_at_uv(0.7, 0.5, false)

Parameters:

  • u (Float)

    The U texture coordinate.

  • v (Float)

    The V texture coordinate.

  • bilinear (Boolean) (defaults to: false)

    Use bilinear texture filtering. This interpolates the colors instead of picking the nearest neighbor.

Returns:

Version:

  • SketchUp 2018

#colorsArray<Sketchup::Color>?

The #colors method returns an array of Color for each pixel in the image.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
colors = image_rep.colors

Returns:

Version:

  • SketchUp 2018

#dataString?

Note:

The byte order of the pixels are RGB(A) on macOS and BGR(A) on Windows.

The #data method gets the pixel data for an image in a string of bytes.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
byte_string = image_rep.data
byte_string.each_byte { |byte| puts byte, ' ' }

Returns:

Version:

  • SketchUp 2018

#heightInteger

The #height method returns the height of an image.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
image_rep.height

Returns:

  • (Integer)

Version:

  • SketchUp 2018

#load_file(filepath) ⇒ Object

The #load_file method loads image data from the specified file.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")

Parameters:

Raises:

  • (ArgumentError)

    if the filepath or image is invalid.

Version:

  • SketchUp 2018

#row_paddingInteger

The #row_padding method returns the size of the row padding of an image in bytes.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
image_rep.row_padding

Returns:

  • (Integer)

Version:

  • SketchUp 2018

#save_file(filepath) ⇒ Object

The #save_file method saves an image data object to an image file specified by a path.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image1.jpg")
# do stuff with the image representation
image_rep.save_file("/path/to/save/image2.jpg")

Parameters:

Version:

  • SketchUp 2018

#set_data(width, height, bits_per_pixel, row_padding, pixel_data) ⇒ Sketchup::ImageRep

Note:

The byte order of the pixels are RGB(A) on macOS and BGR(A) on Windows.

Note:

The encoding of the pixel_data String parameter should be ASCII-8BIT. Any other encoding could corrupt the binary data. Using `Array#pack(“C*”)` gives correct encoding.

The #set_data method discards any existing data and sets new pixel data for the Sketchup::ImageRep.

Examples:

Setting new data

image_rep = Sketchup::ImageRep.new
width = 800
height = 600
bpp = 24
pixel = [127, 127, 127].pack("C*")
pixels = pixel * width * height
image_rep.set_data(width, height, bpp, 0, pixels)
image_rep.save_file(UI.savepanel)

Handling system color differences

# Generates red image on Mac and blue on Windows.
image_rep = Sketchup::ImageRep.new
color = Sketchup::Color.new("Red")
rgba = color.to_a # Red, green , blue, alpha
color_data = rgba.pack("C*")
image_rep.set_data(1, 1, 32, 0, color_data)
image_rep.save_file(UI.savepanel)

# Generates red image on both systems.
image_rep = Sketchup::ImageRep.new
color = Sketchup::Color.new("Red")
color_code = color.to_a # Red, green, blue, alpha
if Sketchup.platform == :platform_win
  # Change order to Blue, green, red, alpha on Windows.
  color_code = color_code.values_at(2, 1, 0, 3)
end
color_data = color_code.pack("C*")
image_rep.set_data(1, 1, 32, 0, color_data)
image_rep.save_file(UI.savepanel)

Parameters:

  • width (Integer)

    The width of the pixel data. Must be greater than 0.

  • height (Integer)

    The height of the pixel data. Must be greater than 0.

  • bits_per_pixel (Integer)

    The bits per pixel for the pixel data. Must be either 8/24/32.

  • row_padding (Integer)

    The row padding for the pixel data which is sized in bytes. Row padding is used to pad each row with zeros so that each scanline on the pixel data will end on the data-type boundary.

  • pixel_data (String)

    The binary string containing the pixel data representing the new image.

Returns:

Raises:

  • (ArgumentError)

    If width, height, bits_per_pixel or pixel_data are invalid.

  • (TypeError)

    If width, height, bits_per_pixel or pixel_data are wrong data types.

Version:

  • SketchUp 2018

#sizeInteger

The #size method gets the total size of the image data in bytes.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
data_size = image_rep.size

Returns:

  • (Integer)

Version:

  • SketchUp 2018

#widthInteger

The #width method returns the width of an image.

Examples:

image_rep = Sketchup::ImageRep.new
image_rep.load_file("/path/to/image.jpg")
image_rep.width

Returns:

  • (Integer)

Version:

  • SketchUp 2018