mixed 8c tabs+spaces -> spaces

This commit is contained in:
wiredfool 2016-03-16 04:23:45 -07:00
parent ac6e86765d
commit b1a190ad78
6 changed files with 696 additions and 696 deletions

View File

@ -39,14 +39,14 @@ ImagingGetModeDIB(int size_out[2])
mode = "P";
if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)) {
mode = "RGB";
if (GetDeviceCaps(dc, BITSPIXEL) == 1)
mode = "1";
mode = "RGB";
if (GetDeviceCaps(dc, BITSPIXEL) == 1)
mode = "1";
}
if (size_out) {
size_out[0] = GetDeviceCaps(dc, HORZRES);
size_out[1] = GetDeviceCaps(dc, VERTRES);
size_out[0] = GetDeviceCaps(dc, HORZRES);
size_out[1] = GetDeviceCaps(dc, VERTRES);
}
DeleteDC(dc);
@ -66,18 +66,18 @@ ImagingNewDIB(const char *mode, int xsize, int ysize)
/* Check mode */
if (strcmp(mode, "1") != 0 && strcmp(mode, "L") != 0 &&
strcmp(mode, "RGB") != 0)
return (ImagingDIB) ImagingError_ModeError();
strcmp(mode, "RGB") != 0)
return (ImagingDIB) ImagingError_ModeError();
/* Create DIB context and info header */
dib = (ImagingDIB) malloc(sizeof(*dib));
if (!dib)
return (ImagingDIB) ImagingError_MemoryError();
return (ImagingDIB) ImagingError_MemoryError();
dib->info = (BITMAPINFO*) malloc(sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD));
if (!dib->info) {
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
return (ImagingDIB) ImagingError_MemoryError();
}
memset(dib->info, 0, sizeof(BITMAPINFOHEADER));
@ -91,17 +91,17 @@ ImagingNewDIB(const char *mode, int xsize, int ysize)
/* Create DIB */
dib->dc = CreateCompatibleDC(NULL);
if (!dib->dc) {
free(dib->info);
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
free(dib->info);
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
}
dib->bitmap = CreateDIBSection(dib->dc, dib->info, DIB_RGB_COLORS,
&dib->bits, NULL, 0);
if (!dib->bitmap) {
free(dib->info);
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
}
strcpy(dib->mode, mode);
@ -112,10 +112,10 @@ ImagingNewDIB(const char *mode, int xsize, int ysize)
dib->linesize = (xsize * dib->pixelsize + 3) & -4;
if (dib->pixelsize == 1)
dib->pack = dib->unpack = (ImagingShuffler) memcpy;
dib->pack = dib->unpack = (ImagingShuffler) memcpy;
else {
dib->pack = ImagingPackBGR;
dib->unpack = ImagingPackBGR;
dib->pack = ImagingPackBGR;
dib->unpack = ImagingPackBGR;
}
/* Bind the DIB to the device context */
@ -125,88 +125,88 @@ ImagingNewDIB(const char *mode, int xsize, int ysize)
/* Bind a palette to it as well (only required for 8-bit DIBs) */
if (dib->pixelsize == 1) {
for (i = 0; i < 256; i++) {
palette[i].rgbRed =
palette[i].rgbGreen =
palette[i].rgbBlue = i;
palette[i].rgbReserved = 0;
for (i = 0; i < 256; i++) {
palette[i].rgbRed =
palette[i].rgbGreen =
palette[i].rgbBlue = i;
palette[i].rgbReserved = 0;
}
SetDIBColorTable(dib->dc, 0, 256, palette);
SetDIBColorTable(dib->dc, 0, 256, palette);
}
/* Create an associated palette (for 8-bit displays only) */
if (strcmp(ImagingGetModeDIB(NULL), "P") == 0) {
char palbuf[sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY)];
LPLOGPALETTE pal = (LPLOGPALETTE) palbuf;
int i, r, g, b;
char palbuf[sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY)];
LPLOGPALETTE pal = (LPLOGPALETTE) palbuf;
int i, r, g, b;
/* Load system palette */
pal->palVersion = 0x300;
pal->palNumEntries = 256;
GetSystemPaletteEntries(dib->dc, 0, 256, pal->palPalEntry);
/* Load system palette */
pal->palVersion = 0x300;
pal->palNumEntries = 256;
GetSystemPaletteEntries(dib->dc, 0, 256, pal->palPalEntry);
if (strcmp(mode, "L") == 0) {
if (strcmp(mode, "L") == 0) {
/* Greyscale DIB. Fill all 236 slots with a greyscale ramp
* (this is usually overkill on Windows since VGA only offers
* 6 bits greyscale resolution). Ignore the slots already
* allocated by Windows */
/* Greyscale DIB. Fill all 236 slots with a greyscale ramp
* (this is usually overkill on Windows since VGA only offers
* 6 bits greyscale resolution). Ignore the slots already
* allocated by Windows */
i = 10;
for (r = 0; r < 236; r++) {
pal->palPalEntry[i].peRed =
pal->palPalEntry[i].peGreen =
pal->palPalEntry[i].peBlue = i;
i++;
}
i = 10;
for (r = 0; r < 236; r++) {
pal->palPalEntry[i].peRed =
pal->palPalEntry[i].peGreen =
pal->palPalEntry[i].peBlue = i;
i++;
}
dib->palette = CreatePalette(pal);
dib->palette = CreatePalette(pal);
} else if (strcmp(mode, "RGB") == 0) {
} else if (strcmp(mode, "RGB") == 0) {
#ifdef CUBE216
/* Colour DIB. Create a 6x6x6 colour cube (216 entries) and
* add 20 extra greylevels for best result with greyscale
* images. */
/* Colour DIB. Create a 6x6x6 colour cube (216 entries) and
* add 20 extra greylevels for best result with greyscale
* images. */
i = 10;
for (r = 0; r < 256; r += 51)
for (g = 0; g < 256; g += 51)
for (b = 0; b < 256; b += 51) {
pal->palPalEntry[i].peRed = r;
pal->palPalEntry[i].peGreen = g;
pal->palPalEntry[i].peBlue = b;
i++;
}
for (r = 1; r < 22-1; r++) {
/* Black and white are already provided by the cube. */
pal->palPalEntry[i].peRed =
pal->palPalEntry[i].peGreen =
pal->palPalEntry[i].peBlue = r * 255 / (22-1);
i++;
}
i = 10;
for (r = 0; r < 256; r += 51)
for (g = 0; g < 256; g += 51)
for (b = 0; b < 256; b += 51) {
pal->palPalEntry[i].peRed = r;
pal->palPalEntry[i].peGreen = g;
pal->palPalEntry[i].peBlue = b;
i++;
}
for (r = 1; r < 22-1; r++) {
/* Black and white are already provided by the cube. */
pal->palPalEntry[i].peRed =
pal->palPalEntry[i].peGreen =
pal->palPalEntry[i].peBlue = r * 255 / (22-1);
i++;
}
#else
/* Colour DIB. Alternate palette. */
/* Colour DIB. Alternate palette. */
i = 10;
for (r = 0; r < 256; r += 37)
for (g = 0; g < 256; g += 32)
for (b = 0; b < 256; b += 64) {
pal->palPalEntry[i].peRed = r;
pal->palPalEntry[i].peGreen = g;
pal->palPalEntry[i].peBlue = b;
i++;
}
i = 10;
for (r = 0; r < 256; r += 37)
for (g = 0; g < 256; g += 32)
for (b = 0; b < 256; b += 64) {
pal->palPalEntry[i].peRed = r;
pal->palPalEntry[i].peGreen = g;
pal->palPalEntry[i].peBlue = b;
i++;
}
#endif
dib->palette = CreatePalette(pal);
dib->palette = CreatePalette(pal);
}
}
}
@ -222,8 +222,8 @@ ImagingPasteDIB(ImagingDIB dib, Imaging im, int xy[4])
int y;
for (y = 0; y < im->ysize; y++)
dib->pack(dib->bits + dib->linesize*(dib->ysize-(xy[1]+y)-1) +
xy[0]*dib->pixelsize, im->image[y], im->xsize);
dib->pack(dib->bits + dib->linesize*(dib->ysize-(xy[1]+y)-1) +
xy[0]*dib->pixelsize, im->image[y], im->xsize);
}
@ -233,7 +233,7 @@ ImagingExposeDIB(ImagingDIB dib, void *dc)
/* Copy bitmap to display */
if (dib->palette != 0)
SelectPalette((HDC) dc, dib->palette, FALSE);
SelectPalette((HDC) dc, dib->palette, FALSE);
BitBlt((HDC) dc, 0, 0, dib->xsize, dib->ysize, dib->dc, 0, 0, SRCCOPY);
}
@ -266,15 +266,15 @@ ImagingQueryPaletteDIB(ImagingDIB dib, void *dc)
if (dib->palette != 0) {
/* Realize associated palette */
HPALETTE now = SelectPalette((HDC) dc, dib->palette, FALSE);
n = RealizePalette((HDC) dc);
/* Realize associated palette */
HPALETTE now = SelectPalette((HDC) dc, dib->palette, FALSE);
n = RealizePalette((HDC) dc);
/* Restore palette */
SelectPalette((HDC) dc, now, FALSE);
/* Restore palette */
SelectPalette((HDC) dc, now, FALSE);
} else
n = 0;
n = 0;
return n; /* number of colours that was changed */
}
@ -285,13 +285,13 @@ ImagingDeleteDIB(ImagingDIB dib)
/* Clean up */
if (dib->palette)
DeleteObject(dib->palette);
DeleteObject(dib->palette);
if (dib->bitmap) {
SelectObject(dib->dc, dib->old_bitmap);
DeleteObject(dib->bitmap);
DeleteObject(dib->bitmap);
}
if (dib->dc)
DeleteDC(dib->dc);
DeleteDC(dib->dc);
free(dib->info);
}

View File

@ -31,20 +31,20 @@ ImagingPaletteNew(const char* mode)
ImagingPalette palette;
if (strcmp(mode, "RGB") && strcmp(mode, "RGBA"))
return (ImagingPalette) ImagingError_ModeError();
return (ImagingPalette) ImagingError_ModeError();
palette = calloc(1, sizeof(struct ImagingPaletteInstance));
if (!palette)
return (ImagingPalette) ImagingError_MemoryError();
return (ImagingPalette) ImagingError_MemoryError();
strncpy(palette->mode, mode, IMAGING_MODE_LENGTH);
/* Initialize to ramp */
for (i = 0; i < 256; i++) {
palette->palette[i*4+0] =
palette->palette[i*4+1] =
palette->palette[i*4+2] = (UINT8) i;
palette->palette[i*4+3] = 255; /* opaque */
palette->palette[i*4+0] =
palette->palette[i*4+1] =
palette->palette[i*4+2] = (UINT8) i;
palette->palette[i*4+3] = 255; /* opaque */
}
return palette;
@ -60,35 +60,35 @@ ImagingPaletteNewBrowser(void)
palette = ImagingPaletteNew("RGB");
if (!palette)
return NULL;
return NULL;
/* Blank out unused entries */
/* FIXME: Add 10-level windows palette here? */
for (i = 0; i < 10; i++) {
palette->palette[i*4+0] =
palette->palette[i*4+1] =
palette->palette[i*4+2] = 0;
palette->palette[i*4+0] =
palette->palette[i*4+1] =
palette->palette[i*4+2] = 0;
}
/* Simple 6x6x6 colour cube */
for (b = 0; b < 256; b += 51)
for (g = 0; g < 256; g += 51)
for (r = 0; r < 256; r += 51) {
palette->palette[i*4+0] = r;
palette->palette[i*4+1] = g;
palette->palette[i*4+2] = b;
i++;
}
for (g = 0; g < 256; g += 51)
for (r = 0; r < 256; r += 51) {
palette->palette[i*4+0] = r;
palette->palette[i*4+1] = g;
palette->palette[i*4+2] = b;
i++;
}
/* Blank out unused entries */
/* FIXME: add 30-level greyscale wedge here? */
for (; i < 256; i++) {
palette->palette[i*4+0] =
palette->palette[i*4+1] =
palette->palette[i*4+2] = 0;
palette->palette[i*4+0] =
palette->palette[i*4+1] =
palette->palette[i*4+2] = 0;
}
return palette;
@ -102,11 +102,11 @@ ImagingPaletteDuplicate(ImagingPalette palette)
ImagingPalette new_palette;
if (!palette)
return NULL;
return NULL;
new_palette = malloc(sizeof(struct ImagingPaletteInstance));
if (!new_palette)
return (ImagingPalette) ImagingError_MemoryError();
return (ImagingPalette) ImagingError_MemoryError();
memcpy(new_palette, palette, sizeof(struct ImagingPaletteInstance));
@ -122,15 +122,15 @@ ImagingPaletteDelete(ImagingPalette palette)
/* Destroy palette object */
if (palette) {
if (palette->cache)
free(palette->cache);
free(palette);
if (palette->cache)
free(palette->cache);
free(palette);
}
}
/* -------------------------------------------------------------------- */
/* Colour mapping */
/* Colour mapping */
/* -------------------------------------------------------------------- */
/* This code is used to map RGB triplets to palette indices, using
@ -143,26 +143,26 @@ ImagingPaletteDelete(ImagingPalette palette)
*
* The IJG JPEG library is copyright (C) 1991-1995, Thomas G. Lane. */
#define DIST(a, b, s) (a - b) * (a - b) * s
#define DIST(a, b, s) (a - b) * (a - b) * s
/* Colour weights (no scaling, for now) */
#define RSCALE 1
#define GSCALE 1
#define BSCALE 1
#define RSCALE 1
#define GSCALE 1
#define BSCALE 1
/* Calculated scaled distances */
#define RDIST(a, b) DIST(a, b, RSCALE*RSCALE)
#define GDIST(a, b) DIST(a, b, GSCALE*GSCALE)
#define BDIST(a, b) DIST(a, b, BSCALE*BSCALE)
#define RDIST(a, b) DIST(a, b, RSCALE*RSCALE)
#define GDIST(a, b) DIST(a, b, GSCALE*GSCALE)
#define BDIST(a, b) DIST(a, b, BSCALE*BSCALE)
/* Incremental steps */
#define RSTEP (4 * RSCALE)
#define GSTEP (4 * GSCALE)
#define BSTEP (4 * BSCALE)
#define RSTEP (4 * RSCALE)
#define GSTEP (4 * GSCALE)
#define BSTEP (4 * BSCALE)
#define BOX 8
#define BOX 8
#define BOXVOLUME BOX*BOX*BOX
#define BOXVOLUME BOX*BOX*BOX
void
ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b)
@ -191,25 +191,25 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b)
for (i = 0; i < 256; i++) {
int r, g, b;
unsigned int tmin, tmax;
int r, g, b;
unsigned int tmin, tmax;
/* Find min and max distances to any point in the box */
r = palette->palette[i*4+0];
tmin = (r < r0) ? RDIST(r, r1) : (r > r1) ? RDIST(r, r0) : 0;
tmax = (r <= rc) ? RDIST(r, r1) : RDIST(r, r0);
/* Find min and max distances to any point in the box */
r = palette->palette[i*4+0];
tmin = (r < r0) ? RDIST(r, r1) : (r > r1) ? RDIST(r, r0) : 0;
tmax = (r <= rc) ? RDIST(r, r1) : RDIST(r, r0);
g = palette->palette[i*4+1];
tmin += (g < g0) ? GDIST(g, g1) : (g > g1) ? GDIST(g, g0) : 0;
tmax += (g <= gc) ? GDIST(g, g1) : GDIST(g, g0);
g = palette->palette[i*4+1];
tmin += (g < g0) ? GDIST(g, g1) : (g > g1) ? GDIST(g, g0) : 0;
tmax += (g <= gc) ? GDIST(g, g1) : GDIST(g, g0);
b = palette->palette[i*4+2];
tmin += (b < b0) ? BDIST(b, b1) : (b > b1) ? BDIST(b, b0) : 0;
tmax += (b <= bc) ? BDIST(b, b1) : BDIST(b, b0);
b = palette->palette[i*4+2];
tmin += (b < b0) ? BDIST(b, b1) : (b > b1) ? BDIST(b, b0) : 0;
tmax += (b <= bc) ? BDIST(b, b1) : BDIST(b, b0);
dmin[i] = tmin;
if (tmax < dmax)
dmax = tmax; /* keep the smallest max distance only */
dmin[i] = tmin;
if (tmax < dmax)
dmax = tmax; /* keep the smallest max distance only */
}
@ -220,47 +220,47 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b)
* distance is less than or equal the smallest max distance */
for (i = 0; i < BOXVOLUME; i++)
d[i] = (unsigned int) ~0;
d[i] = (unsigned int) ~0;
for (i = 0; i < 256; i++)
if (dmin[i] <= dmax) {
if (dmin[i] <= dmax) {
int rd, gd, bd;
int ri, gi, bi;
int rx, gx, bx;
int rd, gd, bd;
int ri, gi, bi;
int rx, gx, bx;
ri = (r0 - palette->palette[i*4+0]) * RSCALE;
gi = (g0 - palette->palette[i*4+1]) * GSCALE;
bi = (b0 - palette->palette[i*4+2]) * BSCALE;
ri = (r0 - palette->palette[i*4+0]) * RSCALE;
gi = (g0 - palette->palette[i*4+1]) * GSCALE;
bi = (b0 - palette->palette[i*4+2]) * BSCALE;
rd = ri*ri + gi*gi + bi*bi;
rd = ri*ri + gi*gi + bi*bi;
ri = ri * (2 * RSTEP) + RSTEP * RSTEP;
gi = gi * (2 * GSTEP) + GSTEP * GSTEP;
bi = bi * (2 * BSTEP) + BSTEP * BSTEP;
ri = ri * (2 * RSTEP) + RSTEP * RSTEP;
gi = gi * (2 * GSTEP) + GSTEP * GSTEP;
bi = bi * (2 * BSTEP) + BSTEP * BSTEP;
rx = ri;
for (r = j = 0; r < BOX; r++) {
gd = rd; gx = gi;
for (g = 0; g < BOX; g++) {
bd = gd; bx = bi;
for (b = 0; b < BOX; b++) {
if ((unsigned int) bd < d[j]) {
d[j] = bd;
c[j] = (UINT8) i;
}
bd += bx;
bx += 2 * BSTEP * BSTEP;
j++;
}
gd += gx;
gx += 2 * GSTEP * GSTEP;
}
rd += rx;
rx += 2 * RSTEP * RSTEP;
}
}
rx = ri;
for (r = j = 0; r < BOX; r++) {
gd = rd; gx = gi;
for (g = 0; g < BOX; g++) {
bd = gd; bx = bi;
for (b = 0; b < BOX; b++) {
if ((unsigned int) bd < d[j]) {
d[j] = bd;
c[j] = (UINT8) i;
}
bd += bx;
bx += 2 * BSTEP * BSTEP;
j++;
}
gd += gx;
gx += 2 * GSTEP * GSTEP;
}
rd += rx;
rx += 2 * RSTEP * RSTEP;
}
}
/* Step 3 -- Update cache */
@ -269,9 +269,9 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b)
j = 0;
for (r = r0; r < r1; r+=4)
for (g = g0; g < g1; g+=4)
for (b = b0; b < b1; b+=4)
ImagingPaletteCache(palette, r, g, b) = c[j++];
for (g = g0; g < g1; g+=4)
for (b = b0; b < b1; b+=4)
ImagingPaletteCache(palette, r, g, b) = c[j++];
}
@ -285,18 +285,18 @@ ImagingPaletteCachePrepare(ImagingPalette palette)
if (palette->cache == NULL) {
/* The cache is 512k. It might be a good idea to break it
up into a pointer array (e.g. an 8-bit image?) */
/* The cache is 512k. It might be a good idea to break it
up into a pointer array (e.g. an 8-bit image?) */
palette->cache = (INT16*) malloc(entries * sizeof(INT16));
if (!palette->cache) {
(void) ImagingError_MemoryError();
return -1;
}
palette->cache = (INT16*) malloc(entries * sizeof(INT16));
if (!palette->cache) {
(void) ImagingError_MemoryError();
return -1;
}
/* Mark all entries as empty */
for (i = 0; i < entries; i++)
palette->cache[i] = 0x100;
/* Mark all entries as empty */
for (i = 0; i < entries; i++)
palette->cache[i] = 0x100;
}
@ -310,7 +310,7 @@ ImagingPaletteCacheDelete(ImagingPalette palette)
/* Release the colour cache, if any */
if (palette && palette->cache) {
free(palette->cache);
palette->cache = NULL;
free(palette->cache);
palette->cache = NULL;
}
}

View File

@ -55,20 +55,20 @@ ImagingRankFilter(Imaging im, int size, int rank)
int i, margin, size2;
if (!im || im->bands != 1 || im->type == IMAGING_TYPE_SPECIAL)
return (Imaging) ImagingError_ModeError();
return (Imaging) ImagingError_ModeError();
if (!(size & 1))
return (Imaging) ImagingError_ValueError("bad filter size");
return (Imaging) ImagingError_ValueError("bad filter size");
size2 = size * size;
margin = (size-1) / 2;
if (rank < 0 || rank >= size2)
return (Imaging) ImagingError_ValueError("bad rank value");
return (Imaging) ImagingError_ValueError("bad rank value");
imOut = ImagingNew(im->mode, im->xsize - 2*margin, im->ysize - 2*margin);
if (!imOut)
return NULL;
return NULL;
#define RANK_BODY(type) do {\
type* buf = malloc(size2 * sizeof(type));\

View File

@ -18,7 +18,7 @@
#include "Imaging.h"
#ifdef HAVE_LIBZ
#ifdef HAVE_LIBZ
#include "Zip.h"
@ -37,7 +37,7 @@ static int get_row_len(ImagingCodecState state, int pass)
}
/* -------------------------------------------------------------------- */
/* Decoder */
/* Decoder */
/* -------------------------------------------------------------------- */
int
@ -52,50 +52,50 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
if (!state->state) {
/* Initialization */
if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE)
context->prefix = 1; /* PNG */
/* Initialization */
if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE)
context->prefix = 1; /* PNG */
/* Expand standard buffer to make room for the (optional) filter
prefix, and allocate a buffer to hold the previous line */
free(state->buffer);
state->buffer = (UINT8*) malloc(state->bytes+1);
context->previous = (UINT8*) malloc(state->bytes+1);
if (!state->buffer || !context->previous) {
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
/* Expand standard buffer to make room for the (optional) filter
prefix, and allocate a buffer to hold the previous line */
free(state->buffer);
state->buffer = (UINT8*) malloc(state->bytes+1);
context->previous = (UINT8*) malloc(state->bytes+1);
if (!state->buffer || !context->previous) {
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
context->last_output = 0;
/* Initialize to black */
memset(context->previous, 0, state->bytes+1);
/* Initialize to black */
memset(context->previous, 0, state->bytes+1);
/* Setup decompression context */
context->z_stream.zalloc = (alloc_func) NULL;
context->z_stream.zfree = (free_func) NULL;
context->z_stream.opaque = (voidpf) NULL;
/* Setup decompression context */
context->z_stream.zalloc = (alloc_func) NULL;
context->z_stream.zfree = (free_func) NULL;
context->z_stream.opaque = (voidpf) NULL;
err = inflateInit(&context->z_stream);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
err = inflateInit(&context->z_stream);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
if (context->interlaced) {
context->pass = 0;
state->y = STARTING_ROW[context->pass];
}
if (context->interlaced) {
context->pass = 0;
state->y = STARTING_ROW[context->pass];
}
/* Ready to decode */
state->state = 1;
/* Ready to decode */
state->state = 1;
}
if (context->interlaced) {
row_len = get_row_len(state, context->pass);
row_len = get_row_len(state, context->pass);
} else {
row_len = state->bytes;
row_len = state->bytes;
}
/* Setup the source buffer */
@ -105,162 +105,162 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
/* Decompress what we've got this far */
while (context->z_stream.avail_in > 0) {
context->z_stream.next_out = state->buffer + context->last_output;
context->z_stream.avail_out =
row_len + context->prefix - context->last_output;
context->z_stream.next_out = state->buffer + context->last_output;
context->z_stream.avail_out =
row_len + context->prefix - context->last_output;
err = inflate(&context->z_stream, Z_NO_FLUSH);
err = inflate(&context->z_stream, Z_NO_FLUSH);
if (err < 0) {
/* Something went wrong inside the compression library */
if (err == Z_DATA_ERROR)
state->errcode = IMAGING_CODEC_BROKEN;
else if (err == Z_MEM_ERROR)
state->errcode = IMAGING_CODEC_MEMORY;
else
state->errcode = IMAGING_CODEC_CONFIG;
free(context->previous);
inflateEnd(&context->z_stream);
return -1;
}
if (err < 0) {
/* Something went wrong inside the compression library */
if (err == Z_DATA_ERROR)
state->errcode = IMAGING_CODEC_BROKEN;
else if (err == Z_MEM_ERROR)
state->errcode = IMAGING_CODEC_MEMORY;
else
state->errcode = IMAGING_CODEC_CONFIG;
free(context->previous);
inflateEnd(&context->z_stream);
return -1;
}
n = row_len + context->prefix - context->z_stream.avail_out;
n = row_len + context->prefix - context->z_stream.avail_out;
if (n < row_len + context->prefix) {
context->last_output = n;
break; /* need more input data */
}
if (n < row_len + context->prefix) {
context->last_output = n;
break; /* need more input data */
}
/* Apply predictor */
switch (context->mode) {
case ZIP_PNG:
switch (state->buffer[0]) {
case 0:
break;
case 1:
/* prior */
bpp = (state->bits + 7) / 8;
for (i = bpp+1; i <= row_len; i++)
state->buffer[i] += state->buffer[i-bpp];
break;
case 2:
/* up */
for (i = 1; i <= row_len; i++)
state->buffer[i] += context->previous[i];
break;
case 3:
/* average */
bpp = (state->bits + 7) / 8;
for (i = 1; i <= bpp; i++)
state->buffer[i] += context->previous[i]/2;
for (; i <= row_len; i++)
state->buffer[i] +=
(state->buffer[i-bpp] + context->previous[i])/2;
break;
case 4:
/* paeth filtering */
bpp = (state->bits + 7) / 8;
for (i = 1; i <= bpp; i++)
state->buffer[i] += context->previous[i];
for (; i <= row_len; i++) {
int a, b, c;
int pa, pb, pc;
/* Apply predictor */
switch (context->mode) {
case ZIP_PNG:
switch (state->buffer[0]) {
case 0:
break;
case 1:
/* prior */
bpp = (state->bits + 7) / 8;
for (i = bpp+1; i <= row_len; i++)
state->buffer[i] += state->buffer[i-bpp];
break;
case 2:
/* up */
for (i = 1; i <= row_len; i++)
state->buffer[i] += context->previous[i];
break;
case 3:
/* average */
bpp = (state->bits + 7) / 8;
for (i = 1; i <= bpp; i++)
state->buffer[i] += context->previous[i]/2;
for (; i <= row_len; i++)
state->buffer[i] +=
(state->buffer[i-bpp] + context->previous[i])/2;
break;
case 4:
/* paeth filtering */
bpp = (state->bits + 7) / 8;
for (i = 1; i <= bpp; i++)
state->buffer[i] += context->previous[i];
for (; i <= row_len; i++) {
int a, b, c;
int pa, pb, pc;
/* fetch pixels */
a = state->buffer[i-bpp];
b = context->previous[i];
c = context->previous[i-bpp];
/* fetch pixels */
a = state->buffer[i-bpp];
b = context->previous[i];
c = context->previous[i-bpp];
/* distances to surrounding pixels */
pa = abs(b - c);
pb = abs(a - c);
pc = abs(a + b - 2*c);
/* distances to surrounding pixels */
pa = abs(b - c);
pb = abs(a - c);
pc = abs(a + b - 2*c);
/* pick predictor with the shortest distance */
state->buffer[i] +=
(pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
/* pick predictor with the shortest distance */
state->buffer[i] +=
(pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
}
break;
default:
state->errcode = IMAGING_CODEC_UNKNOWN;
free(context->previous);
inflateEnd(&context->z_stream);
return -1;
}
break;
case ZIP_TIFF_PREDICTOR:
bpp = (state->bits + 7) / 8;
for (i = bpp+1; i <= row_len; i++)
state->buffer[i] += state->buffer[i-bpp];
break;
}
}
break;
default:
state->errcode = IMAGING_CODEC_UNKNOWN;
free(context->previous);
inflateEnd(&context->z_stream);
return -1;
}
break;
case ZIP_TIFF_PREDICTOR:
bpp = (state->bits + 7) / 8;
for (i = bpp+1; i <= row_len; i++)
state->buffer[i] += state->buffer[i-bpp];
break;
}
/* Stuff data into the image */
if (context->interlaced) {
int col = STARTING_COL[context->pass];
if (state->bits >= 8) {
/* Stuff pixels in their correct location, one by one */
for (i = 0; i < row_len; i += ((state->bits + 7) / 8)) {
state->shuffle((UINT8*) im->image[state->y] +
col * im->pixelsize,
state->buffer + context->prefix + i, 1);
col += COL_INCREMENT[context->pass];
}
} else {
/* Handle case with more than a pixel in each byte */
int row_bits = ((state->xsize + OFFSET[context->pass])
/ COL_INCREMENT[context->pass]) * state->bits;
for (i = 0; i < row_bits; i += state->bits) {
UINT8 byte = *(state->buffer + context->prefix + (i / 8));
byte <<= (i % 8);
state->shuffle((UINT8*) im->image[state->y] +
col * im->pixelsize, &byte, 1);
col += COL_INCREMENT[context->pass];
}
}
/* Find next valid scanline */
state->y += ROW_INCREMENT[context->pass];
while (state->y >= state->ysize || row_len <= 0) {
context->pass++;
if (context->pass == 7) {
/* Force exit below */
state->y = state->ysize;
break;
}
state->y = STARTING_ROW[context->pass];
row_len = get_row_len(state, context->pass);
/* Since we're moving to the "first" line, the previous line
* should be black to make filters work corectly */
memset(state->buffer, 0, state->bytes+1);
}
} else {
state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize,
state->buffer + context->prefix,
state->xsize);
state->y++;
}
/* Stuff data into the image */
if (context->interlaced) {
int col = STARTING_COL[context->pass];
if (state->bits >= 8) {
/* Stuff pixels in their correct location, one by one */
for (i = 0; i < row_len; i += ((state->bits + 7) / 8)) {
state->shuffle((UINT8*) im->image[state->y] +
col * im->pixelsize,
state->buffer + context->prefix + i, 1);
col += COL_INCREMENT[context->pass];
}
} else {
/* Handle case with more than a pixel in each byte */
int row_bits = ((state->xsize + OFFSET[context->pass])
/ COL_INCREMENT[context->pass]) * state->bits;
for (i = 0; i < row_bits; i += state->bits) {
UINT8 byte = *(state->buffer + context->prefix + (i / 8));
byte <<= (i % 8);
state->shuffle((UINT8*) im->image[state->y] +
col * im->pixelsize, &byte, 1);
col += COL_INCREMENT[context->pass];
}
}
/* Find next valid scanline */
state->y += ROW_INCREMENT[context->pass];
while (state->y >= state->ysize || row_len <= 0) {
context->pass++;
if (context->pass == 7) {
/* Force exit below */
state->y = state->ysize;
break;
}
state->y = STARTING_ROW[context->pass];
row_len = get_row_len(state, context->pass);
/* Since we're moving to the "first" line, the previous line
* should be black to make filters work corectly */
memset(state->buffer, 0, state->bytes+1);
}
} else {
state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize,
state->buffer + context->prefix,
state->xsize);
state->y++;
}
/* all inflate output has been consumed */
context->last_output = 0;
if (state->y >= state->ysize || err == Z_STREAM_END) {
if (state->y >= state->ysize || err == Z_STREAM_END) {
/* The image and the data should end simultaneously */
/* if (state->y < state->ysize || err != Z_STREAM_END)
state->errcode = IMAGING_CODEC_BROKEN; */
/* The image and the data should end simultaneously */
/* if (state->y < state->ysize || err != Z_STREAM_END)
state->errcode = IMAGING_CODEC_BROKEN; */
free(context->previous);
inflateEnd(&context->z_stream);
return -1; /* end of file (errcode=0) */
free(context->previous);
inflateEnd(&context->z_stream);
return -1; /* end of file (errcode=0) */
}
}
/* Swap buffer pointers */
ptr = state->buffer;
state->buffer = context->previous;
context->previous = ptr;
/* Swap buffer pointers */
ptr = state->buffer;
state->buffer = context->previous;
context->previous = ptr;
}

View File

@ -5,8 +5,8 @@
* coder for ZIP (deflated) image data
*
* History:
* 96-12-29 fl created
* 96-12-30 fl adaptive filter selection, encoder tuning
* 96-12-29 fl created
* 96-12-30 fl adaptive filter selection, encoder tuning
*
* Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997.
@ -17,7 +17,7 @@
#include "Imaging.h"
#ifdef HAVE_LIBZ
#ifdef HAVE_LIBZ
#include "Zip.h"
@ -33,82 +33,82 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
if (!state->state) {
/* Initialization */
/* Initialization */
/* Valid modes are ZIP_PNG, ZIP_PNG_PALETTE, and ZIP_TIFF */
/* Valid modes are ZIP_PNG, ZIP_PNG_PALETTE, and ZIP_TIFF */
/* Expand standard buffer to make room for the filter selector,
and allocate filter buffers */
free(state->buffer);
state->buffer = (UINT8*) malloc(state->bytes+1);
context->previous = (UINT8*) malloc(state->bytes+1);
context->prior = (UINT8*) malloc(state->bytes+1);
context->up = (UINT8*) malloc(state->bytes+1);
context->average = (UINT8*) malloc(state->bytes+1);
context->paeth = (UINT8*) malloc(state->bytes+1);
if (!state->buffer || !context->previous || !context->prior ||
!context->up || !context->average || !context->paeth) {
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
/* Expand standard buffer to make room for the filter selector,
and allocate filter buffers */
free(state->buffer);
state->buffer = (UINT8*) malloc(state->bytes+1);
context->previous = (UINT8*) malloc(state->bytes+1);
context->prior = (UINT8*) malloc(state->bytes+1);
context->up = (UINT8*) malloc(state->bytes+1);
context->average = (UINT8*) malloc(state->bytes+1);
context->paeth = (UINT8*) malloc(state->bytes+1);
if (!state->buffer || !context->previous || !context->prior ||
!context->up || !context->average || !context->paeth) {
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
/* Initalise filter buffers */
state->buffer[0] = 0;
context->prior[0] = 1;
context->up[0] = 2;
context->average[0] = 3;
context->paeth[0] = 4;
/* Initalise filter buffers */
state->buffer[0] = 0;
context->prior[0] = 1;
context->up[0] = 2;
context->average[0] = 3;
context->paeth[0] = 4;
/* Initialise previous buffer to black */
memset(context->previous, 0, state->bytes+1);
/* Initialise previous buffer to black */
memset(context->previous, 0, state->bytes+1);
/* Setup compression context */
context->z_stream.zalloc = (alloc_func)0;
context->z_stream.zfree = (free_func)0;
context->z_stream.opaque = (voidpf)0;
context->z_stream.next_in = 0;
context->z_stream.avail_in = 0;
/* Setup compression context */
context->z_stream.zalloc = (alloc_func)0;
context->z_stream.zfree = (free_func)0;
context->z_stream.opaque = (voidpf)0;
context->z_stream.next_in = 0;
context->z_stream.avail_in = 0;
compress_level = (context->optimize) ? Z_BEST_COMPRESSION
: context->compress_level;
compress_level = (context->optimize) ? Z_BEST_COMPRESSION
: context->compress_level;
if (context->compress_type == -1) {
compress_type = (context->mode == ZIP_PNG) ? Z_FILTERED
: Z_DEFAULT_STRATEGY;
} else {
compress_type = context->compress_type;
}
if (context->compress_type == -1) {
compress_type = (context->mode == ZIP_PNG) ? Z_FILTERED
: Z_DEFAULT_STRATEGY;
} else {
compress_type = context->compress_type;
}
err = deflateInit2(&context->z_stream,
/* compression level */
compress_level,
/* compression method */
Z_DEFLATED,
/* compression memory resources */
15, 9,
/* compression strategy (image data are filtered)*/
compress_type);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
err = deflateInit2(&context->z_stream,
/* compression level */
compress_level,
/* compression method */
Z_DEFLATED,
/* compression memory resources */
15, 9,
/* compression strategy (image data are filtered)*/
compress_type);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
if (context->dictionary && context->dictionary_size > 0) {
err = deflateSetDictionary(&context->z_stream, (unsigned char *)context->dictionary,
context->dictionary_size);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
}
if (context->dictionary && context->dictionary_size > 0) {
err = deflateSetDictionary(&context->z_stream, (unsigned char *)context->dictionary,
context->dictionary_size);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
}
/* Ready to decode */
state->state = 1;
/* Ready to decode */
state->state = 1;
}
@ -116,222 +116,222 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
context->z_stream.next_out = buf;
context->z_stream.avail_out = bytes;
if (context->z_stream.next_in && context->z_stream.avail_in > 0) {
/* We have some data from previous round, deflate it first */
err = deflate(&context->z_stream, Z_NO_FLUSH);
/* We have some data from previous round, deflate it first */
err = deflate(&context->z_stream, Z_NO_FLUSH);
if (err < 0) {
/* Something went wrong inside the compression library */
if (err == Z_DATA_ERROR)
state->errcode = IMAGING_CODEC_BROKEN;
else if (err == Z_MEM_ERROR)
state->errcode = IMAGING_CODEC_MEMORY;
else
state->errcode = IMAGING_CODEC_CONFIG;
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
deflateEnd(&context->z_stream);
return -1;
}
if (err < 0) {
/* Something went wrong inside the compression library */
if (err == Z_DATA_ERROR)
state->errcode = IMAGING_CODEC_BROKEN;
else if (err == Z_MEM_ERROR)
state->errcode = IMAGING_CODEC_MEMORY;
else
state->errcode = IMAGING_CODEC_CONFIG;
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
deflateEnd(&context->z_stream);
return -1;
}
}
ImagingSectionEnter(&cookie);
for (;;) {
switch (state->state) {
switch (state->state) {
case 1:
case 1:
/* Compress image data */
while (context->z_stream.avail_out > 0) {
/* Compress image data */
while (context->z_stream.avail_out > 0) {
if (state->y >= state->ysize) {
/* End of image; now flush compressor buffers */
state->state = 2;
break;
if (state->y >= state->ysize) {
/* End of image; now flush compressor buffers */
state->state = 2;
break;
}
}
/* Stuff image data into the compressor */
state->shuffle(state->buffer+1,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize,
state->xsize);
/* Stuff image data into the compressor */
state->shuffle(state->buffer+1,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize,
state->xsize);
state->y++;
state->y++;
context->output = state->buffer;
context->output = state->buffer;
if (context->mode == ZIP_PNG) {
if (context->mode == ZIP_PNG) {
/* Filter the image data. For each line, select
the filter that gives the least total distance
from zero for the filtered data (taken from
LIBPNG) */
/* Filter the image data. For each line, select
the filter that gives the least total distance
from zero for the filtered data (taken from
LIBPNG) */
bpp = (state->bits + 7) / 8;
bpp = (state->bits + 7) / 8;
/* 0. No filter */
for (i = 1, sum = 0; i <= state->bytes; i++) {
UINT8 v = state->buffer[i];
sum += (v < 128) ? v : 256 - v;
}
/* 0. No filter */
for (i = 1, sum = 0; i <= state->bytes; i++) {
UINT8 v = state->buffer[i];
sum += (v < 128) ? v : 256 - v;
}
/* 2. Up. We'll test this first to save time when
an image line is identical to the one above. */
if (sum > 0) {
for (i = 1, s = 0; i <= state->bytes; i++) {
UINT8 v = state->buffer[i] - context->previous[i];
context->up[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->up;
sum = s; /* 0 if line was duplicated */
}
}
/* 2. Up. We'll test this first to save time when
an image line is identical to the one above. */
if (sum > 0) {
for (i = 1, s = 0; i <= state->bytes; i++) {
UINT8 v = state->buffer[i] - context->previous[i];
context->up[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->up;
sum = s; /* 0 if line was duplicated */
}
}
/* 1. Prior */
if (sum > 0) {
for (i = 1, s = 0; i <= bpp; i++) {
UINT8 v = state->buffer[i];
context->prior[i] = v;
s += (v < 128) ? v : 256 - v;
}
for (; i <= state->bytes; i++) {
UINT8 v = state->buffer[i] - state->buffer[i-bpp];
context->prior[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->prior;
sum = s; /* 0 if line is solid */
}
}
/* 1. Prior */
if (sum > 0) {
for (i = 1, s = 0; i <= bpp; i++) {
UINT8 v = state->buffer[i];
context->prior[i] = v;
s += (v < 128) ? v : 256 - v;
}
for (; i <= state->bytes; i++) {
UINT8 v = state->buffer[i] - state->buffer[i-bpp];
context->prior[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->prior;
sum = s; /* 0 if line is solid */
}
}
/* 3. Average (not very common in real-life images,
so its only used with the optimize option) */
if (context->optimize && sum > 0) {
for (i = 1, s = 0; i <= bpp; i++) {
UINT8 v = state->buffer[i] - context->previous[i]/2;
context->average[i] = v;
s += (v < 128) ? v : 256 - v;
}
for (; i <= state->bytes; i++) {
UINT8 v = state->buffer[i] -
(state->buffer[i-bpp] + context->previous[i])/2;
context->average[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->average;
sum = s;
}
}
/* 3. Average (not very common in real-life images,
so its only used with the optimize option) */
if (context->optimize && sum > 0) {
for (i = 1, s = 0; i <= bpp; i++) {
UINT8 v = state->buffer[i] - context->previous[i]/2;
context->average[i] = v;
s += (v < 128) ? v : 256 - v;
}
for (; i <= state->bytes; i++) {
UINT8 v = state->buffer[i] -
(state->buffer[i-bpp] + context->previous[i])/2;
context->average[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->average;
sum = s;
}
}
/* 4. Paeth */
if (sum > 0) {
for (i = 1, s = 0; i <= bpp; i++) {
UINT8 v = state->buffer[i] - context->previous[i];
context->paeth[i] = v;
s += (v < 128) ? v : 256 - v;
}
for (; i <= state->bytes; i++) {
UINT8 v;
int a, b, c;
int pa, pb, pc;
/* 4. Paeth */
if (sum > 0) {
for (i = 1, s = 0; i <= bpp; i++) {
UINT8 v = state->buffer[i] - context->previous[i];
context->paeth[i] = v;
s += (v < 128) ? v : 256 - v;
}
for (; i <= state->bytes; i++) {
UINT8 v;
int a, b, c;
int pa, pb, pc;
/* fetch pixels */
a = state->buffer[i-bpp];
b = context->previous[i];
c = context->previous[i-bpp];
/* fetch pixels */
a = state->buffer[i-bpp];
b = context->previous[i];
c = context->previous[i-bpp];
/* distances to surrounding pixels */
pa = abs(b - c);
pb = abs(a - c);
pc = abs(a + b - 2*c);
/* distances to surrounding pixels */
pa = abs(b - c);
pb = abs(a - c);
pc = abs(a + b - 2*c);
/* pick predictor with the shortest distance */
v = state->buffer[i] -
((pa <= pb && pa <= pc) ? a :
/* pick predictor with the shortest distance */
v = state->buffer[i] -
((pa <= pb && pa <= pc) ? a :
(pb <= pc) ? b : c);
context->paeth[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->paeth;
sum = s;
}
}
}
context->paeth[i] = v;
s += (v < 128) ? v : 256 - v;
}
if (s < sum) {
context->output = context->paeth;
sum = s;
}
}
}
/* Compress this line */
context->z_stream.next_in = context->output;
context->z_stream.avail_in = state->bytes+1;
/* Compress this line */
context->z_stream.next_in = context->output;
context->z_stream.avail_in = state->bytes+1;
err = deflate(&context->z_stream, Z_NO_FLUSH);
err = deflate(&context->z_stream, Z_NO_FLUSH);
if (err < 0) {
/* Something went wrong inside the compression library */
if (err == Z_DATA_ERROR)
state->errcode = IMAGING_CODEC_BROKEN;
else if (err == Z_MEM_ERROR)
state->errcode = IMAGING_CODEC_MEMORY;
else
state->errcode = IMAGING_CODEC_CONFIG;
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
deflateEnd(&context->z_stream);
ImagingSectionLeave(&cookie);
return -1;
}
if (err < 0) {
/* Something went wrong inside the compression library */
if (err == Z_DATA_ERROR)
state->errcode = IMAGING_CODEC_BROKEN;
else if (err == Z_MEM_ERROR)
state->errcode = IMAGING_CODEC_MEMORY;
else
state->errcode = IMAGING_CODEC_CONFIG;
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
deflateEnd(&context->z_stream);
ImagingSectionLeave(&cookie);
return -1;
}
/* Swap buffer pointers */
ptr = state->buffer;
state->buffer = context->previous;
context->previous = ptr;
/* Swap buffer pointers */
ptr = state->buffer;
state->buffer = context->previous;
context->previous = ptr;
}
}
if (context->z_stream.avail_out == 0)
break; /* Buffer full */
if (context->z_stream.avail_out == 0)
break; /* Buffer full */
case 2:
case 2:
/* End of image data; flush compressor buffers */
/* End of image data; flush compressor buffers */
while (context->z_stream.avail_out > 0) {
while (context->z_stream.avail_out > 0) {
err = deflate(&context->z_stream, Z_FINISH);
err = deflate(&context->z_stream, Z_FINISH);
if (err == Z_STREAM_END) {
if (err == Z_STREAM_END) {
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
free(context->paeth);
free(context->average);
free(context->up);
free(context->prior);
free(context->previous);
deflateEnd(&context->z_stream);
deflateEnd(&context->z_stream);
state->errcode = IMAGING_CODEC_END;
state->errcode = IMAGING_CODEC_END;
break;
}
break;
}
if (context->z_stream.avail_out == 0)
break; /* Buffer full */
if (context->z_stream.avail_out == 0)
break; /* Buffer full */
}
}
}
ImagingSectionLeave(&cookie);
return bytes - context->z_stream.avail_out;
}
ImagingSectionLeave(&cookie);
return bytes - context->z_stream.avail_out;
}

182
path.c
View File

@ -37,7 +37,7 @@ extern int PyImaging_CheckBuffer(PyObject* buffer);
extern int PyImaging_GetBuffer(PyObject* buffer, Py_buffer *view);
/* -------------------------------------------------------------------- */
/* Class */
/* Class */
/* -------------------------------------------------------------------- */
typedef struct {
@ -86,7 +86,7 @@ path_new(Py_ssize_t count, double* xy, int duplicate)
path = PyObject_New(PyPathObject, &PyPathType);
if (path == NULL)
return NULL;
return NULL;
path->count = count;
path->xy = xy;
@ -102,7 +102,7 @@ path_dealloc(PyPathObject* path)
}
/* -------------------------------------------------------------------- */
/* Helpers */
/* Helpers */
/* -------------------------------------------------------------------- */
#define PyPath_Check(op) (Py_TYPE(op) == &PyPathType)
@ -114,14 +114,14 @@ PyPath_Flatten(PyObject* data, double **pxy)
double *xy;
if (PyPath_Check(data)) {
/* This was another path object. */
PyPathObject *path = (PyPathObject*) data;
/* This was another path object. */
PyPathObject *path = (PyPathObject*) data;
xy = alloc_array(path->count);
if (!xy)
return -1;
memcpy(xy, path->xy, 2 * path->count * sizeof(double));
*pxy = xy;
return path->count;
if (!xy)
return -1;
memcpy(xy, path->xy, 2 * path->count * sizeof(double));
*pxy = xy;
return path->count;
}
if (PyImaging_CheckBuffer(data)) {
@ -143,8 +143,8 @@ PyPath_Flatten(PyObject* data, double **pxy)
}
if (!PySequence_Check(data)) {
PyErr_SetString(PyExc_TypeError, "argument must be sequence");
return -1;
PyErr_SetString(PyExc_TypeError, "argument must be sequence");
return -1;
}
j = 0;
@ -156,7 +156,7 @@ PyPath_Flatten(PyObject* data, double **pxy)
/* Allocate for worst case */
xy = alloc_array(n);
if (!xy)
return -1;
return -1;
/* Copy table to path array */
if (PyList_Check(data)) {
@ -164,9 +164,9 @@ PyPath_Flatten(PyObject* data, double **pxy)
double x, y;
PyObject *op = PyList_GET_ITEM(data, i);
if (PyFloat_Check(op))
xy[j++] = PyFloat_AS_DOUBLE(op);
xy[j++] = PyFloat_AS_DOUBLE(op);
else if (PyInt_Check(op))
xy[j++] = (float) PyInt_AS_LONG(op);
xy[j++] = (float) PyInt_AS_LONG(op);
else if (PyNumber_Check(op))
xy[j++] = PyFloat_AsDouble(op);
else if (PyArg_ParseTuple(op, "dd", &x, &y)) {
@ -182,9 +182,9 @@ PyPath_Flatten(PyObject* data, double **pxy)
double x, y;
PyObject *op = PyTuple_GET_ITEM(data, i);
if (PyFloat_Check(op))
xy[j++] = PyFloat_AS_DOUBLE(op);
xy[j++] = PyFloat_AS_DOUBLE(op);
else if (PyInt_Check(op))
xy[j++] = (float) PyInt_AS_LONG(op);
xy[j++] = (float) PyInt_AS_LONG(op);
else if (PyNumber_Check(op))
xy[j++] = PyFloat_AsDouble(op);
else if (PyArg_ParseTuple(op, "dd", &x, &y)) {
@ -211,9 +211,9 @@ PyPath_Flatten(PyObject* data, double **pxy)
}
}
if (PyFloat_Check(op))
xy[j++] = PyFloat_AS_DOUBLE(op);
xy[j++] = PyFloat_AS_DOUBLE(op);
else if (PyInt_Check(op))
xy[j++] = (float) PyInt_AS_LONG(op);
xy[j++] = (float) PyInt_AS_LONG(op);
else if (PyNumber_Check(op))
xy[j++] = PyFloat_AsDouble(op);
else if (PyArg_ParseTuple(op, "dd", &x, &y)) {
@ -229,9 +229,9 @@ PyPath_Flatten(PyObject* data, double **pxy)
}
if (j & 1) {
PyErr_SetString(PyExc_ValueError, "wrong number of coordinates");
free(xy);
return -1;
PyErr_SetString(PyExc_ValueError, "wrong number of coordinates");
free(xy);
return -1;
}
*pxy = xy;
@ -240,7 +240,7 @@ PyPath_Flatten(PyObject* data, double **pxy)
/* -------------------------------------------------------------------- */
/* Factories */
/* Factories */
/* -------------------------------------------------------------------- */
PyObject*
@ -274,7 +274,7 @@ PyPath_Create(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */
/* Methods */
/* Methods */
/* -------------------------------------------------------------------- */
static PyObject*
@ -289,17 +289,17 @@ path_compact(PyPathObject* self, PyObject* args)
double cityblock = 2.0;
if (!PyArg_ParseTuple(args, "|d:compact", &cityblock))
return NULL;
return NULL;
xy = self->xy;
/* remove bogus vertices */
for (i = j = 1; i < self->count; i++) {
if (fabs(xy[j+j-2]-xy[i+i]) + fabs(xy[j+j-1]-xy[i+i+1]) >= cityblock) {
xy[j+j] = xy[i+i];
xy[j+j+1] = xy[i+i+1];
j++;
}
if (fabs(xy[j+j-2]-xy[i+i]) + fabs(xy[j+j-1]-xy[i+i+1]) >= cityblock) {
xy[j+j] = xy[i+i];
xy[j+j+1] = xy[i+i+1];
j++;
}
}
i = self->count - j;
@ -336,7 +336,7 @@ path_getbbox(PyPathObject* self, PyObject* args)
double x0, y0, x1, y1;
if (!PyArg_ParseTuple(args, ":getbbox"))
return NULL;
return NULL;
xy = self->xy;
@ -344,14 +344,14 @@ path_getbbox(PyPathObject* self, PyObject* args)
y0 = y1 = xy[1];
for (i = 1; i < self->count; i++) {
if (xy[i+i] < x0)
x0 = xy[i+i];
if (xy[i+i] > x1)
x1 = xy[i+i];
if (xy[i+i+1] < y0)
y0 = xy[i+i+1];
if (xy[i+i+1] > y1)
y1 = xy[i+i+1];
if (xy[i+i] < x0)
x0 = xy[i+i];
if (xy[i+i] > x1)
x1 = xy[i+i];
if (xy[i+i+1] < y0)
y0 = xy[i+i+1];
if (xy[i+i+1] > y1)
y1 = xy[i+i+1];
}
return Py_BuildValue("dddd", x0, y0, x1, y1);
@ -363,8 +363,8 @@ path_getitem(PyPathObject* self, int i)
if (i < 0)
i = self->count + i;
if (i < 0 || i >= self->count) {
PyErr_SetString(PyExc_IndexError, "path index out of range");
return NULL;
PyErr_SetString(PyExc_IndexError, "path index out of range");
return NULL;
}
return Py_BuildValue("dd", self->xy[i+i], self->xy[i+i+1]);
@ -403,22 +403,22 @@ path_map(PyPathObject* self, PyObject* args)
PyObject* function;
if (!PyArg_ParseTuple(args, "O:map", &function))
return NULL;
return NULL;
xy = self->xy;
/* apply function to coordinate set */
for (i = 0; i < self->count; i++) {
double x = xy[i+i];
double y = xy[i+i+1];
PyObject* item = PyObject_CallFunction(function, "dd", x, y);
if (!item || !PyArg_ParseTuple(item, "dd", &x, &y)) {
Py_XDECREF(item);
return NULL;
}
xy[i+i] = x;
xy[i+i+1] = y;
Py_DECREF(item);
double x = xy[i+i];
double y = xy[i+i+1];
PyObject* item = PyObject_CallFunction(function, "dd", x, y);
if (!item || !PyArg_ParseTuple(item, "dd", &x, &y)) {
Py_XDECREF(item);
return NULL;
}
xy[i+i] = x;
xy[i+i+1] = y;
Py_DECREF(item);
}
Py_INCREF(Py_None);
@ -458,7 +458,7 @@ path_tolist(PyPathObject* self, PyObject* args)
int flat = 0;
if (!PyArg_ParseTuple(args, "|i:tolist", &flat))
return NULL;
return NULL;
if (flat) {
list = PyList_New(self->count*2);
@ -498,32 +498,32 @@ path_transform(PyPathObject* self, PyObject* args)
double wrap = 0.0;
if (!PyArg_ParseTuple(args, "(dddddd)|d:transform",
&a, &b, &c, &d, &e, &f,
&wrap))
return NULL;
&a, &b, &c, &d, &e, &f,
&wrap))
return NULL;
xy = self->xy;
/* transform the coordinate set */
if (b == 0.0 && d == 0.0)
/* scaling */
for (i = 0; i < self->count; i++) {
xy[i+i] = a*xy[i+i]+c;
xy[i+i+1] = e*xy[i+i+1]+f;
}
/* scaling */
for (i = 0; i < self->count; i++) {
xy[i+i] = a*xy[i+i]+c;
xy[i+i+1] = e*xy[i+i+1]+f;
}
else
/* affine transform */
for (i = 0; i < self->count; i++) {
double x = xy[i+i];
double y = xy[i+i+1];
xy[i+i] = a*x+b*y+c;
xy[i+i+1] = d*x+e*y+f;
}
/* affine transform */
for (i = 0; i < self->count; i++) {
double x = xy[i+i];
double y = xy[i+i+1];
xy[i+i] = a*x+b*y+c;
xy[i+i+1] = d*x+e*y+f;
}
/* special treatment of geographical map data */
if (wrap != 0.0)
for (i = 0; i < self->count; i++)
xy[i+i] = fmod(xy[i+i], wrap);
for (i = 0; i < self->count; i++)
xy[i+i] = fmod(xy[i+i], wrap);
Py_INCREF(Py_None);
return Py_None;
@ -543,7 +543,7 @@ static struct PyMethodDef methods[] = {
static PyObject*
path_getattr_id(PyPathObject* self, void* closure)
{
return Py_BuildValue("n", (Py_ssize_t) self->xy);
return Py_BuildValue("n", (Py_ssize_t) self->xy);
}
static struct PyGetSetDef getsetters[] = {
@ -593,13 +593,13 @@ path_subscript(PyPathObject* self, PyObject* item) {
}
static PySequenceMethods path_as_sequence = {
(lenfunc)path_len, /*sq_length*/
(binaryfunc)0, /*sq_concat*/
(ssizeargfunc)0, /*sq_repeat*/
(ssizeargfunc)path_getitem, /*sq_item*/
(ssizessizeargfunc)path_getslice, /*sq_slice*/
(ssizeobjargproc)path_setitem, /*sq_ass_item*/
(ssizessizeobjargproc)0, /*sq_ass_slice*/
(lenfunc)path_len, /*sq_length*/
(binaryfunc)0, /*sq_concat*/
(ssizeargfunc)0, /*sq_repeat*/
(ssizeargfunc)path_getitem, /*sq_item*/
(ssizessizeargfunc)path_getslice, /*sq_slice*/
(ssizeobjargproc)path_setitem, /*sq_ass_item*/
(ssizessizeobjargproc)0, /*sq_ass_slice*/
};
static PyMappingMethods path_as_mapping = {
@ -609,19 +609,19 @@ static PyMappingMethods path_as_mapping = {
};
static PyTypeObject PyPathType = {
PyVarObject_HEAD_INIT(NULL, 0)
"Path", /*tp_name*/
sizeof(PyPathObject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)path_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number */
&path_as_sequence, /*tp_as_sequence */
PyVarObject_HEAD_INIT(NULL, 0)
"Path", /*tp_name*/
sizeof(PyPathObject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)path_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number */
&path_as_sequence, /*tp_as_sequence */
&path_as_mapping, /*tp_as_mapping */
0, /*tp_hash*/
0, /*tp_call*/