144: def self.create_tga(file_or_path, spec={})
145: spec = {
146: :width => nil,
147: :height => nil,
148: :color_depth => nil,
149: :has_alpha => false,
150: :origin => :UPPER_LEFT,
151: }.merge(spec)
152:
153: case [spec[:color_depth], !!spec[:has_alpha]]
154: when [15, true]
155: bpp = 16
156: alpha_depth = 1
157: when [16, false]
158: bpp = 16
159: alpha_depth = 0
160: when [24, false]
161: bpp = 24
162: alpha_depth = 0
163: when [24, true]
164: bpp = 32
165: alpha_depth = 8
166: else
167: raise ArgumentError.new(
168: ":depth=#{colour_depth}-bpp with :alpha=#{has_alpha} not supported")
169: end
170:
171: image_descriptor = alpha_depth
172: case spec[:origin]
173: when :LOWER_LEFT
174:
175: when :UPPER_LEFT
176: image_descriptor |= 0x20
177: else
178: raise ArgumentError.new(":origin must be :LOWER_LEFT or :UPPER_LEFT")
179: end
180:
181: raw_header = [
182: 0,
183: 0,
184: 2,
185: 0,
186: 0,
187: 0,
188: 0,
189: 0,
190: spec[:width],
191: spec[:height],
192: bpp,
193: image_descriptor,
194: ].pack("CCCvvCvvvvCC")
195:
196: h, r = decode_tga_header(raw_header)
197:
198: if file_or_path.respond_to?(:write)
199: file = file_or_path
200: else
201: file = File.open(file_or_path, "w+b:binary")
202: end
203:
204: file.write(raw_header)
205: file.seek(0, IO::SEEK_SET)
206: return open_tga(file)
207: end