Converting 8-bit bitmap to png

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
hubertus
Posts: 1
Joined: 2020-02-25T11:24:41-07:00
Authentication code: 1152

Converting 8-bit bitmap to png

Post by hubertus »

Hi,

I'm currently working on a fun project to restore/remaster old games. To do this I extract imagedata from archive files of old games. Those games use mostly their own format. In my case I have just a byte array where each byte is a pixel. My problem is that the data is converted to png but as a grayscale image. :(

This is the byte array input.bin.
And this is my current result:
Image

I convert my byte array this via this command:

Code: Select all

convert -depth 8 -colors 256 -size 320x200+0 gray:input.bin output.png
I use the following parameters:
- depth 8 = because 1 byte per pixel
- colors 256 = because each pixel can represent one color of 256
- size 320x200+0 = because the image is 320x200 and +0 because there is no header
- gray: = because I don't know a filter which I can use for this and I think this is the reason for my issue but "rgb:" did not work

Does anyone have a suggestion how I can convert this byte array to a png in color? :)

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 - Debian GNU/Linux

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting 8-bit bitmap to png

Post by snibgo »

"-colors 256" will reduce an image to 256 colours. As your image already has only 256 colours, you don't need to reduce them.

Your input.bin in 64000 bytes, 320x200 pixels, at one byte/pixel.

Your command makes an image with 256 shades of gray.
hubertus wrote:Does anyone have a suggestion how I can convert this byte array to a png in color?
You need to know what the colours are, and what colour correspond to what shade of gray. When you know that, you can make a 256x1 CLUT and apply that to your png.

If you don't care what the colours are, you could use "+level-colors". See https://www.imagemagick.org/script/comm ... vel-colors
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting 8-bit bitmap to png

Post by fmw42 »

If all you want is to make the colorspace read as sRGB, but keep the gray tone image, then just do:

Code: Select all

convert -depth 8 -size 320x200+0 gray:input.bin PNG24:output.png
If you want to colorize the image, then you can create clut (color map image) and apply that to your grayscale image.

Unix syntax:

Code: Select all

convert -depth 8 -size 320x200+0 gray:input.bin \
\( xc:red xc:orange xc:yellow xc:green1 xc:cyan xc:blue xc:blueviolet +append -filter Cubic -resize 512x1! \) \
-clut result.png
Windows syntax:

Code: Select all

convert -depth 8 -size 320x200+0 gray:input.bin ^
( xc:red xc:orange xc:yellow xc:green1 xc:cyan xc:blue xc:blueviolet +append -filter Cubic -resize 512x1! ) ^
-clut result.png
You can replace my colors with any list of your own colors.

Locked