用C语言缩放图像
我正在尝试读取图像文件并通过将每个字节乘以某个绝对因子的像素级别缩放来缩放它。不过,我不确定我做对了 -
void scale_file(char *infile, char *outfile, float scale)
{
// open files for reading
FILE *infile_p = fopen(infile, 'r');
FILE *outfile_p = fopen(outfile, 'w');
// init data holders
char *data;
char *scaled_data;
// read each byte, scale and write back
while ( fread(&data, 1, 1, infile_p) != EOF )
{
*scaled_data = (*data) * scale;
fwrite(&scaled_data, 1, 1, outfile);
}
// close files
fclose(infile_p);
fclose(outfile_p);
}
让我困惑的是如何进行每个字节乘法(比例为 0-1.0 浮点数) - 我很确定我要么读错了,要么错过了一些大的东西。此外,假定数据是无符号的 (0-255)。请不要评判我糟糕的代码:)
谢谢
I'm trying to read an image file and scale it by multiplying each byte by a scale its pixel levels by some absolute factor. I'm not sure I'm doing it right, though -
void scale_file(char *infile, char *outfile, float scale)
{
// open files for reading
FILE *infile_p = fopen(infile, 'r');
FILE *outfile_p = fopen(outfile, 'w');
// init data holders
char *data;
char *scaled_data;
// read each byte, scale and write back
while ( fread(&data, 1, 1, infile_p) != EOF )
{
*scaled_data = (*data) * scale;
fwrite(&scaled_data, 1, 1, outfile);
}
// close files
fclose(infile_p);
fclose(outfile_p);
}
What gets me is how to do each byte multiplication (scale is 0-1.0 float) - I'm pretty sure I'm either reading it wrong or missing something big. Also, data is assumed to be unsigned (0-255). Please don't judge my poor code :)
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
char *scaled_data;
char scaled_data;
将*scaled_data = (*data) * scale;
更改为scaled_data = (*data ) * scale;
这将使您的代码可以完成您想要做的事情,但是......
这只能在您自己的自定义格式的图像文件上工作。不存在仅按顺序将像素以字节为单位转储到文件中的标准图像格式。图像文件需要了解更多信息,例如
所有这些都称为元数据
此外(如#5 所提到的),像素数据通常是压缩的。
char *scaled_data;
tochar scaled_data;
*scaled_data = (*data) * scale;
toscaled_data = (*data) * scale;
That would get you code that would do what you are trying to do, but ....
This could only possibly work on an image file of your own custom format. There is no standard image format that just dumps pixels in bytes in a file in sequential order. Image files need to know more information, like
All of this is called Meta-data
In addition (as alluded to by #5), pixel data is usually compressed.
你的代码相当于说“我想通过将位分成两半来缩小我的图像”;这没有任何意义。
图像文件是复杂的格式,带有标题和字段以及需要解释的各种有趣的东西。采纳 nobugz 的建议并查看 ImageMagick。它是一个可以做你想做的事情的库。
You're code is equivalent to saying "I want to scale down my image by dividing the bits in half"; it doesn't make any sense.
Images files are complex formats with headers and fields and all sorts of fun stuff that needs to be interpreted. Take nobugz's advice and check out ImageMagick. It's a library for doing exactly the kind of thing you want.
为什么你认为你错了,除了效率不高之外,我认为你的算法没有任何问题,并且
char *data;
和char *scaled_data;
应该是unsigned char数据;
和unsigned charscaled_data;
why you think you are wrong, i see nothing wrong in your algorithm except for not being efficient and
char *data;
andchar *scaled_data;
should beunsigned char data;
andunsigned char scaled_data;
我对位图(只是原始数据)的理解是,每个像素由三个数字表示,每个数字代表 RGB;将每个值乘以 <=1 的数字只会使图像变暗。如果你想使图像更宽,你可以只输出每个像素两次(使大小加倍),或者只输出每隔一个像素(使大小减半),但这取决于它的光栅化方式。
My understanding of a bitmap (just the raw data) is that each pixle is represented by three numbers one each for RGB; multiplying each by a number <=1 would just make the image darker. If you're trying to make the image wider, you could maby just output each pixle twice (to double the size), or just output every other pixel (to halve the size), but that depends on how its rasterized.
没有为这些指针分配内存 - 为什么需要它们作为指针?
unsigned char
变量就可以了(unsigned
因为它对于字节数据更有意义)。另外,当秤的值超出 256 范围时会发生什么?你想要饱和度、包裹感还是什么?
No memory was allocated for these pointers - why do you need them as pointers?
unsigned char
variables will be just fine (unsigned
because it makes more sense for byte data).Also, what happens when the scale shoots the value out of the 256-range? Do you want saturation, wrapping, or what?