1 | ;
|
2 | /**
|
3 | * @license
|
4 | * Copyright 2019 Google Inc. All Rights Reserved.
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | * =============================================================================
|
17 | */
|
18 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
19 | return new (P || (P = Promise))(function (resolve, reject) {
|
20 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
21 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
22 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
23 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
24 | });
|
25 | };
|
26 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
27 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
28 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
29 | function verb(n) { return function (v) { return step([n, v]); }; }
|
30 | function step(op) {
|
31 | if (f) throw new TypeError("Generator is already executing.");
|
32 | while (_) try {
|
33 | 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;
|
34 | if (y = 0, t) op = [op[0] & 2, t.value];
|
35 | switch (op[0]) {
|
36 | case 0: case 1: t = op; break;
|
37 | case 4: _.label++; return { value: op[1], done: false };
|
38 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
39 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
40 | default:
|
41 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
42 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
43 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
44 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
45 | if (t[2]) _.ops.pop();
|
46 | _.trys.pop(); continue;
|
47 | }
|
48 | op = body.call(thisArg, _);
|
49 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
50 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
51 | }
|
52 | };
|
53 | Object.defineProperty(exports, "__esModule", { value: true });
|
54 | var tfjs_1 = require("@tensorflow/tfjs");
|
55 | var nodejs_kernel_backend_1 = require("./nodejs_kernel_backend");
|
56 | var ImageType;
|
57 | (function (ImageType) {
|
58 | ImageType["JPEG"] = "jpeg";
|
59 | ImageType["PNG"] = "png";
|
60 | ImageType["GIF"] = "gif";
|
61 | ImageType["BMP"] = "BMP";
|
62 | })(ImageType = exports.ImageType || (exports.ImageType = {}));
|
63 | /**
|
64 | * Decode a JPEG-encoded image to a 3D Tensor of dtype `int32`.
|
65 | *
|
66 | * @param contents The JPEG-encoded image in an Uint8Array.
|
67 | * @param channels An optional int. Defaults to 0. Accepted values are
|
68 | * 0: use the number of channels in the JPEG-encoded image.
|
69 | * 1: output a grayscale image.
|
70 | * 3: output an RGB image.
|
71 | * @param ratio An optional int. Defaults to 1. Downscaling ratio. It is used
|
72 | * when image is type Jpeg.
|
73 | * @param fancyUpscaling An optional bool. Defaults to True. If true use a
|
74 | * slower but nicer upscaling of the chroma planes. It is used when image is
|
75 | * type Jpeg.
|
76 | * @param tryRecoverTruncated An optional bool. Defaults to False. If true try
|
77 | * to recover an image from truncated input. It is used when image is type
|
78 | * Jpeg.
|
79 | * @param acceptableFraction An optional float. Defaults to 1. The minimum
|
80 | * required fraction of lines before a truncated input is accepted. It is
|
81 | * used when image is type Jpeg.
|
82 | * @param dctMethod An optional string. Defaults to "". string specifying a hint
|
83 | * about the algorithm used for decompression. Defaults to "" which maps to
|
84 | * a system-specific default. Currently valid values are ["INTEGER_FAST",
|
85 | * "INTEGER_ACCURATE"]. The hint may be ignored (e.g., the internal jpeg
|
86 | * library changes to a version that does not have that specific option.) It
|
87 | * is used when image is type Jpeg.
|
88 | * @returns A 3D Tensor of dtype `int32` with shape [height, width, 1/3].
|
89 | */
|
90 | /**
|
91 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
92 | */
|
93 | function decodeJpeg(contents, channels, ratio, fancyUpscaling, tryRecoverTruncated, acceptableFraction, dctMethod) {
|
94 | if (channels === void 0) { channels = 0; }
|
95 | if (ratio === void 0) { ratio = 1; }
|
96 | if (fancyUpscaling === void 0) { fancyUpscaling = true; }
|
97 | if (tryRecoverTruncated === void 0) { tryRecoverTruncated = false; }
|
98 | if (acceptableFraction === void 0) { acceptableFraction = 1; }
|
99 | if (dctMethod === void 0) { dctMethod = ''; }
|
100 | nodejs_kernel_backend_1.ensureTensorflowBackend();
|
101 | return tfjs_1.tidy(function () {
|
102 | return nodejs_kernel_backend_1.nodeBackend()
|
103 | .decodeJpeg(contents, channels, ratio, fancyUpscaling, tryRecoverTruncated, acceptableFraction, dctMethod)
|
104 | .toInt();
|
105 | });
|
106 | }
|
107 | exports.decodeJpeg = decodeJpeg;
|
108 | /**
|
109 | * Decode a PNG-encoded image to a 3D Tensor of dtype `int32`.
|
110 | *
|
111 | * @param contents The PNG-encoded image in an Uint8Array.
|
112 | * @param channels An optional int. Defaults to 0. Accepted values are
|
113 | * 0: use the number of channels in the PNG-encoded image.
|
114 | * 1: output a grayscale image.
|
115 | * 3: output an RGB image.
|
116 | * 4: output an RGBA image.
|
117 | * @param dtype The data type of the result. Only `int32` is supported at this
|
118 | * time.
|
119 | * @returns A 3D Tensor of dtype `int32` with shape [height, width, 1/3/4].
|
120 | */
|
121 | /**
|
122 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
123 | */
|
124 | function decodePng(contents, channels, dtype) {
|
125 | if (channels === void 0) { channels = 0; }
|
126 | if (dtype === void 0) { dtype = 'int32'; }
|
127 | tfjs_1.util.assert(dtype === 'int32', function () { return 'decodeImage could only return Tensor of type `int32` for now.'; });
|
128 | nodejs_kernel_backend_1.ensureTensorflowBackend();
|
129 | return tfjs_1.tidy(function () {
|
130 | return nodejs_kernel_backend_1.nodeBackend().decodePng(contents, channels).toInt();
|
131 | });
|
132 | }
|
133 | exports.decodePng = decodePng;
|
134 | /**
|
135 | * Decode the first frame of a BMP-encoded image to a 3D Tensor of dtype
|
136 | * `int32`.
|
137 | *
|
138 | * @param contents The BMP-encoded image in an Uint8Array.
|
139 | * @param channels An optional int. Defaults to 0. Accepted values are
|
140 | * 0: use the number of channels in the BMP-encoded image.
|
141 | * 3: output an RGB image.
|
142 | * 4: output an RGBA image.
|
143 | * @returns A 3D Tensor of dtype `int32` with shape [height, width, 3/4].
|
144 | */
|
145 | /**
|
146 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
147 | */
|
148 | function decodeBmp(contents, channels) {
|
149 | if (channels === void 0) { channels = 0; }
|
150 | nodejs_kernel_backend_1.ensureTensorflowBackend();
|
151 | return tfjs_1.tidy(function () {
|
152 | return nodejs_kernel_backend_1.nodeBackend().decodeBmp(contents, channels).toInt();
|
153 | });
|
154 | }
|
155 | exports.decodeBmp = decodeBmp;
|
156 | /**
|
157 | * Decode the frame(s) of a GIF-encoded image to a 4D Tensor of dtype `int32`.
|
158 | *
|
159 | * @param contents The GIF-encoded image in an Uint8Array.
|
160 | * @returns A 4D Tensor of dtype `int32` with shape [num_frames, height, width,
|
161 | * 3]. RGB channel order.
|
162 | */
|
163 | /**
|
164 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
165 | */
|
166 | function decodeGif(contents) {
|
167 | nodejs_kernel_backend_1.ensureTensorflowBackend();
|
168 | return tfjs_1.tidy(function () {
|
169 | return nodejs_kernel_backend_1.nodeBackend().decodeGif(contents).toInt();
|
170 | });
|
171 | }
|
172 | exports.decodeGif = decodeGif;
|
173 | /**
|
174 | * Given the encoded bytes of an image, it returns a 3D or 4D tensor of the
|
175 | * decoded image. Supports BMP, GIF, JPEG and PNG formats.
|
176 | *
|
177 | * @param content The encoded image in an Uint8Array.
|
178 | * @param channels An optional int. Defaults to 0, use the number of channels in
|
179 | * the image. Number of color channels for the decoded image. It is used
|
180 | * when image is type Png, Bmp, or Jpeg.
|
181 | * @param dtype The data type of the result. Only `int32` is supported at this
|
182 | * time.
|
183 | * @param expandAnimations A boolean which controls the shape of the returned
|
184 | * op's output. If True, the returned op will produce a 3-D tensor for PNG,
|
185 | * JPEG, and BMP files; and a 4-D tensor for all GIFs, whether animated or
|
186 | * not. If, False, the returned op will produce a 3-D tensor for all file
|
187 | * types and will truncate animated GIFs to the first frame.
|
188 | * @returns A Tensor with dtype `int32` and a 3- or 4-dimensional shape,
|
189 | * depending on the file type. For gif file the returned Tensor shape is
|
190 | * [num_frames, height, width, 3], and for jpeg/png/bmp the returned Tensor
|
191 | * shape is [height, width, channels]
|
192 | */
|
193 | /**
|
194 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
195 | */
|
196 | function decodeImage(content, channels, dtype, expandAnimations) {
|
197 | if (channels === void 0) { channels = 0; }
|
198 | if (dtype === void 0) { dtype = 'int32'; }
|
199 | if (expandAnimations === void 0) { expandAnimations = true; }
|
200 | tfjs_1.util.assert(dtype === 'int32', function () { return 'decodeImage could only return Tensor of type `int32` for now.'; });
|
201 | var imageType = getImageType(content);
|
202 | // The return tensor has dtype uint8, which is not supported in
|
203 | // TensorFlow.js, casting it to int32 which is the default dtype for image
|
204 | // tensor. If the image is BMP, JPEG or PNG type, expanding the tensors
|
205 | // shape so it becomes Tensor4D, which is the default tensor shape for image
|
206 | // ([batch,imageHeight,imageWidth, depth]).
|
207 | switch (imageType) {
|
208 | case ImageType.JPEG:
|
209 | return decodeJpeg(content, channels);
|
210 | case ImageType.PNG:
|
211 | return decodePng(content, channels);
|
212 | case ImageType.GIF:
|
213 | // If not to expand animations, take first frame of the gif and return
|
214 | // as a 3D tensor.
|
215 | return tfjs_1.tidy(function () {
|
216 | var img = decodeGif(content);
|
217 | return expandAnimations ? img : img.slice(0, 1).squeeze([0]);
|
218 | });
|
219 | case ImageType.BMP:
|
220 | return decodeBmp(content, channels);
|
221 | default:
|
222 | return null;
|
223 | }
|
224 | }
|
225 | exports.decodeImage = decodeImage;
|
226 | /**
|
227 | * Encodes an image tensor to JPEG.
|
228 | *
|
229 | * @param image A 3-D uint8 Tensor of shape [height, width, channels].
|
230 | * @param format An optional string from: "", "grayscale", "rgb".
|
231 | * Defaults to "". Per pixel image format.
|
232 | * - '': Use a default format based on the number of channels in the image.
|
233 | * - grayscale: Output a grayscale JPEG image. The channels dimension of
|
234 | * image must be 1.
|
235 | * - rgb: Output an RGB JPEG image. The channels dimension of image must
|
236 | * be 3.
|
237 | * @param quality An optional int. Defaults to 95. Quality of the compression
|
238 | * from 0 to 100 (higher is better and slower).
|
239 | * @param progressive An optional bool. Defaults to False. If True, create a
|
240 | * JPEG that loads progressively (coarse to fine).
|
241 | * @param optimizeSize An optional bool. Defaults to False. If True, spend
|
242 | * CPU/RAM to reduce size with no quality change.
|
243 | * @param chromaDownsampling An optional bool. Defaults to True.
|
244 | * See http://en.wikipedia.org/wiki/Chroma_subsampling.
|
245 | * @param densityUnit An optional string from: "in", "cm". Defaults to "in".
|
246 | * Unit used to specify x_density and y_density: pixels per inch ('in') or
|
247 | * centimeter ('cm').
|
248 | * @param xDensity An optional int. Defaults to 300. Horizontal pixels per
|
249 | * density unit.
|
250 | * @param yDensity An optional int. Defaults to 300. Vertical pixels per
|
251 | * density unit.
|
252 | * @param xmpMetadata An optional string. Defaults to "". If not empty, embed
|
253 | * this XMP metadata in the image header.
|
254 | * @returns The JPEG encoded data as an Uint8Array.
|
255 | */
|
256 | /**
|
257 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
258 | */
|
259 | function encodeJpeg(image, format, quality, progressive, optimizeSize, chromaDownsampling, densityUnit, xDensity, yDensity, xmpMetadata) {
|
260 | if (format === void 0) { format = ''; }
|
261 | if (quality === void 0) { quality = 95; }
|
262 | if (progressive === void 0) { progressive = false; }
|
263 | if (optimizeSize === void 0) { optimizeSize = false; }
|
264 | if (chromaDownsampling === void 0) { chromaDownsampling = true; }
|
265 | if (densityUnit === void 0) { densityUnit = 'in'; }
|
266 | if (xDensity === void 0) { xDensity = 300; }
|
267 | if (yDensity === void 0) { yDensity = 300; }
|
268 | if (xmpMetadata === void 0) { xmpMetadata = ''; }
|
269 | return __awaiter(this, void 0, void 0, function () {
|
270 | var backendEncodeImage;
|
271 | return __generator(this, function (_a) {
|
272 | nodejs_kernel_backend_1.ensureTensorflowBackend();
|
273 | backendEncodeImage = function (imageData) {
|
274 | return nodejs_kernel_backend_1.nodeBackend().encodeJpeg(imageData, image.shape, format, quality, progressive, optimizeSize, chromaDownsampling, densityUnit, xDensity, yDensity, xmpMetadata);
|
275 | };
|
276 | return [2 /*return*/, encodeImage(image, backendEncodeImage)];
|
277 | });
|
278 | });
|
279 | }
|
280 | exports.encodeJpeg = encodeJpeg;
|
281 | /**
|
282 | * Encodes an image tensor to PNG.
|
283 | *
|
284 | * @param image A 3-D uint8 Tensor of shape [height, width, channels].
|
285 | * @param compression An optional int. Defaults to -1. Compression level.
|
286 | * @returns The PNG encoded data as an Uint8Array.
|
287 | */
|
288 | /**
|
289 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
|
290 | */
|
291 | function encodePng(image, compression) {
|
292 | if (compression === void 0) { compression = 1; }
|
293 | return __awaiter(this, void 0, void 0, function () {
|
294 | var backendEncodeImage;
|
295 | return __generator(this, function (_a) {
|
296 | nodejs_kernel_backend_1.ensureTensorflowBackend();
|
297 | backendEncodeImage = function (imageData) {
|
298 | return nodejs_kernel_backend_1.nodeBackend().encodePng(imageData, image.shape, compression);
|
299 | };
|
300 | return [2 /*return*/, encodeImage(image, backendEncodeImage)];
|
301 | });
|
302 | });
|
303 | }
|
304 | exports.encodePng = encodePng;
|
305 | function encodeImage(image, backendEncodeImage) {
|
306 | return __awaiter(this, void 0, void 0, function () {
|
307 | var encodedDataTensor, _a, _b, encodedPngData;
|
308 | return __generator(this, function (_c) {
|
309 | switch (_c.label) {
|
310 | case 0:
|
311 | _a = backendEncodeImage;
|
312 | _b = Uint8Array.bind;
|
313 | return [4 /*yield*/, image.data()];
|
314 | case 1:
|
315 | encodedDataTensor = _a.apply(void 0, [new (_b.apply(Uint8Array, [void 0, _c.sent()]))()]);
|
316 | // tslint:disable-next-line:no-any
|
317 | return [4 /*yield*/, encodedDataTensor.data()];
|
318 | case 2:
|
319 | encodedPngData = (
|
320 | // tslint:disable-next-line:no-any
|
321 | _c.sent())[0];
|
322 | encodedDataTensor.dispose();
|
323 | return [2 /*return*/, encodedPngData];
|
324 | }
|
325 | });
|
326 | });
|
327 | }
|
328 | /**
|
329 | * Helper function to get image type based on starting bytes of the image file.
|
330 | */
|
331 | function getImageType(content) {
|
332 | // Classify the contents of a file based on starting bytes (aka magic number:
|
333 | // https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files)
|
334 | // This aligns with TensorFlow Core code:
|
335 | // https://github.com/tensorflow/tensorflow/blob/4213d5c1bd921f8d5b7b2dc4bbf1eea78d0b5258/tensorflow/core/kernels/decode_image_op.cc#L44
|
336 | if (content.length > 3 && content[0] === 255 && content[1] === 216 &&
|
337 | content[2] === 255) {
|
338 | // JPEG byte chunk starts with `ff d8 ff`
|
339 | return ImageType.JPEG;
|
340 | }
|
341 | else if (content.length > 4 && content[0] === 71 && content[1] === 73 &&
|
342 | content[2] === 70 && content[3] === 56) {
|
343 | // GIF byte chunk starts with `47 49 46 38`
|
344 | return ImageType.GIF;
|
345 | }
|
346 | else if (content.length > 8 && content[0] === 137 && content[1] === 80 &&
|
347 | content[2] === 78 && content[3] === 71 && content[4] === 13 &&
|
348 | content[5] === 10 && content[6] === 26 && content[7] === 10) {
|
349 | // PNG byte chunk starts with `\211 P N G \r \n \032 \n (89 50 4E 47 0D 0A
|
350 | // 1A 0A)`
|
351 | return ImageType.PNG;
|
352 | }
|
353 | else if (content.length > 3 && content[0] === 66 && content[1] === 77) {
|
354 | // BMP byte chunk starts with `42 4d`
|
355 | return ImageType.BMP;
|
356 | }
|
357 | else {
|
358 | throw new Error('Expected image (BMP, JPEG, PNG, or GIF), but got unsupported ' +
|
359 | 'image type');
|
360 | }
|
361 | }
|
362 | exports.getImageType = getImageType;
|