UNPKG

8.25 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google Inc. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import { Tensor3D, Tensor4D } from '@tensorflow/tfjs';
18export declare enum ImageType {
19 JPEG = "jpeg",
20 PNG = "png",
21 GIF = "gif",
22 BMP = "BMP"
23}
24/**
25 * Decode a JPEG-encoded image to a 3D Tensor of dtype `int32`.
26 *
27 * @param contents The JPEG-encoded image in an Uint8Array.
28 * @param channels An optional int. Defaults to 0. Accepted values are
29 * 0: use the number of channels in the JPEG-encoded image.
30 * 1: output a grayscale image.
31 * 3: output an RGB image.
32 * @param ratio An optional int. Defaults to 1. Downscaling ratio. It is used
33 * when image is type Jpeg.
34 * @param fancyUpscaling An optional bool. Defaults to True. If true use a
35 * slower but nicer upscaling of the chroma planes. It is used when image is
36 * type Jpeg.
37 * @param tryRecoverTruncated An optional bool. Defaults to False. If true try
38 * to recover an image from truncated input. It is used when image is type
39 * Jpeg.
40 * @param acceptableFraction An optional float. Defaults to 1. The minimum
41 * required fraction of lines before a truncated input is accepted. It is
42 * used when image is type Jpeg.
43 * @param dctMethod An optional string. Defaults to "". string specifying a hint
44 * about the algorithm used for decompression. Defaults to "" which maps to
45 * a system-specific default. Currently valid values are ["INTEGER_FAST",
46 * "INTEGER_ACCURATE"]. The hint may be ignored (e.g., the internal jpeg
47 * library changes to a version that does not have that specific option.) It
48 * is used when image is type Jpeg.
49 * @returns A 3D Tensor of dtype `int32` with shape [height, width, 1/3].
50 */
51/**
52 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
53 */
54export declare function decodeJpeg(contents: Uint8Array, channels?: number, ratio?: number, fancyUpscaling?: boolean, tryRecoverTruncated?: boolean, acceptableFraction?: number, dctMethod?: string): Tensor3D;
55/**
56 * Decode a PNG-encoded image to a 3D Tensor of dtype `int32`.
57 *
58 * @param contents The PNG-encoded image in an Uint8Array.
59 * @param channels An optional int. Defaults to 0. Accepted values are
60 * 0: use the number of channels in the PNG-encoded image.
61 * 1: output a grayscale image.
62 * 3: output an RGB image.
63 * 4: output an RGBA image.
64 * @param dtype The data type of the result. Only `int32` is supported at this
65 * time.
66 * @returns A 3D Tensor of dtype `int32` with shape [height, width, 1/3/4].
67 */
68/**
69 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
70 */
71export declare function decodePng(contents: Uint8Array, channels?: number, dtype?: string): Tensor3D;
72/**
73 * Decode the first frame of a BMP-encoded image to a 3D Tensor of dtype
74 * `int32`.
75 *
76 * @param contents The BMP-encoded image in an Uint8Array.
77 * @param channels An optional int. Defaults to 0. Accepted values are
78 * 0: use the number of channels in the BMP-encoded image.
79 * 3: output an RGB image.
80 * 4: output an RGBA image.
81 * @returns A 3D Tensor of dtype `int32` with shape [height, width, 3/4].
82 */
83/**
84 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
85 */
86export declare function decodeBmp(contents: Uint8Array, channels?: number): Tensor3D;
87/**
88 * Decode the frame(s) of a GIF-encoded image to a 4D Tensor of dtype `int32`.
89 *
90 * @param contents The GIF-encoded image in an Uint8Array.
91 * @returns A 4D Tensor of dtype `int32` with shape [num_frames, height, width,
92 * 3]. RGB channel order.
93 */
94/**
95 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
96 */
97export declare function decodeGif(contents: Uint8Array): Tensor4D;
98/**
99 * Given the encoded bytes of an image, it returns a 3D or 4D tensor of the
100 * decoded image. Supports BMP, GIF, JPEG and PNG formats.
101 *
102 * @param content The encoded image in an Uint8Array.
103 * @param channels An optional int. Defaults to 0, use the number of channels in
104 * the image. Number of color channels for the decoded image. It is used
105 * when image is type Png, Bmp, or Jpeg.
106 * @param dtype The data type of the result. Only `int32` is supported at this
107 * time.
108 * @param expandAnimations A boolean which controls the shape of the returned
109 * op's output. If True, the returned op will produce a 3-D tensor for PNG,
110 * JPEG, and BMP files; and a 4-D tensor for all GIFs, whether animated or
111 * not. If, False, the returned op will produce a 3-D tensor for all file
112 * types and will truncate animated GIFs to the first frame.
113 * @returns A Tensor with dtype `int32` and a 3- or 4-dimensional shape,
114 * depending on the file type. For gif file the returned Tensor shape is
115 * [num_frames, height, width, 3], and for jpeg/png/bmp the returned Tensor
116 * shape is [height, width, channels]
117 */
118/**
119 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
120 */
121export declare function decodeImage(content: Uint8Array, channels?: number, dtype?: string, expandAnimations?: boolean): Tensor3D | Tensor4D;
122/**
123 * Encodes an image tensor to JPEG.
124 *
125 * @param image A 3-D uint8 Tensor of shape [height, width, channels].
126 * @param format An optional string from: "", "grayscale", "rgb".
127 * Defaults to "". Per pixel image format.
128 * - '': Use a default format based on the number of channels in the image.
129 * - grayscale: Output a grayscale JPEG image. The channels dimension of
130 * image must be 1.
131 * - rgb: Output an RGB JPEG image. The channels dimension of image must
132 * be 3.
133 * @param quality An optional int. Defaults to 95. Quality of the compression
134 * from 0 to 100 (higher is better and slower).
135 * @param progressive An optional bool. Defaults to False. If True, create a
136 * JPEG that loads progressively (coarse to fine).
137 * @param optimizeSize An optional bool. Defaults to False. If True, spend
138 * CPU/RAM to reduce size with no quality change.
139 * @param chromaDownsampling An optional bool. Defaults to True.
140 * See http://en.wikipedia.org/wiki/Chroma_subsampling.
141 * @param densityUnit An optional string from: "in", "cm". Defaults to "in".
142 * Unit used to specify x_density and y_density: pixels per inch ('in') or
143 * centimeter ('cm').
144 * @param xDensity An optional int. Defaults to 300. Horizontal pixels per
145 * density unit.
146 * @param yDensity An optional int. Defaults to 300. Vertical pixels per
147 * density unit.
148 * @param xmpMetadata An optional string. Defaults to "". If not empty, embed
149 * this XMP metadata in the image header.
150 * @returns The JPEG encoded data as an Uint8Array.
151 */
152/**
153 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
154 */
155export declare function encodeJpeg(image: Tensor3D, format?: '' | 'grayscale' | 'rgb', quality?: number, progressive?: boolean, optimizeSize?: boolean, chromaDownsampling?: boolean, densityUnit?: 'in' | 'cm', xDensity?: number, yDensity?: number, xmpMetadata?: string): Promise<Uint8Array>;
156/**
157 * Encodes an image tensor to PNG.
158 *
159 * @param image A 3-D uint8 Tensor of shape [height, width, channels].
160 * @param compression An optional int. Defaults to -1. Compression level.
161 * @returns The PNG encoded data as an Uint8Array.
162 */
163/**
164 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
165 */
166export declare function encodePng(image: Tensor3D, compression?: number): Promise<Uint8Array>;
167/**
168 * Helper function to get image type based on starting bytes of the image file.
169 */
170export declare function getImageType(content: Uint8Array): string;