From 8191a51d9bafc34c12dab322be8687a04787f60d Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 2 Dec 2021 22:01:26 +0900 Subject: [PATCH] Minor fixes --- src/image.cc | 13 +++++-------- src/video.cc | 12 ++++++++---- src/video.h | 3 ++- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/image.cc b/src/image.cc index 247bff2..ca9b2dc 100644 --- a/src/image.cc +++ b/src/image.cc @@ -1423,16 +1423,13 @@ void Image::GenerateBlueNoiseOffsets() { } bool Image::DuplicateBlueNoiseOffsetExists() const { - for (unsigned int i = 1; i < blue_noise_offsets_.size(); ++i) { - if (blue_noise_offsets_.at(i - 1) == blue_noise_offsets_.at(i)) { - return true; + for (unsigned int i = 0; i < blue_noise_offsets_.size(); ++i) { + for (unsigned int j = i + 1; j < blue_noise_offsets_.size(); ++j) { + if (blue_noise_offsets_.at(i) == blue_noise_offsets_.at(j)) { + return true; + } } } - if (blue_noise_offsets_.size() > 1 && - blue_noise_offsets_.at(0) == - blue_noise_offsets_.at(blue_noise_offsets_.size() - 1)) { - return true; - } return false; } diff --git a/src/video.cc b/src/video.cc index a06722e..a8984aa 100644 --- a/src/video.cc +++ b/src/video.cc @@ -113,14 +113,16 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise, // read frames while (av_read_frame(avf_context, pkt) >= 0) { if (pkt->stream_index == video_stream_idx) { - if (!HandleDecodingPacket(codec_ctx, pkt, frame, blue_noise, grayscale)) { + if (!HandleDecodingPacket(codec_ctx, pkt, frame, blue_noise, grayscale, + overwrite)) { return false; } } } // flush decoders - if (!HandleDecodingPacket(codec_ctx, nullptr, frame, blue_noise, grayscale)) { + if (!HandleDecodingPacket(codec_ctx, nullptr, frame, blue_noise, grayscale, + overwrite)) { return false; } @@ -134,7 +136,7 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise, bool Video::HandleDecodingPacket(AVCodecContext *codec_ctx, AVPacket *pkt, AVFrame *frame, Image *blue_noise, - bool grayscale) { + bool grayscale, bool overwrite) { int return_value = avcodec_send_packet(codec_ctx, pkt); if (return_value < 0) { std::cout << "ERROR: Failed to decode packet (" << packet_count_ << ')' @@ -253,7 +255,9 @@ bool Video::HandleDecodingPacket(AVCodecContext *codec_ctx, AVPacket *pkt, out_name += std::to_string(frame_count_); } out_name += ".png"; - dithered_image->SaveAsPNG(out_name, false); + if (!dithered_image->SaveAsPNG(out_name, overwrite)) { + return false; + } // TODO encode video with dithered_image // cleanup diff --git a/src/video.h b/src/video.h index 3662598..3e6c2e7 100644 --- a/src/video.h +++ b/src/video.h @@ -57,7 +57,8 @@ class Video { unsigned int packet_count_; bool HandleDecodingPacket(AVCodecContext *codec_ctx, AVPacket *pkt, - AVFrame *frame, Image *blue_noise, bool grayscale); + AVFrame *frame, Image *blue_noise, bool grayscale, + bool overwrite); }; #endif