95#define ENC_BUFFER_SIZE 16384
104 uint8_t* val_ptr = (uint8_t*)&val;
105 return (uint16_t)((val_ptr[1] << 0) | (val_ptr[0] << 8));
115 uint8_t* val_ptr = (uint8_t*)&val;
116 return (uint32_t)((val_ptr[3] << 0) | (val_ptr[2] << 8) | (val_ptr[1] << 16) | (val_ptr[0] << 24));
142 printf(
"Example usage: qoi_enc <filename> <width> <height> <bitdepth> <output>\n");
143 printf(
"16: 16-bit RGBA5551\n32: 32-bit RGBA32\n");
152int main(
int argc,
char* argv[])
163 uint8_t* file_buffer0 = NULL;
164 uint8_t* file_buffer1 = NULL;
168 uint32_t width, height;
169 uint8_t channels, colorspace;
174 uint16_t* rgba5551_frame_buffer;
177 uint32_t* rgba32_scratch_frame_buffer;
198 width = strtoul(argv[
ENC_WIDTH], NULL, 0);
203 if (!(bitdepth == 16 || bitdepth == 32)) {
214 if (width < 0 || height < 0)
231 fseek(fp, 0, SEEK_END);
233 file_size = ftell(fp);
235 if ((
size_t)width * (
size_t)height * (bitdepth == 16 ? 2 : 4) < file_size)
237 size_t image_size = (size_t)width * (
size_t)height * (bitdepth == 16 ? 2 : 4);
238 size_t size_difference = image_size - file_size;
241 "%zu %s are required for the file, %s. Your requested image dimensions and bit-depth allocates %zu %s. That is %zu %s difference\n",
243 (file_size > 1) ?
"bytes" :
"byte",
246 (image_size > 1) ?
"bytes" :
"byte",
249 (size_difference > 1) ?
"bytes" :
"byte"
256 fseek(fp, 0, SEEK_SET);
258 file_buffer0 = (uint8_t*)calloc(file_size + 1,
sizeof(uint8_t));
266 if (bitdepth == 16) {
267 file_buffer1 = (uint8_t*)calloc(file_size + 1,
sizeof(uint8_t)*2);
277 if (fread(file_buffer0, 1, file_size, fp) < file_size)
293 if (bitdepth == 32) {
295 rgba32_scratch_frame_buffer = (uint32_t*)file_buffer0;
298 rgba5551_frame_buffer = (uint16_t*)file_buffer0;
300 for (
size_t i = 0; i < file_size /
sizeof(uint16_t); i++) {
303 int r = (
read_be_u16(rgba5551_frame_buffer[i]) >> 11) & 0x1F;
304 int g = (
read_be_u16(rgba5551_frame_buffer[i]) >> 6) & 0x1F;
305 int b = (
read_be_u16(rgba5551_frame_buffer[i]) >> 1) & 0x1F;
306 int a = (
read_be_u16(rgba5551_frame_buffer[i]) >> 0) & 0x01;
309 r = (r << 3) | (r >> 2);
310 g = (g << 3) | (g >> 2);
311 b = (b << 3) | (b >> 2);
315 file_buffer1[i*4] = r;
316 file_buffer1[i*4+1] = g;
317 file_buffer1[i*4+2] = b;
318 file_buffer1[i*4+3] = a;
324 rgba5551_frame_buffer = NULL;
326 rgba32_scratch_frame_buffer = (uint32_t*)file_buffer1;
341 "Encoding %s to %s. Please wait . . .\n",
354 fwrite(header, 1, 14, fp);
359 for (uint32_t px = 0; px < enc.
len; px++)
361 uint32_t rgba = rgba32_scratch_frame_buffer[px];
390 if (bitdepth == 32) {
397 rgba32_scratch_frame_buffer = NULL;
400 rgba32_scratch_frame_buffer = NULL;
int main(void)
Entry point for this N64 ROM.
#define ENC_BUFFER_SIZE
Small buffer size for encoder because of memory constrant of 4MB for N64 without the 4MB Expansion Pa...
const char revised_date[]
Date of the last revision.
void print_help(void)
Prints the help information for the encoder.
QOI_ENC_ARGS
Enumeration for command-line argument indices.
@ ENC_HEIGHT
The height of the image.
@ ENC_WIDTH
The width of the image.
@ ENC_INPUT_FILENAME
The input filename.
@ ENC_NAME
The name of the encoder program, typically "qoi_enc".
@ ENC_BITDEPTH
The bit depth of the image.
@ ENC_OUTPUT_FILENAME
The output filename.
void print_version(void)
Prints the version information of the encoder.
const char version_number[]
Version number of the encoder.
uint16_t read_be_u16(uint16_t val)
Reads a big-endian 16-bit unsigned integer from memory.
uint32_t read_be_u32(uint32_t val)
Reads a big-endian 32-bit unsigned integer from memory.
bool qoi_desc_init(qoi_desc_t *desc)
Initalize the QOI desciptor to the default values.
void write_qoi_header(qoi_desc_t *desc, void *dest)
Writes the QOI metadata information to the file.
void qoi_set_dimensions(qoi_desc_t *desc, uint32_t width, uint32_t height)
Sets the image dimensions of an image for QOI descriptor.
static const uint8_t QOI_PADDING[8]
QOI end of file padding bytes.
void qoi_encode_chunk(qoi_desc_t *desc, qoi_enc_t *enc, void *qoi_pixel_bytes)
Encode pixel data into QOI opcodes.
bool qoi_enc_init(qoi_desc_t *desc, qoi_enc_t *enc)
Initalize the QOI encoder to the default state.
void qoi_set_channels(qoi_desc_t *desc, uint8_t channels)
Sets the amount of channels of an image for QOI descriptor.
bool qoi_enc_set_buffer(qoi_enc_t *enc, void *newBuffer, uint32_t len, bool shouldFreePrevBuffer)
Sets a buffer for the QOI encoder, automatically free if free buffer flag is set.
bool qoi_enc_reset_buffer(qoi_enc_t *enc)
Resets the buffer of the QOI encoder to the default state.
void qoi_set_colorspace(qoi_desc_t *desc, uint8_t colorspace)
Sets the colorspace of an image for QOI descriptor.
QOI descriptor as read by the header of a QOI file.
uint32_t buffer_offset
The offset of the buffer to write the next encoded data to.
uint8_t * enc_buffer
The buffer to store the encoded QOI data before writing to the file.
uint32_t buffer_len
The total length of the buffer.
uint32_t len
The total length of the pixel data to encode (width * height).
uint32_t pixels_written
The total number of pixels encoded so far.