(
gl: WebGL2RenderingContext, texture: WebGLTexture, sourceDims: Dimensions,
targetShape: Dimensions, useCustomShadersToResize = false,
options: FromTextureOptions = {})
| 143 | * @doc {heading: 'Media', subheading: 'Camera'} |
| 144 | */ |
| 145 | export function fromTexture( |
| 146 | gl: WebGL2RenderingContext, texture: WebGLTexture, sourceDims: Dimensions, |
| 147 | targetShape: Dimensions, useCustomShadersToResize = false, |
| 148 | options: FromTextureOptions = {}): tf.Tensor3D { |
| 149 | tf.util.assert( |
| 150 | targetShape.depth === 3 || targetShape.depth === 4, |
| 151 | () => 'fromTexture Error: target depth must be 3 or 4'); |
| 152 | |
| 153 | if (targetShape.depth === 3 && targetShape.width % 4 !== 0) { |
| 154 | // We throw an error here rather than use the CPU workaround as the user is |
| 155 | // likely trying to get the maximum performance. |
| 156 | if (glCapabilities.canDownloadFromRGBTexture.get(gl)) { |
| 157 | // See |
| 158 | // https://www.khronos.org/opengl/wiki/Common_Mistakes#Texture_upload_and_pixel_reads |
| 159 | // for more details. At the moment gl.pixelStorei(gl.PACK_ALIGNMENT, 1); |
| 160 | // is not supported on expo. "EXGL: gl.pixelStorei() doesn't support this |
| 161 | // parameter yet!" |
| 162 | throw new Error( |
| 163 | 'When using targetShape.depth=3, targetShape.width must be' + |
| 164 | ' a multiple of 4. Alternatively do not call detectGLCapabilities()'); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | const originalTargetDepth = targetShape.depth; |
| 169 | const targetDepth = glCapabilities.canDownloadFromRGBTexture.get(gl) ? |
| 170 | originalTargetDepth : |
| 171 | 4; |
| 172 | |
| 173 | sourceDims = { |
| 174 | height: Math.floor(sourceDims.height), |
| 175 | width: Math.floor(sourceDims.width), |
| 176 | depth: sourceDims.depth, |
| 177 | }; |
| 178 | |
| 179 | targetShape = { |
| 180 | height: Math.floor(targetShape.height), |
| 181 | width: Math.floor(targetShape.width), |
| 182 | depth: targetDepth |
| 183 | }; |
| 184 | |
| 185 | const alignCorners = |
| 186 | options.alignCorners != null ? options.alignCorners : false; |
| 187 | const interpolation = |
| 188 | options.interpolation != null ? options.interpolation : 'bilinear'; |
| 189 | const rotation = options.rotation != null ? options.rotation : 0; |
| 190 | |
| 191 | tf.util.assert( |
| 192 | interpolation === 'bilinear' || interpolation === 'nearest_neighbor', |
| 193 | () => 'fromTexture Error: interpolation must be one of' + |
| 194 | ' "bilinear" or "nearest_neighbor"'); |
| 195 | |
| 196 | tf.util.assert( |
| 197 | [0, 90, 180, 270, 360, -90, -180, -270].includes(rotation), |
| 198 | () => 'fromTexture Error: rotation must be ' + |
| 199 | '0, +/- 90, +/- 180, +/- 270 or 360'); |
| 200 | |
| 201 | const resizedTexture = runResizeProgram( |
| 202 | gl, texture, sourceDims, targetShape, alignCorners, |
no test coverage detected
searching dependent graphs…