-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepZoom.js
More file actions
executable file
·289 lines (278 loc) · 8.55 KB
/
DeepZoom.js
File metadata and controls
executable file
·289 lines (278 loc) · 8.55 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* Created by magicpig on 6/18/14.
*/
var images = require("images");
images.setLimit(30000, 30000)
var async = require("async");
var fs = require("fs");
/**
* make DeepZoom image
* @param imageUrl
* @param tileSize
* @constructor
*/
function DeepZoom(imageUrl, tileSize, overLap) {
this.Url = imageUrl;
this.sourceImage = null;
this.width = 0;
this.height = 0;
this.tileSize = 256;
this.sourcePath = "";
this.overLap = 1;
this.quality = 100;
if (imageUrl && typeof imageUrl == "string") {
if (fs.existsSync(imageUrl)) {
this.sourceImage = images(imageUrl);
} else {
return false;
}
}
else {
return false;
}
if (tileSize) {
this.tileSize = tileSize;
}
if (overLap) {
this.overLap = overLap;
}
this.width = this.sourceImage.width();
this.height = this.sourceImage.height();
}
DeepZoom.prototype.setOption = function (key, value) {
this[key] = value;
console.log(this[key]);
}
/**
* 获取图片需要多少层级深
* @param width
* @param height
* @returns {number}
*/
DeepZoom.prototype.getMaximumLevel = function (width, height) {
return Math.ceil(Math.log(Math.max(width, height)) / Math.LN2)
}
/**
* 生成缩略图
* @constructor
*/
DeepZoom.prototype.Make = function (sourcePath) {
if (!fs.existsSync(this.Url)) {
return false;
}
var _self = this;
_self.sourcePath = sourcePath;
_self.mkdir(sourcePath);
this.CopySourceImage(this.getMaximumLevel(this.width, this.height));
this.MakeThumbImages(function () {//生成景深缩略图
_self.CorpImages();//图片分块 对景深缩略图进行分块处理
_self.DeleteZoomSoureImage();//删除景深缩略图
});
}
/**
* 删除景深缩略图
* @constructor
*/
DeepZoom.prototype.DeleteZoomSoureImage = function () {
var maxLevel = this.getMaximumLevel(this.width, this.height);
for (var i = 0; i <= maxLevel; i++) {
var path = this.sourcePath + "/" + i + '.jpeg';
if (fs.existsSync(path)) {
fs.unlinkSync(path);
}
}
}
/**
* 创建文件夹列表
* @param sourcePath
*/
DeepZoom.prototype.mkdir = function (sourcePath) {
if (!fs.existsSync("./" + sourcePath)) {//创建file目录
fs.mkdirSync("./" + sourcePath);
}
var list = this.computeLevels(this.width, this.height, this.tileSize);
for (var i = 0; i < list.length; i++) {//创建所有的子目录
if (!fs.existsSync("./" + sourcePath + "/" + list[i].level)) {//创建file目录
fs.mkdirSync("./" + sourcePath + "/" + list[i].level);
}
}
}
/**
* 对图片进行分片
* @constructor
*/
DeepZoom.prototype.CorpImages = function () {
var list = this.computeLevels(this.width, this.height, this.tileSize);
for (var item in list) {
var level = list[item].level;
if (list[item].columns == 1 && list[item].rows == 1) {//行列为1的,直接copy到目标目录,不再拆分
this.CpImages(list[item].level);
continue;
}
var levelSource = this.sourcePath + "/" + level + ".jpeg";
var sourceImg = images(levelSource);
for (var j = 0; j < list[item].columns; j++) {//循环裁剪出所有的图片
for (var k = 0; k < list[item].rows; k++) {
var rows = k;
var columns = j;
var position = this.getPosition(rows, columns, this.overLap, this.tileSize, list[item].columns, list[item].rows);
console.log("make level Images..", level, sourceImg, position[0], position[1], position[2], position[3]);
var targetImages = images(sourceImg, position[0], position[1], position[2], position[3]);
targetImages.save(this.sourcePath + "/" + level + "/" + j + "_" + k + ".jpg", {
quality: 100
});
}
}
}
}
/**
* 当level 小于一定的量时候,图基本都只有一行一列 ,就是一张缩略图,所以可以直接copy到目标地址
* @param level
* @constructor
*/
DeepZoom.prototype.CpImages = function (level) {
var sourcePath = this.sourcePath + "/" + level + ".jpeg";
var targetPath = this.sourcePath + "/" + level + "/" + "0_0" + ".jpg";
var images = fs.readFileSync(sourcePath);
fs.writeFileSync(targetPath, images);
}
/**
* 最顶级的实际上为原图,复制一张原图到指定位置
* @param level
* @constructor
*/
DeepZoom.prototype.CopySourceImage = function (level) {
var sourcePath = this.Url;
var targetPath = this.sourcePath + "/" + level + ".jpeg";
var images = fs.readFileSync(sourcePath);
fs.writeFileSync(targetPath, images);
}
/**
* 获取需要裁剪的坐标的 x y point 和 x y的增量长度
* @param rows
* @param column
* @param overLap
* @param tileSize
* @param totalColumns
* @param totalRows
* @returns {*[]}
*/
DeepZoom.prototype.getPosition = function (rows, column, overLap, tileSize, totalColumns, totalRows) {
var xStart = 0;
var xEnd = 0;
var yStart = 0;
var yEnd = 0;
xStart = column * tileSize;
yStart = rows * tileSize;
if (rows == 0 && rows < totalRows - 1) {
yStart = 0;
yEnd = tileSize + overLap;
}
if (rows > 0 && rows < totalRows - 1) {
yStart = rows * tileSize - overLap;
yEnd = tileSize + 2;
}
if (rows == 0 && rows == totalRows - 1) {
yStart = 0;
yEnd = tileSize;
}
if (rows > 0 && rows == totalRows - 1) {
yStart = rows * tileSize - overLap;
yEnd = tileSize + overLap;
}
if (column == 0 && column < totalColumns - 1) {
xStart = 0;
xEnd = tileSize + overLap;
}
if (column > 0 && column < totalColumns - 1) {
xStart = column * tileSize - overLap;
xEnd = tileSize + 2;
}
if (column == 0 && column == totalColumns - 1) {
xStart = 0;
xEnd = tileSize;
}
if (column > 0 && column == totalColumns - 1) {
xStart = column * tileSize - overLap;
xEnd = tileSize + overLap;
}
return [xStart, yStart, xEnd, yEnd];
}
/**
* 根据列表生成缩略图
* @constructor
*/
DeepZoom.prototype.MakeThumbImages = function (callback) {
var list = this.computeLevels(this.width, this.height, this.tileSize);
var _self = this;
/**
* 逐个生成缩略图
* @param data
* @param callback
* @returns {boolean}
* @constructor
*/
async.mapLimit(list, 1, function (data, callback) {//直接写在这里,闭包来访问this
if (data.width == data.sourceWidth && data.height == data.sourceHeight) {
callback(null)
return true;
}
var width = data.sourceWidth
var height = data.sourceHeight;
var toWidth = data.width;
var toHeight = data.height;
var newWidth = 0;
var newHeight = 0;
if (width != 0 && height != 0 && toWidth != 0 && toHeight != 0) {
var xscale = width / toWidth;
var yscale = height / toHeight;
if (yscale > xscale) {
newWidth = Math.round(width * (1 / yscale));
newHeight = Math.round(height * (1 / yscale));
}
else {
newWidth = Math.round(width * (1 / xscale));
newHeight = Math.round(height * (1 / xscale));
}
_self.sourceImage.size(newWidth, newHeight).save(data.sourcePath + "/" + data.level + '.jpeg');
callback(null);
return true;
}
callback(null);
return false;
}, function (err, results) {
callback();
});
}
/**
* 获取图片需要缩放多少次,且每一层切分多少块
* @param width
* @param height
* @param tileSize
* @returns {Array}
*/
DeepZoom.prototype.computeLevels = function (width, height, tileSize) {
var thumbImages = new Array();//相片的数组
var maxLevel = this.getMaximumLevel(width, height)
var columns = 0;
var rows = 0;
for (var level = maxLevel; level >= 0; level--) {
// compute number of rows & columns
columns = Math.ceil(width / tileSize);
rows = Math.ceil(height / tileSize);
thumbImages.push({
width: width,
height: height,
columns: columns,
rows: rows,
sourceWidth: this.width,
sourceHeight: this.height,
level: level,
sourcePath: this.sourcePath
})
width = Math.ceil(width / 2)
height = Math.ceil(height / 2)
}
return thumbImages;
}
exports.DeepZoom = DeepZoom;