获取JPEG最小编码单位(MCU)大小

本文介绍了一种通过解析JPEG图像的SOF0头信息来确定最小编码单元(MCU)尺寸的方法。利用shell脚本读取并解析文件,检查图像是否为YCbCr编码,并基于Y分量的采样因子计算出MCU的宽度和高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是JPEG最小截取编码块 8x8的大小。

引述地址:

https://www.impulseadventure.com/photo/rotation-partial-mcu.html

https://www.impulseadventure.com/photo/jpeg-minimum-coded-unit.html

https://quippe.eu/blog/2016/11/17/determining-minimum-coded-unit-dimensions.html

 

源码:get_mcu_dimensions.sh

file=$1

# Get position and length of SOF0 header in file.
sof0hex=$(exiv2 -p S $file | grep SOF0 | sed 's/|.*|//')
IFS=' '
read -a sof0hexfields <<< "${sof0hex}"
offset=${sof0hexfields[0]}
length=${sof0hexfields[1]}

# Read SOF0 values into array.
sof0string=$(hexdump "$1" -s $offset -n $length -v -e '/1 "%02x "' | \
             sed 's/\ $//')
read -a sof0 <<< "${sof0string}"

# Check if length of SOF0 is as expected.
sof0length=${#sof0[@]}
sof0explength=$((16#${sof0[0]}${sof0[1]}))
if [ ${#sof0[@]} -ne $((16#${sof0[0]}${sof0[1]})) ]; then
  echo "Length of SOF0 in bytes ($sof0length) not as expected ($sof0explength)."
  exit
fi

# Check if the image is a YCbCr image (the only encoding this script handles).
if [ ${sof0[7]} -ne 3 ]; then
  echo "Image has ${sof0[7]} instead of 3 components: not YCbCr."
  exit
fi
if [ ${sof0[14]} -ne 3 ]; then
  echo "Image is not YCbCr (most likely YIQ instead, or else just screwed)."
  exit
fi

# Check if Cb and Cr are both 11.
if [ ${sof0[12]} -ne 11 -o ${sof0[15]} -ne 11 ]; then
  echo "Sampling factors of Cb and/or Cr is/are not equal to 11."
  exit
fi

# Determine MCU based on Y component sampling factors:
y_hor=$(echo ${sof0[9]} | cut -c 1)
y_ver=$(echo ${sof0[9]} | cut -c 2)

mcu_x=$((y_hor * 8))
mcu_y=$((y_ver * 8))

echo -e $"mcu_x\t$mcu_x"
echo -e $"mcu_y\t$mcu_y"

 

使用如下:

ubuntu@ubuntu:~$ ./get_mcu_dimensions.sh example_16x16.jpg 
mcu_x   16
mcu_y   16

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值