<commit_before>// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#undef PROFILER_ENABLED
#define PROFILER_ENABLED 1
#include "arch_specific.h"
#include "args.h"
#include "image.h"
#include "image_io.h"
#include "os_specific.h"
#include "padded_bytes.h"
#include "pik.h"
#include "pik_info.h"
#include "profiler.h"
#include "simd/dispatch.h"
#include "tsc_timer.h"

namespace pik {
namespace {

bool WriteFile(const PaddedBytes& compressed, const char* pathname) {
  FILE* f = fopen(pathname, "wb");
  if (f == nullptr) {
    fprintf(stderr, "Failed to open %s.\n", pathname);
    return false;
  }
  const size_t bytes_written =
      fwrite(compressed.data(), 1, compressed.size(), f);
  if (bytes_written != compressed.size()) {
    fprintf(stderr, "I/O error, only wrote %zu bytes.\n", bytes_written);
    return false;
  }
  fclose(f);
  return true;
}

struct CompressArgs {
  bool Init(int argc, char** argv) {
    for (int i = 1; i < argc; i++) {
      if (argv[i][0] == '-') {
        const std::string arg = argv[i];
        if (arg == "--fast") {
          params.fast_mode = true;
        } else if (arg == "--denoise") {
          if (!ParseOverride(argc, argv, &i, &params.denoise)) return false;
        } else if (arg == "--noise") {
          if (!ParseOverride(argc, argv, &i, &params.apply_noise)) return false;
        } else if (arg == "--num_threads") {
          if (!ParseUnsigned(argc, argv, &i, &num_threads)) return false;
        } else if (arg == "-v") {
          params.verbose = true;
        } else if (arg == "--print_profile") {
          if (!ParseOverride(argc, argv, &i, &print_profile)) return false;
        } else if (arg == "--distance") {
          if (!ParseFloat(argc, argv, &i, &params.butteraugli_distance)) {
            return false;
          }
          if (!(0.5f <= params.butteraugli_distance &&
                params.butteraugli_distance <= 3.0f)) {
            fprintf(stderr,
                    "Invalid/out of range distance '%s', try 0.5 to 3.\n",
                    argv[i]);
            return false;
          }
        } else if (arg == "--target_size") {
          if (!ParseUnsigned(argc, argv, &i, &params.target_size)) return false;
        } else {
          // Unknown arg or --help: caller will print help string
          return false;
        }
      } else {
        if (file_in == nullptr) {
          file_in = argv[i];
        } else if (file_out == nullptr) {
          file_out = argv[i];
        } else {
          fprintf(stderr, "Extra argument after in/out names: %s.\n", argv[i]);
          return false;
        }
      }
    }

    if (file_in == nullptr) {
      fprintf(stderr, "Missing input filename.\n");
      return false;
    }

    return true;
  }

  static const char* HelpFormatString() {
    return "Usage: %s in.png out.pik [--distance <maxError>] [--fast] "
           "[--denoise <0,1>] [--noise <0,1>] [--num_threads <0..N>\n"
           "[--print_profile <0,1>]\n"<commit_msg>Allow the use of --target_size<commit_after>// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

#undef PROFILER_ENABLED
#define PROFILER_ENABLED 1
#include "arch_specific.h"
#include "args.h"
#include "image.h"
#include "image_io.h"
#include "os_specific.h"
#include "padded_bytes.h"
#include "pik.h"
#include "pik_info.h"
#include "profiler.h"
#include "simd/dispatch.h"
#include "tsc_timer.h"

namespace pik {
namespace {

bool WriteFile(const PaddedBytes& compressed, const char* pathname) {
  FILE* f = fopen(pathname, "wb");
  if (f == nullptr) {
    fprintf(stderr, "Failed to open %s.\n", pathname);
    return false;
  }
  const size_t bytes_written =
      fwrite(compressed.data(), 1, compressed.size(), f);
  if (bytes_written != compressed.size()) {
    fprintf(stderr, "I/O error, only wrote %zu bytes.\n", bytes_written);
    return false;
  }
  fclose(f);
  return true;
}

struct CompressArgs {
  bool Init(int argc, char** argv) {
    bool distance_specified = false;
    bool target_size_specified = false;
    for (int i = 1; i < argc; i++) {
      if (argv[i][0] == '-') {
        const std::string arg = argv[i];
        if (arg == "--fast") {
          params.fast_mode = true;
        } else if (arg == "--denoise") {
          if (!ParseOverride(argc, argv, &i, &params.denoise)) return false;
        } else if (arg == "--noise") {
          if (!ParseOverride(argc, argv, &i, &params.apply_noise)) return false;
        } else if (arg == "--num_threads") {
          if (!ParseUnsigned(argc, argv, &i, &num_threads)) return false;
        } else if (arg == "-v") {
          params.verbose = true;
        } else if (arg == "--print_profile") {
          if (!ParseOverride(argc, argv, &i, &print_profile)) return false;
        } else if (arg == "--distance") {
          if (!ParseFloat(argc, argv, &i, &params.butteraugli_distance)) {
            return false;
          }
          if (!(0.5f <= params.butteraugli_distance &&
                params.butteraugli_distance <= 3.0f)) {
            fprintf(stderr,
                    "Invalid/out of range distance '%s', try 0.5 to 3.\n",
                    argv[i]);
            return false;
          }
          distance_specified = true;
        } else if (arg == "--target_size") {
          if (!ParseUnsigned(argc, argv, &i, &params.target_size)) return false;
          target_size_specified = true;
        } else {
          // Unknown arg or --help: caller will print help string
          return false;
        }
      } else {
        if (file_in == nullptr) {
          file_in = argv[i];
        } else if (file_out == nullptr) {
          file_out = argv[i];
        } else {
          fprintf(stderr, "Extra argument after in/out names: %s.\n", argv[i]);
          return false;
        }
      }
    }

    // The default distance is 1, and there is a check downstream that only one
    // of distance and target_size is specified. Thus, if target_size is
    // specified, we reset distance to -1 to avoid the error if it is
    // unwarranted.
    if (target_size_specified && !distance_specified) {
      params.butteraugli_distance = -1.0f;
    }

    if (file_in == nullptr) {
      fprintf(stderr, "Missing input filename.\n");
      return false;
    }

    return true;
  }

  static const char* HelpFormatString() {
    return "Usage: %s in.png out.pik [--distance <maxError>] [--fast] "
           "[--denoise <0,1>] [--noise <0,1>] [--num_threads <0..N>\n"
           "[--print_profile <0,1>]\n"
{'fraction_non_alphanumeric': 0.1427091427091427, 'fraction_numerical': 0.007252007252007252, 'mean_word_length': 2.3798687089715536, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5717309', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2409, 'n_words': 752}