用C语言缩放图像

发布于 2024-08-23 21:26:23 字数 702 浏览 0 评论 0原文

我正在尝试读取图像文件并通过将每个字节乘以某个绝对因子的像素级别缩放来缩放它。不过,我不确定我做对了 -

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

巷雨优美回忆 2024-08-30 21:26:24
  1. char *scaled_data;
  2. 更改为 char scaled_data;*scaled_data = (*data) * scale; 更改为 scaled_data = (*data ) * scale;

这将使您的代码可以完成您想要做的事情,但是......

这只能在您自己的自定义格式的图像文件上工作。不存在仅按顺序将像素以字节为单位转储到文件中的标准图像格式。图像文件需要了解更多信息,例如

  1. 图像的高度和宽度
  2. 像素如何表示(1字节灰色,3字节颜色等)
  3. 如果像素表示为调色板的索引,则它们具有调色板
  4. 各种其他信息(GPS 坐标、创建它的软件、创建日期等)
  5. 用于像素的压缩方法

所有这些都称为元数据

此外(如#5 所提到的),像素数据通常是压缩的。

  1. change char *scaled_data; to char scaled_data;
  2. change *scaled_data = (*data) * scale; to scaled_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

  1. The height and width of the image
  2. How pixels are represented (1 byte gray, 3 bytes color, etc)
  3. If pixels are represented as an index into a palette, they have the palette
  4. All kinds of other information (GPS coordinates, the software that created it, the date it was created, etc)
  5. The method of compression used for the pixels

All of this is called Meta-data

In addition (as alluded to by #5), pixel data is usually compressed.

懒的傷心 2024-08-30 21:26:24

你的代码相当于说“我想通过将位分成两半来缩小我的图像”;这没有任何意义。

图像文件是复杂的格式,带有标题和字段以及需要解释的各种有趣的东西。采纳 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.

扛刀软妹 2024-08-30 21:26:24

为什么你认为你错了,除了效率不高之外,我认为你的算法没有任何问题,并且 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; and char *scaled_data; should be unsigned char data; and unsigned char scaled_data;

柏林苍穹下 2024-08-30 21:26:24

我对位图(只是原始数据)的理解是,每个像素由三个数字表示,每个数字代表 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.

情域 2024-08-30 21:26:23
char *data;
char *scaled_data;

没有为这些指针分配内存 - 为什么需要它们作为指针? unsigned char 变量就可以了(unsigned 因为它对于字节数据更有意义)。

另外,当秤的值超出 256 范围时会发生什么?你想要饱和度、包裹感还是什么?

char *data;
char *scaled_data;

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文