<commit_before>#include "guetzli.hpp"

#include <guetzli/quality.h>
#include <guetzli/processor.h>

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>


// GuetzliImage

GuetzliImage* guetzliImageNew(GuetzliImageType type, int length) {
    GuetzliImage* image = (GuetzliImage*) malloc(sizeof(GuetzliImage));
    image->type = type;
    image->length = length;
    image->data = new char[image->length];
    return image;
}

void guetzliImageFree(GuetzliImage* image) {
    delete[] image->data;
    image->data = nullptr;
    image->length = 0;
}

GuetzliImage* guetzliImageReadFile(const char* filename, GuetzliImageType type) {
    std::ifstream inFile(filename, std::ios::binary);
    std::vector<char> inBuffer((std::istreambuf_iterator<char>(inFile)), (std::istreambuf_iterator<char>()));

    // The image indeed
    GuetzliImage* image = guetzliImageNew(type, inBuffer.size());
    memcpy(image->data, inBuffer.data(), image->length);
    return image;
}

void guetzliImageWriteFile(const char* filename, GuetzliImage* image) {
    std::ofstream outFile(filename, std::ios::binary);
    outFile.write(image->data, image->length);
}

GuetzliImage* guetzliImageOptimize(GuetzliImage* in, int quality) {
    if (in->type == GUETZLI_IMAGE_TYPE_PNG) {
        std::cerr << "PNG images not supported yet." << std::endl;
        exit(1);
    }

    std::string inData;
    std::string outData;
    inData.assign(in->data, in->length);

    guetzli::Params params;
    params.butteraugli_target = static_cast<float>(guetzli::ButteraugliScoreForQuality(quality));
    Process(params, nullptr, inData, &outData);<commit_msg>fixes GuetzliImage not freed<commit_after>#include "guetzli.hpp"

#include <guetzli/quality.h>
#include <guetzli/processor.h>

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>


// GuetzliImage

GuetzliImage* guetzliImageNew(GuetzliImageType type, int length) {
    GuetzliImage* image = (GuetzliImage*) malloc(sizeof(GuetzliImage));
    image->type = type;
    image->length = length;
    image->data = new char[image->length];
    return image;
}

void guetzliImageFree(GuetzliImage* image) {
    delete[] image->data;
    image->data = nullptr;
    image->length = 0;
    free(image);
}

GuetzliImage* guetzliImageReadFile(const char* filename, GuetzliImageType type) {
    std::ifstream inFile(filename, std::ios::binary);
    std::vector<char> inBuffer((std::istreambuf_iterator<char>(inFile)), (std::istreambuf_iterator<char>()));

    // The image indeed
    GuetzliImage* image = guetzliImageNew(type, inBuffer.size());
    memcpy(image->data, inBuffer.data(), image->length);
    return image;
}

void guetzliImageWriteFile(const char* filename, GuetzliImage* image) {
    std::ofstream outFile(filename, std::ios::binary);
    outFile.write(image->data, image->length);
}

GuetzliImage* guetzliImageOptimize(GuetzliImage* in, int quality) {
    if (in->type == GUETZLI_IMAGE_TYPE_PNG) {
        std::cerr << "PNG images not supported yet." << std::endl;
        exit(1);
    }

    std::string inData;
    std::string outData;
    inData.assign(in->data, in->length);

    guetzli::Params params;
    params.butteraugli_target = static_cast<float>(guetzli::ButteraugliScoreForQuality(quality));
    Process(params, nullptr, inData, &outData);
{'fraction_non_alphanumeric': 0.14417360285374553, 'fraction_numerical': 0.0011890606420927466, 'mean_word_length': 4.8117443868739205, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 53, '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': '5071755', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1139, 'n_words': 221}