src/tt_bitmap2mesh/image/bmp.rb
def write_stream(stream)
      # DIB Header
      dib_header = [
        4 + 2 + 2 + 2 + 2, # bcSize
        @width,            # bcWidth
        @height,           # bcHeight
        1,                 # bcPlanes
        @bitspp            # bcBitCount
      ].pack('Vvvvv')

      # Color Palette
      if bitspp < 16
        raise NotImplementedError, 'No palettes supported'
      end

      # Pixel Array
      row_size = ((@bitspp * @width + 31) / 32) * 4
      row_pixel_size = @width * 3
      row_padding = Array.new(row_size - row_pixel_size, 0)
      pixels = []
      @height.times { |y|
        @width.times { |x|
          color = self[x, y]
          bgr = color.to_a[0..2].reverse!
          pixels.concat(bgr)
        }
        pixels.concat(row_padding)
      }
      pixel_array = pixels.pack('C*')

      # BMP File Header
      filesize = BMP_HEADER_SIZE + dib_header.size + pixel_array.size
      bmp_file_header = [
          MAGIC_MARKER_WORD, # bfType
          filesize,          # bfSize
          0,                 # bfReserved1
          0,                 # bfReserved2
          BMP_HEADER_SIZE + dib_header.size # bfOffBits
      ].flatten.pack('vVvvV')

      stream.write(bmp_file_header)
      stream.write(dib_header)
      stream.write(pixel_array)
    end
# TODO: Currently saves OS/2 24bit BMPs only - no palette.
# @param [IO] stream
Currently saves OS/2 24bit BMPs only - no palette.
Currently saves OS / 2 24bit BMPs only - no palette .
def write_stream(stream)
      dib_header = [
        4 + 2 + 2 + 2 + 2, 
        @width,            
        @height,           
        1,                 
        @bitspp            
      ].pack('Vvvvv')
      if bitspp < 16
        raise NotImplementedError, 'No palettes supported'
      end
      row_size = ((@bitspp * @width + 31) / 32) * 4
      row_pixel_size = @width * 3
      row_padding = Array.new(row_size - row_pixel_size, 0)
      pixels = []
      @height.times { |y|
        @width.times { |x|
          color = self[x, y]
          bgr = color.to_a[0..2].reverse!
          pixels.concat(bgr)
        }
        pixels.concat(row_padding)
      }
      pixel_array = pixels.pack('C*')
      filesize = BMP_HEADER_SIZE + dib_header.size + pixel_array.size
      bmp_file_header = [
          MAGIC_MARKER_WORD, 
          filesize,          
          0,                 
          0,                 
          BMP_HEADER_SIZE + dib_header.size 
      ].flatten.pack('vVvvV')
      stream.write(bmp_file_header)
      stream.write(dib_header)
      stream.write(pixel_array)
    end
def write_stream ( stream ) dib_header = [ 4 + 2 + 2 + 2 + 2 , @width , @height , 1 , @bitspp ] . pack ( 'Vvvvv' ) if bitspp < 16 raise NotImplementedError , 'No palettes supported' end row_size = ( ( @bitspp * @width + 31 ) / 32 ) * 4 row_pixel_size = @width * 3 row_padding = Array . new ( row_size - row_pixel_size , 0 ) pixels = [ ] @height . times { | y | @width . times { | x | color = self [ x , y ] bgr = color . to_a [ 0 .. 2 ] . reverse! pixels . concat ( bgr ) } pixels . concat ( row_padding ) } pixel_array = pixels . pack ( 'C*' ) filesize = BMP_HEADER_SIZE + dib_header . size + pixel_array . size bmp_file_header = [ MAGIC_MARKER_WORD , filesize , 0 , 0 , BMP_HEADER_SIZE + dib_header . size ] . flatten . pack ( 'vVvvV' ) stream . write ( bmp_file_header ) stream . write ( dib_header ) stream . write ( pixel_array ) end
TODO: Currently saves OS/2 24bit BMPs only - no palette.
TODO : Currently saves OS / 2 24bit BMPs only - no palette .
# DIB Header # bcSize # bcWidth # bcHeight # bcPlanes # bcBitCount # Color Palette # Pixel Array # BMP File Header # bfType # bfSize # bfReserved1 # bfReserved2 # bfOffBits
stream
None