c++ - 以 pgm 文件格式的输出构建频率表
问题描述
在这里我正在阅读 PGM 格式我想在 C++ 中为霍夫曼压缩和解压缩构建频率表如何提取图片的输出以制作频率表?考虑到我可能会使用此源代码,我是否应该将其转换为文本文件并再次阅读
https://github.com/cynricfu/huffman-coding
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
//----------------------------READ IMAGE--------------------------------
void ReadImage(char fname[], int ***fimage, int& M, int& N, int& Q)
{
int i, j;
char d;
char header [100], *ptr;
ifstream ifp;
ifp.open(fname, ios::binary);
if (!ifp)
{
cout << "Can't read image: <" << fname << '>' << endl;
void getch();
exit(1);
}
ifp.getline(header,100,'\n');
if((header[0]!='P') || header[1]!='5') /* 'P5' Format */
{
cout << "Image <" << fname << "> is not in binary PGM 'P5' format." << endl;
void getch();
exit(1);
}
ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');
M=strtol(header,&ptr,0);
N=atoi(ptr);
ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);
fimage = new int [N];
for(i=0; i<N; i++)
(*fimage)[i] = new int[M];
for(i=0; i<N; i++)
{
for(j=0; j<M; j++)
{
d = ifp.get();
(*fimage)[i][j]= (int)d;
cout <<j;
}
cout <<i;
}
}
//-----------------------------Main-------------------------------------
int main()
{
int i, j;
int N, M, Q; // N=Rows M=Cols Q=GreyLevels
int **fimage; // int **I 2-D Array of Image
char infile[40]; // name of input file
char outfile[40]; // name of output image file
cout << "Enter name of *.pgm INPUT image file: ? ";
cin >> infile;
cout << i<<endl;
cout <<j<<endl;
ReadImage(infile, &fimage, M, N, Q); // Memory created for fimage
}
解决方案
推荐阅读
- gradle - 远程 Gradle 脚本插件缓存
- raster - 克里金预测函数为每个值返回 NAN。[r]
- rest - OData 是唯一适用于 SSO/MFA 环境的方法吗?
- typo3 - 我可以在没有文件收集的情况下将 Typo3 fs-media-gallery 与 sys_category 一起使用吗?
- react-native - FlatList 滚动到 Footer 组件
- excel - 使用终端更改多个文件的名称后带有文件扩展名的问号
- oracle - 是否有任何 Oracle INNER JOIN 替代查询?
- python - 烧瓶如何将@login_required重定向到特定路线?
- java - 部署应用程序时包含数据的文件路径
- google-sheets - 如何将表格从 gSheets 嵌入到 gDoc 并保留 AVERAGE 公式和单元格保护