forked from cambridgecoding/photoeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module3.js
132 lines (90 loc) · 3.02 KB
/
module3.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class ImageUtils {
static getCanvas(w, h) {
var c = document.querySelector("canvas");
c.width = w;
c.height = h;
return c;
}
static getPixels(img) {
var c = ImageUtils.getCanvas(img.width, img.height);
var ctx = c.getContext('2d');
ctx.drawImage(img, 0, 0);
return ctx.getImageData(0,0,c.width,c.height);
}
static putPixels(imageData, w, h) {
var c = ImageUtils.getCanvas(w, h);
var ctx = c.getContext('2d');
ctx.putImageData(imageData, 0, 0);
}
// new in module 3
static fromImageData(imageData) {
var width = imageData.width;
var height = imageData.height;
var data = imageData.data;
var pixelGrid = initializePixelGrid(height);
for(var y = 0; y < height; y++) {
var row = (y * width * 4);
for(var x = 0; x < width; x++) {
var index = row + (x * 4);
var rgba = new RGBA(data[index], data[index+1], data[index+2], data[index+3]);
pixelGrid[y][x] = rgba;
}
}
return new ImageModel(height, width, pixelGrid);
}
// new in module 3
static fromImgSrc(imgSrc) {
var img = new Image();
img.src = imgSrc;
var data = ImageUtils.getPixels(img);
return ImageUtils.fromImageData(data);
}
// new in module 3
static toImageData(imageModel) {
console.log(imageModel);
var buffer = new ArrayBuffer(imageModel.height * imageModel.width * 4);
var data = new Uint8ClampedArray(buffer);
for(var y = 0; y < imageModel.height; y++) {
var row = (y * imageModel.width * 4);
for(var x = 0; x < imageModel.width; x++) {
var rgba = imageModel.pixelGrid[y][x];
var index = row + (x * 4);
data[index] = rgba.red;
data[index + 1] = rgba.green;
data[index + 2] = rgba.blue;
data[index + 3] = rgba.alpha;
}
}
return new ImageData(data, imageModel.width, imageModel.height);
}
// new in module 3
static drawImageModel(imageModel) {
var c = ImageUtils.getCanvas(imageModel.width, imageModel.height);
var imageData = ImageUtils.toImageData(imageModel);
ImageUtils.putPixels(imageData, imageModel.width, imageModel.height);
}
}
class ImageModel {
constructor(heightValue, widthValue, pixelGridValue) {
this.height = heightValue;
this.width = widthValue;
this.pixelGrid = pixelGridValue;
}
}
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
class RGBA {
constructor(redValue, greenValue, blueValue, alphaValue) {
this.red = redValue;
this.green = greenValue;
this.blue = blueValue;
this.alpha = alphaValue;
}
}
// class definitions here
$(document).ready(function() {
var img = new Image();
img.src = "img/cat.jpg";
});