Minor fixes

This commit is contained in:
Stephen Seo 2021-12-02 22:01:26 +09:00
parent cd9c363a48
commit 8191a51d9b
3 changed files with 15 additions and 13 deletions

View file

@ -1423,15 +1423,12 @@ void Image::GenerateBlueNoiseOffsets() {
} }
bool Image::DuplicateBlueNoiseOffsetExists() const { bool Image::DuplicateBlueNoiseOffsetExists() const {
for (unsigned int i = 1; i < blue_noise_offsets_.size(); ++i) { for (unsigned int i = 0; i < blue_noise_offsets_.size(); ++i) {
if (blue_noise_offsets_.at(i - 1) == blue_noise_offsets_.at(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; 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; return false;

View file

@ -113,14 +113,16 @@ bool Video::DitherVideo(const std::string &output_filename, Image *blue_noise,
// read frames // read frames
while (av_read_frame(avf_context, pkt) >= 0) { while (av_read_frame(avf_context, pkt) >= 0) {
if (pkt->stream_index == video_stream_idx) { 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; return false;
} }
} }
} }
// flush decoders // flush decoders
if (!HandleDecodingPacket(codec_ctx, nullptr, frame, blue_noise, grayscale)) { if (!HandleDecodingPacket(codec_ctx, nullptr, frame, blue_noise, grayscale,
overwrite)) {
return false; 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, bool Video::HandleDecodingPacket(AVCodecContext *codec_ctx, AVPacket *pkt,
AVFrame *frame, Image *blue_noise, AVFrame *frame, Image *blue_noise,
bool grayscale) { bool grayscale, bool overwrite) {
int return_value = avcodec_send_packet(codec_ctx, pkt); int return_value = avcodec_send_packet(codec_ctx, pkt);
if (return_value < 0) { if (return_value < 0) {
std::cout << "ERROR: Failed to decode packet (" << packet_count_ << ')' 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 += std::to_string(frame_count_);
} }
out_name += ".png"; out_name += ".png";
dithered_image->SaveAsPNG(out_name, false); if (!dithered_image->SaveAsPNG(out_name, overwrite)) {
return false;
}
// TODO encode video with dithered_image // TODO encode video with dithered_image
// cleanup // cleanup

View file

@ -57,7 +57,8 @@ class Video {
unsigned int packet_count_; unsigned int packet_count_;
bool HandleDecodingPacket(AVCodecContext *codec_ctx, AVPacket *pkt, bool HandleDecodingPacket(AVCodecContext *codec_ctx, AVPacket *pkt,
AVFrame *frame, Image *blue_noise, bool grayscale); AVFrame *frame, Image *blue_noise, bool grayscale,
bool overwrite);
}; };
#endif #endif