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.
Related
I have some images (.bmp, .png, .jpg) in my directory. I want to convert this image to WORD array in order to display this image in LCD in 565 formats (16 bit pixel). How to convert an image to WORD array? Please help. Is is there any utility to just convert the image to binary? or Please provide some code in Windows C/CPP to convert it to binary?
This will depend a whole lot on the exact format of the input image.
Just converting "to words" isn't really expressing what you want to do, which is probably more like "convert bitmap images to an array of RGB565 pixels in row-major format".
You should look at image-processing libraries that allow you to load bitmap images, and read out the value of each pixel.
You can probably just convert directly to RGB565, shouldn't be too hard from any other bitmap format.
Note that there are both indexed and "true color" bitmap formats, and you sound like you need to handle both. If you'er lucky, the library for each format will abstract this away and have e.g. auint32 read_pixel_as_rgb888() function.
Also note that many bitmap image formats focus a lot on compression, which is why just reading in the bits of the file is not nearly enough, you need to de-compress the data according to the format. This is quite complex, which is why pre-written libraries are the only sane choice.
For PNG, look at libpng, for JPEG look for libjpeg. On second thought, these libraries might be a bit too low-level, and maybe you should look at something like SDL_Image instead.
You could try to use CImg to open image files - http://cimg.sourceforge.net/
I need to use some part of a jpeg image (a rectangle in the center) decompressing it up to DCT coeficients. Is this possible with libjpeg , or some other open source tool ?
What you're asking to do is non-standard use case. It is unlikely any open source library will have this feature built-in. It shouldn't take major modifications to get the data you need. How much effort are you willing to provide and how much do you understand the JPEG standard and the library you're using?
If your requested rectangle is on MCU boundaries, then it should be relatively simple to modify the decode loop to copy out the coefficient data that you're interested in. I am not familiar with the open source JPEG libraries, but within my own JPEG codec, the changes required would be trivial.
So what I need is simple: I have IplImage* I want to encode it into JPEG and wrap it with some additional JPEG data if needed (JPEG files contain noty only encoded pixels) and put tha file (not saved onto hard drive) into char* buffer. How to do such thing?
JPEG is a complex format. You can use the IJG jpeg library as a base to work with. However, be warned, it is a mess in itself and has a slight learning curve. It is open source, and you'll typically need to configure its build according to your compiler using a provided makefile (which may not be found with the library code itself)
I'm trying to write a JPEG/JFIF encoder and decoder from scratch using C. I experimented writing a sample JPEG file, but it seems that I cannot open it using MS paint, Firefox. But I can decode it using JPEGsnoop ( http://www.impulseadventure.com/photo/jpeg-snoop.html?ver=1.5.2) and http://nothings.org/stb_image.c . I think the sample JPEG file complies the JPEG/JFIF standard, I don't know why applications like MS paint and Firefox cannot open it.
Here is how the sample JPEG looks like:
SOI
APP0 segment
DQT segment (contains two quantization tables)
COM segment
SOF0 segment
DHT segment (contains four Huffman tables)
SOS segment
huffman encoded data
EOI
The sample JPEG file has three component Y Cb Cr. No subsampling for Cb Cr component.
The two quantization tables are all filled with ones.
The Four huffman tables in DHT segment are all identical, it looks like this
[0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0]
[0,1,2, ... , 254]
That means all the codes are 8bits, so huffman encoding does not really compress data.
The huffman encoded data look like this:
[0x0000(DC) 0x0000(AC)](Y)
[0x0000(DC) 0x0000(AC)](Cb)
[0x0000(DC) 0x0000(AC)](Cr) for all (i, j) MCUs except (10, 10)
the data in (10, 10) MCU:
[0x0008(DC) 0x0000(DC), 0x0000(AC)](Y)
[0x0000(DC) 0x0000(AC)](Cb)
[0x0000(DC) 0x0000(AC)](Cr)
Can anyone tell me what is wrong with this sample JPEG file? thanks.
Here is a link to the sample JPEG file (ha.jpg) http://www.guoxiaoyong.net/ha.jpg
I had a similar problem years ago with some PNG code (though I didn't write it from scratch). It turns out my code was more standards compliant than the libraries by Windows, some browsers, etc. They did fine on typical cases, but choked on unusual and contrived images, even if they were completely in line with the standard. A common way to trip them up was to use an odd pixel width for the image. Almost half of my test suite was not viewable with Windows. (This was many versions ago, like Windows 95. The Windows codecs have improved substantially.)
I ended up building the open source PNG library and using it as my reference implementation. As long as the images that my code produced could be parsed by the reference implementation and vice versa, I called it good. I also checked that my code could display any image that Windows could display. Every time I found a bug, I added the image to my test suite before I fixed it. That was good enough for my project.
You could do the same. I believe there's an open source JPEG library that's widely used as a reference implementation.
If you really want to figure out why Firefox (or whatever) cannot open your image, you could try starting with an image that does open in Firefox. Incrementally make small changes (e.g, with a hex editor) to make it more like the image that fails. That might help you narrow down what aspect of your image is tripping up the application. Admittedly, some of those steps may be hard to try.
Firefox, (and many other apps AFAIK) is based on the open-source JPEG library from the Independent JPEG group.
You could download the source for this, and then see exactly why and when it doesn't like your file.
Also, this would save you reinventing the wheel :-)
I think your file is very unconventionally coded. I would suggest that you find a reference file and try to mimic that structure. Also, I would use the sample tables from the standard. Your Huffman data is full of zeros making every DC-value zero, followed by and End-of-block.
If you look in jpegsnoop your image is in two shades but it should be homogeneous. My guess is that you haven't got enough data to code the image at the resolution you've specified. I believe a lot of decoders would assume that it means your file is corrupt.
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.