import Jimp from 'jimp';
import { Pixel } from './main';

/**
 * 
 * @param name Filename.
 * @param heightScale Resulting image height will be `originalHeight * (1/(heightScale * widthScale))` (approximately)
 * @param widthScale Resulting image width will be `originalWidth * (1/widthScale)` (approximately)
 */
export default async function convert(name: string, heightScale: number = 1, widthScale: number = 1): Promise<Pixel[][]> {
	const img = await Jimp.read(name);

	const ary: Pixel[][] = [];

	for (let y = 0; y < img.getHeight(); y++) {
		ary[y] = [];
		if (y % heightScale == 0) {
			for (let x = 0; x < img.getWidth(); x++) {
				if (x % widthScale == 0) {
					const col = img.getPixelColor(x, y).toString(16).padStart(8, '0');
					ary[y][x] = {
						red: parseInt(col[0] + col[1], 16),
						green: parseInt(col[2] + col[3], 16),
						blue: parseInt(col[4] + col[5], 16)
					} as Pixel
				} else {
					ary[y][x] = null;
				}
			}
		}
	}

	return ary.filter(row => row.length !== 0).map(row => row.filter(cell => cell !== null));
}