43 #ifndef GIF_TEMP_MALLOC
45 #define GIF_TEMP_MALLOC malloc
50 #define GIF_TEMP_FREE free
55 #define GIF_MALLOC malloc
81 int GifIMax(
int l,
int r) {
return l > r ? l : r; }
82 int GifIMin(
int l,
int r) {
return l < r ? l : r; }
83 int GifIAbs(
int i) {
return i < 0 ? -i : i; }
92 if (treeRoot > (1 << pPal->
bitDepth) - 1)
94 int ind = treeRoot - (1 << pPal->
bitDepth);
98 int r_err = r - ((
int32_t)pPal->
r[ind]);
99 int g_err = g - ((
int32_t)pPal->
g[ind]);
100 int b_err = b - ((
int32_t)pPal->
b[ind]);
103 if (diff < *bestDiff)
113 int comps[3]; comps[0] = r; comps[1] = g; comps[2] = b;
116 int splitPos = pPal->
treeSplit[treeRoot];
117 if (splitPos > splitComp)
121 if (*bestDiff > splitPos - splitComp)
130 if (*bestDiff > splitComp - splitPos)
140 uint8_t gA = image[pixA * 4 + 1];
141 uint8_t bA = image[pixA * 4 + 2];
142 uint8_t aA = image[pixA * 4 + 3];
145 uint8_t gB = image[pixB * 4 + 1];
146 uint8_t bB = image[pixB * 4 + 2];
147 uint8_t aB = image[pixA * 4 + 3];
149 image[pixA * 4] = rB;
150 image[pixA * 4 + 1] = gB;
151 image[pixA * 4 + 2] = bB;
152 image[pixA * 4 + 3] = aB;
154 image[pixB * 4] = rA;
155 image[pixB * 4 + 1] = gA;
156 image[pixB * 4 + 2] = bA;
157 image[pixB * 4 + 3] = aA;
163 int storeIndex = left;
165 for (
int ii = left; ii < right; ++ii)
167 int arrayVal = image[ii * 4 + elt];
168 if (arrayVal < pivotValue)
173 else if (arrayVal == pivotValue)
189 if (left < right - 1)
191 int pivotValue = image[(neededCenter) * 4 + com];
193 int pivotIndex =
GifPartition(image, left, right - 1, com, pivotValue);
197 if (pivotIndex > neededCenter)
200 if (pivotIndex < neededCenter)
208 if (left < right - 1)
210 return GifPartition(image, left, right - 1, com, neededMean);
221 int numColors = (1 << pal->
bitDepth);
224 if (treeNode >= numColors)
226 int entry = treeNode - numColors;
237 for (
int ii = 0; ii < numPixels; ++ii)
251 if (entry == numColors - 1)
255 for (
int ii = 0; ii < numPixels; ++ii)
272 for (
int ii = 0; ii < numPixels; ++ii)
274 r += image[ii * 4 + 0];
275 g += image[ii * 4 + 1];
276 b += image[ii * 4 + 2];
295 int minR = 255, maxR = 0;
296 int minG = 255, maxG = 0;
297 int minB = 255, maxB = 0;
298 for (
int ii = 0; ii < numPixels; ++ii)
300 int r = image[ii * 4 + 0];
301 int g = image[ii * 4 + 1];
302 int b = image[ii * 4 + 2];
304 if (r > maxR) maxR = r;
305 if (r < minR) minR = r;
307 if (g > maxG) maxG = g;
308 if (g < minG) minG = g;
310 if (b > maxB) maxB = b;
311 if (b < minB) minB = b;
314 int rRange = maxR - minR;
315 int gRange = maxG - minG;
316 int bRange = maxB - minB;
319 int splitCom = 1;
int rangeMin = minG;
int rangeMax = maxG;
320 if (bRange > gRange) { splitCom = 2; rangeMin = minB; rangeMax = maxB; }
321 if (rRange > bRange && rRange > gRange) { splitCom = 0; rangeMin = minR; rangeMax = maxR; }
323 int subPixelsA = numPixels / 2;
326 int splitValue = image[subPixelsA * 4 + splitCom];
329 int splitUnbalance =
GifIAbs((splitValue - rangeMin) - (rangeMax - splitValue));
330 if (splitUnbalance > (1536 >> treeLevel))
332 splitValue = rangeMin + (rangeMax - rangeMin) / 2;
337 if (treeNode == numColors / 2)
343 int subPixelsB = numPixels - subPixelsA;
347 GifSplitPalette(image, subPixelsA, treeNode * 2, treeLevel + 1, buildForDither, pal);
348 GifSplitPalette(image + subPixelsA * 4, subPixelsB, treeNode * 2 + 1, treeLevel + 1, buildForDither, pal);
360 for (
int ii = 0; ii < numPixels; ++ii)
362 if (lastFrame[0] != frame[0] ||
363 lastFrame[1] != frame[1] ||
364 lastFrame[2] != frame[2])
366 writeIter[0] = frame[0];
367 writeIter[1] = frame[1];
368 writeIter[2] = frame[2];
387 size_t imageSize = (size_t)(width * height * 4 *
sizeof(
uint8_t));
389 memcpy(destroyableImage, nextFrame, imageSize);
391 int numPixels = (
int)(width * height);
395 GifSplitPalette(destroyableImage, numPixels, 1, 0, buildForDither, pPal);
400 pPal->
treeSplit[1 << (bitDepth - 1)] = 0;
403 pPal->
r[0] = pPal->
g[0] = pPal->
b[0] = 0;
409 int numPixels = (
int)(width * height);
416 for (
int ii = 0; ii < numPixels * 4; ++ii)
420 quantPixels[ii] = pix16;
423 for (
uint32_t yy = 0; yy < height; ++yy)
425 for (
uint32_t xx = 0; xx < width; ++xx)
427 int32_t* nextPix = quantPixels + 4 * (yy * width + xx);
428 const uint8_t* lastPix = lastFrame ? lastFrame + 4 * (yy * width + xx) :
NULL;
432 int32_t gg = (nextPix[1] + 127) / 256;
433 int32_t bb = (nextPix[2] + 127) / 256;
460 nextPix[0] = pPal->
r[bestInd];
461 nextPix[1] = pPal->
g[bestInd];
462 nextPix[2] = pPal->
b[bestInd];
463 nextPix[3] = bestInd;
467 int quantloc_7 = (
int)(yy * width + xx + 1);
468 int quantloc_3 = (
int)(yy * width + width + xx - 1);
469 int quantloc_5 = (
int)(yy * width + width + xx);
470 int quantloc_1 = (
int)(yy * width + width + xx + 1);
472 if (quantloc_7 < numPixels)
474 int32_t* pix7 = quantPixels + 4 * quantloc_7;
475 pix7[0] +=
GifIMax(-pix7[0], r_err * 7 / 16);
476 pix7[1] +=
GifIMax(-pix7[1], g_err * 7 / 16);
477 pix7[2] +=
GifIMax(-pix7[2], b_err * 7 / 16);
480 if (quantloc_3 < numPixels)
482 int32_t* pix3 = quantPixels + 4 * quantloc_3;
483 pix3[0] +=
GifIMax(-pix3[0], r_err * 3 / 16);
484 pix3[1] +=
GifIMax(-pix3[1], g_err * 3 / 16);
485 pix3[2] +=
GifIMax(-pix3[2], b_err * 3 / 16);
488 if (quantloc_5 < numPixels)
490 int32_t* pix5 = quantPixels + 4 * quantloc_5;
491 pix5[0] +=
GifIMax(-pix5[0], r_err * 5 / 16);
492 pix5[1] +=
GifIMax(-pix5[1], g_err * 5 / 16);
493 pix5[2] +=
GifIMax(-pix5[2], b_err * 5 / 16);
496 if (quantloc_1 < numPixels)
498 int32_t* pix1 = quantPixels + 4 * quantloc_1;
499 pix1[0] +=
GifIMax(-pix1[0], r_err / 16);
500 pix1[1] +=
GifIMax(-pix1[1], g_err / 16);
501 pix1[2] +=
GifIMax(-pix1[2], b_err / 16);
507 for (
int ii = 0; ii < numPixels * 4; ++ii)
509 outFrame[ii] = (
uint8_t)quantPixels[ii];
518 uint32_t numPixels = width * height;
519 for (
uint32_t ii = 0; ii < numPixels; ++ii)
524 lastFrame[0] == nextFrame[0] &&
525 lastFrame[1] == nextFrame[1] &&
526 lastFrame[2] == nextFrame[2])
528 outFrame[0] = lastFrame[0];
529 outFrame[1] = lastFrame[1];
530 outFrame[2] = lastFrame[2];
541 outFrame[0] = pPal->
r[bestInd];
542 outFrame[1] = pPal->
g[bestInd];
543 outFrame[2] = pPal->
b[bestInd];
544 outFrame[3] = (
uint8_t)bestInd;
547 if (lastFrame) lastFrame += 4;
597 for (
uint32_t ii = 0; ii < length; ++ii)
623 for (
int ii = 1; ii < (1 << pPal->
bitDepth); ++ii)
643 fputc(delay & 0xff, f);
644 fputc((delay >> 8) & 0xff, f);
650 fputc(left & 0xff, f);
651 fputc((left >> 8) & 0xff, f);
652 fputc(top & 0xff, f);
653 fputc((top >> 8) & 0xff, f);
655 fputc(width & 0xff, f);
656 fputc((width >> 8) & 0xff, f);
657 fputc(height & 0xff, f);
658 fputc((height >> 8) & 0xff, f);
663 fputc(0x80 + pPal->
bitDepth - 1, f);
666 const int minCodeSize = pPal->
bitDepth;
669 fputc(minCodeSize, f);
673 memset(codetree, 0,
sizeof(
GifLzwNode) * 4096);
685 for (
uint32_t yy = 0; yy < height; ++yy)
687 for (
uint32_t xx = 0; xx < width; ++xx)
691 uint8_t nextValue = image[((height - 1 - yy) * width + xx) * 4 + 3];
694 uint8_t nextValue = image[(yy * width + xx) * 4 + 3];
706 else if (codetree[curCode].m_next[nextValue])
709 curCode = codetree[curCode].
m_next[nextValue];
719 if (maxCode >= (1ul << codeSize))
730 memset(codetree, 0,
sizeof(
GifLzwNode) * 4096);
731 codeSize = (
uint32_t)(minCodeSize + 1);
732 maxCode = clearCode + 1;
768 (void)bitDepth; (void)dither;
769 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
771 fopen_s(&writer->
f, filename,
"wb");
773 writer->
f = fopen(filename,
"wb");
775 if (!writer->
f)
return false;
782 fputs(
"GIF89a", writer->
f);
785 fputc(width & 0xff, writer->
f);
786 fputc((width >> 8) & 0xff, writer->
f);
787 fputc(height & 0xff, writer->
f);
788 fputc((height >> 8) & 0xff, writer->
f);
790 fputc(0xf0, writer->
f);
807 fputc(0x21, writer->
f);
808 fputc(0xff, writer->
f);
809 fputc(11, writer->
f);
810 fputs(
"NETSCAPE2.0", writer->
f);
829 if (!writer->
f)
return false;
835 GifMakePalette((dither ?
NULL : oldImage), image, width, height, bitDepth, dither, &pal);
852 if (!writer->
f)
return false;
854 fputc(0x3b, writer->
f);