I want to iterate through every picture (E.g. 500x500 pixels, every pixel can have 255 different colors) I want to be able to pass in $n$ and draw the $n$-th picture without having the picture $n-1$.
My idea was to divide the problem into
- Getting the $x$-th ordered set of pixels. E.g.: $[white, red, red]$
- Permutate the $x$-th ordered set of pixels until I got every $y$-th combination. E.g.: $[white, red, red], [red, white, red], [red, red, white]$ (No duplicates, e.g. $[red, red, white]$ shall not appear twice)
So I would need calculations to get $x$ and $y$ from $n$.
- Given $n$, is it possible to draw the corresponding picture?
- If yes, is it possible with my idea? If not, can you give me a mathematical hint to do it?
First, note that my comment does provide a way to list the $N$ distinct $w\times h$ pictures using $k$ colors as $p_1$ through $p_N$ in such a way that picture $p_n$ can be drawn given $n$. Identify the colors as $c_0,c_1,\dots, c_k$ and identify the picture whose pixel colors (left to right, top to bottom) are $(c_{i_0},\dots,c_{i_{wh-1}})$ as $p_n$, where $$n=\sum_{j=0}^{wh-1}c_{i_j}\cdot k^j.$$
Perhaps more understandably, I’m observing that a picture can be looked at as a single number: the number with $wh$-many digits (each pixel is a digit), using digits from $0$ to $k-1$, where there are $k$ available colors. And starting the digits with $0$ means that the numbers corresponding to the possible pictures are exactly the numbers from $0$ to $k^{wh}-1$.
That is, let $n$ for a given picture be the $wh$-digit base-$k$ number whose $j$-th digit identifies the color of the $j$-th pixel. Then it’s straightforward to compose the picture $p_n$ from $n$ (on a system that can handle the large base $k$ integer calculations.
From the comments, however, I believe you are asking for something different: Is it possible to list the pictures as $p_n$ in such a way that the collection of all pictures with identical color histograms are listed contiguously?
For example, the $w<N$ pictures with 20 blue pixels, 17 red pixels, and 12 green pixels, say for $7\times7$ pictures with at most five colors might be $p_{x+1}, p_{x+2}, \dots p_{x+w}$ (if there were $x$ pictures having histograms that “preceded” the one mentioned) and do this in such a way that $p_n$ can be drawn directly, without drawing all previous pictures to get it.
It is, but it would be clumsy and require some auxiliary tables or formulas to refer to.
The number of different histograms for $w\times h$ pictures using at most $c$ colors is the number of ordered partitions of $w\cdot h$ with at most $c$ parts. (This number can be computed using “stars and bars” and is $\ell={wh+c-1\choose c-1}$. The histograms $h_1,\dots,h_\ell$ can be enumerated lexicographically, but to know what histogram the $n$-th picture has requires having precomputed the partial sums of the number of pictures with each histogram. For a given histogram, say $wh=i_1+i_2+\cdots+i_c$, there are $wh\choose i_1,i_2,\dots,i_c$ pictures. And you would have to systematically enumerate the pictures with a given histogram in order to get the picture knowing (using the precomputations) that $p_n$ is, say, the $m$-th picture with histogram $i_1+i_2+\cdots+i_c$ and be clever about how to find that $m$-th picture with that specific histogram directly from knowing how you enumerated pictures with identical histograms (these could also be enumerated lexicographically and formulas could probably be obtained).
My comment answers your initial question, “Can I list the pictures $p_1$ through $p_N$ and then given $n$, draw $p_n$ directly.” But you say you had that idea and I guess don’t want to use it, being curious to know if there is an approach that lists pictures with identical histograms together in the list of all pictures. So you are asking not just for an answer to your initial question, but you’re asking for an alternative answer that takes a specific different approach, where the collection of pictures with the same distribution of colors are listed consecutively.
I wouldn’t say this is impossible, but it’s certainly not straightforward. If all you really need is a way to assign a unique number to each picture and to recover the picture from the number, my suggestion of using $wh$-length base-$k$ numbers as picture labels should work.