7
\$\begingroup\$

I'm not sure if this kind of golf is allowed, but here it is.

Your objective is to create an image containing one pixel of every possible RGB color. The image with the smallest file size wins.

Rules:

  • The image must be a PNG.
  • The image must contain exactly one of every possible RGB color.
  • Acceptable image dimensions are:
1x16777216
2x8388608
4x4194304
8x2097152
16x1048576
32x524288
64x262144
128x131072
256x65536
512x32768
1024x16384
2048x8192
4096x4096
8192x2048
16384x1024
32768x512
65536x256
131072x128
262144x64
524288x32
1048576x16
2097152x8
4194304x4
8388608x2
16777216x1

Example Image Example image

\$\endgroup\$
17
  • 4
    \$\begingroup\$ I think this has already been done, albeit as a popularity contest. \$\endgroup\$ – user Jan 12 at 14:35
  • 6
    \$\begingroup\$ @cairdcoinheringaahing I think the sentence "the image with the smallest file size wins" in a challenge named "Smallest image containing one of every RGB color" very strongly suggests that the image with the smallest file size wins. (note: I'm not OP) \$\endgroup\$ – the default. Jan 12 at 15:12
  • 4
    \$\begingroup\$ Also, as no one has said it yet, welcome to the site! In the future, we recommend using the Sandbox to get feedback on challenge ideas before posting to main \$\endgroup\$ – caird coinheringaahing Jan 12 at 15:21
  • 2
    \$\begingroup\$ Why is this being close voted? \$\endgroup\$ – the default. Jan 12 at 16:27
  • 6
    \$\begingroup\$ The challenge to me seems pretty clearly specified - craft a PNG with one of every color with the smallest file size by any means necessary (by hand, by code, etc.) \$\endgroup\$ – Sisyphus Jan 13 at 1:56
5
\$\begingroup\$

49683 bytes

image

Generated by this Python code:

import struct
import zlib

width = 2 ** 16
height = 2 ** 8
bit_depth = 8
color_type = 2
compression_method = 0
filter_method = 0
interlace_method = 0
scanlines = bytes(
    ([1] + (2 ** 8) * ([2, 1, 1] + (2 ** 8 - 1) * [1, 1, 1]))
    + (2 ** 8 - 1) * ([2] + 2 ** 16 * [2, 1, 2])
)

header = struct.pack(
    "!IIBBBBB",
    width,
    height,
    bit_depth,
    color_type,
    compression_method,
    filter_method,
    interlace_method,
)
compress = zlib.compressobj(level=9, wbits=15, memLevel=9)
compressed = compress.compress(scanlines) + compress.flush()
png = b"\x89PNG\r\n\x1a\n"
for type, data in [(b"IHDR", header), (b"IDAT", compressed), (b"IEND", b"")]:
    crc = 0xFFFFFFFF
    for b in type + data:
        crc ^= b
        for k in range(8):
            crc = 0xEDB88320 * (crc & 1) ^ crc >> 1
    crc ^= 0xFFFFFFFF
    png += struct.pack("!I4s", len(data), type) + data + struct.pack("!I", crc)

print(len(png))
with open("rgb.png", "wb") as f:
    f.write(png)

Try it online!

(Note: many PNG readers accept an image missing the IEND chunk, but it is required by the PNG specification so I have included it.)

\$\endgroup\$
1
\$\begingroup\$

256x65536, 49909 bytes

enter image description here

In my experience Google Chrome is the best tool to view this image.

The red channel starts at 255 in the first row and decreases by 1 each row.
The green channel slowly decreases from 255 to 0 in every row and the blue channel repeatly decreases from 255 to 0.

Generated by the following code in Python 3 with numpy and imageio:

import os

import imageio
import numpy as np

FILENAME = 'out.png'
DTYPE = np.uint8

values = np.arange(255, -1, -1, dtype=DTYPE)
image = np.empty((256, 256**2, 3), dtype=DTYPE)

image[..., 0] = values[:, np.newaxis]  # r
image[..., 1] = np.repeat(values, 256) # g
image[..., 2] = np.tile(values, 256)   # b

# Verify that all colors are present (slow)
# assert len(np.unique(image.reshape(256**3, 3), axis=0)) == 256 ** 3

imageio.imwrite(FILENAME, image)
os.system('wc ' + FILENAME)

I tried to reduce the file size with zopflipng, but even after 50 minutes of running it provided no improvement.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.