Page 1 of 1

Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T21:06:31-07:00
by patrick99e99
I am seeking a way to easily get a 1D palette image that is sorted by intensity (lowest on the left, highest on the right).

I am curious if there's a simple way to give imagemagick an input image and it will find all the colors, sort them, and then output a pixel for every color in a long horizontal row?

Possible?

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T22:00:26-07:00
by snibgo
I'm not sure what you mean. Do you want the image sorted, or the palette sorted? I think you want the image sorted, whether or not it has a palette, like this:

Input:
Image
Output:
Image

My process module "sortpixels" does that, sorting according to the "-intensity" setting.

I also have a "sortpixelsblue" (not yet published) which sorts by the blue channel, then the green channel within that, then the red channel. In other words, it sorts by channel, but blue is the most significant. (I may rename it "sortpixelsbgr".)

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T22:34:24-07:00
by fmw42
What might be the purpose/use for such a sorted image?

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T22:50:29-07:00
by patrick99e99
Sorry for not giving more context... I am making a video game that uses multiple (large) colored textures that are identical to each other except for their color.. So I have a blue, purple, red, green, orange, yellow, white version of all my texture files, which was really driving me crazy because it was so wasteful app-size-wise as well as memory-wise. I have been exploring ways to programmatically color the textures, and the technique I have decided to go with is to use a single grayscale image, and with OpenGL fragment shaders, colorize the image from a palette based on the original colored files.

The way this has to work is, the palette needs to be 1px in height, and "number of colors in the texture" pixels in width. If they are sorted from lowest intensity to highest intensity, then OpenGL can easily translate a grayscale pixel in intensity to it's matching colored version.

I have tried using Gimp to generate a color palette, but it is not really working well (sorting seems to be kind of buggy, and generating an image based off of the palette is not as easy as I would like it to be either).

So I am hoping for an ImageMagick solution that I can just give it a colorized file, and it will output a 1px high png image that has every color in the texture sorted from lowest intensity (on the left side) to brightest intensity (on the right side).

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T23:24:01-07:00
by snibgo
patrick99e99 wrote:... give it a colorized file, and it will output a 1px high png image that has every color in the texture sorted from lowest intensity (on the left side) to brightest intensity (on the right side).
That's exactly what my module does.

But my modules need to be compiled. If you don't have many colours (eg <=256), you could have a script that writes pixels as text lines (intensity, red, green, blue), sort by intensity, and rebuild the image from just the RGB values.

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T23:32:55-07:00
by patrick99e99
Yeah, I just don't know anything about how to programmatically build image files. Can you give me an example of how to build an image from a text file?

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-08T23:34:59-07:00
by fmw42
On unix systems, you can do the following:

convert input imaged to a histogram in text format and store each histogram intensity value in an Arr(ay) sorted by highest intensity first
Then us NetPGM to convert the array into a 1D grayscale (row) image and save as png.

Code: Select all

infile="logo:"
Arr=(`convert "$infile" -colorspace gray -depth 8 -format "%c" histogram:info: |\
tr -cs "0-9\n" " " | cut -d\  -f3 | sort -gr`)
num=${#Arr[*]}
echo "P2 $num 1 255\n ${Arr[*]}" | convert - -depth 8 lut.png
So now what do you want to do with this 1D lookup table image?

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-09T01:12:36-07:00
by patrick99e99
Thank you very much for that code.. I'll try it out.

The plan is, in my game, I have a fragment shader program running which will have the grayscale texture as the primary texture, and a 2ndary texture will be the palette, and as the image is drawn, it will be sampling the color from the palette based off of intensity and drawing itself with the colors of the palette.

Re: Is it possible to *easily* create a nx1 png palette from an image?

Posted: 2016-11-09T01:17:56-07:00
by patrick99e99
So, how do I make that be non-grayscale? I need this 1D palette to be of the colors from the colored image, not the grayscale version.. I tried changing that flag to "-colorspace rgb", but saw no difference.