JPG学习笔记2(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, 75, 1), 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: rgba(255, 255, 255, 1); font-family: "微软雅黑", "宋体", "黑体", Arial; font-size: 15px; font-weight: bold; height: 24px; line-height: 23px; margin: 12px 0 !important; padding: 5px 0 5px 10px; text-shadow: 2px 2px 3px rgba(34, 34, 34, 1) }
#topics h1 span { font-weight: bold; line-height: 1.5; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; text-decoration: underline; color: rgba(201, 27, 67, 1); text-shadow: 2px 2px 3px rgba(34, 34, 34, 1) }
我们已经从BMP图中拿到了需要压缩RGB的数据,我们需要对原数据从RGB域转变YCbCr域,之后对YCbCr数据进行下采样(down sampling)。对于不需要看文章的同学,这边直接给出源代码。https://github.com/Cheemion/JPEG_COMPRESS
1.RGB域和YCbCr域
RGB代表红绿蓝,通过3种颜色的叠加来得到我们看到的颜色。0-到255分别代表颜色从浅到深。
Y是RGB的加权平均值,称之为亮度(luminance)
Cb是B分量和亮度的差值, 称为Chrominance(Cb)
Cr是R分量和亮度的差值,称为Chrominance(Cr)
以下代码将RGB转为YCbCr。为什么将RGB转为YCbCr? 因为人眼对亮度(Y)的变化更敏感,所以我可以对Cr和Cb进行下采样(压缩,比如本来1个字节代表一个pixel的数据,压缩后用1个字节代表4个pixels的数据),尽可能保留完整的Y分量。通过这样子我们可以进一步的压缩数据。
void JPG::convertToYCbCr() {
for(uint i = 0; i < height; i++) {
for(uint j = 0; j < width; j++) {
YCbCr temp = BMPData[i * width + j];
BMPData[i * width + j].Y = 0.299 * temp.red + 0.587 * temp.green + 0.114 * temp.blue;
BMPData[i * width + j].Cb = -0.1687 * temp.red - 0.3313 * temp.green + 0.5 * temp.blue + 128;
BMPData[i * width + j].Cr = 0.5 * temp.red - 0.4187 * temp.green - 0.0813 * temp.blue + 128;
}
}
}
2.sampling(采样)
采样通常是对连续信号进行采样,比如下图蓝色是连续信号x(t),红色是对信号进行采样后得到的信号x[n]=x(T*n), T是采样间隔,1/T是采样频率。
而在JPEG中,我们是对已经离散的数据进行采样,并且JPEG中的采样数值是相对采样数值。相对于最高采样频率的采样数值。
如下左图,Y(luminance)分量的水平采样频率和垂直采样频率都是4,是最高的采样频率。最高的采样频率就相当于保留原图的Y分量,不进行下采样。
Cb分量的水平和垂直的采样频率都是2,等于最高采样频率的一半。所以水平每2个点采样一次,垂直每2个点采样一次。
Cr分量的水平和垂直采样频率都是1,等于最高采样频率的1/4。所以水平和垂直每4个点采样一个点。
3个分量的量叠加就得到了我们的像素的值。
2.YCbCr数据在JPEG中的存储
JPEG规定所有的数据都是以8*8的一个block(data unit)的形式进行离散余弦变化和存储的.可以把这8*8的block看成是最小存储单元。
MCU是Y,Cb,Cr的完整的block组成的能够完整还原一个范围的色彩的最小单元。啥意思?
假设我们的图片是10*10的大小.
若Y,Cb,Cr的水平和垂直的采样频率都为1,则原图由4个mcu(4种颜色分别代表一个MCU)组成(每个mcu包含1个y的block,一个cb的block,一个cr的block, 每个mcu的大小为8*8),边缘空白的地方可用0替代,也可以重复边缘的值。
左上角那块4*4的小block的值分别
pixel[0,0] = y[0,0] + cb[0,0] + cr[0,0]
pixel[0,1] = y[0,1] + cb[0,1] + cr[0,1]
pixel[1,0] = y[1,0] + cb[1,0] + cr[1,0]
pixel[1,1] = y[1,1] + cb[1,1] + cr[1,1]
若Y的水平和垂直采样频率为2, cb和cr的采样频率为1, 则原图由1个mcu组成(大小为16*16)。mcu中包含4个y的block(2*2),一个cb,一个cr。总共6个block,大小只占原来block的一半。
左上角那块4*4的小block的值分别
pixel[0,0] = y[0,0] + cb[0,0] + cr[0,0]
pixel[0,1] = y[0,1] + cb[0,0] + cr[0,0]
pixel[1,0] = y[1,0] + cb[0,0] + cr[0,0]
pixel[1,1] = y[1,1] + cb[0,0] + cr[0,0]
总结:mcu大小= 垂直最大采样值 * 水平最大采样值, 一个mcu包含y的水平采样值*y的垂直采样值个的y个block(y的水平采样为2,垂直为2,则一个muc有4个yblock)。其他分量同理
1.3定义JPG class代码
//定义Block
using Block = int[64];
//定义YCbCr,同时这个结构用来展示存放rgb数据
struct YCbCr {
union
{
double Y;
double red;
};
union
{
double Cb;
double green;
};
union {
double Cr;
double blue;
};
};
struct MCU {
Block* y;
Block* cb;
Block* cr;
};
//大于JPG类,用于压缩图片
class JPG
{
public:
//rgb转到YCbCr
void convertToYCbCr();
//下采样
void subsampling();
//变化
void discreteCosineTransform();
//量化
void quantization();
//哈夫曼
void huffmanCoding();
//输出
void output(std::string path);
public:
MCU* data;
Block* blocks;
//BMPData存放的是bmp图片的RGB数据
YCbCr* BMPData;
uint blockNum; //原图的像素
uint width;
uint height; //mcu 有多少个 长度是多少
uint mcuWidth;
uint mcuHeight; //一个完整的muc的水平和垂直像素个数
uint mcuVerticalPixelNum;
uint mcuHorizontalPixelNum; //用于subsampling
// only support 1 or 2
byte YVerticalSamplingFrequency;
byte YHorizontalSamplingFrequency;
byte CbVerticalSamplingFrequency;
byte CbHorizontalSamplingFrequency;
byte CrVerticalSamplingFrequency;
byte CrHorizontalSamplingFrequency;
byte maxVerticalSamplingFrequency;
byte maxHorizontalSamplingFrequency;
public:
JPG(uint width, uint height,const RGB* const rgbs,
byte YVerticalSamplingFrequency, byte YHorizontalSamplingFrequency,
byte CbVerticalSamplingFrequency, byte CbHorizontalSamplingFrequency,
byte CrVerticalSamplingFrequency, byte CrHorizontalSamplingFrequency
)
:width(width), height(height),
YVerticalSamplingFrequency(YVerticalSamplingFrequency), YHorizontalSamplingFrequency(YHorizontalSamplingFrequency),
CbVerticalSamplingFrequency(CbVerticalSamplingFrequency), CbHorizontalSamplingFrequency(CbHorizontalSamplingFrequency),
CrVerticalSamplingFrequency(CrVerticalSamplingFrequency), CrHorizontalSamplingFrequency(CrHorizontalSamplingFrequency)
{
maxHorizontalSamplingFrequency = std::max({YHorizontalSamplingFrequency, CbHorizontalSamplingFrequency, CrHorizontalSamplingFrequency});
maxVerticalSamplingFrequency = std::max({YVerticalSamplingFrequency, CbVerticalSamplingFrequency, CrVerticalSamplingFrequency});
//mcu的个数
mcuWidth = (width + (maxHorizontalSamplingFrequency * 8 - 1)) / (maxHorizontalSamplingFrequency * 8);
mcuHeight = (height + (maxVerticalSamplingFrequency * 8 - 1)) / (maxVerticalSamplingFrequency * 8); mcuVerticalPixelNum = maxVerticalSamplingFrequency * 8;
mcuHorizontalPixelNum = maxHorizontalSamplingFrequency * 8;
//总共多少个MCU
data = new MCU[mcuWidth * mcuHeight];
//一个MCU有多少个Block
blockNum = (YVerticalSamplingFrequency * YHorizontalSamplingFrequency + CbVerticalSamplingFrequency * CbHorizontalSamplingFrequency + CrHorizontalSamplingFrequency * CrVerticalSamplingFrequency); //分配block内存空间
blocks = new Block[mcuHeight * mcuHeight * blockNum]; //把内存映射到对于的结构中
for (uint i = 0; i < mcuHeight; i++) {
for (uint j = 0; j < mcuWidth; j++) {
data[i * mcuWidth + j].y = &blocks[(i * mcuWidth + j) * blockNum];
data[i * mcuWidth + j].cb = data[i * mcuWidth + j].y + YVerticalSamplingFrequency * YHorizontalSamplingFrequency;
data[i * mcuWidth + j].cr = data[i * mcuWidth + j].cb + CbVerticalSamplingFrequency * CbHorizontalSamplingFrequency;
}
}
//BMP数据用于存放,bmp的原图的数据
BMPData = new YCbCr[width * height];
//把bmp数据暂时存放在BMPdata中
for(uint i = 0; i < height; i++) {
for(uint j = 0; j < width; j++) {
BMPData[i * width + j].red = static_cast<double>(rgbs[i * width + j].red);
BMPData[i * width + j].blue = static_cast<double>(rgbs[i * width + j].blue);
BMPData[i * width + j].green = static_cast<double>(rgbs[i * width + j].green);
}
}
}
~JPG() {
delete[] data;
delete[] blocks;
delete[] BMPData;
} };
1.6下采样代码
//这里直接把左上的点 当作subsampling的点了
//也可以取平均值
void JPG::subsampling() {
//遍历mcu
for (uint i = 0; i < mcuHeight; i++) {
for (uint j = 0; j < mcuWidth; j++) {
//拿到mcu
MCU& currentMCU = data[i * mcuWidth + j];
//每个mcu起始的坐标点
uint heightOffset = i * maxVerticalSamplingFrequency * 8;
uint widthOffset = j * maxHorizontalSamplingFrequency * 8;
//iterate over 每一个component Y, cb cr
for (uint componentID = 1; componentID <= 3; componentID++) {
//遍历block, 从muc中拿block
for(uint ii = 0, yOffSet = heightOffset; ii < getVerticalSamplingFrequency(componentID); ii++, yOffSet = yOffSet + 8) {
for(uint jj = 0, xOffset = widthOffset; jj < getHorizontalSamplingFrequency(componentID); jj++, xOffset = xOffset + 8) {
//拿到具体的block对象
Block& currentBlock = currentMCU[componentID][ii * getHorizontalSamplingFrequency(componentID) + jj];
//遍历Block every pixels 像素, 并且采样赋值
for(uint y = 0; y < 8; y++) {
for(uint x = 0; x < 8; x++) {
//得到被采样的那个点的坐标
uint sampledY = yOffSet + y * maxVerticalSamplingFrequency / getVerticalSamplingFrequency(componentID);
uint sampledX = xOffset + x * maxHorizontalSamplingFrequency / getHorizontalSamplingFrequency(componentID);
//cannot find in original pictures;
if(sampledX >= width || sampledY >= height) {
currentBlock[y * 8 + x] = 0;
} else {
currentBlock[y * 8 + x] = BMPData[sampledY * width + sampledX][componentID];
}
}
}
}
}
}
}
}
}
完整代码 https://github.com/Cheemion/JPEG_COMPRESS/tree/main/Day2
完结
祝你开心每一天。
最新文章
- Oracle(创建index)
- Core Audio(三)
- Linux安装JDK、MyEclipse、MySql
- iOS应用中URL地址如何重定向-b
- (linux shell)第一章--小试牛刀(上)
- Gradle的简介与安装
- Iterator对对象遍历
- Mysql表分区的选择与实践小结
- JavaSE基础知识(5)—面向对象(抽象类和接口)
- A2D JS框架 - AOP封装
- maomao的fft板子
- windows更改Jupyter Notebook工作的目录
- 心迹 使用说明&;功能展示
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
- Delphi 如何访问监控摄像头?
- django报错解决:Invalid HTTP_HOST header: &#39;xxx.com&#39;. You may need to add u&#39;xxx.com&#39; to ALLOWED_HOSTS.
- Linux中svn的使用
- hdu-2159(完全背包)
- Hive之 hive的三种使用方式(CLI、HWI、Thrift)
- 洛谷 P1865 A % B Problem
热门文章
- DSL是什么?Elasticsearch的Query DSL又是什么?
- pytorch——预测值转换为概率,单层感知机
- IPC图像处理项目流程图
- Soul 网关 Nacos 数据同步源码解析
- jackson学习之三:常用API操作
- CentOS7.9静默安装Oracle19C软件
- Supporting Multiple Versions of WebSocket Protocol 支持多版本WebSocket协议
- ";INVALID"; is not a valid start token
- python 招聘数据分析
- 动态传参,命名空间,嵌套,gloabal,nonlocal