<commit_before>import DependencyTracker from '../../utils/DependencyTracker';
import Program from './glutil/Program';
import Texture from './glutil/Texture';
import * as shader from './shader';
import * as utils from '../../utils';

const TEXTURE_UNIT = 0;

export default class ImageLayer {
    constructor(url, gl, projectionMatrix, transformationMatrix) {
        this._url = url;
        this._gl = gl;
        this._projectionMatrix = projectionMatrix;
        this._transformationMatrix = transformationMatrix;
        this._dependencyTracker = new DependencyTracker();
        this._program = new Program(gl, shader.vsh.imagelayer, shader.fsh.imagelayer);

        this._imageHeight = 0;
        this._imageWidth = 0;
        this._textureHeight = 0;
        this._textureWidth = 0;

        this._vertexBuffer = null;
        this._textureCoordinateBuffer = null;
        this._texture = null;

        this._isReady = false;

        this._readyPromise = this._init();
    }

    _init() {
        return this._loadImageData()
            .then((imageData) => {
                this._createTexture(imageData);
                this._createVertexBuffer();
                this._createTextureCoordinateBuffer();

                this._isReady = true;
            });
    }

    _loadImageData() {
        return utils.loadImage(this._url)
            .then((image) => {
                    const paddedWidth = Math.pow(2, Math.floor(Math.log(image.width) / Math.log(2)) + 1),
                        paddedHeight = Math.pow(2, Math.floor(Math.log(image.width) / Math.log(2)) + 1),
                        canvas = document.createElement('canvas'),
                        ctx = canvas.getContext('2d');

                    canvas.width = this._textureWidth = paddedWidth;
                    canvas.height = this._textureHeight = paddedHeight;

                    this._imageWidth = image.width;
                    this._imageHeight = image.height;

                    ctx.drawImage(image,
                        0, 0, image.width, image.height,
                        0, paddedHeight - image.height, image.width, image.height);

                    // Simulate CLAMP_TO_EDGE for the open ends of the padded texture ---
                    // otherwise, sampling at the edges will give a frame effect.
                    for (let i = 0; i < paddedWidth - image.width; i++) {
                        ctx.drawImage(image,
                            image.width - 1, 0, 1, image.height,
                            image.width + i, paddedHeight - image.height, 1, image.height);
                    }

                    // dito
                    for (let i = 0; i < paddedHeight - image.height; i++) {
                        ctx.drawImage(image,
                            0, 0, image.width, 1,
                            0, paddedHeight - image.height - i, image.width, 1
                        );
                    }

                    return canvas;
                });
    }

    _createVertexBuffer() {
        const gl = this._gl,<commit_msg>Include corner in clamp simulation for completeness sake.<commit_after>import DependencyTracker from '../../utils/DependencyTracker';
import Program from './glutil/Program';
import Texture from './glutil/Texture';
import * as shader from './shader';
import * as utils from '../../utils';

const TEXTURE_UNIT = 0;

export default class ImageLayer {
    constructor(url, gl, projectionMatrix, transformationMatrix) {
        this._url = url;
        this._gl = gl;
        this._projectionMatrix = projectionMatrix;
        this._transformationMatrix = transformationMatrix;
        this._dependencyTracker = new DependencyTracker();
        this._program = new Program(gl, shader.vsh.imagelayer, shader.fsh.imagelayer);

        this._imageHeight = 0;
        this._imageWidth = 0;
        this._textureHeight = 0;
        this._textureWidth = 0;

        this._vertexBuffer = null;
        this._textureCoordinateBuffer = null;
        this._texture = null;

        this._isReady = false;

        this._readyPromise = this._init();
    }

    _init() {
        return this._loadImageData()
            .then((imageData) => {
                this._createTexture(imageData);
                this._createVertexBuffer();
                this._createTextureCoordinateBuffer();

                this._isReady = true;
            });
    }

    _loadImageData() {
        return utils.loadImage(this._url)
            .then((image) => {
                    const paddedWidth = Math.pow(2, Math.floor(Math.log(image.width) / Math.log(2)) + 1),
                        paddedHeight = Math.pow(2, Math.floor(Math.log(image.width) / Math.log(2)) + 1),
                        canvas = document.createElement('canvas'),
                        ctx = canvas.getContext('2d');

                    canvas.width = this._textureWidth = paddedWidth;
                    canvas.height = this._textureHeight = paddedHeight;

                    this._imageWidth = image.width;
                    this._imageHeight = image.height;

                    ctx.drawImage(image,
                        0, 0, image.width, image.height,
                        0, paddedHeight - image.height, image.width, image.height);

                    // Simulate CLAMP_TO_EDGE for the open ends of the padded texture ---
                    // otherwise, sampling at the edges will give a frame effect.
                    for (let i = 0; i < paddedWidth - image.width; i++) {
                        ctx.drawImage(image,
                            image.width - 1, 0, 1, image.height,
                            image.width + i, paddedHeight - image.height, 1, image.height);
                    }

                    // dito
                    for (let i = 0; i < paddedHeight - image.height; i++) {
                        ctx.drawImage(image,
                            0, 0, image.width, 1,
                            0, paddedHeight - image.height - i, image.width, 1
                        );
                    }

                    // dito --- the top right corner fully degenerates to the color
                    // of the top right pixel
                    let topRightPixel =
                        ctx.getImageData(image.width - 1, paddedHeight - image.height, 1, 1).data;
                    ctx.fillStyle = `rgb(${topRightPixel[0]},${topRightPixel[1]},${topRightPixel[2]})`;
                    ctx.fillRect(image.width, 0, paddedWidth - image.width, paddedHeight - image.height);

                    return canvas;
                });
    }

    _createVertexBuffer() {
        const gl = this._gl,
{'fraction_non_alphanumeric': 0.10576057605760576, 'fraction_numerical': 0.00885088508850885, 'mean_word_length': 1.48397913561848, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2954705', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1649, 'n_words': 399}