Depth of Image in BMP files: bug or feature ?
Posted: 2008-07-31T12:42:27-07:00
by kritchek
Hi,
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 :
Code: Select all
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.
What do you think about that ?
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-07-31T13:12:32-07:00
by fmw42
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)
But then one can change the number of colors:
convert rose24.bmp -colors 16 rose16colors.bmp
identify -verbose rose16colors.bmp
Image: rose16colors.bmp
Format: BMP (Microsoft Windows bitmap image)
Class: PseudoClass
Geometry: 70x46+0+0
Resolution: 28.35x28.35
Print size: 2.46914x1.62257
Units: PixelsPerCentimeter
Type: Palette
Endianess: Undefined
Colorspace: RGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
red:
min: 43 (0.168627)
max: 245 (0.960784)
mean: 144.565 (0.56692)
standard deviation: 69.0378 (0.270737)
green:
min: 47 (0.184314)
max: 244 (0.956863)
mean: 89.0382 (0.349169)
standard deviation: 51.5993 (0.20235)
blue:
min: 30 (0.117647)
max: 239 (0.937255)
mean: 78.9 (0.309412)
standard deviation: 53.0419 (0.208007)
Histogram:
352: ( 51, 50, 45) #33322D rgb(51,50,45)
337: (209, 56, 45) #D1382D rgb(209,56,45)
274: (101, 98, 83) #656253 rgb(101,98,83)
261: (233, 81, 89) #E95159 rgb(233,81,89)
254: (237, 51, 52) #ED3334 rgb(237,51,52)
241: (161, 59, 44) #A13B2C rgb(161,59,44)
239: ( 58, 67, 56) #3A4338 rgb(58,67,56)
235: ( 98, 57, 47) #62392F rgb(98,57,47)
199: (245,244,239) #F5F4EF rgb(245,244,239)
189: (110,147, 87) #6E9357 rgb(110,147,87)
147: (140,133,146) #8C8592 rgb(140,133,146)
130: (188, 72, 68) #BC4844 rgb(188,72,68)
118: (166,166,187) #A6A6BB rgb(166,166,187)
111: ( 83, 76, 56) #534C38 rgb(83,76,56)
73: ( 93,131, 61) #5D833D rgb(93,131,61)
60: ( 43, 47, 30) #2B2F1E rgb(43,47,30)
Colormap: 16
0: ( 51, 50, 45) #33322D rgb(51,50,45)
1: ( 43, 47, 30) #2B2F1E rgb(43,47,30)
2: ( 98, 57, 47) #62392F rgb(98,57,47)
3: ( 83, 76, 56) #534C38 rgb(83,76,56)
4: (101, 98, 83) #656253 rgb(101,98,83)
5: ( 58, 67, 56) #3A4338 rgb(58,67,56)
6: (161, 59, 44) #A13B2C rgb(161,59,44)
7: (209, 56, 45) #D1382D rgb(209,56,45)
8: (237, 51, 52) #ED3334 rgb(237,51,52)
9: (233, 81, 89) #E95159 rgb(233,81,89)
10: (188, 72, 68) #BC4844 rgb(188,72,68)
11: (110,147, 87) #6E9357 rgb(110,147,87)
12: ( 93,131, 61) #5D833D rgb(93,131,61)
13: (245,244,239) #F5F4EF rgb(245,244,239)
14: (166,166,187) #A6A6BB rgb(166,166,187)
15: (140,133,146) #8C8592 rgb(140,133,146)
etc ...
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-07-31T13:49:25-07:00
by kritchek
Well, I don't know where is the problem.
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.
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-07-31T14:05:55-07:00
by fmw42
I suggest you post your image so others can take a look at it
However once you have an image with only 16 colors, you cannot generate more colors by telling it -colors 256.
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-08-01T00:15:29-07:00
by kritchek
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.
Then just call :
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.
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-08-01T00:49:50-07:00
by fmw42
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.
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-08-01T00:53:36-07:00
by fmw42
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.
Perhaps someone from IM can explain this better.
Re: Depth of Image in BMP files: bug or feature ?
Posted: 2008-08-01T08:55:35-07:00
by el_supremo
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:
Code: Select all
if (image->compression == RLECompression)
bmp_info.bits_per_pixel=8;
So if you don't mind the bmp being compressed you can specify that it is RLE compressed and that will force it back to 8 bits again.
If you don't want compression either, I think the only way is to modify the code so that it does what you want.
Pete