# File lib/native_file_types/apple2/AppleHiResPic.rb, line 81
def to_png
  if file_system_image.file_system==AppleDos then
    buffer=contents[4,contents.length-4]
  else
    buffer=contents
  end

  canvas = PNG::Canvas.new HGR_COLS*SCALE, HGR_ROWS*SCALE, PNG::Color::Black
  0.upto(HGR_ROWS-1) do |y|
    last_bit_set=false
    0.upto(39) do |x_byte|
      offset=@@scanline_offsets[y]+x_byte
      current_byte=buffer[offset]                               
      current_byte=0 if current_byte.nil? #if we overrun the buffer then assume it's black
      0.upto(6) do |x_bit|
        x=x_byte*7+x_bit
        bit_set=((current_byte & (2**x_bit))>0)
        if (bit_set) then
          if (last_bit_set) then 
            #adjacent pixels should both be white
            AppleHiResPic.set_pixel(canvas,x-1,y,HGR_WHITE) 
            AppleHiResPic.set_pixel(canvas,x,y,HGR_WHITE)
          else
            if current_byte>=0x80 then
              pallete=[HGR_BLUE,HGR_ORANGE]
            else
              pallete=[HGR_VIOLET,HGR_GREEN]
            end
            this_pixel_colour=pallete[x%2]
            AppleHiResPic.set_pixel(canvas,x,y,this_pixel_colour)
            AppleHiResPic.set_pixel(canvas,x+1,y,this_pixel_colour) unless x>=HGR_ROWS
          end
        end
        last_bit_set=bit_set
      end
    end
  end
  
  png = PNG.new canvas
  result=png.raw_bytes
  result
end