Forcing one palette per Tiles

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?".
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Forcing one palette per Tiles

Post by X-death »

Hi,
First Apologies for the question in the first post but i have made a research in your forum and in the documentation but without sucess.
What i want to do is a bit complicated.
i work on an old game console ( Super Nes ) and i want to display a 176 colors image with eleven différent palettes.

i have sucessfully converted an image to 15-bit RGB with Image magik but now i search a way to use only one palette per tiles.
my goal is to extract eleven differents palette from a picture and one tiles can only use one of them.

Is it possible to doing this with ImageMagick ?

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

Re: Forcing one palette per Tiles

Post by fmw42 »

I think you need to explain in more detail, perhaps with some diagrams or pictures.

What is a "tile" in your concept? Is it an image?

What do you mean by "display a 176 colors image with eleven différent palettes."? What do you mean by a "palette"? Is that a color table? The 176 colors usually makes up the color table for a palette image. Do you want to recolor an image based on different color tables? If so, see the function -remap without -dither none. http://www.imagemagick.org/Usage/quantize/#remap
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Re: Forcing one palette per Tiles

Post by X-death »

Thanks for your fastest reply !
excuse me so 1Tiles = 8x8 pixel block
So yes i have sucessfully recolored a picture in 15 bit RGB but now i need to remap them with three rules :

1) Picture must have eleven ( or less) differents color tables
2)Color tables can only have a max of 16 differents color ( with remap some of the tiles use 20 differrents color)
3)A tiles in a picture ( 8x8 pixel) must only use one color tables

( now i will read your link thanks )
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Forcing one palette per Tiles

Post by fmw42 »

I do not think IM has any way to have multiple colortables in an image or to do this with one simple command. I think you would have to crop each tile, process it with one colortable image from a stored set of colortable images via -remap, then montage all the tiles together again.

You may be able to avoid the crop step by using a different -region for each tile of the image and different colortable image using -remap.

see http://www.imagemagick.org/Usage/masking/#regions
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Re: Forcing one palette per Tiles

Post by X-death »

and ImageMagik can find the best colortable for each tiles ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Forcing one palette per Tiles

Post by fmw42 »

What is the desired criterion or test for "best colortable"? I assumed you knew which colortable you wanted to use for each tile.
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Re: Forcing one palette per Tiles

Post by X-death »

No i don't know it that's why it's hard.

i understand what i want is complicated, but old game consoles have some restricted rules ;)

Colortables must be generated from the original image ( it can be with remap command and the 15 Bit RGB colormap).
So image can be cropped into 8x8 parts ( Tiles ).
So the next step ( if it's possible) is to find the best colortable ( one of the eleven) for each tiles and apply it to the tiles.
Final step is to rebuild image from the new tiles.

the critical steps is to generate the best colortable because one tiles must only use one colortables.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Forcing one palette per Tiles

Post by snibgo »

X-death wrote:So the next step ( if it's possible) is to find the best colortable ( one of the eleven) for each tiles and apply it to the tiles.
This seems to be:

1. Get a tile.
2. Apply a -remap to that tile. Calculate the difference between the tile and remapped tile (compare -metric RMSE).
3. Repeat step 2 for all maps.
4. The lowest difference gives the best remap for that tile.
5. Repeat steps 1-4 for all tiles.
snibgo's IM pages: im.snibgo.com
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Re: Forcing one palette per Tiles

Post by X-death »

Thanks you very much !
i will work on that !
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Forcing one palette per Tiles

Post by fmw42 »

The OP has not given any criterion for best match. User snibgo's compare is quite reasonable, if the criterion is to change the colors and keep the results closest to the original. But that may not be what is desired. I have no idea what the goal is or what criterion is needed for gaming images. No clear explanation was provided even though I asked for how "best colortable" would be determined.
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Re: Forcing one palette per Tiles

Post by X-death »

Excuse me i'm not english and i have misunderstanding your question.

So my criterion is to change the colors of the tiles for match the rules : 1 colortable per tiles and a result closest to the original.
I think the crirical parts of my process is the colortable generation, i have only a limited number of colortable ( eleven) and with a max of 16 colors per colortables.

I have a question ,
if the picture could be croped by a fixed number of colors region maybe that step could help the colortable generation process?
Next step is generate fixed number of colortable per region and test every tiles for match the most acceptable colortable ( with less difference).

Is it possible to crop a picture by size ( x8 multiple) and by color region ?

please if it'n not clear tell me and i will try to made a complete explanation of my wanted process with picture.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Forcing one palette per Tiles

Post by fmw42 »

Is it possible to crop a picture by size ( x8 multiple) and by color region ?
I am not sure I understand. By color region, do you mean a solid color or just a generally similar color. Color regions will not be rectangles? So what do you do at the boundaries where an 8x8 region will be mixed colors? If this is not a problem, then you need an image segmentation by color. IM does not have very good color segmentation. The only ones are -colors and -segment. But neither identify the regions for the given color. You probably need to look elsewhere for the segmentation.

The rest of your process was detailed by user snibgo above, which finds the best colortable to use via testing each one and selecting the one with the lowest rmse measure using compare on each tile.

Perhaps you could post an example image that needs to be processed and your colortables. You can upload to some place like dropbox.com and then put the URLs here. Can you also explain why one global -remap is not adequate and why you have to process by such small (8x8) tiles. That is only 64 pixels and thus only 64 colors maximum. But you say you want 176 colors total over the image.

Are you trying to make each 8x8 region one color only? If so, scale the image down in blocks of 8x8 using -scale so that it averages each block of 8x8, then do a global remap, then scale it up again.
X-death
Posts: 11
Joined: 2015-06-23T08:59:51-07:00
Authentication code: 6789

Re: Forcing one palette per Tiles

Post by X-death »

Hi,
sorry for the delay and thanks all for the high quality of your reply :D
I will take some time for explain the global process of what i want and my difficulties.

So first i need this tools for help me to made graphics in old video games ( Super Nintendo / Super NES)
the video processor of SNES can't manipulate pixel directly it can only manipulate portion of the screen named Tiles ( 8x8 pixel area) for speeding the process.
for displaying a tiles the Super Nes can use one of these 16 colortable only and a color table have a maximum of 16 different colors encoded in 15-bit RGB.

Exemple of the total Snes possible colors :

Image

So for displayind and image i need to be sure that each tiles of the image use only one colortable.
And i can't use all of the colormap for displaying one big image ( i must keep some for other graphical parts).

So according to your reply here is the complete process witch use 11 of the 16 colormap so 176 colors
-Take one image
-Convert it to 15 bit RGB with the global 15 bit RGB color map
-Convert it to 176 colors with the help of the 15 bit RGB color map
-Extract Palette ( crucial part )
-Extract Tiles
-test each tiles with each pal and find the best result
-apply the best result in a tiles and save it into a diffrent folder
-Same process for each tiles..
-Finally recreate picture with the new tiles
For writting the process i have started to write a bash file.
Here is my code ( not finished yet i'm looking into the image magik code and exemple for the -Metric RMSE)

http://pastebin.com/KR8aa1KH

and here is my test image ( not really important it's just for the test)

Image

I think the most critical part is the colormap extraction , if colormap can be sorted by color and not by number the result can be better.

At this point did you understand all of my explanation ?
can you help me with a command for generate good colormap ?

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

Re: Forcing one palette per Tiles

Post by fmw42 »

Sorry, I still do not understand. Can you post an example single colortable from your 16 colortables? Or post all 16 colortable. In your posted image of the SNES color palette, there are more than 16 colors? So I do not understand what that image represents. Is a tile a block of 8x8 pixels from the posted image?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Forcing one palette per Tiles

Post by fmw42 »

From your code:
convert shenmue_snes.png -dither None -colors 176 +dither -remap Snes_palette.png shenmue_snes_176.png
You seem to be using -colors 176 to reduce the image to 176 colors. (Note -colors N does not guarantee that you get N colors. N would only be the maximum number possible). Then you seem to be using that other image of the SNES color table to remap to those colors.

Then from your code:
convert -crop 8x8 shenmue_snes_176.png Tiles/tiles%.png
You seem to be cropping 8x8 tiles from the remapped original image. (Note that proper syntax would have the crop after the input image)

Then from your code:
convert shenmue_snes_176.png -unique-colors -filter Point -scale 100% Palettes/shenmue_snes_pal.png
convert -crop 16x1 Palettes/shenmue_snes_pal.png Palettes/pal%.png
You seem to be forming color tables by getting 11 groups of 16x1 colors as images from the (176?) unique colors to then use to process the tiles and do the comparison. (Note that proper syntax would have the crop after the input image) (Note that -filter Point -scale 100% does not seem to do anything -- you can remove that part of the code)

Do I understand this so far?

But there are more combinations of 16 colors than the contiguous 16 colors from the 176 unique colors? So there are many many more possible 16-color colortables than 11. Why are you using the unique colors in the order IM extracts them from the image for your colortables. What if IM had listed them in some other order. It probably lists the colors as it finds them by scan-line order, which can come from anywhere in the image. I presumed you had 11 fixed colortables for any image!
Post Reply