def to_picture
canvas = PNG::Canvas.new picture_width, picture_height, TMS9918_PALLETE[0]
colour_attributes_filename=filename.sub(/_P$/,'_C')
colour_attributes_file=file_system_image.files[colour_attributes_filename]
if colour_attributes_file.nil? || colour_attributes_file.contents.length<6144 then
colour_attributes=(0x0F.chr)*6144
else
colour_attributes=colour_attributes_file.contents
end
picture_width.times do |x|
picture_height.times do |y|
byte_offset=y<<5
byte_offset|=y
byte_offset&=0xFF07
bit_offset=x % 0x08
byte_offset+=x
byte_offset-=bit_offset
bitmask=1<<(7-bit_offset)
bitmap_byte=contents[byte_offset]
colour_byte=colour_attributes[byte_offset]
foreground_colour=TMS9918_PALLETE[colour_byte>>4]
background_colour=TMS9918_PALLETE[colour_byte%16]
if ((bitmap_byte & bitmask)==bitmask) then
canvas[x, y]= foreground_colour
else
canvas[x, y]= background_colour
end
end
end
png = PNG.new canvas
result=png.raw_bytes
result
end