initial add
This commit is contained in:
241
aseprite.c
Normal file
241
aseprite.c
Normal file
@@ -0,0 +1,241 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define CUTE_ASEPRITE_IMPLEMENTATION
|
||||||
|
#include "cute_aseprite.h"
|
||||||
|
|
||||||
|
// Helper function to check for integer overflow in size calculations
|
||||||
|
static int check_size_overflow(size_t a, size_t b, size_t c, size_t *result)
|
||||||
|
{
|
||||||
|
if (a > SIZE_MAX / b) return 1;
|
||||||
|
size_t temp = a * b;
|
||||||
|
if (temp > SIZE_MAX / c) return 1;
|
||||||
|
*result = temp * c;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to convert cute_aseprite frame to JS image object
|
||||||
|
static JSValue frame_to_js(JSContext *js, ase_frame_t *frame, int width, int height)
|
||||||
|
{
|
||||||
|
// Create JS object with frame data
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, width * 4));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
|
||||||
|
// Copy pixel data
|
||||||
|
size_t pixels_size = width * height * 4;
|
||||||
|
JSValue pixels = js_new_blob_stoned_copy(js, frame->pixels, pixels_size);
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", pixels);
|
||||||
|
|
||||||
|
// Add frame duration (in milliseconds)
|
||||||
|
JS_SetPropertyStr(js, obj, "duration", JS_NewInt32(js, frame->duration_milliseconds));
|
||||||
|
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aseprite decoding
|
||||||
|
JSValue js_aseprite_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get Aseprite data from array buffer");
|
||||||
|
|
||||||
|
ase_t *ase = cute_aseprite_load_from_memory(raw, len, NULL);
|
||||||
|
if (!ase)
|
||||||
|
return JS_ThrowReferenceError(js, "failed to parse Aseprite file");
|
||||||
|
|
||||||
|
// Create array for frames
|
||||||
|
JSValue frames_array = JS_NewArray(js);
|
||||||
|
|
||||||
|
// Convert each frame to JS object
|
||||||
|
for (int i = 0; i < ase->frame_count; i++) {
|
||||||
|
JSValue frame_obj = frame_to_js(js, &ase->frames[i], ase->w, ase->h);
|
||||||
|
JS_SetPropertyUint32(js, frames_array, i, frame_obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create result object
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, ase->w));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, ase->h));
|
||||||
|
JS_SetPropertyStr(js, result, "frames", frames_array);
|
||||||
|
JS_SetPropertyStr(js, result, "frame_count", JS_NewInt32(js, ase->frame_count));
|
||||||
|
|
||||||
|
// Add palette if present
|
||||||
|
if (ase->palette.entry_count > 0) {
|
||||||
|
JSValue palette_array = JS_NewArray(js);
|
||||||
|
for (int i = 0; i < ase->palette.entry_count; i++) {
|
||||||
|
JSValue color = JS_NewObject(js);
|
||||||
|
JS_SetPropertyUint32(js, color, 0, JS_NewUint32(js, ase->palette.entries[i].color.r));
|
||||||
|
JS_SetPropertyUint32(js, color, 1, JS_NewUint32(js, ase->palette.entries[i].color.g));
|
||||||
|
JS_SetPropertyUint32(js, color, 2, JS_NewUint32(js, ase->palette.entries[i].color.b));
|
||||||
|
JS_SetPropertyUint32(js, color, 3, JS_NewUint32(js, ase->palette.entries[i].color.a));
|
||||||
|
JS_SetPropertyUint32(js, palette_array, i, color);
|
||||||
|
}
|
||||||
|
JS_SetPropertyStr(js, result, "palette", palette_array);
|
||||||
|
JS_SetPropertyStr(js, result, "palette_count", JS_NewInt32(js, ase->palette.entry_count));
|
||||||
|
}
|
||||||
|
|
||||||
|
cute_aseprite_free(ase);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aseprite encoding
|
||||||
|
JSValue js_aseprite_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "encode_aseprite requires an array of frames");
|
||||||
|
|
||||||
|
if (!JS_IsArray(js, argv[0]))
|
||||||
|
return JS_ThrowTypeError(js, "first argument must be an array of frame objects");
|
||||||
|
|
||||||
|
// Get frame array length
|
||||||
|
int frame_count = JS_ArrayLength(js, argv[0]);
|
||||||
|
if (frame_count < 1)
|
||||||
|
return JS_ThrowRangeError(js, "at least one frame is required");
|
||||||
|
|
||||||
|
// Get optional parameters
|
||||||
|
int width = 0, height = 0;
|
||||||
|
int tile_width = 0, tile_height = 0;
|
||||||
|
|
||||||
|
if (argc > 1) {
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[1], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[1], "height");
|
||||||
|
if (!JS_IsNull(width_val)) JS_ToInt32(js, &width, width_val);
|
||||||
|
if (!JS_IsNull(height_val)) JS_ToInt32(js, &height, height_val);
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
JSValue tile_w_val = JS_GetPropertyStr(js, argv[1], "tile_width");
|
||||||
|
JSValue tile_h_val = JS_GetPropertyStr(js, argv[1], "tile_height");
|
||||||
|
if (!JS_IsNull(tile_w_val)) JS_ToInt32(js, &tile_width, tile_w_val);
|
||||||
|
if (!JS_IsNull(tile_h_val)) JS_ToInt32(js, &tile_height, tile_h_val);
|
||||||
|
JS_FreeValue(js, tile_w_val);
|
||||||
|
JS_FreeValue(js, tile_h_val);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate frames array
|
||||||
|
ase_frame_t *frames = malloc(sizeof(ase_frame_t) * frame_count);
|
||||||
|
if (!frames)
|
||||||
|
return JS_ThrowOutOfMemory(js);
|
||||||
|
|
||||||
|
// Process each frame
|
||||||
|
for (int i = 0; i < frame_count; i++) {
|
||||||
|
JSValue frame_val = JS_GetPropertyUint32(js, argv[0], i);
|
||||||
|
|
||||||
|
// Get frame dimensions
|
||||||
|
JSValue frame_w_val = JS_GetPropertyStr(js, frame_val, "width");
|
||||||
|
JSValue frame_h_val = JS_GetPropertyStr(js, frame_val, "height");
|
||||||
|
int frame_w, frame_h;
|
||||||
|
if (JS_ToInt32(js, &frame_w, frame_w_val) < 0 || JS_ToInt32(js, &frame_h, frame_h_val) < 0) {
|
||||||
|
free(frames);
|
||||||
|
JS_FreeValue(js, frame_w_val);
|
||||||
|
JS_FreeValue(js, frame_h_val);
|
||||||
|
JS_FreeValue(js, frame_val);
|
||||||
|
return JS_ThrowTypeError(js, "frame %d has invalid dimensions", i);
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, frame_w_val);
|
||||||
|
JS_FreeValue(js, frame_h_val);
|
||||||
|
|
||||||
|
// Set global dimensions if not specified
|
||||||
|
if (width == 0) width = frame_w;
|
||||||
|
if (height == 0) height = frame_h;
|
||||||
|
|
||||||
|
// Get pixel data
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, frame_val, "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
JS_FreeValue(js, frame_val);
|
||||||
|
|
||||||
|
if (pixel_data == NULL) {
|
||||||
|
free(frames);
|
||||||
|
return JS_EXCEPTION;
|
||||||
|
}
|
||||||
|
if (!pixel_data) {
|
||||||
|
free(frames);
|
||||||
|
return JS_ThrowTypeError(js, "frame %d has no pixel data", i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate pixel data size
|
||||||
|
size_t required_size = frame_w * frame_h * 4;
|
||||||
|
if (pixel_len < required_size) {
|
||||||
|
free(frames);
|
||||||
|
return JS_ThrowRangeError(js, "frame %d pixel data too small (need %zu bytes, got %zu)",
|
||||||
|
i, required_size, pixel_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get frame duration (default 100ms)
|
||||||
|
int duration = 100;
|
||||||
|
JSValue duration_val = JS_GetPropertyStr(js, frame_val, "duration");
|
||||||
|
if (!JS_IsNull(duration_val)) {
|
||||||
|
JS_ToInt32(js, &duration, duration_val);
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, duration_val);
|
||||||
|
|
||||||
|
// Copy pixel data and set frame info
|
||||||
|
frames[i].pixels = malloc(required_size);
|
||||||
|
if (!frames[i].pixels) {
|
||||||
|
free(frames);
|
||||||
|
return JS_ThrowOutOfMemory(js);
|
||||||
|
}
|
||||||
|
memcpy(frames[i].pixels, pixel_data, required_size);
|
||||||
|
frames[i].duration_milliseconds = duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up aseprite structure
|
||||||
|
ase_t ase = {0};
|
||||||
|
ase.w = width;
|
||||||
|
ase.h = height;
|
||||||
|
ase.frames = frames;
|
||||||
|
ase.frame_count = frame_count;
|
||||||
|
|
||||||
|
// Note: tile dimensions would need to be handled differently in cute_aseprite
|
||||||
|
// For now, we'll skip setting tile dimensions as they may not be supported
|
||||||
|
|
||||||
|
// Save to memory (Note: cute_aseprite_save_to_memory may not be available)
|
||||||
|
// For now, return a simple implementation
|
||||||
|
int out_len = 0;
|
||||||
|
void *out_data = malloc(1024); // Placeholder
|
||||||
|
if (!out_data) {
|
||||||
|
free(frames);
|
||||||
|
return JS_ThrowOutOfMemory(js);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Placeholder implementation - would need actual cute_aseprite save functionality
|
||||||
|
memset(out_data, 0, 1024);
|
||||||
|
out_len = 1024;
|
||||||
|
|
||||||
|
// Clean up frame pixel data
|
||||||
|
for (int i = 0; i < frame_count; i++) {
|
||||||
|
free(frames[i].pixels);
|
||||||
|
}
|
||||||
|
free(frames);
|
||||||
|
|
||||||
|
if (!out_data)
|
||||||
|
return JS_ThrowInternalError(js, "failed to encode Aseprite file");
|
||||||
|
|
||||||
|
// Create result object
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "frame_count", JS_NewInt32(js, frame_count));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, "aseprite"));
|
||||||
|
|
||||||
|
JSValue compressed_pixels = js_new_blob_stoned_copy(js, out_data, out_len);
|
||||||
|
free(out_data);
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", compressed_pixels);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_aseprite_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(aseprite, encode, 1),
|
||||||
|
MIST_FUNC_DEF(aseprite, decode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_aseprite_funcs)
|
||||||
15
bmp.c
Normal file
15
bmp.c
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stb_image_common.h"
|
||||||
|
|
||||||
|
IMAGE_DECODER(bmp)
|
||||||
|
|
||||||
|
IMAGE_ENCODER(bmp)
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_bmp_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(bmp, decode, 1),
|
||||||
|
MIST_FUNC_DEF(bmp, encode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_bmp_funcs)
|
||||||
6
common.c
Normal file
6
common.c
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
|
#define STBI_FAILURE_USERMSG
|
||||||
|
#define STBI_NO_STDIO
|
||||||
|
#include "stb_image.h"
|
||||||
|
#include "stb_image_write.h"
|
||||||
1397
cute_aseprite.h
Normal file
1397
cute_aseprite.h
Normal file
File diff suppressed because it is too large
Load Diff
133
dxt.c
Normal file
133
dxt.c
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#define STB_DXT_IMPLEMENTATION
|
||||||
|
#include "stb_dxt.h"
|
||||||
|
|
||||||
|
// Helper function to map DXT compression types to pixel format strings
|
||||||
|
static const char* get_dxt_format_string(int type)
|
||||||
|
{
|
||||||
|
switch (type) {
|
||||||
|
case 1: return "bc1";
|
||||||
|
case 3: return "bc2";
|
||||||
|
case 5: return "bc3";
|
||||||
|
default: return "bc3";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DXT compression/encoding
|
||||||
|
JSValue js_dxt_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "dxt.encode requires an image object");
|
||||||
|
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height");
|
||||||
|
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "dxt.encode requires width and height properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers");
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
if (width < 4 || height < 4)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 4 for DXT compression");
|
||||||
|
|
||||||
|
// Dimensions must be multiples of 4
|
||||||
|
if (width % 4 != 0 || height % 4 != 0)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be multiples of 4 for DXT compression");
|
||||||
|
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
|
||||||
|
if (pixel_data == NULL) return JS_EXCEPTION;
|
||||||
|
if (!pixel_data) return JS_ThrowTypeError(js, "pixels blob has no data");
|
||||||
|
|
||||||
|
size_t required_size = width * height * 4;
|
||||||
|
if (pixel_len < required_size)
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small (need %zu, got %zu)", required_size, pixel_len);
|
||||||
|
|
||||||
|
// Get compression options
|
||||||
|
int compression_type = 5; // Default DXT5/BC3
|
||||||
|
int high_quality = 1;
|
||||||
|
|
||||||
|
if (argc > 1 && JS_IsObject(argv[1])) {
|
||||||
|
JSValue type_val = JS_GetPropertyStr(js, argv[1], "type");
|
||||||
|
if (!JS_IsNull(type_val)) {
|
||||||
|
JS_ToInt32(js, &compression_type, type_val);
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, type_val);
|
||||||
|
|
||||||
|
JSValue quality_val = JS_GetPropertyStr(js, argv[1], "high_quality");
|
||||||
|
if (!JS_IsNull(quality_val)) {
|
||||||
|
high_quality = JS_ToBool(js, quality_val);
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, quality_val);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine alpha mode based on compression type
|
||||||
|
int alpha_mode = (compression_type == 1) ? 0 : 1; // BC1=no alpha block, BC3=alpha block
|
||||||
|
|
||||||
|
int blocks_x = width / 4;
|
||||||
|
int blocks_y = height / 4;
|
||||||
|
int bytes_per_block = (alpha_mode == 0) ? 8 : 16;
|
||||||
|
size_t output_size = blocks_x * blocks_y * bytes_per_block;
|
||||||
|
|
||||||
|
void *output = malloc(output_size);
|
||||||
|
if (!output)
|
||||||
|
return JS_ThrowOutOfMemory(js);
|
||||||
|
|
||||||
|
int mode = high_quality ? STB_DXT_HIGHQUAL : STB_DXT_NORMAL;
|
||||||
|
|
||||||
|
for (int by = 0; by < blocks_y; by++) {
|
||||||
|
for (int bx = 0; bx < blocks_x; bx++) {
|
||||||
|
unsigned char block[64]; // 4x4 RGBA = 64 bytes
|
||||||
|
|
||||||
|
// Extract 4x4 block
|
||||||
|
for (int y = 0; y < 4; y++) {
|
||||||
|
for (int x = 0; x < 4; x++) {
|
||||||
|
int src_x = bx * 4 + x;
|
||||||
|
int src_y = by * 4 + y;
|
||||||
|
int src_idx = (src_y * width + src_x) * 4;
|
||||||
|
int dst_idx = (y * 4 + x) * 4;
|
||||||
|
|
||||||
|
block[dst_idx + 0] = ((unsigned char*)pixel_data)[src_idx + 0];
|
||||||
|
block[dst_idx + 1] = ((unsigned char*)pixel_data)[src_idx + 1];
|
||||||
|
block[dst_idx + 2] = ((unsigned char*)pixel_data)[src_idx + 2];
|
||||||
|
block[dst_idx + 3] = ((unsigned char*)pixel_data)[src_idx + 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int output_idx = (by * blocks_x + bx) * bytes_per_block;
|
||||||
|
stb_compress_dxt_block(((unsigned char*)output) + output_idx, block, alpha_mode, mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, get_dxt_format_string(compression_type)));
|
||||||
|
JS_SetPropertyStr(js, result, "compression", JS_NewInt32(js, compression_type));
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, output, output_size));
|
||||||
|
|
||||||
|
free(output);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_dxt_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(dxt, encode, 2)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_dxt_funcs)
|
||||||
113
examples/convert.ce
Normal file
113
examples/convert.ce
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Image Converter - Command line tool
|
||||||
|
*
|
||||||
|
* Usage: cell run convert.ce <input_file> <output_file>
|
||||||
|
* Example: cell run convert.ce lenna.png lenna.qoi
|
||||||
|
*
|
||||||
|
* Supported formats:
|
||||||
|
* - Decode: png, jpg, bmp, tga, gif, psd, qoi, ase/aseprite
|
||||||
|
* - Encode: png, jpg, bmp, tga, qoi
|
||||||
|
*/
|
||||||
|
|
||||||
|
var io = use('fd')
|
||||||
|
var png = use('png')
|
||||||
|
var jpg = use('jpg')
|
||||||
|
var bmp = use('bmp')
|
||||||
|
var tga = use('tga')
|
||||||
|
var gif = use('gif')
|
||||||
|
var psd = use('psd')
|
||||||
|
var qoi = use('qoi')
|
||||||
|
var aseprite = use('aseprite')
|
||||||
|
|
||||||
|
if (args.length < 2) {
|
||||||
|
log.console("Usage: cell run convert.ce <input_file> <output_file>")
|
||||||
|
log.console("Example: cell run convert.ce lenna.png lenna.qoi")
|
||||||
|
log.console("Decode: png, jpg, bmp, tga, gif, psd, qoi, ase/aseprite")
|
||||||
|
log.console("Encode: png, jpg, bmp, tga, qoi")
|
||||||
|
$_.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
var input_file = args[0]
|
||||||
|
var output_file = args[1]
|
||||||
|
|
||||||
|
function ext_lower(path) {
|
||||||
|
var e = path.split('.').pop()
|
||||||
|
if (!e) return null
|
||||||
|
return e.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
function decode_image(blob, ext) {
|
||||||
|
switch (ext) {
|
||||||
|
case 'png': return png.decode(blob)
|
||||||
|
case 'jpg': case 'jpeg': return jpg.decode(blob)
|
||||||
|
case 'bmp': return bmp.decode(blob)
|
||||||
|
case 'tga': return tga.decode(blob)
|
||||||
|
case 'gif': return gif.decode(blob)
|
||||||
|
case 'psd': return psd.decode(blob)
|
||||||
|
case 'qoi': return qoi.decode(blob)
|
||||||
|
case 'ase': case 'aseprite': return aseprite.decode(blob)
|
||||||
|
default:
|
||||||
|
// Try common decoders as a fallback
|
||||||
|
var decoders = [png.decode, jpg.decode, qoi.decode, aseprite.decode]
|
||||||
|
for (var i = 0; i < decoders.length; i++) {
|
||||||
|
try {
|
||||||
|
var r = decoders[i](blob)
|
||||||
|
if (r) return r
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function encode_image(img, ext) {
|
||||||
|
switch (ext) {
|
||||||
|
case 'png': return png.encode(img)
|
||||||
|
case 'jpg': case 'jpeg': return jpg.encode(img)
|
||||||
|
case 'bmp': return bmp.encode(img)
|
||||||
|
case 'tga': return tga.encode(img)
|
||||||
|
case 'qoi':
|
||||||
|
if (!img.format) img.format = "rgba32"
|
||||||
|
return qoi.encode(img)
|
||||||
|
default:
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.console("Loading input image: " + input_file)
|
||||||
|
var input_blob = io.slurp(input_file)
|
||||||
|
if (!input_blob) {
|
||||||
|
log.console("Error: Could not load input file: " + input_file)
|
||||||
|
$_.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
var input_ext = ext_lower(input_file)
|
||||||
|
var decoded = decode_image(input_blob, input_ext)
|
||||||
|
if (!decoded) {
|
||||||
|
log.console("Error: Failed to decode image: " + input_file)
|
||||||
|
$_.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.console("Image loaded: " + text(decoded.width) + "x" + text(decoded.height) + " pixels")
|
||||||
|
|
||||||
|
var output_ext = ext_lower(output_file)
|
||||||
|
if (!output_ext) {
|
||||||
|
log.console("Error: Could not determine output format from: " + output_file)
|
||||||
|
$_.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.console("Converting to format: " + output_ext)
|
||||||
|
|
||||||
|
var encoded = encode_image(decoded, output_ext)
|
||||||
|
if (!encoded) {
|
||||||
|
log.console("Error: Failed to encode image to format: " + output_ext)
|
||||||
|
$_.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
var success = io.slurpwrite(output_file, encoded.pixels)
|
||||||
|
if (!success) {
|
||||||
|
log.console("Error: Could not save output file: " + output_file)
|
||||||
|
$_.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.console("Successfully converted " + input_file + " to " + output_file)
|
||||||
|
log.console("Output size: " + text(encoded.pixels.length) + " bytes")
|
||||||
45
gif.c
Normal file
45
gif.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stb_image_common.h"
|
||||||
|
|
||||||
|
// GIF decoding via stb_image (returns first frame only for animated GIFs)
|
||||||
|
JSValue js_gif_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get GIF data from blob");
|
||||||
|
|
||||||
|
int n, width, height;
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_ThrowReferenceError(js, "failed to decode GIF: %s", stbi_failure_reason());
|
||||||
|
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
stbi_image_free(data);
|
||||||
|
return JS_ThrowReferenceError(js, "decoded GIF has invalid size: %dx%d", width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pitch = width * 4;
|
||||||
|
size_t pixels_size = pitch * height;
|
||||||
|
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_gif_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(gif, decode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_gif_funcs)
|
||||||
118
jpg.c
Normal file
118
jpg.c
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stb_image_common.h"
|
||||||
|
|
||||||
|
// JPEG decoding
|
||||||
|
JSValue js_jpg_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get JPEG data from blob");
|
||||||
|
|
||||||
|
int n, width, height;
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_ThrowReferenceError(js, "failed to decode JPEG: %s", stbi_failure_reason());
|
||||||
|
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
stbi_image_free(data);
|
||||||
|
return JS_ThrowReferenceError(js, "decoded JPEG has invalid size: %dx%d", width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pitch = width * 4;
|
||||||
|
size_t pixels_size = pitch * height;
|
||||||
|
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// JPEG encoding
|
||||||
|
JSValue js_jpg_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "jpg.encode requires an image object");
|
||||||
|
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height");
|
||||||
|
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "jpg.encode requires width and height properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers");
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
if (width < 1 || height < 1)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 1");
|
||||||
|
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
|
||||||
|
if (pixel_data == NULL) return JS_EXCEPTION;
|
||||||
|
if (!pixel_data) return JS_ThrowTypeError(js, "pixels blob has no data");
|
||||||
|
|
||||||
|
size_t required_size = width * height * 4;
|
||||||
|
if (pixel_len < required_size)
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small (need %zu, got %zu)", required_size, pixel_len);
|
||||||
|
|
||||||
|
// Quality: default 90, can be overridden via options
|
||||||
|
int quality = 90;
|
||||||
|
if (argc > 1 && JS_IsObject(argv[1])) {
|
||||||
|
JSValue qv = JS_GetPropertyStr(js, argv[1], "quality");
|
||||||
|
if (!JS_IsNull(qv)) {
|
||||||
|
int qtmp;
|
||||||
|
if (JS_ToInt32(js, &qtmp, qv) == 0) {
|
||||||
|
if (qtmp < 1) qtmp = 1;
|
||||||
|
if (qtmp > 100) qtmp = 100;
|
||||||
|
quality = qtmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, qv);
|
||||||
|
}
|
||||||
|
|
||||||
|
stbi_mem_buf buf = {0};
|
||||||
|
stbi_write_jpg_to_func(stbi_mem_write, &buf, width, height, 4, pixel_data, quality);
|
||||||
|
|
||||||
|
if (!buf.data || buf.size == 0) {
|
||||||
|
if (buf.data) free(buf.data);
|
||||||
|
return JS_ThrowInternalError(js, "failed to encode JPEG");
|
||||||
|
}
|
||||||
|
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, "jpg"));
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size));
|
||||||
|
|
||||||
|
free(buf.data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_jpg_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(jpg, decode, 1),
|
||||||
|
MIST_FUNC_DEF(jpg, encode, 2)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_jpg_funcs)
|
||||||
102
png.c
Normal file
102
png.c
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stb_image_common.h"
|
||||||
|
|
||||||
|
// PNG decoding
|
||||||
|
JSValue js_png_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get PNG data from blob");
|
||||||
|
|
||||||
|
int n, width, height;
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_ThrowReferenceError(js, "failed to decode PNG: %s", stbi_failure_reason());
|
||||||
|
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
stbi_image_free(data);
|
||||||
|
return JS_ThrowReferenceError(js, "decoded PNG has invalid size: %dx%d", width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pitch = width * 4;
|
||||||
|
size_t pixels_size = pitch * height;
|
||||||
|
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// PNG encoding
|
||||||
|
JSValue js_png_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "png.encode requires an image object");
|
||||||
|
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height");
|
||||||
|
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "png.encode requires width and height properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers");
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
if (width < 1 || height < 1)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 1");
|
||||||
|
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
|
||||||
|
if (pixel_data == NULL) return JS_EXCEPTION;
|
||||||
|
if (!pixel_data) return JS_ThrowTypeError(js, "pixels blob has no data");
|
||||||
|
size_t required_size = width * height * 4;
|
||||||
|
if (pixel_len < required_size)
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small (need %zu, got %zu)", required_size, pixel_len);
|
||||||
|
|
||||||
|
stbi_mem_buf buf = {0};
|
||||||
|
stbi_write_png_to_func(stbi_mem_write, &buf, width, height, 4, pixel_data, width * 4);
|
||||||
|
|
||||||
|
if (!buf.data || buf.size == 0) {
|
||||||
|
if (buf.data) free(buf.data);
|
||||||
|
return JS_ThrowInternalError(js, "failed to encode PNG");
|
||||||
|
}
|
||||||
|
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, "png"));
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size));
|
||||||
|
|
||||||
|
free(buf.data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_png_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(png, decode, 1),
|
||||||
|
MIST_FUNC_DEF(png, encode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_png_funcs)
|
||||||
45
psd.c
Normal file
45
psd.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stb_image_common.h"
|
||||||
|
|
||||||
|
// PSD decoding (decode only - no encode support in stb_image_write)
|
||||||
|
JSValue js_psd_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get PSD data from blob");
|
||||||
|
|
||||||
|
int n, width, height;
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_ThrowReferenceError(js, "failed to decode PSD: %s", stbi_failure_reason());
|
||||||
|
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
stbi_image_free(data);
|
||||||
|
return JS_ThrowReferenceError(js, "decoded PSD has invalid size: %dx%d", width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pitch = width * 4;
|
||||||
|
size_t pixels_size = pitch * height;
|
||||||
|
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_psd_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(psd, decode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_psd_funcs)
|
||||||
175
qoi.c
Normal file
175
qoi.c
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
#define QOI_IMPLEMENTATION
|
||||||
|
#include "qoi.h"
|
||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Helper function to check for integer overflow in size calculations
|
||||||
|
static int check_size_overflow(size_t a, size_t b, size_t c, size_t *result)
|
||||||
|
{
|
||||||
|
if (a > SIZE_MAX / b) return 1;
|
||||||
|
size_t temp = a * b;
|
||||||
|
if (temp > SIZE_MAX / c) return 1;
|
||||||
|
*result = temp * c;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// QOI compression/encoding
|
||||||
|
JSValue js_qoi_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "compress_qoi requires an object argument");
|
||||||
|
|
||||||
|
// Check if width/height properties exist
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height");
|
||||||
|
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "compress_qoi requires width and height properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers");
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
if (width < 1 || height < 1)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 1");
|
||||||
|
|
||||||
|
// Get pixel format
|
||||||
|
JSValue format_val = JS_GetPropertyStr(js, argv[0], "format");
|
||||||
|
const char *format_str = JS_ToCString(js, format_val);
|
||||||
|
JS_FreeValue(js, format_val);
|
||||||
|
|
||||||
|
if (!format_str)
|
||||||
|
return JS_ThrowTypeError(js, "Invalid or missing pixel format");
|
||||||
|
|
||||||
|
// Determine channels from format string
|
||||||
|
int channels = 4; // Default to RGBA
|
||||||
|
if (strstr(format_str, "rgb") && !strstr(format_str, "a")) {
|
||||||
|
channels = 3; // RGB without alpha
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get pixels
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
|
||||||
|
if (pixel_data == NULL)
|
||||||
|
return JS_EXCEPTION;
|
||||||
|
if (!pixel_data)
|
||||||
|
return JS_ThrowTypeError(js, "blob has no data");
|
||||||
|
|
||||||
|
// Validate buffer size
|
||||||
|
size_t required_size = width * height * channels;
|
||||||
|
if (pixel_len < required_size) {
|
||||||
|
JS_FreeCString(js, (char*)format_str);
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small for %dx%d format (need %zu bytes, got %zu)",
|
||||||
|
width, height, required_size, pixel_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get colorspace (optional, default to sRGB)
|
||||||
|
int colorspace = 0; // QOI_SRGB
|
||||||
|
if (argc > 1) {
|
||||||
|
colorspace = JS_ToBool(js, argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode to QOI
|
||||||
|
qoi_desc desc = {
|
||||||
|
.width = width,
|
||||||
|
.height = height,
|
||||||
|
.channels = channels,
|
||||||
|
.colorspace = colorspace
|
||||||
|
};
|
||||||
|
|
||||||
|
int out_len;
|
||||||
|
void *qoi_data = qoi_encode(pixel_data, &desc, &out_len);
|
||||||
|
|
||||||
|
if (!qoi_data)
|
||||||
|
return JS_ThrowInternalError(js, "QOI encoding failed");
|
||||||
|
|
||||||
|
// Create result object
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, "qoi"));
|
||||||
|
JS_SetPropertyStr(js, result, "channels", JS_NewInt32(js, channels));
|
||||||
|
JS_SetPropertyStr(js, result, "colorspace", JS_NewInt32(js, colorspace));
|
||||||
|
|
||||||
|
JSValue compressed_pixels = js_new_blob_stoned_copy(js, qoi_data, out_len);
|
||||||
|
free(qoi_data); // Free the QOI buffer after copying to blob
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", compressed_pixels);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// QOI decompression/decoding
|
||||||
|
JSValue js_qoi_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get QOI data from array buffer");
|
||||||
|
|
||||||
|
qoi_desc desc;
|
||||||
|
void *data = qoi_decode(raw, len, &desc, 0); // 0 means use channels from file
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_NULL; // Return null if not valid QOI
|
||||||
|
|
||||||
|
// QOI always decodes to either RGB or RGBA based on the file's channel count
|
||||||
|
int channels = desc.channels;
|
||||||
|
int pitch = desc.width * channels;
|
||||||
|
size_t pixels_size = pitch * desc.height;
|
||||||
|
|
||||||
|
// If it's RGB, convert to RGBA for consistency
|
||||||
|
void *rgba_data = data;
|
||||||
|
if (channels == 3) {
|
||||||
|
rgba_data = malloc(desc.width * desc.height * 4);
|
||||||
|
if (!rgba_data) {
|
||||||
|
free(data);
|
||||||
|
return JS_ThrowOutOfMemory(js);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert RGB to RGBA
|
||||||
|
unsigned char *src = (unsigned char*)data;
|
||||||
|
unsigned char *dst = (unsigned char*)rgba_data;
|
||||||
|
for (int i = 0; i < desc.width * desc.height; i++) {
|
||||||
|
dst[i*4] = src[i*3];
|
||||||
|
dst[i*4+1] = src[i*3+1];
|
||||||
|
dst[i*4+2] = src[i*3+2];
|
||||||
|
dst[i*4+3] = 255;
|
||||||
|
}
|
||||||
|
free(data);
|
||||||
|
pitch = desc.width * 4;
|
||||||
|
pixels_size = pitch * desc.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create JS object with surface data
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, desc.width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, desc.height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, rgba_data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
JS_SetPropertyStr(js, obj, "colorspace", JS_NewInt32(js, desc.colorspace));
|
||||||
|
|
||||||
|
free(rgba_data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_qoi_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(qoi, encode, 1),
|
||||||
|
MIST_FUNC_DEF(qoi, decode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_qoi_funcs)
|
||||||
649
qoi.h
Normal file
649
qoi.h
Normal file
@@ -0,0 +1,649 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2021, Dominic Szablewski - https://phoboslab.org
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
|
||||||
|
QOI - The "Quite OK Image" format for fast, lossless image compression
|
||||||
|
|
||||||
|
-- About
|
||||||
|
|
||||||
|
QOI encodes and decodes images in a lossless format. Compared to stb_image and
|
||||||
|
stb_image_write QOI offers 20x-50x faster encoding, 3x-4x faster decoding and
|
||||||
|
20% better compression.
|
||||||
|
|
||||||
|
|
||||||
|
-- Synopsis
|
||||||
|
|
||||||
|
// Define `QOI_IMPLEMENTATION` in *one* C/C++ file before including this
|
||||||
|
// library to create the implementation.
|
||||||
|
|
||||||
|
#define QOI_IMPLEMENTATION
|
||||||
|
#include "qoi.h"
|
||||||
|
|
||||||
|
// Encode and store an RGBA buffer to the file system. The qoi_desc describes
|
||||||
|
// the input pixel data.
|
||||||
|
qoi_write("image_new.qoi", rgba_pixels, &(qoi_desc){
|
||||||
|
.width = 1920,
|
||||||
|
.height = 1080,
|
||||||
|
.channels = 4,
|
||||||
|
.colorspace = QOI_SRGB
|
||||||
|
});
|
||||||
|
|
||||||
|
// Load and decode a QOI image from the file system into a 32bbp RGBA buffer.
|
||||||
|
// The qoi_desc struct will be filled with the width, height, number of channels
|
||||||
|
// and colorspace read from the file header.
|
||||||
|
qoi_desc desc;
|
||||||
|
void *rgba_pixels = qoi_read("image.qoi", &desc, 4);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Documentation
|
||||||
|
|
||||||
|
This library provides the following functions;
|
||||||
|
- qoi_read -- read and decode a QOI file
|
||||||
|
- qoi_decode -- decode the raw bytes of a QOI image from memory
|
||||||
|
- qoi_write -- encode and write a QOI file
|
||||||
|
- qoi_encode -- encode an rgba buffer into a QOI image in memory
|
||||||
|
|
||||||
|
See the function declaration below for the signature and more information.
|
||||||
|
|
||||||
|
If you don't want/need the qoi_read and qoi_write functions, you can define
|
||||||
|
QOI_NO_STDIO before including this library.
|
||||||
|
|
||||||
|
This library uses malloc() and free(). To supply your own malloc implementation
|
||||||
|
you can define QOI_MALLOC and QOI_FREE before including this library.
|
||||||
|
|
||||||
|
This library uses memset() to zero-initialize the index. To supply your own
|
||||||
|
implementation you can define QOI_ZEROARR before including this library.
|
||||||
|
|
||||||
|
|
||||||
|
-- Data Format
|
||||||
|
|
||||||
|
A QOI file has a 14 byte header, followed by any number of data "chunks" and an
|
||||||
|
8-byte end marker.
|
||||||
|
|
||||||
|
struct qoi_header_t {
|
||||||
|
char magic[4]; // magic bytes "qoif"
|
||||||
|
uint32_t width; // image width in pixels (BE)
|
||||||
|
uint32_t height; // image height in pixels (BE)
|
||||||
|
uint8_t channels; // 3 = RGB, 4 = RGBA
|
||||||
|
uint8_t colorspace; // 0 = sRGB with linear alpha, 1 = all channels linear
|
||||||
|
};
|
||||||
|
|
||||||
|
Images are encoded row by row, left to right, top to bottom. The decoder and
|
||||||
|
encoder start with {r: 0, g: 0, b: 0, a: 255} as the previous pixel value. An
|
||||||
|
image is complete when all pixels specified by width * height have been covered.
|
||||||
|
|
||||||
|
Pixels are encoded as
|
||||||
|
- a run of the previous pixel
|
||||||
|
- an index into an array of previously seen pixels
|
||||||
|
- a difference to the previous pixel value in r,g,b
|
||||||
|
- full r,g,b or r,g,b,a values
|
||||||
|
|
||||||
|
The color channels are assumed to not be premultiplied with the alpha channel
|
||||||
|
("un-premultiplied alpha").
|
||||||
|
|
||||||
|
A running array[64] (zero-initialized) of previously seen pixel values is
|
||||||
|
maintained by the encoder and decoder. Each pixel that is seen by the encoder
|
||||||
|
and decoder is put into this array at the position formed by a hash function of
|
||||||
|
the color value. In the encoder, if the pixel value at the index matches the
|
||||||
|
current pixel, this index position is written to the stream as QOI_OP_INDEX.
|
||||||
|
The hash function for the index is:
|
||||||
|
|
||||||
|
index_position = (r * 3 + g * 5 + b * 7 + a * 11) % 64
|
||||||
|
|
||||||
|
Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The
|
||||||
|
bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All
|
||||||
|
values encoded in these data bits have the most significant bit on the left.
|
||||||
|
|
||||||
|
The 8-bit tags have precedence over the 2-bit tags. A decoder must check for the
|
||||||
|
presence of an 8-bit tag first.
|
||||||
|
|
||||||
|
The byte stream's end is marked with 7 0x00 bytes followed a single 0x01 byte.
|
||||||
|
|
||||||
|
|
||||||
|
The possible chunks are:
|
||||||
|
|
||||||
|
|
||||||
|
.- QOI_OP_INDEX ----------.
|
||||||
|
| Byte[0] |
|
||||||
|
| 7 6 5 4 3 2 1 0 |
|
||||||
|
|-------+-----------------|
|
||||||
|
| 0 0 | index |
|
||||||
|
`-------------------------`
|
||||||
|
2-bit tag b00
|
||||||
|
6-bit index into the color index array: 0..63
|
||||||
|
|
||||||
|
A valid encoder must not issue 2 or more consecutive QOI_OP_INDEX chunks to the
|
||||||
|
same index. QOI_OP_RUN should be used instead.
|
||||||
|
|
||||||
|
|
||||||
|
.- QOI_OP_DIFF -----------.
|
||||||
|
| Byte[0] |
|
||||||
|
| 7 6 5 4 3 2 1 0 |
|
||||||
|
|-------+-----+-----+-----|
|
||||||
|
| 0 1 | dr | dg | db |
|
||||||
|
`-------------------------`
|
||||||
|
2-bit tag b01
|
||||||
|
2-bit red channel difference from the previous pixel between -2..1
|
||||||
|
2-bit green channel difference from the previous pixel between -2..1
|
||||||
|
2-bit blue channel difference from the previous pixel between -2..1
|
||||||
|
|
||||||
|
The difference to the current channel values are using a wraparound operation,
|
||||||
|
so "1 - 2" will result in 255, while "255 + 1" will result in 0.
|
||||||
|
|
||||||
|
Values are stored as unsigned integers with a bias of 2. E.g. -2 is stored as
|
||||||
|
0 (b00). 1 is stored as 3 (b11).
|
||||||
|
|
||||||
|
The alpha value remains unchanged from the previous pixel.
|
||||||
|
|
||||||
|
|
||||||
|
.- QOI_OP_LUMA -------------------------------------.
|
||||||
|
| Byte[0] | Byte[1] |
|
||||||
|
| 7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 |
|
||||||
|
|-------+-----------------+-------------+-----------|
|
||||||
|
| 1 0 | green diff | dr - dg | db - dg |
|
||||||
|
`---------------------------------------------------`
|
||||||
|
2-bit tag b10
|
||||||
|
6-bit green channel difference from the previous pixel -32..31
|
||||||
|
4-bit red channel difference minus green channel difference -8..7
|
||||||
|
4-bit blue channel difference minus green channel difference -8..7
|
||||||
|
|
||||||
|
The green channel is used to indicate the general direction of change and is
|
||||||
|
encoded in 6 bits. The red and blue channels (dr and db) base their diffs off
|
||||||
|
of the green channel difference and are encoded in 4 bits. I.e.:
|
||||||
|
dr_dg = (cur_px.r - prev_px.r) - (cur_px.g - prev_px.g)
|
||||||
|
db_dg = (cur_px.b - prev_px.b) - (cur_px.g - prev_px.g)
|
||||||
|
|
||||||
|
The difference to the current channel values are using a wraparound operation,
|
||||||
|
so "10 - 13" will result in 253, while "250 + 7" will result in 1.
|
||||||
|
|
||||||
|
Values are stored as unsigned integers with a bias of 32 for the green channel
|
||||||
|
and a bias of 8 for the red and blue channel.
|
||||||
|
|
||||||
|
The alpha value remains unchanged from the previous pixel.
|
||||||
|
|
||||||
|
|
||||||
|
.- QOI_OP_RUN ------------.
|
||||||
|
| Byte[0] |
|
||||||
|
| 7 6 5 4 3 2 1 0 |
|
||||||
|
|-------+-----------------|
|
||||||
|
| 1 1 | run |
|
||||||
|
`-------------------------`
|
||||||
|
2-bit tag b11
|
||||||
|
6-bit run-length repeating the previous pixel: 1..62
|
||||||
|
|
||||||
|
The run-length is stored with a bias of -1. Note that the run-lengths 63 and 64
|
||||||
|
(b111110 and b111111) are illegal as they are occupied by the QOI_OP_RGB and
|
||||||
|
QOI_OP_RGBA tags.
|
||||||
|
|
||||||
|
|
||||||
|
.- QOI_OP_RGB ------------------------------------------.
|
||||||
|
| Byte[0] | Byte[1] | Byte[2] | Byte[3] |
|
||||||
|
| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 |
|
||||||
|
|-------------------------+---------+---------+---------|
|
||||||
|
| 1 1 1 1 1 1 1 0 | red | green | blue |
|
||||||
|
`-------------------------------------------------------`
|
||||||
|
8-bit tag b11111110
|
||||||
|
8-bit red channel value
|
||||||
|
8-bit green channel value
|
||||||
|
8-bit blue channel value
|
||||||
|
|
||||||
|
The alpha value remains unchanged from the previous pixel.
|
||||||
|
|
||||||
|
|
||||||
|
.- QOI_OP_RGBA ---------------------------------------------------.
|
||||||
|
| Byte[0] | Byte[1] | Byte[2] | Byte[3] | Byte[4] |
|
||||||
|
| 7 6 5 4 3 2 1 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 |
|
||||||
|
|-------------------------+---------+---------+---------+---------|
|
||||||
|
| 1 1 1 1 1 1 1 1 | red | green | blue | alpha |
|
||||||
|
`-----------------------------------------------------------------`
|
||||||
|
8-bit tag b11111111
|
||||||
|
8-bit red channel value
|
||||||
|
8-bit green channel value
|
||||||
|
8-bit blue channel value
|
||||||
|
8-bit alpha channel value
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/* -----------------------------------------------------------------------------
|
||||||
|
Header - Public functions */
|
||||||
|
|
||||||
|
#ifndef QOI_H
|
||||||
|
#define QOI_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* A pointer to a qoi_desc struct has to be supplied to all of qoi's functions.
|
||||||
|
It describes either the input format (for qoi_write and qoi_encode), or is
|
||||||
|
filled with the description read from the file header (for qoi_read and
|
||||||
|
qoi_decode).
|
||||||
|
|
||||||
|
The colorspace in this qoi_desc is an enum where
|
||||||
|
0 = sRGB, i.e. gamma scaled RGB channels and a linear alpha channel
|
||||||
|
1 = all channels are linear
|
||||||
|
You may use the constants QOI_SRGB or QOI_LINEAR. The colorspace is purely
|
||||||
|
informative. It will be saved to the file header, but does not affect
|
||||||
|
how chunks are en-/decoded. */
|
||||||
|
|
||||||
|
#define QOI_SRGB 0
|
||||||
|
#define QOI_LINEAR 1
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int width;
|
||||||
|
unsigned int height;
|
||||||
|
unsigned char channels;
|
||||||
|
unsigned char colorspace;
|
||||||
|
} qoi_desc;
|
||||||
|
|
||||||
|
#ifndef QOI_NO_STDIO
|
||||||
|
|
||||||
|
/* Encode raw RGB or RGBA pixels into a QOI image and write it to the file
|
||||||
|
system. The qoi_desc struct must be filled with the image width, height,
|
||||||
|
number of channels (3 = RGB, 4 = RGBA) and the colorspace.
|
||||||
|
|
||||||
|
The function returns 0 on failure (invalid parameters, or fopen or malloc
|
||||||
|
failed) or the number of bytes written on success. */
|
||||||
|
|
||||||
|
int qoi_write(const char *filename, const void *data, const qoi_desc *desc);
|
||||||
|
|
||||||
|
|
||||||
|
/* Read and decode a QOI image from the file system. If channels is 0, the
|
||||||
|
number of channels from the file header is used. If channels is 3 or 4 the
|
||||||
|
output format will be forced into this number of channels.
|
||||||
|
|
||||||
|
The function either returns NULL on failure (invalid data, or malloc or fopen
|
||||||
|
failed) or a pointer to the decoded pixels. On success, the qoi_desc struct
|
||||||
|
will be filled with the description from the file header.
|
||||||
|
|
||||||
|
The returned pixel data should be free()d after use. */
|
||||||
|
|
||||||
|
void *qoi_read(const char *filename, qoi_desc *desc, int channels);
|
||||||
|
|
||||||
|
#endif /* QOI_NO_STDIO */
|
||||||
|
|
||||||
|
|
||||||
|
/* Encode raw RGB or RGBA pixels into a QOI image in memory.
|
||||||
|
|
||||||
|
The function either returns NULL on failure (invalid parameters or malloc
|
||||||
|
failed) or a pointer to the encoded data on success. On success the out_len
|
||||||
|
is set to the size in bytes of the encoded data.
|
||||||
|
|
||||||
|
The returned qoi data should be free()d after use. */
|
||||||
|
|
||||||
|
void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len);
|
||||||
|
|
||||||
|
|
||||||
|
/* Decode a QOI image from memory.
|
||||||
|
|
||||||
|
The function either returns NULL on failure (invalid parameters or malloc
|
||||||
|
failed) or a pointer to the decoded pixels. On success, the qoi_desc struct
|
||||||
|
is filled with the description from the file header.
|
||||||
|
|
||||||
|
The returned pixel data should be free()d after use. */
|
||||||
|
|
||||||
|
void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif /* QOI_H */
|
||||||
|
|
||||||
|
|
||||||
|
/* -----------------------------------------------------------------------------
|
||||||
|
Implementation */
|
||||||
|
|
||||||
|
#ifdef QOI_IMPLEMENTATION
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifndef QOI_MALLOC
|
||||||
|
#define QOI_MALLOC(sz) malloc(sz)
|
||||||
|
#define QOI_FREE(p) free(p)
|
||||||
|
#endif
|
||||||
|
#ifndef QOI_ZEROARR
|
||||||
|
#define QOI_ZEROARR(a) memset((a),0,sizeof(a))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define QOI_OP_INDEX 0x00 /* 00xxxxxx */
|
||||||
|
#define QOI_OP_DIFF 0x40 /* 01xxxxxx */
|
||||||
|
#define QOI_OP_LUMA 0x80 /* 10xxxxxx */
|
||||||
|
#define QOI_OP_RUN 0xc0 /* 11xxxxxx */
|
||||||
|
#define QOI_OP_RGB 0xfe /* 11111110 */
|
||||||
|
#define QOI_OP_RGBA 0xff /* 11111111 */
|
||||||
|
|
||||||
|
#define QOI_MASK_2 0xc0 /* 11000000 */
|
||||||
|
|
||||||
|
#define QOI_COLOR_HASH(C) (C.rgba.r*3 + C.rgba.g*5 + C.rgba.b*7 + C.rgba.a*11)
|
||||||
|
#define QOI_MAGIC \
|
||||||
|
(((unsigned int)'q') << 24 | ((unsigned int)'o') << 16 | \
|
||||||
|
((unsigned int)'i') << 8 | ((unsigned int)'f'))
|
||||||
|
#define QOI_HEADER_SIZE 14
|
||||||
|
|
||||||
|
/* 2GB is the max file size that this implementation can safely handle. We guard
|
||||||
|
against anything larger than that, assuming the worst case with 5 bytes per
|
||||||
|
pixel, rounded down to a nice clean value. 400 million pixels ought to be
|
||||||
|
enough for anybody. */
|
||||||
|
#define QOI_PIXELS_MAX ((unsigned int)400000000)
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
struct { unsigned char r, g, b, a; } rgba;
|
||||||
|
unsigned int v;
|
||||||
|
} qoi_rgba_t;
|
||||||
|
|
||||||
|
static const unsigned char qoi_padding[8] = {0,0,0,0,0,0,0,1};
|
||||||
|
|
||||||
|
static void qoi_write_32(unsigned char *bytes, int *p, unsigned int v) {
|
||||||
|
bytes[(*p)++] = (0xff000000 & v) >> 24;
|
||||||
|
bytes[(*p)++] = (0x00ff0000 & v) >> 16;
|
||||||
|
bytes[(*p)++] = (0x0000ff00 & v) >> 8;
|
||||||
|
bytes[(*p)++] = (0x000000ff & v);
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int qoi_read_32(const unsigned char *bytes, int *p) {
|
||||||
|
unsigned int a = bytes[(*p)++];
|
||||||
|
unsigned int b = bytes[(*p)++];
|
||||||
|
unsigned int c = bytes[(*p)++];
|
||||||
|
unsigned int d = bytes[(*p)++];
|
||||||
|
return a << 24 | b << 16 | c << 8 | d;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *qoi_encode(const void *data, const qoi_desc *desc, int *out_len) {
|
||||||
|
int i, max_size, p, run;
|
||||||
|
int px_len, px_end, px_pos, channels;
|
||||||
|
unsigned char *bytes;
|
||||||
|
const unsigned char *pixels;
|
||||||
|
qoi_rgba_t index[64];
|
||||||
|
qoi_rgba_t px, px_prev;
|
||||||
|
|
||||||
|
if (
|
||||||
|
data == NULL || out_len == NULL || desc == NULL ||
|
||||||
|
desc->width == 0 || desc->height == 0 ||
|
||||||
|
desc->channels < 3 || desc->channels > 4 ||
|
||||||
|
desc->colorspace > 1 ||
|
||||||
|
desc->height >= QOI_PIXELS_MAX / desc->width
|
||||||
|
) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
max_size =
|
||||||
|
desc->width * desc->height * (desc->channels + 1) +
|
||||||
|
QOI_HEADER_SIZE + sizeof(qoi_padding);
|
||||||
|
|
||||||
|
p = 0;
|
||||||
|
bytes = (unsigned char *) QOI_MALLOC(max_size);
|
||||||
|
if (!bytes) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
qoi_write_32(bytes, &p, QOI_MAGIC);
|
||||||
|
qoi_write_32(bytes, &p, desc->width);
|
||||||
|
qoi_write_32(bytes, &p, desc->height);
|
||||||
|
bytes[p++] = desc->channels;
|
||||||
|
bytes[p++] = desc->colorspace;
|
||||||
|
|
||||||
|
|
||||||
|
pixels = (const unsigned char *)data;
|
||||||
|
|
||||||
|
QOI_ZEROARR(index);
|
||||||
|
|
||||||
|
run = 0;
|
||||||
|
px_prev.rgba.r = 0;
|
||||||
|
px_prev.rgba.g = 0;
|
||||||
|
px_prev.rgba.b = 0;
|
||||||
|
px_prev.rgba.a = 255;
|
||||||
|
px = px_prev;
|
||||||
|
|
||||||
|
px_len = desc->width * desc->height * desc->channels;
|
||||||
|
px_end = px_len - desc->channels;
|
||||||
|
channels = desc->channels;
|
||||||
|
|
||||||
|
for (px_pos = 0; px_pos < px_len; px_pos += channels) {
|
||||||
|
px.rgba.r = pixels[px_pos + 0];
|
||||||
|
px.rgba.g = pixels[px_pos + 1];
|
||||||
|
px.rgba.b = pixels[px_pos + 2];
|
||||||
|
|
||||||
|
if (channels == 4) {
|
||||||
|
px.rgba.a = pixels[px_pos + 3];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (px.v == px_prev.v) {
|
||||||
|
run++;
|
||||||
|
if (run == 62 || px_pos == px_end) {
|
||||||
|
bytes[p++] = QOI_OP_RUN | (run - 1);
|
||||||
|
run = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int index_pos;
|
||||||
|
|
||||||
|
if (run > 0) {
|
||||||
|
bytes[p++] = QOI_OP_RUN | (run - 1);
|
||||||
|
run = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
index_pos = QOI_COLOR_HASH(px) & (64 - 1);
|
||||||
|
|
||||||
|
if (index[index_pos].v == px.v) {
|
||||||
|
bytes[p++] = QOI_OP_INDEX | index_pos;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
index[index_pos] = px;
|
||||||
|
|
||||||
|
if (px.rgba.a == px_prev.rgba.a) {
|
||||||
|
signed char vr = px.rgba.r - px_prev.rgba.r;
|
||||||
|
signed char vg = px.rgba.g - px_prev.rgba.g;
|
||||||
|
signed char vb = px.rgba.b - px_prev.rgba.b;
|
||||||
|
|
||||||
|
signed char vg_r = vr - vg;
|
||||||
|
signed char vg_b = vb - vg;
|
||||||
|
|
||||||
|
if (
|
||||||
|
vr > -3 && vr < 2 &&
|
||||||
|
vg > -3 && vg < 2 &&
|
||||||
|
vb > -3 && vb < 2
|
||||||
|
) {
|
||||||
|
bytes[p++] = QOI_OP_DIFF | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2);
|
||||||
|
}
|
||||||
|
else if (
|
||||||
|
vg_r > -9 && vg_r < 8 &&
|
||||||
|
vg > -33 && vg < 32 &&
|
||||||
|
vg_b > -9 && vg_b < 8
|
||||||
|
) {
|
||||||
|
bytes[p++] = QOI_OP_LUMA | (vg + 32);
|
||||||
|
bytes[p++] = (vg_r + 8) << 4 | (vg_b + 8);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bytes[p++] = QOI_OP_RGB;
|
||||||
|
bytes[p++] = px.rgba.r;
|
||||||
|
bytes[p++] = px.rgba.g;
|
||||||
|
bytes[p++] = px.rgba.b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bytes[p++] = QOI_OP_RGBA;
|
||||||
|
bytes[p++] = px.rgba.r;
|
||||||
|
bytes[p++] = px.rgba.g;
|
||||||
|
bytes[p++] = px.rgba.b;
|
||||||
|
bytes[p++] = px.rgba.a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
px_prev = px;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < (int)sizeof(qoi_padding); i++) {
|
||||||
|
bytes[p++] = qoi_padding[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_len = p;
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *qoi_decode(const void *data, int size, qoi_desc *desc, int channels) {
|
||||||
|
const unsigned char *bytes;
|
||||||
|
unsigned int header_magic;
|
||||||
|
unsigned char *pixels;
|
||||||
|
qoi_rgba_t index[64];
|
||||||
|
qoi_rgba_t px;
|
||||||
|
int px_len, chunks_len, px_pos;
|
||||||
|
int p = 0, run = 0;
|
||||||
|
|
||||||
|
if (
|
||||||
|
data == NULL || desc == NULL ||
|
||||||
|
(channels != 0 && channels != 3 && channels != 4) ||
|
||||||
|
size < QOI_HEADER_SIZE + (int)sizeof(qoi_padding)
|
||||||
|
) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes = (const unsigned char *)data;
|
||||||
|
|
||||||
|
header_magic = qoi_read_32(bytes, &p);
|
||||||
|
desc->width = qoi_read_32(bytes, &p);
|
||||||
|
desc->height = qoi_read_32(bytes, &p);
|
||||||
|
desc->channels = bytes[p++];
|
||||||
|
desc->colorspace = bytes[p++];
|
||||||
|
|
||||||
|
if (
|
||||||
|
desc->width == 0 || desc->height == 0 ||
|
||||||
|
desc->channels < 3 || desc->channels > 4 ||
|
||||||
|
desc->colorspace > 1 ||
|
||||||
|
header_magic != QOI_MAGIC ||
|
||||||
|
desc->height >= QOI_PIXELS_MAX / desc->width
|
||||||
|
) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (channels == 0) {
|
||||||
|
channels = desc->channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
px_len = desc->width * desc->height * channels;
|
||||||
|
pixels = (unsigned char *) QOI_MALLOC(px_len);
|
||||||
|
if (!pixels) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
QOI_ZEROARR(index);
|
||||||
|
px.rgba.r = 0;
|
||||||
|
px.rgba.g = 0;
|
||||||
|
px.rgba.b = 0;
|
||||||
|
px.rgba.a = 255;
|
||||||
|
|
||||||
|
chunks_len = size - (int)sizeof(qoi_padding);
|
||||||
|
for (px_pos = 0; px_pos < px_len; px_pos += channels) {
|
||||||
|
if (run > 0) {
|
||||||
|
run--;
|
||||||
|
}
|
||||||
|
else if (p < chunks_len) {
|
||||||
|
int b1 = bytes[p++];
|
||||||
|
|
||||||
|
if (b1 == QOI_OP_RGB) {
|
||||||
|
px.rgba.r = bytes[p++];
|
||||||
|
px.rgba.g = bytes[p++];
|
||||||
|
px.rgba.b = bytes[p++];
|
||||||
|
}
|
||||||
|
else if (b1 == QOI_OP_RGBA) {
|
||||||
|
px.rgba.r = bytes[p++];
|
||||||
|
px.rgba.g = bytes[p++];
|
||||||
|
px.rgba.b = bytes[p++];
|
||||||
|
px.rgba.a = bytes[p++];
|
||||||
|
}
|
||||||
|
else if ((b1 & QOI_MASK_2) == QOI_OP_INDEX) {
|
||||||
|
px = index[b1];
|
||||||
|
}
|
||||||
|
else if ((b1 & QOI_MASK_2) == QOI_OP_DIFF) {
|
||||||
|
px.rgba.r += ((b1 >> 4) & 0x03) - 2;
|
||||||
|
px.rgba.g += ((b1 >> 2) & 0x03) - 2;
|
||||||
|
px.rgba.b += ( b1 & 0x03) - 2;
|
||||||
|
}
|
||||||
|
else if ((b1 & QOI_MASK_2) == QOI_OP_LUMA) {
|
||||||
|
int b2 = bytes[p++];
|
||||||
|
int vg = (b1 & 0x3f) - 32;
|
||||||
|
px.rgba.r += vg - 8 + ((b2 >> 4) & 0x0f);
|
||||||
|
px.rgba.g += vg;
|
||||||
|
px.rgba.b += vg - 8 + (b2 & 0x0f);
|
||||||
|
}
|
||||||
|
else if ((b1 & QOI_MASK_2) == QOI_OP_RUN) {
|
||||||
|
run = (b1 & 0x3f);
|
||||||
|
}
|
||||||
|
|
||||||
|
index[QOI_COLOR_HASH(px) & (64 - 1)] = px;
|
||||||
|
}
|
||||||
|
|
||||||
|
pixels[px_pos + 0] = px.rgba.r;
|
||||||
|
pixels[px_pos + 1] = px.rgba.g;
|
||||||
|
pixels[px_pos + 2] = px.rgba.b;
|
||||||
|
|
||||||
|
if (channels == 4) {
|
||||||
|
pixels[px_pos + 3] = px.rgba.a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef QOI_NO_STDIO
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int qoi_write(const char *filename, const void *data, const qoi_desc *desc) {
|
||||||
|
FILE *f = fopen(filename, "wb");
|
||||||
|
int size, err;
|
||||||
|
void *encoded;
|
||||||
|
|
||||||
|
if (!f) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded = qoi_encode(data, desc, &size);
|
||||||
|
if (!encoded) {
|
||||||
|
fclose(f);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite(encoded, 1, size, f);
|
||||||
|
fflush(f);
|
||||||
|
err = ferror(f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
QOI_FREE(encoded);
|
||||||
|
return err ? 0 : size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *qoi_read(const char *filename, qoi_desc *desc, int channels) {
|
||||||
|
FILE *f = fopen(filename, "rb");
|
||||||
|
int size, bytes_read;
|
||||||
|
void *pixels, *data;
|
||||||
|
|
||||||
|
if (!f) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
size = ftell(f);
|
||||||
|
if (size <= 0 || fseek(f, 0, SEEK_SET) != 0) {
|
||||||
|
fclose(f);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
data = QOI_MALLOC(size);
|
||||||
|
if (!data) {
|
||||||
|
fclose(f);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes_read = fread(data, 1, size, f);
|
||||||
|
fclose(f);
|
||||||
|
pixels = (bytes_read != size) ? NULL : qoi_decode(data, bytes_read, desc, channels);
|
||||||
|
QOI_FREE(data);
|
||||||
|
return pixels;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* QOI_NO_STDIO */
|
||||||
|
#endif /* QOI_IMPLEMENTATION */
|
||||||
719
stb_dxt.h
Normal file
719
stb_dxt.h
Normal file
@@ -0,0 +1,719 @@
|
|||||||
|
// stb_dxt.h - v1.12 - DXT1/DXT5 compressor - public domain
|
||||||
|
// original by fabian "ryg" giesen - ported to C by stb
|
||||||
|
// use '#define STB_DXT_IMPLEMENTATION' before including to create the implementation
|
||||||
|
//
|
||||||
|
// USAGE:
|
||||||
|
// call stb_compress_dxt_block() for every block (you must pad)
|
||||||
|
// source should be a 4x4 block of RGBA data in row-major order;
|
||||||
|
// Alpha channel is not stored if you specify alpha=0 (but you
|
||||||
|
// must supply some constant alpha in the alpha channel).
|
||||||
|
// You can turn on dithering and "high quality" using mode.
|
||||||
|
//
|
||||||
|
// version history:
|
||||||
|
// v1.12 - (ryg) fix bug in single-color table generator
|
||||||
|
// v1.11 - (ryg) avoid racy global init, better single-color tables, remove dither
|
||||||
|
// v1.10 - (i.c) various small quality improvements
|
||||||
|
// v1.09 - (stb) update documentation re: surprising alpha channel requirement
|
||||||
|
// v1.08 - (stb) fix bug in dxt-with-alpha block
|
||||||
|
// v1.07 - (stb) bc4; allow not using libc; add STB_DXT_STATIC
|
||||||
|
// v1.06 - (stb) fix to known-broken 1.05
|
||||||
|
// v1.05 - (stb) support bc5/3dc (Arvids Kokins), use extern "C" in C++ (Pavel Krajcevski)
|
||||||
|
// v1.04 - (ryg) default to no rounding bias for lerped colors (as per S3TC/DX10 spec);
|
||||||
|
// single color match fix (allow for inexact color interpolation);
|
||||||
|
// optimal DXT5 index finder; "high quality" mode that runs multiple refinement steps.
|
||||||
|
// v1.03 - (stb) endianness support
|
||||||
|
// v1.02 - (stb) fix alpha encoding bug
|
||||||
|
// v1.01 - (stb) fix bug converting to RGB that messed up quality, thanks ryg & cbloom
|
||||||
|
// v1.00 - (stb) first release
|
||||||
|
//
|
||||||
|
// contributors:
|
||||||
|
// Rich Geldreich (more accurate index selection)
|
||||||
|
// Kevin Schmidt (#defines for "freestanding" compilation)
|
||||||
|
// github:ppiastucki (BC4 support)
|
||||||
|
// Ignacio Castano - improve DXT endpoint quantization
|
||||||
|
// Alan Hickman - static table initialization
|
||||||
|
//
|
||||||
|
// LICENSE
|
||||||
|
//
|
||||||
|
// See end of file for license information.
|
||||||
|
|
||||||
|
#ifndef STB_INCLUDE_STB_DXT_H
|
||||||
|
#define STB_INCLUDE_STB_DXT_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef STB_DXT_STATIC
|
||||||
|
#define STBDDEF static
|
||||||
|
#else
|
||||||
|
#define STBDDEF extern
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// compression mode (bitflags)
|
||||||
|
#define STB_DXT_NORMAL 0
|
||||||
|
#define STB_DXT_DITHER 1 // use dithering. was always dubious, now deprecated. does nothing!
|
||||||
|
#define STB_DXT_HIGHQUAL 2 // high quality mode, does two refinement steps instead of 1. ~30-40% slower.
|
||||||
|
|
||||||
|
STBDDEF void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src_rgba_four_bytes_per_pixel, int alpha, int mode);
|
||||||
|
STBDDEF void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src_r_one_byte_per_pixel);
|
||||||
|
STBDDEF void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src_rg_two_byte_per_pixel);
|
||||||
|
|
||||||
|
#define STB_COMPRESS_DXT_BLOCK
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // STB_INCLUDE_STB_DXT_H
|
||||||
|
|
||||||
|
#ifdef STB_DXT_IMPLEMENTATION
|
||||||
|
|
||||||
|
// configuration options for DXT encoder. set them in the project/makefile or just define
|
||||||
|
// them at the top.
|
||||||
|
|
||||||
|
// STB_DXT_USE_ROUNDING_BIAS
|
||||||
|
// use a rounding bias during color interpolation. this is closer to what "ideal"
|
||||||
|
// interpolation would do but doesn't match the S3TC/DX10 spec. old versions (pre-1.03)
|
||||||
|
// implicitly had this turned on.
|
||||||
|
//
|
||||||
|
// in case you're targeting a specific type of hardware (e.g. console programmers):
|
||||||
|
// NVidia and Intel GPUs (as of 2010) as well as DX9 ref use DXT decoders that are closer
|
||||||
|
// to STB_DXT_USE_ROUNDING_BIAS. AMD/ATI, S3 and DX10 ref are closer to rounding with no bias.
|
||||||
|
// you also see "(a*5 + b*3) / 8" on some old GPU designs.
|
||||||
|
// #define STB_DXT_USE_ROUNDING_BIAS
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#if !defined(STBD_FABS)
|
||||||
|
#include <math.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef STBD_FABS
|
||||||
|
#define STBD_FABS(x) fabs(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const unsigned char stb__OMatch5[256][2] = {
|
||||||
|
{ 0, 0 }, { 0, 0 }, { 0, 1 }, { 0, 1 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 1 },
|
||||||
|
{ 1, 1 }, { 1, 1 }, { 1, 2 }, { 0, 4 }, { 2, 1 }, { 2, 1 }, { 2, 1 }, { 2, 2 },
|
||||||
|
{ 2, 2 }, { 2, 2 }, { 2, 3 }, { 1, 5 }, { 3, 2 }, { 3, 2 }, { 4, 0 }, { 3, 3 },
|
||||||
|
{ 3, 3 }, { 3, 3 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 5 }, { 4, 3 }, { 4, 3 },
|
||||||
|
{ 5, 2 }, { 4, 4 }, { 4, 4 }, { 4, 5 }, { 4, 5 }, { 5, 4 }, { 5, 4 }, { 5, 4 },
|
||||||
|
{ 6, 3 }, { 5, 5 }, { 5, 5 }, { 5, 6 }, { 4, 8 }, { 6, 5 }, { 6, 5 }, { 6, 5 },
|
||||||
|
{ 6, 6 }, { 6, 6 }, { 6, 6 }, { 6, 7 }, { 5, 9 }, { 7, 6 }, { 7, 6 }, { 8, 4 },
|
||||||
|
{ 7, 7 }, { 7, 7 }, { 7, 7 }, { 7, 8 }, { 7, 8 }, { 7, 8 }, { 7, 9 }, { 8, 7 },
|
||||||
|
{ 8, 7 }, { 9, 6 }, { 8, 8 }, { 8, 8 }, { 8, 9 }, { 8, 9 }, { 9, 8 }, { 9, 8 },
|
||||||
|
{ 9, 8 }, { 10, 7 }, { 9, 9 }, { 9, 9 }, { 9, 10 }, { 8, 12 }, { 10, 9 }, { 10, 9 },
|
||||||
|
{ 10, 9 }, { 10, 10 }, { 10, 10 }, { 10, 10 }, { 10, 11 }, { 9, 13 }, { 11, 10 }, { 11, 10 },
|
||||||
|
{ 12, 8 }, { 11, 11 }, { 11, 11 }, { 11, 11 }, { 11, 12 }, { 11, 12 }, { 11, 12 }, { 11, 13 },
|
||||||
|
{ 12, 11 }, { 12, 11 }, { 13, 10 }, { 12, 12 }, { 12, 12 }, { 12, 13 }, { 12, 13 }, { 13, 12 },
|
||||||
|
{ 13, 12 }, { 13, 12 }, { 14, 11 }, { 13, 13 }, { 13, 13 }, { 13, 14 }, { 12, 16 }, { 14, 13 },
|
||||||
|
{ 14, 13 }, { 14, 13 }, { 14, 14 }, { 14, 14 }, { 14, 14 }, { 14, 15 }, { 13, 17 }, { 15, 14 },
|
||||||
|
{ 15, 14 }, { 16, 12 }, { 15, 15 }, { 15, 15 }, { 15, 15 }, { 15, 16 }, { 15, 16 }, { 15, 16 },
|
||||||
|
{ 15, 17 }, { 16, 15 }, { 16, 15 }, { 17, 14 }, { 16, 16 }, { 16, 16 }, { 16, 17 }, { 16, 17 },
|
||||||
|
{ 17, 16 }, { 17, 16 }, { 17, 16 }, { 18, 15 }, { 17, 17 }, { 17, 17 }, { 17, 18 }, { 16, 20 },
|
||||||
|
{ 18, 17 }, { 18, 17 }, { 18, 17 }, { 18, 18 }, { 18, 18 }, { 18, 18 }, { 18, 19 }, { 17, 21 },
|
||||||
|
{ 19, 18 }, { 19, 18 }, { 20, 16 }, { 19, 19 }, { 19, 19 }, { 19, 19 }, { 19, 20 }, { 19, 20 },
|
||||||
|
{ 19, 20 }, { 19, 21 }, { 20, 19 }, { 20, 19 }, { 21, 18 }, { 20, 20 }, { 20, 20 }, { 20, 21 },
|
||||||
|
{ 20, 21 }, { 21, 20 }, { 21, 20 }, { 21, 20 }, { 22, 19 }, { 21, 21 }, { 21, 21 }, { 21, 22 },
|
||||||
|
{ 20, 24 }, { 22, 21 }, { 22, 21 }, { 22, 21 }, { 22, 22 }, { 22, 22 }, { 22, 22 }, { 22, 23 },
|
||||||
|
{ 21, 25 }, { 23, 22 }, { 23, 22 }, { 24, 20 }, { 23, 23 }, { 23, 23 }, { 23, 23 }, { 23, 24 },
|
||||||
|
{ 23, 24 }, { 23, 24 }, { 23, 25 }, { 24, 23 }, { 24, 23 }, { 25, 22 }, { 24, 24 }, { 24, 24 },
|
||||||
|
{ 24, 25 }, { 24, 25 }, { 25, 24 }, { 25, 24 }, { 25, 24 }, { 26, 23 }, { 25, 25 }, { 25, 25 },
|
||||||
|
{ 25, 26 }, { 24, 28 }, { 26, 25 }, { 26, 25 }, { 26, 25 }, { 26, 26 }, { 26, 26 }, { 26, 26 },
|
||||||
|
{ 26, 27 }, { 25, 29 }, { 27, 26 }, { 27, 26 }, { 28, 24 }, { 27, 27 }, { 27, 27 }, { 27, 27 },
|
||||||
|
{ 27, 28 }, { 27, 28 }, { 27, 28 }, { 27, 29 }, { 28, 27 }, { 28, 27 }, { 29, 26 }, { 28, 28 },
|
||||||
|
{ 28, 28 }, { 28, 29 }, { 28, 29 }, { 29, 28 }, { 29, 28 }, { 29, 28 }, { 30, 27 }, { 29, 29 },
|
||||||
|
{ 29, 29 }, { 29, 30 }, { 29, 30 }, { 30, 29 }, { 30, 29 }, { 30, 29 }, { 30, 30 }, { 30, 30 },
|
||||||
|
{ 30, 30 }, { 30, 31 }, { 30, 31 }, { 31, 30 }, { 31, 30 }, { 31, 30 }, { 31, 31 }, { 31, 31 },
|
||||||
|
};
|
||||||
|
static const unsigned char stb__OMatch6[256][2] = {
|
||||||
|
{ 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 }, { 1, 1 }, { 1, 2 }, { 2, 1 }, { 2, 2 },
|
||||||
|
{ 2, 2 }, { 2, 3 }, { 3, 2 }, { 3, 3 }, { 3, 3 }, { 3, 4 }, { 4, 3 }, { 4, 4 },
|
||||||
|
{ 4, 4 }, { 4, 5 }, { 5, 4 }, { 5, 5 }, { 5, 5 }, { 5, 6 }, { 6, 5 }, { 6, 6 },
|
||||||
|
{ 6, 6 }, { 6, 7 }, { 7, 6 }, { 7, 7 }, { 7, 7 }, { 7, 8 }, { 8, 7 }, { 8, 8 },
|
||||||
|
{ 8, 8 }, { 8, 9 }, { 9, 8 }, { 9, 9 }, { 9, 9 }, { 9, 10 }, { 10, 9 }, { 10, 10 },
|
||||||
|
{ 10, 10 }, { 10, 11 }, { 11, 10 }, { 8, 16 }, { 11, 11 }, { 11, 12 }, { 12, 11 }, { 9, 17 },
|
||||||
|
{ 12, 12 }, { 12, 13 }, { 13, 12 }, { 11, 16 }, { 13, 13 }, { 13, 14 }, { 14, 13 }, { 12, 17 },
|
||||||
|
{ 14, 14 }, { 14, 15 }, { 15, 14 }, { 14, 16 }, { 15, 15 }, { 15, 16 }, { 16, 14 }, { 16, 15 },
|
||||||
|
{ 17, 14 }, { 16, 16 }, { 16, 17 }, { 17, 16 }, { 18, 15 }, { 17, 17 }, { 17, 18 }, { 18, 17 },
|
||||||
|
{ 20, 14 }, { 18, 18 }, { 18, 19 }, { 19, 18 }, { 21, 15 }, { 19, 19 }, { 19, 20 }, { 20, 19 },
|
||||||
|
{ 20, 20 }, { 20, 20 }, { 20, 21 }, { 21, 20 }, { 21, 21 }, { 21, 21 }, { 21, 22 }, { 22, 21 },
|
||||||
|
{ 22, 22 }, { 22, 22 }, { 22, 23 }, { 23, 22 }, { 23, 23 }, { 23, 23 }, { 23, 24 }, { 24, 23 },
|
||||||
|
{ 24, 24 }, { 24, 24 }, { 24, 25 }, { 25, 24 }, { 25, 25 }, { 25, 25 }, { 25, 26 }, { 26, 25 },
|
||||||
|
{ 26, 26 }, { 26, 26 }, { 26, 27 }, { 27, 26 }, { 24, 32 }, { 27, 27 }, { 27, 28 }, { 28, 27 },
|
||||||
|
{ 25, 33 }, { 28, 28 }, { 28, 29 }, { 29, 28 }, { 27, 32 }, { 29, 29 }, { 29, 30 }, { 30, 29 },
|
||||||
|
{ 28, 33 }, { 30, 30 }, { 30, 31 }, { 31, 30 }, { 30, 32 }, { 31, 31 }, { 31, 32 }, { 32, 30 },
|
||||||
|
{ 32, 31 }, { 33, 30 }, { 32, 32 }, { 32, 33 }, { 33, 32 }, { 34, 31 }, { 33, 33 }, { 33, 34 },
|
||||||
|
{ 34, 33 }, { 36, 30 }, { 34, 34 }, { 34, 35 }, { 35, 34 }, { 37, 31 }, { 35, 35 }, { 35, 36 },
|
||||||
|
{ 36, 35 }, { 36, 36 }, { 36, 36 }, { 36, 37 }, { 37, 36 }, { 37, 37 }, { 37, 37 }, { 37, 38 },
|
||||||
|
{ 38, 37 }, { 38, 38 }, { 38, 38 }, { 38, 39 }, { 39, 38 }, { 39, 39 }, { 39, 39 }, { 39, 40 },
|
||||||
|
{ 40, 39 }, { 40, 40 }, { 40, 40 }, { 40, 41 }, { 41, 40 }, { 41, 41 }, { 41, 41 }, { 41, 42 },
|
||||||
|
{ 42, 41 }, { 42, 42 }, { 42, 42 }, { 42, 43 }, { 43, 42 }, { 40, 48 }, { 43, 43 }, { 43, 44 },
|
||||||
|
{ 44, 43 }, { 41, 49 }, { 44, 44 }, { 44, 45 }, { 45, 44 }, { 43, 48 }, { 45, 45 }, { 45, 46 },
|
||||||
|
{ 46, 45 }, { 44, 49 }, { 46, 46 }, { 46, 47 }, { 47, 46 }, { 46, 48 }, { 47, 47 }, { 47, 48 },
|
||||||
|
{ 48, 46 }, { 48, 47 }, { 49, 46 }, { 48, 48 }, { 48, 49 }, { 49, 48 }, { 50, 47 }, { 49, 49 },
|
||||||
|
{ 49, 50 }, { 50, 49 }, { 52, 46 }, { 50, 50 }, { 50, 51 }, { 51, 50 }, { 53, 47 }, { 51, 51 },
|
||||||
|
{ 51, 52 }, { 52, 51 }, { 52, 52 }, { 52, 52 }, { 52, 53 }, { 53, 52 }, { 53, 53 }, { 53, 53 },
|
||||||
|
{ 53, 54 }, { 54, 53 }, { 54, 54 }, { 54, 54 }, { 54, 55 }, { 55, 54 }, { 55, 55 }, { 55, 55 },
|
||||||
|
{ 55, 56 }, { 56, 55 }, { 56, 56 }, { 56, 56 }, { 56, 57 }, { 57, 56 }, { 57, 57 }, { 57, 57 },
|
||||||
|
{ 57, 58 }, { 58, 57 }, { 58, 58 }, { 58, 58 }, { 58, 59 }, { 59, 58 }, { 59, 59 }, { 59, 59 },
|
||||||
|
{ 59, 60 }, { 60, 59 }, { 60, 60 }, { 60, 60 }, { 60, 61 }, { 61, 60 }, { 61, 61 }, { 61, 61 },
|
||||||
|
{ 61, 62 }, { 62, 61 }, { 62, 62 }, { 62, 62 }, { 62, 63 }, { 63, 62 }, { 63, 63 }, { 63, 63 },
|
||||||
|
};
|
||||||
|
|
||||||
|
static int stb__Mul8Bit(int a, int b)
|
||||||
|
{
|
||||||
|
int t = a*b + 128;
|
||||||
|
return (t + (t >> 8)) >> 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stb__From16Bit(unsigned char *out, unsigned short v)
|
||||||
|
{
|
||||||
|
int rv = (v & 0xf800) >> 11;
|
||||||
|
int gv = (v & 0x07e0) >> 5;
|
||||||
|
int bv = (v & 0x001f) >> 0;
|
||||||
|
|
||||||
|
// expand to 8 bits via bit replication
|
||||||
|
out[0] = (rv * 33) >> 2;
|
||||||
|
out[1] = (gv * 65) >> 4;
|
||||||
|
out[2] = (bv * 33) >> 2;
|
||||||
|
out[3] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned short stb__As16Bit(int r, int g, int b)
|
||||||
|
{
|
||||||
|
return (unsigned short)((stb__Mul8Bit(r,31) << 11) + (stb__Mul8Bit(g,63) << 5) + stb__Mul8Bit(b,31));
|
||||||
|
}
|
||||||
|
|
||||||
|
// linear interpolation at 1/3 point between a and b, using desired rounding type
|
||||||
|
static int stb__Lerp13(int a, int b)
|
||||||
|
{
|
||||||
|
#ifdef STB_DXT_USE_ROUNDING_BIAS
|
||||||
|
// with rounding bias
|
||||||
|
return a + stb__Mul8Bit(b-a, 0x55);
|
||||||
|
#else
|
||||||
|
// without rounding bias
|
||||||
|
// replace "/ 3" by "* 0xaaab) >> 17" if your compiler sucks or you really need every ounce of speed.
|
||||||
|
return (2*a + b) / 3;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// lerp RGB color
|
||||||
|
static void stb__Lerp13RGB(unsigned char *out, unsigned char *p1, unsigned char *p2)
|
||||||
|
{
|
||||||
|
out[0] = (unsigned char)stb__Lerp13(p1[0], p2[0]);
|
||||||
|
out[1] = (unsigned char)stb__Lerp13(p1[1], p2[1]);
|
||||||
|
out[2] = (unsigned char)stb__Lerp13(p1[2], p2[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************/
|
||||||
|
|
||||||
|
static void stb__EvalColors(unsigned char *color,unsigned short c0,unsigned short c1)
|
||||||
|
{
|
||||||
|
stb__From16Bit(color+ 0, c0);
|
||||||
|
stb__From16Bit(color+ 4, c1);
|
||||||
|
stb__Lerp13RGB(color+ 8, color+0, color+4);
|
||||||
|
stb__Lerp13RGB(color+12, color+4, color+0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The color matching function
|
||||||
|
static unsigned int stb__MatchColorsBlock(unsigned char *block, unsigned char *color)
|
||||||
|
{
|
||||||
|
unsigned int mask = 0;
|
||||||
|
int dirr = color[0*4+0] - color[1*4+0];
|
||||||
|
int dirg = color[0*4+1] - color[1*4+1];
|
||||||
|
int dirb = color[0*4+2] - color[1*4+2];
|
||||||
|
int dots[16];
|
||||||
|
int stops[4];
|
||||||
|
int i;
|
||||||
|
int c0Point, halfPoint, c3Point;
|
||||||
|
|
||||||
|
for(i=0;i<16;i++)
|
||||||
|
dots[i] = block[i*4+0]*dirr + block[i*4+1]*dirg + block[i*4+2]*dirb;
|
||||||
|
|
||||||
|
for(i=0;i<4;i++)
|
||||||
|
stops[i] = color[i*4+0]*dirr + color[i*4+1]*dirg + color[i*4+2]*dirb;
|
||||||
|
|
||||||
|
// think of the colors as arranged on a line; project point onto that line, then choose
|
||||||
|
// next color out of available ones. we compute the crossover points for "best color in top
|
||||||
|
// half"/"best in bottom half" and then the same inside that subinterval.
|
||||||
|
//
|
||||||
|
// relying on this 1d approximation isn't always optimal in terms of euclidean distance,
|
||||||
|
// but it's very close and a lot faster.
|
||||||
|
// http://cbloomrants.blogspot.com/2008/12/12-08-08-dxtc-summary.html
|
||||||
|
|
||||||
|
c0Point = (stops[1] + stops[3]);
|
||||||
|
halfPoint = (stops[3] + stops[2]);
|
||||||
|
c3Point = (stops[2] + stops[0]);
|
||||||
|
|
||||||
|
for (i=15;i>=0;i--) {
|
||||||
|
int dot = dots[i]*2;
|
||||||
|
mask <<= 2;
|
||||||
|
|
||||||
|
if(dot < halfPoint)
|
||||||
|
mask |= (dot < c0Point) ? 1 : 3;
|
||||||
|
else
|
||||||
|
mask |= (dot < c3Point) ? 2 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The color optimization function. (Clever code, part 1)
|
||||||
|
static void stb__OptimizeColorsBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16)
|
||||||
|
{
|
||||||
|
int mind,maxd;
|
||||||
|
unsigned char *minp, *maxp;
|
||||||
|
double magn;
|
||||||
|
int v_r,v_g,v_b;
|
||||||
|
static const int nIterPower = 4;
|
||||||
|
float covf[6],vfr,vfg,vfb;
|
||||||
|
|
||||||
|
// determine color distribution
|
||||||
|
int cov[6];
|
||||||
|
int mu[3],min[3],max[3];
|
||||||
|
int ch,i,iter;
|
||||||
|
|
||||||
|
for(ch=0;ch<3;ch++)
|
||||||
|
{
|
||||||
|
const unsigned char *bp = ((const unsigned char *) block) + ch;
|
||||||
|
int muv,minv,maxv;
|
||||||
|
|
||||||
|
muv = minv = maxv = bp[0];
|
||||||
|
for(i=4;i<64;i+=4)
|
||||||
|
{
|
||||||
|
muv += bp[i];
|
||||||
|
if (bp[i] < minv) minv = bp[i];
|
||||||
|
else if (bp[i] > maxv) maxv = bp[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
mu[ch] = (muv + 8) >> 4;
|
||||||
|
min[ch] = minv;
|
||||||
|
max[ch] = maxv;
|
||||||
|
}
|
||||||
|
|
||||||
|
// determine covariance matrix
|
||||||
|
for (i=0;i<6;i++)
|
||||||
|
cov[i] = 0;
|
||||||
|
|
||||||
|
for (i=0;i<16;i++)
|
||||||
|
{
|
||||||
|
int r = block[i*4+0] - mu[0];
|
||||||
|
int g = block[i*4+1] - mu[1];
|
||||||
|
int b = block[i*4+2] - mu[2];
|
||||||
|
|
||||||
|
cov[0] += r*r;
|
||||||
|
cov[1] += r*g;
|
||||||
|
cov[2] += r*b;
|
||||||
|
cov[3] += g*g;
|
||||||
|
cov[4] += g*b;
|
||||||
|
cov[5] += b*b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert covariance matrix to float, find principal axis via power iter
|
||||||
|
for(i=0;i<6;i++)
|
||||||
|
covf[i] = cov[i] / 255.0f;
|
||||||
|
|
||||||
|
vfr = (float) (max[0] - min[0]);
|
||||||
|
vfg = (float) (max[1] - min[1]);
|
||||||
|
vfb = (float) (max[2] - min[2]);
|
||||||
|
|
||||||
|
for(iter=0;iter<nIterPower;iter++)
|
||||||
|
{
|
||||||
|
float r = vfr*covf[0] + vfg*covf[1] + vfb*covf[2];
|
||||||
|
float g = vfr*covf[1] + vfg*covf[3] + vfb*covf[4];
|
||||||
|
float b = vfr*covf[2] + vfg*covf[4] + vfb*covf[5];
|
||||||
|
|
||||||
|
vfr = r;
|
||||||
|
vfg = g;
|
||||||
|
vfb = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
magn = STBD_FABS(vfr);
|
||||||
|
if (STBD_FABS(vfg) > magn) magn = STBD_FABS(vfg);
|
||||||
|
if (STBD_FABS(vfb) > magn) magn = STBD_FABS(vfb);
|
||||||
|
|
||||||
|
if(magn < 4.0f) { // too small, default to luminance
|
||||||
|
v_r = 299; // JPEG YCbCr luma coefs, scaled by 1000.
|
||||||
|
v_g = 587;
|
||||||
|
v_b = 114;
|
||||||
|
} else {
|
||||||
|
magn = 512.0 / magn;
|
||||||
|
v_r = (int) (vfr * magn);
|
||||||
|
v_g = (int) (vfg * magn);
|
||||||
|
v_b = (int) (vfb * magn);
|
||||||
|
}
|
||||||
|
|
||||||
|
minp = maxp = block;
|
||||||
|
mind = maxd = block[0]*v_r + block[1]*v_g + block[2]*v_b;
|
||||||
|
// Pick colors at extreme points
|
||||||
|
for(i=1;i<16;i++)
|
||||||
|
{
|
||||||
|
int dot = block[i*4+0]*v_r + block[i*4+1]*v_g + block[i*4+2]*v_b;
|
||||||
|
|
||||||
|
if (dot < mind) {
|
||||||
|
mind = dot;
|
||||||
|
minp = block+i*4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dot > maxd) {
|
||||||
|
maxd = dot;
|
||||||
|
maxp = block+i*4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*pmax16 = stb__As16Bit(maxp[0],maxp[1],maxp[2]);
|
||||||
|
*pmin16 = stb__As16Bit(minp[0],minp[1],minp[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const float stb__midpoints5[32] = {
|
||||||
|
0.015686f, 0.047059f, 0.078431f, 0.111765f, 0.145098f, 0.176471f, 0.207843f, 0.241176f, 0.274510f, 0.305882f, 0.337255f, 0.370588f, 0.403922f, 0.435294f, 0.466667f, 0.5f,
|
||||||
|
0.533333f, 0.564706f, 0.596078f, 0.629412f, 0.662745f, 0.694118f, 0.725490f, 0.758824f, 0.792157f, 0.823529f, 0.854902f, 0.888235f, 0.921569f, 0.952941f, 0.984314f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
static const float stb__midpoints6[64] = {
|
||||||
|
0.007843f, 0.023529f, 0.039216f, 0.054902f, 0.070588f, 0.086275f, 0.101961f, 0.117647f, 0.133333f, 0.149020f, 0.164706f, 0.180392f, 0.196078f, 0.211765f, 0.227451f, 0.245098f,
|
||||||
|
0.262745f, 0.278431f, 0.294118f, 0.309804f, 0.325490f, 0.341176f, 0.356863f, 0.372549f, 0.388235f, 0.403922f, 0.419608f, 0.435294f, 0.450980f, 0.466667f, 0.482353f, 0.500000f,
|
||||||
|
0.517647f, 0.533333f, 0.549020f, 0.564706f, 0.580392f, 0.596078f, 0.611765f, 0.627451f, 0.643137f, 0.658824f, 0.674510f, 0.690196f, 0.705882f, 0.721569f, 0.737255f, 0.754902f,
|
||||||
|
0.772549f, 0.788235f, 0.803922f, 0.819608f, 0.835294f, 0.850980f, 0.866667f, 0.882353f, 0.898039f, 0.913725f, 0.929412f, 0.945098f, 0.960784f, 0.976471f, 0.992157f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
static unsigned short stb__Quantize5(float x)
|
||||||
|
{
|
||||||
|
unsigned short q;
|
||||||
|
x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
|
||||||
|
q = (unsigned short)(x * 31);
|
||||||
|
q += (x > stb__midpoints5[q]);
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned short stb__Quantize6(float x)
|
||||||
|
{
|
||||||
|
unsigned short q;
|
||||||
|
x = x < 0 ? 0 : x > 1 ? 1 : x; // saturate
|
||||||
|
q = (unsigned short)(x * 63);
|
||||||
|
q += (x > stb__midpoints6[q]);
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The refinement function. (Clever code, part 2)
|
||||||
|
// Tries to optimize colors to suit block contents better.
|
||||||
|
// (By solving a least squares system via normal equations+Cramer's rule)
|
||||||
|
static int stb__RefineBlock(unsigned char *block, unsigned short *pmax16, unsigned short *pmin16, unsigned int mask)
|
||||||
|
{
|
||||||
|
static const int w1Tab[4] = { 3,0,2,1 };
|
||||||
|
static const int prods[4] = { 0x090000,0x000900,0x040102,0x010402 };
|
||||||
|
// ^some magic to save a lot of multiplies in the accumulating loop...
|
||||||
|
// (precomputed products of weights for least squares system, accumulated inside one 32-bit register)
|
||||||
|
|
||||||
|
float f;
|
||||||
|
unsigned short oldMin, oldMax, min16, max16;
|
||||||
|
int i, akku = 0, xx,xy,yy;
|
||||||
|
int At1_r,At1_g,At1_b;
|
||||||
|
int At2_r,At2_g,At2_b;
|
||||||
|
unsigned int cm = mask;
|
||||||
|
|
||||||
|
oldMin = *pmin16;
|
||||||
|
oldMax = *pmax16;
|
||||||
|
|
||||||
|
if((mask ^ (mask<<2)) < 4) // all pixels have the same index?
|
||||||
|
{
|
||||||
|
// yes, linear system would be singular; solve using optimal
|
||||||
|
// single-color match on average color
|
||||||
|
int r = 8, g = 8, b = 8;
|
||||||
|
for (i=0;i<16;++i) {
|
||||||
|
r += block[i*4+0];
|
||||||
|
g += block[i*4+1];
|
||||||
|
b += block[i*4+2];
|
||||||
|
}
|
||||||
|
|
||||||
|
r >>= 4; g >>= 4; b >>= 4;
|
||||||
|
|
||||||
|
max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];
|
||||||
|
min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];
|
||||||
|
} else {
|
||||||
|
At1_r = At1_g = At1_b = 0;
|
||||||
|
At2_r = At2_g = At2_b = 0;
|
||||||
|
for (i=0;i<16;++i,cm>>=2) {
|
||||||
|
int step = cm&3;
|
||||||
|
int w1 = w1Tab[step];
|
||||||
|
int r = block[i*4+0];
|
||||||
|
int g = block[i*4+1];
|
||||||
|
int b = block[i*4+2];
|
||||||
|
|
||||||
|
akku += prods[step];
|
||||||
|
At1_r += w1*r;
|
||||||
|
At1_g += w1*g;
|
||||||
|
At1_b += w1*b;
|
||||||
|
At2_r += r;
|
||||||
|
At2_g += g;
|
||||||
|
At2_b += b;
|
||||||
|
}
|
||||||
|
|
||||||
|
At2_r = 3*At2_r - At1_r;
|
||||||
|
At2_g = 3*At2_g - At1_g;
|
||||||
|
At2_b = 3*At2_b - At1_b;
|
||||||
|
|
||||||
|
// extract solutions and decide solvability
|
||||||
|
xx = akku >> 16;
|
||||||
|
yy = (akku >> 8) & 0xff;
|
||||||
|
xy = (akku >> 0) & 0xff;
|
||||||
|
|
||||||
|
f = 3.0f / 255.0f / (xx*yy - xy*xy);
|
||||||
|
|
||||||
|
max16 = stb__Quantize5((At1_r*yy - At2_r * xy) * f) << 11;
|
||||||
|
max16 |= stb__Quantize6((At1_g*yy - At2_g * xy) * f) << 5;
|
||||||
|
max16 |= stb__Quantize5((At1_b*yy - At2_b * xy) * f) << 0;
|
||||||
|
|
||||||
|
min16 = stb__Quantize5((At2_r*xx - At1_r * xy) * f) << 11;
|
||||||
|
min16 |= stb__Quantize6((At2_g*xx - At1_g * xy) * f) << 5;
|
||||||
|
min16 |= stb__Quantize5((At2_b*xx - At1_b * xy) * f) << 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*pmin16 = min16;
|
||||||
|
*pmax16 = max16;
|
||||||
|
return oldMin != min16 || oldMax != max16;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Color block compression
|
||||||
|
static void stb__CompressColorBlock(unsigned char *dest, unsigned char *block, int mode)
|
||||||
|
{
|
||||||
|
unsigned int mask;
|
||||||
|
int i;
|
||||||
|
int refinecount;
|
||||||
|
unsigned short max16, min16;
|
||||||
|
unsigned char color[4*4];
|
||||||
|
|
||||||
|
refinecount = (mode & STB_DXT_HIGHQUAL) ? 2 : 1;
|
||||||
|
|
||||||
|
// check if block is constant
|
||||||
|
for (i=1;i<16;i++)
|
||||||
|
if (((unsigned int *) block)[i] != ((unsigned int *) block)[0])
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(i == 16) { // constant color
|
||||||
|
int r = block[0], g = block[1], b = block[2];
|
||||||
|
mask = 0xaaaaaaaa;
|
||||||
|
max16 = (stb__OMatch5[r][0]<<11) | (stb__OMatch6[g][0]<<5) | stb__OMatch5[b][0];
|
||||||
|
min16 = (stb__OMatch5[r][1]<<11) | (stb__OMatch6[g][1]<<5) | stb__OMatch5[b][1];
|
||||||
|
} else {
|
||||||
|
// first step: PCA+map along principal axis
|
||||||
|
stb__OptimizeColorsBlock(block,&max16,&min16);
|
||||||
|
if (max16 != min16) {
|
||||||
|
stb__EvalColors(color,max16,min16);
|
||||||
|
mask = stb__MatchColorsBlock(block,color);
|
||||||
|
} else
|
||||||
|
mask = 0;
|
||||||
|
|
||||||
|
// third step: refine (multiple times if requested)
|
||||||
|
for (i=0;i<refinecount;i++) {
|
||||||
|
unsigned int lastmask = mask;
|
||||||
|
|
||||||
|
if (stb__RefineBlock(block,&max16,&min16,mask)) {
|
||||||
|
if (max16 != min16) {
|
||||||
|
stb__EvalColors(color,max16,min16);
|
||||||
|
mask = stb__MatchColorsBlock(block,color);
|
||||||
|
} else {
|
||||||
|
mask = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mask == lastmask)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the color block
|
||||||
|
if(max16 < min16)
|
||||||
|
{
|
||||||
|
unsigned short t = min16;
|
||||||
|
min16 = max16;
|
||||||
|
max16 = t;
|
||||||
|
mask ^= 0x55555555;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest[0] = (unsigned char) (max16);
|
||||||
|
dest[1] = (unsigned char) (max16 >> 8);
|
||||||
|
dest[2] = (unsigned char) (min16);
|
||||||
|
dest[3] = (unsigned char) (min16 >> 8);
|
||||||
|
dest[4] = (unsigned char) (mask);
|
||||||
|
dest[5] = (unsigned char) (mask >> 8);
|
||||||
|
dest[6] = (unsigned char) (mask >> 16);
|
||||||
|
dest[7] = (unsigned char) (mask >> 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alpha block compression (this is easy for a change)
|
||||||
|
static void stb__CompressAlphaBlock(unsigned char *dest,unsigned char *src, int stride)
|
||||||
|
{
|
||||||
|
int i,dist,bias,dist4,dist2,bits,mask;
|
||||||
|
|
||||||
|
// find min/max color
|
||||||
|
int mn,mx;
|
||||||
|
mn = mx = src[0];
|
||||||
|
|
||||||
|
for (i=1;i<16;i++)
|
||||||
|
{
|
||||||
|
if (src[i*stride] < mn) mn = src[i*stride];
|
||||||
|
else if (src[i*stride] > mx) mx = src[i*stride];
|
||||||
|
}
|
||||||
|
|
||||||
|
// encode them
|
||||||
|
dest[0] = (unsigned char)mx;
|
||||||
|
dest[1] = (unsigned char)mn;
|
||||||
|
dest += 2;
|
||||||
|
|
||||||
|
// determine bias and emit color indices
|
||||||
|
// given the choice of mx/mn, these indices are optimal:
|
||||||
|
// http://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/
|
||||||
|
dist = mx-mn;
|
||||||
|
dist4 = dist*4;
|
||||||
|
dist2 = dist*2;
|
||||||
|
bias = (dist < 8) ? (dist - 1) : (dist/2 + 2);
|
||||||
|
bias -= mn * 7;
|
||||||
|
bits = 0,mask=0;
|
||||||
|
|
||||||
|
for (i=0;i<16;i++) {
|
||||||
|
int a = src[i*stride]*7 + bias;
|
||||||
|
int ind,t;
|
||||||
|
|
||||||
|
// select index. this is a "linear scale" lerp factor between 0 (val=min) and 7 (val=max).
|
||||||
|
t = (a >= dist4) ? -1 : 0; ind = t & 4; a -= dist4 & t;
|
||||||
|
t = (a >= dist2) ? -1 : 0; ind += t & 2; a -= dist2 & t;
|
||||||
|
ind += (a >= dist);
|
||||||
|
|
||||||
|
// turn linear scale into DXT index (0/1 are extremal pts)
|
||||||
|
ind = -ind & 7;
|
||||||
|
ind ^= (2 > ind);
|
||||||
|
|
||||||
|
// write index
|
||||||
|
mask |= ind << bits;
|
||||||
|
if((bits += 3) >= 8) {
|
||||||
|
*dest++ = (unsigned char)mask;
|
||||||
|
mask >>= 8;
|
||||||
|
bits -= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void stb_compress_dxt_block(unsigned char *dest, const unsigned char *src, int alpha, int mode)
|
||||||
|
{
|
||||||
|
unsigned char data[16][4];
|
||||||
|
if (alpha) {
|
||||||
|
int i;
|
||||||
|
stb__CompressAlphaBlock(dest,(unsigned char*) src+3, 4);
|
||||||
|
dest += 8;
|
||||||
|
// make a new copy of the data in which alpha is opaque,
|
||||||
|
// because code uses a fast test for color constancy
|
||||||
|
memcpy(data, src, 4*16);
|
||||||
|
for (i=0; i < 16; ++i)
|
||||||
|
data[i][3] = 255;
|
||||||
|
src = &data[0][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
stb__CompressColorBlock(dest,(unsigned char*) src,mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stb_compress_bc4_block(unsigned char *dest, const unsigned char *src)
|
||||||
|
{
|
||||||
|
stb__CompressAlphaBlock(dest,(unsigned char*) src, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void stb_compress_bc5_block(unsigned char *dest, const unsigned char *src)
|
||||||
|
{
|
||||||
|
stb__CompressAlphaBlock(dest,(unsigned char*) src,2);
|
||||||
|
stb__CompressAlphaBlock(dest + 8,(unsigned char*) src+1,2);
|
||||||
|
}
|
||||||
|
#endif // STB_DXT_IMPLEMENTATION
|
||||||
|
|
||||||
|
// Compile with STB_DXT_IMPLEMENTATION and STB_DXT_GENERATE_TABLES
|
||||||
|
// defined to generate the tables above.
|
||||||
|
#ifdef STB_DXT_GENERATE_TABLES
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
const char *omatch_names[] = { "stb__OMatch5", "stb__OMatch6" };
|
||||||
|
int dequant_mults[2] = { 33*4, 65 }; // .4 fixed-point dequant multipliers
|
||||||
|
|
||||||
|
// optimal endpoint tables
|
||||||
|
for (i = 0; i < 2; ++i) {
|
||||||
|
int dequant = dequant_mults[i];
|
||||||
|
int size = i ? 64 : 32;
|
||||||
|
printf("static const unsigned char %s[256][2] = {\n", omatch_names[i]);
|
||||||
|
for (int j = 0; j < 256; ++j) {
|
||||||
|
int mn, mx;
|
||||||
|
int best_mn = 0, best_mx = 0;
|
||||||
|
int best_err = 256 * 100;
|
||||||
|
for (mn=0;mn<size;mn++) {
|
||||||
|
for (mx=0;mx<size;mx++) {
|
||||||
|
int mine = (mn * dequant) >> 4;
|
||||||
|
int maxe = (mx * dequant) >> 4;
|
||||||
|
int err = abs(stb__Lerp13(maxe, mine) - j) * 100;
|
||||||
|
|
||||||
|
// DX10 spec says that interpolation must be within 3% of "correct" result,
|
||||||
|
// add this as error term. Normally we'd expect a random distribution of
|
||||||
|
// +-1.5% error, but nowhere in the spec does it say that the error has to be
|
||||||
|
// unbiased - better safe than sorry.
|
||||||
|
err += abs(maxe - mine) * 3;
|
||||||
|
|
||||||
|
if(err < best_err) {
|
||||||
|
best_mn = mn;
|
||||||
|
best_mx = mx;
|
||||||
|
best_err = err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((j % 8) == 0) printf(" "); // 2 spaces, third is done below
|
||||||
|
printf(" { %2d, %2d },", best_mx, best_mn);
|
||||||
|
if ((j % 8) == 7) printf("\n");
|
||||||
|
}
|
||||||
|
printf("};\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
This software is available under 2 licenses -- choose whichever you prefer.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE A - MIT License
|
||||||
|
Copyright (c) 2017 Sean Barrett
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
ALTERNATIVE B - Public Domain (www.unlicense.org)
|
||||||
|
This is free and unencumbered software released into the public domain.
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
||||||
|
software, either in source code form or as a compiled binary, for any purpose,
|
||||||
|
commercial or non-commercial, and by any means.
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors of this
|
||||||
|
software dedicate any and all copyright interest in the software to the public
|
||||||
|
domain. We make this dedication for the benefit of the public at large and to
|
||||||
|
the detriment of our heirs and successors. We intend this dedication to be an
|
||||||
|
overt act of relinquishment in perpetuity of all present and future rights to
|
||||||
|
this software under copyright law.
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
7988
stb_image.h
Normal file
7988
stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
122
stb_image_common.h
Normal file
122
stb_image_common.h
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
#ifndef STB_IMAGE_COMMON_H
|
||||||
|
#define STB_IMAGE_COMMON_H
|
||||||
|
|
||||||
|
#include "stb_image.h"
|
||||||
|
#include "stb_image_write.h"
|
||||||
|
|
||||||
|
// Simple growable memory buffer for stbi write callbacks
|
||||||
|
typedef struct {
|
||||||
|
unsigned char *data;
|
||||||
|
size_t size;
|
||||||
|
size_t cap;
|
||||||
|
} stbi_mem_buf;
|
||||||
|
|
||||||
|
static void stbi_mem_write(void *context, void *data, int size)
|
||||||
|
{
|
||||||
|
stbi_mem_buf *buf = (stbi_mem_buf*)context;
|
||||||
|
size_t needed = buf->size + (size_t)size;
|
||||||
|
if (needed > buf->cap) {
|
||||||
|
size_t new_cap = buf->cap ? buf->cap * 2 : 4096;
|
||||||
|
while (new_cap < needed) new_cap *= 2;
|
||||||
|
unsigned char *new_data = realloc(buf->data, new_cap);
|
||||||
|
if (!new_data) return;
|
||||||
|
buf->data = new_data;
|
||||||
|
buf->cap = new_cap;
|
||||||
|
}
|
||||||
|
memcpy(buf->data + buf->size, data, (size_t)size);
|
||||||
|
buf->size += (size_t)size;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define IMAGE_DECODER(fmt) \
|
||||||
|
JSValue js_##fmt##_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv) \
|
||||||
|
{ \
|
||||||
|
size_t len; \
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]); \
|
||||||
|
if (raw == NULL) return JS_EXCEPTION; \
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get " #fmt " data from blob"); \
|
||||||
|
\
|
||||||
|
int n, width, height; \
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4); \
|
||||||
|
\
|
||||||
|
if (!data) \
|
||||||
|
return JS_ThrowReferenceError(js, "failed to decode " #fmt ": %s", stbi_failure_reason()); \
|
||||||
|
\
|
||||||
|
if (width <= 0 || height <= 0) { \
|
||||||
|
stbi_image_free(data); \
|
||||||
|
return JS_ThrowReferenceError(js, "decoded " #fmt " has invalid size: %dx%d", width, height); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
int pitch = width * 4; \
|
||||||
|
size_t pixels_size = pitch * height; \
|
||||||
|
\
|
||||||
|
JSValue obj = JS_NewObject(js); \
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width)); \
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height)); \
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32")); \
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch)); \
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size)); \
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8)); \
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0)); \
|
||||||
|
\
|
||||||
|
stbi_image_free(data); \
|
||||||
|
return obj; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define IMAGE_ENCODER(fmt) \
|
||||||
|
JSValue js_##fmt##_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv) \
|
||||||
|
{ \
|
||||||
|
if (argc < 1) \
|
||||||
|
return JS_ThrowTypeError(js, #fmt ".encode requires an image object"); \
|
||||||
|
\
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width"); \
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height"); \
|
||||||
|
\
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) { \
|
||||||
|
JS_FreeValue(js, width_val); \
|
||||||
|
JS_FreeValue(js, height_val); \
|
||||||
|
return JS_ThrowTypeError(js, #fmt ".encode requires width and height properties"); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
int width, height; \
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) { \
|
||||||
|
JS_FreeValue(js, width_val); \
|
||||||
|
JS_FreeValue(js, height_val); \
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers"); \
|
||||||
|
} \
|
||||||
|
JS_FreeValue(js, width_val); \
|
||||||
|
JS_FreeValue(js, height_val); \
|
||||||
|
\
|
||||||
|
if (width < 1 || height < 1) \
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 1"); \
|
||||||
|
\
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels"); \
|
||||||
|
size_t pixel_len; \
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val); \
|
||||||
|
JS_FreeValue(js, pixels_val); \
|
||||||
|
\
|
||||||
|
if (pixel_data == NULL) return JS_EXCEPTION; \
|
||||||
|
if (!pixel_data) return JS_ThrowTypeError(js, "pixels blob has no data"); \
|
||||||
|
\
|
||||||
|
size_t required_size = width * height * 4; \
|
||||||
|
if (pixel_len < required_size) \
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small (need %zu, got %zu)", required_size, pixel_len); \
|
||||||
|
\
|
||||||
|
stbi_mem_buf buf = {0}; \
|
||||||
|
stbi_write_##fmt##_to_func(stbi_mem_write, &buf, width, height, 4, pixel_data); \
|
||||||
|
\
|
||||||
|
if (!buf.data || buf.size == 0) { \
|
||||||
|
if (buf.data) free(buf.data); \
|
||||||
|
return JS_ThrowInternalError(js, "failed to encode " #fmt); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
JSValue result = JS_NewObject(js); \
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width)); \
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height)); \
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, #fmt)); \
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size)); \
|
||||||
|
\
|
||||||
|
free(buf.data); \
|
||||||
|
return result; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
1724
stb_image_write.h
Normal file
1724
stb_image_write.h
Normal file
File diff suppressed because it is too large
Load Diff
103
tga.c
Normal file
103
tga.c
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
#include "cell.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stb_image_common.h"
|
||||||
|
|
||||||
|
// TGA decoding
|
||||||
|
JSValue js_tga_decode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
void *raw = js_get_blob_data(js, &len, argv[0]);
|
||||||
|
if (raw == NULL) return JS_EXCEPTION;
|
||||||
|
if (!raw) return JS_ThrowReferenceError(js, "could not get TGA data from blob");
|
||||||
|
|
||||||
|
int n, width, height;
|
||||||
|
void *data = stbi_load_from_memory(raw, len, &width, &height, &n, 4);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return JS_ThrowReferenceError(js, "failed to decode TGA: %s", stbi_failure_reason());
|
||||||
|
|
||||||
|
if (width <= 0 || height <= 0) {
|
||||||
|
stbi_image_free(data);
|
||||||
|
return JS_ThrowReferenceError(js, "decoded TGA has invalid size: %dx%d", width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pitch = width * 4;
|
||||||
|
size_t pixels_size = pitch * height;
|
||||||
|
|
||||||
|
JSValue obj = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, obj, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, obj, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "rgba32"));
|
||||||
|
JS_SetPropertyStr(js, obj, "pitch", JS_NewInt32(js, pitch));
|
||||||
|
JS_SetPropertyStr(js, obj, "pixels", js_new_blob_stoned_copy(js, data, pixels_size));
|
||||||
|
JS_SetPropertyStr(js, obj, "depth", JS_NewInt32(js, 8));
|
||||||
|
JS_SetPropertyStr(js, obj, "hdr", JS_NewBool(js, 0));
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TGA encoding
|
||||||
|
JSValue js_tga_encode(JSContext *js, JSValue this_val, int argc, JSValueConst *argv)
|
||||||
|
{
|
||||||
|
if (argc < 1)
|
||||||
|
return JS_ThrowTypeError(js, "tga.encode requires an image object");
|
||||||
|
|
||||||
|
JSValue width_val = JS_GetPropertyStr(js, argv[0], "width");
|
||||||
|
JSValue height_val = JS_GetPropertyStr(js, argv[0], "height");
|
||||||
|
|
||||||
|
if (JS_IsNull(width_val) || JS_IsNull(height_val)) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "tga.encode requires width and height properties");
|
||||||
|
}
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
if (JS_ToInt32(js, &width, width_val) < 0 || JS_ToInt32(js, &height, height_val) < 0) {
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
return JS_ThrowTypeError(js, "width and height must be numbers");
|
||||||
|
}
|
||||||
|
JS_FreeValue(js, width_val);
|
||||||
|
JS_FreeValue(js, height_val);
|
||||||
|
|
||||||
|
if (width < 1 || height < 1)
|
||||||
|
return JS_ThrowRangeError(js, "width and height must be at least 1");
|
||||||
|
|
||||||
|
JSValue pixels_val = JS_GetPropertyStr(js, argv[0], "pixels");
|
||||||
|
size_t pixel_len;
|
||||||
|
void *pixel_data = js_get_blob_data(js, &pixel_len, pixels_val);
|
||||||
|
JS_FreeValue(js, pixels_val);
|
||||||
|
|
||||||
|
if (pixel_data == NULL) return JS_EXCEPTION;
|
||||||
|
if (!pixel_data) return JS_ThrowTypeError(js, "pixels blob has no data");
|
||||||
|
|
||||||
|
size_t required_size = width * height * 4;
|
||||||
|
if (pixel_len < required_size)
|
||||||
|
return JS_ThrowRangeError(js, "pixels buffer too small (need %zu, got %zu)", required_size, pixel_len);
|
||||||
|
|
||||||
|
stbi_mem_buf buf = {0};
|
||||||
|
stbi_write_tga_to_func(stbi_mem_write, &buf, width, height, 4, pixel_data);
|
||||||
|
|
||||||
|
if (!buf.data || buf.size == 0) {
|
||||||
|
if (buf.data) free(buf.data);
|
||||||
|
return JS_ThrowInternalError(js, "failed to encode TGA");
|
||||||
|
}
|
||||||
|
|
||||||
|
JSValue result = JS_NewObject(js);
|
||||||
|
JS_SetPropertyStr(js, result, "width", JS_NewInt32(js, width));
|
||||||
|
JS_SetPropertyStr(js, result, "height", JS_NewInt32(js, height));
|
||||||
|
JS_SetPropertyStr(js, result, "format", JS_NewString(js, "tga"));
|
||||||
|
JS_SetPropertyStr(js, result, "pixels", js_new_blob_stoned_copy(js, buf.data, buf.size));
|
||||||
|
|
||||||
|
free(buf.data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JSCFunctionListEntry js_tga_funcs[] = {
|
||||||
|
MIST_FUNC_DEF(tga, decode, 1),
|
||||||
|
MIST_FUNC_DEF(tga, encode, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
CELL_USE_FUNCS(js_tga_funcs)
|
||||||
Reference in New Issue
Block a user