2023-07-02 19:34:34 -04:00
|
|
|
import { histIndex, imageIndex } from "./0-utility";
|
2023-07-20 19:55:26 -04:00
|
|
|
import {
|
|
|
|
camera,
|
|
|
|
transform1Weight,
|
|
|
|
transform2Weight,
|
|
|
|
transform3Weight,
|
|
|
|
} from "./2a-variations";
|
|
|
|
import {
|
|
|
|
TransformPost,
|
|
|
|
transform1Post,
|
|
|
|
transform2Post,
|
|
|
|
transform3Post,
|
|
|
|
} from "./2b-post";
|
|
|
|
import { RendererFinal, transformFinal } from "./2c-final";
|
2023-07-02 19:34:34 -04:00
|
|
|
|
2023-07-20 19:55:26 -04:00
|
|
|
export class RendererHistogram extends RendererFinal {
|
|
|
|
protected histogram: number[] = [];
|
2023-07-02 19:34:34 -04:00
|
|
|
|
|
|
|
constructor(
|
2023-07-20 19:55:26 -04:00
|
|
|
size: number,
|
|
|
|
transforms: [number, TransformPost][],
|
|
|
|
final: TransformPost
|
2023-07-02 19:34:34 -04:00
|
|
|
) {
|
2023-07-20 19:55:26 -04:00
|
|
|
super(size, transforms, final);
|
|
|
|
|
|
|
|
for (var i = 0; i < this.size * this.size; i++) {
|
2023-07-02 19:34:34 -04:00
|
|
|
this.histogram.push(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 19:55:26 -04:00
|
|
|
plot(x: number, y: number): void {
|
|
|
|
[x, y] = this.final.apply(x, y);
|
|
|
|
const [pixelX, pixelY] = camera(x, y, this.size);
|
2023-07-02 19:34:34 -04:00
|
|
|
|
|
|
|
if (
|
|
|
|
pixelX < 0 ||
|
2023-07-20 19:55:26 -04:00
|
|
|
pixelY >= this.size ||
|
2023-07-02 19:34:34 -04:00
|
|
|
pixelY < 0 ||
|
2023-07-20 19:55:26 -04:00
|
|
|
pixelY >= this.size
|
2023-07-02 19:34:34 -04:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-20 19:55:26 -04:00
|
|
|
const hIndex = histIndex(pixelX, pixelY, this.size);
|
|
|
|
this.histogram[hIndex] += 1;
|
2023-07-02 19:34:34 -04:00
|
|
|
}
|
|
|
|
|
2023-07-20 19:55:26 -04:00
|
|
|
render(image: ImageData): void {
|
|
|
|
for (var x = 0; x < this.size; x++) {
|
|
|
|
for (var y = 0; y < this.size; y++) {
|
|
|
|
const hIndex = histIndex(x, y, this.size);
|
|
|
|
const iIndex = imageIndex(x, y, this.size);
|
|
|
|
image.data[iIndex + 0] = 0;
|
|
|
|
image.data[iIndex + 1] = 0;
|
|
|
|
image.data[iIndex + 2] = 0;
|
|
|
|
image.data[iIndex + 3] = (this.histogram[hIndex] > 0 ? 1 : 0) * 0xff;
|
2023-07-02 19:34:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 19:55:26 -04:00
|
|
|
export function buildBinary(size: number) {
|
|
|
|
return new RendererHistogram(
|
|
|
|
size,
|
|
|
|
[
|
|
|
|
[transform1Weight, transform1Post],
|
|
|
|
[transform2Weight, transform2Post],
|
|
|
|
[transform3Weight, transform3Post],
|
|
|
|
],
|
|
|
|
transformFinal
|
|
|
|
);
|
2023-07-02 19:34:34 -04:00
|
|
|
}
|