<commit_before>require "png_quantizator/version"
require 'open3'
require 'fileutils'
require 'securerandom'
require 'tempfile'

module PngQuantizator
  class PngQuantError < StandardError; end

  class Image
    def initialize(file_path)
      raise ArgumentError, "could not find #{file_path}" unless File.file?(file_path)

      @file_path = file_path
    end

    def quantize!
      Tempfile.open([SecureRandom.hex(16), ".png"]) do |temp_path|
        res = quantize_to(temp_path)
        FileUtils.cp(temp_path, @file_path)

        temp_path.close
        temp_path.delete
        res
      end
    end

    def quantize_to(destination_path)
      raise PngQuantError, "pngquant not found in PATH=#{ENV['PATH']}" unless which("pngquant")

      exit_code, err_msg = Open3.popen3("pngquant 256") do |stdin, stdout, stderr, wait_thr|
        stdin.write(File.read(@file_path))

        File.open(destination_path, "w") do |f|
          f.write stdout.gets(nil)
          f.flush<commit_msg>Allow different number of color reduction<commit_after>require "png_quantizator/version"
require 'open3'
require 'fileutils'
require 'securerandom'
require 'tempfile'

module PngQuantizator
  class PngQuantError < StandardError; end

  class Image
    def initialize(file_path)
      raise ArgumentError, "could not find #{file_path}" unless File.file?(file_path)

      @file_path = file_path
    end

    def quantize!(colors = 256)
      Tempfile.open([SecureRandom.hex(16), ".png"]) do |temp_path|
        res = quantize_to(temp_path, colors)
        FileUtils.cp(temp_path, @file_path)

        temp_path.close
        temp_path.delete
        res
      end
    end

    def quantize_to(destination_path, colors = 256)
      raise PngQuantError, "pngquant not found in PATH=#{ENV['PATH']}" unless which("pngquant")

      exit_code, err_msg = Open3.popen3("pngquant #{colors}") do |stdin, stdout, stderr, wait_thr|
        stdin.write(File.read(@file_path))

        File.open(destination_path, "w") do |f|
          f.write stdout.gets(nil)
          f.flush
{'fraction_non_alphanumeric': 0.1054421768707483, 'fraction_numerical': 0.00923226433430515, 'mean_word_length': 3.2541322314049586, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1269269', 'n_tokens_mistral': 688, 'n_tokens_neox': 661, 'n_words': 156}