Welcome to the Creatures Wiki! Log in and join the community.

555/565

From Creatures Wiki
Revision as of 16:09, 9 July 2022 by Ligfx (talk | contribs) (Add more info on RGB5551 format)
Jump to navigation Jump to search

Summary

555, 565, and 5551 are used to describe different allocations of bits inside a 16-bit integer representing a pixel, as used in Creatures sprite files such as S16 files, C16 files, and BLK files. This is briefly discussed in the Creating Agent Art tutorial for Creatures 2.

Technical Information

In RGB555 format, the bits are allocated as follows:


0rrrrrgggggbbbbb

In RGB565 format, the bits are allocated as follows:


rrrrrggggggbbbbb

Additionally, sprite files used by the Macintosh Creatures Evolution Engine exclusively use RGB5551 format and big-endian integers:


rrrrrgggggbbbbb0

The following macros can be used in C or C++ to decode these values to normal 24-bit format:


/* Decoding macros by bd_ aka bdonlan. I release these into the public domain. */

#define RGB(r, g, b) ((r) << 16 | (g) << 8 | (b))

#define DECODE_555(v)           \
	(RGB(                      \
		  ((uint32_t)(v) & 0x7c00) >> 7,  \
		  ((uint32_t)(v) & 0x03e0) >> 2,  \
		  ((uint32_t)(v) & 0x001f) << 3  \
		 ))

#define DECODE_565(v)           \
	(RGB(                      \
		  ((uint32_t)(v) & 0xf800) >> 8 , \
		  ((uint32_t)(v) & 0x07e0) >> 3 , \
		  ((uint32_t)(v) & 0x001f) << 3   \
		 ))

#define DECODE_5551(v)           \
	(RGB(                      \
		  ((uint32_t)(v) & 0xf800) >> 8,  \
		  ((uint32_t)(v) & 0x07c0) >> 3,  \
		  ((uint32_t)(v) & 0x003e) << 2  \
		 ))

The following functions can be used in JavaScript to decode and encode these values:


// Functions by SG aka Zenzoa. I release these into the public domain.

function parsePixel(pixelData, is555, is5551) {
	let r, g, b
	if (is5551) {
		// n16 + m16 format
		r = (pixel & 0xf800) >> 8
		g = (pixel & 0x07c0) >> 3
		b = (pixel & 0x003e) << 2
	} else if (is555) {
		// 555 format
		r = (pixel & 0x7c00) >> 7
		g = (pixel & 0x03e0) >> 2
		b = (pixel & 0x001f) << 3
	} else {
		// 565 format
		r = (pixel & 0xf800) >> 8
		g = (pixel & 0x07e0) >> 3
		b = (pixel & 0x001f) << 3
	}
	return { r, g, b }
}
	
function encodePixel(r, g, b, is555, is5551) {
	if (is5551) {
		// n16 + m16 format
		return ((r << 8) & 0xf800) | ((g << 3) & 0x07c0) | ((b >> 2) & 0x003e)
	} else if (is555) {
		// 555 format
		return ((r << 7) & 0x7c00) | ((g << 2) & 0x03e0) | ((b >> 3) & 0x001f)
	} else {
		// 565 format
		return ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | ((b >> 3) & 0x001f)
	}
}