From 33ed31ef98e887765c72567d47a561be0d35a298 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Wed, 13 May 2026 18:48:51 +0200 Subject: [PATCH] factor out commonly used greyscale pixel formaton --- nihav-commonfmt/src/codecs/cinepak.rs | 10 +--------- nihav-commonfmt/src/codecs/cinepakenc.rs | 14 ++------------ nihav-commonfmt/src/codecs/gifenc.rs | 16 +++------------- nihav-core/src/formats.rs | 7 +++++++ nihav-hlblocks/src/imgseqdec.rs | 11 ++--------- nihav-indeo/src/demuxers/dvi.rs | 12 +----------- 6 files changed, 16 insertions(+), 54 deletions(-) diff --git a/nihav-commonfmt/src/codecs/cinepak.rs b/nihav-commonfmt/src/codecs/cinepak.rs index eeef065..7b150f5 100644 --- a/nihav-commonfmt/src/codecs/cinepak.rs +++ b/nihav-commonfmt/src/codecs/cinepak.rs @@ -304,15 +304,7 @@ impl NADecoder for CinepakDecoder { validate!(is_intra); let fmt = match mode { DecodeMode::YUV => YUV420_FORMAT, - DecodeMode::Gray => NAPixelFormaton { - model: ColourModel::YUV(YUVSubmodel::YUVJ), - components: 1, - comp_info: [Some(NAPixelChromaton{h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}), None, None, None, None], - elem_size: 1, - be: true, - alpha: false, - palette: false, - }, + DecodeMode::Gray => GREY_FORMAT, DecodeMode::Palette => PAL8_FORMAT, _ => unreachable!(), }; diff --git a/nihav-commonfmt/src/codecs/cinepakenc.rs b/nihav-commonfmt/src/codecs/cinepakenc.rs index e8c624a..b61b8a0 100644 --- a/nihav-commonfmt/src/codecs/cinepakenc.rs +++ b/nihav-commonfmt/src/codecs/cinepakenc.rs @@ -144,16 +144,6 @@ impl RNG { } } -const GRAY_FORMAT: NAPixelFormaton = NAPixelFormaton { - model: ColourModel::YUV(YUVSubmodel::YUVJ), - components: 1, - comp_info: [Some(NAPixelChromaton{h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}), None, None, None, None], - elem_size: 1, - be: true, - alpha: false, - palette: false, - }; - struct MaskWriter { masks: Vec, mask: u32, @@ -1159,7 +1149,7 @@ impl NAEncoder for CinepakEncoder { }, NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), NACodecTypeInfo::Video(vinfo) => { - let pix_fmt = if vinfo.format == GRAY_FORMAT { GRAY_FORMAT } else { YUV420_FORMAT }; + let pix_fmt = if vinfo.format == GREY_FORMAT { GREY_FORMAT } else { YUV420_FORMAT }; let outinfo = NAVideoInfo::new((vinfo.width + 3) & !3, (vinfo.height + 3) & !3, false, pix_fmt); let mut ofmt = *encinfo; ofmt.format = NACodecTypeInfo::Video(outinfo); @@ -1173,7 +1163,7 @@ impl NAEncoder for CinepakEncoder { NACodecTypeInfo::None => Err(EncoderError::FormatError), NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), NACodecTypeInfo::Video(vinfo) => { - if vinfo.format != YUV420_FORMAT && vinfo.format != GRAY_FORMAT { + if vinfo.format != YUV420_FORMAT && vinfo.format != GREY_FORMAT { return Err(EncoderError::FormatError); } if ((vinfo.width | vinfo.height) & 3) != 0 { diff --git a/nihav-commonfmt/src/codecs/gifenc.rs b/nihav-commonfmt/src/codecs/gifenc.rs index 94084eb..4ed90c9 100644 --- a/nihav-commonfmt/src/codecs/gifenc.rs +++ b/nihav-commonfmt/src/codecs/gifenc.rs @@ -2,16 +2,6 @@ use nihav_core::codecs::*; use nihav_core::io::byteio::*; use nihav_core::io::bitwriter::*; -const GRAY_FORMAT: NAPixelFormaton = NAPixelFormaton { - model: ColourModel::YUV(YUVSubmodel::YUVJ), - components: 1, - comp_info: [Some(NAPixelChromaton{h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}), None, None, None, None], - elem_size: 1, - be: true, - alpha: false, - palette: false, - }; - #[derive(Clone,Copy,Default,PartialEq)] enum CompressionLevel { None, @@ -271,7 +261,7 @@ impl NAEncoder for GIFEncoder { }, NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), NACodecTypeInfo::Video(vinfo) => { - let format = if vinfo.format == GRAY_FORMAT { GRAY_FORMAT } else { PAL8_FORMAT }; + let format = if vinfo.format == GREY_FORMAT { GREY_FORMAT } else { PAL8_FORMAT }; let outinfo = NAVideoInfo::new(vinfo.width, vinfo.height, false, format); let mut ofmt = *encinfo; ofmt.format = NACodecTypeInfo::Video(outinfo); @@ -288,12 +278,12 @@ impl NAEncoder for GIFEncoder { if vinfo.width > 65535 || vinfo.height > 65535 { return Err(EncoderError::FormatError); } - if vinfo.format != PAL8_FORMAT && vinfo.format != GRAY_FORMAT { + if vinfo.format != PAL8_FORMAT && vinfo.format != GREY_FORMAT { return Err(EncoderError::FormatError); } self.width = vinfo.width; self.height = vinfo.height; - self.grayscale = vinfo.format == GRAY_FORMAT; + self.grayscale = vinfo.format == GREY_FORMAT; let edata = self.tr_idx.map(|val| vec![val]); diff --git a/nihav-core/src/formats.rs b/nihav-core/src/formats.rs index c1ba215..370314e 100644 --- a/nihav-core/src/formats.rs +++ b/nihav-core/src/formats.rs @@ -552,6 +552,13 @@ pub const PAL8_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColourModel::R None, None], elem_size: 3, be: false, alpha: false, palette: true }; +/// Pre-defined format for 8-bit greyscale. +pub const GREY_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColourModel::YUV(YUVSubmodel::YUVJ), components: 1, + comp_info: [ + chromaton!(0, 0, false, 8, 0, 0, 1), + None, None, None, None], + elem_size: 1, be: true, alpha: false, palette: false }; + /// Predefined format for RGB555 packed video. pub const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton::make_rgb16_fmt(5, 5, 5, true, false); diff --git a/nihav-hlblocks/src/imgseqdec.rs b/nihav-hlblocks/src/imgseqdec.rs index 814ec38..633f345 100644 --- a/nihav-hlblocks/src/imgseqdec.rs +++ b/nihav-hlblocks/src/imgseqdec.rs @@ -208,15 +208,8 @@ fn read_pnm_header(file: &mut BufReader, pgmyuv: bool) -> DemuxerResult { - vinfo.format = NAPixelFormaton { - model: ColourModel::YUV(YUVSubmodel::YUVJ), - components: 1, - comp_info: [Some(NAPixelChromaton{h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}), None, None, None, None], - elem_size: 1, - be: true, - alpha: false, - palette: false, - }; + // todo: modify format for different bit-depths + vinfo.format = GREY_FORMAT; vinfo.bits = bits; }, b'5' | b'2' => { diff --git a/nihav-indeo/src/demuxers/dvi.rs b/nihav-indeo/src/demuxers/dvi.rs index 5244185..36da559 100644 --- a/nihav-indeo/src/demuxers/dvi.rs +++ b/nihav-indeo/src/demuxers/dvi.rs @@ -1,16 +1,6 @@ use std::collections::VecDeque; use nihav_core::demuxers::*; -const GRAY_FORMAT: NAPixelFormaton = NAPixelFormaton { - model: ColourModel::YUV(YUVSubmodel::YUVJ), - components: 1, - comp_info: [Some(NAPixelChromaton{h_ss: 0, v_ss: 0, packed: false, depth: 8, shift: 0, comp_offs: 0, next_elem: 1}), None, None, None, None], - elem_size: 1, - be: true, - alpha: false, - palette: false, -}; - struct DVIDemuxer<'a> { src: &'a mut dyn ByteIO, data_end: u64, @@ -182,7 +172,7 @@ impl<'a> DemuxCore<'a> for DVIDemuxer<'a> { }; let fmt = match hdr_subtype { - 1 | 11 | 12 => GRAY_FORMAT, // Y/U/V component only + 1 | 11 | 12 => GREY_FORMAT, // Y/U/V component only _ => YUV410_FORMAT, }; -- 2.39.5