UNPKG

8.22 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google LLC. 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 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
52 */
53export declare function decodeJpeg(contents: Uint8Array, channels?: number, ratio?: number, fancyUpscaling?: boolean, tryRecoverTruncated?: boolean, acceptableFraction?: number, dctMethod?: string): Tensor3D;
54/**
55 * Decode a PNG-encoded image to a 3D Tensor of dtype `int32`.
56 *
57 * @param contents The PNG-encoded image in an Uint8Array.
58 * @param channels An optional int. Defaults to 0. Accepted values are
59 * 0: use the number of channels in the PNG-encoded image.
60 * 1: output a grayscale image.
61 * 3: output an RGB image.
62 * 4: output an RGBA image.
63 * @param dtype The data type of the result. Only `int32` is supported at this
64 * time.
65 * @returns A 3D Tensor of dtype `int32` with shape [height, width, 1/3/4].
66 *
67 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
68 */
69export declare function decodePng(contents: Uint8Array, channels?: number, dtype?: string): Tensor3D;
70/**
71 * Decode the first frame of a BMP-encoded image to a 3D Tensor of dtype
72 * `int32`.
73 *
74 * @param contents The BMP-encoded image in an Uint8Array.
75 * @param channels An optional int. Defaults to 0. Accepted values are
76 * 0: use the number of channels in the BMP-encoded image.
77 * 3: output an RGB image.
78 * 4: output an RGBA image.
79 * @returns A 3D Tensor of dtype `int32` with shape [height, width, 3/4].
80 *
81 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
82 */
83export declare function decodeBmp(contents: Uint8Array, channels?: number): Tensor3D;
84/**
85 * Decode the frame(s) of a GIF-encoded image to a 4D Tensor of dtype `int32`.
86 *
87 * @param contents The GIF-encoded image in an Uint8Array.
88 * @returns A 4D Tensor of dtype `int32` with shape [num_frames, height, width,
89 * 3]. RGB channel order.
90 *
91 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
92 */
93export declare function decodeGif(contents: Uint8Array): Tensor4D;
94/**
95 * Given the encoded bytes of an image, it returns a 3D or 4D tensor of the
96 * decoded image. Supports BMP, GIF, JPEG and PNG formats.
97 *
98 * @param content The encoded image in an Uint8Array.
99 * @param channels An optional int. Defaults to 0, use the number of channels in
100 * the image. Number of color channels for the decoded image. It is used
101 * when image is type Png, Bmp, or Jpeg.
102 * @param dtype The data type of the result. Only `int32` is supported at this
103 * time.
104 * @param expandAnimations A boolean which controls the shape of the returned
105 * op's output. If True, the returned op will produce a 3-D tensor for PNG,
106 * JPEG, and BMP files; and a 4-D tensor for all GIFs, whether animated or
107 * not. If, False, the returned op will produce a 3-D tensor for all file
108 * types and will truncate animated GIFs to the first frame.
109 * @returns A Tensor with dtype `int32` and a 3- or 4-dimensional shape,
110 * depending on the file type. For gif file the returned Tensor shape is
111 * [num_frames, height, width, 3], and for jpeg/png/bmp the returned Tensor
112 * shape is [height, width, channels]
113 *
114 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
115 */
116export declare function decodeImage(content: Uint8Array, channels?: number, dtype?: string, expandAnimations?: boolean): Tensor3D | Tensor4D;
117/**
118 * Encodes an image tensor to JPEG.
119 *
120 * @param image A 3-D uint8 Tensor of shape [height, width, channels].
121 * @param format An optional string from: "", "grayscale", "rgb".
122 * Defaults to "". Per pixel image format.
123 * - '': Use a default format based on the number of channels in the image.
124 * - grayscale: Output a grayscale JPEG image. The channels dimension of
125 * image must be 1.
126 * - rgb: Output an RGB JPEG image. The channels dimension of image must
127 * be 3.
128 * @param quality An optional int. Defaults to 95. Quality of the compression
129 * from 0 to 100 (higher is better and slower).
130 * @param progressive An optional bool. Defaults to False. If True, create a
131 * JPEG that loads progressively (coarse to fine).
132 * @param optimizeSize An optional bool. Defaults to False. If True, spend
133 * CPU/RAM to reduce size with no quality change.
134 * @param chromaDownsampling An optional bool. Defaults to True.
135 * See http://en.wikipedia.org/wiki/Chroma_subsampling.
136 * @param densityUnit An optional string from: "in", "cm". Defaults to "in".
137 * Unit used to specify x_density and y_density: pixels per inch ('in') or
138 * centimeter ('cm').
139 * @param xDensity An optional int. Defaults to 300. Horizontal pixels per
140 * density unit.
141 * @param yDensity An optional int. Defaults to 300. Vertical pixels per
142 * density unit.
143 * @param xmpMetadata An optional string. Defaults to "". If not empty, embed
144 * this XMP metadata in the image header.
145 * @returns The JPEG encoded data as an Uint8Array.
146 *
147 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
148 */
149export 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>;
150/**
151 * Encodes an image tensor to PNG.
152 *
153 * @param image A 3-D uint8 Tensor of shape [height, width, channels].
154 * @param compression An optional int. Defaults to -1. Compression level.
155 * @returns The PNG encoded data as an Uint8Array.
156 *
157 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
158 */
159export declare function encodePng(image: Tensor3D, compression?: number): Promise<Uint8Array>;
160/**
161 * Helper function to get image type based on starting bytes of the image file.
162 */
163export declare function getImageType(content: Uint8Array): string;