|
|
409786 |
diff --git a/src/extra/gd/Makefile.am b/src/extra/gd/Makefile.am
|
|
|
409786 |
index 3fb9330..14bda8c 100644
|
|
|
409786 |
--- a/src/extra/gd/Makefile.am
|
|
|
409786 |
+++ b/src/extra/gd/Makefile.am
|
|
|
409786 |
@@ -22,7 +22,7 @@ libgd_la_SOURCES = gd.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c \
|
|
|
409786 |
gd_io_file.c gd_ss.c gd_io_ss.c gd_png.c gd_jpeg.c gdxpm.c \
|
|
|
409786 |
gdfontt.c gdfonts.c gdfontmb.c gdfontl.c gdfontg.c \
|
|
|
409786 |
gdtables.c gdft.c gdcache.c gdkanji.c wbmp.c \
|
|
|
409786 |
- gd_wbmp.c gdhelpers.c gd_topal.c gd_clip.c
|
|
|
409786 |
+ gd_wbmp.c gdhelpers.c gd_clip.c
|
|
|
409786 |
|
|
|
409786 |
gddir = $(includedir)/libwmf/gd
|
|
|
409786 |
|
|
|
409786 |
diff --git a/src/extra/gd/Makefile.gd b/src/extra/gd/Makefile.gd
|
|
|
409786 |
index 3cd876f..b90e4ac 100644
|
|
|
409786 |
--- a/src/extra/gd/Makefile.gd
|
|
|
409786 |
+++ b/src/extra/gd/Makefile.gd
|
|
|
409786 |
@@ -145,7 +145,7 @@ LIBOBJS=gd.o gd_gd.o gd_gd2.o gd_io.o gd_io_dp.o \
|
|
|
409786 |
gd_io_file.o gd_ss.o gd_io_ss.o gd_png.o gd_jpeg.o gdxpm.o \
|
|
|
409786 |
gdfontt.o gdfonts.o gdfontmb.o gdfontl.o gdfontg.o \
|
|
|
409786 |
gdtables.o gdft.o gdcache.o gdkanji.o wbmp.o \
|
|
|
409786 |
- gd_wbmp.o gdhelpers.o gd_topal.o
|
|
|
409786 |
+ gd_wbmp.o gdhelpers.o
|
|
|
409786 |
|
|
|
409786 |
#Shared library. This should work fine on any ELF platform (Linux, etc.) with
|
|
|
409786 |
#GNU ld or something similarly intelligent. To avoid the chicken-and-egg
|
|
|
409786 |
diff --git a/src/extra/gd/Makefile.in b/src/extra/gd/Makefile.in
|
|
|
409786 |
index edb5c90..8cf93a6 100644
|
|
|
409786 |
--- a/src/extra/gd/Makefile.in
|
|
|
409786 |
+++ b/src/extra/gd/Makefile.in
|
|
|
409786 |
@@ -107,7 +107,7 @@ am_libgd_la_OBJECTS = gd.lo gd_gd.lo gd_gd2.lo gd_io.lo gd_io_dp.lo \
|
|
|
409786 |
gd_io_file.lo gd_ss.lo gd_io_ss.lo gd_png.lo gd_jpeg.lo \
|
|
|
409786 |
gdxpm.lo gdfontt.lo gdfonts.lo gdfontmb.lo gdfontl.lo \
|
|
|
409786 |
gdfontg.lo gdtables.lo gdft.lo gdcache.lo gdkanji.lo wbmp.lo \
|
|
|
409786 |
- gd_wbmp.lo gdhelpers.lo gd_topal.lo gd_clip.lo
|
|
|
409786 |
+ gd_wbmp.lo gdhelpers.lo gd_clip.lo
|
|
|
409786 |
libgd_la_OBJECTS = $(am_libgd_la_OBJECTS)
|
|
|
409786 |
AM_V_lt = $(am__v_lt_@AM_V@)
|
|
|
409786 |
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
|
|
|
409786 |
@@ -370,7 +370,7 @@ libgd_la_SOURCES = gd.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c \
|
|
|
409786 |
gd_io_file.c gd_ss.c gd_io_ss.c gd_png.c gd_jpeg.c gdxpm.c \
|
|
|
409786 |
gdfontt.c gdfonts.c gdfontmb.c gdfontl.c gdfontg.c \
|
|
|
409786 |
gdtables.c gdft.c gdcache.c gdkanji.c wbmp.c \
|
|
|
409786 |
- gd_wbmp.c gdhelpers.c gd_topal.c gd_clip.c
|
|
|
409786 |
+ gd_wbmp.c gdhelpers.c gd_clip.c
|
|
|
409786 |
|
|
|
409786 |
gddir = $(includedir)/libwmf/gd
|
|
|
409786 |
gd_HEADERS = $(HDRGDINST)
|
|
|
409786 |
@@ -472,7 +472,6 @@ distclean-compile:
|
|
|
409786 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gd_jpeg.Plo@am__quote@
|
|
|
409786 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gd_png.Plo@am__quote@
|
|
|
409786 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gd_ss.Plo@am__quote@
|
|
|
409786 |
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gd_topal.Plo@am__quote@
|
|
|
409786 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gd_wbmp.Plo@am__quote@
|
|
|
409786 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdcache.Plo@am__quote@
|
|
|
409786 |
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdfontg.Plo@am__quote@
|
|
|
409786 |
diff --git a/src/extra/gd/gd.c b/src/extra/gd/gd.c
|
|
|
409786 |
index 6296472..dc6a9a7 100644
|
|
|
409786 |
--- a/src/extra/gd/gd.c
|
|
|
409786 |
+++ b/src/extra/gd/gd.c
|
|
|
409786 |
@@ -1995,7 +1995,7 @@ gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX
|
|
|
409786 |
else
|
|
|
409786 |
{
|
|
|
409786 |
/* Find or create the best match */
|
|
|
409786 |
- mapTo = gdImageColorResolveAlpha (dst,
|
|
|
409786 |
+ nc = gdImageColorResolveAlpha (dst,
|
|
|
409786 |
gdTrueColorGetRed (c),
|
|
|
409786 |
gdTrueColorGetGreen (c),
|
|
|
409786 |
gdTrueColorGetBlue (c),
|
|
|
409786 |
diff --git a/src/extra/gd/gd.h b/src/extra/gd/gd.h
|
|
|
409786 |
index 619ddd3..8c19354 100644
|
|
|
409786 |
--- a/src/extra/gd/gd.h
|
|
|
409786 |
+++ b/src/extra/gd/gd.h
|
|
|
409786 |
@@ -308,24 +308,6 @@ int gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a);
|
|
|
409786 |
|
|
|
409786 |
void gdImageColorDeallocate(gdImagePtr im, int color);
|
|
|
409786 |
|
|
|
409786 |
-/* Converts a truecolor image to a palette-based image,
|
|
|
409786 |
- using a high-quality two-pass quantization routine
|
|
|
409786 |
- which attempts to preserve alpha channel information
|
|
|
409786 |
- as well as R/G/B color information when creating
|
|
|
409786 |
- a palette. If ditherFlag is set, the image will be
|
|
|
409786 |
- dithered to approximate colors better, at the expense
|
|
|
409786 |
- of some obvious "speckling." colorsWanted can be
|
|
|
409786 |
- anything up to 256. If the original source image
|
|
|
409786 |
- includes photographic information or anything that
|
|
|
409786 |
- came out of a JPEG, 256 is strongly recommended.
|
|
|
409786 |
-
|
|
|
409786 |
- Better yet, don't use this function -- write real
|
|
|
409786 |
- truecolor PNGs and JPEGs. The disk space gain of
|
|
|
409786 |
- conversion to palette is not great (for small images
|
|
|
409786 |
- it can be negative) and the quality loss is ugly. */
|
|
|
409786 |
-
|
|
|
409786 |
-void gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted);
|
|
|
409786 |
-
|
|
|
409786 |
/* Specifies a color index (if a palette image) or an
|
|
|
409786 |
RGB color (if a truecolor image) which should be
|
|
|
409786 |
considered 100% transparent. FOR TRUECOLOR IMAGES,
|
|
|
409786 |
diff --git a/src/extra/gd/gd_gd2.c b/src/extra/gd/gd_gd2.c
|
|
|
409786 |
index 602b869..05d8dcb 100644
|
|
|
409786 |
--- a/src/extra/gd/gd_gd2.c
|
|
|
409786 |
+++ b/src/extra/gd/gd_gd2.c
|
|
|
409786 |
@@ -361,7 +361,7 @@ gdImageCreateFromGd2Ctx (gdIOCtxPtr in)
|
|
|
409786 |
xhi = im->sx;
|
|
|
409786 |
};
|
|
|
409786 |
/*GD2_DBG(printf("y=%d: ",y)); */
|
|
|
409786 |
- if (fmt == GD2_FMT_RAW)
|
|
|
409786 |
+ if (fmt != GD2_FMT_COMPRESSED)
|
|
|
409786 |
{
|
|
|
409786 |
for (x = xlo; x < xhi; x++)
|
|
|
409786 |
{
|
|
|
409786 |
@@ -617,7 +617,7 @@ gdImageCreateFromGd2PartCtx (gdIOCtx * in, int srcx, int srcy, int w, int h)
|
|
|
409786 |
|
|
|
409786 |
for (x = xlo; x < xhi; x++)
|
|
|
409786 |
{
|
|
|
409786 |
- if (fmt == GD2_FMT_RAW)
|
|
|
409786 |
+ if (fmt != GD2_FMT_COMPRESSED)
|
|
|
409786 |
{
|
|
|
409786 |
if (im->trueColor)
|
|
|
409786 |
{
|
|
|
409786 |
diff --git a/src/extra/gd/gd_png.c b/src/extra/gd/gd_png.c
|
|
|
409786 |
index b37fc2c..c7f3aa0 100644
|
|
|
409786 |
--- a/src/extra/gd/gd_png.c
|
|
|
409786 |
+++ b/src/extra/gd/gd_png.c
|
|
|
409786 |
@@ -131,7 +131,6 @@ gdImageCreateFromPngCtx (gdIOCtx * infile)
|
|
|
409786 |
gdImagePtr im = NULL;
|
|
|
409786 |
int i, j, *open;
|
|
|
409786 |
volatile int transparent = -1;
|
|
|
409786 |
- volatile int palette_allocated = FALSE;
|
|
|
409786 |
|
|
|
409786 |
/* Make sure the signature can't match by dumb luck -- TBB */
|
|
|
409786 |
memset (sig, 0, sizeof (sig));
|
|
|
409786 |
@@ -177,6 +176,7 @@ gdImageCreateFromPngCtx (gdIOCtx * infile)
|
|
|
409786 |
}
|
|
|
409786 |
#endif
|
|
|
409786 |
open = NULL;
|
|
|
409786 |
+ palette = NULL;
|
|
|
409786 |
|
|
|
409786 |
png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
|
|
|
409786 |
|
|
|
409786 |
@@ -254,7 +254,6 @@ gdImageCreateFromPngCtx (gdIOCtx * infile)
|
|
|
409786 |
gdImageDestroy(im);
|
|
|
409786 |
return NULL;
|
|
|
409786 |
}
|
|
|
409786 |
- palette_allocated = TRUE;
|
|
|
409786 |
if (bit_depth < 8)
|
|
|
409786 |
{
|
|
|
409786 |
num_palette = 1 << bit_depth;
|
|
|
409786 |
@@ -321,8 +320,7 @@ gdImageCreateFromPngCtx (gdIOCtx * infile)
|
|
|
409786 |
fprintf (stderr, "gd-png error: cannot allocate image data\n");
|
|
|
409786 |
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
|
|
|
409786 |
gdImageDestroy(im);
|
|
|
409786 |
- if (palette_allocated)
|
|
|
409786 |
- gdFree (palette);
|
|
|
409786 |
+ gdFree(palette);
|
|
|
409786 |
return NULL;
|
|
|
409786 |
}
|
|
|
409786 |
if ((row_pointers = (png_bytepp) gdMalloc (height * sizeof (png_bytep))) == NULL)
|
|
|
409786 |
@@ -331,8 +329,7 @@ gdImageCreateFromPngCtx (gdIOCtx * infile)
|
|
|
409786 |
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
|
|
|
409786 |
gdFree (image_data);
|
|
|
409786 |
gdImageDestroy(im);
|
|
|
409786 |
- if (palette_allocated)
|
|
|
409786 |
- gdFree (palette);
|
|
|
409786 |
+ gdFree(palette);
|
|
|
409786 |
return NULL;
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
@@ -429,8 +426,7 @@ gdImageCreateFromPngCtx (gdIOCtx * infile)
|
|
|
409786 |
}
|
|
|
409786 |
#endif
|
|
|
409786 |
|
|
|
409786 |
- if (palette_allocated)
|
|
|
409786 |
- gdFree (palette);
|
|
|
409786 |
+ gdFree (palette);
|
|
|
409786 |
gdFree (image_data);
|
|
|
409786 |
gdFree (row_pointers);
|
|
|
409786 |
|
|
|
409786 |
diff --git a/src/extra/gd/gd_topal.c b/src/extra/gd/gd_topal.c
|
|
|
409786 |
deleted file mode 100644
|
|
|
409786 |
index 4ca86c9..0000000
|
|
|
409786 |
--- a/src/extra/gd/gd_topal.c
|
|
|
409786 |
+++ /dev/null
|
|
|
409786 |
@@ -1,1698 +0,0 @@
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * gd_topal.c
|
|
|
409786 |
- *
|
|
|
409786 |
- * This code is adapted pretty much entirely from jquant2.c,
|
|
|
409786 |
- * Copyright (C) 1991-1996, Thomas G. Lane. That file is
|
|
|
409786 |
- * part of the Independent JPEG Group's software. Conditions of
|
|
|
409786 |
- * use are compatible with the gd license. See the gd license
|
|
|
409786 |
- * statement and README-JPEG.TXT for additional information.
|
|
|
409786 |
- *
|
|
|
409786 |
- * This file contains 2-pass color quantization (color mapping) routines.
|
|
|
409786 |
- * These routines provide selection of a custom color map for an image,
|
|
|
409786 |
- * followed by mapping of the image to that color map, with optional
|
|
|
409786 |
- * Floyd-Steinberg dithering.
|
|
|
409786 |
- *
|
|
|
409786 |
- * It is also possible to use just the second pass to map to an arbitrary
|
|
|
409786 |
- * externally-given color map.
|
|
|
409786 |
- *
|
|
|
409786 |
- * Note: ordered dithering is not supported, since there isn't any fast
|
|
|
409786 |
- * way to compute intercolor distances; it's unclear that ordered dither's
|
|
|
409786 |
- * fundamental assumptions even hold with an irregularly spaced color map.
|
|
|
409786 |
- *
|
|
|
409786 |
- * SUPPORT FOR ALPHA CHANNELS WAS HACKED IN BY THOMAS BOUTELL, who also
|
|
|
409786 |
- * adapted the code to work within gd rather than within libjpeg, and
|
|
|
409786 |
- * may not have done a great job of either. It's not Thomas G. Lane's fault.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-#include "gd.h"
|
|
|
409786 |
-#include "gdhelpers.h"
|
|
|
409786 |
-#include <string.h>
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * This module implements the well-known Heckbert paradigm for color
|
|
|
409786 |
- * quantization. Most of the ideas used here can be traced back to
|
|
|
409786 |
- * Heckbert's seminal paper
|
|
|
409786 |
- * Heckbert, Paul. "Color Image Quantization for Frame Buffer Display",
|
|
|
409786 |
- * Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
|
|
|
409786 |
- *
|
|
|
409786 |
- * In the first pass over the image, we accumulate a histogram showing the
|
|
|
409786 |
- * usage count of each possible color. To keep the histogram to a reasonable
|
|
|
409786 |
- * size, we reduce the precision of the input; typical practice is to retain
|
|
|
409786 |
- * 5 or 6 bits per color, so that 8 or 4 different input values are counted
|
|
|
409786 |
- * in the same histogram cell.
|
|
|
409786 |
- *
|
|
|
409786 |
- * Next, the color-selection step begins with a box representing the whole
|
|
|
409786 |
- * color space, and repeatedly splits the "largest" remaining box until we
|
|
|
409786 |
- * have as many boxes as desired colors. Then the mean color in each
|
|
|
409786 |
- * remaining box becomes one of the possible output colors.
|
|
|
409786 |
- *
|
|
|
409786 |
- * The second pass over the image maps each input pixel to the closest output
|
|
|
409786 |
- * color (optionally after applying a Floyd-Steinberg dithering correction).
|
|
|
409786 |
- * This mapping is logically trivial, but making it go fast enough requires
|
|
|
409786 |
- * considerable care.
|
|
|
409786 |
- *
|
|
|
409786 |
- * Heckbert-style quantizers vary a good deal in their policies for choosing
|
|
|
409786 |
- * the "largest" box and deciding where to cut it. The particular policies
|
|
|
409786 |
- * used here have proved out well in experimental comparisons, but better ones
|
|
|
409786 |
- * may yet be found.
|
|
|
409786 |
- *
|
|
|
409786 |
- * In earlier versions of the IJG code, this module quantized in YCbCr color
|
|
|
409786 |
- * space, processing the raw upsampled data without a color conversion step.
|
|
|
409786 |
- * This allowed the color conversion math to be done only once per colormap
|
|
|
409786 |
- * entry, not once per pixel. However, that optimization precluded other
|
|
|
409786 |
- * useful optimizations (such as merging color conversion with upsampling)
|
|
|
409786 |
- * and it also interfered with desired capabilities such as quantizing to an
|
|
|
409786 |
- * externally-supplied colormap. We have therefore abandoned that approach.
|
|
|
409786 |
- * The present code works in the post-conversion color space, typically RGB.
|
|
|
409786 |
- *
|
|
|
409786 |
- * To improve the visual quality of the results, we actually work in scaled
|
|
|
409786 |
- * RGBA space, giving G distances more weight than R, and R in turn more than
|
|
|
409786 |
- * B. Alpha is weighted least. To do everything in integer math, we must
|
|
|
409786 |
- * use integer scale factors. The 2/3/1 scale factors used here correspond
|
|
|
409786 |
- * loosely to the relative weights of the colors in the NTSC grayscale
|
|
|
409786 |
- * equation.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-#ifndef TRUE
|
|
|
409786 |
-#define TRUE 1
|
|
|
409786 |
-#endif /* TRUE */
|
|
|
409786 |
-
|
|
|
409786 |
-#ifndef FALSE
|
|
|
409786 |
-#define FALSE 0
|
|
|
409786 |
-#endif /* FALSE */
|
|
|
409786 |
-
|
|
|
409786 |
-#define R_SCALE 2 /* scale R distances by this much */
|
|
|
409786 |
-#define G_SCALE 3 /* scale G distances by this much */
|
|
|
409786 |
-#define B_SCALE 1 /* and B by this much */
|
|
|
409786 |
-#define A_SCALE 4 /* and alpha by this much. This really
|
|
|
409786 |
- only scales by 1 because alpha
|
|
|
409786 |
- values are 7-bit to begin with. */
|
|
|
409786 |
-
|
|
|
409786 |
-/* Channel ordering (fixed in gd) */
|
|
|
409786 |
-#define C0_SCALE R_SCALE
|
|
|
409786 |
-#define C1_SCALE G_SCALE
|
|
|
409786 |
-#define C2_SCALE B_SCALE
|
|
|
409786 |
-#define C3_SCALE A_SCALE
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * First we have the histogram data structure and routines for creating it.
|
|
|
409786 |
- *
|
|
|
409786 |
- * The number of bits of precision can be adjusted by changing these symbols.
|
|
|
409786 |
- * We recommend keeping 6 bits for G and 5 each for R and B.
|
|
|
409786 |
- * If you have plenty of memory and cycles, 6 bits all around gives marginally
|
|
|
409786 |
- * better results; if you are short of memory, 5 bits all around will save
|
|
|
409786 |
- * some space but degrade the results.
|
|
|
409786 |
- * To maintain a fully accurate histogram, we'd need to allocate a "long"
|
|
|
409786 |
- * (preferably unsigned long) for each cell. In practice this is overkill;
|
|
|
409786 |
- * we can get by with 16 bits per cell. Few of the cell counts will overflow,
|
|
|
409786 |
- * and clamping those that do overflow to the maximum value will give close-
|
|
|
409786 |
- * enough results. This reduces the recommended histogram size from 256Kb
|
|
|
409786 |
- * to 128Kb, which is a useful savings on PC-class machines.
|
|
|
409786 |
- * (In the second pass the histogram space is re-used for pixel mapping data;
|
|
|
409786 |
- * in that capacity, each cell must be able to store zero to the number of
|
|
|
409786 |
- * desired colors. 16 bits/cell is plenty for that too.)
|
|
|
409786 |
- * Since the JPEG code is intended to run in small memory model on 80x86
|
|
|
409786 |
- * machines, we can't just allocate the histogram in one chunk. Instead
|
|
|
409786 |
- * of a true 3-D array, we use a row of pointers to 2-D arrays. Each
|
|
|
409786 |
- * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
|
|
|
409786 |
- * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that
|
|
|
409786 |
- * on 80x86 machines, the pointer row is in near memory but the actual
|
|
|
409786 |
- * arrays are in far memory (same arrangement as we use for image arrays).
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-#define MAXNUMCOLORS (gdMaxColors) /* maximum size of colormap */
|
|
|
409786 |
-
|
|
|
409786 |
-#define HIST_C0_BITS 5 /* bits of precision in R histogram */
|
|
|
409786 |
-#define HIST_C1_BITS 6 /* bits of precision in G histogram */
|
|
|
409786 |
-#define HIST_C2_BITS 5 /* bits of precision in B histogram */
|
|
|
409786 |
-#define HIST_C3_BITS 3 /* bits of precision in A histogram */
|
|
|
409786 |
-
|
|
|
409786 |
-/* Number of elements along histogram axes. */
|
|
|
409786 |
-#define HIST_C0_ELEMS (1<
|
|
|
409786 |
-#define HIST_C1_ELEMS (1<
|
|
|
409786 |
-#define HIST_C2_ELEMS (1<
|
|
|
409786 |
-#define HIST_C3_ELEMS (1<
|
|
|
409786 |
-
|
|
|
409786 |
-/* These are the amounts to shift an input value to get a histogram index. */
|
|
|
409786 |
-#define C0_SHIFT (8-HIST_C0_BITS)
|
|
|
409786 |
-#define C1_SHIFT (8-HIST_C1_BITS)
|
|
|
409786 |
-#define C2_SHIFT (8-HIST_C2_BITS)
|
|
|
409786 |
-/* Beware! Alpha is 7 bit to begin with */
|
|
|
409786 |
-#define C3_SHIFT (7-HIST_C3_BITS)
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-typedef unsigned short histcell; /* histogram cell; prefer an unsigned type */
|
|
|
409786 |
-
|
|
|
409786 |
-typedef histcell *histptr; /* for pointers to histogram cells */
|
|
|
409786 |
-
|
|
|
409786 |
-typedef histcell hist1d[HIST_C3_ELEMS]; /* typedefs for the array */
|
|
|
409786 |
-typedef hist1d *hist2d; /* type for the 2nd-level pointers */
|
|
|
409786 |
-typedef hist2d *hist3d; /* type for third-level pointer */
|
|
|
409786 |
-typedef hist3d *hist4d; /* type for top-level pointer */
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/* Declarations for Floyd-Steinberg dithering.
|
|
|
409786 |
-
|
|
|
409786 |
- * Errors are accumulated into the array fserrors[], at a resolution of
|
|
|
409786 |
- * 1/16th of a pixel count. The error at a given pixel is propagated
|
|
|
409786 |
- * to its not-yet-processed neighbors using the standard F-S fractions,
|
|
|
409786 |
- * ... (here) 7/16
|
|
|
409786 |
- * 3/16 5/16 1/16
|
|
|
409786 |
- * We work left-to-right on even rows, right-to-left on odd rows.
|
|
|
409786 |
- *
|
|
|
409786 |
- * We can get away with a single array (holding one row's worth of errors)
|
|
|
409786 |
- * by using it to store the current row's errors at pixel columns not yet
|
|
|
409786 |
- * processed, but the next row's errors at columns already processed. We
|
|
|
409786 |
- * need only a few extra variables to hold the errors immediately around the
|
|
|
409786 |
- * current column. (If we are lucky, those variables are in registers, but
|
|
|
409786 |
- * even if not, they're probably cheaper to access than array elements are.)
|
|
|
409786 |
- *
|
|
|
409786 |
- * The fserrors[] array has (#columns + 2) entries; the extra entry at
|
|
|
409786 |
- * each end saves us from special-casing the first and last pixels.
|
|
|
409786 |
- * Each entry is three values long, one value for each color component.
|
|
|
409786 |
- *
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-typedef signed short FSERROR; /* 16 bits should be enough */
|
|
|
409786 |
-typedef int LOCFSERROR; /* use 'int' for calculation temps */
|
|
|
409786 |
-
|
|
|
409786 |
-typedef FSERROR *FSERRPTR; /* pointer to error array */
|
|
|
409786 |
-
|
|
|
409786 |
-/* Private object */
|
|
|
409786 |
-
|
|
|
409786 |
-typedef struct
|
|
|
409786 |
- {
|
|
|
409786 |
- hist4d histogram; /* pointer to the histogram */
|
|
|
409786 |
- int needs_zeroed; /* TRUE if next pass must zero histogram */
|
|
|
409786 |
-
|
|
|
409786 |
- /* Variables for Floyd-Steinberg dithering */
|
|
|
409786 |
- FSERRPTR fserrors; /* accumulated errors */
|
|
|
409786 |
- int on_odd_row; /* flag to remember which row we are on */
|
|
|
409786 |
- int *error_limiter; /* table for clamping the applied error */
|
|
|
409786 |
- int *error_limiter_storage; /* gdMalloc'd storage for the above */
|
|
|
409786 |
- int transparentIsPresent; /* TBB: for rescaling to ensure that */
|
|
|
409786 |
- int opaqueIsPresent; /* 100% opacity & transparency are preserved */
|
|
|
409786 |
- }
|
|
|
409786 |
-my_cquantizer;
|
|
|
409786 |
-
|
|
|
409786 |
-typedef my_cquantizer *my_cquantize_ptr;
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * Prescan the pixel array.
|
|
|
409786 |
- *
|
|
|
409786 |
- * The prescan simply updates the histogram, which has been
|
|
|
409786 |
- * initialized to zeroes by start_pass.
|
|
|
409786 |
- *
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-prescan_quantize (gdImagePtr im, my_cquantize_ptr cquantize)
|
|
|
409786 |
-{
|
|
|
409786 |
- register histptr histp;
|
|
|
409786 |
- register hist4d histogram = cquantize->histogram;
|
|
|
409786 |
- int row;
|
|
|
409786 |
- int col;
|
|
|
409786 |
- int *ptr;
|
|
|
409786 |
- int width = im->sx;
|
|
|
409786 |
-
|
|
|
409786 |
- for (row = 0; row < im->sy; row++)
|
|
|
409786 |
- {
|
|
|
409786 |
- ptr = im->tpixels[row];
|
|
|
409786 |
- for (col = width; col > 0; col--)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* get pixel value and index into the histogram */
|
|
|
409786 |
- int r, g, b, a;
|
|
|
409786 |
- r = gdTrueColorGetRed (*ptr) >> C0_SHIFT;
|
|
|
409786 |
- g = gdTrueColorGetGreen (*ptr) >> C1_SHIFT;
|
|
|
409786 |
- b = gdTrueColorGetBlue (*ptr) >> C2_SHIFT;
|
|
|
409786 |
- a = gdTrueColorGetAlpha (*ptr);
|
|
|
409786 |
- /* We must have 100% opacity and transparency available
|
|
|
409786 |
- in the color map to do an acceptable job with alpha
|
|
|
409786 |
- channel, if opacity and transparency are present in the
|
|
|
409786 |
- original, because of the visual properties of large
|
|
|
409786 |
- flat-color border areas (requiring 100% transparency)
|
|
|
409786 |
- and the behavior of poorly implemented browsers
|
|
|
409786 |
- (requiring 100% opacity). Test for the presence of
|
|
|
409786 |
- these here, and rescale the most opaque and transparent
|
|
|
409786 |
- palette entries at the end if so. This avoids the need
|
|
|
409786 |
- to develop a fuller understanding I have not been able
|
|
|
409786 |
- to reach so far in my study of this subject. TBB */
|
|
|
409786 |
- if (a == gdAlphaTransparent)
|
|
|
409786 |
- {
|
|
|
409786 |
- cquantize->transparentIsPresent = 1;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (a == gdAlphaOpaque)
|
|
|
409786 |
- {
|
|
|
409786 |
- cquantize->opaqueIsPresent = 1;
|
|
|
409786 |
- }
|
|
|
409786 |
- a >>= C3_SHIFT;
|
|
|
409786 |
- histp = &histogram[r][g][b][a];
|
|
|
409786 |
- /* increment, check for overflow and undo increment if so. */
|
|
|
409786 |
- if (++(*histp) <= 0)
|
|
|
409786 |
- (*histp)--;
|
|
|
409786 |
- ptr++;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * Next we have the really interesting routines: selection of a colormap
|
|
|
409786 |
- * given the completed histogram.
|
|
|
409786 |
- * These routines work with a list of "boxes", each representing a rectangular
|
|
|
409786 |
- * subset of the input color space (to histogram precision).
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-typedef struct
|
|
|
409786 |
-{
|
|
|
409786 |
- /* The bounds of the box (inclusive); expressed as histogram indexes */
|
|
|
409786 |
- int c0min, c0max;
|
|
|
409786 |
- int c1min, c1max;
|
|
|
409786 |
- int c2min, c2max;
|
|
|
409786 |
- int c3min, c3max;
|
|
|
409786 |
- /* The volume (actually 2-norm) of the box */
|
|
|
409786 |
- int volume;
|
|
|
409786 |
- /* The number of nonzero histogram cells within this box */
|
|
|
409786 |
- long colorcount;
|
|
|
409786 |
-}
|
|
|
409786 |
-box;
|
|
|
409786 |
-
|
|
|
409786 |
-typedef box *boxptr;
|
|
|
409786 |
-
|
|
|
409786 |
-static boxptr
|
|
|
409786 |
-find_biggest_color_pop (boxptr boxlist, int numboxes)
|
|
|
409786 |
-/* Find the splittable box with the largest color population */
|
|
|
409786 |
-/* Returns NULL if no splittable boxes remain */
|
|
|
409786 |
-{
|
|
|
409786 |
- register boxptr boxp;
|
|
|
409786 |
- register int i;
|
|
|
409786 |
- register long maxc = 0;
|
|
|
409786 |
- boxptr which = NULL;
|
|
|
409786 |
-
|
|
|
409786 |
- for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (boxp->colorcount > maxc && boxp->volume > 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- which = boxp;
|
|
|
409786 |
- maxc = boxp->colorcount;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- return which;
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static boxptr
|
|
|
409786 |
-find_biggest_volume (boxptr boxlist, int numboxes)
|
|
|
409786 |
-/* Find the splittable box with the largest (scaled) volume */
|
|
|
409786 |
-/* Returns NULL if no splittable boxes remain */
|
|
|
409786 |
-{
|
|
|
409786 |
- register boxptr boxp;
|
|
|
409786 |
- register int i;
|
|
|
409786 |
- register int maxv = 0;
|
|
|
409786 |
- boxptr which = NULL;
|
|
|
409786 |
-
|
|
|
409786 |
- for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (boxp->volume > maxv)
|
|
|
409786 |
- {
|
|
|
409786 |
- which = boxp;
|
|
|
409786 |
- maxv = boxp->volume;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- return which;
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-update_box (my_cquantize_ptr cquantize, boxptr boxp)
|
|
|
409786 |
-/* Shrink the min/max bounds of a box to enclose only nonzero elements, */
|
|
|
409786 |
-/* and recompute its volume and population */
|
|
|
409786 |
-{
|
|
|
409786 |
- hist4d histogram = cquantize->histogram;
|
|
|
409786 |
- histptr histp;
|
|
|
409786 |
- int c0, c1, c2, c3;
|
|
|
409786 |
- int c0min, c0max, c1min, c1max, c2min, c2max, c3min, c3max;
|
|
|
409786 |
- int dist0, dist1, dist2, dist3;
|
|
|
409786 |
- long ccount;
|
|
|
409786 |
-
|
|
|
409786 |
- c0min = boxp->c0min;
|
|
|
409786 |
- c0max = boxp->c0max;
|
|
|
409786 |
- c1min = boxp->c1min;
|
|
|
409786 |
- c1max = boxp->c1max;
|
|
|
409786 |
- c2min = boxp->c2min;
|
|
|
409786 |
- c2max = boxp->c2max;
|
|
|
409786 |
- c3min = boxp->c3min;
|
|
|
409786 |
- c3max = boxp->c3max;
|
|
|
409786 |
-
|
|
|
409786 |
- if (c0max > c0min)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- histp = &histogram[c0][c1][c2][c3min];
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (*histp++ != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c0min = c0min = c0;
|
|
|
409786 |
- goto have_c0min;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c0min:
|
|
|
409786 |
- if (c0max > c0min)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c0 = c0max; c0 >= c0min; c0--)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- histp = &histogram[c0][c1][c2][c3min];
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (*histp++ != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c0max = c0max = c0;
|
|
|
409786 |
- goto have_c0max;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c0max:
|
|
|
409786 |
- if (c1max > c1min)
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- histp = &histogram[c0][c1][c2][c3min];
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- if (*histp++ != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c1min = c1min = c1;
|
|
|
409786 |
- goto have_c1min;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c1min:
|
|
|
409786 |
- if (c1max > c1min)
|
|
|
409786 |
- for (c1 = c1max; c1 >= c1min; c1--)
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- histp = &histogram[c0][c1][c2][c3min];
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- if (*histp++ != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c1max = c1max = c1;
|
|
|
409786 |
- goto have_c1max;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c1max:
|
|
|
409786 |
- /* The original version hand-rolled the array lookup a little, but
|
|
|
409786 |
- with four dimensions, I don't even want to think about it. TBB */
|
|
|
409786 |
- if (c2max > c2min)
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- if (histogram[c0][c1][c2][c3] != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c2min = c2min = c2;
|
|
|
409786 |
- goto have_c2min;
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c2min:
|
|
|
409786 |
- if (c2max > c2min)
|
|
|
409786 |
- for (c2 = c2max; c2 >= c2min; c2--)
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- if (histogram[c0][c1][c2][c3] != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c2max = c2max = c2;
|
|
|
409786 |
- goto have_c2max;
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c2max:
|
|
|
409786 |
- if (c3max > c3min)
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- if (histogram[c0][c1][c2][c3] != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c3min = c3min = c3;
|
|
|
409786 |
- goto have_c3min;
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c3min:
|
|
|
409786 |
- if (c3max > c3min)
|
|
|
409786 |
- for (c3 = c3max; c3 >= c3min; c3--)
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- if (histogram[c0][c1][c2][c3] != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- boxp->c3max = c3max = c3;
|
|
|
409786 |
- goto have_c3max;
|
|
|
409786 |
- }
|
|
|
409786 |
-have_c3max:
|
|
|
409786 |
- /* Update box volume.
|
|
|
409786 |
- * We use 2-norm rather than real volume here; this biases the method
|
|
|
409786 |
- * against making long narrow boxes, and it has the side benefit that
|
|
|
409786 |
- * a box is splittable iff norm > 0.
|
|
|
409786 |
- * Since the differences are expressed in histogram-cell units,
|
|
|
409786 |
- * we have to shift back to 8-bit units to get consistent distances;
|
|
|
409786 |
- * after which, we scale according to the selected distance scale factors.
|
|
|
409786 |
- * TBB: alpha shifts back to 7 bit units. That was accounted for in the
|
|
|
409786 |
- * alpha scale factor.
|
|
|
409786 |
- */
|
|
|
409786 |
- dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
|
|
|
409786 |
- dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
|
|
|
409786 |
- dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
|
|
|
409786 |
- dist3 = ((c3max - c3min) << C3_SHIFT) * C3_SCALE;
|
|
|
409786 |
- boxp->volume = dist0 * dist0 + dist1 * dist1 + dist2 * dist2 + dist3 * dist3;
|
|
|
409786 |
-
|
|
|
409786 |
- /* Now scan remaining volume of box and compute population */
|
|
|
409786 |
- ccount = 0;
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- histp = &histogram[c0][c1][c2][c3min];
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++, histp++)
|
|
|
409786 |
- if (*histp != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- ccount++;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- boxp->colorcount = ccount;
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static int
|
|
|
409786 |
-median_cut (my_cquantize_ptr cquantize,
|
|
|
409786 |
- boxptr boxlist, int numboxes,
|
|
|
409786 |
- int desired_colors)
|
|
|
409786 |
-/* Repeatedly select and split the largest box until we have enough boxes */
|
|
|
409786 |
-{
|
|
|
409786 |
- int n, lb;
|
|
|
409786 |
- int c0, c1, c2, c3, cmax;
|
|
|
409786 |
- register boxptr b1, b2;
|
|
|
409786 |
-
|
|
|
409786 |
- while (numboxes < desired_colors)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* Select box to split.
|
|
|
409786 |
- * Current algorithm: by population for first half, then by volume.
|
|
|
409786 |
- */
|
|
|
409786 |
- if (numboxes * 2 <= desired_colors)
|
|
|
409786 |
- {
|
|
|
409786 |
- b1 = find_biggest_color_pop (boxlist, numboxes);
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- b1 = find_biggest_volume (boxlist, numboxes);
|
|
|
409786 |
- }
|
|
|
409786 |
- if (b1 == NULL) /* no splittable boxes left! */
|
|
|
409786 |
- break;
|
|
|
409786 |
- b2 = &boxlist[numboxes]; /* where new box will go */
|
|
|
409786 |
- /* Copy the color bounds to the new box. */
|
|
|
409786 |
- b2->c0max = b1->c0max;
|
|
|
409786 |
- b2->c1max = b1->c1max;
|
|
|
409786 |
- b2->c2max = b1->c2max;
|
|
|
409786 |
- b2->c3max = b1->c3max;
|
|
|
409786 |
- b2->c0min = b1->c0min;
|
|
|
409786 |
- b2->c1min = b1->c1min;
|
|
|
409786 |
- b2->c2min = b1->c2min;
|
|
|
409786 |
- b2->c3min = b1->c3min;
|
|
|
409786 |
- /* Choose which axis to split the box on.
|
|
|
409786 |
- * Current algorithm: longest scaled axis.
|
|
|
409786 |
- * See notes in update_box about scaling distances.
|
|
|
409786 |
- */
|
|
|
409786 |
- c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
|
|
|
409786 |
- c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
|
|
|
409786 |
- c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
|
|
|
409786 |
- c3 = ((b1->c3max - b1->c3min) << C3_SHIFT) * C3_SCALE;
|
|
|
409786 |
- /* We want to break any ties in favor of green, then red, then blue,
|
|
|
409786 |
- with alpha last. */
|
|
|
409786 |
- cmax = c1;
|
|
|
409786 |
- n = 1;
|
|
|
409786 |
- if (c0 > cmax)
|
|
|
409786 |
- {
|
|
|
409786 |
- cmax = c0;
|
|
|
409786 |
- n = 0;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (c2 > cmax)
|
|
|
409786 |
- {
|
|
|
409786 |
- cmax = c2;
|
|
|
409786 |
- n = 2;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (c3 > cmax)
|
|
|
409786 |
- {
|
|
|
409786 |
- n = 3;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Choose split point along selected axis, and update box bounds.
|
|
|
409786 |
- * Current algorithm: split at halfway point.
|
|
|
409786 |
- * (Since the box has been shrunk to minimum volume,
|
|
|
409786 |
- * any split will produce two nonempty subboxes.)
|
|
|
409786 |
- * Note that lb value is max for lower box, so must be < old max.
|
|
|
409786 |
- */
|
|
|
409786 |
- switch (n)
|
|
|
409786 |
- {
|
|
|
409786 |
- case 0:
|
|
|
409786 |
- lb = (b1->c0max + b1->c0min) / 2;
|
|
|
409786 |
- b1->c0max = lb;
|
|
|
409786 |
- b2->c0min = lb + 1;
|
|
|
409786 |
- break;
|
|
|
409786 |
- case 1:
|
|
|
409786 |
- lb = (b1->c1max + b1->c1min) / 2;
|
|
|
409786 |
- b1->c1max = lb;
|
|
|
409786 |
- b2->c1min = lb + 1;
|
|
|
409786 |
- break;
|
|
|
409786 |
- case 2:
|
|
|
409786 |
- lb = (b1->c2max + b1->c2min) / 2;
|
|
|
409786 |
- b1->c2max = lb;
|
|
|
409786 |
- b2->c2min = lb + 1;
|
|
|
409786 |
- break;
|
|
|
409786 |
- case 3:
|
|
|
409786 |
- lb = (b1->c3max + b1->c3min) / 2;
|
|
|
409786 |
- b1->c3max = lb;
|
|
|
409786 |
- b2->c3min = lb + 1;
|
|
|
409786 |
- break;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Update stats for boxes */
|
|
|
409786 |
- update_box (cquantize, b1);
|
|
|
409786 |
- update_box (cquantize, b2);
|
|
|
409786 |
- numboxes++;
|
|
|
409786 |
- }
|
|
|
409786 |
- return numboxes;
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-compute_color (gdImagePtr im, my_cquantize_ptr cquantize,
|
|
|
409786 |
- boxptr boxp, int icolor)
|
|
|
409786 |
-/*
|
|
|
409786 |
- Compute representative color for a box, put it in
|
|
|
409786 |
- palette index icolor */
|
|
|
409786 |
-{
|
|
|
409786 |
- /* Current algorithm: mean weighted by pixels (not colors) */
|
|
|
409786 |
- /* Note it is important to get the rounding correct! */
|
|
|
409786 |
- hist4d histogram = cquantize->histogram;
|
|
|
409786 |
- histptr histp;
|
|
|
409786 |
- int c0, c1, c2, c3;
|
|
|
409786 |
- int c0min, c0max, c1min, c1max, c2min, c2max, c3min, c3max;
|
|
|
409786 |
- long count;
|
|
|
409786 |
- long total = 0;
|
|
|
409786 |
- long c0total = 0;
|
|
|
409786 |
- long c1total = 0;
|
|
|
409786 |
- long c2total = 0;
|
|
|
409786 |
- long c3total = 0;
|
|
|
409786 |
-
|
|
|
409786 |
- c0min = boxp->c0min;
|
|
|
409786 |
- c0max = boxp->c0max;
|
|
|
409786 |
- c1min = boxp->c1min;
|
|
|
409786 |
- c1max = boxp->c1max;
|
|
|
409786 |
- c2min = boxp->c2min;
|
|
|
409786 |
- c2max = boxp->c2max;
|
|
|
409786 |
- c3min = boxp->c3min;
|
|
|
409786 |
- c3max = boxp->c3max;
|
|
|
409786 |
-
|
|
|
409786 |
- for (c0 = c0min; c0 <= c0max; c0++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c1 = c1min; c1 <= c1max; c1++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (c2 = c2min; c2 <= c2max; c2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- histp = &histogram[c0][c1][c2][c3min];
|
|
|
409786 |
- for (c3 = c3min; c3 <= c3max; c3++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if ((count = *histp++) != 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- total += count;
|
|
|
409786 |
- c0total += ((c0 << C0_SHIFT) + ((1 << C0_SHIFT) >> 1)) * count;
|
|
|
409786 |
- c1total += ((c1 << C1_SHIFT) + ((1 << C1_SHIFT) >> 1)) * count;
|
|
|
409786 |
- c2total += ((c2 << C2_SHIFT) + ((1 << C2_SHIFT) >> 1)) * count;
|
|
|
409786 |
- c3total += ((c3 << C3_SHIFT) + ((1 << C3_SHIFT) >> 1)) * count;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- if (total)
|
|
|
409786 |
- {
|
|
|
409786 |
- im->red[icolor] = (int) ((c0total + (total >> 1)) / total);
|
|
|
409786 |
- im->green[icolor] = (int) ((c1total + (total >> 1)) / total);
|
|
|
409786 |
- im->blue[icolor] = (int) ((c2total + (total >> 1)) / total);
|
|
|
409786 |
- im->alpha[icolor] = (int) ((c3total + (total >> 1)) / total);
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- im->red[icolor] = 255;
|
|
|
409786 |
- im->green[icolor] = 255;
|
|
|
409786 |
- im->blue[icolor] = 255;
|
|
|
409786 |
- im->alpha[icolor] = 255;
|
|
|
409786 |
- }
|
|
|
409786 |
- im->open[icolor] = 0;
|
|
|
409786 |
- if (im->colorsTotal <= icolor)
|
|
|
409786 |
- {
|
|
|
409786 |
- im->colorsTotal = icolor + 1;
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-select_colors (gdImagePtr im, my_cquantize_ptr cquantize, int desired_colors)
|
|
|
409786 |
-/* Master routine for color selection */
|
|
|
409786 |
-{
|
|
|
409786 |
- boxptr boxlist;
|
|
|
409786 |
- int numboxes;
|
|
|
409786 |
- int i;
|
|
|
409786 |
-
|
|
|
409786 |
- /* Allocate workspace for box list */
|
|
|
409786 |
- boxlist = (boxptr) gdMalloc (desired_colors * sizeof (box));
|
|
|
409786 |
- /* Initialize one box containing whole space */
|
|
|
409786 |
- numboxes = 1;
|
|
|
409786 |
- /* Note maxval for alpha is different */
|
|
|
409786 |
- boxlist[0].c0min = 0;
|
|
|
409786 |
- boxlist[0].c0max = 255 >> C0_SHIFT;
|
|
|
409786 |
- boxlist[0].c1min = 0;
|
|
|
409786 |
- boxlist[0].c1max = 255 >> C1_SHIFT;
|
|
|
409786 |
- boxlist[0].c2min = 0;
|
|
|
409786 |
- boxlist[0].c2max = 255 >> C2_SHIFT;
|
|
|
409786 |
- boxlist[0].c3min = 0;
|
|
|
409786 |
- boxlist[0].c3max = gdAlphaMax >> C3_SHIFT;
|
|
|
409786 |
- /* Shrink it to actually-used volume and set its statistics */
|
|
|
409786 |
- update_box (cquantize, &boxlist[0]);
|
|
|
409786 |
- /* Perform median-cut to produce final box list */
|
|
|
409786 |
- numboxes = median_cut (cquantize, boxlist, numboxes, desired_colors);
|
|
|
409786 |
- /* Compute the representative color for each box, fill colormap */
|
|
|
409786 |
- for (i = 0; i < numboxes; i++)
|
|
|
409786 |
- compute_color (im, cquantize, &boxlist[i], i);
|
|
|
409786 |
- /* TBB: if the image contains colors at both scaled ends
|
|
|
409786 |
- of the alpha range, rescale slightly to make sure alpha
|
|
|
409786 |
- covers the full spectrum from 100% transparent to 100%
|
|
|
409786 |
- opaque. Even a faint distinct background color is
|
|
|
409786 |
- generally considered failure with regard to alpha. */
|
|
|
409786 |
-
|
|
|
409786 |
- im->colorsTotal = numboxes;
|
|
|
409786 |
- gdFree (boxlist);
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * These routines are concerned with the time-critical task of mapping input
|
|
|
409786 |
- * colors to the nearest color in the selected colormap.
|
|
|
409786 |
- *
|
|
|
409786 |
- * We re-use the histogram space as an "inverse color map", essentially a
|
|
|
409786 |
- * cache for the results of nearest-color searches. All colors within a
|
|
|
409786 |
- * histogram cell will be mapped to the same colormap entry, namely the one
|
|
|
409786 |
- * closest to the cell's center. This may not be quite the closest entry to
|
|
|
409786 |
- * the actual input color, but it's almost as good. A zero in the cache
|
|
|
409786 |
- * indicates we haven't found the nearest color for that cell yet; the array
|
|
|
409786 |
- * is cleared to zeroes before starting the mapping pass. When we find the
|
|
|
409786 |
- * nearest color for a cell, its colormap index plus one is recorded in the
|
|
|
409786 |
- * cache for future use. The pass2 scanning routines call fill_inverse_cmap
|
|
|
409786 |
- * when they need to use an unfilled entry in the cache.
|
|
|
409786 |
- *
|
|
|
409786 |
- * Our method of efficiently finding nearest colors is based on the "locally
|
|
|
409786 |
- * sorted search" idea described by Heckbert and on the incremental distance
|
|
|
409786 |
- * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
|
|
|
409786 |
- * Gems II (James Arvo, ed. Academic Press, 1991). Thomas points out that
|
|
|
409786 |
- * the distances from a given colormap entry to each cell of the histogram can
|
|
|
409786 |
- * be computed quickly using an incremental method: the differences between
|
|
|
409786 |
- * distances to adjacent cells themselves differ by a constant. This allows a
|
|
|
409786 |
- * fairly fast implementation of the "brute force" approach of computing the
|
|
|
409786 |
- * distance from every colormap entry to every histogram cell. Unfortunately,
|
|
|
409786 |
- * it needs a work array to hold the best-distance-so-far for each histogram
|
|
|
409786 |
- * cell (because the inner loop has to be over cells, not colormap entries).
|
|
|
409786 |
- * The work array elements have to be INT32s, so the work array would need
|
|
|
409786 |
- * 256Kb at our recommended precision. This is not feasible in DOS machines.
|
|
|
409786 |
- *
|
|
|
409786 |
- * To get around these problems, we apply Thomas' method to compute the
|
|
|
409786 |
- * nearest colors for only the cells within a small subbox of the histogram.
|
|
|
409786 |
- * The work array need be only as big as the subbox, so the memory usage
|
|
|
409786 |
- * problem is solved. Furthermore, we need not fill subboxes that are never
|
|
|
409786 |
- * referenced in pass2; many images use only part of the color gamut, so a
|
|
|
409786 |
- * fair amount of work is saved. An additional advantage of this
|
|
|
409786 |
- * approach is that we can apply Heckbert's locality criterion to quickly
|
|
|
409786 |
- * eliminate colormap entries that are far away from the subbox; typically
|
|
|
409786 |
- * three-fourths of the colormap entries are rejected by Heckbert's criterion,
|
|
|
409786 |
- * and we need not compute their distances to individual cells in the subbox.
|
|
|
409786 |
- * The speed of this approach is heavily influenced by the subbox size: too
|
|
|
409786 |
- * small means too much overhead, too big loses because Heckbert's criterion
|
|
|
409786 |
- * can't eliminate as many colormap entries. Empirically the best subbox
|
|
|
409786 |
- * size seems to be about 1/512th of the histogram (1/8th in each direction).
|
|
|
409786 |
- *
|
|
|
409786 |
- * Thomas' article also describes a refined method which is asymptotically
|
|
|
409786 |
- * faster than the brute-force method, but it is also far more complex and
|
|
|
409786 |
- * cannot efficiently be applied to small subboxes. It is therefore not
|
|
|
409786 |
- * useful for programs intended to be portable to DOS machines. On machines
|
|
|
409786 |
- * with plenty of memory, filling the whole histogram in one shot with Thomas'
|
|
|
409786 |
- * refined method might be faster than the present code --- but then again,
|
|
|
409786 |
- * it might not be any faster, and it's certainly more complicated.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/* log2(histogram cells in update box) for each axis; this can be adjusted */
|
|
|
409786 |
-#define BOX_C0_LOG (HIST_C0_BITS-3)
|
|
|
409786 |
-#define BOX_C1_LOG (HIST_C1_BITS-3)
|
|
|
409786 |
-#define BOX_C2_LOG (HIST_C2_BITS-3)
|
|
|
409786 |
-#define BOX_C3_LOG (HIST_C3_BITS-3)
|
|
|
409786 |
-
|
|
|
409786 |
-#define BOX_C0_ELEMS (1<
|
|
|
409786 |
-#define BOX_C1_ELEMS (1<
|
|
|
409786 |
-#define BOX_C2_ELEMS (1<
|
|
|
409786 |
-#define BOX_C3_ELEMS (1<
|
|
|
409786 |
-
|
|
|
409786 |
-#define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG)
|
|
|
409786 |
-#define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG)
|
|
|
409786 |
-#define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG)
|
|
|
409786 |
-#define BOX_C3_SHIFT (C3_SHIFT + BOX_C3_LOG)
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * The next three routines implement inverse colormap filling. They could
|
|
|
409786 |
- * all be folded into one big routine, but splitting them up this way saves
|
|
|
409786 |
- * some stack space (the mindist[] and bestdist[] arrays need not coexist)
|
|
|
409786 |
- * and may allow some compilers to produce better code by registerizing more
|
|
|
409786 |
- * inner-loop variables.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-static int
|
|
|
409786 |
-find_nearby_colors (gdImagePtr im,
|
|
|
409786 |
- int minc0, int minc1, int minc2, int minc3, int colorlist[])
|
|
|
409786 |
-/* Locate the colormap entries close enough to an update box to be candidates
|
|
|
409786 |
- * for the nearest entry to some cell(s) in the update box. The update box
|
|
|
409786 |
- * is specified by the center coordinates of its first cell. The number of
|
|
|
409786 |
- * candidate colormap entries is returned, and their colormap indexes are
|
|
|
409786 |
- * placed in colorlist[].
|
|
|
409786 |
- * This routine uses Heckbert's "locally sorted search" criterion to select
|
|
|
409786 |
- * the colors that need further consideration.
|
|
|
409786 |
- */
|
|
|
409786 |
-{
|
|
|
409786 |
- int numcolors = im->colorsTotal;
|
|
|
409786 |
- int maxc0, maxc1, maxc2, maxc3;
|
|
|
409786 |
- int centerc0, centerc1, centerc2, centerc3;
|
|
|
409786 |
- int i, x, ncolors;
|
|
|
409786 |
- int minmaxdist, min_dist, max_dist, tdist;
|
|
|
409786 |
- int mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */
|
|
|
409786 |
-
|
|
|
409786 |
- /* Compute true coordinates of update box's upper corner and center.
|
|
|
409786 |
- * Actually we compute the coordinates of the center of the upper-corner
|
|
|
409786 |
- * histogram cell, which are the upper bounds of the volume we care about.
|
|
|
409786 |
- * Note that since ">>" rounds down, the "center" values may be closer to
|
|
|
409786 |
- * min than to max; hence comparisons to them must be "<=", not "<".
|
|
|
409786 |
- */
|
|
|
409786 |
- maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
|
|
|
409786 |
- centerc0 = (minc0 + maxc0) >> 1;
|
|
|
409786 |
- maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
|
|
|
409786 |
- centerc1 = (minc1 + maxc1) >> 1;
|
|
|
409786 |
- maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
|
|
|
409786 |
- centerc2 = (minc2 + maxc2) >> 1;
|
|
|
409786 |
- maxc3 = minc3 + ((1 << BOX_C3_SHIFT) - (1 << C3_SHIFT));
|
|
|
409786 |
- centerc3 = (minc3 + maxc3) >> 1;
|
|
|
409786 |
-
|
|
|
409786 |
- /* For each color in colormap, find:
|
|
|
409786 |
- * 1. its minimum squared-distance to any point in the update box
|
|
|
409786 |
- * (zero if color is within update box);
|
|
|
409786 |
- * 2. its maximum squared-distance to any point in the update box.
|
|
|
409786 |
- * Both of these can be found by considering only the corners of the box.
|
|
|
409786 |
- * We save the minimum distance for each color in mindist[];
|
|
|
409786 |
- * only the smallest maximum distance is of interest.
|
|
|
409786 |
- */
|
|
|
409786 |
- minmaxdist = 0x7FFFFFFFL;
|
|
|
409786 |
-
|
|
|
409786 |
- for (i = 0; i < numcolors; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* We compute the squared-c0-distance term, then add in the other three. */
|
|
|
409786 |
- x = im->red[i];
|
|
|
409786 |
- if (x < minc0)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc0) * C0_SCALE;
|
|
|
409786 |
- min_dist = tdist * tdist;
|
|
|
409786 |
- tdist = (x - maxc0) * C0_SCALE;
|
|
|
409786 |
- max_dist = tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else if (x > maxc0)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc0) * C0_SCALE;
|
|
|
409786 |
- min_dist = tdist * tdist;
|
|
|
409786 |
- tdist = (x - minc0) * C0_SCALE;
|
|
|
409786 |
- max_dist = tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- /* within cell range so no contribution to min_dist */
|
|
|
409786 |
- min_dist = 0;
|
|
|
409786 |
- if (x <= centerc0)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc0) * C0_SCALE;
|
|
|
409786 |
- max_dist = tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc0) * C0_SCALE;
|
|
|
409786 |
- max_dist = tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
- x = im->green[i];
|
|
|
409786 |
- if (x < minc1)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc1) * C1_SCALE;
|
|
|
409786 |
- min_dist += tdist * tdist;
|
|
|
409786 |
- tdist = (x - maxc1) * C1_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else if (x > maxc1)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc1) * C1_SCALE;
|
|
|
409786 |
- min_dist += tdist * tdist;
|
|
|
409786 |
- tdist = (x - minc1) * C1_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- /* within cell range so no contribution to min_dist */
|
|
|
409786 |
- if (x <= centerc1)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc1) * C1_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc1) * C1_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
- x = im->blue[i];
|
|
|
409786 |
- if (x < minc2)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc2) * C2_SCALE;
|
|
|
409786 |
- min_dist += tdist * tdist;
|
|
|
409786 |
- tdist = (x - maxc2) * C2_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else if (x > maxc2)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc2) * C2_SCALE;
|
|
|
409786 |
- min_dist += tdist * tdist;
|
|
|
409786 |
- tdist = (x - minc2) * C2_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- /* within cell range so no contribution to min_dist */
|
|
|
409786 |
- if (x <= centerc2)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc2) * C2_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc2) * C2_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
- x = im->alpha[i];
|
|
|
409786 |
- if (x < minc3)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc3) * C3_SCALE;
|
|
|
409786 |
- min_dist += tdist * tdist;
|
|
|
409786 |
- tdist = (x - maxc3) * C3_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else if (x > maxc3)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc3) * C3_SCALE;
|
|
|
409786 |
- min_dist += tdist * tdist;
|
|
|
409786 |
- tdist = (x - minc3) * C3_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- /* within cell range so no contribution to min_dist */
|
|
|
409786 |
- if (x <= centerc3)
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - maxc3) * C3_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- tdist = (x - minc3) * C3_SCALE;
|
|
|
409786 |
- max_dist += tdist * tdist;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
- mindist[i] = min_dist; /* save away the results */
|
|
|
409786 |
- if (max_dist < minmaxdist)
|
|
|
409786 |
- minmaxdist = max_dist;
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
- /* Now we know that no cell in the update box is more than minmaxdist
|
|
|
409786 |
- * away from some colormap entry. Therefore, only colors that are
|
|
|
409786 |
- * within minmaxdist of some part of the box need be considered.
|
|
|
409786 |
- */
|
|
|
409786 |
- ncolors = 0;
|
|
|
409786 |
- for (i = 0; i < numcolors; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (mindist[i] <= minmaxdist)
|
|
|
409786 |
- colorlist[ncolors++] = i;
|
|
|
409786 |
- }
|
|
|
409786 |
- return ncolors;
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-find_best_colors (gdImagePtr im,
|
|
|
409786 |
- int minc0, int minc1, int minc2, int minc3,
|
|
|
409786 |
- int numcolors, int colorlist[], int bestcolor[])
|
|
|
409786 |
-/* Find the closest colormap entry for each cell in the update box,
|
|
|
409786 |
- * given the list of candidate colors prepared by find_nearby_colors.
|
|
|
409786 |
- * Return the indexes of the closest entries in the bestcolor[] array.
|
|
|
409786 |
- * This routine uses Thomas' incremental distance calculation method to
|
|
|
409786 |
- * find the distance from a colormap entry to successive cells in the box.
|
|
|
409786 |
- */
|
|
|
409786 |
-{
|
|
|
409786 |
- int ic0, ic1, ic2, ic3;
|
|
|
409786 |
- int i, icolor;
|
|
|
409786 |
- register int *bptr; /* pointer into bestdist[] array */
|
|
|
409786 |
- int *cptr; /* pointer into bestcolor[] array */
|
|
|
409786 |
- int dist0, dist1, dist2; /* initial distance values */
|
|
|
409786 |
- register int dist3 = 0; /* current distance in inner loop */
|
|
|
409786 |
- int xx0, xx1, xx2; /* distance increments */
|
|
|
409786 |
- register int xx3 = 0;
|
|
|
409786 |
- int inc0, inc1, inc2, inc3; /* initial values for increments */
|
|
|
409786 |
- /* This array holds the distance to the nearest-so-far color for each cell */
|
|
|
409786 |
- int bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS * BOX_C3_ELEMS];
|
|
|
409786 |
-
|
|
|
409786 |
- /* Initialize best-distance for each cell of the update box */
|
|
|
409786 |
- bptr = bestdist;
|
|
|
409786 |
- for (i = BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS * BOX_C3_ELEMS - 1; i >= 0; i--)
|
|
|
409786 |
- *bptr++ = 0x7FFFFFFFL;
|
|
|
409786 |
-
|
|
|
409786 |
- /* For each color selected by find_nearby_colors,
|
|
|
409786 |
- * compute its distance to the center of each cell in the box.
|
|
|
409786 |
- * If that's less than best-so-far, update best distance and color number.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
- /* Nominal steps between cell centers ("x" in Thomas article) */
|
|
|
409786 |
-#define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE)
|
|
|
409786 |
-#define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE)
|
|
|
409786 |
-#define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE)
|
|
|
409786 |
-#define STEP_C3 ((1 << C3_SHIFT) * C3_SCALE)
|
|
|
409786 |
-
|
|
|
409786 |
- for (i = 0; i < numcolors; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- icolor = colorlist[i];
|
|
|
409786 |
- /* Compute (square of) distance from minc0/c1/c2 to this color */
|
|
|
409786 |
- inc0 = (minc0 - (im->red[icolor])) * C0_SCALE;
|
|
|
409786 |
- dist0 = inc0 * inc0;
|
|
|
409786 |
- inc1 = (minc1 - (im->green[icolor])) * C1_SCALE;
|
|
|
409786 |
- dist0 += inc1 * inc1;
|
|
|
409786 |
- inc2 = (minc2 - (im->blue[icolor])) * C2_SCALE;
|
|
|
409786 |
- dist0 += inc2 * inc2;
|
|
|
409786 |
- inc3 = (minc3 - (im->alpha[icolor])) * C3_SCALE;
|
|
|
409786 |
- dist0 += inc3 * inc3;
|
|
|
409786 |
- /* Form the initial difference increments */
|
|
|
409786 |
- inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
|
|
|
409786 |
- inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
|
|
|
409786 |
- inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
|
|
|
409786 |
- inc3 = inc3 * (2 * STEP_C3) + STEP_C3 * STEP_C3;
|
|
|
409786 |
- /* Now loop over all cells in box, updating distance per Thomas method */
|
|
|
409786 |
- bptr = bestdist;
|
|
|
409786 |
- cptr = bestcolor;
|
|
|
409786 |
- xx0 = inc0;
|
|
|
409786 |
- for (ic0 = BOX_C0_ELEMS - 1; ic0 >= 0; ic0--)
|
|
|
409786 |
- {
|
|
|
409786 |
- dist1 = dist0;
|
|
|
409786 |
- xx1 = inc1;
|
|
|
409786 |
- for (ic1 = BOX_C1_ELEMS - 1; ic1 >= 0; ic1--)
|
|
|
409786 |
- {
|
|
|
409786 |
- dist2 = dist1;
|
|
|
409786 |
- xx2 = inc2;
|
|
|
409786 |
- for (ic2 = BOX_C2_ELEMS - 1; ic2 >= 0; ic2--)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (ic3 = BOX_C3_ELEMS - 1; ic3 >= 0; ic3--)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (dist3 < *bptr)
|
|
|
409786 |
- {
|
|
|
409786 |
- *bptr = dist3;
|
|
|
409786 |
- *cptr = icolor;
|
|
|
409786 |
- }
|
|
|
409786 |
- dist3 += xx3;
|
|
|
409786 |
- xx3 += 2 * STEP_C3 * STEP_C3;
|
|
|
409786 |
- bptr++;
|
|
|
409786 |
- cptr++;
|
|
|
409786 |
- }
|
|
|
409786 |
- dist2 += xx2;
|
|
|
409786 |
- xx2 += 2 * STEP_C2 * STEP_C2;
|
|
|
409786 |
- }
|
|
|
409786 |
- dist1 += xx1;
|
|
|
409786 |
- xx1 += 2 * STEP_C1 * STEP_C1;
|
|
|
409786 |
- }
|
|
|
409786 |
- dist0 += xx0;
|
|
|
409786 |
- xx0 += 2 * STEP_C0 * STEP_C0;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-fill_inverse_cmap (gdImagePtr im, my_cquantize_ptr cquantize,
|
|
|
409786 |
- int c0, int c1, int c2, int c3)
|
|
|
409786 |
-/* Fill the inverse-colormap entries in the update box that contains */
|
|
|
409786 |
-/* histogram cell c0/c1/c2/c3. (Only that one cell MUST be filled, but */
|
|
|
409786 |
-/* we can fill as many others as we wish.) */
|
|
|
409786 |
-{
|
|
|
409786 |
- hist4d histogram = cquantize->histogram;
|
|
|
409786 |
- int minc0, minc1, minc2, minc3; /* lower left corner of update box */
|
|
|
409786 |
- int ic0, ic1, ic2, ic3;
|
|
|
409786 |
- register int *cptr; /* pointer into bestcolor[] array */
|
|
|
409786 |
- register histptr cachep; /* pointer into main cache array */
|
|
|
409786 |
- /* This array lists the candidate colormap indexes. */
|
|
|
409786 |
- int colorlist[MAXNUMCOLORS];
|
|
|
409786 |
- int numcolors; /* number of candidate colors */
|
|
|
409786 |
- /* This array holds the actually closest colormap index for each cell. */
|
|
|
409786 |
- int bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS * BOX_C3_ELEMS];
|
|
|
409786 |
-
|
|
|
409786 |
- /* Convert cell coordinates to update box ID */
|
|
|
409786 |
- c0 >>= BOX_C0_LOG;
|
|
|
409786 |
- c1 >>= BOX_C1_LOG;
|
|
|
409786 |
- c2 >>= BOX_C2_LOG;
|
|
|
409786 |
- c3 >>= BOX_C3_LOG;
|
|
|
409786 |
-
|
|
|
409786 |
- /* Compute true coordinates of update box's origin corner.
|
|
|
409786 |
- * Actually we compute the coordinates of the center of the corner
|
|
|
409786 |
- * histogram cell, which are the lower bounds of the volume we care about.
|
|
|
409786 |
- */
|
|
|
409786 |
- minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
|
|
|
409786 |
- minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
|
|
|
409786 |
- minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
|
|
|
409786 |
- minc3 = (c3 << BOX_C3_SHIFT) + ((1 << C3_SHIFT) >> 1);
|
|
|
409786 |
- /* Determine which colormap entries are close enough to be candidates
|
|
|
409786 |
- * for the nearest entry to some cell in the update box.
|
|
|
409786 |
- */
|
|
|
409786 |
- numcolors = find_nearby_colors (im, minc0, minc1, minc2, minc3, colorlist);
|
|
|
409786 |
-
|
|
|
409786 |
- /* Determine the actually nearest colors. */
|
|
|
409786 |
- find_best_colors (im, minc0, minc1, minc2, minc3, numcolors, colorlist,
|
|
|
409786 |
- bestcolor);
|
|
|
409786 |
-
|
|
|
409786 |
- /* Save the best color numbers (plus 1) in the main cache array */
|
|
|
409786 |
- c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */
|
|
|
409786 |
- c1 <<= BOX_C1_LOG;
|
|
|
409786 |
- c2 <<= BOX_C2_LOG;
|
|
|
409786 |
- c3 <<= BOX_C3_LOG;
|
|
|
409786 |
- cptr = bestcolor;
|
|
|
409786 |
- for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++)
|
|
|
409786 |
- {
|
|
|
409786 |
- cachep = &histogram[c0 + ic0][c1 + ic1][c2 + ic2][c3];
|
|
|
409786 |
- for (ic3 = 0; ic3 < BOX_C3_ELEMS; ic3++)
|
|
|
409786 |
- {
|
|
|
409786 |
- *cachep++ = (histcell) ((*cptr++) + 1);
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * Map some rows of pixels to the output colormapped representation.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-pass2_no_dither (gdImagePtr im, my_cquantize_ptr cquantize)
|
|
|
409786 |
-/* This version performs no dithering */
|
|
|
409786 |
-{
|
|
|
409786 |
- hist4d histogram = cquantize->histogram;
|
|
|
409786 |
- register int *inptr;
|
|
|
409786 |
- register unsigned char *outptr;
|
|
|
409786 |
- register histptr cachep;
|
|
|
409786 |
- register int c0, c1, c2, c3;
|
|
|
409786 |
- int row;
|
|
|
409786 |
- int col;
|
|
|
409786 |
- int width = im->sx;
|
|
|
409786 |
- int num_rows = im->sy;
|
|
|
409786 |
- for (row = 0; row < num_rows; row++)
|
|
|
409786 |
- {
|
|
|
409786 |
- inptr = im->tpixels[row];
|
|
|
409786 |
- outptr = im->pixels[row];
|
|
|
409786 |
- for (col = 0; col < width; col++)
|
|
|
409786 |
- {
|
|
|
409786 |
- int r, g, b, a;
|
|
|
409786 |
- /* get pixel value and index into the cache */
|
|
|
409786 |
- r = gdTrueColorGetRed (*inptr);
|
|
|
409786 |
- g = gdTrueColorGetGreen (*inptr);
|
|
|
409786 |
- b = gdTrueColorGetBlue (*inptr);
|
|
|
409786 |
- a = gdTrueColorGetAlpha (*inptr++);
|
|
|
409786 |
- c0 = r >> C0_SHIFT;
|
|
|
409786 |
- c1 = g >> C1_SHIFT;
|
|
|
409786 |
- c2 = b >> C2_SHIFT;
|
|
|
409786 |
- c3 = a >> C3_SHIFT;
|
|
|
409786 |
- cachep = &histogram[c0][c1][c2][c3];
|
|
|
409786 |
- /* If we have not seen this color before, find nearest colormap entry */
|
|
|
409786 |
- /* and update the cache */
|
|
|
409786 |
- if (*cachep == 0)
|
|
|
409786 |
- {
|
|
|
409786 |
-#if 0
|
|
|
409786 |
- /* TBB: quick and dirty approach for use when testing
|
|
|
409786 |
- fill_inverse_cmap for errors */
|
|
|
409786 |
- int i;
|
|
|
409786 |
- int best = -1;
|
|
|
409786 |
- int mindist = 0x7FFFFFFF;
|
|
|
409786 |
- for (i = 0; (i < im->colorsTotal); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- int rdist = (im->red[i] >> C0_SHIFT) - c0;
|
|
|
409786 |
- int gdist = (im->green[i] >> C1_SHIFT) - c1;
|
|
|
409786 |
- int bdist = (im->blue[i] >> C2_SHIFT) - c2;
|
|
|
409786 |
- int adist = (im->alpha[i] >> C3_SHIFT) - c3;
|
|
|
409786 |
- int dist = (rdist * rdist) * R_SCALE +
|
|
|
409786 |
- (gdist * gdist) * G_SCALE +
|
|
|
409786 |
- (bdist * bdist) * B_SCALE +
|
|
|
409786 |
- (adist * adist) * A_SCALE;
|
|
|
409786 |
- if (dist < mindist)
|
|
|
409786 |
- {
|
|
|
409786 |
- best = i;
|
|
|
409786 |
- mindist = dist;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- *cachep = best + 1;
|
|
|
409786 |
-#endif
|
|
|
409786 |
- fill_inverse_cmap (im, cquantize, c0, c1, c2, c3);
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Now emit the colormap index for this cell */
|
|
|
409786 |
- *outptr++ = (*cachep - 1);
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-/* We assume that right shift corresponds to signed division by 2 with
|
|
|
409786 |
- * rounding towards minus infinity. This is correct for typical "arithmetic
|
|
|
409786 |
- * shift" instructions that shift in copies of the sign bit. But some
|
|
|
409786 |
- * C compilers implement >> with an unsigned shift. For these machines you
|
|
|
409786 |
- * must define RIGHT_SHIFT_IS_UNSIGNED.
|
|
|
409786 |
- * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
|
|
|
409786 |
- * It is only applied with constant shift counts. SHIFT_TEMPS must be
|
|
|
409786 |
- * included in the variables of any routine using RIGHT_SHIFT.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-#ifdef RIGHT_SHIFT_IS_UNSIGNED
|
|
|
409786 |
-#define SHIFT_TEMPS INT32 shift_temp;
|
|
|
409786 |
-#define RIGHT_SHIFT(x,shft) \
|
|
|
409786 |
- ((shift_temp = (x)) < 0 ? \
|
|
|
409786 |
- (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
|
|
|
409786 |
- (shift_temp >> (shft)))
|
|
|
409786 |
-#else
|
|
|
409786 |
-#define SHIFT_TEMPS
|
|
|
409786 |
-#define RIGHT_SHIFT(x,shft) ((x) >> (shft))
|
|
|
409786 |
-#endif
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-pass2_fs_dither (gdImagePtr im, my_cquantize_ptr cquantize)
|
|
|
409786 |
-
|
|
|
409786 |
-/* This version performs Floyd-Steinberg dithering */
|
|
|
409786 |
-{
|
|
|
409786 |
- hist4d histogram = cquantize->histogram;
|
|
|
409786 |
- register LOCFSERROR cur0, cur1, cur2, cur3; /* current error or pixel value */
|
|
|
409786 |
- LOCFSERROR belowerr0, belowerr1, belowerr2, belowerr3; /* error for pixel below cur */
|
|
|
409786 |
- LOCFSERROR bpreverr0, bpreverr1, bpreverr2, bpreverr3; /* error for below/prev col */
|
|
|
409786 |
- register FSERRPTR errorptr; /* => fserrors[] at column before current */
|
|
|
409786 |
- int *inptr; /* => current input pixel */
|
|
|
409786 |
- unsigned char *outptr; /* => current output pixel */
|
|
|
409786 |
- histptr cachep;
|
|
|
409786 |
- int dir; /* +1 or -1 depending on direction */
|
|
|
409786 |
- int dir4; /* 4*dir, for advancing errorptr */
|
|
|
409786 |
- int row;
|
|
|
409786 |
- int col;
|
|
|
409786 |
- int width = im->sx;
|
|
|
409786 |
- int num_rows = im->sy;
|
|
|
409786 |
- int *error_limit = cquantize->error_limiter;
|
|
|
409786 |
- int *colormap0 = im->red;
|
|
|
409786 |
- int *colormap1 = im->green;
|
|
|
409786 |
- int *colormap2 = im->blue;
|
|
|
409786 |
- int *colormap3 = im->alpha;
|
|
|
409786 |
- SHIFT_TEMPS
|
|
|
409786 |
-
|
|
|
409786 |
- for (row = 0; row < num_rows; row++)
|
|
|
409786 |
- {
|
|
|
409786 |
- inptr = im->tpixels[row];
|
|
|
409786 |
- outptr = im->pixels[row];
|
|
|
409786 |
- if (cquantize->on_odd_row)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* work right to left in this row */
|
|
|
409786 |
- inptr += (width - 1); /* so point to rightmost pixel */
|
|
|
409786 |
- outptr += width - 1;
|
|
|
409786 |
- dir = -1;
|
|
|
409786 |
- dir4 = -4;
|
|
|
409786 |
- errorptr = cquantize->fserrors + (width + 1) * 4; /* => entry after last column */
|
|
|
409786 |
- cquantize->on_odd_row = FALSE; /* flip for next time */
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- /* work left to right in this row */
|
|
|
409786 |
- dir = 1;
|
|
|
409786 |
- dir4 = 4;
|
|
|
409786 |
- errorptr = cquantize->fserrors; /* => entry before first real column */
|
|
|
409786 |
- cquantize->on_odd_row = TRUE; /* flip for next time */
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Preset error values: no error propagated to first pixel from left */
|
|
|
409786 |
- cur0 = cur1 = cur2 = cur3 = 0;
|
|
|
409786 |
- /* and no error propagated to row below yet */
|
|
|
409786 |
- belowerr0 = belowerr1 = belowerr2 = belowerr3 = 0;
|
|
|
409786 |
- bpreverr0 = bpreverr1 = bpreverr2 = bpreverr3 = 0;
|
|
|
409786 |
-
|
|
|
409786 |
- for (col = width; col > 0; col--)
|
|
|
409786 |
- {
|
|
|
409786 |
- int a;
|
|
|
409786 |
- /* curN holds the error propagated from the previous pixel on the
|
|
|
409786 |
- * current line. Add the error propagated from the previous line
|
|
|
409786 |
- * to form the complete error correction term for this pixel, and
|
|
|
409786 |
- * round the error term (which is expressed * 16) to an integer.
|
|
|
409786 |
- * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
|
|
|
409786 |
- * for either sign of the error value.
|
|
|
409786 |
- * Note: errorptr points to *previous* column's array entry.
|
|
|
409786 |
- */
|
|
|
409786 |
- cur0 = RIGHT_SHIFT (cur0 + errorptr[dir4 + 0] + 8, 4);
|
|
|
409786 |
- cur1 = RIGHT_SHIFT (cur1 + errorptr[dir4 + 1] + 8, 4);
|
|
|
409786 |
- cur2 = RIGHT_SHIFT (cur2 + errorptr[dir4 + 2] + 8, 4);
|
|
|
409786 |
- cur3 = RIGHT_SHIFT (cur3 + errorptr[dir4 + 3] + 8, 4);
|
|
|
409786 |
- /* Limit the error using transfer function set by init_error_limit.
|
|
|
409786 |
- * See comments with init_error_limit for rationale.
|
|
|
409786 |
- */
|
|
|
409786 |
- cur0 = error_limit[cur0];
|
|
|
409786 |
- cur1 = error_limit[cur1];
|
|
|
409786 |
- cur2 = error_limit[cur2];
|
|
|
409786 |
- cur3 = error_limit[cur3];
|
|
|
409786 |
- /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
|
|
|
409786 |
- * The maximum error is +- MAXJSAMPLE (or less with error limiting);
|
|
|
409786 |
- * but we'll be lazy and just clamp this with an if test (TBB).
|
|
|
409786 |
- */
|
|
|
409786 |
- cur0 += gdTrueColorGetRed (*inptr);
|
|
|
409786 |
- cur1 += gdTrueColorGetGreen (*inptr);
|
|
|
409786 |
- cur2 += gdTrueColorGetBlue (*inptr);
|
|
|
409786 |
- /* Expand to 8 bits for consistency with dithering algorithm -- TBB */
|
|
|
409786 |
- a = gdTrueColorGetAlpha (*inptr);
|
|
|
409786 |
- cur3 += (a << 1) + (a >> 6);
|
|
|
409786 |
- if (cur0 < 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur0 = 0;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur0 > 255)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur0 = 255;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur1 < 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur1 = 0;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur1 > 255)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur1 = 255;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur2 < 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur2 = 0;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur2 > 255)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur2 = 255;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur3 < 0)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur3 = 0;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cur3 > 255)
|
|
|
409786 |
- {
|
|
|
409786 |
- cur3 = 255;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Index into the cache with adjusted pixel value */
|
|
|
409786 |
- cachep = &histogram
|
|
|
409786 |
- [cur0 >> C0_SHIFT]
|
|
|
409786 |
- [cur1 >> C1_SHIFT]
|
|
|
409786 |
- [cur2 >> C2_SHIFT]
|
|
|
409786 |
- [cur3 >> (C3_SHIFT + 1)];
|
|
|
409786 |
- /* If we have not seen this color before, find nearest colormap */
|
|
|
409786 |
- /* entry and update the cache */
|
|
|
409786 |
- if (*cachep == 0)
|
|
|
409786 |
- fill_inverse_cmap (im, cquantize,
|
|
|
409786 |
- cur0 >> C0_SHIFT, cur1 >> C1_SHIFT, cur2 >> C2_SHIFT,
|
|
|
409786 |
- cur3 >> (C3_SHIFT + 1));
|
|
|
409786 |
- /* Now emit the colormap index for this cell */
|
|
|
409786 |
- {
|
|
|
409786 |
- register int pixcode = *cachep - 1;
|
|
|
409786 |
- *outptr = pixcode;
|
|
|
409786 |
- /* Compute representation error for this pixel */
|
|
|
409786 |
- cur0 -= colormap0[pixcode];
|
|
|
409786 |
- cur1 -= colormap1[pixcode];
|
|
|
409786 |
- cur2 -= colormap2[pixcode];
|
|
|
409786 |
- cur3 -= ((colormap3[pixcode] << 1) + (colormap3[pixcode] >> 6));
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Compute error fractions to be propagated to adjacent pixels.
|
|
|
409786 |
- * Add these into the running sums, and simultaneously shift the
|
|
|
409786 |
- * next-line error sums left by 1 column.
|
|
|
409786 |
- */
|
|
|
409786 |
- {
|
|
|
409786 |
- register LOCFSERROR bnexterr, delta;
|
|
|
409786 |
-
|
|
|
409786 |
- bnexterr = cur0; /* Process component 0 */
|
|
|
409786 |
- delta = cur0 * 2;
|
|
|
409786 |
- cur0 += delta; /* form error * 3 */
|
|
|
409786 |
- errorptr[0] = (FSERROR) (bpreverr0 + cur0);
|
|
|
409786 |
- cur0 += delta; /* form error * 5 */
|
|
|
409786 |
- bpreverr0 = belowerr0 + cur0;
|
|
|
409786 |
- belowerr0 = bnexterr;
|
|
|
409786 |
- cur0 += delta; /* form error * 7 */
|
|
|
409786 |
- bnexterr = cur1; /* Process component 1 */
|
|
|
409786 |
- delta = cur1 * 2;
|
|
|
409786 |
- cur1 += delta; /* form error * 3 */
|
|
|
409786 |
- errorptr[1] = (FSERROR) (bpreverr1 + cur1);
|
|
|
409786 |
- cur1 += delta; /* form error * 5 */
|
|
|
409786 |
- bpreverr1 = belowerr1 + cur1;
|
|
|
409786 |
- belowerr1 = bnexterr;
|
|
|
409786 |
- cur1 += delta; /* form error * 7 */
|
|
|
409786 |
- bnexterr = cur2; /* Process component 2 */
|
|
|
409786 |
- delta = cur2 * 2;
|
|
|
409786 |
- cur2 += delta; /* form error * 3 */
|
|
|
409786 |
- errorptr[2] = (FSERROR) (bpreverr2 + cur2);
|
|
|
409786 |
- cur2 += delta; /* form error * 5 */
|
|
|
409786 |
- bpreverr2 = belowerr2 + cur2;
|
|
|
409786 |
- belowerr2 = bnexterr;
|
|
|
409786 |
- cur2 += delta; /* form error * 7 */
|
|
|
409786 |
- bnexterr = cur3; /* Process component 3 */
|
|
|
409786 |
- delta = cur3 * 2;
|
|
|
409786 |
- cur3 += delta; /* form error * 3 */
|
|
|
409786 |
- errorptr[3] = (FSERROR) (bpreverr3 + cur3);
|
|
|
409786 |
- cur3 += delta; /* form error * 5 */
|
|
|
409786 |
- bpreverr3 = belowerr3 + cur3;
|
|
|
409786 |
- belowerr3 = bnexterr;
|
|
|
409786 |
- cur3 += delta; /* form error * 7 */
|
|
|
409786 |
- }
|
|
|
409786 |
- /* At this point curN contains the 7/16 error value to be propagated
|
|
|
409786 |
- * to the next pixel on the current line, and all the errors for the
|
|
|
409786 |
- * next line have been shifted over. We are therefore ready to move on.
|
|
|
409786 |
- */
|
|
|
409786 |
- inptr += dir; /* Advance pixel pointers to next column */
|
|
|
409786 |
- outptr += dir;
|
|
|
409786 |
- errorptr += dir4; /* advance errorptr to current column */
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Post-loop cleanup: we must unload the final error values into the
|
|
|
409786 |
- * final fserrors[] entry. Note we need not unload belowerrN because
|
|
|
409786 |
- * it is for the dummy column before or after the actual array.
|
|
|
409786 |
- */
|
|
|
409786 |
- errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
|
|
|
409786 |
- errorptr[1] = (FSERROR) bpreverr1;
|
|
|
409786 |
- errorptr[2] = (FSERROR) bpreverr2;
|
|
|
409786 |
- errorptr[3] = (FSERROR) bpreverr3;
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-
|
|
|
409786 |
-/*
|
|
|
409786 |
- * Initialize the error-limiting transfer function (lookup table).
|
|
|
409786 |
- * The raw F-S error computation can potentially compute error values of up to
|
|
|
409786 |
- * +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be
|
|
|
409786 |
- * much less, otherwise obviously wrong pixels will be created. (Typical
|
|
|
409786 |
- * effects include weird fringes at color-area boundaries, isolated bright
|
|
|
409786 |
- * pixels in a dark area, etc.) The standard advice for avoiding this problem
|
|
|
409786 |
- * is to ensure that the "corners" of the color cube are allocated as output
|
|
|
409786 |
- * colors; then repeated errors in the same direction cannot cause cascading
|
|
|
409786 |
- * error buildup. However, that only prevents the error from getting
|
|
|
409786 |
- * completely out of hand; Aaron Giles reports that error limiting improves
|
|
|
409786 |
- * the results even with corner colors allocated.
|
|
|
409786 |
- * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
|
|
|
409786 |
- * well, but the smoother transfer function used below is even better. Thanks
|
|
|
409786 |
- * to Aaron Giles for this idea.
|
|
|
409786 |
- */
|
|
|
409786 |
-
|
|
|
409786 |
-static int
|
|
|
409786 |
-init_error_limit (my_cquantize_ptr cquantize)
|
|
|
409786 |
-/* Allocate and fill in the error_limiter table */
|
|
|
409786 |
-{
|
|
|
409786 |
- int *table;
|
|
|
409786 |
- int in, out;
|
|
|
409786 |
-
|
|
|
409786 |
- cquantize->error_limiter_storage = (int *) gdMalloc ((255 * 2 + 1) * sizeof (int));
|
|
|
409786 |
- if (!cquantize->error_limiter_storage)
|
|
|
409786 |
- {
|
|
|
409786 |
- return 0;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
|
|
|
409786 |
- cquantize->error_limiter = cquantize->error_limiter_storage + 255;
|
|
|
409786 |
- table = cquantize->error_limiter;
|
|
|
409786 |
-#define STEPSIZE ((255+1)/16)
|
|
|
409786 |
- /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
|
|
|
409786 |
- out = 0;
|
|
|
409786 |
- for (in = 0; in < STEPSIZE; in++, out++)
|
|
|
409786 |
- {
|
|
|
409786 |
- table[in] = out;
|
|
|
409786 |
- table[-in] = -out;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
|
|
|
409786 |
- for (; in < STEPSIZE * 3; in++, out += (in & 1) ? 0 : 1)
|
|
|
409786 |
- {
|
|
|
409786 |
- table[in] = out;
|
|
|
409786 |
- table[-in] = -out;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
|
|
|
409786 |
- for (; in <= 255; in++)
|
|
|
409786 |
- {
|
|
|
409786 |
- table[in] = out;
|
|
|
409786 |
- table[-in] = -out;
|
|
|
409786 |
- }
|
|
|
409786 |
-#undef STEPSIZE
|
|
|
409786 |
- return 1;
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-static void
|
|
|
409786 |
-zeroHistogram (hist4d histogram)
|
|
|
409786 |
-{
|
|
|
409786 |
- int i;
|
|
|
409786 |
- int j;
|
|
|
409786 |
- /* Zero the histogram or inverse color map */
|
|
|
409786 |
- for (i = 0; i < HIST_C0_ELEMS; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- for (j = 0; j < HIST_C1_ELEMS; j++)
|
|
|
409786 |
- {
|
|
|
409786 |
- memset (histogram[i][j],
|
|
|
409786 |
- 0,
|
|
|
409786 |
- HIST_C2_ELEMS * HIST_C3_ELEMS * sizeof (histcell));
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
-}
|
|
|
409786 |
-
|
|
|
409786 |
-/* Here we go at last. */
|
|
|
409786 |
-void
|
|
|
409786 |
-gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted)
|
|
|
409786 |
-{
|
|
|
409786 |
- my_cquantize_ptr cquantize = 0;
|
|
|
409786 |
- int i;
|
|
|
409786 |
- size_t arraysize;
|
|
|
409786 |
- if (!im->trueColor)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* Nothing to do! */
|
|
|
409786 |
- return;
|
|
|
409786 |
- }
|
|
|
409786 |
- if (colorsWanted > gdMaxColors)
|
|
|
409786 |
- {
|
|
|
409786 |
- colorsWanted = gdMaxColors;
|
|
|
409786 |
- }
|
|
|
409786 |
- im->pixels = gdCalloc (sizeof (unsigned char *), im->sy);
|
|
|
409786 |
- if (!im->pixels)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* No can do */
|
|
|
409786 |
- goto outOfMemory;
|
|
|
409786 |
- }
|
|
|
409786 |
- for (i = 0; (i < im->sy); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- im->pixels[i] = (unsigned char *) gdCalloc (sizeof (unsigned char), im->sx);
|
|
|
409786 |
- if (!im->pixels[i])
|
|
|
409786 |
- {
|
|
|
409786 |
- goto outOfMemory;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- cquantize = (my_cquantize_ptr) gdCalloc (sizeof (my_cquantizer), 1);
|
|
|
409786 |
- if (!cquantize)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* No can do */
|
|
|
409786 |
- goto outOfMemory;
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Allocate the histogram/inverse colormap storage */
|
|
|
409786 |
- cquantize->histogram = (hist4d) gdMalloc (HIST_C0_ELEMS * sizeof (hist3d));
|
|
|
409786 |
- for (i = 0; i < HIST_C0_ELEMS; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- int j;
|
|
|
409786 |
- cquantize->histogram[i] = (hist3d) gdCalloc (HIST_C1_ELEMS,
|
|
|
409786 |
- sizeof (hist2d));
|
|
|
409786 |
- if (!cquantize->histogram[i])
|
|
|
409786 |
- {
|
|
|
409786 |
- goto outOfMemory;
|
|
|
409786 |
- }
|
|
|
409786 |
- for (j = 0; (j < HIST_C1_ELEMS); j++)
|
|
|
409786 |
- {
|
|
|
409786 |
- cquantize->histogram[i][j] = (hist2d) gdCalloc (HIST_C2_ELEMS * HIST_C3_ELEMS,
|
|
|
409786 |
- sizeof (histcell));
|
|
|
409786 |
- if (!cquantize->histogram[i][j])
|
|
|
409786 |
- {
|
|
|
409786 |
- goto outOfMemory;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- cquantize->fserrors = (FSERRPTR) gdMalloc (4 * sizeof (FSERROR));
|
|
|
409786 |
- init_error_limit (cquantize);
|
|
|
409786 |
- arraysize = (size_t) ((im->sx + 2) *
|
|
|
409786 |
- (4 * sizeof (FSERROR)));
|
|
|
409786 |
- /* Allocate Floyd-Steinberg workspace. */
|
|
|
409786 |
- cquantize->fserrors = gdCalloc (arraysize, 1);
|
|
|
409786 |
- if (!cquantize->fserrors)
|
|
|
409786 |
- {
|
|
|
409786 |
- goto outOfMemory;
|
|
|
409786 |
- }
|
|
|
409786 |
- cquantize->on_odd_row = FALSE;
|
|
|
409786 |
-
|
|
|
409786 |
- /* Do the work! */
|
|
|
409786 |
- zeroHistogram (cquantize->histogram);
|
|
|
409786 |
- prescan_quantize (im, cquantize);
|
|
|
409786 |
- select_colors (im, cquantize, 256);
|
|
|
409786 |
- /* TBB HACK REMOVE */
|
|
|
409786 |
- {
|
|
|
409786 |
- FILE *out = fopen ("palettemap.png", "wb");
|
|
|
409786 |
- int i;
|
|
|
409786 |
- gdImagePtr im2 = gdImageCreateTrueColor (256, 256);
|
|
|
409786 |
- for (i = 0; (i < 256); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- gdImageFilledRectangle (im2, (i % 16) * 16, (i / 16) * 16,
|
|
|
409786 |
- (i % 16) * 16 + 15, (i / 16) * 16 + 15,
|
|
|
409786 |
- gdTrueColorAlpha (im->red[i], im->green[i],
|
|
|
409786 |
- im->blue[i], im->alpha[i]));
|
|
|
409786 |
- }
|
|
|
409786 |
- gdImagePng (im2, out);
|
|
|
409786 |
- fclose (out);
|
|
|
409786 |
- gdImageDestroy (im2);
|
|
|
409786 |
- }
|
|
|
409786 |
- zeroHistogram (cquantize->histogram);
|
|
|
409786 |
- if (dither)
|
|
|
409786 |
- {
|
|
|
409786 |
- pass2_fs_dither (im, cquantize);
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- pass2_no_dither (im, cquantize);
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cquantize->transparentIsPresent)
|
|
|
409786 |
- {
|
|
|
409786 |
- int mt = -1;
|
|
|
409786 |
- for (i = 0; (i < im->colorsTotal); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (im->alpha[i] > mt)
|
|
|
409786 |
- {
|
|
|
409786 |
- mt = im->alpha[i];
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- for (i = 0; (i < im->colorsTotal); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (im->alpha[i] == mt)
|
|
|
409786 |
- {
|
|
|
409786 |
- im->alpha[i] = gdAlphaTransparent;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cquantize->opaqueIsPresent)
|
|
|
409786 |
- {
|
|
|
409786 |
- int mo = 128;
|
|
|
409786 |
- int moIndex = -1;
|
|
|
409786 |
- for (i = 0; (i < im->colorsTotal); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (im->alpha[i] < mo)
|
|
|
409786 |
- {
|
|
|
409786 |
- moIndex = i;
|
|
|
409786 |
- mo = im->alpha[i];
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- for (i = 0; (i < im->colorsTotal); i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (im->alpha[i] == mo)
|
|
|
409786 |
- {
|
|
|
409786 |
- im->alpha[i] = gdAlphaOpaque;
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- /* Success! Get rid of the truecolor image data. */
|
|
|
409786 |
- im->trueColor = 0;
|
|
|
409786 |
- /* Junk the truecolor pixels */
|
|
|
409786 |
- for (i = 0; i < im->sy; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- gdFree (im->tpixels[i]);
|
|
|
409786 |
- }
|
|
|
409786 |
- gdFree (im->tpixels);
|
|
|
409786 |
- im->tpixels = 0;
|
|
|
409786 |
- /* Tediously free stuff. */
|
|
|
409786 |
-outOfMemory:
|
|
|
409786 |
- if (im->trueColor)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (im->pixels)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* On failure only */
|
|
|
409786 |
- for (i = 0; i < im->sy; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (im->pixels[i])
|
|
|
409786 |
- {
|
|
|
409786 |
- gdFree (im->pixels[i]);
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- gdFree (im->pixels);
|
|
|
409786 |
- }
|
|
|
409786 |
- im->pixels = 0;
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
- if (!cquantize)
|
|
|
409786 |
- return;
|
|
|
409786 |
-
|
|
|
409786 |
- for (i = 0; i < HIST_C0_ELEMS; i++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (cquantize->histogram[i])
|
|
|
409786 |
- {
|
|
|
409786 |
- int j;
|
|
|
409786 |
- for (j = 0; j < HIST_C1_ELEMS; j++)
|
|
|
409786 |
- {
|
|
|
409786 |
- if (cquantize->histogram[i][j])
|
|
|
409786 |
- {
|
|
|
409786 |
- gdFree (cquantize->histogram[i][j]);
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- gdFree (cquantize->histogram[i]);
|
|
|
409786 |
- }
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cquantize->histogram)
|
|
|
409786 |
- {
|
|
|
409786 |
- gdFree (cquantize->histogram);
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cquantize->fserrors)
|
|
|
409786 |
- {
|
|
|
409786 |
- gdFree (cquantize->fserrors);
|
|
|
409786 |
- }
|
|
|
409786 |
- if (cquantize->error_limiter_storage)
|
|
|
409786 |
- {
|
|
|
409786 |
- gdFree (cquantize->error_limiter_storage);
|
|
|
409786 |
- }
|
|
|
409786 |
- gdFree (cquantize);
|
|
|
409786 |
-}
|
|
|
409786 |
diff --git a/src/extra/gd/gd_wbmp.c b/src/extra/gd/gd_wbmp.c
|
|
|
409786 |
index 5281337..f1258da 100644
|
|
|
409786 |
--- a/src/extra/gd/gd_wbmp.c
|
|
|
409786 |
+++ b/src/extra/gd/gd_wbmp.c
|
|
|
409786 |
@@ -105,7 +105,10 @@ gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
|
|
|
409786 |
|
|
|
409786 |
/* create the WBMP */
|
|
|
409786 |
if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL)
|
|
|
409786 |
- fprintf (stderr, "Could not create WBMP\n");
|
|
|
409786 |
+ {
|
|
|
409786 |
+ fprintf (stderr, "Could not create WBMP\n");
|
|
|
409786 |
+ return;
|
|
|
409786 |
+ }
|
|
|
409786 |
|
|
|
409786 |
/* fill up the WBMP structure */
|
|
|
409786 |
pos = 0;
|
|
|
409786 |
diff --git a/src/extra/gd/gdcache.c b/src/extra/gd/gdcache.c
|
|
|
409786 |
index e4770f5..ff65b97 100644
|
|
|
409786 |
--- a/src/extra/gd/gdcache.c
|
|
|
409786 |
+++ b/src/extra/gd/gdcache.c
|
|
|
409786 |
@@ -1,5 +1,6 @@
|
|
|
409786 |
#include "gd.h"
|
|
|
409786 |
#include "gdhelpers.h"
|
|
|
409786 |
+#include <assert.h>
|
|
|
409786 |
|
|
|
409786 |
#ifdef HAVE_LIBTTF
|
|
|
409786 |
#define NEED_CACHE 1
|
|
|
409786 |
@@ -130,7 +131,9 @@ gdCacheGet (gdCache_head_t * head, void *keydata)
|
|
|
409786 |
else
|
|
|
409786 |
{ /* cache full - replace least-recently-used */
|
|
|
409786 |
/* preveprev becomes new end of list */
|
|
|
409786 |
- prevprev->next = NULL;
|
|
|
409786 |
+ assert (prevprev);
|
|
|
409786 |
+ if (prevprev)
|
|
|
409786 |
+ prevprev->next = NULL;
|
|
|
409786 |
elem = prev;
|
|
|
409786 |
(*(head->gdCacheRelease)) (elem->userdata);
|
|
|
409786 |
}
|
|
|
409786 |
diff --git a/src/extra/gd/gdft.c b/src/extra/gd/gdft.c
|
|
|
409786 |
index b1e9414..33430f6 100644
|
|
|
409786 |
--- a/src/extra/gd/gdft.c
|
|
|
409786 |
+++ b/src/extra/gd/gdft.c
|
|
|
409786 |
@@ -574,6 +574,7 @@ gdft_draw_bitmap (gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
|
|
|
409786 |
|
|
|
409786 |
for (col = 0; col < bitmap.width; col++, pc++)
|
|
|
409786 |
{
|
|
|
409786 |
+ const int trueColor = im->trueColor;
|
|
|
409786 |
if (bitmap.pixel_mode == ft_pixel_mode_grays)
|
|
|
409786 |
{
|
|
|
409786 |
/*
|
|
|
409786 |
@@ -603,7 +604,7 @@ gdft_draw_bitmap (gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
|
|
|
409786 |
if (x >= im->sx || x < 0)
|
|
|
409786 |
continue;
|
|
|
409786 |
/* get pixel location in gd buffer */
|
|
|
409786 |
- if (im->trueColor)
|
|
|
409786 |
+ if (trueColor)
|
|
|
409786 |
{
|
|
|
409786 |
tpixel = &im->tpixels[y][x];
|
|
|
409786 |
}
|
|
|
409786 |
@@ -614,7 +615,7 @@ gdft_draw_bitmap (gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
|
|
|
409786 |
if (tc_key.pixel == NUMCOLORS)
|
|
|
409786 |
{
|
|
|
409786 |
/* use fg color directly */
|
|
|
409786 |
- if (im->trueColor)
|
|
|
409786 |
+ if (trueColor)
|
|
|
409786 |
{
|
|
|
409786 |
*tpixel = fg;
|
|
|
409786 |
}
|
|
|
409786 |
@@ -625,23 +626,17 @@ gdft_draw_bitmap (gdImage * im, int fg, FT_Bitmap bitmap, int pen_x, int pen_y)
|
|
|
409786 |
}
|
|
|
409786 |
else
|
|
|
409786 |
{
|
|
|
409786 |
+ tc_elem = (tweencolor_t *) gdCacheGet (tc_cache, &tc_key);
|
|
|
409786 |
+ if (!tc_elem) return tc_cache->error;
|
|
|
409786 |
/* find antialised color */
|
|
|
409786 |
- if (im->trueColor)
|
|
|
409786 |
+ if (trueColor)
|
|
|
409786 |
{
|
|
|
409786 |
tc_key.bgcolor = *tpixel;
|
|
|
409786 |
- }
|
|
|
409786 |
- else
|
|
|
409786 |
- {
|
|
|
409786 |
- tc_key.bgcolor = *pixel;
|
|
|
409786 |
- }
|
|
|
409786 |
- tc_elem = (tweencolor_t *) gdCacheGet (
|
|
|
409786 |
- tc_cache, &tc_key);
|
|
|
409786 |
- if (im->trueColor)
|
|
|
409786 |
- {
|
|
|
409786 |
*tpixel = tc_elem->tweencolor;
|
|
|
409786 |
}
|
|
|
409786 |
else
|
|
|
409786 |
{
|
|
|
409786 |
+ tc_key.bgcolor = *pixel;
|
|
|
409786 |
*pixel = tc_elem->tweencolor;
|
|
|
409786 |
}
|
|
|
409786 |
}
|
|
|
409786 |
diff --git a/src/extra/gd/testac.c b/src/extra/gd/testac.c
|
|
|
409786 |
index 55a5434..47d466d 100644
|
|
|
409786 |
--- a/src/extra/gd/testac.c
|
|
|
409786 |
+++ b/src/extra/gd/testac.c
|
|
|
409786 |
@@ -115,13 +115,6 @@ testDrawing (
|
|
|
409786 |
Otherwise the file would typically be slightly larger. */
|
|
|
409786 |
gdImageSaveAlpha (im_out, !blending);
|
|
|
409786 |
|
|
|
409786 |
- /* If requested, convert from truecolor to palette. */
|
|
|
409786 |
- if (palette)
|
|
|
409786 |
- {
|
|
|
409786 |
- /* Dithering, 256 colors. */
|
|
|
409786 |
- gdImageTrueColorToPalette (im_out, 1, 256);
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
gdImagePng (im_out, out);
|
|
|
409786 |
fclose (out);
|
|
|
409786 |
|
|
|
409786 |
diff --git a/src/extra/trio/strio.c b/src/extra/trio/strio.c
|
|
|
409786 |
index 0e7196c..c6796af 100644
|
|
|
409786 |
--- a/src/extra/trio/strio.c
|
|
|
409786 |
+++ b/src/extra/trio/strio.c
|
|
|
409786 |
@@ -20,8 +20,6 @@
|
|
|
409786 |
* - StrToLongDouble
|
|
|
409786 |
*/
|
|
|
409786 |
|
|
|
409786 |
-static const char rcsid[] = "@(#)$Id: strio.c,v 1.1 2001/06/07 08:23:02 fjfranklin Exp $";
|
|
|
409786 |
-
|
|
|
409786 |
#if defined(unix) || defined(__xlC__)
|
|
|
409786 |
# define PLATFORM_UNIX
|
|
|
409786 |
#elif defined(WIN32) || defined(_WIN32)
|
|
|
409786 |
diff --git a/src/extra/trio/trio.c b/src/extra/trio/trio.c
|
|
|
409786 |
index 83a2ce9..930e210 100644
|
|
|
409786 |
--- a/src/extra/trio/trio.c
|
|
|
409786 |
+++ b/src/extra/trio/trio.c
|
|
|
409786 |
@@ -41,8 +41,6 @@
|
|
|
409786 |
* immediately followed by an 's'.
|
|
|
409786 |
*/
|
|
|
409786 |
|
|
|
409786 |
-static const char rcsid[] = "@(#)$Id: trio.c,v 1.1 2001/06/07 08:23:02 fjfranklin Exp $";
|
|
|
409786 |
-
|
|
|
409786 |
#if defined(unix) || defined(__xlC__) /* AIX xlC workaround */
|
|
|
409786 |
# define PLATFORM_UNIX
|
|
|
409786 |
#elif defined(AMIGA) && defined(__GNUC__)
|
|
|
409786 |
diff --git a/src/font.c b/src/font.c
|
|
|
409786 |
index b3927c9..6b88499 100644
|
|
|
409786 |
--- a/src/font.c
|
|
|
409786 |
+++ b/src/font.c
|
|
|
409786 |
@@ -535,6 +535,8 @@ static void ipa_font_add_api (wmfAPI* API,char* name)
|
|
|
409786 |
float wmf_ipa_font_stringwidth (wmfAPI* API,wmfFont* font,char* str)
|
|
|
409786 |
{ FT_Face face = WMF_FONT_FTFACE (font);
|
|
|
409786 |
|
|
|
409786 |
+ if (!face) return 0.0;
|
|
|
409786 |
+
|
|
|
409786 |
FT_Vector delta;
|
|
|
409786 |
|
|
|
409786 |
FT_Bool use_kerning;
|
|
|
409786 |
@@ -592,6 +594,8 @@ float wmf_ipa_font_stringwidth (wmfAPI* API,wmfFont* font,char* str)
|
|
|
409786 |
static float ipa_char_position (wmfFont* font,char* str,char* last)
|
|
|
409786 |
{ FT_Face face = WMF_FONT_FTFACE (font);
|
|
|
409786 |
|
|
|
409786 |
+ if (!face) return 0.0;
|
|
|
409786 |
+
|
|
|
409786 |
FT_Vector delta;
|
|
|
409786 |
|
|
|
409786 |
FT_Bool use_kerning;
|
|
|
409786 |
@@ -1218,14 +1222,19 @@ static char* ipa_font_gs_readline (wmfAPI* API,FILE* in)
|
|
|
409786 |
line = more;
|
|
|
409786 |
|
|
|
409786 |
if (line == 0)
|
|
|
409786 |
- { fContinue = wmf_false;
|
|
|
409786 |
+ {
|
|
|
409786 |
break;
|
|
|
409786 |
}
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
if (line == 0) return (0);
|
|
|
409786 |
|
|
|
409786 |
- if (fReadExtra) while (buf[strlen(buf)-1] != '\n') fgets (buf,128,in);
|
|
|
409786 |
+ if (fReadExtra)
|
|
|
409786 |
+ {
|
|
|
409786 |
+ while (buf[strlen(buf)-1] != '\n')
|
|
|
409786 |
+ if (fgets (buf,128,in) == 0)
|
|
|
409786 |
+ break;
|
|
|
409786 |
+ }
|
|
|
409786 |
|
|
|
409786 |
/* Strip the string */
|
|
|
409786 |
|
|
|
409786 |
diff --git a/src/ipa/ipa/bmp.h b/src/ipa/ipa/bmp.h
|
|
|
409786 |
index 568d26e..94a1a17 100644
|
|
|
409786 |
--- a/src/ipa/ipa/bmp.h
|
|
|
409786 |
+++ b/src/ipa/ipa/bmp.h
|
|
|
409786 |
@@ -947,8 +947,8 @@ static int DecodeImage (wmfBMP* bmp,BMPSource* src,unsigned int compression,unsi
|
|
|
409786 |
}
|
|
|
409786 |
/* ?? TODO if (QuantumTick (y,image->rows)) MagickMonitor (LoadImageText,y,image->rows); */
|
|
|
409786 |
}
|
|
|
409786 |
- byte = ReadBlobByte (src); /* end of line */
|
|
|
409786 |
- byte = ReadBlobByte (src);
|
|
|
409786 |
+ ReadBlobByte (src); /* end of line */
|
|
|
409786 |
+ ReadBlobByte (src);
|
|
|
409786 |
|
|
|
409786 |
return 1;
|
|
|
409786 |
}
|
|
|
409786 |
diff --git a/src/ipa/svg.c b/src/ipa/svg.c
|
|
|
409786 |
index 0692296..9c4adf7 100644
|
|
|
409786 |
--- a/src/ipa/svg.c
|
|
|
409786 |
+++ b/src/ipa/svg.c
|
|
|
409786 |
@@ -156,7 +156,6 @@ static void wmf_svg_draw_text (wmfAPI* API,wmfDrawText_t* draw_text)
|
|
|
409786 |
svgPoint pt;
|
|
|
409786 |
|
|
|
409786 |
float font_height;
|
|
|
409786 |
- float font_ratio;
|
|
|
409786 |
|
|
|
409786 |
float sin_theta;
|
|
|
409786 |
float cos_theta;
|
|
|
409786 |
@@ -172,8 +171,6 @@ static void wmf_svg_draw_text (wmfAPI* API,wmfDrawText_t* draw_text)
|
|
|
409786 |
pt = svg_translate (API,draw_text->pt);
|
|
|
409786 |
|
|
|
409786 |
font_height = svg_height (API,(float) draw_text->font_height);
|
|
|
409786 |
- font_ratio = svg_width (API,(float) (draw_text->font_height * draw_text->font_ratio));
|
|
|
409786 |
- font_ratio /= font_height;
|
|
|
409786 |
|
|
|
409786 |
theta = - WMF_TEXT_ANGLE (draw_text->dc->font);
|
|
|
409786 |
|
|
|
409786 |
diff --git a/src/ipa/xgd/font.h b/src/ipa/xgd/font.h
|
|
|
409786 |
index 79b5d87..5b15e57 100644
|
|
|
409786 |
--- a/src/ipa/xgd/font.h
|
|
|
409786 |
+++ b/src/ipa/xgd/font.h
|
|
|
409786 |
@@ -157,10 +157,6 @@ static void gd_draw_ftbitmap (wmfAPI* API,FT_Bitmap* bitmap,gdPoint pt,wmfRGB* f
|
|
|
409786 |
|
|
|
409786 |
fg_pixel = gdImageColorResolve (gd->image,fg->r,fg->g,fg->b);
|
|
|
409786 |
|
|
|
409786 |
- if (bitmap->pixel_mode == ft_pixel_mode_mono)
|
|
|
409786 |
- { color = fg_pixel;
|
|
|
409786 |
- }
|
|
|
409786 |
-
|
|
|
409786 |
for (row = 0; row < rows; row++)
|
|
|
409786 |
{ buffer = bitmap->buffer + row * bitmap->pitch;
|
|
|
409786 |
|
|
|
409786 |
diff --git a/src/meta.c b/src/meta.c
|
|
|
409786 |
index cf046a5..ea14f94 100644
|
|
|
409786 |
--- a/src/meta.c
|
|
|
409786 |
+++ b/src/meta.c
|
|
|
409786 |
@@ -441,7 +441,6 @@ static void write_b64 (wmfAPI * API, const unsigned char * buffer, unsigned long
|
|
|
409786 |
b32 = (b32 << 16);
|
|
|
409786 |
*ptr++ = B64[(b32 >> 18) ];
|
|
|
409786 |
*ptr++ = B64[(b32 >> 12) & 0x3f];
|
|
|
409786 |
- remaining -= 1;
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
*ptr++ = '\n';
|
|
|
409786 |
diff --git a/src/player.c b/src/player.c
|
|
|
409786 |
index 9b66eb7..813a2bf 100644
|
|
|
409786 |
--- a/src/player.c
|
|
|
409786 |
+++ b/src/player.c
|
|
|
409786 |
@@ -554,7 +554,7 @@ static wmf_error_t WmfPlayMetaFile (wmfAPI* API)
|
|
|
409786 |
|
|
|
409786 |
case META_SETVIEWPORTORG:
|
|
|
409786 |
SCAN_DIAGNOSTIC (API,"New Record: SETVIEWPORTORG");
|
|
|
409786 |
- changed = meta_orgext (API,&Record);
|
|
|
409786 |
+ meta_orgext (API,&Record);
|
|
|
409786 |
if (SCAN (API)) wmf_write (API, Size, Function, "setviewportorg",
|
|
|
409786 |
atts->atts, Record.parameter, Record.size * 2);
|
|
|
409786 |
SCAN_DIAGNOSTIC (API,"\n");
|
|
|
409786 |
diff --git a/src/player/meta.h b/src/player/meta.h
|
|
|
409786 |
index d656dfd..b9762e3 100644
|
|
|
409786 |
--- a/src/player/meta.h
|
|
|
409786 |
+++ b/src/player/meta.h
|
|
|
409786 |
@@ -412,16 +412,12 @@ static int meta_arc (wmfAPI* API,wmfRecord* Record)
|
|
|
409786 |
d_pt.y = c_pt.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
/* fallthrough */
|
|
|
409786 |
+ default:
|
|
|
409786 |
case '2':
|
|
|
409786 |
d_pt.x = c_pt.x;
|
|
|
409786 |
d_pt.y = drawarc.TL.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
break;
|
|
|
409786 |
-
|
|
|
409786 |
- default:
|
|
|
409786 |
- WMF_ERROR (API,"Glitch!");
|
|
|
409786 |
- API->err = wmf_E_Glitch;
|
|
|
409786 |
- break;
|
|
|
409786 |
}
|
|
|
409786 |
break;
|
|
|
409786 |
|
|
|
409786 |
@@ -445,16 +441,12 @@ static int meta_arc (wmfAPI* API,wmfRecord* Record)
|
|
|
409786 |
d_pt.y = drawarc.BR.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
/* fallthrough */
|
|
|
409786 |
+ default:
|
|
|
409786 |
case '3':
|
|
|
409786 |
d_pt.x = drawarc.TL.x;
|
|
|
409786 |
d_pt.y = c_pt.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
break;
|
|
|
409786 |
-
|
|
|
409786 |
- default:
|
|
|
409786 |
- WMF_ERROR (API,"Glitch!");
|
|
|
409786 |
- API->err = wmf_E_Glitch;
|
|
|
409786 |
- break;
|
|
|
409786 |
}
|
|
|
409786 |
break;
|
|
|
409786 |
|
|
|
409786 |
@@ -478,20 +470,17 @@ static int meta_arc (wmfAPI* API,wmfRecord* Record)
|
|
|
409786 |
d_pt.y = c_pt.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
/* fallthrough */
|
|
|
409786 |
+ default:
|
|
|
409786 |
case '4':
|
|
|
409786 |
d_pt.x = c_pt.x;
|
|
|
409786 |
d_pt.y = drawarc.BR.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
break;
|
|
|
409786 |
-
|
|
|
409786 |
- default:
|
|
|
409786 |
- WMF_ERROR (API,"Glitch!");
|
|
|
409786 |
- API->err = wmf_E_Glitch;
|
|
|
409786 |
- break;
|
|
|
409786 |
}
|
|
|
409786 |
break;
|
|
|
409786 |
|
|
|
409786 |
case '4':
|
|
|
409786 |
+ default:
|
|
|
409786 |
switch (Qe)
|
|
|
409786 |
{
|
|
|
409786 |
case '4':
|
|
|
409786 |
@@ -511,23 +500,14 @@ static int meta_arc (wmfAPI* API,wmfRecord* Record)
|
|
|
409786 |
d_pt.y = drawarc.TL.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
/* fallthrough */
|
|
|
409786 |
+ default:
|
|
|
409786 |
case '1':
|
|
|
409786 |
d_pt.x = drawarc.BR.x;
|
|
|
409786 |
d_pt.y = c_pt.y;
|
|
|
409786 |
D_Coord_Register (API,d_pt,scope);
|
|
|
409786 |
break;
|
|
|
409786 |
-
|
|
|
409786 |
- default:
|
|
|
409786 |
- WMF_ERROR (API,"Glitch!");
|
|
|
409786 |
- API->err = wmf_E_Glitch;
|
|
|
409786 |
- break;
|
|
|
409786 |
}
|
|
|
409786 |
break;
|
|
|
409786 |
-
|
|
|
409786 |
- default:
|
|
|
409786 |
- WMF_ERROR (API,"Glitch!");
|
|
|
409786 |
- API->err = wmf_E_Glitch;
|
|
|
409786 |
- break;
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
return (changed);
|
|
|
409786 |
@@ -2428,7 +2408,7 @@ static int meta_dc_color (wmfAPI* API,wmfRecord* Record,wmfAttributes* attrlist)
|
|
|
409786 |
hash[5] = hex[(color.b >> 4) & 0x0f];
|
|
|
409786 |
hash[6] = hex[ color.b & 0x0f];
|
|
|
409786 |
hash[7] = 0;
|
|
|
409786 |
- value = wmf_attr_add (API, attrlist, "color", hash);
|
|
|
409786 |
+ wmf_attr_add (API, attrlist, "color", hash);
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
if (SCAN (API)) wmf_ipa_color_add (API,&color;;
|
|
|
409786 |
@@ -3152,7 +3132,7 @@ static int meta_pen_create (wmfAPI* API,wmfRecord* Record,wmfAttributes* attrlis
|
|
|
409786 |
hash[5] = hex[(color.b >> 4) & 0x0f];
|
|
|
409786 |
hash[6] = hex[ color.b & 0x0f];
|
|
|
409786 |
hash[7] = 0;
|
|
|
409786 |
- value = wmf_attr_add (API, attrlist, "color", hash);
|
|
|
409786 |
+ wmf_attr_add (API, attrlist, "color", hash);
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
WMF_PEN_SET_COLOR (pen,&color;;
|
|
|
409786 |
@@ -3254,7 +3234,7 @@ static int meta_brush_create (wmfAPI* API,wmfRecord* Record,wmfAttributes* attrl
|
|
|
409786 |
hash[5] = hex[(color.b >> 4) & 0x0f];
|
|
|
409786 |
hash[6] = hex[ color.b & 0x0f];
|
|
|
409786 |
hash[7] = 0;
|
|
|
409786 |
- value = wmf_attr_add (API, attrlist, "color", hash);
|
|
|
409786 |
+ wmf_attr_add (API, attrlist, "color", hash);
|
|
|
409786 |
}
|
|
|
409786 |
|
|
|
409786 |
WMF_BRUSH_SET_COLOR (brush,&color;;
|
|
|
409786 |
diff --git a/src/recorder.c b/src/recorder.c
|
|
|
409786 |
index b5e6431..ff371a7 100644
|
|
|
409786 |
--- a/src/recorder.c
|
|
|
409786 |
+++ b/src/recorder.c
|
|
|
409786 |
@@ -48,6 +48,9 @@ static void s_rbox_set (wmfAPI * API, wmfConstruct * construct,
|
|
|
409786 |
{
|
|
|
409786 |
WMF_ERROR (API, "Hmm. Record out of range...");
|
|
|
409786 |
API->err = wmf_E_Glitch;
|
|
|
409786 |
+ rbox->start = NULL;
|
|
|
409786 |
+ rbox->end = NULL;
|
|
|
409786 |
+ rbox->ptr = NULL;
|
|
|
409786 |
return;
|
|
|
409786 |
}
|
|
|
409786 |
rbox->start = construct->buffer + construct->rec_offset[n ];
|