export (GD2::Image)

    # File lib/gd2/image.rb, line 446
        def export(filename, options = {})
          unless format = options.delete(:format)
            md = filename.match /\.([^.]+)\z/
            format = md ? md[1].downcase : nil
          end
          format = format.to_sym if format

          case format
          when :jpeg, :jpg
            write_sym = :gdImageJpeg
            args = [nil, options.delete(:quality) || -1]
          when :png
            write_sym = :gdImagePngEx
            args = [nil, options.delete(:level) || -1]
          when :gif
            write_sym = :gdImageGif
            args = [nil]
          when :wbmp
            write_sym = :gdImageWBMP
            fgcolor = options.delete(:fgcolor)
            raise ArgumentError, 'Missing required option :fgcolor' if fgcolor.nil?
            args = [color2pixel(fgcolor), nil]
          when :gd
            write_sym = :gdImageGd
            args = [nil]
          when :gd2
            write_sym = :gdImageGd2
            args = [nil, options.delete(:chunk_size) || 0,
              options.delete(:fmt) || FMT_COMPRESSED]
          else
            raise UnrecognizedImageTypeError,
              'Format (or file extension) is not recognized'
          end

          raise ArgumentError, "Unrecognized options #{options.inspect}" unless
            options.empty?

          File.open(filename, 'wb') do |file|
            args[args[0].nil? ? 0 : 1] = file
            SYM[write_sym].call(image_ptr, *args)
            file.pos
          end
        end