Class: PSD::File

Inherits:
File
  • Object
show all
Defined in:
lib/psd/file.rb

Overview

An extension of the built-in Ruby File class that adds numerous helpers for reading/writing binary data.

Constant Summary collapse

FORMATS =

All of the formats and their pack codes that we want to be able to convert into methods for easy reading/writing.

{
  ulonglong: {
    length: 8,
    code: 'Q>'
  },
  longlong: {
    length: 8,
    code: 'q>'
  },
  double: {
    length: 8,
    code: 'G'
  },
  float: {
    length: 4,
    code: 'F'
  },
  uint: {
    length: 4,
    code: 'L>'
  },
  int: {
    length: 4,
    code: 'l>'
  },
  ushort: {
    length: 2,
    code: 'S>'
  },
  short: {
    length: 2,
    code: 's>'
  }
}

Instance Method Summary collapse

Instance Method Details

#read_booleanObject

Reads a boolean value.



92
93
94
# File 'lib/psd/file.rb', line 92

def read_boolean
  read_byte != 0
end

#read_byteObject



78
79
80
# File 'lib/psd/file.rb', line 78

def read_byte
  read(1).bytes.to_a[0]
end

#read_path_numberObject

Adobe’s lovely signed 32-bit fixed-point number with 8bits.24bits

http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/PhotoshopFileFormats.htm#50577409_17587


54
55
56
57
# File 'lib/psd/file.rb', line 54

def read_path_number
  read(1).unpack('c*')[0].to_f +
    (read(3).unpack('B*')[0].to_i(2).to_f / (2 ** 24)).to_f # pre-decimal point
end

#read_space_colorObject

Reads a 32-bit color space value.



97
98
99
100
101
102
103
104
105
# File 'lib/psd/file.rb', line 97

def read_space_color
  color_space = read_short
  color_components = []
  4.times.map do |i|
    color_components.push(read_short >> 8)
  end

  { color_mode: color_space, color_components: color_components }
end

#read_string(length = nil) ⇒ Object

Reads a string of the given length and converts it to UTF-8 from the internally used MacRoman encoding.



73
74
75
76
# File 'lib/psd/file.rb', line 73

def read_string(length=nil)
  length = @file.read(1).bytes.to_a[0] if length.nil?
  read(length).delete("\000")
end

#read_unicode_string(length = nil) ⇒ Object

Reads a unicode string, which is double the length of a normal string and encoded as UTF-16.



83
84
85
86
87
88
89
# File 'lib/psd/file.rb', line 83

def read_unicode_string(length=nil)
  length ||= read_int if length.nil?
  return '' if length.nil? || length <= 0
  read(length * 2)
    .encode('UTF-8', 'UTF-16BE', universal_newline: true)
    .delete("\000")
end

#write_path_number(num) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/psd/file.rb', line 59

def write_path_number(num)
  write [num.to_i].pack('C')

  # Now for the fun part.
  # We first convert the decimal to be a whole number representing a
  # fraction with the denominator of 2^24
  # Next, we write that number as a 24-bit integer to the file
  binary_numerator = ((num - num.to_i).abs * 2 ** 24).to_i
  write [binary_numerator >> 16].pack('C')
  write [binary_numerator >> 8].pack('C')
  write [binary_numerator >> 0].pack('C')
end