| 557 | */ |
| 558 | template <typename T> |
| 559 | void save_to_ppm(T &tensor, const std::string &ppm_filename) |
| 560 | { |
| 561 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(&tensor, arm_compute::Format::RGB888, arm_compute::Format::U8); |
| 562 | ARM_COMPUTE_ERROR_ON(tensor.info()->num_dimensions() > 2); |
| 563 | |
| 564 | std::ofstream fs; |
| 565 | |
| 566 | try |
| 567 | { |
| 568 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 569 | fs.open(ppm_filename, std::ios::out | std::ios::binary); |
| 570 | |
| 571 | const unsigned int width = tensor.info()->tensor_shape()[0]; |
| 572 | const unsigned int height = tensor.info()->tensor_shape()[1]; |
| 573 | |
| 574 | fs << "P6\n" << width << " " << height << " 255\n"; |
| 575 | |
| 576 | // Map buffer if creating a CLTensor |
| 577 | map(tensor, true); |
| 578 | |
| 579 | switch (tensor.info()->format()) |
| 580 | { |
| 581 | case arm_compute::Format::U8: |
| 582 | { |
| 583 | arm_compute::Window window; |
| 584 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, 1)); |
| 585 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 586 | |
| 587 | arm_compute::Iterator in(&tensor, window); |
| 588 | |
| 589 | arm_compute::execute_window_loop( |
| 590 | window, |
| 591 | [&](const arm_compute::Coordinates &) |
| 592 | { |
| 593 | const unsigned char value = *in.ptr(); |
| 594 | |
| 595 | fs << value << value << value; |
| 596 | }, |
| 597 | in); |
| 598 | |
| 599 | break; |
| 600 | } |
| 601 | case arm_compute::Format::RGB888: |
| 602 | { |
| 603 | arm_compute::Window window; |
| 604 | window.set(arm_compute::Window::DimX, arm_compute::Window::Dimension(0, width, width)); |
| 605 | window.set(arm_compute::Window::DimY, arm_compute::Window::Dimension(0, height, 1)); |
| 606 | |
| 607 | arm_compute::Iterator in(&tensor, window); |
| 608 | |
| 609 | arm_compute::execute_window_loop( |
| 610 | window, |
| 611 | [&](const arm_compute::Coordinates &) { |
| 612 | fs.write(reinterpret_cast<std::fstream::char_type *>(in.ptr()), |
| 613 | width * tensor.info()->element_size()); |
| 614 | }, |
| 615 | in); |
| 616 | |