<commit_before>import {getSVGFilterMatrixValue} from '../../generators/svg-filter';
import {bgFetch} from './network';
import {getURLHostOrProtocol} from '../../utils/url';
import {loadAsDataURL} from '../../utils/network';
import {FilterConfig} from '../../definitions';

export interface ImageDetails {
    src: string;
    dataURL: string;
    width: number;
    height: number;
    isDark: boolean;
    isLight: boolean;
    isTransparent: boolean;
    isLarge: boolean;
}

export async function getImageDetails(url: string) {
    let dataURL: string;
    if (url.startsWith('data:')) {
        dataURL = url;
    } else {
        dataURL = await getImageDataURL(url);
    }
    const image = await urlToImage(dataURL);
    const info = analyzeImage(image);
    return {
        src: url,
        dataURL,
        width: image.naturalWidth,
        height: image.naturalHeight,
        ...info,
    };
}

async function getImageDataURL(url: string) {
    if (getURLHostOrProtocol(url) === (location.host || location.protocol)) {
        return await loadAsDataURL(url);
    }
    return await bgFetch({url, responseType: 'data-url'});
}

async function urlToImage(url: string) {
    return new Promise<HTMLImageElement>((resolve, reject) => {
        const image = new Image();
        image.onload = () => resolve(image);
        image.onerror = () => reject(`Unable to load image ${url}`);
        image.src = url;
    });
}

const MAX_ANALIZE_PIXELS_COUNT = 32 * 32;
let canvas: HTMLCanvasElement | OffscreenCanvas;
let context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;

function createCanvas() {
    const maxWidth = MAX_ANALIZE_PIXELS_COUNT;
    const maxHeight = MAX_ANALIZE_PIXELS_COUNT;
    canvas = document.createElement('canvas');
    canvas.width = maxWidth;
    canvas.height = maxHeight;
    context = canvas.getContext('2d');
    context.imageSmoothingEnabled = false;
}

function removeCanvas() {
    canvas = null;
    context = null;
}

function analyzeImage(image: HTMLImageElement) {
    if (!canvas) {
        createCanvas();
    }
    const {naturalWidth, naturalHeight} = image;
    const naturalPixelsCount = naturalWidth * naturalHeight;<commit_msg>Ignore non-existing images. (#3740)<commit_after>import {getSVGFilterMatrixValue} from '../../generators/svg-filter';
import {bgFetch} from './network';
import {getURLHostOrProtocol} from '../../utils/url';
import {loadAsDataURL} from '../../utils/network';
import {FilterConfig} from '../../definitions';
import {logWarn} from '../utils/log';

export interface ImageDetails {
    src: string;
    dataURL: string;
    width: number;
    height: number;
    isDark: boolean;
    isLight: boolean;
    isTransparent: boolean;
    isLarge: boolean;
}

export async function getImageDetails(url: string) {
    let dataURL: string;
    if (url.startsWith('data:')) {
        dataURL = url;
    } else {
        dataURL = await getImageDataURL(url);
    }
    const image = await urlToImage(dataURL);
    const info = analyzeImage(image);
    return {
        src: url,
        dataURL,
        width: image.naturalWidth,
        height: image.naturalHeight,
        ...info,
    };
}

async function getImageDataURL(url: string) {
    if (getURLHostOrProtocol(url) === (location.host || location.protocol)) {
        return await loadAsDataURL(url);
    }
    return await bgFetch({url, responseType: 'data-url'});
}

async function urlToImage(url: string) {
    return new Promise<HTMLImageElement>((resolve, reject) => {
        const image = new Image();
        image.onload = () => resolve(image);
        image.onerror = () => reject(`Unable to load image ${url}`);
        image.src = url;
    });
}

const MAX_ANALIZE_PIXELS_COUNT = 32 * 32;
let canvas: HTMLCanvasElement | OffscreenCanvas;
let context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;

function createCanvas() {
    const maxWidth = MAX_ANALIZE_PIXELS_COUNT;
    const maxHeight = MAX_ANALIZE_PIXELS_COUNT;
    canvas = document.createElement('canvas');
    canvas.width = maxWidth;
    canvas.height = maxHeight;
    context = canvas.getContext('2d');
    context.imageSmoothingEnabled = false;
}

function removeCanvas() {
    canvas = null;
    context = null;
}

function analyzeImage(image: HTMLImageElement) {
    if (!canvas) {
        createCanvas();
    }
    const {naturalWidth, naturalHeight} = image;
    if (naturalHeight === 0 || naturalWidth === 0) {
        logWarn(`logWarn(Image is empty ${image.currentSrc})`);
        return null;
    }
    const naturalPixelsCount = naturalWidth * naturalHeight;
{'fraction_non_alphanumeric': 0.12690134724033028, 'fraction_numerical': 0.004345936549326379, 'mean_word_length': 3.2898415657036346, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6119662', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1378, 'n_words': 335}