如何像素化二进制 (P6) PPM 文件
How to pixelate a binary (P6) PPM file
我正在努力对由存储在 binary (P6) PPM 文件中的 RGB 值组成的图像进行像素化。 对图片进行像素化的步骤如下:
- 读入二进制数据,存入一维数组
- 遍历此数组中存储的数据,单元格大小为 'c'(行 x 列)。此变量 'c' 可由用户更改,但对于此程序,它当前设置为
int c = 4;
,这意味着它以 4 x 4
的块维度遍历像素数据
- 现在求出每个大小为 'c'
的单元格中的平均颜色值
- 将每个平均颜色值输出到新的输出 PPM 文件
每个二进制 PPM 文件以以下格式的 header 开头,由 'magic number' 组成,然后是宽度、高度,最后是最大颜色值 255。Header 注释被忽略。 下面的 header 示例显示格式为 P6 的 PPM 图像(因此是 binary 文件),宽度为16,高度 16,最大颜色值 255:
P6
16
16
255
我挣扎的地方:
- 我不确定如何找到每个单元格的平均 RGB 值,然后将其输出到新的数据流。当线性读取数据(即不将其读入数组(或缓冲区))时,我想出了一种方法,方法是将该单元格中的总颜色除以循环迭代次数,但这被证明是不正确的。
- 嵌套的 for 循环是否以改变输出图像方向的方式排序,例如它是否旋转了 180 度等?我正在努力通过读取原始二进制数据来确定这一点。
我的尝试:
#define _CRT_SECURE_NO_WARNINGS //preprocessor requirement
#include <stdio.h> //library for I/O functions
#include <stdlib.h> //library for general functions
#define magic_size 10 //macro for PPM character found within header
typedef struct {
int t_r, t_g, t_b; //Struct to hold RGB pixel data
} Pixel;
int main()
{
char magic_number[magic_size]; //variable for PPM format
int width = 0, height = 0, max_col = 0; //imagine dimensions
int c = 4; //mosaic parameter
/* INPUT FILE HANDLING */
FILE *inputFile;
inputFile = fopen("Sheffield512x512.ppm", "r");
//input file error handling
if (inputFile == NULL)
{
printf(stderr, "ERROR: file cannot be opened");
getchar(); //prevent cmd premature closure
exit(1); //exit program cleanly
}
/* OUTPUT FILE HANDLING */
FILE *outputFile;
outputFile = fopen("mosaic.ppm", "w");
//output file error handling
if (outputFile == NULL)
{
printf(stderr, "ERROR: cannot write to file");
getchar(); //prevent cmd premature closure
exit(1); //exit program cleanly
}
// Scan the header (these variables are used later on)
fscanf(inputFile, "%s\n%d\n%d\n%d", &magic_number, &width, &height, &max_col);
// Error handling. Program only supports binary files (i.e. of P6 format)
if (magic_number[1] != '6')
{
printf("Only Binary images supported!\n");
getchar(); //prevent cmd premature closure
return;
}
// Raw 1 dimensional store of pixel data
Pixel *data = malloc(width*height * sizeof(Pixel));
//2D index to access pixel data
Pixel **pixels = malloc(height * sizeof(Pixel*));
// Read the binary file data
size_t r = fread(data, width*height, sizeof(unsigned char), inputFile);
// Build a 1-dimensional index for the binary data
for (unsigned int i = 0; i < height; ++i)
{
pixels[i] = data + (i * width);
}
// Close the input file
fclose(inputFile);
/* BEGIN PIXELATION PROCESS */
// Print the OUTPUT file header
fprintf(outputFile, "%s\n%d\n%d\n%d", magic_number, width, height, max_col);
//loop condition variables
int cw_x = ceil((double)(width / (float)c));
int cw_y = ceil((double)(height / (float)c));
//iterate through 2d array in cells of size c
for (int c_x = 0; c_x < cw_x; c_x += 1)
{
for (int c_y = 0; c_y < cw_y; c_y += 1)
{
//iterate within the cells
for (int _x = 0; _x < c; _x++)
{
int x = c_x * c + _x;
//bounds checking
if (x < width)
{
for (int _y = 0; _y < c; _y++)
{
int y = c_y * c + _y;
//bounds checking
if (y < height)
{
//write data to the output FILE stream
fwrite(data, width*height, sizeof(unsigned char), outputFile);
}
}
}
}
}
}
//close the output file
fclose(outputFile);
return 0;
}
在评论中,我对您的代码中的错误进行了一些反馈。你可以自己解决这些问题。使用调试器 test/check 所有这些准备步骤。例如读取文件并立即写入(并显示图像),以便您知道读取正常。
你的主要问题和疑问是关于循环的。
本质上你有一个 one-dimensional 数组,它由 scanline-after-scanline 组成,每条扫描线都包含像素。与 BMP 格式相反,您的格式似乎没有使用填充字节来对齐字边界上的扫描线。这样就容易多了。
一个像素由三个颜色值组成,R、G和B,我假设每个颜色值是一个字节(unsigned char)。那么内存分配和读取就变成了:
unsigned char *data = malloc(width*height*3);
r = fread(data, width*height*3, 1, inputFile);
循环现在以四 的增量遍历所有行,并以四 的增量处理每个像素。所以它一次处理一个方格,计算平均值并写出来:
c= 4;
for (y=0; y<height; y += c)
{
for (x=0; x<width; x += c)
{
unsigned int avgR=0, avgG=0, avgB= 0;
for (dy=0; dy<c && y+dy<height; dy++)
{
for (dx=0; dx<c && x+dx<width; dx++)
{
avgR += data[ y*width*3 // line in image
+ x*3 // pixel on line
+ dy*width*3 // line of square
+ dx*3 // R pixel in line of square
];
avgG += data[ y*width*3 // line in image
+ x*3 // pixel on line
+ dy*width*3 // line of square
+ dx*3 + 1 // G pixel in line of square
];
avgB += data[ y*width*3 // line in image
+ x*3 // pixel on line
+ dy*width*3 // line of square
+ dx*3 + 2 // B pixel in line of square
];
}
}
unsigned char avgRb= avgR/(dx*dy);
unsigned char avgGb= avgG/(dx*dy);
unsigned char avgBb= avgB/(dx*dy);
fwrite(&avgR,1,1,outputFile);
fwrite(&avgG,1,1,outputFile);
fwrite(&avgB,1,1,outputFile);
}
}
这可以使用指针算法进行优化,但这显示了您需要的循环的基础知识。
备注:
..&& y+dy<height
测试最后一个方块不适合高度时的边框大小写。宽度相同。
因此,平均值是除以 (dx*dy)
。
免责声明
我无法测试它,所以该算法是一种心理构造。
我正在努力对由存储在 binary (P6) PPM 文件中的 RGB 值组成的图像进行像素化。 对图片进行像素化的步骤如下:
- 读入二进制数据,存入一维数组
- 遍历此数组中存储的数据,单元格大小为 'c'(行 x 列)。此变量 'c' 可由用户更改,但对于此程序,它当前设置为
int c = 4;
,这意味着它以4 x 4
的块维度遍历像素数据
- 现在求出每个大小为 'c' 的单元格中的平均颜色值
- 将每个平均颜色值输出到新的输出 PPM 文件
每个二进制 PPM 文件以以下格式的 header 开头,由 'magic number' 组成,然后是宽度、高度,最后是最大颜色值 255。Header 注释被忽略。 下面的 header 示例显示格式为 P6 的 PPM 图像(因此是 binary 文件),宽度为16,高度 16,最大颜色值 255:
P6
16
16
255
我挣扎的地方:
- 我不确定如何找到每个单元格的平均 RGB 值,然后将其输出到新的数据流。当线性读取数据(即不将其读入数组(或缓冲区))时,我想出了一种方法,方法是将该单元格中的总颜色除以循环迭代次数,但这被证明是不正确的。
- 嵌套的 for 循环是否以改变输出图像方向的方式排序,例如它是否旋转了 180 度等?我正在努力通过读取原始二进制数据来确定这一点。
我的尝试:
#define _CRT_SECURE_NO_WARNINGS //preprocessor requirement
#include <stdio.h> //library for I/O functions
#include <stdlib.h> //library for general functions
#define magic_size 10 //macro for PPM character found within header
typedef struct {
int t_r, t_g, t_b; //Struct to hold RGB pixel data
} Pixel;
int main()
{
char magic_number[magic_size]; //variable for PPM format
int width = 0, height = 0, max_col = 0; //imagine dimensions
int c = 4; //mosaic parameter
/* INPUT FILE HANDLING */
FILE *inputFile;
inputFile = fopen("Sheffield512x512.ppm", "r");
//input file error handling
if (inputFile == NULL)
{
printf(stderr, "ERROR: file cannot be opened");
getchar(); //prevent cmd premature closure
exit(1); //exit program cleanly
}
/* OUTPUT FILE HANDLING */
FILE *outputFile;
outputFile = fopen("mosaic.ppm", "w");
//output file error handling
if (outputFile == NULL)
{
printf(stderr, "ERROR: cannot write to file");
getchar(); //prevent cmd premature closure
exit(1); //exit program cleanly
}
// Scan the header (these variables are used later on)
fscanf(inputFile, "%s\n%d\n%d\n%d", &magic_number, &width, &height, &max_col);
// Error handling. Program only supports binary files (i.e. of P6 format)
if (magic_number[1] != '6')
{
printf("Only Binary images supported!\n");
getchar(); //prevent cmd premature closure
return;
}
// Raw 1 dimensional store of pixel data
Pixel *data = malloc(width*height * sizeof(Pixel));
//2D index to access pixel data
Pixel **pixels = malloc(height * sizeof(Pixel*));
// Read the binary file data
size_t r = fread(data, width*height, sizeof(unsigned char), inputFile);
// Build a 1-dimensional index for the binary data
for (unsigned int i = 0; i < height; ++i)
{
pixels[i] = data + (i * width);
}
// Close the input file
fclose(inputFile);
/* BEGIN PIXELATION PROCESS */
// Print the OUTPUT file header
fprintf(outputFile, "%s\n%d\n%d\n%d", magic_number, width, height, max_col);
//loop condition variables
int cw_x = ceil((double)(width / (float)c));
int cw_y = ceil((double)(height / (float)c));
//iterate through 2d array in cells of size c
for (int c_x = 0; c_x < cw_x; c_x += 1)
{
for (int c_y = 0; c_y < cw_y; c_y += 1)
{
//iterate within the cells
for (int _x = 0; _x < c; _x++)
{
int x = c_x * c + _x;
//bounds checking
if (x < width)
{
for (int _y = 0; _y < c; _y++)
{
int y = c_y * c + _y;
//bounds checking
if (y < height)
{
//write data to the output FILE stream
fwrite(data, width*height, sizeof(unsigned char), outputFile);
}
}
}
}
}
}
//close the output file
fclose(outputFile);
return 0;
}
在评论中,我对您的代码中的错误进行了一些反馈。你可以自己解决这些问题。使用调试器 test/check 所有这些准备步骤。例如读取文件并立即写入(并显示图像),以便您知道读取正常。
你的主要问题和疑问是关于循环的。
本质上你有一个 one-dimensional 数组,它由 scanline-after-scanline 组成,每条扫描线都包含像素。与 BMP 格式相反,您的格式似乎没有使用填充字节来对齐字边界上的扫描线。这样就容易多了。
一个像素由三个颜色值组成,R、G和B,我假设每个颜色值是一个字节(unsigned char)。那么内存分配和读取就变成了:
unsigned char *data = malloc(width*height*3);
r = fread(data, width*height*3, 1, inputFile);
循环现在以四 的增量遍历所有行,并以四 的增量处理每个像素。所以它一次处理一个方格,计算平均值并写出来:
c= 4;
for (y=0; y<height; y += c)
{
for (x=0; x<width; x += c)
{
unsigned int avgR=0, avgG=0, avgB= 0;
for (dy=0; dy<c && y+dy<height; dy++)
{
for (dx=0; dx<c && x+dx<width; dx++)
{
avgR += data[ y*width*3 // line in image
+ x*3 // pixel on line
+ dy*width*3 // line of square
+ dx*3 // R pixel in line of square
];
avgG += data[ y*width*3 // line in image
+ x*3 // pixel on line
+ dy*width*3 // line of square
+ dx*3 + 1 // G pixel in line of square
];
avgB += data[ y*width*3 // line in image
+ x*3 // pixel on line
+ dy*width*3 // line of square
+ dx*3 + 2 // B pixel in line of square
];
}
}
unsigned char avgRb= avgR/(dx*dy);
unsigned char avgGb= avgG/(dx*dy);
unsigned char avgBb= avgB/(dx*dy);
fwrite(&avgR,1,1,outputFile);
fwrite(&avgG,1,1,outputFile);
fwrite(&avgB,1,1,outputFile);
}
}
这可以使用指针算法进行优化,但这显示了您需要的循环的基础知识。
备注:
..&& y+dy<height
测试最后一个方块不适合高度时的边框大小写。宽度相同。因此,平均值是除以
(dx*dy)
。
免责声明
我无法测试它,所以该算法是一种心理构造。