UNPKG

28.8 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [op[0] & 2, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var _this = this;
38Object.defineProperty(exports, "__esModule", { value: true });
39/**
40 * @license
41 * Copyright 2020 Google LLC. All Rights Reserved.
42 * Licensed under the Apache License, Version 2.0 (the "License");
43 * you may not use this file except in compliance with the License.
44 * You may obtain a copy of the License at
45 *
46 * http://www.apache.org/licenses/LICENSE-2.0
47 *
48 * Unless required by applicable law or agreed to in writing, software
49 * distributed under the License is distributed on an "AS IS" BASIS,
50 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51 * See the License for the specific language governing permissions and
52 * limitations under the License.
53 * =============================================================================
54 */
55var tfjs_1 = require("@tensorflow/tfjs");
56// tslint:disable-next-line: no-imports-from-dist
57var jasmine_util_1 = require("@tensorflow/tfjs-core/dist/jasmine_util");
58var fs = require("fs");
59var util_1 = require("util");
60var image_1 = require("./image");
61var tf = require("./index");
62var readFile = util_1.promisify(fs.readFile);
63describe('decode images', function () {
64 it('decode png', function () { return __awaiter(_this, void 0, void 0, function () {
65 var beforeNumTensors, uint8array, imageTensor, _a, _b;
66 return __generator(this, function (_c) {
67 switch (_c.label) {
68 case 0:
69 beforeNumTensors = tfjs_1.memory().numTensors;
70 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_png_test.png')];
71 case 1:
72 uint8array = _c.sent();
73 imageTensor = tf.node.decodePng(uint8array);
74 expect(imageTensor.dtype).toBe('int32');
75 expect(imageTensor.shape).toEqual([2, 2, 3]);
76 _b = (_a = tfjs_1.test_util).expectArraysEqual;
77 return [4 /*yield*/, imageTensor.data()];
78 case 2:
79 _b.apply(_a, [_c.sent(),
80 [238, 101, 0, 50, 50, 50, 100, 50, 0, 200, 100, 50]]);
81 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
82 return [2 /*return*/];
83 }
84 });
85 }); });
86 it('decode png 1 channels', function () { return __awaiter(_this, void 0, void 0, function () {
87 var beforeNumTensors, uint8array, imageTensor, _a, _b;
88 return __generator(this, function (_c) {
89 switch (_c.label) {
90 case 0:
91 beforeNumTensors = tfjs_1.memory().numTensors;
92 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_png_test.png')];
93 case 1:
94 uint8array = _c.sent();
95 imageTensor = tf.node.decodeImage(uint8array, 1);
96 expect(imageTensor.dtype).toBe('int32');
97 expect(imageTensor.shape).toEqual([2, 2, 1]);
98 _b = (_a = tfjs_1.test_util).expectArraysEqual;
99 return [4 /*yield*/, imageTensor.data()];
100 case 2:
101 _b.apply(_a, [_c.sent(), [130, 50, 59, 124]]);
102 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
103 return [2 /*return*/];
104 }
105 });
106 }); });
107 it('decode png 3 channels', function () { return __awaiter(_this, void 0, void 0, function () {
108 var beforeNumTensors, uint8array, imageTensor, _a, _b;
109 return __generator(this, function (_c) {
110 switch (_c.label) {
111 case 0:
112 beforeNumTensors = tfjs_1.memory().numTensors;
113 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_png_test.png')];
114 case 1:
115 uint8array = _c.sent();
116 imageTensor = tf.node.decodeImage(uint8array);
117 expect(imageTensor.dtype).toBe('int32');
118 expect(imageTensor.shape).toEqual([2, 2, 3]);
119 _b = (_a = tfjs_1.test_util).expectArraysEqual;
120 return [4 /*yield*/, imageTensor.data()];
121 case 2:
122 _b.apply(_a, [_c.sent(),
123 [238, 101, 0, 50, 50, 50, 100, 50, 0, 200, 100, 50]]);
124 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
125 return [2 /*return*/];
126 }
127 });
128 }); });
129 it('decode png 4 channels', function () { return __awaiter(_this, void 0, void 0, function () {
130 var beforeNumTensors, uint8array, imageTensor, _a, _b;
131 return __generator(this, function (_c) {
132 switch (_c.label) {
133 case 0:
134 beforeNumTensors = tfjs_1.memory().numTensors;
135 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_png_4_channel_test.png')];
136 case 1:
137 uint8array = _c.sent();
138 imageTensor = tf.node.decodeImage(uint8array, 4);
139 expect(imageTensor.dtype).toBe('int32');
140 expect(imageTensor.shape).toEqual([2, 2, 4]);
141 _b = (_a = tfjs_1.test_util).expectArraysEqual;
142 return [4 /*yield*/, imageTensor.data()];
143 case 2:
144 _b.apply(_a, [_c.sent(), [
145 238, 101, 0, 255, 50, 50, 50, 255, 100, 50, 0, 255, 200, 100, 50, 255
146 ]]);
147 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
148 return [2 /*return*/];
149 }
150 });
151 }); });
152 it('decode bmp', function () { return __awaiter(_this, void 0, void 0, function () {
153 var beforeNumTensors, uint8array, imageTensor, _a, _b;
154 return __generator(this, function (_c) {
155 switch (_c.label) {
156 case 0:
157 beforeNumTensors = tfjs_1.memory().numTensors;
158 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_bmp_test.bmp')];
159 case 1:
160 uint8array = _c.sent();
161 imageTensor = tf.node.decodeBmp(uint8array);
162 expect(imageTensor.dtype).toBe('int32');
163 expect(imageTensor.shape).toEqual([2, 2, 3]);
164 _b = (_a = tfjs_1.test_util).expectArraysEqual;
165 return [4 /*yield*/, imageTensor.data()];
166 case 2:
167 _b.apply(_a, [_c.sent(),
168 [238, 101, 0, 50, 50, 50, 100, 50, 0, 200, 100, 50]]);
169 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
170 return [2 /*return*/];
171 }
172 });
173 }); });
174 it('decode bmp through decodeImage', function () { return __awaiter(_this, void 0, void 0, function () {
175 var beforeNumTensors, uint8array, imageTensor, _a, _b;
176 return __generator(this, function (_c) {
177 switch (_c.label) {
178 case 0:
179 beforeNumTensors = tfjs_1.memory().numTensors;
180 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_bmp_test.bmp')];
181 case 1:
182 uint8array = _c.sent();
183 imageTensor = tf.node.decodeImage(uint8array);
184 expect(imageTensor.dtype).toBe('int32');
185 expect(imageTensor.shape).toEqual([2, 2, 3]);
186 _b = (_a = tfjs_1.test_util).expectArraysEqual;
187 return [4 /*yield*/, imageTensor.data()];
188 case 2:
189 _b.apply(_a, [_c.sent(),
190 [238, 101, 0, 50, 50, 50, 100, 50, 0, 200, 100, 50]]);
191 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
192 return [2 /*return*/];
193 }
194 });
195 }); });
196 it('decode jpg', function () { return __awaiter(_this, void 0, void 0, function () {
197 var beforeNumTensors, uint8array, imageTensor, _a, _b;
198 return __generator(this, function (_c) {
199 switch (_c.label) {
200 case 0:
201 beforeNumTensors = tfjs_1.memory().numTensors;
202 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_jpeg_test.jpeg')];
203 case 1:
204 uint8array = _c.sent();
205 imageTensor = tf.node.decodeJpeg(uint8array);
206 expect(imageTensor.dtype).toBe('int32');
207 expect(imageTensor.shape).toEqual([2, 2, 3]);
208 _b = (_a = tfjs_1.test_util).expectArraysEqual;
209 return [4 /*yield*/, imageTensor.data()];
210 case 2:
211 _b.apply(_a, [_c.sent(),
212 [239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]]);
213 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
214 return [2 /*return*/];
215 }
216 });
217 }); });
218 it('decode jpg 1 channel', function () { return __awaiter(_this, void 0, void 0, function () {
219 var beforeNumTensors, uint8array, imageTensor, _a, _b;
220 return __generator(this, function (_c) {
221 switch (_c.label) {
222 case 0:
223 beforeNumTensors = tfjs_1.memory().numTensors;
224 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_jpeg_test.jpeg')];
225 case 1:
226 uint8array = _c.sent();
227 imageTensor = tf.node.decodeImage(uint8array, 1);
228 expect(imageTensor.dtype).toBe('int32');
229 expect(imageTensor.shape).toEqual([2, 2, 1]);
230 _b = (_a = tfjs_1.test_util).expectArraysEqual;
231 return [4 /*yield*/, imageTensor.data()];
232 case 2:
233 _b.apply(_a, [_c.sent(), [130, 47, 56, 121]]);
234 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
235 return [2 /*return*/];
236 }
237 });
238 }); });
239 it('decode jpg 3 channels', function () { return __awaiter(_this, void 0, void 0, function () {
240 var beforeNumTensors, uint8array, imageTensor, _a, _b;
241 return __generator(this, function (_c) {
242 switch (_c.label) {
243 case 0:
244 beforeNumTensors = tfjs_1.memory().numTensors;
245 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_jpeg_test.jpeg')];
246 case 1:
247 uint8array = _c.sent();
248 imageTensor = tf.node.decodeImage(uint8array, 3);
249 expect(imageTensor.dtype).toBe('int32');
250 expect(imageTensor.shape).toEqual([2, 2, 3]);
251 _b = (_a = tfjs_1.test_util).expectArraysEqual;
252 return [4 /*yield*/, imageTensor.data()];
253 case 2:
254 _b.apply(_a, [_c.sent(),
255 [239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]]);
256 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
257 return [2 /*return*/];
258 }
259 });
260 }); });
261 it('decode jpg with 0 channels, use the number of channels in the ' +
262 'JPEG-encoded image', function () { return __awaiter(_this, void 0, void 0, function () {
263 var beforeNumTensors, uint8array, imageTensor, _a, _b;
264 return __generator(this, function (_c) {
265 switch (_c.label) {
266 case 0:
267 beforeNumTensors = tfjs_1.memory().numTensors;
268 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_jpeg_test.jpeg')];
269 case 1:
270 uint8array = _c.sent();
271 imageTensor = tf.node.decodeImage(uint8array);
272 expect(imageTensor.dtype).toBe('int32');
273 expect(imageTensor.shape).toEqual([2, 2, 3]);
274 _b = (_a = tfjs_1.test_util).expectArraysEqual;
275 return [4 /*yield*/, imageTensor.data()];
276 case 2:
277 _b.apply(_a, [_c.sent(),
278 [239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]]);
279 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
280 return [2 /*return*/];
281 }
282 });
283 }); });
284 it('decode jpg with downscale', function () { return __awaiter(_this, void 0, void 0, function () {
285 var beforeNumTensors, uint8array, imageTensor, _a, _b;
286 return __generator(this, function (_c) {
287 switch (_c.label) {
288 case 0:
289 beforeNumTensors = tfjs_1.memory().numTensors;
290 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_jpeg_test.jpeg')];
291 case 1:
292 uint8array = _c.sent();
293 imageTensor = tf.node.decodeJpeg(uint8array, 0, 2);
294 expect(imageTensor.dtype).toBe('int32');
295 expect(imageTensor.shape).toEqual([1, 1, 3]);
296 _b = (_a = tfjs_1.test_util).expectArraysEqual;
297 return [4 /*yield*/, imageTensor.data()];
298 case 2:
299 _b.apply(_a, [_c.sent(), [147, 75, 25]]);
300 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
301 return [2 /*return*/];
302 }
303 });
304 }); });
305 it('decode gif', function () { return __awaiter(_this, void 0, void 0, function () {
306 var beforeNumTensors, uint8array, imageTensor, _a, _b;
307 return __generator(this, function (_c) {
308 switch (_c.label) {
309 case 0:
310 beforeNumTensors = tfjs_1.memory().numTensors;
311 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/gif_test.gif')];
312 case 1:
313 uint8array = _c.sent();
314 imageTensor = tf.node.decodeImage(uint8array);
315 expect(imageTensor.dtype).toBe('int32');
316 expect(imageTensor.shape).toEqual([2, 2, 2, 3]);
317 _b = (_a = tfjs_1.test_util).expectArraysEqual;
318 return [4 /*yield*/, imageTensor.data()];
319 case 2:
320 _b.apply(_a, [_c.sent(), [
321 238, 101, 0, 50, 50, 50, 100, 50, 0, 200, 100, 50,
322 200, 100, 50, 34, 68, 102, 170, 0, 102, 255, 255, 255
323 ]]);
324 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
325 return [2 /*return*/];
326 }
327 });
328 }); });
329 it('decode gif with no expandAnimation', function () { return __awaiter(_this, void 0, void 0, function () {
330 var beforeNumTensors, uint8array, imageTensor, _a, _b;
331 return __generator(this, function (_c) {
332 switch (_c.label) {
333 case 0:
334 beforeNumTensors = tfjs_1.memory().numTensors;
335 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/gif_test.gif')];
336 case 1:
337 uint8array = _c.sent();
338 imageTensor = tf.node.decodeImage(uint8array, 3, 'int32', false);
339 expect(imageTensor.dtype).toBe('int32');
340 expect(imageTensor.shape).toEqual([2, 2, 3]);
341 _b = (_a = tfjs_1.test_util).expectArraysEqual;
342 return [4 /*yield*/, imageTensor.data()];
343 case 2:
344 _b.apply(_a, [_c.sent(),
345 [238, 101, 0, 50, 50, 50, 100, 50, 0, 200, 100, 50]]);
346 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors + 1);
347 return [2 /*return*/];
348 }
349 });
350 }); });
351 it('throw error if request non int32 dtype', function (done) { return __awaiter(_this, void 0, void 0, function () {
352 var uint8array, error_1;
353 return __generator(this, function (_a) {
354 switch (_a.label) {
355 case 0:
356 _a.trys.push([0, 2, , 3]);
357 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_png_test.png')];
358 case 1:
359 uint8array = _a.sent();
360 tf.node.decodeImage(uint8array, 0, 'uint8');
361 done.fail();
362 return [3 /*break*/, 3];
363 case 2:
364 error_1 = _a.sent();
365 expect(error_1.message)
366 .toBe('decodeImage could only return Tensor of type `int32` for now.');
367 done();
368 return [3 /*break*/, 3];
369 case 3: return [2 /*return*/];
370 }
371 });
372 }); });
373 it('throw error if decode invalid image type', function (done) { return __awaiter(_this, void 0, void 0, function () {
374 var uint8array, error_2;
375 return __generator(this, function (_a) {
376 switch (_a.label) {
377 case 0:
378 _a.trys.push([0, 2, , 3]);
379 return [4 /*yield*/, getUint8ArrayFromImage('package.json')];
380 case 1:
381 uint8array = _a.sent();
382 tf.node.decodeImage(uint8array);
383 done.fail();
384 return [3 /*break*/, 3];
385 case 2:
386 error_2 = _a.sent();
387 expect(error_2.message)
388 .toBe('Expected image (BMP, JPEG, PNG, or GIF), ' +
389 'but got unsupported image type');
390 done();
391 return [3 /*break*/, 3];
392 case 3: return [2 /*return*/];
393 }
394 });
395 }); });
396 it('throw error if backend is not tensorflow', function (done) { return __awaiter(_this, void 0, void 0, function () {
397 var testBackend_1, uint8array, err_1;
398 return __generator(this, function (_a) {
399 switch (_a.label) {
400 case 0:
401 _a.trys.push([0, 2, , 3]);
402 testBackend_1 = new jasmine_util_1.TestKernelBackend();
403 tfjs_1.registerBackend('fake', function () { return testBackend_1; });
404 tfjs_1.setBackend('fake');
405 return [4 /*yield*/, getUint8ArrayFromImage('test_objects/images/image_png_test.png')];
406 case 1:
407 uint8array = _a.sent();
408 tf.node.decodeImage(uint8array);
409 done.fail();
410 return [3 /*break*/, 3];
411 case 2:
412 err_1 = _a.sent();
413 expect(err_1.message)
414 .toBe('Expect the current backend to be "tensorflow", but got "fake"');
415 tfjs_1.setBackend('tensorflow');
416 done();
417 return [3 /*break*/, 3];
418 case 3: return [2 /*return*/];
419 }
420 });
421 }); });
422});
423describe('encode images', function () {
424 it('encodeJpeg', function () { return __awaiter(_this, void 0, void 0, function () {
425 var imageTensor, beforeNumTensors, jpegEncodedData;
426 return __generator(this, function (_a) {
427 switch (_a.label) {
428 case 0:
429 imageTensor = tf.tensor3d(new Uint8Array([239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]), [2, 2, 3]);
430 beforeNumTensors = tfjs_1.memory().numTensors;
431 return [4 /*yield*/, tf.node.encodeJpeg(imageTensor)];
432 case 1:
433 jpegEncodedData = _a.sent();
434 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors);
435 expect(image_1.getImageType(jpegEncodedData)).toEqual(image_1.ImageType.JPEG);
436 imageTensor.dispose();
437 return [2 /*return*/];
438 }
439 });
440 }); });
441 it('encodeJpeg grayscale', function () { return __awaiter(_this, void 0, void 0, function () {
442 var imageTensor, beforeNumTensors, jpegEncodedData;
443 return __generator(this, function (_a) {
444 switch (_a.label) {
445 case 0:
446 imageTensor = tf.tensor3d(new Uint8Array([239, 0, 47, 0]), [2, 2, 1]);
447 beforeNumTensors = tfjs_1.memory().numTensors;
448 return [4 /*yield*/, tf.node.encodeJpeg(imageTensor, 'grayscale')];
449 case 1:
450 jpegEncodedData = _a.sent();
451 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors);
452 expect(image_1.getImageType(jpegEncodedData)).toEqual(image_1.ImageType.JPEG);
453 imageTensor.dispose();
454 return [2 /*return*/];
455 }
456 });
457 }); });
458 it('encodeJpeg with parameters', function () { return __awaiter(_this, void 0, void 0, function () {
459 var imageTensor, format, quality, progressive, optimizeSize, chromaDownsampling, densityUnit, xDensity, yDensity, beforeNumTensors, jpegEncodedData;
460 return __generator(this, function (_a) {
461 switch (_a.label) {
462 case 0:
463 imageTensor = tf.tensor3d(new Uint8Array([239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]), [2, 2, 3]);
464 format = 'rgb';
465 quality = 50;
466 progressive = true;
467 optimizeSize = true;
468 chromaDownsampling = false;
469 densityUnit = 'cm';
470 xDensity = 500;
471 yDensity = 500;
472 beforeNumTensors = tfjs_1.memory().numTensors;
473 return [4 /*yield*/, tf.node.encodeJpeg(imageTensor, format, quality, progressive, optimizeSize, chromaDownsampling, densityUnit, xDensity, yDensity)];
474 case 1:
475 jpegEncodedData = _a.sent();
476 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors);
477 expect(image_1.getImageType(jpegEncodedData)).toEqual(image_1.ImageType.JPEG);
478 imageTensor.dispose();
479 return [2 /*return*/];
480 }
481 });
482 }); });
483 it('encodePng', function () { return __awaiter(_this, void 0, void 0, function () {
484 var imageTensor, beforeNumTensors, pngEncodedData, pngDecodedTensor, pngDecodedData, _a, _b;
485 return __generator(this, function (_c) {
486 switch (_c.label) {
487 case 0:
488 imageTensor = tf.tensor3d(new Uint8Array([239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]), [2, 2, 3]);
489 beforeNumTensors = tfjs_1.memory().numTensors;
490 return [4 /*yield*/, tf.node.encodePng(imageTensor)];
491 case 1:
492 pngEncodedData = _c.sent();
493 return [4 /*yield*/, tf.node.decodePng(pngEncodedData)];
494 case 2:
495 pngDecodedTensor = _c.sent();
496 return [4 /*yield*/, pngDecodedTensor.data()];
497 case 3:
498 pngDecodedData = _c.sent();
499 pngDecodedTensor.dispose();
500 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors);
501 expect(image_1.getImageType(pngEncodedData)).toEqual(image_1.ImageType.PNG);
502 _b = (_a = tfjs_1.test_util).expectArraysEqual;
503 return [4 /*yield*/, imageTensor.data()];
504 case 4:
505 _b.apply(_a, [_c.sent(), pngDecodedData]);
506 imageTensor.dispose();
507 return [2 /*return*/];
508 }
509 });
510 }); });
511 it('encodePng grayscale', function () { return __awaiter(_this, void 0, void 0, function () {
512 var imageTensor, beforeNumTensors, pngEncodedData;
513 return __generator(this, function (_a) {
514 switch (_a.label) {
515 case 0:
516 imageTensor = tf.tensor3d(new Uint8Array([239, 0, 47, 0]), [2, 2, 1]);
517 beforeNumTensors = tfjs_1.memory().numTensors;
518 return [4 /*yield*/, tf.node.encodePng(imageTensor)];
519 case 1:
520 pngEncodedData = _a.sent();
521 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors);
522 expect(image_1.getImageType(pngEncodedData)).toEqual(image_1.ImageType.PNG);
523 imageTensor.dispose();
524 return [2 /*return*/];
525 }
526 });
527 }); });
528 it('encodePng with parameters', function () { return __awaiter(_this, void 0, void 0, function () {
529 var imageTensor, compression, beforeNumTensors, pngEncodedData;
530 return __generator(this, function (_a) {
531 switch (_a.label) {
532 case 0:
533 imageTensor = tf.tensor3d(new Uint8Array([239, 100, 0, 46, 48, 47, 92, 49, 0, 194, 98, 47]), [2, 2, 3]);
534 compression = 4;
535 beforeNumTensors = tfjs_1.memory().numTensors;
536 return [4 /*yield*/, tf.node.encodePng(imageTensor, compression)];
537 case 1:
538 pngEncodedData = _a.sent();
539 expect(tfjs_1.memory().numTensors).toBe(beforeNumTensors);
540 expect(image_1.getImageType(pngEncodedData)).toEqual(image_1.ImageType.PNG);
541 imageTensor.dispose();
542 return [2 /*return*/];
543 }
544 });
545 }); });
546});
547function getUint8ArrayFromImage(path) {
548 return __awaiter(this, void 0, void 0, function () {
549 var image, buf;
550 return __generator(this, function (_a) {
551 switch (_a.label) {
552 case 0: return [4 /*yield*/, readFile(path)];
553 case 1:
554 image = _a.sent();
555 buf = Buffer.from(image);
556 return [2 /*return*/, new Uint8Array(buf)];
557 }
558 });
559 });
560}