Skip to content

Commit 2dc34e5

Browse files
authored
Merge pull request #2 from mathis6787/dev
Dev
2 parents 9e0c995 + bff98ec commit 2dc34e5

File tree

5 files changed

+169
-34
lines changed

5 files changed

+169
-34
lines changed

native/src/api.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ impl ImageFormatEnum {
4040
ImageFormatEnum::Tiff => ImageFormat::Tiff,
4141
}
4242
}
43+
44+
pub fn from_image_format(format: ImageFormat) -> Option<Self> {
45+
match format {
46+
ImageFormat::Png => Some(ImageFormatEnum::Png),
47+
ImageFormat::Jpeg => Some(ImageFormatEnum::Jpeg),
48+
ImageFormat::Gif => Some(ImageFormatEnum::Gif),
49+
ImageFormat::WebP => Some(ImageFormatEnum::WebP),
50+
ImageFormat::Bmp => Some(ImageFormatEnum::Bmp),
51+
ImageFormat::Ico => Some(ImageFormatEnum::Ico),
52+
ImageFormat::Tiff => Some(ImageFormatEnum::Tiff),
53+
_ => None,
54+
}
55+
}
4356
}
4457

4558
#[allow(dead_code)]
@@ -224,6 +237,18 @@ pub fn get_metadata(img: &DynamicImage) -> ImageMetadata {
224237
}
225238
}
226239

240+
/// Guess image format from byte data
241+
pub fn guess_image_format(data: &[u8]) -> Result<ImageFormatEnum, ImageError> {
242+
let format = image::guess_format(data)?;
243+
ImageFormatEnum::from_image_format(format)
244+
.ok_or_else(|| {
245+
ImageError::Unsupported(image::error::UnsupportedError::from_format_and_kind(
246+
image::error::ImageFormatHint::Unknown,
247+
image::error::UnsupportedErrorKind::Format(image::error::ImageFormatHint::Unknown),
248+
))
249+
})
250+
}
251+
227252
/// Convert ImageError to error code
228253
pub fn error_to_code(err: &ImageError) -> ImageErrorCode {
229254
match err {

native/src/ffi.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ pub extern "C" fn fast_image_load_from_memory_with_format(
100100
}
101101
}
102102

103+
// ============================================================================
104+
// Format Detection
105+
// ============================================================================
106+
107+
/// Guess image format from byte data
108+
/// Returns the format enum value or ImageErrorCode on error
109+
#[unsafe(no_mangle)]
110+
pub extern "C" fn fast_image_guess_format(
111+
data: *const u8,
112+
len: usize,
113+
out_format: *mut u32,
114+
) -> ImageErrorCode {
115+
if data.is_null() || len == 0 || out_format.is_null() {
116+
return ImageErrorCode::InvalidPointer;
117+
}
118+
119+
let buffer = unsafe { slice::from_raw_parts(data, len) };
120+
121+
match guess_image_format(buffer) {
122+
Ok(format) => {
123+
unsafe {
124+
*out_format = format as u32;
125+
}
126+
ImageErrorCode::Success
127+
}
128+
Err(e) => error_to_code(&e),
129+
}
130+
}
131+
103132
// ============================================================================
104133
// Image Saving
105134
// ============================================================================

packages/fast_image/hook/build.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ Future<void> verifyAssetHash(
5050
required IOSSdk? iOSSdk,
5151
}) async {
5252
final hash = await hashAsset(asset);
53-
final expectedHash = assetHashes[createTargetName(targetOS, targetArchitecture, iOSSdk)];
53+
final targetName = targetOS.dylibFileName(
54+
createTargetName(targetOS, targetArchitecture, iOSSdk),
55+
);
56+
final expectedHash = assetHashes[targetName];
5457

5558
if (hash != expectedHash) {
5659
throw Exception('Asset hash mismatch: $hash != $expectedHash');

packages/fast_image/lib/src/bindings/bindings.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ external void fast_image_free_buffer(ffi.Pointer<ffi.Uint8> ptr, int len);
2020
@ffi.Native<ffi.Void Function(ffi.Pointer<ImageHandle>)>()
2121
external void fast_image_free(ffi.Pointer<ImageHandle> handle);
2222

23+
/// Guess image format from byte data
24+
/// Returns the format enum value or ImageErrorCode on error
25+
@ffi.Native<
26+
ImageErrorCode$1 Function(
27+
ffi.Pointer<ffi.Uint8>,
28+
ffi.UintPtr,
29+
ffi.Pointer<ffi.Uint32>,
30+
)
31+
>()
32+
external int fast_image_guess_format(
33+
ffi.Pointer<ffi.Uint8> data,
34+
int len,
35+
ffi.Pointer<ffi.Uint32> out_format,
36+
);
37+
2338
/// Load an image from a file path
2439
/// Returns null on error
2540
@ffi.Native<ffi.Pointer<ImageHandle> Function(ffi.Pointer<ffi.Char>)>()

0 commit comments

Comments
 (0)