How can I create bitmaps (in C)? - c

I am wondering how I can both import and export bitmaps to and from C. 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

Related

Creating a GdkPixbuf from raw Bitmap data

I am trying to port a Clean graphics library (specifically, ObjectIO) to Linux with GDK. The library consists of a non-OS-specific part, which I don't want to touch because of compatibility issues, and an OS-specific part which I can touch.
One of the functions I have to implement is one that takes raw data from a .bmp file and has to build a GdkPixbuf from this. This function has to be written in C.
I am aware of the function gdk_pixbuf_new_from_file(), but I do not get the file name (and can't change that, as it's in the non-OS-specific part of the library). I only get the raw data.
I saw this manual entry, and I guess I could use gdk_pixbuf_new_from_data(), but this requires that you know the row stride of the image, of which I'm unsure how to get it.
Is there a way to get the row stride from the file? If not, the only possibility I see is to create a temporary file with the data and then call gdk_pixbuf_new_from_file() - but that would be really ugly.
You want to use the function:
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, ImageWidth);

Implementing imagesc in C

I am in need of C version(not C++) of imagesc function in MATLAB. I am in conversion of a MATLAB program to C(It is the requirement). The only thing left to implement is imagesc functionality. I don't necessarily need to plot or show the image in a window. I can write the image to a bitmap file also. So in essence, I need a matrix of RGB color values or grayscale color values as the result of this implementation.
So, I am looking for a simple library that is portable to Windows and Linux, and also dothe above. Or, I am ready to implement one of my own. But, I don't have a clue on how to do that. If there is no such library, would you please provide some information on implementing it?
Man, did you do a single Google search? There's tons of image manipulation libraries for C. ImageMagick's C library is one decent example, but if you don't like it you have many options.
Look at OpenCV, a computer vision and image processing library that works with C and C++. To accomplish something similar to imagesc in C++, I use the functions imread and imshow or alternatively I load the data in a Mat structure and I display it with imshow.

Is there an user-level accessible font table present in Linux?

Since there is this: http://en.wikipedia.org/wiki/Code_page_437 For MSDOS, is there something similar for Linux systems? Is it possible to access that font data via userland program? I would actually just need an access to the actual bit patterns which define the font, and I would do the rendering myself. I'm fairly sure that something like this exists, but I haven't been able to find what exactly is it and how to access it. After all, e.g. text mode console font has to reside somewhere, and I really do hope it is "rawly" accessible somehow for a userland program.
Before I forget, I'm programming my program in C, and have access only to the "standard" linux/posix development headers. The only thing I could came up with myself is to use the fonts in /usr/share/fonts, but having to write my own implementations to extract the data from there doesn't sound really an option; I would really want to achieve this with the least amount of bytes possible, so I feel I'm left with finding a standard way of doing this.
It's not really feasible for me to store my own 8x8 ASCII-compatible font with the program either(it takes some 1024 bytes(128 chars * 8x8 bits) just to store the font, which is definitely unacceptable for the strict size limits(some < 1024 bytes for code+data) which I am working with), so being able to use the font data stored at the system itself would greatly simplify my task.
I had a look at consolechars sources and it looks like there is a whole library for this kind of stuff. On Ubuntu it's named libconsole and header files (like lct/font.h) are in the console-tools-dev package. There are functions to find and load fonts which seems to be exactly what you need. And consolechars source is a nice example of how to use them.
You should use freetype , its commonly installed in all the Linuxes.

How can I convert (encode) RGB buffer to JPEG in plain C?

I would expect that there's some sort of library that I can use in this fashion:
int* buffer[720*480]; // read in from file/memory/network stream
raw_params params;
params.depth = 16;
params.width = 720;
params.height = 480;
params.map = "rgb";
params.interleave = JPEG_RAW_INTERLEAVE_OFF;
jpeg_encode(buffer, params)
But I can't seem to find it.
Clarification
I'm looking for a simple example of how to use such a library as well. Code as robust as the source of netpbm and magick are too complex for my level of understanding.
Yes, there is a library for this (luckily!): http://freshmeat.net/projects/libjpeg/ equal (?) to http://sourceforge.net/projects/libjpeg/
The libjpeg library from the Independent JPEG Group is the reference implementation of the JPEG standard, written by folks who were and are involved in the creation of the standard. It is robust, stable, and highly portable.
It comes as a source kit, which includes extensive documentation and samples.
Its preferred representation for a source image is as an array pointers to of arrays of pixels, not as a single monolithic array as you show. You can easily create the array of pointers to rows to address your image buffer, however.
It is highly configurable and extensible. Specifically, it allows both the data source and the destination to be configured through call backs. This adds some complexity to a first use, but it isn't all that hard to deal with.
There are certainly commercial libraries available as well, and I know that there is commercially available IP for implementation of JPEG in hardware. A commercial library is likely to be faster, but will never be cheaper.
Here's a fairly simple example that's copied in a number of places on the web:
http://oaf.mutualads.org/svn/code/renderer/tr_image_jpg.c
http://www.torquepowered.com/community/forums/viewthread/45455
And this particular page of the documentation is relevant to understanding the require structs:
http://apodeline.free.fr/DOC/libjpeg/libjpeg-2.html

C code for loading bitmap

Does anybody know a good C sample that loads bitmaps and handles all the cases: rle, b/w bitmaps, so on?
Code should be cross-platform.
Thanks.
I would suggest using a library like SDL image
If you are looking for a minimal bmp loader this link will give you all you need to know about the BMP format, data structures and sample code without any library dependency to load:
http://paulbourke.net/dataformats/bmp/.
It also contains code to see the loaded BMP in a open gl texture, so pretty much all you need...
Chris Backhouse made a functional little BMP loader (with an eye to using them as OpenGL textures). It's C++, not C, and he admits it's not cross platform. However, it's small and easy to understand, so I thought I'd add the link here:
http://users.ox.ac.uk/~orie1330/bmploader.html
You need some external library to do this (I recommend ImageMagick). The ImageMagick web site also includes documentation and examples.
Check out for OpenCV Library developed by Intel .
If you are tied to the BMP file format, it's pretty simple to look at the header yourself and get the pixels. See this google search. One of the more interesting matches is here. The most counter-intuitive part is that every line of pixels is 4-byte aligned. Also, watch out for compressed BMPs... (My experience is that many third-party tools have trouble with compressed BMPs, so maybe some libraries you encounter will also..)
If you aren't tied to the BMP file format, I recommend libpng. The manual provides some sample code which is pretty clear.
As others suggested you might want to use an external library like SDL. If you want to learn something and do it yourself, see my answer to this very similar question: Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C where you'll find C code which prints out each pixel, and have a look at the wikipedia page about bmp files, because it's very good.

Resources