开发者

How can I create bitmaps (in C)?

开发者 https://www.devze.com 2022-12-14 02:22 出处:网络
I am wondering how I can both import and export bitmaps to and from 开发者_如何学JAVAC. I\'m basically lost on where to begin.A bitmap in memory looks similar to this:

I am wondering how I can both import and export bitmaps to and from 开发者_如何学JAVAC. I'm basically lost on where to begin.


A bitmap in memory looks similar to this:

struct Image {
    int width;
    int height;
    char *data;    // 1 byte per channel & only 1 channel == grayscale
}

struct Image theImage;
theImage.width = 100;
theImage.height = 100;
theImage.data = malloc(sizeof(char) * theImage.width * theImage.height);

As to importing and exporting, there are some really simple file formats out there, take a look at BMP. For more complex formats you best use an already available library.

Most frameworks already have load/save methods for the most common fileformats. You could take a look at SDL if you're looking for a lightweight library.


Have you taken a look at ImageMagick's C API wrapper, MagickWand? Here's the documentation if you want to peruse.


I like using SDL with the dummy driver. You can draw onto an in-memory buffer just like you would onto a screen, then save it out to a PNG or whatever with SDL_image or similar.

Another popular library for this is GD.


The simple answer is to use an appropriate library. What library is appropriate will depend on what platform you are using. On a GUI platform the GUI API/Framework will include these facilities.


I like the netpbm/pbmplus tools, although I usually like the command line; the API is efficient but not much fun to use.

This semester I wrote a significant amount of software for beginning students to use to manipulate images; you might want to check out the homework assignments and supporting software for the Tufts course Machine Structure and Assembly-Language Programming.


The term "bitmap" is somewhat generic, unless you specifically mean a Windows BMP.

I'd recommend using an image processing library on your platform (like gd). A good graphics library has routines to do input and output with images in various formats.


FreeImage is excellent. I've used this for my own game development work, and it supports tons of formats. Here's the list of features and formats supported - http://freeimage.sourceforge.net/features.html

0

精彩评论

暂无评论...
验证码 换一张
取 消