便携式函数来创建从原始字节bmp文件? [英] A portable function to create a bmp file from raw bytes?

查看:173
本文介绍了便携式函数来创建从原始字节bmp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有原始字节数组,我要让那些字节的bmp文件。也就是说,我必须填写的位图头和结构法其他的东西,然后写下来字节,所以我必须以正确的格式BMP文件。

I have an array of raw bytes and I want to make a bmp file from those bytes. That is, I have to fill bitmap header structre and other things, then write down the bytes so I have a bmp file in the proper format.

当我需要这个只有一些快速的检查,我不知道是否有这样做的便携式方法 - 以原始字节,并将其保存为bmp文件。我正在写在Unix上的任何Windows版本都不会做的。

As I need this for some quick check only, I wonder if there is a portable method to do this - take raw bytes and save them as a bmp file. Any Windows version won't do as I'm writing that on Unix.

另外,我可以保存这些字节作为任何其他图像格式 - 我只是需要有一个快速浏览拍摄的图像。

Alternatively, I can save those bytes as any other image format - I only need to have a quick look at the resulting picture.

推荐答案

下面会给你一个 .ppm从字节数组形象。在P6指定二进制格式的每像素3个字节,但纯文本,还支持和各种形式的灰度。你应该使用这种情况的原因是,它是容易,因为可以和大多数* nix系统有一堆ppmto的* -tools:ppmtobmp,ppmtojpeg,...,ppmtopng ......你的名字

The following will give you a .ppm image from a byte array. The "P6" specifies binary format 3 byte per pixel, but plain text is also supported and various forms of grayscale. The reason you should use this is that it is easy as can be and most *nix systems have a bunch of ppmto*-tools: ppmtobmp, ppmtojpeg, ..., ppmtopng... you name it.

typedef struct {
    int width;
    int height;
    uint8_t *data;
    size_t size;
} ppm_image;

size_t ppm_save(ppm_image *img, FILE *outfile) {
    size_t n = 0;
    n += fprintf(outfile, "P6\n# THIS IS A COMMENT\n%d %d\n%d\n", 
                 img->width, img->height, 0xFF);
    n += fwrite(img->data, 1, img->width * img->height * 3, outfile);
    return n;
}

有也ppmtocad ......谁又能猜到?

There is also ppmtocad... who would have guessed?

这篇关于便携式函数来创建从原始字节bmp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆