1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
use crate::tensor_data::{TensorCastError, TensorDataType, TensorElement};
#[cfg(feature = "image")]
use crate::tensor_data::{DecodedTensor, TensorImageLoadError, TensorImageSaveError};
use super::{TensorBuffer, TensorData, TensorDimension};
// Much of the following duplicates code from: `crates/re_components/src/tensor.rs`, which
// will eventually go away as the Tensor migration is completed.
// ----------------------------------------------------------------------------
impl TensorData {
/// Create a new tensor.
#[inline]
pub fn new(shape: Vec<TensorDimension>, buffer: TensorBuffer) -> Self {
Self { shape, buffer }
}
/// The shape of the tensor, including optional dimension names.
#[inline]
pub fn shape(&self) -> &[TensorDimension] {
self.shape.as_slice()
}
/// Returns the shape of the tensor with all leading & trailing dimensions of size 1 ignored.
///
/// If all dimension sizes are one, this returns only the first dimension.
#[inline]
pub fn shape_short(&self) -> &[TensorDimension] {
if self.shape.is_empty() {
&self.shape
} else {
let first_not_one = self.shape.iter().position(|dim| dim.size != 1);
let last_not_one = self.shape.iter().rev().position(|dim| dim.size != 1);
&self.shape[first_not_one.unwrap_or(0)..self.shape.len() - last_not_one.unwrap_or(0)]
}
}
/// The number of dimensions of the tensor.
///
/// An image tensor will usually have two (height, width) or three (height, width, channels) dimensions.
#[inline]
pub fn num_dim(&self) -> usize {
self.shape.len()
}
/// If the tensor can be interpreted as an image, return the height, width, and channels/depth of it.
pub fn image_height_width_channels(&self) -> Option<[u64; 3]> {
let mut shape_short = self.shape.as_slice();
// Ignore trailing dimensions of size 1:
while 2 < shape_short.len() && shape_short.last().map_or(false, |d| d.size == 1) {
shape_short = &shape_short[..shape_short.len() - 1];
}
// If the trailing dimension looks like a channel we ignore leading dimensions of size 1 down to
// a minimum of 3 dimensions. Otherwise we ignore leading dimensions of size 1 down to 2 dimensions.
let shrink_to = if shape_short
.last()
.map_or(false, |d| matches!(d.size, 1 | 3 | 4))
{
3
} else {
2
};
while shrink_to < shape_short.len() && shape_short.first().map_or(false, |d| d.size == 1) {
shape_short = &shape_short[1..];
}
// TODO(emilk): check dimension names against our standard dimension names ("height", "width", "depth")
match &self.buffer {
// In the case of NV12, return the shape of the RGB image, not the tensor size.
TensorBuffer::Nv12(_) => {
// NV12 encodes a color image in 1.5 "channels" -> 1 luma (per pixel) + (1U+1V) / 4 pixels.
match shape_short {
[h, w] => Some([h.size * 2 / 3, w.size, 3]),
_ => None,
}
}
// In the case of YUY2, return the shape of the RGB image, not the tensor size.
TensorBuffer::Yuy2(_) => {
// YUY2 encodes a color image in 2 "channels" -> 1 luma (per pixel) + (1U + 1V) (per 2 pixels).
match shape_short {
[h, w] => Some([h.size, w.size / 2, 3]),
_ => None,
}
}
TensorBuffer::Jpeg(_)
| TensorBuffer::U8(_)
| TensorBuffer::U16(_)
| TensorBuffer::U32(_)
| TensorBuffer::U64(_)
| TensorBuffer::I8(_)
| TensorBuffer::I16(_)
| TensorBuffer::I32(_)
| TensorBuffer::I64(_)
| TensorBuffer::F16(_)
| TensorBuffer::F32(_)
| TensorBuffer::F64(_) => {
match shape_short.len() {
1 => {
// Special case: Nx1(x1x1x …) tensors are treated as Nx1 gray images.
// Special case: Nx1(x1x1x …) tensors are treated as Nx1 gray images.
if self.shape.len() >= 2 {
Some([shape_short[0].size, 1, 1])
} else {
None
}
}
2 => Some([shape_short[0].size, shape_short[1].size, 1]),
3 => {
let channels = shape_short[2].size;
if matches!(channels, 1 | 3 | 4) {
// mono, rgb, rgba
Some([shape_short[0].size, shape_short[1].size, channels])
} else {
None
}
}
_ => None,
}
}
}
}
/// Returns true if the tensor can be interpreted as an image.
#[inline]
pub fn is_shaped_like_an_image(&self) -> bool {
self.image_height_width_channels().is_some()
}
/// Returns true if either all dimensions have size 1 or only a single dimension has a size larger than 1.
///
/// Empty tensors return false.
#[inline]
pub fn is_vector(&self) -> bool {
if self.shape.is_empty() {
false
} else {
self.shape.iter().filter(|dim| dim.size > 1).count() <= 1
}
}
/// Query with x, y, channel indices.
///
/// Allows to query values for any image-like tensor even if it has more or less dimensions than 3.
/// (useful for sampling e.g. `N x M x C x 1` tensor which is a valid image)
#[inline]
pub fn get_with_image_coords(&self, x: u64, y: u64, channel: u64) -> Option<TensorElement> {
match self.shape.len() {
1 => {
if y == 0 && channel == 0 {
self.get(&[x])
} else {
None
}
}
2 => {
if channel == 0 {
self.get(&[y, x])
} else {
None
}
}
3 => self.get(&[y, x, channel]),
4 => {
// Optimization for common case, next case handles this too.
if self.shape[3].size == 1 {
self.get(&[y, x, channel, 0])
} else {
None
}
}
dim => self.image_height_width_channels().and_then(|_| {
self.get(
&[x, y, channel]
.into_iter()
.chain(std::iter::repeat(0).take(dim - 3))
.collect::<Vec<u64>>(),
)
}),
}
}
/// Get the value of the element at the given index.
///
/// Return `None` if out-of-bounds, or if the tensor is encoded (e.g. [`TensorBuffer::Jpeg`]).
pub fn get(&self, index: &[u64]) -> Option<TensorElement> {
let mut stride: usize = 1;
let mut offset: usize = 0;
for (TensorDimension { size, .. }, index) in self.shape.iter().zip(index).rev() {
if size <= index {
return None;
}
offset += *index as usize * stride;
stride *= *size as usize;
}
match &self.buffer {
TensorBuffer::U8(buf) => Some(TensorElement::U8(buf[offset])),
TensorBuffer::U16(buf) => Some(TensorElement::U16(buf[offset])),
TensorBuffer::U32(buf) => Some(TensorElement::U32(buf[offset])),
TensorBuffer::U64(buf) => Some(TensorElement::U64(buf[offset])),
TensorBuffer::I8(buf) => Some(TensorElement::I8(buf[offset])),
TensorBuffer::I16(buf) => Some(TensorElement::I16(buf[offset])),
TensorBuffer::I32(buf) => Some(TensorElement::I32(buf[offset])),
TensorBuffer::I64(buf) => Some(TensorElement::I64(buf[offset])),
TensorBuffer::F16(buf) => Some(TensorElement::F16(buf[offset])),
TensorBuffer::F32(buf) => Some(TensorElement::F32(buf[offset])),
TensorBuffer::F64(buf) => Some(TensorElement::F64(buf[offset])),
TensorBuffer::Jpeg(_) => None, // Too expensive to unpack here.
TensorBuffer::Nv12(_) => {
{
// Returns the U32 packed RGBA value of the pixel at index [y, x] if it is valid.
let [y, x] = index else {
return None;
};
if let Some([r, g, b]) = self.get_nv12_pixel(*x, *y) {
let mut rgba = 0;
rgba |= (r as u32) << 24;
rgba |= (g as u32) << 16;
rgba |= (b as u32) << 8;
rgba |= 0xff;
Some(TensorElement::U32(rgba))
} else {
None
}
}
}
TensorBuffer::Yuy2(_) => {
{
// Returns the U32 packed RGBA value of the pixel at index [y, x] if it is valid.
let [y, x] = index else {
return None;
};
if let Some([r, g, b]) = self.get_yuy2_pixel(*x, *y) {
let mut rgba = 0;
rgba |= (r as u32) << 24;
rgba |= (g as u32) << 16;
rgba |= (b as u32) << 8;
rgba |= 0xff;
Some(TensorElement::U32(rgba))
} else {
None
}
}
}
}
}
/// Returns decoded RGB8 value at the given image coordinates if this tensor is a NV12 image.
///
/// If the tensor is not [`TensorBuffer::Nv12`], `None` is returned.
///
/// It is undefined what happens if the coordinate is out-of-bounds.
pub fn get_nv12_pixel(&self, x: u64, y: u64) -> Option<[u8; 3]> {
let TensorBuffer::Nv12(buf) = &self.buffer else {
return None;
};
match self.image_height_width_channels() {
Some([h, w, _]) => {
let uv_offset = w * h;
let luma = buf[(y * w + x) as usize];
let u = buf[(uv_offset + (y / 2) * w + x) as usize];
let v = buf[(uv_offset + (y / 2) * w + x) as usize + 1];
Some(Self::set_color_standard(luma, u, v))
}
_ => None,
}
}
/// Returns decoded RGB8 value at the given image coordinates if this tensor is a YUY2 image.
///
/// If the tensor is not [`TensorBuffer::Yuy2`], `None` is returned.
///
/// It is undefined what happens if the coordinate is out-of-bounds.
pub fn get_yuy2_pixel(&self, x: u64, y: u64) -> Option<[u8; 3]> {
let TensorBuffer::Yuy2(buf) = &self.buffer else {
return None;
};
match self.image_height_width_channels() {
Some([_, w, _]) => {
// given an x and y coordinate, get the offset into the YUY2 buffer
let index = ((y * w + x) * 2) as usize;
let (luma, u, v) = if x % 2 == 0 {
(buf[index], buf[index + 1], buf[index + 3])
} else {
(buf[index], buf[index - 1], buf[index + 1])
};
Some(Self::set_color_standard(luma, u, v))
}
_ => None,
}
}
/// Sets the color standard for the given YUV color.
///
/// This conversion mirrors the function of the same name in `crates/re_renderer/shader/decodings.wgsl`
///
/// Specifying the color standard should be exposed in the future [#3541](https://github.com/rerun-io/rerun/pull/3541)
fn set_color_standard(y: u8, u: u8, v: u8) -> [u8; 3] {
let (y, u, v) = (y as f32, u as f32, v as f32);
// rescale YUV values
let y = (y - 16.0) / 219.0;
let u = (u - 128.0) / 224.0;
let v = (v - 128.0) / 224.0;
// BT.601 (aka. SDTV, aka. Rec.601). wiki: https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
let r = y + 1.402 * v;
let g = y - 0.344 * u - 0.714 * v;
let b = y + 1.772 * u;
// BT.709 (aka. HDTV, aka. Rec.709). wiki: https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.709_conversion
// let r = y + 1.575 * v;
// let g = y - 0.187 * u - 0.468 * v;
// let b = y + 1.856 * u;
[
(255.0 * r).clamp(0.0, 255.0) as u8,
(255.0 * g).clamp(0.0, 255.0) as u8,
(255.0 * b).clamp(0.0, 255.0) as u8,
]
}
/// The datatype of the tensor.
#[inline]
pub fn dtype(&self) -> TensorDataType {
self.buffer.dtype()
}
/// The size of the tensor data, in bytes.
#[inline]
pub fn size_in_bytes(&self) -> usize {
self.buffer.size_in_bytes()
}
}
// ----------------------------------------------------------------------------
macro_rules! ndarray_from_tensor {
($type:ty, $variant:ident) => {
impl<'a> TryFrom<&'a TensorData> for ::ndarray::ArrayViewD<'a, $type> {
type Error = TensorCastError;
fn try_from(value: &'a TensorData) -> Result<Self, Self::Error> {
let shape: Vec<_> = value.shape.iter().map(|d| d.size as usize).collect();
if let TensorBuffer::$variant(data) = &value.buffer {
ndarray::ArrayViewD::from_shape(shape, data.as_slice())
.map_err(|err| TensorCastError::BadTensorShape { source: err })
} else {
Err(TensorCastError::TypeMismatch)
}
}
}
};
}
macro_rules! tensor_from_ndarray {
($type:ty, $variant:ident) => {
impl<'a, D: ::ndarray::Dimension> TryFrom<::ndarray::ArrayView<'a, $type, D>>
for TensorData
{
type Error = TensorCastError;
fn try_from(view: ::ndarray::ArrayView<'a, $type, D>) -> Result<Self, Self::Error> {
let shape = view
.shape()
.iter()
.map(|dim| TensorDimension {
size: *dim as u64,
name: None,
})
.collect();
match view.to_slice() {
Some(slice) => Ok(TensorData {
shape,
buffer: TensorBuffer::$variant(Vec::from(slice).into()),
}),
None => Ok(TensorData {
shape,
buffer: TensorBuffer::$variant(
view.iter().cloned().collect::<Vec<_>>().into(),
),
}),
}
}
}
impl<D: ::ndarray::Dimension> TryFrom<::ndarray::Array<$type, D>> for TensorData {
type Error = TensorCastError;
fn try_from(value: ndarray::Array<$type, D>) -> Result<Self, Self::Error> {
let value = value.as_standard_layout();
let shape = value
.shape()
.iter()
.map(|dim| TensorDimension {
size: *dim as u64,
name: None,
})
.collect();
value
.is_standard_layout()
.then(|| TensorData {
shape,
buffer: TensorBuffer::$variant(value.to_owned().into_raw_vec().into()),
})
.ok_or(TensorCastError::NotContiguousStdOrder)
}
}
impl From<Vec<$type>> for TensorData {
fn from(vec: Vec<$type>) -> Self {
TensorData {
shape: vec![TensorDimension::unnamed(vec.len() as u64)],
buffer: TensorBuffer::$variant(vec.into()),
}
}
}
impl From<&[$type]> for TensorData {
fn from(slice: &[$type]) -> Self {
TensorData {
shape: vec![TensorDimension::unnamed(slice.len() as u64)],
buffer: TensorBuffer::$variant(slice.into()),
}
}
}
};
}
macro_rules! tensor_type {
($type:ty, $variant:ident) => {
ndarray_from_tensor!($type, $variant);
tensor_from_ndarray!($type, $variant);
};
}
tensor_type!(u16, U16);
tensor_type!(u32, U32);
tensor_type!(u64, U64);
tensor_type!(i8, I8);
tensor_type!(i16, I16);
tensor_type!(i32, I32);
tensor_type!(i64, I64);
tensor_type!(arrow2::types::f16, F16);
tensor_type!(f32, F32);
tensor_type!(f64, F64);
tensor_from_ndarray!(u8, U8);
// Manual expansion of ndarray_from_tensor! macro for `u8` types. We need to do this, because u8 can store encoded data
impl<'a> TryFrom<&'a TensorData> for ::ndarray::ArrayViewD<'a, u8> {
type Error = TensorCastError;
fn try_from(value: &'a TensorData) -> Result<Self, Self::Error> {
match &value.buffer {
TensorBuffer::U8(data) | TensorBuffer::Nv12(data) => {
let shape: Vec<_> = value.shape.iter().map(|d| d.size as usize).collect();
ndarray::ArrayViewD::from_shape(shape, bytemuck::cast_slice(data.as_slice()))
.map_err(|err| TensorCastError::BadTensorShape { source: err })
}
_ => Err(TensorCastError::TypeMismatch),
}
}
}
// Manual expansion of tensor_type! macro for `half::f16` types. We need to do this
// because arrow uses its own half type. The two use the same underlying representation
// but are still distinct types. `half::f16`, however, is more full-featured and
// generally a better choice to use when converting to ndarray.
// ==========================================
// TODO(jleibs): would be nice to support this with the macro definition as well
// but the bytemuck casts add a bit of complexity here.
impl<'a> TryFrom<&'a TensorData> for ::ndarray::ArrayViewD<'a, half::f16> {
type Error = TensorCastError;
fn try_from(value: &'a TensorData) -> Result<Self, Self::Error> {
let shape: Vec<_> = value.shape.iter().map(|d| d.size as usize).collect();
if let TensorBuffer::F16(data) = &value.buffer {
ndarray::ArrayViewD::from_shape(shape, bytemuck::cast_slice(data.as_slice()))
.map_err(|err| TensorCastError::BadTensorShape { source: err })
} else {
Err(TensorCastError::TypeMismatch)
}
}
}
impl<'a, D: ::ndarray::Dimension> TryFrom<::ndarray::ArrayView<'a, half::f16, D>> for TensorData {
type Error = TensorCastError;
fn try_from(view: ::ndarray::ArrayView<'a, half::f16, D>) -> Result<Self, Self::Error> {
let shape = view
.shape()
.iter()
.map(|dim| TensorDimension {
size: *dim as u64,
name: None,
})
.collect();
match view.to_slice() {
Some(slice) => Ok(TensorData {
shape,
buffer: TensorBuffer::F16(Vec::from(bytemuck::cast_slice(slice)).into()),
}),
None => Ok(TensorData {
shape,
buffer: TensorBuffer::F16(
view.iter()
.map(|f| arrow2::types::f16::from_bits(f.to_bits()))
.collect::<Vec<_>>()
.into(),
),
}),
}
}
}
impl<D: ::ndarray::Dimension> TryFrom<::ndarray::Array<half::f16, D>> for TensorData {
type Error = TensorCastError;
fn try_from(value: ndarray::Array<half::f16, D>) -> Result<Self, Self::Error> {
let shape = value
.shape()
.iter()
.map(|dim| TensorDimension {
size: *dim as u64,
name: None,
})
.collect();
if value.is_standard_layout() {
Ok(TensorData {
shape,
buffer: TensorBuffer::F16(
bytemuck::cast_slice(value.into_raw_vec().as_slice())
.to_vec()
.into(),
),
})
} else {
Ok(TensorData {
shape,
buffer: TensorBuffer::F16(
value
.iter()
.map(|f| arrow2::types::f16::from_bits(f.to_bits()))
.collect::<Vec<_>>()
.into(),
),
})
}
}
}
// ----------------------------------------------------------------------------
#[cfg(feature = "image")]
impl TensorData {
/// Construct a tensor from the contents of an image file on disk.
///
/// JPEGs will be kept encoded, left to the viewer to decode on-the-fly.
/// Other images types will be decoded directly.
///
/// Requires the `image` feature.
#[cfg(not(target_arch = "wasm32"))]
pub fn from_image_file(path: &std::path::Path) -> Result<Self, TensorImageLoadError> {
re_tracing::profile_function!(path.to_string_lossy());
let img_bytes = {
re_tracing::profile_scope!("fs::read");
std::fs::read(path)?
};
let img_format = if let Some(extension) = path.extension() {
if let Some(format) = image::ImageFormat::from_extension(extension) {
format
} else {
image::guess_format(&img_bytes)?
}
} else {
image::guess_format(&img_bytes)?
};
Self::from_image_bytes(img_bytes, img_format)
}
/// Construct a tensor from the contents of a JPEG file on disk.
///
/// Requires the `image` feature.
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn from_jpeg_file(path: &std::path::Path) -> Result<Self, TensorImageLoadError> {
re_tracing::profile_function!(path.to_string_lossy());
let jpeg_bytes = {
re_tracing::profile_scope!("fs::read");
std::fs::read(path)?
};
Self::from_jpeg_bytes(jpeg_bytes)
}
/// Construct a new tensor from the contents of a `.jpeg` file at the given path.
#[deprecated = "Renamed 'from_jpeg_file'"]
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn tensor_from_jpeg_file(
image_path: impl AsRef<std::path::Path>,
) -> Result<Self, TensorImageLoadError> {
Self::from_jpeg_file(image_path.as_ref())
}
/// Construct a tensor from the contents of an image file.
///
/// JPEGs will be kept encoded, left to the viewer to decode on-the-fly.
/// Other images types will be decoded directly.
///
/// Requires the `image` feature.
#[inline]
pub fn from_image_bytes(
bytes: Vec<u8>,
format: image::ImageFormat,
) -> Result<Self, TensorImageLoadError> {
re_tracing::profile_function!(format!("{format:?}"));
if format == image::ImageFormat::Jpeg {
Self::from_jpeg_bytes(bytes)
} else {
let image = image::load_from_memory_with_format(&bytes, format)?;
Self::from_image(image)
}
}
/// Construct a tensor from the contents of a JPEG file, without decoding it now.
///
/// Requires the `image` feature.
pub fn from_jpeg_bytes(jpeg_bytes: Vec<u8>) -> Result<Self, TensorImageLoadError> {
re_tracing::profile_function!();
use zune_jpeg::JpegDecoder;
let mut decoder = JpegDecoder::new(&jpeg_bytes);
decoder.decode_headers()?;
let (w, h) = decoder
.dimensions()
.expect("can't fail after a successful decode_headers");
Ok(Self {
shape: vec![
TensorDimension::height(h as _),
TensorDimension::width(w as _),
TensorDimension::depth(3),
],
buffer: TensorBuffer::Jpeg(jpeg_bytes.into()),
})
}
/// Construct a new tensor from the contents of a `.jpeg` file.
#[deprecated = "Renamed 'from_jpeg_bytes'"]
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub fn tensor_from_jpeg_bytes(jpeg_bytes: Vec<u8>) -> Result<Self, TensorImageLoadError> {
Self::from_jpeg_bytes(jpeg_bytes)
}
/// Construct a tensor from something that can be turned into a [`image::DynamicImage`].
///
/// Requires the `image` feature.
///
/// This is a convenience function that calls [`DecodedTensor::from_image`].
#[inline]
pub fn from_image(
image: impl Into<image::DynamicImage>,
) -> Result<TensorData, TensorImageLoadError> {
Self::from_dynamic_image(image.into())
}
/// Construct a tensor from [`image::DynamicImage`].
///
/// Requires the `image` feature.
///
/// This is a convenience function that calls [`DecodedTensor::from_dynamic_image`].
#[inline]
pub fn from_dynamic_image(
image: image::DynamicImage,
) -> Result<TensorData, TensorImageLoadError> {
DecodedTensor::from_dynamic_image(image).map(DecodedTensor::into_inner)
}
/// Predicts if [`Self::to_dynamic_image`] is likely to succeed, without doing anything expensive
#[inline]
pub fn could_be_dynamic_image(&self) -> bool {
self.is_shaped_like_an_image()
&& matches!(
self.dtype(),
TensorDataType::U8
| TensorDataType::U16
| TensorDataType::F16
| TensorDataType::F32
| TensorDataType::F64
)
}
/// Try to convert an image-like tensor into an [`image::DynamicImage`].
pub fn to_dynamic_image(&self) -> Result<image::DynamicImage, TensorImageSaveError> {
use ecolor::{gamma_u8_from_linear_f32, linear_u8_from_linear_f32};
use image::{DynamicImage, GrayImage, RgbImage, RgbaImage};
type Rgb16Image = image::ImageBuffer<image::Rgb<u16>, Vec<u16>>;
type Rgba16Image = image::ImageBuffer<image::Rgba<u16>, Vec<u16>>;
type Gray16Image = image::ImageBuffer<image::Luma<u16>, Vec<u16>>;
let [h, w, channels] = self
.image_height_width_channels()
.ok_or_else(|| TensorImageSaveError::ShapeNotAnImage(self.shape.clone()))?;
let w = w as u32;
let h = h as u32;
let dyn_img_result = match (channels, &self.buffer) {
(1, TensorBuffer::U8(buf)) => {
GrayImage::from_raw(w, h, buf.to_vec()).map(DynamicImage::ImageLuma8)
}
(1, TensorBuffer::U16(buf)) => {
Gray16Image::from_raw(w, h, buf.to_vec()).map(DynamicImage::ImageLuma16)
}
// TODO(emilk) f16
(1, TensorBuffer::F32(buf)) => {
let pixels = buf
.iter()
.map(|pixel| gamma_u8_from_linear_f32(*pixel))
.collect();
GrayImage::from_raw(w, h, pixels).map(DynamicImage::ImageLuma8)
}
(1, TensorBuffer::F64(buf)) => {
let pixels = buf
.iter()
.map(|&pixel| gamma_u8_from_linear_f32(pixel as f32))
.collect();
GrayImage::from_raw(w, h, pixels).map(DynamicImage::ImageLuma8)
}
(3, TensorBuffer::U8(buf)) => {
RgbImage::from_raw(w, h, buf.to_vec()).map(DynamicImage::ImageRgb8)
}
(3, TensorBuffer::U16(buf)) => {
Rgb16Image::from_raw(w, h, buf.to_vec()).map(DynamicImage::ImageRgb16)
}
(3, TensorBuffer::F32(buf)) => {
let pixels = buf.iter().copied().map(gamma_u8_from_linear_f32).collect();
RgbImage::from_raw(w, h, pixels).map(DynamicImage::ImageRgb8)
}
(3, TensorBuffer::F64(buf)) => {
let pixels = buf
.iter()
.map(|&comp| gamma_u8_from_linear_f32(comp as f32))
.collect();
RgbImage::from_raw(w, h, pixels).map(DynamicImage::ImageRgb8)
}
(4, TensorBuffer::U8(buf)) => {
RgbaImage::from_raw(w, h, buf.to_vec()).map(DynamicImage::ImageRgba8)
}
(4, TensorBuffer::U16(buf)) => {
Rgba16Image::from_raw(w, h, buf.to_vec()).map(DynamicImage::ImageRgba16)
}
(4, TensorBuffer::F32(buf)) => {
let rgba: &[[f32; 4]] = bytemuck::cast_slice(buf);
let pixels: Vec<u8> = rgba
.iter()
.flat_map(|&[r, g, b, a]| {
let r = gamma_u8_from_linear_f32(r);
let g = gamma_u8_from_linear_f32(g);
let b = gamma_u8_from_linear_f32(b);
let a = linear_u8_from_linear_f32(a);
[r, g, b, a]
})
.collect();
RgbaImage::from_raw(w, h, pixels).map(DynamicImage::ImageRgba8)
}
(4, TensorBuffer::F64(buf)) => {
let rgba: &[[f64; 4]] = bytemuck::cast_slice(buf);
let pixels: Vec<u8> = rgba
.iter()
.flat_map(|&[r, g, b, a]| {
let r = gamma_u8_from_linear_f32(r as _);
let g = gamma_u8_from_linear_f32(g as _);
let b = gamma_u8_from_linear_f32(b as _);
let a = linear_u8_from_linear_f32(a as _);
[r, g, b, a]
})
.collect();
RgbaImage::from_raw(w, h, pixels).map(DynamicImage::ImageRgba8)
}
(_, _) => {
return Err(TensorImageSaveError::UnsupportedChannelsDtype(
channels,
self.buffer.dtype(),
))
}
};
dyn_img_result.ok_or(TensorImageSaveError::BadData)
}
}
#[cfg(feature = "image")]
impl TryFrom<image::DynamicImage> for TensorData {
type Error = TensorImageLoadError;
fn try_from(value: image::DynamicImage) -> Result<Self, Self::Error> {
Self::from_image(value)
}
}
#[cfg(feature = "image")]
impl<P: image::Pixel, S> TryFrom<image::ImageBuffer<P, S>> for TensorData
where
image::DynamicImage: std::convert::From<image::ImageBuffer<P, S>>,
{
type Error = TensorImageLoadError;
fn try_from(value: image::ImageBuffer<P, S>) -> Result<Self, Self::Error> {
Self::from_image(value)
}
}
#[test]
fn test_image_height_width_channels() {
let test_cases = [
// Normal grayscale:
(vec![1, 1, 480, 640, 1, 1], Some([480, 640, 1])),
(vec![1, 1, 480, 640, 1], Some([480, 640, 1])),
(vec![1, 1, 480, 640], Some([480, 640, 1])),
(vec![1, 480, 640, 1, 1], Some([480, 640, 1])),
(vec![1, 480, 640], Some([480, 640, 1])),
(vec![480, 640, 1, 1], Some([480, 640, 1])),
(vec![480, 640, 1], Some([480, 640, 1])),
(vec![480, 640], Some([480, 640, 1])),
//
// Normal RGB:
(vec![1, 1, 480, 640, 3, 1], Some([480, 640, 3])),
(vec![1, 1, 480, 640, 3], Some([480, 640, 3])),
(vec![1, 480, 640, 3, 1], Some([480, 640, 3])),
(vec![480, 640, 3, 1], Some([480, 640, 3])),
(vec![480, 640, 3], Some([480, 640, 3])),
//
// h=1, w=640, grayscale:
(vec![1, 640], Some([1, 640, 1])),
//
// h=1, w=640, RGB:
(vec![1, 640, 3], Some([1, 640, 3])),
//
// h=480, w=1, grayscale:
(vec![480, 1], Some([480, 1, 1])),
//
// h=480, w=1, RGB:
(vec![480, 1, 3], Some([480, 1, 3])),
//
// h=1, w=1, grayscale:
(vec![1, 1], Some([1, 1, 1])),
(vec![1, 1, 1], Some([1, 1, 1])),
(vec![1, 1, 1, 1], Some([1, 1, 1])),
//
// h=1, w=1, RGB:
(vec![1, 1, 3], Some([1, 1, 3])),
(vec![1, 1, 1, 3], Some([1, 1, 3])),
//
// h=1, w=3, Mono:
(vec![1, 3, 1], Some([1, 3, 1])),
//
// Ambiguous cases.
//
// These are here to show how the current implementation behaves, not to suggest that it is a
// commitment to preserving this behavior going forward.
// If you need to change this test, it's ok but we should still communicate the subtle change
// in behavior.
(vec![1, 1, 3, 1], Some([1, 1, 3])), // Could be [1, 3, 1]
(vec![1, 3, 1, 1], Some([1, 3, 1])), // Could be [3, 1, 1]
];
for (shape, expected_hwc) in test_cases {
let tensor = TensorData::new(
shape
.iter()
.map(|&size| TensorDimension::unnamed(size as u64))
.collect(),
TensorBuffer::U8(vec![0; shape.iter().product()].into()),
);
let hwc = tensor.image_height_width_channels();
assert_eq!(
hwc, expected_hwc,
"Shape {shape:?} produced HWC {hwc:?}, but expected {expected_hwc:?}"
);
}
}