| 564 | } |
| 565 | |
| 566 | bool ImageReader::resize_image(const image_data_t& input, int target_width, int target_height, image_data_t& output) { |
| 567 | if (input.pixels.size() == 0 || input.width <= 0 || input.height <= 0 || target_width <= 0 || target_height <= 0) { |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | SwsContext* sws_ctx = sws_getContext(input.width, |
| 572 | input.height, |
| 573 | AV_PIX_FMT_RGB24, |
| 574 | target_width, |
| 575 | target_height, |
| 576 | AV_PIX_FMT_RGB24, |
| 577 | SWS_BICUBIC, |
| 578 | nullptr, |
| 579 | nullptr, |
| 580 | nullptr); |
| 581 | if (!sws_ctx) { |
| 582 | std::cerr << "Error: Could not create resize context" << std::endl; |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | const int out_size = av_image_get_buffer_size(AV_PIX_FMT_RGB24, target_width, target_height, 1); |
| 587 | if (out_size <= 0) { |
| 588 | sws_freeContext(sws_ctx); |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | bytes out_buffer = memory_pool_.acquire(static_cast<size_t>(out_size)); |
| 593 | if (out_buffer.size() == 0) { |
| 594 | sws_freeContext(sws_ctx); |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | uint8_t* src_data[4] = { input.pixels.data(), nullptr, nullptr, nullptr }; |
| 599 | int src_linesize[4] = { input.width * 3, 0, 0, 0 }; |
| 600 | uint8_t* dst_data[4] = { out_buffer.data(), nullptr, nullptr, nullptr }; |
| 601 | int dst_linesize[4] = { target_width * 3, 0, 0, 0 }; |
| 602 | |
| 603 | bool ok = sws_scale(sws_ctx, |
| 604 | src_data, |
| 605 | src_linesize, |
| 606 | 0, |
| 607 | input.height, |
| 608 | dst_data, |
| 609 | dst_linesize) > 0; |
| 610 | sws_freeContext(sws_ctx); |
| 611 | |
| 612 | if (!ok) { |
| 613 | memory_pool_.recycle(std::move(out_buffer)); |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | recycle(output); |
| 618 | output.width = target_width; |
| 619 | output.height = target_height; |
| 620 | output.pixels = std::move(out_buffer); |
| 621 | return true; |
| 622 | } |
| 623 |
no test coverage detected