Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
I want to convert a 24-bits bmp files into a 8-bits bmp file. This is not always possible because imagemagick forces the depth of the produced image whatever the depth requested by -depth option.
This can be explained easily by a piece of code found in the file bmp.c, in the WriteBMPImage function :
bmp_info.bits_per_pixel=8;
if (image->colors <= 2)
bmp_info.bits_per_pixel=1;
else
if (image->colors <= 16)
bmp_info.bits_per_pixel=4;
else
if (image->colors <= 256)
bmp_info.bits_per_pixel=8;
This means that ImageMagick chooses the depth following the number of coulours so it is impossible to obtain a 8 bits BMP with less than 256 colours !
Commenting this piece of code solves my problem. I don't know if it is a bug or a feature but it could be a good idea to add a command-line option in order to be able to choose to activate the automatic depth reduction.
Perhaps I do not understand your problem. I took the rose: image and converted to 16-bit png. Then opened it in another program and converted it to 24-bits bmp (rose24.bmp)
identify rose24.bmp
rose24.bmp BMP 70x46 70x46+0+0 8-bit DirectClass 9.58kb
(indeed when IM opens the file on Q16 environment it only sees it as 8-bits)
I have 24 bits bmp files but in these files, there are only 16 colours used.
When I convert the image with this :
convert image24bits.bmp -colors 256 -Palette image.bmp, I have a 4bit depth when I identify the resulted image.
Moreover, if I have a 24 bits bmp with only black and white colours in it, the same conversion result in a 2 bits depth image. The piece of code at the beginning of the thread show that very well.
In order to reproduce the problem (how can I post an image here ?), just use a simple image with 4 colors in it for example. This image should be 8-bits depth in BMP.
In this example, img2.bmp will have a 4-bits depth. Identify say that the depth is 8 bits but when I look at the file or when I ask file property to Windows explorer, it says that the depth is 4 bits.
Same thing if I add -depth 8 option. I really don't see why IM forces the depth following the colour count of the image.
to post an image, you need to have a web site where you can place it. then you just put the link to the image here and surround it by the Img tag by highlighting the link and then clicking the Img button above.
If you have an 8bit IM version Q8, then the bit depth of any image will be 8-bits. If you are on a 16bit IM then you will nominally have 16-bit image if the type supports it. but you can force the depth to 8 by using -depth 8. This has no bearing on the number of colors in the image. If you only have 4 colors, then even if it is 8bit in depth it will still have 4 colors. But other systems, like Windows, etc will convert the image as it wants and may report it as 4-bit depth. IM is built to have the images converted in it with the same bit depth as the IM compile by default. At least this is my understanding.
I presume that the code in bmp.c is trying to make the most compact file possible so that if there are only two colour in the image, the code saves it as a one bit image.
There appears to be one way around it. The next statement in the bmp coder after the nested if is: