<commit_before>'use strict';

import {getKind, getPixelArray} from './kind';
import {RGBA} from './kindNames';
import {Image, getImageData, Canvas, getCanvasArray} from './canvas';
import extend from './extend';
import {createWriteStream} from 'fs';
import * as ColorModels from './model/models';

let computedPropertyDescriptor = {
    configurable: true,
    enumerable: false,
    get: undefined
};

export default class IJ {
    constructor(width, height, data, options) {
        if (width === undefined) width = 1;
        if (height === undefined) height = 1;
        if (data && !data.length) {
            options = data;
            data = null;
        }
        if (options === undefined) options = {};

        if (!(width > 0))
            throw new RangeError('width must be greater than 0');
        if (!(height > 0))
            throw new RangeError('height must be greater than 0');

        this.width = width;
        this.height = height;

        let kind = options.kind || RGBA;
        if (typeof kind === 'string') kind = getKind(kind);
        if (!kind) throw new RangeError('invalid image kind: ' + kind);

        this.components = kind.components;
        this.alpha = kind.alpha;
        this.bitDepth = kind.bitDepth;
        this.colorModel = kind.colorModel;

        this.channels = this.components + this.alpha;
        this.maxValue = (1 << this.bitDepth) - 1;
        this.size = this.width * this.height;

        this.computed = {};


        let length = this.size * this.channels;
        if (!data)
            data = getPixelArray(kind, length);
        else if (data.length !== length)
            throw new RangeError(`incorrect data size. Expected ${length} but got ${data.length}`);

        this.data = data;
    }

    static load(url) {
        return new Promise(function (resolve, reject) {
            let image = new Image();

            // see https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
            image.crossOrigin = 'Anonymous';

            image.onload = function () {
                let w = image.width, h = image.height;
                let canvas = new Canvas(w, h);
                let ctx = canvas.getContext('2d');
                ctx.drawImage(image, 0, 0, w, h);
                let data = ctx.getImageData(0, 0, w, h).data;
                resolve(new IJ(w, h, data));
            };
            image.onerror = reject;
            image.src = url;
        });
    }

    static extendMethod(name, method, inplace = false, returnThis = true) {
        if (IJ.prototype.hasOwnProperty(name)) {
            console.warn(`Method '${name}' already exists and will be overwritten`);
        }
        if (inplace) {
            IJ.prototype[name] = function (...args) {
                // reset computed properties
                this.computed = {};
                let result = method.apply(this, args);
                if (returnThis)
                    return this;
                return result;
            };
        } else {
            IJ.prototype[name] = function (...args) {
                return method.apply(this, args);
            };
        }
        return IJ;
    }

    static extendProperty(name, method) {
        if (IJ.prototype.hasOwnProperty(name)) {
            console.warn(`Property getter '${name}' already exists and will be overwritten`);
        }
        computedPropertyDescriptor.get = function () {
            if (this.computed.hasOwnProperty(name)) {
                return this.computed[name];
            } else {
                let result = method.call(this);
                this.computed[name] = result;
                return result;
            }
        };
        Object.defineProperty(IJ.prototype, name, computedPropertyDescriptor);
        return IJ;
    }

    static createFrom(other, {
        width = other.width,
        height = other.height,
        kind = other.kind
        } = {}) {
        // TODO if kind is incomplete, take values from this
        return new IJ(width, height, {kind});
    }

    setValueXY(row, column, channel, value) {
        this.data[(row * this.width + column) * this.channels + channel] = value;
    }

    getValueXY(row, column, channel) {
        return this.data[(row * this.width + column) * this.channels + channel];
    }

    setMatrix(matrix, channel) {
        // the user is expected to know what he is doing !
        // we blinding put the matrix result
        for (let i = 0; i < this.width; i++) {
            for (let j = 0; j < this.height; j++) {
                for (let k = 0; k < this.channels; k++) {
                    if (channel) {
                        this.data[(j * this.width + i) * this.channels + channel] = matrix[i][j];
                    } else {
                        this.data[(j * this.width + i) * this.channels + k] = matrix[i][j][k];
                    }
                }
            }
        }
    }

    getMatrix(channel) {
        let matrix = new Array(this.width);
        for (let i = 0; i < this.width; i++) {
            matrix[i] = new Array(this.height);<commit_msg>Add setValue and getValue to access directly<commit_after>'use strict';

import {getKind, getPixelArray} from './kind';
import {RGBA} from './kindNames';
import {Image, getImageData, Canvas, getCanvasArray} from './canvas';
import extend from './extend';
import {createWriteStream} from 'fs';
import * as ColorModels from './model/models';

let computedPropertyDescriptor = {
    configurable: true,
    enumerable: false,
    get: undefined
};

export default class IJ {
    constructor(width, height, data, options) {
        if (width === undefined) width = 1;
        if (height === undefined) height = 1;
        if (data && !data.length) {
            options = data;
            data = null;
        }
        if (options === undefined) options = {};

        if (!(width > 0))
            throw new RangeError('width must be greater than 0');
        if (!(height > 0))
            throw new RangeError('height must be greater than 0');

        this.width = width;
        this.height = height;

        let kind = options.kind || RGBA;
        if (typeof kind === 'string') kind = getKind(kind);
        if (!kind) throw new RangeError('invalid image kind: ' + kind);

        this.components = kind.components;
        this.alpha = kind.alpha;
        this.bitDepth = kind.bitDepth;
        this.colorModel = kind.colorModel;

        this.channels = this.components + this.alpha;
        this.maxValue = (1 << this.bitDepth) - 1;
        this.size = this.width * this.height;

        this.computed = {};


        let length = this.size * this.channels;
        if (!data)
            data = getPixelArray(kind, length);
        else if (data.length !== length)
            throw new RangeError(`incorrect data size. Expected ${length} but got ${data.length}`);

        this.data = data;
    }

    static load(url) {
        return new Promise(function (resolve, reject) {
            let image = new Image();

            // see https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
            image.crossOrigin = 'Anonymous';

            image.onload = function () {
                let w = image.width, h = image.height;
                let canvas = new Canvas(w, h);
                let ctx = canvas.getContext('2d');
                ctx.drawImage(image, 0, 0, w, h);
                let data = ctx.getImageData(0, 0, w, h).data;
                resolve(new IJ(w, h, data));
            };
            image.onerror = reject;
            image.src = url;
        });
    }

    static extendMethod(name, method, inplace = false, returnThis = true) {
        if (IJ.prototype.hasOwnProperty(name)) {
            console.warn(`Method '${name}' already exists and will be overwritten`);
        }
        if (inplace) {
            IJ.prototype[name] = function (...args) {
                // reset computed properties
                this.computed = {};
                let result = method.apply(this, args);
                if (returnThis)
                    return this;
                return result;
            };
        } else {
            IJ.prototype[name] = function (...args) {
                return method.apply(this, args);
            };
        }
        return IJ;
    }

    static extendProperty(name, method) {
        if (IJ.prototype.hasOwnProperty(name)) {
            console.warn(`Property getter '${name}' already exists and will be overwritten`);
        }
        computedPropertyDescriptor.get = function () {
            if (this.computed.hasOwnProperty(name)) {
                return this.computed[name];
            } else {
                let result = method.call(this);
                this.computed[name] = result;
                return result;
            }
        };
        Object.defineProperty(IJ.prototype, name, computedPropertyDescriptor);
        return IJ;
    }

    static createFrom(other, {
        width = other.width,
        height = other.height,
        kind = other.kind
        } = {}) {
        // TODO if kind is incomplete, take values from this
        return new IJ(width, height, {kind});
    }

    setValueXY(row, column, channel, value) {
        this.data[(row * this.width + column) * this.channels + channel] = value;
    }

    getValueXY(row, column, channel) {
        return this.data[(row * this.width + column) * this.channels + channel];
    }

    setValue(target, channel, value) {
        this.data[target * this.channels + channel] = value;
    }

    getValue(target, channel) {
        return this.data[target * this.channels + channel];
    }

    setMatrix(matrix, channel) {
        // the user is expected to know what he is doing !
        // we blinding put the matrix result
        for (let i = 0; i < this.width; i++) {
            for (let j = 0; j < this.height; j++) {
                for (let k = 0; k < this.channels; k++) {
                    if (channel) {
                        this.data[(j * this.width + i) * this.channels + channel] = matrix[i][j];
                    } else {
                        this.data[(j * this.width + i) * this.channels + k] = matrix[i][j][k];
                    }
                }
            }
        }
    }

    getMatrix(channel) {
        let matrix = new Array(this.width);
        for (let i = 0; i < this.width; i++) {
            matrix[i] = new Array(this.height);
{'fraction_non_alphanumeric': 0.12498805542283803, 'fraction_numerical': 0.003248924988055423, 'mean_word_length': 1.7064908197569175, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '6452730', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2821, 'n_words': 804}