From 0c9435c94d3c33b472fbc7e14a10cb1e5f7d5cbe Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Sat, 13 Jun 2020 03:00:21 +1000 Subject: [PATCH] frame: implemented set pixel function Counterpart to Frame::GetPixel() --- app/codec/frame.cpp | 13 +++++++++++++ app/codec/frame.h | 1 + app/render/color.cpp | 36 +++++++++++++++++++++++++++--------- app/render/color.h | 2 ++ 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/app/codec/frame.cpp b/app/codec/frame.cpp index b10c794bb..00dcb1806 100644 --- a/app/codec/frame.cpp +++ b/app/codec/frame.cpp @@ -93,6 +93,19 @@ bool Frame::contains_pixel(int x, int y) const return (is_allocated() && x >= 0 && x < width() && y >= 0 && y < height()); } +void Frame::set_pixel(int x, int y, const Color &c) +{ + if (!contains_pixel(x, y)) { + return; + } + + int pixel_index = y * linesize_pixels() + x; + + int byte_offset = PixelFormat::GetBufferSize(video_params().format(), pixel_index, 1); + + c.toData(data_.data() + byte_offset, video_params().format()); +} + const rational &Frame::sample_aspect_ratio() const { return sample_aspect_ratio_; diff --git a/app/codec/frame.h b/app/codec/frame.h index 7bf055e03..daea040ce 100644 --- a/app/codec/frame.h +++ b/app/codec/frame.h @@ -55,6 +55,7 @@ public: Color get_pixel(int x, int y) const; bool contains_pixel(int x, int y) const; + void set_pixel(int x, int y, const Color& c); const rational& sample_aspect_ratio() const; void set_sample_aspect_ratio(const rational& sample_aspect_ratio); diff --git a/app/render/color.cpp b/app/render/color.cpp index e0b20d6e2..e8e4cbfca 100644 --- a/app/render/color.cpp +++ b/app/render/color.cpp @@ -67,15 +67,7 @@ Color Color::fromHsv(const float &h, const float &s, const float &v) Color::Color(const char *data, const PixelFormat::Format &format) { - OIIO::convert_types(PixelFormat::GetOIIOTypeDesc(format), - data, - PixelFormat::GetOIIOTypeDesc(PixelFormat::PIX_FMT_RGB32F), - data_, - PixelFormat::FormatHasAlphaChannel(format) ? kRGBAChannels : kRGBChannels); - - if (!PixelFormat::FormatHasAlphaChannel(format)) { - set_alpha(1.0f); - } + *this = fromData(data, format); } Color::Color(const QColor &c) @@ -202,6 +194,32 @@ float Color::lightness() const return l; } +void Color::toData(char *data, const PixelFormat::Format &format) const +{ + OIIO::convert_types(PixelFormat::GetOIIOTypeDesc(PixelFormat::PIX_FMT_RGB32F), + data_, + PixelFormat::GetOIIOTypeDesc(format), + data, + PixelFormat::FormatHasAlphaChannel(format) ? kRGBAChannels : kRGBChannels); +} + +Color Color::fromData(const char *data, const PixelFormat::Format &format) +{ + Color c; + + OIIO::convert_types(PixelFormat::GetOIIOTypeDesc(format), + data, + PixelFormat::GetOIIOTypeDesc(PixelFormat::PIX_FMT_RGB32F), + c.data_, + PixelFormat::FormatHasAlphaChannel(format) ? kRGBAChannels : kRGBChannels); + + if (!PixelFormat::FormatHasAlphaChannel(format)) { + c.set_alpha(1.0f); + } + + return c; +} + QColor Color::toQColor() const { QColor c; diff --git a/app/render/color.h b/app/render/color.h index f2e6070d8..aedcfe488 100644 --- a/app/render/color.h +++ b/app/render/color.h @@ -84,6 +84,8 @@ public: float* data() {return data_;} const float* data() const {return data_;} + void toData(char* data, const PixelFormat::Format& format) const; + static Color fromData(const char* data, const PixelFormat::Format& format); QColor toQColor() const;