Page 1 of 1

I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-17T17:40:23-07:00
by bitMan_
Here's an explanation of what I want to do. Problem is, I don't know how to do it. First, a little info:

The SNES (video game console) stores it's images (known as "tiles") in a format of grayscale images (or "raw data" if you can call it that) with colors from 00 - 0F. Actual color is assigned in-game using palettes. I'm trying to replicate the way the SNES stores it's tiles; 16-color/16-gray images with zero overhead.

The program I use to view the tile data claims it's 4bpp. Here's an image of the program for reference: http://i.imgur.com/mEWoY7o.png

It's colored because a random palette has been assigned to the image, but as you can see, it appears to just be data from 00-0F to define the images. Now here's my problem: I don't know how to make an actual usable 16-color image file without overhead. I don't want any compression, no headers, no footers. I just want pure color index (00 through 0F representing grayscale colors for example) data I can use.

This program works great for converting, for example, a black and white bmp to 8 bytes (pure binary image). Now just think that except instead of 0 and 1 it's 0 through 16 which is 00 through 0F in hex.

According to my calculations, the file should be 640 bytes if it's 4bpp and is 80x16.

Unforunately, when saving as a bmp from photoshop, it's 716 bytes. Not a problem, I thought, I can run it through ImageMagick.
It came out as 714 bytes when trying to set the depth to 4. It also comes out as 714 when using -colors 16.

I was actually able to use

Code: Select all

convert test.bmp gray:test.bmp
to produce a file of the correct size (640 bytes), but using that method it won't open in any program whatsoever.

Please help! :(

Re: I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-17T23:15:24-07:00
by snibgo
I think "-depth 4 gray:x.bin" does what you want. The extension doesn't matter, but don't use a standard extension like "BMP" because it isn't BMP format, and programs that see the extension might misinterpret the file.

Code: Select all

convert -size 80x16 xc:#a4a4a4 -depth 4 gray:x.bin
This gives a file of 640 bytes, where every byte is (hex) #AA.

Re: I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-17T23:21:40-07:00
by bitMan_
Thanks, that works.
Unfortunately, color translation is very bad and I often get color loss, regardless of whether or not I use -colors 16. Using different colors on the source image provides varying results, but I'd like every color regardless of source colors to simply have a unique value from 0-16 / 00/0F. How can I prevent color (at least, color shade) loss? The source image will always only use 16 colors, that's guaranteed. What's not guarunteed is the source image will always use colors distinct enough to ImageMagick to result in completely unique colors (as in, rgb(50, 100, 50) and rgb(50, 120, 50) should NOT be blended into the same color in the .gray) with the above command.

Re: I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-17T23:51:35-07:00
by snibgo
bitMan_ wrote:The source image will always only use 16 colors, that's guaranteed. What's not guarunteed is the source image will always use colors distinct enough ...
So you need to change the colours, and make them distinct enough.

One method for this: "-unique-colors", then write to "txt:", give you a list of the 16 colours. A script then reads these, building 16 IM commands like this:

Code: Select all

-fill {new_colour} -opaque {old_colour}
...where {old_colour} is one of the 16 you have found, and {new_colour} is #000, #111, #222 ... #fff.

Then run a convert command that includes these 16 IM commands.

(Quite possibly, the job could be done in a single convert command, with no script required.)

Re: I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-18T00:01:21-07:00
by bitMan_
I'm very new to ImageMagick, and I don't know what kind of script you mean or how to make or run them. Can you elaborate a little bit?

Re: I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-18T00:12:47-07:00
by snibgo
What version of IM do you use? What shell (bash, Windows, or whatever)? Please provide a sample image. You can upload to somewhere like dropbox.com and paste the URL here.

Re: I want to make raw 16-color images. 00 through 0F hex. How?

Posted: 2016-12-18T03:14:40-07:00
by snibgo
Here is a Windows BAT script. It creates a 16-colour version of toes.png, then changes each of those colours to a different shade of gray.

It assumes the input image doesn't already have one of those shades of gray.

Code: Select all

set SRC=toes.png

%IM%convert ^
  %SRC% ^
  -format "tmp.png\n" ^
  +write info: ^
  -colors 16 ^
  +write tmp.png ^
  -unique-colors ^
  -crop 1x1 +repage ^
  -format "-fill gray(%%[fx:int(t*255/15)]) -opaque srgb(%%[fx:100*p{0,0}.r]\%%,%%[fx:100*p{0,0}.g]\%%,%%[fx:100*p{0,0}.b]\%%)\n" ^
  +write info: ^
  -format "-write out.png\n" ^
  info: >col16.scr

%IM%convert ^
  @col16.scr ^
  NULL:
If using v7, change the second convert to "magick -script col16.scr".

tmp.png:
Image
out.png:
Image
tmp.png has some colours very similar to each other. out.png has separated colours.

My code uses a script mechanism that isn't suppose to work in v6, but it does work.