Skip to content
Permalink
Browse files

Switch more vars from arrays to malloc.

But imageData and imageDataBU do not work when malloc'ed (I get
a screen with garbage colors).
  • Loading branch information...
marcmerlin committed Mar 10, 2019
1 parent 2715fe2 commit c2dc4c78d3fe8d0cc45d8aa621457466a48d4713
Showing with 31 additions and 15 deletions.
  1. +4 −2 GifDecoder.h
  2. +27 −0 GifDecoder_Impl.h
  3. +0 −13 LzwDecoder_Impl.h
@@ -95,14 +95,16 @@ class GifDecoder {
unsigned long nextFrameTime_ms;

int colorCount;
rgb_24 palette[256];
rgb_24 *palette;

char tempBuffer[260];
char *tempBuffer;

// Buffer image data is decoded into
//uint8_t *imageData = (uint8_t *) malloc(maxGifWidth * maxGifHeight);
uint8_t imageData[maxGifWidth * maxGifHeight];

// Backup image data buffer for saving portions of image disposal method == 3
//uint8_t *imageDataBU = (uint8_t *) malloc(maxGifWidth * maxGifHeight);;
uint8_t imageDataBU[maxGifWidth * maxGifHeight];

callback screenClearCallback;
@@ -674,8 +674,34 @@ int GifDecoder<maxGifWidth, maxGifHeight, lzwMaxBits>::parseData() {
return ERROR_NONE;
}

void *mallocordie(const char *varname, uint32_t req) {
Serial.print("Malloc ");
Serial.print(varname);
Serial.print(" in GifDecoder startDecoding. Requested bytes: ");
Serial.println(req);
void *mem = malloc(req);
if (mem) {
return mem;
} else {
Serial.print("FATAL: malloc failed for ");
Serial.println(varname);
while (1);
}
return NULL;
}

template <int maxGifWidth, int maxGifHeight, int lzwMaxBits>
int GifDecoder<maxGifWidth, maxGifHeight, lzwMaxBits>::startDecoding(void) {
uint32_t gifsize = maxGifWidth * maxGifHeight;

if (!stack ) stack = (uint8_t *) mallocordie("stack", LZW_SIZTABLE);
if (!prefix ) prefix = (uint16_t *) mallocordie("prefix", LZW_SIZTABLE*2);
if (!suffix ) suffix = (uint8_t *) mallocordie("suffix", LZW_SIZTABLE);
//if (!imageData ) imageData = (uint8_t *) mallocordie("imageData", gifsize);
//if (!imageDataBU ) imageDataBU = (uint8_t *) mallocordie("imageDataBU", gifsize);
if (!palette ) palette = (rgb_24 *) mallocordie("palette", sizeof(rgb_24)*256);
if (!tempBuffer ) tempBuffer = (char *) mallocordie("tempBuffer", 260);

// Initialize variables
keyFrame = true;
prevDisposalMethod = DISPOSAL_NONE;
@@ -812,3 +838,4 @@ void GifDecoder<maxGifWidth, maxGifHeight, lzwMaxBits>::decompressAndDisplayFram
if(updateScreenCallback)
(*updateScreenCallback)();
}

@@ -42,15 +42,6 @@ void GifDecoder<maxGifWidth, maxGifHeight, lzwMaxBits>::lzw_setTempBuffer(uint8_
temp_buffer = tempBuffer;
}

void mallocdie(uint8_t seq, uint16_t req) {
Serial.print("Failed to malloc #");
Serial.print(seq);
Serial.print(" in lzw_decode_init. Requested bytes: ");
Serial.println(req);
while(1);
return;
}

// Initialize LZW decoder
// csize initial code size in bits
// buf input data
@@ -63,10 +54,6 @@ void GifDecoder<maxGifWidth, maxGifHeight, lzwMaxBits>::lzw_decode_init (int csi
bs = 0;
bcnt = 0;

if (!stack && !(stack = (uint8_t *) malloc(LZW_SIZTABLE))) mallocdie(1, LZW_SIZTABLE);
if (!prefix && !(prefix = (uint16_t *) malloc(LZW_SIZTABLE*2))) mallocdie(2, LZW_SIZTABLE*2);
if (!suffix && !(suffix = (uint8_t *) malloc(LZW_SIZTABLE))) mallocdie(3, LZW_SIZTABLE);

// Initialize decoder variables
codesize = csize;
cursize = codesize + 1;

0 comments on commit c2dc4c7

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.