<commit_before>// This file was developed by <NAME> <<EMAIL>>.
// It is published under the BSD 3-Clause License within the LICENSE file.

#include <tev/imageio/PfmImageLoader.h>
#include <tev/ThreadPool.h>

using namespace Eigen;
using namespace filesystem;
using namespace std;

TEV_NAMESPACE_BEGIN

bool PfmImageLoader::canLoadFile(istream& iStream) const {
    char b[2];
    iStream.read(b, sizeof(b));

    bool result = !!iStream && iStream.gcount() == sizeof(b) && b[0] == 'P' && (b[1] == 'F' || b[1] == 'f');

    iStream.clear();
    iStream.seekg(0);
    return result;
}

ImageData PfmImageLoader::load(istream& iStream, const path&, const string& channelSelector) const {
    ImageData result;
    ThreadPool threadPool;

    string magic;
    Vector2i size;
    float scale;

    iStream >> magic >> size.x() >> size.y() >> scale;

    if (magic != "PF" && magic != "Pf") {
        throw invalid_argument{tfm::format("Invalid magic PFM string %s", magic)};
    }

    if (!isfinite(scale) || scale == 0) {
        throw invalid_argument{tfm::format("Invalid PFM scale %f", scale)};
    }

    int numChannels = 1;
    if (magic[1] == 'F')
    {
        if (magic.size() == 3 && magic[2] == '4')
        {
            numChannels = 4;
        }
        else
        {
            numChannels = 3;
        }
    }
    
    bool isPfmLittleEndian = scale < 0;
    scale = abs(scale);

    vector<Channel> channels = makeNChannels(numChannels, size);

    auto numPixels = (DenseIndex)size.x() * size.y();
    if (numPixels == 0) {
        throw invalid_argument{"Image has zero pixels."};<commit_msg>Fix magic check<commit_after>// This file was developed by <NAME> <<EMAIL>>.
// It is published under the BSD 3-Clause License within the LICENSE file.

#include <tev/imageio/PfmImageLoader.h>
#include <tev/ThreadPool.h>

using namespace Eigen;
using namespace filesystem;
using namespace std;

TEV_NAMESPACE_BEGIN

bool PfmImageLoader::canLoadFile(istream& iStream) const {
    char b[2];
    iStream.read(b, sizeof(b));

    bool result = !!iStream && iStream.gcount() == sizeof(b) && b[0] == 'P' && (b[1] == 'F' || b[1] == 'f');

    iStream.clear();
    iStream.seekg(0);
    return result;
}

ImageData PfmImageLoader::load(istream& iStream, const path&, const string& channelSelector) const {
    ImageData result;
    ThreadPool threadPool;

    string magic;
    Vector2i size;
    float scale;

    iStream >> magic >> size.x() >> size.y() >> scale;

    if (magic != "PF" && magic != "Pf" && magic != "PF4") {
        throw invalid_argument{tfm::format("Invalid magic PFM string %s", magic)};
    }

    if (!isfinite(scale) || scale == 0) {
        throw invalid_argument{tfm::format("Invalid PFM scale %f", scale)};
    }

    int numChannels = 1;
    if (magic[1] == 'F')
    {
        if (magic.size() == 3 && magic[2] == '4')
        {
            numChannels = 4;
        }
        else
        {
            numChannels = 3;
        }
    }
    
    bool isPfmLittleEndian = scale < 0;
    scale = abs(scale);

    vector<Channel> channels = makeNChannels(numChannels, size);

    auto numPixels = (DenseIndex)size.x() * size.y();
    if (numPixels == 0) {
        throw invalid_argument{"Image has zero pixels."};
{'fraction_non_alphanumeric': 0.15175817396668723, 'fraction_numerical': 0.0107958050586058, 'mean_word_length': 2.7929824561403507, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, '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': '4914445', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1075, 'n_words': 285}