Updated external libraries

This commit is contained in:
raysan5
2021-10-06 21:13:17 +02:00
parent 8722ff7043
commit 700d448d75
14 changed files with 3415 additions and 1124 deletions

351
src/external/msf_gif.h vendored
View File

@ -13,19 +13,31 @@ USAGE EXAMPLE:
int width = 480, height = 320, centisecondsPerFrame = 5, bitDepth = 16;
MsfGifState gifState = {};
// msf_gif_bgra_flag = true; //optionally, set this flag if your pixels are in BGRA format instead of RGBA
// msf_gif_alpha_threshold = 128; //optionally, enable transparency (see function documentation below for details)
msf_gif_begin(&gifState, width, height);
msf_gif_frame(&gifState, ..., centisecondsPerFrame, bitDepth, width * 4); //frame 1
msf_gif_frame(&gifState, ..., centisecondsPerFrame, bitDepth, width * 4); //frame 2
msf_gif_frame(&gifState, ..., centisecondsPerFrame, bitDepth, width * 4); //frame 3, etc...
MsfGifResult result = msf_gif_end(&gifState);
FILE * fp = fopen("MyGif.gif", "wb");
fwrite(result.data, result.dataSize, 1, fp);
fclose(fp);
if (result.data) {
FILE * fp = fopen("MyGif.gif", "wb");
fwrite(result.data, result.dataSize, 1, fp);
fclose(fp);
}
msf_gif_free(result);
Detailed function documentation can be found in the header section below.
ERROR HANDLING:
If memory allocation fails, the functions will signal the error via their return values.
If one function call fails, the library will free all of its allocations,
and all subsequent calls will safely no-op and return 0 until the next call to `msf_gif_begin()`.
Therefore, it's safe to check only the return value of `msf_gif_end()`.
REPLACING MALLOC:
This library uses malloc+realloc+free internally for memory allocation.
@ -39,10 +51,20 @@ REPLACING MALLOC:
If your allocator needs a context pointer, you can set the `customAllocatorContext` field of the MsfGifState struct
before calling msf_gif_begin(), and it will be passed to all subsequent allocator macro calls.
The maximum number of bytes the library will allocate to encode a single gif is bounded by the following formula:
`(2 * 1024 * 1024) + (width * height * 8) + ((1024 + width * height * 1.5) * 3 * frameCount)`
The peak heap memory usage in bytes, if using a general-purpose heap allocator, is bounded by the following formula:
`(2 * 1024 * 1024) + (width * height * 9.5) + 1024 + (16 * frameCount) + (2 * sizeOfResultingGif)
See end of file for license information.
*/
//version 2.1
//version 2.2
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// HEADER ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef MSF_GIF_H
#define MSF_GIF_H
@ -63,12 +85,24 @@ typedef struct { //internal use
int depth, count, rbits, gbits, bbits;
} MsfCookedFrame;
typedef struct MsfGifBuffer {
struct MsfGifBuffer * next;
size_t size;
uint8_t data[1];
} MsfGifBuffer;
typedef size_t (* MsfGifFileWriteFunc) (const void * buffer, size_t size, size_t count, void * stream);
typedef struct {
MsfGifFileWriteFunc fileWriteFunc;
void * fileWriteData;
MsfCookedFrame previousFrame;
uint8_t * listHead;
uint8_t * listTail;
MsfCookedFrame currentFrame;
int16_t * lzwMem;
MsfGifBuffer * listHead;
MsfGifBuffer * listTail;
int width, height;
void * customAllocatorContext;
int framesSubmitted; //needed for transparency to work correctly (because we reach into the previous frame)
} MsfGifState;
#ifdef __cplusplus
@ -83,7 +117,8 @@ extern "C" {
int msf_gif_begin(MsfGifState * handle, int width, int height);
/**
* @param pixelData Pointer to raw framebuffer data. Rows must be contiguous in memory, in RGBA8 format.
* @param pixelData Pointer to raw framebuffer data. Rows must be contiguous in memory, in RGBA8 format
* (or BGRA8 if you have set `msf_gif_bgra_flag = true`).
* Note: This function does NOT free `pixelData`. You must free it yourself afterwards.
* @param centiSecondsPerFrame How many hundredths of a second this frame should be displayed for.
* Note: This being specified in centiseconds is a limitation of the GIF format.
@ -111,6 +146,35 @@ MsfGifResult msf_gif_end(MsfGifState * handle);
*/
void msf_gif_free(MsfGifResult result);
//The gif format only supports 1-bit transparency, meaning a pixel will either be fully transparent or fully opaque.
//Pixels with an alpha value less than the alpha threshold will be treated as transparent.
//To enable exporting transparent gifs, set it to a value between 1 and 255 (inclusive) before calling msf_gif_frame().
//Setting it to 0 causes the alpha channel to be ignored. Its initial value is 0.
extern int msf_gif_alpha_threshold;
//Set `msf_gif_bgra_flag = true` before calling `msf_gif_frame()` if your pixels are in BGRA byte order instead of RBGA.
extern int msf_gif_bgra_flag;
//TO-FILE FUNCTIONS
//These functions are equivalent to the ones above, but they write results to a file incrementally,
//instead of building a buffer in memory. This can result in lower memory usage when saving large gifs,
//because memory usage is bounded by only the size of a single frame, and is not dependent on the number of frames.
//There is currently no reason to use these unless you are on a memory-constrained platform.
//If in doubt about which API to use, for now you should use the normal (non-file) functions above.
//The signature of MsfGifFileWriteFunc matches fwrite for convenience, so that you can use the C file API like so:
// FILE * fp = fopen("MyGif.gif", "wb");
// msf_gif_begin_to_file(&handle, width, height, (MsfGifFileWriteFunc) fwrite, (void *) fp);
// msf_gif_frame_to_file(...)
// msf_gif_end_to_file(&handle);
// fclose(fp);
//If you use a custom file write function, you must take care to return the same values that fwrite() would return.
//Note that all three functions will potentially write to the file.
int msf_gif_begin_to_file(MsfGifState * handle, int width, int height, MsfGifFileWriteFunc func, void * filePointer);
int msf_gif_frame_to_file(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPerFame, int maxBitDepth, int pitchInBytes);
int msf_gif_end_to_file(MsfGifState * handle); //returns 0 on error and non-zero on success
#ifdef __cplusplus
}
#endif //__cplusplus
@ -125,10 +189,6 @@ void msf_gif_free(MsfGifResult result);
#ifndef MSF_GIF_ALREADY_IMPLEMENTED_IN_THIS_TRANSLATION_UNIT
#define MSF_GIF_ALREADY_IMPLEMENTED_IN_THIS_TRANSLATION_UNIT
#ifndef MSF_GIF_BUFFER_INIT_SIZE
#define MSF_GIF_BUFFER_INIT_SIZE 1024 * 1024 * 4 //4MB by default, you can increase this if you want to realloc less
#endif
//ensure the library user has either defined all of malloc/realloc/free, or none
#if defined(MSF_GIF_MALLOC) && defined(MSF_GIF_REALLOC) && defined(MSF_GIF_FREE) //ok
#elif !defined(MSF_GIF_MALLOC) && !defined(MSF_GIF_REALLOC) && !defined(MSF_GIF_FREE) //ok
@ -189,13 +249,21 @@ static inline int msf_imax(int a, int b) { return b < a? a : b; }
#include <emmintrin.h>
#endif
static MsfCookedFrame msf_cook_frame(void * allocContext, uint8_t * raw, uint8_t * used,
int width, int height, int pitch, int depth)
int msf_gif_alpha_threshold = 0;
int msf_gif_bgra_flag = 0;
static void msf_cook_frame(MsfCookedFrame * frame, uint8_t * raw, uint8_t * used,
int width, int height, int pitch, int depth)
{ MsfTimeFunc
//bit depth for each channel
const static int rdepths[17] = { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5 };
const static int gdepths[17] = { 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6 };
const static int bdepths[17] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5 };
const static int rdepthsArray[17] = { 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5 };
const static int gdepthsArray[17] = { 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6 };
const static int bdepthsArray[17] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5 };
//this extra level of indirection looks unnecessary but we need to explicitly decay the arrays to pointers
//in order to be able to swap them because of C's annoying not-quite-pointers, not-quite-value-types stack arrays.
const int * rdepths = msf_gif_bgra_flag? bdepthsArray : rdepthsArray;
const int * gdepths = gdepthsArray;
const int * bdepths = msf_gif_bgra_flag? rdepthsArray : bdepthsArray;
const static int ditherKernel[16] = {
0 << 12, 8 << 12, 2 << 12, 10 << 12,
@ -204,13 +272,11 @@ static MsfCookedFrame msf_cook_frame(void * allocContext, uint8_t * raw, uint8_t
15 << 12, 7 << 12, 13 << 12, 5 << 12,
};
uint32_t * cooked = (uint32_t *) MSF_GIF_MALLOC(allocContext, width * height * sizeof(uint32_t));
if (!cooked) { MsfCookedFrame blank = {0}; return blank; }
uint32_t * cooked = frame->pixels;
int count = 0;
MsfTimeLoop("do") do {
int rbits = rdepths[depth], gbits = gdepths[depth], bbits = bdepths[depth];
int paletteSize = 1 << (rbits + gbits + bbits);
int paletteSize = (1 << (rbits + gbits + bbits)) + 1;
memset(used, 0, paletteSize * sizeof(uint8_t));
//TODO: document what this math does and why it's correct
@ -230,7 +296,6 @@ static MsfCookedFrame msf_cook_frame(void * allocContext, uint8_t * raw, uint8_t
#if (defined (__SSE2__) || defined (_M_X64) || _M_IX86_FP == 2) && !defined(MSF_GIF_NO_SSE2)
__m128i k = _mm_loadu_si128((__m128i *) &ditherKernel[(y & 3) * 4]);
__m128i k2 = _mm_or_si128(_mm_srli_epi32(k, rbits), _mm_slli_epi32(_mm_srli_epi32(k, bbits), 16));
// MsfTimeLoop("SIMD")
for (; x < width - 3; x += 4) {
uint8_t * pixels = &raw[y * pitch + x * 4];
__m128i p = _mm_loadu_si128((__m128i *) pixels);
@ -246,17 +311,30 @@ static MsfCookedFrame msf_cook_frame(void * allocContext, uint8_t * raw, uint8_t
__m128i g2 = _mm_adds_epu16(g1, _mm_srli_epi32(k, gbits));
__m128i g3 = _mm_and_si128(_mm_srli_epi32(g2, 16 - rbits - gbits), _mm_set1_epi32(gmask));
__m128i out = _mm_or_si128(_mm_or_si128(r3, g3), b3);
//mask in transparency based on threshold
//NOTE: we can theoretically do a sub instead of srli by doing an unsigned compare via bias
// to maybe save a TINY amount of throughput? but lol who cares maybe I'll do it later -m
__m128i invAlphaMask = _mm_cmplt_epi32(_mm_srli_epi32(p, 24), _mm_set1_epi32(msf_gif_alpha_threshold));
out = _mm_or_si128(_mm_and_si128(invAlphaMask, _mm_set1_epi32(paletteSize - 1)), _mm_andnot_si128(invAlphaMask, out));
//TODO: does storing this as a __m128i then reading it back as a uint32_t violate strict aliasing?
uint32_t * c = &cooked[y * width + x];
__m128i out = _mm_or_si128(_mm_or_si128(r3, g3), b3);
_mm_storeu_si128((__m128i *) c, out);
}
#endif
//scalar cleanup loop
// MsfTimeLoop("scalar")
for (; x < width; ++x) {
uint8_t * p = &raw[y * pitch + x * 4];
//transparent pixel if alpha is low
if (p[3] < msf_gif_alpha_threshold) {
cooked[y * width + x] = paletteSize - 1;
continue;
}
int dx = x & 3, dy = y & 3;
int k = ditherKernel[dy * 4 + dx];
cooked[y * width + x] =
@ -267,30 +345,25 @@ static MsfCookedFrame msf_cook_frame(void * allocContext, uint8_t * raw, uint8_t
}
count = 0;
MsfTimeLoop("mark and count") for (int i = 0; i < width * height; ++i) {
MsfTimeLoop("mark") for (int i = 0; i < width * height; ++i) {
used[cooked[i]] = 1;
}
//count used colors
MsfTimeLoop("count") for (int j = 0; j < paletteSize; ++j) {
//count used colors, transparent is ignored
MsfTimeLoop("count") for (int j = 0; j < paletteSize - 1; ++j) {
count += used[j];
}
} while (count >= 256 && --depth);
MsfCookedFrame ret = { cooked, depth, count, rdepths[depth], gdepths[depth], bdepths[depth] };
return ret;
*frame = ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Frame Compression ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct {
uint8_t * next;
size_t size;
} MsfBufferHeader;
static inline int msf_put_code(uint8_t * * writeHead, uint32_t * blockBits, int len, uint32_t code) {
static inline void msf_put_code(uint8_t * * writeHead, uint32_t * blockBits, int len, uint32_t code) {
//insert new code into block buffer
int idx = *blockBits / 8;
int bit = *blockBits % 8;
@ -308,8 +381,6 @@ static inline int msf_put_code(uint8_t * * writeHead, uint32_t * blockBits, int
(*writeHead)[0] = 255;
memset((*writeHead) + 4, 0, 256);
}
return 1;
}
typedef struct {
@ -324,30 +395,29 @@ static inline void msf_lzw_reset(MsfStridedList * lzw, int tableSize, int stride
lzw->stride = stride;
}
static uint8_t * msf_compress_frame(void * allocContext, int width, int height, int centiSeconds,
MsfCookedFrame frame, MsfCookedFrame previous, uint8_t * used)
static MsfGifBuffer * msf_compress_frame(void * allocContext, int width, int height, int centiSeconds,
MsfCookedFrame frame, MsfGifState * handle, uint8_t * used, int16_t * lzwMem)
{ MsfTimeFunc
//NOTE: we reserve enough memory for theoretical the worst case upfront because it's a reasonable amount,
// and prevents us from ever having to check size or realloc during compression
int maxBufSize = sizeof(MsfBufferHeader) + 32 + 256 * 3 + width * height * 3 / 2; //headers + color table + data
uint8_t * allocation = (uint8_t *) MSF_GIF_MALLOC(allocContext, maxBufSize);
if (!allocation) { return NULL; }
uint8_t * writeBase = allocation + sizeof(MsfBufferHeader);
uint8_t * writeHead = writeBase;
int lzwAllocSize = 4096 * (frame.count + 1) * sizeof(int16_t);
MsfStridedList lzw = { (int16_t *) MSF_GIF_MALLOC(allocContext, lzwAllocSize) };
if (!lzw.data) { MSF_GIF_FREE(allocContext, allocation, maxBufSize); return NULL; }
int maxBufSize = offsetof(MsfGifBuffer, data) + 32 + 256 * 3 + width * height * 3 / 2; //headers + color table + data
MsfGifBuffer * buffer = (MsfGifBuffer *) MSF_GIF_MALLOC(allocContext, maxBufSize);
if (!buffer) { return NULL; }
uint8_t * writeHead = buffer->data;
MsfStridedList lzw = { lzwMem };
//allocate tlb
int totalBits = frame.rbits + frame.gbits + frame.bbits;
int tlbSize = 1 << totalBits;
uint8_t tlb[1 << 16]; //only 64k, so stack allocating is fine
int tlbSize = (1 << totalBits) + 1;
uint8_t tlb[(1 << 16) + 1]; //only 64k, so stack allocating is fine
//generate palette
typedef struct { uint8_t r, g, b; } Color3;
Color3 table[256] = { {0} };
int tableIdx = 1; //we start counting at 1 because 0 is the transparent color
MsfTimeLoop("table") for (int i = 0; i < tlbSize; ++i) {
//transparent is always last in the table
tlb[tlbSize-1] = 0;
MsfTimeLoop("table") for (int i = 0; i < tlbSize-1; ++i) {
if (used[i]) {
tlb[i] = tableIdx;
int rmask = (1 << frame.rbits) - 1;
@ -363,19 +433,32 @@ static uint8_t * msf_compress_frame(void * allocContext, int width, int height,
table[tableIdx].r = r | r >> frame.rbits | r >> (frame.rbits * 2) | r >> (frame.rbits * 3);
table[tableIdx].g = g | g >> frame.gbits | g >> (frame.gbits * 2) | g >> (frame.gbits * 3);
table[tableIdx].b = b | b >> frame.bbits | b >> (frame.bbits * 2) | b >> (frame.bbits * 3);
if (msf_gif_bgra_flag) {
uint8_t temp = table[tableIdx].r;
table[tableIdx].r = table[tableIdx].b;
table[tableIdx].b = temp;
}
++tableIdx;
}
}
int hasTransparentPixels = used[tlbSize-1];
//SPEC: "Because of some algorithmic constraints however, black & white images which have one color bit
// must be indicated as having a code size of 2."
int tableBits = msf_imax(2, msf_bit_log(tableIdx - 1));
int tableSize = 1 << tableBits;
//NOTE: we don't just compare `depth` field here because it will be wrong for the first frame and we will segfault
MsfCookedFrame previous = handle->previousFrame;
int hasSamePal = frame.rbits == previous.rbits && frame.gbits == previous.gbits && frame.bbits == previous.bbits;
int framesCompatible = hasSamePal && !hasTransparentPixels;
//NOTE: because __attribute__((__packed__)) is annoyingly compiler-specific, we do this unreadable weirdness
char headerBytes[19] = "\x21\xF9\x04\x05\0\0\0\0" "\x2C\0\0\0\0\0\0\0\0\x80";
//NOTE: we need to check the frame number because if we reach into the buffer prior to the first frame,
// we'll just clobber the file header instead, which is a bug
if (hasTransparentPixels && handle->framesSubmitted > 0) {
handle->listTail->data[3] = 0x09; //set the previous frame's disposal to background, so transparency is possible
}
memcpy(&headerBytes[4], &centiSeconds, 2);
memcpy(&headerBytes[13], &width, 2);
memcpy(&headerBytes[15], &height, 2);
@ -397,12 +480,10 @@ static uint8_t * msf_compress_frame(void * allocContext, int width, int height,
msf_lzw_reset(&lzw, tableSize, tableIdx);
msf_put_code(&writeHead, &blockBits, msf_bit_log(lzw.len - 1), tableSize);
int lastCode = hasSamePal && frame.pixels[0] == previous.pixels[0]? 0 : tlb[frame.pixels[0]];
int lastCode = framesCompatible && frame.pixels[0] == previous.pixels[0]? 0 : tlb[frame.pixels[0]];
MsfTimeLoop("compress") for (int i = 1; i < width * height; ++i) {
//PERF: branching vs. branchless version of this line is observed to have no discernable impact on speed
int color = hasSamePal && frame.pixels[i] == previous.pixels[i]? 0 : tlb[frame.pixels[i]];
//PERF: branchless version must use && otherwise it will segfault on frame 1, but it's well-predicted so OK
// int color = (!(hasSamePal && frame.pixels[i] == previous.pixels[i])) * tlb[frame.pixels[i]];
int color = framesCompatible && frame.pixels[i] == previous.pixels[i]? 0 : tlb[frame.pixels[i]];
int code = (&lzw.data[lastCode * lzw.stride])[color];
if (code < 0) {
//write to code stream
@ -424,9 +505,6 @@ static uint8_t * msf_compress_frame(void * allocContext, int width, int height,
}
}
MSF_GIF_FREE(allocContext, lzw.data, lzwAllocSize);
MSF_GIF_FREE(allocContext, previous.pixels, width * height * sizeof(uint32_t));
//write code for leftover index buffer contents, then the end code
msf_put_code(&writeHead, &blockBits, msf_imin(12, msf_bit_log(lzw.len - 1)), lastCode);
msf_put_code(&writeHead, &blockBits, msf_imin(12, msf_bit_log(lzw.len)), tableSize + 1);
@ -439,38 +517,72 @@ static uint8_t * msf_compress_frame(void * allocContext, int width, int height,
}
*writeHead++ = 0; //terminating block
//filling in buffer header and shrink buffer to fit data
MsfBufferHeader * header = (MsfBufferHeader *) allocation;
header->next = NULL;
header->size = writeHead - writeBase;
uint8_t * moved = (uint8_t *) MSF_GIF_REALLOC(allocContext, allocation, maxBufSize, writeHead - allocation);
if (!moved) { MSF_GIF_FREE(allocContext, allocation, maxBufSize); return NULL; }
//fill in buffer header and shrink buffer to fit data
buffer->next = NULL;
buffer->size = writeHead - buffer->data;
MsfGifBuffer * moved =
(MsfGifBuffer *) MSF_GIF_REALLOC(allocContext, buffer, maxBufSize, offsetof(MsfGifBuffer, data) + buffer->size);
if (!moved) { MSF_GIF_FREE(allocContext, buffer, maxBufSize); return NULL; }
return moved;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Incremental API ///
/// To-memory API ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static const int lzwAllocSize = 4096 * 256 * sizeof(int16_t);
//NOTE: by C standard library conventions, freeing NULL should be a no-op,
// but just in case the user's custom free doesn't follow that rule, we do null checks on our end as well.
static void msf_free_gif_state(MsfGifState * handle) {
if (handle->previousFrame.pixels) MSF_GIF_FREE(handle->customAllocatorContext, handle->previousFrame.pixels,
handle->width * handle->height * sizeof(uint32_t));
if (handle->currentFrame.pixels) MSF_GIF_FREE(handle->customAllocatorContext, handle->currentFrame.pixels,
handle->width * handle->height * sizeof(uint32_t));
if (handle->lzwMem) MSF_GIF_FREE(handle->customAllocatorContext, handle->lzwMem, lzwAllocSize);
for (MsfGifBuffer * node = handle->listHead; node;) {
MsfGifBuffer * next = node->next; //NOTE: we have to copy the `next` pointer BEFORE freeing the node holding it
MSF_GIF_FREE(handle->customAllocatorContext, node, offsetof(MsfGifBuffer, data) + node->size);
node = next;
}
handle->listHead = NULL; //this implicitly marks the handle as invalid until the next msf_gif_begin() call
}
int msf_gif_begin(MsfGifState * handle, int width, int height) { MsfTimeFunc
//NOTE: we cannot stomp the entire struct to zero because we must preserve `customAllocatorContext`.
MsfCookedFrame empty = {0}; //god I hate MSVC...
handle->previousFrame = empty;
handle->currentFrame = empty;
handle->width = width;
handle->height = height;
handle->framesSubmitted = 0;
//allocate memory for LZW buffer
//NOTE: Unfortunately we can't just use stack memory for the LZW table because it's 2MB,
// which is more stack space than most operating systems give by default,
// and we can't realistically expect users to be willing to override that just to use our library,
// so we have to allocate this on the heap.
handle->lzwMem = (int16_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, lzwAllocSize);
handle->previousFrame.pixels =
(uint32_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, handle->width * handle->height * sizeof(uint32_t));
handle->currentFrame.pixels =
(uint32_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, handle->width * handle->height * sizeof(uint32_t));
//setup header buffer header (lol)
handle->listHead = (uint8_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, sizeof(MsfBufferHeader) + 32);
if (!handle->listHead) { return 0; }
handle->listHead = (MsfGifBuffer *) MSF_GIF_MALLOC(handle->customAllocatorContext, offsetof(MsfGifBuffer, data) + 32);
if (!handle->listHead || !handle->lzwMem || !handle->previousFrame.pixels || !handle->currentFrame.pixels) {
msf_free_gif_state(handle);
return 0;
}
handle->listTail = handle->listHead;
MsfBufferHeader * header = (MsfBufferHeader *) handle->listHead;
header->next = NULL;
header->size = 32;
handle->listHead->next = NULL;
handle->listHead->size = 32;
//NOTE: because __attribute__((__packed__)) is annoyingly compiler-specific, we do this unreadable weirdness
char headerBytes[33] = "GIF89a\0\0\0\0\x10\0\0" "\x21\xFF\x0BNETSCAPE2.0\x03\x01\0\0\0";
char headerBytes[33] = "GIF89a\0\0\0\0\x70\0\0" "\x21\xFF\x0BNETSCAPE2.0\x03\x01\0\0\0";
memcpy(&headerBytes[6], &width, 2);
memcpy(&headerBytes[8], &height, 2);
memcpy(handle->listHead + sizeof(MsfBufferHeader), headerBytes, 32);
memcpy(handle->listHead->data, headerBytes, 32);
return 1;
}
@ -482,86 +594,83 @@ int msf_gif_frame(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPer
if (pitchInBytes == 0) pitchInBytes = handle->width * 4;
if (pitchInBytes < 0) pixelData -= pitchInBytes * (handle->height - 1);
uint8_t used[1 << 16]; //only 64k, so stack allocating is fine
MsfCookedFrame frame =
msf_cook_frame(handle->customAllocatorContext, pixelData, used, handle->width, handle->height, pitchInBytes,
msf_imin(maxBitDepth, handle->previousFrame.depth + 160 / msf_imax(1, handle->previousFrame.count)));
//TODO: de-duplicate cleanup code
if (!frame.pixels) {
MSF_GIF_FREE(handle->customAllocatorContext,
handle->previousFrame.pixels, handle->width * handle->height * sizeof(uint32_t));
for (uint8_t * node = handle->listHead; node;) {
MsfBufferHeader * header = (MsfBufferHeader *) node;
node = header->next;
MSF_GIF_FREE(handle->customAllocatorContext, header, sizeof(MsfBufferHeader) + header->size);
}
handle->listHead = handle->listTail = NULL;
return 0;
}
uint8_t used[(1 << 16) + 1]; //only 64k, so stack allocating is fine
msf_cook_frame(&handle->currentFrame, pixelData, used, handle->width, handle->height, pitchInBytes,
msf_imin(maxBitDepth, handle->previousFrame.depth + 160 / msf_imax(1, handle->previousFrame.count)));
uint8_t * buffer = msf_compress_frame(handle->customAllocatorContext,
handle->width, handle->height, centiSecondsPerFame, frame, handle->previousFrame, used);
((MsfBufferHeader *) handle->listTail)->next = buffer;
MsfGifBuffer * buffer = msf_compress_frame(handle->customAllocatorContext, handle->width, handle->height,
centiSecondsPerFame, handle->currentFrame, handle, used, handle->lzwMem);
if (!buffer) { msf_free_gif_state(handle); return 0; }
handle->listTail->next = buffer;
handle->listTail = buffer;
if (!buffer) {
MSF_GIF_FREE(handle->customAllocatorContext, frame.pixels, handle->width * handle->height * sizeof(uint32_t));
MSF_GIF_FREE(handle->customAllocatorContext,
handle->previousFrame.pixels, handle->width * handle->height * sizeof(uint32_t));
for (uint8_t * node = handle->listHead; node;) {
MsfBufferHeader * header = (MsfBufferHeader *) node;
node = header->next;
MSF_GIF_FREE(handle->customAllocatorContext, header, sizeof(MsfBufferHeader) + header->size);
}
handle->listHead = handle->listTail = NULL;
return 0;
}
handle->previousFrame = frame;
//swap current and previous frames
MsfCookedFrame tmp = handle->previousFrame;
handle->previousFrame = handle->currentFrame;
handle->currentFrame = tmp;
handle->framesSubmitted += 1;
return 1;
}
MsfGifResult msf_gif_end(MsfGifState * handle) { MsfTimeFunc
if (!handle->listHead) { MsfGifResult empty = {0}; return empty; }
MSF_GIF_FREE(handle->customAllocatorContext,
handle->previousFrame.pixels, handle->width * handle->height * sizeof(uint32_t));
//first pass: determine total size
size_t total = 1; //1 byte for trailing marker
for (uint8_t * node = handle->listHead; node;) {
MsfBufferHeader * header = (MsfBufferHeader *) node;
node = header->next;
total += header->size;
}
for (MsfGifBuffer * node = handle->listHead; node; node = node->next) { total += node->size; }
//second pass: write data
uint8_t * buffer = (uint8_t *) MSF_GIF_MALLOC(handle->customAllocatorContext, total);
if (buffer) {
uint8_t * writeHead = buffer;
for (uint8_t * node = handle->listHead; node;) {
MsfBufferHeader * header = (MsfBufferHeader *) node;
memcpy(writeHead, node + sizeof(MsfBufferHeader), header->size);
writeHead += header->size;
node = header->next;
for (MsfGifBuffer * node = handle->listHead; node; node = node->next) {
memcpy(writeHead, node->data, node->size);
writeHead += node->size;
}
*writeHead++ = 0x3B;
}
//third pass: free buffers
for (uint8_t * node = handle->listHead; node;) {
MsfBufferHeader * header = (MsfBufferHeader *) node;
node = header->next;
MSF_GIF_FREE(handle->customAllocatorContext, header, sizeof(MsfBufferHeader) + header->size);
}
msf_free_gif_state(handle);
MsfGifResult ret = { buffer, total, total, handle->customAllocatorContext };
return ret;
}
void msf_gif_free(MsfGifResult result) {
void msf_gif_free(MsfGifResult result) { MsfTimeFunc
if (result.data) { MSF_GIF_FREE(result.contextPointer, result.data, result.allocSize); }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// To-file API ///
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int msf_gif_begin_to_file(MsfGifState * handle, int width, int height, MsfGifFileWriteFunc func, void * filePointer) {
handle->fileWriteFunc = func;
handle->fileWriteData = filePointer;
return msf_gif_begin(handle, width, height);
}
int msf_gif_frame_to_file(MsfGifState * handle, uint8_t * pixelData, int centiSecondsPerFame, int maxBitDepth, int pitchInBytes) {
if (!msf_gif_frame(handle, pixelData, centiSecondsPerFame, maxBitDepth, pitchInBytes)) { return 0; }
//NOTE: this is a somewhat hacky implementation which is not perfectly efficient, but it's good enough for now
MsfGifBuffer * head = handle->listHead;
if (!handle->fileWriteFunc(head->data, head->size, 1, handle->fileWriteData)) { msf_free_gif_state(handle); return 0; }
handle->listHead = head->next;
MSF_GIF_FREE(handle->customAllocatorContext, head, offsetof(MsfGifBuffer, data) + head->size);
return 1;
}
int msf_gif_end_to_file(MsfGifState * handle) {
//NOTE: this is a somewhat hacky implementation which is not perfectly efficient, but it's good enough for now
MsfGifResult result = msf_gif_end(handle);
int ret = (int) handle->fileWriteFunc(result.data, result.dataSize, 1, handle->fileWriteData);
msf_gif_free(result);
return ret;
}
#endif //MSF_GIF_ALREADY_IMPLEMENTED_IN_THIS_TRANSLATION_UNIT
#endif //MSF_GIF_IMPL
@ -570,7 +679,7 @@ void msf_gif_free(MsfGifResult result) {
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2020 Miles Fogle
Copyright (c) 2021 Miles Fogle
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