Refactor detectContentType to prepare for animation detection (flutter/engine#54127)

Refactor in preparation for https://github.com/flutter/flutter/issues/151911

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Harry Terkelsen
2024-07-25 15:59:24 -07:00
committed by GitHub
parent 32c2cbf531
commit bb753d807f
7 changed files with 244 additions and 167 deletions

View File

@@ -42978,6 +42978,7 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/html/surface_stats.dart + ../
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/html/transform.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/html_image_element_codec.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/image_decoder.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/image_format_detector.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/initialization.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/js_interop/js_app.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/js_interop/js_loader.dart + ../../../flutter/LICENSE
@@ -45868,6 +45869,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/surface_stats.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/transform.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html_image_element_codec.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/image_decoder.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/image_format_detector.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/initialization.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/js_interop/js_app.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/js_interop/js_loader.dart

View File

@@ -108,6 +108,7 @@ export 'engine/html/surface_stats.dart';
export 'engine/html/transform.dart';
export 'engine/html_image_element_codec.dart';
export 'engine/image_decoder.dart';
export 'engine/image_format_detector.dart';
export 'engine/initialization.dart';
export 'engine/js_interop/js_app.dart';
export 'engine/js_interop/js_loader.dart';

View File

@@ -17,20 +17,16 @@ Future<ui.Codec> skiaInstantiateImageCodec(Uint8List list,
ui.Codec codec;
// ImageDecoder does not detect image type automatically. It requires us to
// tell it what the image type is.
final String contentType = tryDetectContentType(list, 'encoded image bytes');
final ImageType imageType = tryDetectImageType(list, 'encoded image bytes');
if (browserSupportsImageDecoder) {
codec = await CkBrowserImageDecoder.create(
data: list,
contentType: contentType,
contentType: imageType.mimeType,
debugSource: 'encoded image bytes',
);
} else {
// TODO(harryterkelsen): If the image is animated, then use Skia to decode.
// This is currently too conservative, assuming all GIF and WEBP images are
// animated. We should detect if they are actually animated by reading the
// image headers, https://github.com/flutter/flutter/issues/151911.
if (contentType == 'image/gif' || contentType == 'image/webp') {
if (imageType.isAnimated) {
codec = CkAnimatedImage.decodeFromBytes(list, 'encoded image bytes',
targetWidth: targetWidth, targetHeight: targetHeight);
} else {
@@ -336,10 +332,10 @@ Future<ui.Codec> skiaInstantiateWebImageCodec(
} on ImageCodecException {
imageElementCodec.dispose();
final Uint8List list = await fetchImage(url, chunkCallback);
final String contentType = tryDetectContentType(list, url);
final ImageType imageType = tryDetectImageType(list, url);
if (browserSupportsImageDecoder) {
return CkBrowserImageDecoder.create(
data: list, contentType: contentType, debugSource: url);
data: list, contentType: imageType.mimeType, debugSource: url);
} else {
final DomBlob blob = createDomBlob(<ByteBuffer>[list.buffer]);
final CkImageBlobCodec codec = CkImageBlobCodec(blob);
@@ -623,13 +619,13 @@ class CkImage implements ui.Image, StackTraceDebugger {
}
}
/// Detect the content type or throw an error if content type can't be detected.
String tryDetectContentType(Uint8List data, String debugSource) {
/// Detect the image type or throw an error if image type can't be detected.
ImageType tryDetectImageType(Uint8List data, String debugSource) {
// ImageDecoder does not detect image type automatically. It requires us to
// tell it what the image type is.
final String? contentType = detectContentType(data);
final ImageType? imageType = detectImageType(data);
if (contentType == null) {
if (imageType == null) {
final String fileHeader;
if (data.isNotEmpty) {
fileHeader =
@@ -642,7 +638,7 @@ String tryDetectContentType(Uint8List data, String debugSource) {
'File header was $fileHeader.\n'
'Image source: $debugSource');
}
return contentType;
return imageType;
}
sealed class ImageSource {

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:meta/meta.dart';
import 'package:ui/src/engine.dart';
@@ -207,155 +206,6 @@ class AnimatedImageFrameInfo implements ui.FrameInfo {
final ui.Image image;
}
/// Detects the image file format and returns the corresponding "Content-Type"
/// value (a.k.a. MIME type).
///
/// The returned value can be passed to `ImageDecoder` when decoding an image.
///
/// Returns null if [data] cannot be mapped to a known content type.
String? detectContentType(Uint8List data) {
if (debugContentTypeDetector != null) {
return debugContentTypeDetector!.call(data);
}
formatLoop:
for (final ImageFileFormat format in ImageFileFormat.values) {
if (data.length < format.header.length) {
continue;
}
for (int i = 0; i < format.header.length; i++) {
final int? magicByte = format.header[i];
if (magicByte == null) {
// Wildcard, accepts everything.
continue;
}
final int headerByte = data[i];
if (headerByte != magicByte) {
continue formatLoop;
}
}
return format.contentType;
}
if (isAvif(data)) {
return 'image/avif';
}
return null;
}
/// Represents an image file format, such as PNG or JPEG.
class ImageFileFormat {
const ImageFileFormat(this.header, this.contentType);
/// First few bytes in the file that uniquely identify the image file format.
///
/// Null elements are treated as wildcard values and are not checked. This is
/// used to detect formats whose header is split up into multiple disjoint
/// parts, such that the first part is not unique enough to identify the
/// format. For example, without this, WebP may be confused with .ani
/// (animated cursor), .cda, and other formats that start with "RIFF".
final List<int?> header;
/// The value that's passed as [_ImageDecoderOptions.type].
///
/// The server typically also uses this value as the "Content-Type" header,
/// but servers are not required to correctly detect the type. This value
/// is also known as MIME type.
final String contentType;
/// All image file formats known to the Flutter Web engine.
///
/// This list may need to be changed as browsers adopt new formats, and drop
/// support for obsolete ones.
///
/// This list is checked linearly from top to bottom when detecting an image
/// type. It should therefore contain the most popular file formats at the
/// top, and less popular towards the bottom.
static const List<ImageFileFormat> values = <ImageFileFormat>[
// ICO is not supported in Chrome. It is deemed too simple and too specific. See also:
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/webcodecs/image_decoder_external.cc;l=38;drc=fd8802b593110ea18a97ef044f8a40dd24a622ec
// PNG
ImageFileFormat(
<int?>[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], 'image/png'),
// GIF87a
ImageFileFormat(<int?>[0x47, 0x49, 0x46, 0x38, 0x37, 0x61], 'image/gif'),
// GIF89a
ImageFileFormat(<int?>[0x47, 0x49, 0x46, 0x38, 0x39, 0x61], 'image/gif'),
// JPEG
ImageFileFormat(<int?>[0xFF, 0xD8, 0xFF], 'image/jpeg'),
// WebP
ImageFileFormat(<int?>[
0x52,
0x49,
0x46,
0x46,
null,
null,
null,
null,
0x57,
0x45,
0x42,
0x50
], 'image/webp'),
// BMP
ImageFileFormat(<int?>[0x42, 0x4D], 'image/bmp'),
];
}
/// Function signature of [debugContentTypeDetector], which is the same as the
/// signature of [detectContentType].
typedef DebugContentTypeDetector = String? Function(Uint8List);
/// If not null, replaces the functionality of [detectContentType] with its own.
///
/// This is useful in tests, for example, to test unsupported content types.
DebugContentTypeDetector? debugContentTypeDetector;
/// A string of bytes that every AVIF image contains somewhere in its first 16
/// bytes.
///
/// This signature is necessary but not sufficient, which may lead to false
/// positives. For example, the file may be HEIC or a video. This is OK,
/// because in the worst case, the image decoder fails to decode the file.
/// This is something we must anticipate regardless of this detection logic.
/// The codec must already protect itself from downloaded files lying about
/// their contents.
///
/// The alternative would be to implement a more precise detection, which would
/// add complexity and code size. This is how Chromium does it:
///
/// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/image-decoders/avif/avif_image_decoder.cc;l=504;drc=fd8802b593110ea18a97ef044f8a40dd24a622ec
final List<int> _avifSignature = 'ftyp'.codeUnits;
/// Optimistically detects whether [data] is an AVIF image file.
bool isAvif(Uint8List data) {
firstByteLoop:
for (int i = 0; i < 16; i += 1) {
for (int j = 0; j < _avifSignature.length; j += 1) {
if (i + j >= data.length) {
// Reached EOF without finding the signature.
return false;
}
if (data[i + j] != _avifSignature[j]) {
continue firstByteLoop;
}
}
return true;
}
return false;
}
// Wraps another codec and resizes each output image.
class ResizingCodec implements ui.Codec {
ResizingCodec(

View File

@@ -0,0 +1,227 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
/// Detects the image file format and returns the corresponding "Content-Type"
/// value (a.k.a. MIME type).
///
/// The returned value can be passed to `ImageDecoder` when decoding an image.
///
/// Returns null if [data] cannot be mapped to a known content type.
ImageType? detectImageType(Uint8List data) {
if (debugImageTypeDetector != null) {
return debugImageTypeDetector!.call(data);
}
formatLoop:
for (final ImageFileFormat format in ImageFileFormat.values) {
if (data.length < format.header.length) {
continue;
}
for (int i = 0; i < format.header.length; i++) {
final int? magicByte = format.header[i];
if (magicByte == null) {
// Wildcard, accepts everything.
continue;
}
final int headerByte = data[i];
if (headerByte != magicByte) {
continue formatLoop;
}
}
final ImageFileType fileType = format.fileType;
// TODO(harryterkelsen): If the image is animated, we use Skia to decode.
// This is currently too conservative, assuming all GIF and WEBP images are
// animated. We should detect if they are actually animated by reading the
// image headers, https://github.com/flutter/flutter/issues/151911.
return ImageType(fileType,
isAnimated:
fileType == ImageFileType.gif || fileType == ImageFileType.webp);
}
if (isAvif(data)) {
return const ImageType(ImageFileType.avif);
}
return null;
}
/// The file format of the image, and whether or not it is animated.
class ImageType {
const ImageType(this.fileType, {this.isAnimated = false});
final ImageFileType fileType;
final bool isAnimated;
/// The MIME type for this image (e.g 'image/jpeg').
String get mimeType {
String fileString;
switch (fileType) {
case ImageFileType.png:
fileString = 'png';
case ImageFileType.gif:
fileString = 'gif';
case ImageFileType.jpeg:
fileString = 'jpeg';
case ImageFileType.webp:
fileString = 'webp';
case ImageFileType.bmp:
fileString = 'bmp';
case ImageFileType.avif:
fileString = 'avif';
}
return 'image/$fileString';
}
@override
bool operator ==(Object other) {
if (other is! ImageType) {
return false;
}
return other.fileType == fileType && other.isAnimated == isAnimated;
}
@override
int get hashCode => Object.hash(fileType, isAnimated);
}
/// The supported image file formats in Flutter Web.
enum ImageFileType {
png,
gif,
jpeg,
webp,
bmp,
avif,
}
/// Represents an image file format, such as PNG or JPEG.
class ImageFileFormat {
const ImageFileFormat(this.header, this.fileType);
/// First few bytes in the file that uniquely identify the image file format.
///
/// Null elements are treated as wildcard values and are not checked. This is
/// used to detect formats whose header is split up into multiple disjoint
/// parts, such that the first part is not unique enough to identify the
/// format. For example, without this, WebP may be confused with .ani
/// (animated cursor), .cda, and other formats that start with "RIFF".
final List<int?> header;
/// The value that's passed as [_ImageDecoderOptions.type].
///
/// The server typically also uses this value as the "Content-Type" header,
/// but servers are not required to correctly detect the type. This value
/// is also known as MIME type.
final ImageFileType fileType;
/// All image file formats known to the Flutter Web engine.
///
/// This list may need to be changed as browsers adopt new formats, and drop
/// support for obsolete ones.
///
/// This list is checked linearly from top to bottom when detecting an image
/// type. It should therefore contain the most popular file formats at the
/// top, and less popular towards the bottom.
static const List<ImageFileFormat> values = <ImageFileFormat>[
// ICO is not supported in Chrome. It is deemed too simple and too specific. See also:
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/webcodecs/image_decoder_external.cc;l=38;drc=fd8802b593110ea18a97ef044f8a40dd24a622ec
// PNG
ImageFileFormat(
<int?>[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A],
ImageFileType.png,
),
// GIF87a
ImageFileFormat(
<int?>[0x47, 0x49, 0x46, 0x38, 0x37, 0x61],
ImageFileType.gif,
),
// GIF89a
ImageFileFormat(
<int?>[0x47, 0x49, 0x46, 0x38, 0x39, 0x61],
ImageFileType.gif,
),
// JPEG
ImageFileFormat(
<int?>[0xFF, 0xD8, 0xFF],
ImageFileType.jpeg,
),
// WebP
ImageFileFormat(
<int?>[
0x52,
0x49,
0x46,
0x46,
null,
null,
null,
null,
0x57,
0x45,
0x42,
0x50
],
ImageFileType.webp,
),
// BMP
ImageFileFormat(
<int?>[0x42, 0x4D],
ImageFileType.bmp,
),
];
}
/// Function signature of [debugImageTypeDetector], which is the same as the
/// signature of [detectImageType].
typedef DebugImageTypeDetector = ImageType? Function(Uint8List);
/// If not null, replaces the functionality of [detectImageType] with its own.
///
/// This is useful in tests, for example, to test unsupported content types.
DebugImageTypeDetector? debugImageTypeDetector;
/// A string of bytes that every AVIF image contains somewhere in its first 16
/// bytes.
///
/// This signature is necessary but not sufficient, which may lead to false
/// positives. For example, the file may be HEIC or a video. This is OK,
/// because in the worst case, the image decoder fails to decode the file.
/// This is something we must anticipate regardless of this detection logic.
/// The codec must already protect itself from downloaded files lying about
/// their contents.
///
/// The alternative would be to implement a more precise detection, which would
/// add complexity and code size. This is how Chromium does it:
///
/// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/image-decoders/avif/avif_image_decoder.cc;l=504;drc=fd8802b593110ea18a97ef044f8a40dd24a622ec
final List<int> _avifSignature = 'ftyp'.codeUnits;
/// Optimistically detects whether [data] is an AVIF image file.
bool isAvif(Uint8List data) {
firstByteLoop:
for (int i = 0; i < 16; i += 1) {
for (int j = 0; j < _avifSignature.length; j += 1) {
if (i + j >= data.length) {
// Reached EOF without finding the signature.
return false;
}
if (data[i + j] != _avifSignature[j]) {
continue firstByteLoop;
}
}
return true;
}
return false;
}

View File

@@ -357,12 +357,12 @@ class SkwasmRenderer implements Renderer {
int? targetHeight,
bool allowUpscaling = true
}) async {
final String? contentType = detectContentType(list);
final ImageType? contentType = detectImageType(list);
if (contentType == null) {
throw Exception('Could not determine content type of image from data');
}
final SkwasmImageDecoder baseDecoder = SkwasmImageDecoder(
contentType: contentType,
contentType: contentType.mimeType,
dataSource: list.toJS,
debugSource: 'encoded image bytes',
);

View File

@@ -316,7 +316,8 @@ void _testCkAnimatedImage() {
// The precise PNG encoding is browser-specific, but we can check the file
// signature.
expect(detectContentType(png!.buffer.asUint8List()), 'image/png');
expect(detectImageType(png!.buffer.asUint8List()),
const ImageType(ImageFileType.png));
});
test('CkAnimatedImage toByteData(RGBA)', () async {