# File lib/native_file_types/apple2/AppleHiResPic.rb, line 61
def self.buffer_to_picture(buffer)
  canvas = PNG::Canvas.new HGR_COLS, HGR_ROWS, PNG::Color::Black
  HGR_ROWS.times do |y|
    last_bit_set=false
    40.times 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

      7.times 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_COLS-1
          end
        end
        
        last_bit_set=bit_set
      end
    end
  end
  
  png = PNG.new canvas
  result=png.raw_bytes
  result
end