Class: Pixels::TargaBase

Inherits:
Object
  • Object
show all
Defined in:
lib/pixels.rb

Overview

Abstract class

Direct Known Subclasses

Targa15, Targa16, Targa24, Targa32

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, header, instance_vars, bytes_per_pixel) ⇒ TargaBase

Do not instantiate this object directly. Use from_file.



262
263
264
265
266
267
268
269
270
271
# File 'lib/pixels.rb', line 262

def initialize(file, header, instance_vars, bytes_per_pixel)
  @mutex = Mutex.new    # Obtain this lock whenever you use @file
  @file = file
  @header = header
  for k, v in instance_vars
    instance_variable_set("@#{k.to_s}", v)
  end
  @bytes_per_pixel = bytes_per_pixel
  @bytes_per_row = bytes_per_pixel * @width
end

Instance Attribute Details

#alpha_depthObject (readonly)

Bit-depth of the alpha channel (bits per pixel)



255
256
257
# File 'lib/pixels.rb', line 255

def alpha_depth
  @alpha_depth
end

#bppObject (readonly)

Number of bits used to store each pixel



249
250
251
# File 'lib/pixels.rb', line 249

def bpp
  @bpp
end

#color_depthObject (readonly)

Color-depth of the image (bits per pixel)



252
253
254
# File 'lib/pixels.rb', line 252

def color_depth
  @color_depth
end

#heightObject (readonly)

Height of the image (pixels)



246
247
248
# File 'lib/pixels.rb', line 246

def height
  @height
end

#originObject (readonly)

Indicates which pixel appears first in the TGA file. One of :UPPER_LEFT or :LOWER_LEFT.



259
260
261
# File 'lib/pixels.rb', line 259

def origin
  @origin
end

#widthObject (readonly)

Width of the image (pixels)



243
244
245
# File 'lib/pixels.rb', line 243

def width
  @width
end

Instance Method Details

#closeObject

Close the underlying file.



311
312
313
314
315
316
317
# File 'lib/pixels.rb', line 311

def close
  @mutex.synchronize {
    @file.close
    @file = nil
    @mutex = nil
  }
end

#each_row_rgbObject

Iterate through each row of the image, representing each pixel as an RGB value.

For each y-coordinate in the image, this method calls the given block with two arguments: get_row_rgb(y) and y.

If no block is provided, an Enumerator is returned.



326
327
328
329
330
331
# File 'lib/pixels.rb', line 326

def each_row_rgb
  return Enumerable::Enumerator.new(self, :each_row_rgb) unless block_given?
  for y in (0..@height-1)
    yield get_row_rgb(y), y
  end
end

#each_row_rgbaObject

Iterate through each row of the image, representing each pixel as an RGBA value.

For each y-coordinate in the image, this method calls the given block with two arguments: get_row_rgba(y) and y.

If no block is provided, an Enumerator is returned.



340
341
342
343
344
345
# File 'lib/pixels.rb', line 340

def each_row_rgba
  return Enumerable::Enumerator.new(self, :each_row_rgba) unless block_given?
  for y in (0..@height-1)
    yield get_row_rgba(y), y
  end
end

#get_row_rgb(y) ⇒ Object

Return the row of pixels having the specified y-coordinate. The row is represented as an array of [r, g, b] values for each pixel in the row.

Each r, g, b value is an integer between 0 and 255.



351
352
353
# File 'lib/pixels.rb', line 351

def get_row_rgb(y)
  return get_row(y).map { |color| rgb_from_color(color) }
end

#get_row_rgba(y) ⇒ Object

Return the row of pixels having the specified y-coordinate. The row is represented as an array of [r, g, b, a] values for each pixel in the row.

Each r, g, b, a value is an integer between 0 and 255.



359
360
361
# File 'lib/pixels.rb', line 359

def get_row_rgba(y)
  return get_row(y).map { |color| rgba_from_color(color) }
end

#put_row_rgb(y, row_rgb) ⇒ Object

Replace the row of pixels having the specified y-coordinate. The row is represented as an array of [r, g, b] values for each pixel in the row.

Each r, g, b value is an integer between 0 and 255.



367
368
369
# File 'lib/pixels.rb', line 367

def put_row_rgb(y, row_rgb)
  return put_row(y, row_rgb.map { |r, g, b| color_from_rgb(r, g, b) })
end

#put_row_rgba(y, row_rgba) ⇒ Object

Replace the row of pixels having the specified y-coordinate. The row is represented as an array of [r, g, b, a] values for each pixel in the row.

Each r, g, b, a value is an integer between 0 and 255.



375
376
377
# File 'lib/pixels.rb', line 375

def put_row_rgba(y, row_rgba)
  return put_row(y, row_rgba.map { |r, g, b, a| color_from_rgba(r, g, b, a) })
end

#read_row_bytes(y) ⇒ Object

Return a string containing the raw bytes from the row at the specified y-coordinate.

You probably want to use get_row_rgb or get_row_rgba instead.



289
290
291
292
293
294
# File 'lib/pixels.rb', line 289

def read_row_bytes(y)
  @mutex.synchronize {
    @file.seek(row_offset(y), IO::SEEK_SET)
    return @file.read(@bytes_per_row)
  }
end

#specObject

Return a Hash containing the file format specification, which can be used as the “spec” parameter in Pixels::create_tga.



275
276
277
278
279
280
281
282
283
# File 'lib/pixels.rb', line 275

def spec
  return {
    :width => width,
    :height => height,
    :color_depth => color_depth,
    :has_alpha => has_alpha?,
    :origin => origin,
  }
end

#write_row_bytes(y, raw_data) ⇒ Object

Write a string containing the raw bytes for a row Return a string containing the raw bytes from the row at the specified y-coordinate.

You probably want to use put_row_rgb or put_row_rgba instead.



300
301
302
303
304
305
306
307
308
# File 'lib/pixels.rb', line 300

def write_row_bytes(y, raw_data)
  if raw_data.length != @bytes_per_row
    raise ArgumentError.new("raw_data.length was #{raw_data.length}, expected #{@bytes_per_row}")
  end
  @mutex.synchronize {
    @file.seek(row_offset(y), IO::SEEK_SET)
    return @file.write(raw_data)
  }
end