forked from tuefekci/deeplearnjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndarray-image-visualizer.ts
90 lines (80 loc) · 3.02 KB
/
ndarray-image-visualizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @license
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {Array3D} from './deeplearn';
import {PolymerElement, PolymerHTMLElement} from './polymer-spec';
// tslint:disable-next-line
export let NDArrayImageVisualizerPolymer: new () => PolymerHTMLElement =
PolymerElement({is: 'ndarray-image-visualizer', properties: {}});
export class NDArrayImageVisualizer extends NDArrayImageVisualizerPolymer {
private canvas: HTMLCanvasElement;
private canvasContext: CanvasRenderingContext2D;
private imageData: ImageData;
ready() {
this.canvas = this.querySelector('#canvas') as HTMLCanvasElement;
this.canvas.width = 0;
this.canvas.height = 0;
this.canvasContext =
this.canvas.getContext('2d') as CanvasRenderingContext2D;
this.canvas.style.display = 'none';
}
setShape(shape: number[]) {
this.canvas.width = shape[1];
this.canvas.height = shape[0];
}
setSize(width: number, height: number) {
this.canvas.style.width = width + 'px';
this.canvas.style.height = height + 'px';
}
saveImageDataFromNDArray(ndarray: Array3D) {
this.imageData = this.canvasContext.createImageData(
this.canvas.width, this.canvas.height);
if (ndarray.shape[2] === 1) {
this.drawGrayscaleImageData(ndarray);
} else if (ndarray.shape[2] === 3) {
this.drawRGBImageData(ndarray);
}
}
drawRGBImageData(ndarray: Array3D) {
let pixelOffset = 0;
for (let i = 0; i < ndarray.shape[0]; i++) {
for (let j = 0; j < ndarray.shape[1]; j++) {
this.imageData.data[pixelOffset++] = ndarray.get(i, j, 0);
this.imageData.data[pixelOffset++] = ndarray.get(i, j, 1);
this.imageData.data[pixelOffset++] = ndarray.get(i, j, 2);
this.imageData.data[pixelOffset++] = 255;
}
}
}
drawGrayscaleImageData(ndarray: Array3D) {
let pixelOffset = 0;
for (let i = 0; i < ndarray.shape[0]; i++) {
for (let j = 0; j < ndarray.shape[1]; j++) {
const value = ndarray.get(i, j, 0);
this.imageData.data[pixelOffset++] = value;
this.imageData.data[pixelOffset++] = value;
this.imageData.data[pixelOffset++] = value;
this.imageData.data[pixelOffset++] = 255;
}
}
}
draw() {
this.canvas.style.display = '';
this.canvasContext.putImageData(this.imageData, 0, 0);
}
}
document.registerElement(
NDArrayImageVisualizer.prototype.is, NDArrayImageVisualizer);