| 399 | } |
| 400 | |
| 401 | Status ConvertFromPHWC4(absl::Span<const float> in, const BHWC& shape, |
| 402 | absl::Span<float> out) { |
| 403 | RETURN_IF_ERROR(ValidateConvertFromPHWC4(in, shape, out)); |
| 404 | if (shape.c == 4) { |
| 405 | std::memcpy(out.data(), in.data(), |
| 406 | shape.DimensionsProduct() * sizeof(float)); |
| 407 | return OkStatus(); |
| 408 | } |
| 409 | |
| 410 | int num_planes = IntegralDivideRoundUp(shape.c, kPhwc4ChannelsInPlane); |
| 411 | const int num_pixels = shape.h * shape.w; |
| 412 | const int padded_size = num_pixels * num_planes * kPhwc4ChannelsInPlane; |
| 413 | // A layer is a set of kPhwc4ChannelsInPlane channels images. |
| 414 | const int num_full_planes = shape.c / kPhwc4ChannelsInPlane; |
| 415 | for (int b = 0; b < shape.b; b++) { |
| 416 | const float* src = in.data() + b * padded_size; |
| 417 | for (int p = 0; p < num_full_planes; p++) { |
| 418 | float* dest = |
| 419 | out.data() + shape.LinearIndex({b, 0, 0, p * kPhwc4ChannelsInPlane}); |
| 420 | for (int i = 0; i < num_pixels; i++) { |
| 421 | std::memcpy(dest, src, kPhwc4ChannelsInPlane * sizeof(float)); |
| 422 | src += kPhwc4ChannelsInPlane; |
| 423 | dest += shape.c; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Unpadding last kPhwc4ChannelsInPlane-channel plane |
| 429 | const int remaining_channels = |
| 430 | shape.c - num_full_planes * kPhwc4ChannelsInPlane; |
| 431 | if (remaining_channels == 0) { |
| 432 | return OkStatus(); |
| 433 | } |
| 434 | for (int b = 0; b < shape.b; b++) { |
| 435 | const float* src = in.data() + b * padded_size + |
| 436 | num_pixels * num_full_planes * kPhwc4ChannelsInPlane; |
| 437 | float* dest = |
| 438 | out.data() + |
| 439 | shape.LinearIndex({b, 0, 0, num_full_planes * kPhwc4ChannelsInPlane}); |
| 440 | for (int p = 0; p < num_pixels; p++) { |
| 441 | std::memcpy(dest, src, remaining_channels * sizeof(float)); |
| 442 | src += kPhwc4ChannelsInPlane; |
| 443 | dest += shape.c; |
| 444 | } |
| 445 | } |
| 446 | return OkStatus(); |
| 447 | } |
| 448 | |
| 449 | Status ConvertFromPHWC4Half(absl::Span<const HalfBits> in, const BHWC& shape, |
| 450 | absl::Span<float> out) { |