C code for loading bitmap - c

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.

Related

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.

Bitmap Image Output in C

I'm working on small project in C and at one point, I need to write a picture with the content of an array to a file. This will have to run on an embedded system at some point, so additional libraries are not an option.
The code I have so far works (in a modified version) for RGB, but fails for 8bit Grayscale.
This is a stripped down version of the code so far: http://pastebin.com/U1UYAPuT
As I strongly suspect the header to be broken in some way, my question comes down to: What is a correct header for a BMP file for 8Bit Grayscale?
Your code would be a lot simpler if you ditched BMP and wrote images as PGM files instead. The format is a lot more portable and easy to work with in code. Both formats are uncompressed so data rates would be about the same. The only thing you would lose would be the ability to view the images natively on Windows systems -- whether or not this is a big deal depends on your requirements.
Here are some examples.
EDIT
At the very least, if you write your images in PGM and broken BMP, you can use imagemagick to reliably convert the PGM to a working BMP. Then compare the headers of the working and broken BMP images using a binary diff tool and fix your BMP writer, if required.

TrueType Font Parsing in C

I want to read a ttf and draw a text with that font on a buffer. Even though there are libraries such as freetype, gd to do this task, I want to write my own code. Can you advice me on how to accomplish this task?
Unless you're one of the world's top experts on fonts, typography, and writing systems, the answer is simple: DON'T. TrueType/OpenType has a lot of tables you need to support for correct rendering, and even when using FreeType (which is an extremely low-level library), most people get it wrong.
If you need to do low-level, deterministic-across-platforms font handling, then at the very least, you should be using FreeType and libotf. This will provide you with access to the glyphs and outlines which you can then render however you like. In most cases though using your GUI system's text rendering routines will be a lot easier and less error-prone.
Finally, if you insist on ignoring my advice, a good RTFS on FreeType and Microsoft's online resources explaining the tables in TrueType/OpenType fonts are probably the best place to get started.
I would suggest you
Read all the TTF docs you can find
Find all the open source TTF parsers + renderers you can find, in many different languages, such as Freetype (c/c++), Batik (java), and anything else you can google for. Also George Williams' fontforge will likely be very helpful to you on your journey.
Rip apart all the programs you collected in 1. and see how they work. See if you can make a tiny small example program to do something simple, like dump the list of points for the outline of the letter "I".
Work on your rasterization. Start with something very simple, like rasterizing the letter "l".
The problem with TTF is that there is not a simple file format, and freetype handles a lot of crazy details for you. However if you don't care about portability, and you already have a specific TTF file you want to render, and you only care about a small simple alphabet, like Latin or Cyrillic, you might be OK.
Also you might want to check out a list of TTF documentation I linked to from my little project https://github.com/donbright/font_to_svg/
Not impossible, for anyone else tempted to try. I was curious about doing it because I like the DIY graphics approach where I allocate some memory and write into it, then save as a jpg or png. I pirated a bitmap font from giflib but that's strictly 8x8 pixels.
A few links:
`http://stevehanov.ca/blog/index.php?id=143`
`https://www.google.com/search?q=ttf+parser+c&ie=utf-8&oe=utf-8`
as R.. wrote the same time as i did in my comment, i would not to suggest to build another TTF-parser by your own. If you are eager to learn this very "spannende" field of Computer Science I would recommend "The Art of Computer Programming" Vol 2 from Donald E. Knuth. (it is Metafont, not TTF, but proven to be correct:-)

steganography library in C or Obj-C

Does anyone know of a good steganography library I can use thats written in C or Objective-C? It would need to be used in a Mac OS X application.
My provocative answer will be that you should try to implement/learn some algorithms for image hiding/unhiding yourself. Sample algorithm of 3-bit image hiding into 24-bit image is very simple and consists of about 10 Python lines or so (of course it may be a bit more with C/Obj-C). And you will get not bad quality of stego image - about 87.5% of original quality. So check my blog article about this steganography method.
This stego decoding method is done in GPU pixel shader program for fast decoding procedure.
However encoding was done with Python script which after use was deleted ;P
But i think that encoding procedure is very straightforward and can be understood from my blog article. If any questions about my stego algorithm- feel free to ask.

How can I create bitmaps (in 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

Resources