Replaced tabs with spaces

This commit is contained in:
Andrew Murray 2020-05-01 22:08:57 +10:00
parent 51f31f3f5b
commit ae43af61da
43 changed files with 2182 additions and 2181 deletions

View File

@ -4,8 +4,8 @@
* tkinter hooks * tkinter hooks
* *
* history: * history:
* 99-07-26 fl created * 99-07-26 fl created
* 99-08-15 fl moved to its own support module * 99-08-15 fl moved to its own support module
* *
* Copyright (c) Secret Labs AB 1999. * Copyright (c) Secret Labs AB 1999.
* *
@ -45,8 +45,8 @@ _tkinit(PyObject* self, PyObject* args)
interp = (Tcl_Interp*)PyLong_AsVoidPtr(arg); interp = (Tcl_Interp*)PyLong_AsVoidPtr(arg);
else { else {
TkappObject* app; TkappObject* app;
/* Do it the hard way. This will break if the TkappObject /* Do it the hard way. This will break if the TkappObject
layout changes */ layout changes */
app = (TkappObject*)PyLong_AsVoidPtr(arg); app = (TkappObject*)PyLong_AsVoidPtr(arg);
interp = app->interp; interp = app->interp;
} }

View File

@ -836,7 +836,7 @@ void addAnimFlagToModule(PyObject* m) {
void addTransparencyFlagToModule(PyObject* m) { void addTransparencyFlagToModule(PyObject* m) {
PyModule_AddObject(m, "HAVE_TRANSPARENCY", PyModule_AddObject(m, "HAVE_TRANSPARENCY",
PyBool_FromLong(!WebPDecoderBuggyAlpha())); PyBool_FromLong(!WebPDecoderBuggyAlpha()));
} }
static int setup_module(PyObject* m) { static int setup_module(PyObject* m) {

View File

@ -28,7 +28,7 @@
#include "Imaging.h" #include "Imaging.h"
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Windows DIB support */ /* Windows DIB support */
#ifdef _WIN32 #ifdef _WIN32
@ -57,12 +57,12 @@ _new(const char* mode, int xsize, int ysize)
display = PyObject_New(ImagingDisplayObject, &ImagingDisplayType); display = PyObject_New(ImagingDisplayObject, &ImagingDisplayType);
if (display == NULL) if (display == NULL)
return NULL; return NULL;
display->dib = ImagingNewDIB(mode, xsize, ysize); display->dib = ImagingNewDIB(mode, xsize, ysize);
if (!display->dib) { if (!display->dib) {
Py_DECREF(display); Py_DECREF(display);
return NULL; return NULL;
} }
return display; return display;
@ -72,7 +72,7 @@ static void
_delete(ImagingDisplayObject* display) _delete(ImagingDisplayObject* display)
{ {
if (display->dib) if (display->dib)
ImagingDeleteDIB(display->dib); ImagingDeleteDIB(display->dib);
PyObject_Del(display); PyObject_Del(display);
} }
@ -81,7 +81,7 @@ _expose(ImagingDisplayObject* display, PyObject* args)
{ {
HDC hdc; HDC hdc;
if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) if (!PyArg_ParseTuple(args, F_HANDLE, &hdc))
return NULL; return NULL;
ImagingExposeDIB(display->dib, hdc); ImagingExposeDIB(display->dib, hdc);
@ -98,7 +98,7 @@ _draw(ImagingDisplayObject* display, PyObject* args)
if (!PyArg_ParseTuple(args, F_HANDLE "(iiii)(iiii)", &hdc, if (!PyArg_ParseTuple(args, F_HANDLE "(iiii)(iiii)", &hdc,
dst+0, dst+1, dst+2, dst+3, dst+0, dst+1, dst+2, dst+3,
src+0, src+1, src+2, src+3)) src+0, src+1, src+2, src+3))
return NULL; return NULL;
ImagingDrawDIB(display->dib, hdc, dst, src); ImagingDrawDIB(display->dib, hdc, dst, src);
@ -117,15 +117,15 @@ _paste(ImagingDisplayObject* display, PyObject* args)
int xy[4]; int xy[4];
xy[0] = xy[1] = xy[2] = xy[3] = 0; xy[0] = xy[1] = xy[2] = xy[3] = 0;
if (!PyArg_ParseTuple(args, "O|(iiii)", &op, xy+0, xy+1, xy+2, xy+3)) if (!PyArg_ParseTuple(args, "O|(iiii)", &op, xy+0, xy+1, xy+2, xy+3))
return NULL; return NULL;
im = PyImaging_AsImaging(op); im = PyImaging_AsImaging(op);
if (!im) if (!im)
return NULL; return NULL;
if (xy[2] <= xy[0]) if (xy[2] <= xy[0])
xy[2] = xy[0] + im->xsize; xy[2] = xy[0] + im->xsize;
if (xy[3] <= xy[1]) if (xy[3] <= xy[1])
xy[3] = xy[1] + im->ysize; xy[3] = xy[1] + im->ysize;
ImagingPasteDIB(display->dib, im, xy); ImagingPasteDIB(display->dib, im, xy);
@ -140,7 +140,7 @@ _query_palette(ImagingDisplayObject* display, PyObject* args)
int status; int status;
if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) if (!PyArg_ParseTuple(args, F_HANDLE, &hdc))
return NULL; return NULL;
status = ImagingQueryPaletteDIB(display->dib, hdc); status = ImagingQueryPaletteDIB(display->dib, hdc);
@ -154,7 +154,7 @@ _getdc(ImagingDisplayObject* display, PyObject* args)
HDC dc; HDC dc;
if (!PyArg_ParseTuple(args, F_HANDLE, &window)) if (!PyArg_ParseTuple(args, F_HANDLE, &window))
return NULL; return NULL;
dc = GetDC(window); dc = GetDC(window);
if (!dc) { if (!dc) {
@ -172,7 +172,7 @@ _releasedc(ImagingDisplayObject* display, PyObject* args)
HDC dc; HDC dc;
if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, &window, &dc)) if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, &window, &dc))
return NULL; return NULL;
ReleaseDC(window, dc); ReleaseDC(window, dc);
@ -228,13 +228,13 @@ static struct PyMethodDef methods[] = {
static PyObject* static PyObject*
_getattr_mode(ImagingDisplayObject* self, void* closure) _getattr_mode(ImagingDisplayObject* self, void* closure)
{ {
return Py_BuildValue("s", self->dib->mode); return Py_BuildValue("s", self->dib->mode);
} }
static PyObject* static PyObject*
_getattr_size(ImagingDisplayObject* self, void* closure) _getattr_size(ImagingDisplayObject* self, void* closure)
{ {
return Py_BuildValue("ii", self->dib->xsize, self->dib->ysize); return Py_BuildValue("ii", self->dib->xsize, self->dib->ysize);
} }
static struct PyGetSetDef getsetters[] = { static struct PyGetSetDef getsetters[] = {
@ -244,13 +244,13 @@ static struct PyGetSetDef getsetters[] = {
}; };
static PyTypeObject ImagingDisplayType = { static PyTypeObject ImagingDisplayType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"ImagingDisplay", /*tp_name*/ "ImagingDisplay", /*tp_name*/
sizeof(ImagingDisplayObject), /*tp_size*/ sizeof(ImagingDisplayObject),/*tp_size*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)_delete, /*tp_dealloc*/ (destructor)_delete, /*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
0, /*tp_getattr*/ 0, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
@ -285,11 +285,11 @@ PyImaging_DisplayWin32(PyObject* self, PyObject* args)
int xsize, ysize; int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize))
return NULL; return NULL;
display = _new(mode, xsize, ysize); display = _new(mode, xsize, ysize);
if (display == NULL) if (display == NULL)
return NULL; return NULL;
return (PyObject*) display; return (PyObject*) display;
} }
@ -647,7 +647,7 @@ PyImaging_CreateWindowWin32(PyObject* self, PyObject* args)
PyObject* callback; PyObject* callback;
int width = 0, height = 0; int width = 0, height = 0;
if (!PyArg_ParseTuple(args, "sO|ii", &title, &callback, &width, &height)) if (!PyArg_ParseTuple(args, "sO|ii", &title, &callback, &width, &height))
return NULL; return NULL;
if (width <= 0) if (width <= 0)
width = CW_USEDEFAULT; width = CW_USEDEFAULT;
@ -817,7 +817,7 @@ error:
#endif /* _WIN32 */ #endif /* _WIN32 */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* X11 support */ /* X11 support */
#ifdef HAVE_XCB #ifdef HAVE_XCB
#include <xcb/xcb.h> #include <xcb/xcb.h>

View File

@ -1095,7 +1095,7 @@ PyImaging_JpegEncoderNew(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* JPEG 2000 */ /* JPEG 2000 */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
#ifdef HAVE_OPENJPEG #ifdef HAVE_OPENJPEG

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@
* decoder for packed bitfields (converts to floating point) * decoder for packed bitfields (converts to floating point)
* *
* history: * history:
* 97-05-31 fl created (much more than originally intended) * 97-05-31 fl created (much more than originally intended)
* *
* Copyright (c) Fredrik Lundh 1997. * Copyright (c) Fredrik Lundh 1997.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -27,18 +27,18 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
if (state->state == 0) { if (state->state == 0) {
/* Initialize context variables */ /* Initialize context variables */
/* this decoder only works for float32 image buffers */ /* this decoder only works for float32 image buffers */
if (im->type != IMAGING_TYPE_FLOAT32) { if (im->type != IMAGING_TYPE_FLOAT32) {
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
} }
/* sanity check */ /* sanity check */
if (bitstate->bits < 1 || bitstate->bits >= 32) { if (bitstate->bits < 1 || bitstate->bits >= 32) {
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
} }
bitstate->mask = (1<<bitstate->bits)-1; bitstate->mask = (1<<bitstate->bits)-1;
@ -46,14 +46,14 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
if (bitstate->sign) if (bitstate->sign)
bitstate->signmask = (1<<(bitstate->bits-1)); bitstate->signmask = (1<<(bitstate->bits-1));
/* check image orientation */ /* check image orientation */
if (state->ystep < 0) { if (state->ystep < 0) {
state->y = state->ysize-1; state->y = state->ysize-1;
state->ystep = -1; state->ystep = -1;
} else } else
state->ystep = 1; state->ystep = 1;
state->state = 1; state->state = 1;
} }
@ -119,7 +119,7 @@ ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
*(FLOAT32*)(&im->image32[state->y][state->x]) = pixel; *(FLOAT32*)(&im->image32[state->y][state->x]) = pixel;
/* step forward */ /* step forward */
if (++state->x >= state->xsize) { if (++state->x >= state->xsize) {
/* new line */ /* new line */
state->y += state->ystep; state->y += state->ystep;
if (state->y < 0 || state->y >= state->ysize) { if (state->y < 0 || state->y >= state->ysize) {

View File

@ -5,9 +5,9 @@
* interpolate between two existing images * interpolate between two existing images
* *
* history: * history:
* 96-03-20 fl Created * 96-03-20 fl Created
* 96-05-18 fl Simplified blend expression * 96-05-18 fl Simplified blend expression
* 96-10-05 fl Fixed expression bug, special case for interpolation * 96-10-05 fl Fixed expression bug, special case for interpolation
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -32,48 +32,48 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha)
return ImagingError_ModeError(); return ImagingError_ModeError();
if (imIn1->type != imIn2->type || if (imIn1->type != imIn2->type ||
imIn1->bands != imIn2->bands || imIn1->bands != imIn2->bands ||
imIn1->xsize != imIn2->xsize || imIn1->xsize != imIn2->xsize ||
imIn1->ysize != imIn2->ysize) imIn1->ysize != imIn2->ysize)
return ImagingError_Mismatch(); return ImagingError_Mismatch();
/* Shortcuts */ /* Shortcuts */
if (alpha == 0.0) if (alpha == 0.0)
return ImagingCopy(imIn1); return ImagingCopy(imIn1);
else if (alpha == 1.0) else if (alpha == 1.0)
return ImagingCopy(imIn2); return ImagingCopy(imIn2);
imOut = ImagingNewDirty(imIn1->mode, imIn1->xsize, imIn1->ysize); imOut = ImagingNewDirty(imIn1->mode, imIn1->xsize, imIn1->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
if (alpha >= 0 && alpha <= 1.0) { if (alpha >= 0 && alpha <= 1.0) {
/* Interpolate between bands */ /* Interpolate between bands */
for (y = 0; y < imIn1->ysize; y++) { for (y = 0; y < imIn1->ysize; y++) {
UINT8* in1 = (UINT8*) imIn1->image[y]; UINT8* in1 = (UINT8*) imIn1->image[y];
UINT8* in2 = (UINT8*) imIn2->image[y]; UINT8* in2 = (UINT8*) imIn2->image[y];
UINT8* out = (UINT8*) imOut->image[y]; UINT8* out = (UINT8*) imOut->image[y];
for (x = 0; x < imIn1->linesize; x++) for (x = 0; x < imIn1->linesize; x++)
out[x] = (UINT8) out[x] = (UINT8)
((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x])); ((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x]));
} }
} else { } else {
/* Extrapolation; must make sure to clip resulting values */ /* Extrapolation; must make sure to clip resulting values */
for (y = 0; y < imIn1->ysize; y++) { for (y = 0; y < imIn1->ysize; y++) {
UINT8* in1 = (UINT8*) imIn1->image[y]; UINT8* in1 = (UINT8*) imIn1->image[y];
UINT8* in2 = (UINT8*) imIn2->image[y]; UINT8* in2 = (UINT8*) imIn2->image[y];
UINT8* out = (UINT8*) imOut->image[y]; UINT8* out = (UINT8*) imOut->image[y];
for (x = 0; x < imIn1->linesize; x++) { for (x = 0; x < imIn1->linesize; x++) {
float temp = (float) float temp = (float)
((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x])); ((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x]));
if (temp <= 0.0) if (temp <= 0.0)
out[x] = 0; out[x] = 0;
else if (temp >= 255.0) else if (temp >= 255.0)
out[x] = 255; out[x] = 255;
else else
out[x] = (UINT8) temp; out[x] = (UINT8) temp;
} }
} }
} }
return imOut; return imOut;

View File

@ -5,7 +5,7 @@
* code to convert YCbCr data * code to convert YCbCr data
* *
* history: * history:
* 98-07-01 hk Created * 98-07-01 hk Created
* *
* Copyright (c) Secret Labs AB 1998 * Copyright (c) Secret Labs AB 1998
* *

View File

@ -26,7 +26,7 @@ _copy(Imaging imOut, Imaging imIn)
int y; int y;
if (!imIn) if (!imIn)
return (Imaging) ImagingError_ValueError(NULL); return (Imaging) ImagingError_ValueError(NULL);
imOut = ImagingNew2Dirty(imIn->mode, imOut, imIn); imOut = ImagingNew2Dirty(imIn->mode, imOut, imIn);
if (!imOut) if (!imOut)
@ -36,7 +36,7 @@ _copy(Imaging imOut, Imaging imIn)
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
if (imIn->block != NULL && imOut->block != NULL) if (imIn->block != NULL && imOut->block != NULL)
memcpy(imOut->block, imIn->block, imIn->ysize * imIn->linesize); memcpy(imOut->block, imIn->block, imIn->ysize * imIn->linesize);
else else
for (y = 0; y < imIn->ysize; y++) for (y = 0; y < imIn->ysize; y++)
memcpy(imOut->image[y], imIn->image[y], imIn->linesize); memcpy(imOut->image[y], imIn->image[y], imIn->linesize);

View File

@ -5,9 +5,9 @@
* cut region from image * cut region from image
* *
* history: * history:
* 95-11-27 fl Created * 95-11-27 fl Created
* 98-07-10 fl Fixed "null result" error * 98-07-10 fl Fixed "null result" error
* 99-02-05 fl Rewritten to use Paste primitive * 99-02-05 fl Rewritten to use Paste primitive
* *
* Copyright (c) Secret Labs AB 1997-99. * Copyright (c) Secret Labs AB 1997-99.
* Copyright (c) Fredrik Lundh 1995. * Copyright (c) Fredrik Lundh 1995.
@ -28,7 +28,7 @@ ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1)
INT32 zero = 0; INT32 zero = 0;
if (!imIn) if (!imIn)
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
xsize = sx1 - sx0; xsize = sx1 - sx0;
if (xsize < 0) if (xsize < 0)
@ -39,12 +39,12 @@ ImagingCrop(Imaging imIn, int sx0, int sy0, int sx1, int sy1)
imOut = ImagingNewDirty(imIn->mode, xsize, ysize); imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
ImagingCopyPalette(imOut, imIn); ImagingCopyPalette(imOut, imIn);
if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize) if (sx0 < 0 || sy0 < 0 || sx1 > imIn->xsize || sy1 > imIn->ysize)
(void) ImagingFill(imOut, &zero); (void) ImagingFill(imOut, &zero);
dx0 = -sx0; dx0 = -sx0;
dy0 = -sy0; dy0 = -sy0;

View File

@ -5,11 +5,11 @@
* encoder for EPS hex data * encoder for EPS hex data
* *
* history: * history:
* 96-04-19 fl created * 96-04-19 fl created
* 96-06-27 fl don't drop last block of encoded data * 96-06-27 fl don't drop last block of encoded data
* *
* notes: * notes:
* FIXME: rename to HexEncode.c ?? * FIXME: rename to HexEncode.c ??
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -31,47 +31,47 @@ ImagingEpsEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
UINT8* in, i; UINT8* in, i;
if (!state->state) { if (!state->state) {
state->state = HEXBYTE; state->state = HEXBYTE;
state->xsize *= im->pixelsize; /* Hack! */ state->xsize *= im->pixelsize; /* Hack! */
} }
in = (UINT8*) im->image[state->y]; in = (UINT8*) im->image[state->y];
for (;;) { for (;;) {
if (state->state == NEWLINE) { if (state->state == NEWLINE) {
if (bytes < 1) if (bytes < 1)
break; break;
*ptr++ = '\n'; *ptr++ = '\n';
bytes--; bytes--;
state->state = HEXBYTE; state->state = HEXBYTE;
} }
if (bytes < 2) if (bytes < 2)
break; break;
i = in[state->x++]; i = in[state->x++];
*ptr++ = hex[(i>>4)&15]; *ptr++ = hex[(i>>4)&15];
*ptr++ = hex[i&15]; *ptr++ = hex[i&15];
bytes -= 2; bytes -= 2;
/* Skip junk bytes */ /* Skip junk bytes */
if (im->bands == 3 && (state->x & 3) == 3) if (im->bands == 3 && (state->x & 3) == 3)
state->x++; state->x++;
if (++state->count >= 79/2) { if (++state->count >= 79/2) {
state->state = NEWLINE; state->state = NEWLINE;
state->count = 0; state->count = 0;
} }
if (state->x >= state->xsize) { if (state->x >= state->xsize) {
state->x = 0; state->x = 0;
if (++state->y >= state->ysize) { if (++state->y >= state->ysize) {
state->errcode = IMAGING_CODEC_END; state->errcode = IMAGING_CODEC_END;
break; break;
} }
in = (UINT8*) im->image[state->y]; in = (UINT8*) im->image[state->y];
} }
} }

View File

@ -57,7 +57,7 @@ void *
ImagingError_ValueError(const char *message) ImagingError_ValueError(const char *message)
{ {
if (!message) if (!message)
message = "exception: bad argument to function"; message = "exception: bad argument to function";
fprintf(stderr, "*** %s\n", message); fprintf(stderr, "*** %s\n", message);
return NULL; return NULL;
} }

View File

@ -5,9 +5,9 @@
* fill image with constant pixel value * fill image with constant pixel value
* *
* history: * history:
* 95-11-26 fl moved from Imaging.c * 95-11-26 fl moved from Imaging.c
* 96-05-17 fl added radial fill, renamed wedge to linear * 96-05-17 fl added radial fill, renamed wedge to linear
* 98-06-23 fl changed ImageFill signature * 98-06-23 fl changed ImageFill signature
* *
* Copyright (c) Secret Labs AB 1997-98. All rights reserved. * Copyright (c) Secret Labs AB 1997-98. All rights reserved.
* Copyright (c) Fredrik Lundh 1995-96. * Copyright (c) Fredrik Lundh 1995-96.

View File

@ -5,8 +5,8 @@
* decoder for Autodesk Animator FLI/FLC animations * decoder for Autodesk Animator FLI/FLC animations
* *
* history: * history:
* 97-01-03 fl Created * 97-01-03 fl Created
* 97-01-17 fl Added SS2 support (FLC) * 97-01-17 fl Added SS2 support (FLC)
* *
* Copyright (c) Fredrik Lundh 1997. * Copyright (c) Fredrik Lundh 1997.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -18,10 +18,10 @@
#include "Imaging.h" #include "Imaging.h"
#define I16(ptr)\ #define I16(ptr)\
((ptr)[0] + ((ptr)[1] << 8)) ((ptr)[0] + ((ptr)[1] << 8))
#define I32(ptr)\ #define I32(ptr)\
((ptr)[0] + ((ptr)[1] << 8) + ((ptr)[2] << 16) + ((ptr)[3] << 24)) ((ptr)[0] + ((ptr)[1] << 8) + ((ptr)[2] << 16) + ((ptr)[3] << 24))
#define ERR_IF_DATA_OOB(offset) \ #define ERR_IF_DATA_OOB(offset) \
@ -29,7 +29,7 @@
state->errcode = IMAGING_CODEC_OVERRUN; \ state->errcode = IMAGING_CODEC_OVERRUN; \
return -1; \ return -1; \
} }
int int
ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes) ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{ {
@ -42,7 +42,7 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
/* If not even the chunk size is present, we'd better leave */ /* If not even the chunk size is present, we'd better leave */
if (bytes < 4) if (bytes < 4)
return 0; return 0;
/* We don't decode anything unless we have a full chunk in the /* We don't decode anything unless we have a full chunk in the
input buffer */ input buffer */
@ -51,7 +51,7 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
framesize = I32(ptr); framesize = I32(ptr);
if (framesize < I32(ptr)) if (framesize < I32(ptr))
return 0; return 0;
/* Make sure this is a frame chunk. The Python driver takes /* Make sure this is a frame chunk. The Python driver takes
case of other chunk types. */ case of other chunk types. */
@ -61,8 +61,8 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
return -1; return -1;
} }
if (I16(ptr+4) != 0xF1FA) { if (I16(ptr+4) != 0xF1FA) {
state->errcode = IMAGING_CODEC_UNKNOWN; state->errcode = IMAGING_CODEC_UNKNOWN;
return -1; return -1;
} }
chunks = I16(ptr+6); chunks = I16(ptr+6);
@ -71,173 +71,173 @@ ImagingFliDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
/* Process subchunks */ /* Process subchunks */
for (c = 0; c < chunks; c++) { for (c = 0; c < chunks; c++) {
UINT8* data; UINT8* data;
if (bytes < 10) { if (bytes < 10) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
data = ptr + 6; data = ptr + 6;
switch (I16(ptr+4)) { switch (I16(ptr+4)) {
case 4: case 11: case 4: case 11:
/* FLI COLOR chunk */ /* FLI COLOR chunk */
break; /* ignored; handled by Python code */ break; /* ignored; handled by Python code */
case 7: case 7:
/* FLI SS2 chunk (word delta) */ /* FLI SS2 chunk (word delta) */
/* OOB ok, we've got 4 bytes min on entry */ /* OOB ok, we've got 4 bytes min on entry */
lines = I16(data); data += 2; lines = I16(data); data += 2;
for (l = y = 0; l < lines && y < state->ysize; l++, y++) { for (l = y = 0; l < lines && y < state->ysize; l++, y++) {
UINT8* local_buf = (UINT8*) im->image[y]; UINT8* local_buf = (UINT8*) im->image[y];
int p, packets; int p, packets;
ERR_IF_DATA_OOB(2) ERR_IF_DATA_OOB(2)
packets = I16(data); data += 2; packets = I16(data); data += 2;
while (packets & 0x8000) { while (packets & 0x8000) {
/* flag word */ /* flag word */
if (packets & 0x4000) { if (packets & 0x4000) {
y += 65536 - packets; /* skip lines */ y += 65536 - packets; /* skip lines */
if (y >= state->ysize) { if (y >= state->ysize) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
local_buf = (UINT8*) im->image[y]; local_buf = (UINT8*) im->image[y];
} else { } else {
/* store last byte (used if line width is odd) */ /* store last byte (used if line width is odd) */
local_buf[state->xsize-1] = (UINT8) packets; local_buf[state->xsize-1] = (UINT8) packets;
} }
ERR_IF_DATA_OOB(2) ERR_IF_DATA_OOB(2)
packets = I16(data); data += 2; packets = I16(data); data += 2;
} }
for (p = x = 0; p < packets; p++) { for (p = x = 0; p < packets; p++) {
ERR_IF_DATA_OOB(2) ERR_IF_DATA_OOB(2)
x += data[0]; /* pixel skip */ x += data[0]; /* pixel skip */
if (data[1] >= 128) { if (data[1] >= 128) {
ERR_IF_DATA_OOB(4) ERR_IF_DATA_OOB(4)
i = 256-data[1]; /* run */ i = 256-data[1]; /* run */
if (x + i + i > state->xsize) if (x + i + i > state->xsize)
break; break;
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
local_buf[x++] = data[2]; local_buf[x++] = data[2];
local_buf[x++] = data[3]; local_buf[x++] = data[3];
} }
data += 2 + 2; data += 2 + 2;
} else { } else {
i = 2 * (int) data[1]; /* chunk */ i = 2 * (int) data[1]; /* chunk */
if (x + i > state->xsize) if (x + i > state->xsize)
break; break;
ERR_IF_DATA_OOB(2+i) ERR_IF_DATA_OOB(2+i)
memcpy(local_buf + x, data + 2, i); memcpy(local_buf + x, data + 2, i);
data += 2 + i; data += 2 + i;
x += i; x += i;
} }
} }
if (p < packets) if (p < packets)
break; /* didn't process all packets */ break; /* didn't process all packets */
} }
if (l < lines) { if (l < lines) {
/* didn't process all lines */ /* didn't process all lines */
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
break; break;
case 12: case 12:
/* FLI LC chunk (byte delta) */ /* FLI LC chunk (byte delta) */
/* OOB Check ok, we have 4 bytes min here */ /* OOB Check ok, we have 4 bytes min here */
y = I16(data); ymax = y + I16(data+2); data += 4; y = I16(data); ymax = y + I16(data+2); data += 4;
for (; y < ymax && y < state->ysize; y++) { for (; y < ymax && y < state->ysize; y++) {
UINT8* out = (UINT8*) im->image[y]; UINT8* out = (UINT8*) im->image[y];
ERR_IF_DATA_OOB(1) ERR_IF_DATA_OOB(1)
int p, packets = *data++; int p, packets = *data++;
for (p = x = 0; p < packets; p++, x += i) { for (p = x = 0; p < packets; p++, x += i) {
ERR_IF_DATA_OOB(2) ERR_IF_DATA_OOB(2)
x += data[0]; /* skip pixels */ x += data[0]; /* skip pixels */
if (data[1] & 0x80) { if (data[1] & 0x80) {
i = 256-data[1]; /* run */ i = 256-data[1]; /* run */
if (x + i > state->xsize) if (x + i > state->xsize)
break; break;
ERR_IF_DATA_OOB(3) ERR_IF_DATA_OOB(3)
memset(out + x, data[2], i); memset(out + x, data[2], i);
data += 3; data += 3;
} else { } else {
i = data[1]; /* chunk */ i = data[1]; /* chunk */
if (x + i > state->xsize) if (x + i > state->xsize)
break; break;
ERR_IF_DATA_OOB(2+i) ERR_IF_DATA_OOB(2+i)
memcpy(out + x, data + 2, i); memcpy(out + x, data + 2, i);
data += i + 2; data += i + 2;
} }
} }
if (p < packets) if (p < packets)
break; /* didn't process all packets */ break; /* didn't process all packets */
} }
if (y < ymax) { if (y < ymax) {
/* didn't process all lines */ /* didn't process all lines */
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
break; break;
case 13: case 13:
/* FLI BLACK chunk */ /* FLI BLACK chunk */
for (y = 0; y < state->ysize; y++) for (y = 0; y < state->ysize; y++)
memset(im->image[y], 0, state->xsize); memset(im->image[y], 0, state->xsize);
break; break;
case 15: case 15:
/* FLI BRUN chunk */ /* FLI BRUN chunk */
/* OOB, ok, we've got 4 bytes min on entry */ /* OOB, ok, we've got 4 bytes min on entry */
for (y = 0; y < state->ysize; y++) { for (y = 0; y < state->ysize; y++) {
UINT8* out = (UINT8*) im->image[y]; UINT8* out = (UINT8*) im->image[y];
data += 1; /* ignore packetcount byte */ data += 1; /* ignore packetcount byte */
for (x = 0; x < state->xsize; x += i) { for (x = 0; x < state->xsize; x += i) {
ERR_IF_DATA_OOB(2) ERR_IF_DATA_OOB(2)
if (data[0] & 0x80) { if (data[0] & 0x80) {
i = 256 - data[0]; i = 256 - data[0];
if (x + i > state->xsize) { if (x + i > state->xsize) {
break; /* safety first */ break; /* safety first */
} }
ERR_IF_DATA_OOB(i+1) ERR_IF_DATA_OOB(i+1)
memcpy(out + x, data + 1, i); memcpy(out + x, data + 1, i);
data += i + 1; data += i + 1;
} else { } else {
i = data[0]; i = data[0];
if (x + i > state->xsize) if (x + i > state->xsize)
break; /* safety first */ break; /* safety first */
memset(out + x, data[1], i); memset(out + x, data[1], i);
data += 2; data += 2;
} }
} }
if (x != state->xsize) { if (x != state->xsize) {
/* didn't unpack whole line */ /* didn't unpack whole line */
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
} }
break; break;
case 16: case 16:
/* COPY chunk */ /* COPY chunk */
if (state->xsize > bytes/state->ysize) { if (state->xsize > bytes/state->ysize) {
/* not enough data for frame */ /* not enough data for frame */
return ptr - buf; /* bytes consumed */ return ptr - buf; /* bytes consumed */
} }
for (y = 0; y < state->ysize; y++) { for (y = 0; y < state->ysize; y++) {
UINT8* local_buf = (UINT8*) im->image[y]; UINT8* local_buf = (UINT8*) im->image[y];
memcpy(local_buf, data, state->xsize); memcpy(local_buf, data, state->xsize);
data += state->xsize; data += state->xsize;
} }
break; break;
case 18: case 18:
/* PSTAMP chunk */ /* PSTAMP chunk */
break; /* ignored */ break; /* ignored */
default: default:
/* unknown chunk */ /* unknown chunk */
/* printf("unknown FLI/FLC chunk: %d\n", I16(ptr+4)); */ /* printf("unknown FLI/FLC chunk: %d\n", I16(ptr+4)); */
state->errcode = IMAGING_CODEC_UNKNOWN; state->errcode = IMAGING_CODEC_UNKNOWN;
return -1; return -1;
} }
advance = I32(ptr); advance = I32(ptr);
if (advance < 0 || advance > bytes) { if (advance < 0 || advance > bytes) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
ptr += advance; ptr += advance;
bytes -= advance; bytes -= advance;
} }
return -1; /* end of frame */ return -1; /* end of frame */

View File

@ -33,47 +33,47 @@ ImagingGetBBox(Imaging im, int bbox[4])
bbox[1] = -1; bbox[1] = -1;
bbox[2] = bbox[3] = 0; bbox[2] = bbox[3] = 0;
#define GETBBOX(image, mask)\ #define GETBBOX(image, mask)\
for (y = 0; y < im->ysize; y++) {\ for (y = 0; y < im->ysize; y++) {\
has_data = 0;\ has_data = 0;\
for (x = 0; x < im->xsize; x++)\ for (x = 0; x < im->xsize; x++)\
if (im->image[y][x] & mask) {\ if (im->image[y][x] & mask) {\
has_data = 1;\ has_data = 1;\
if (x < bbox[0])\ if (x < bbox[0])\
bbox[0] = x;\ bbox[0] = x;\
if (x >= bbox[2])\ if (x >= bbox[2])\
bbox[2] = x+1;\ bbox[2] = x+1;\
}\ }\
if (has_data) {\ if (has_data) {\
if (bbox[1] < 0)\ if (bbox[1] < 0)\
bbox[1] = y;\ bbox[1] = y;\
bbox[3] = y+1;\ bbox[3] = y+1;\
}\ }\
} }
if (im->image8) { if (im->image8) {
GETBBOX(image8, 0xff); GETBBOX(image8, 0xff);
} else { } else {
INT32 mask = 0xffffffff; INT32 mask = 0xffffffff;
if (im->bands == 3) { if (im->bands == 3) {
((UINT8*) &mask)[3] = 0; ((UINT8*) &mask)[3] = 0;
} else if (strcmp(im->mode, "RGBa") == 0 || } else if (strcmp(im->mode, "RGBa") == 0 ||
strcmp(im->mode, "RGBA") == 0 || strcmp(im->mode, "RGBA") == 0 ||
strcmp(im->mode, "La") == 0 || strcmp(im->mode, "La") == 0 ||
strcmp(im->mode, "LA") == 0 || strcmp(im->mode, "LA") == 0 ||
strcmp(im->mode, "PA") == 0) { strcmp(im->mode, "PA") == 0) {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
mask = 0x000000ff; mask = 0x000000ff;
#else #else
mask = 0xff000000; mask = 0xff000000;
#endif #endif
} }
GETBBOX(image32, mask); GETBBOX(image32, mask);
} }
/* Check that we got a box */ /* Check that we got a box */
if (bbox[1] < 0) if (bbox[1] < 0)
return 0; /* no data */ return 0; /* no data */
return 1; /* ok */ return 1; /* ok */
} }
@ -91,25 +91,25 @@ ImagingGetProjection(Imaging im, UINT8* xproj, UINT8* yproj)
memset(xproj, 0, im->xsize); memset(xproj, 0, im->xsize);
memset(yproj, 0, im->ysize); memset(yproj, 0, im->ysize);
#define GETPROJ(image, mask)\ #define GETPROJ(image, mask)\
for (y = 0; y < im->ysize; y++) {\ for (y = 0; y < im->ysize; y++) {\
has_data = 0;\ has_data = 0;\
for (x = 0; x < im->xsize; x++)\ for (x = 0; x < im->xsize; x++)\
if (im->image[y][x] & mask) {\ if (im->image[y][x] & mask) {\
has_data = 1;\ has_data = 1;\
xproj[x] = 1;\ xproj[x] = 1;\
}\ }\
if (has_data)\ if (has_data)\
yproj[y] = 1;\ yproj[y] = 1;\
} }
if (im->image8) { if (im->image8) {
GETPROJ(image8, 0xff); GETPROJ(image8, 0xff);
} else { } else {
INT32 mask = 0xffffffff; INT32 mask = 0xffffffff;
if (im->bands == 3) if (im->bands == 3)
((UINT8*) &mask)[3] = 0; ((UINT8*) &mask)[3] = 0;
GETPROJ(image32, mask); GETPROJ(image32, mask);
} }
return 1; /* ok */ return 1; /* ok */
@ -124,7 +124,7 @@ ImagingGetExtrema(Imaging im, void *extrema)
FLOAT32 fmin, fmax; FLOAT32 fmin, fmax;
if (im->bands != 1) { if (im->bands != 1) {
(void) ImagingError_ModeError(); (void) ImagingError_ModeError();
return -1; /* mismatch */ return -1; /* mismatch */
} }
@ -202,11 +202,11 @@ ImagingGetExtrema(Imaging im, void *extrema)
memcpy(extrema, &v, sizeof(v)); memcpy(extrema, &v, sizeof(v));
v = (UINT16) imax; v = (UINT16) imax;
memcpy(((char*)extrema) + sizeof(v), &v, sizeof(v)); memcpy(((char*)extrema) + sizeof(v), &v, sizeof(v));
break; break;
} }
/* FALL THROUGH */ /* FALL THROUGH */
default: default:
(void) ImagingError_ModeError(); (void) ImagingError_ModeError();
return -1; return -1;
} }
return 1; /* ok */ return 1; /* ok */
@ -265,14 +265,14 @@ getcolors32(Imaging im, int maxcolors, int* size)
/* printf("code_poly=%d\n", code_poly); */ /* printf("code_poly=%d\n", code_poly); */
if (!code_size) if (!code_size)
return ImagingError_MemoryError(); /* just give up */ return ImagingError_MemoryError(); /* just give up */
if (!im->image32) if (!im->image32)
return ImagingError_ModeError(); return ImagingError_ModeError();
table = calloc(code_size + 1, sizeof(ImagingColorItem)); table = calloc(code_size + 1, sizeof(ImagingColorItem));
if (!table) if (!table)
return ImagingError_MemoryError(); return ImagingError_MemoryError();
pixel_mask = 0xffffffff; pixel_mask = 0xffffffff;
if (im->bands == 3) if (im->bands == 3)

View File

@ -10,10 +10,10 @@
/* Max size for a LZW code word. */ /* Max size for a LZW code word. */
#define GIFBITS 12 #define GIFBITS 12
#define GIFTABLE (1<<GIFBITS) #define GIFTABLE (1<<GIFBITS)
#define GIFBUFFER (1<<GIFBITS) #define GIFBUFFER (1<<GIFBITS)
typedef struct { typedef struct {

View File

@ -5,15 +5,15 @@
* a fast, suspendable GIF decoder * a fast, suspendable GIF decoder
* *
* history: * history:
* 95-09-03 fl Created * 95-09-03 fl Created
* 95-09-05 fl Fixed sign problem on 16-bit platforms * 95-09-05 fl Fixed sign problem on 16-bit platforms
* 95-09-13 fl Added some storage shortcuts * 95-09-13 fl Added some storage shortcuts
* 96-03-28 fl Revised API, integrated with PIL * 96-03-28 fl Revised API, integrated with PIL
* 96-12-10 fl Added interlace support * 96-12-10 fl Added interlace support
* 96-12-16 fl Fixed premature termination bug introduced by last fix * 96-12-16 fl Fixed premature termination bug introduced by last fix
* 97-01-05 fl Don't mess up on bogus configuration * 97-01-05 fl Don't mess up on bogus configuration
* 97-01-17 fl Don't mess up on very small, interlaced files * 97-01-17 fl Don't mess up on very small, interlaced files
* 99-02-07 fl Minor speedups * 99-02-07 fl Minor speedups
* *
* Copyright (c) Secret Labs AB 1997-99. * Copyright (c) Secret Labs AB 1997-99.
* Copyright (c) Fredrik Lundh 1995-97. * Copyright (c) Fredrik Lundh 1995-97.
@ -25,7 +25,7 @@
#include "Imaging.h" #include "Imaging.h"
#include <stdio.h> #include <stdio.h>
#include <memory.h> /* memcpy() */ #include <memory.h> /* memcpy() */
#include "Gif.h" #include "Gif.h"
@ -34,23 +34,23 @@
state->x = 0;\ state->x = 0;\
state->y += context->step;\ state->y += context->step;\
while (state->y >= state->ysize)\ while (state->y >= state->ysize)\
switch (context->interlace) {\ switch (context->interlace) {\
case 1:\ case 1:\
context->repeat = state->y = 4;\ context->repeat = state->y = 4;\
context->interlace = 2;\ context->interlace = 2;\
break;\ break;\
case 2:\ case 2:\
context->step = 4;\ context->step = 4;\
context->repeat = state->y = 2;\ context->repeat = state->y = 2;\
context->interlace = 3;\ context->interlace = 3;\
break;\ break;\
case 3:\ case 3:\
context->step = 2;\ context->step = 2;\
context->repeat = state->y = 1;\ context->repeat = state->y = 1;\
context->interlace = 0;\ context->interlace = 0;\
break;\ break;\
default:\ default:\
return -1;\ return -1;\
}\ }\
if (state->y < state->ysize)\ if (state->y < state->ysize)\
out = im->image8[state->y + state->yoff] + state->xoff;\ out = im->image8[state->y + state->yoff] + state->xoff;\
@ -70,227 +70,227 @@ ImagingGifDecode(Imaging im, ImagingCodecState state, UINT8* buffer, Py_ssize_t
if (!state->state) { if (!state->state) {
/* Initialise state */ /* Initialise state */
if (context->bits < 0 || context->bits > 12) { if (context->bits < 0 || context->bits > 12) {
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
} }
/* Clear code */ /* Clear code */
context->clear = 1 << context->bits; context->clear = 1 << context->bits;
/* End code */ /* End code */
context->end = context->clear + 1; context->end = context->clear + 1;
/* Interlace */ /* Interlace */
if (context->interlace) { if (context->interlace) {
context->interlace = 1; context->interlace = 1;
context->step = context->repeat = 8; context->step = context->repeat = 8;
} else } else
context->step = 1; context->step = 1;
state->state = 1; state->state = 1;
} }
out = im->image8[state->y + state->yoff] + state->xoff + state->x; out = im->image8[state->y + state->yoff] + state->xoff + state->x;
for (;;) { for (;;) {
if (state->state == 1) { if (state->state == 1) {
/* First free entry in table */ /* First free entry in table */
context->next = context->clear + 2; context->next = context->clear + 2;
/* Initial code size */ /* Initial code size */
context->codesize = context->bits + 1; context->codesize = context->bits + 1;
context->codemask = (1 << context->codesize) - 1; context->codemask = (1 << context->codesize) - 1;
/* Buffer pointer. We fill the buffer from right, which /* Buffer pointer. We fill the buffer from right, which
allows us to return all of it in one operation. */ allows us to return all of it in one operation. */
context->bufferindex = GIFBUFFER; context->bufferindex = GIFBUFFER;
state->state = 2; state->state = 2;
} }
if (context->bufferindex < GIFBUFFER) { if (context->bufferindex < GIFBUFFER) {
/* Return whole buffer in one chunk */ /* Return whole buffer in one chunk */
i = GIFBUFFER - context->bufferindex; i = GIFBUFFER - context->bufferindex;
p = &context->buffer[context->bufferindex]; p = &context->buffer[context->bufferindex];
context->bufferindex = GIFBUFFER; context->bufferindex = GIFBUFFER;
} else { } else {
/* Get current symbol */ /* Get current symbol */
while (context->bitcount < context->codesize) { while (context->bitcount < context->codesize) {
if (context->blocksize > 0) { if (context->blocksize > 0) {
/* Read next byte */ /* Read next byte */
c = *ptr++; bytes--; c = *ptr++; bytes--;
context->blocksize--; context->blocksize--;
/* New bits are shifted in from from the left. */ /* New bits are shifted in from from the left. */
context->bitbuffer |= (INT32) c << context->bitcount; context->bitbuffer |= (INT32) c << context->bitcount;
context->bitcount += 8; context->bitcount += 8;
} else { } else {
/* New GIF block */ /* New GIF block */
/* We don't start decoding unless we have a full block */ /* We don't start decoding unless we have a full block */
if (bytes < 1) if (bytes < 1)
return ptr - buffer; return ptr - buffer;
c = *ptr; c = *ptr;
if (bytes < c+1) if (bytes < c+1)
return ptr - buffer; return ptr - buffer;
context->blocksize = c; context->blocksize = c;
ptr++; bytes--; ptr++; bytes--;
} }
} }
/* Extract current symbol from bit buffer. */ /* Extract current symbol from bit buffer. */
c = (int) context->bitbuffer & context->codemask; c = (int) context->bitbuffer & context->codemask;
/* Adjust buffer */ /* Adjust buffer */
context->bitbuffer >>= context->codesize; context->bitbuffer >>= context->codesize;
context->bitcount -= context->codesize; context->bitcount -= context->codesize;
/* If c is less than "clear", it's a data byte. Otherwise, /* If c is less than "clear", it's a data byte. Otherwise,
it's either clear/end or a code symbol which should be it's either clear/end or a code symbol which should be
expanded. */ expanded. */
if (c == context->clear) { if (c == context->clear) {
if (state->state != 2) if (state->state != 2)
state->state = 1; state->state = 1;
continue; continue;
} }
if (c == context->end) if (c == context->end)
break; break;
i = 1; i = 1;
p = &context->lastdata; p = &context->lastdata;
if (state->state == 2) { if (state->state == 2) {
/* First valid symbol after clear; use as is */ /* First valid symbol after clear; use as is */
if (c > context->clear) { if (c > context->clear) {
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
context->lastdata = context->lastcode = c; context->lastdata = context->lastcode = c;
state->state = 3; state->state = 3;
} else { } else {
thiscode = c; thiscode = c;
if (c > context->next) { if (c > context->next) {
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
if (c == context->next) { if (c == context->next) {
/* c == next is allowed. not sure why. */ /* c == next is allowed. not sure why. */
if (context->bufferindex <= 0) { if (context->bufferindex <= 0) {
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
context->buffer[--context->bufferindex] = context->buffer[--context->bufferindex] =
context->lastdata; context->lastdata;
c = context->lastcode; c = context->lastcode;
} }
while (c >= context->clear) { while (c >= context->clear) {
/* Copy data string to buffer (beginning from right) */ /* Copy data string to buffer (beginning from right) */
if (context->bufferindex <= 0 || c >= GIFTABLE) { if (context->bufferindex <= 0 || c >= GIFTABLE) {
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
context->buffer[--context->bufferindex] = context->buffer[--context->bufferindex] =
context->data[c]; context->data[c];
c = context->link[c]; c = context->link[c];
} }
context->lastdata = c; context->lastdata = c;
if (context->next < GIFTABLE) { if (context->next < GIFTABLE) {
/* We'll only add this symbol if we have room /* We'll only add this symbol if we have room
for it (take advise, Netscape!) */ for it (take advise, Netscape!) */
context->data[context->next] = c; context->data[context->next] = c;
context->link[context->next] = context->lastcode; context->link[context->next] = context->lastcode;
if (context->next == context->codemask && if (context->next == context->codemask &&
context->codesize < GIFBITS) { context->codesize < GIFBITS) {
/* Expand code size */ /* Expand code size */
context->codesize++; context->codesize++;
context->codemask = (1 << context->codesize) - 1; context->codemask = (1 << context->codesize) - 1;
} }
context->next++; context->next++;
} }
context->lastcode = thiscode; context->lastcode = thiscode;
} }
} }
/* Copy the bytes into the image */ /* Copy the bytes into the image */
if (state->y >= state->ysize) { if (state->y >= state->ysize) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
/* To squeeze some extra pixels out of this loop, we test for /* To squeeze some extra pixels out of this loop, we test for
some common cases and handle them separately. */ some common cases and handle them separately. */
/* FIXME: should we handle the transparency index in here??? */ /* FIXME: should we handle the transparency index in here??? */
if (i == 1) { if (i == 1) {
if (state->x < state->xsize-1) { if (state->x < state->xsize-1) {
/* Single pixel, not at the end of the line. */ /* Single pixel, not at the end of the line. */
*out++ = p[0]; *out++ = p[0];
state->x++; state->x++;
continue; continue;
} }
} else if (state->x + i <= state->xsize) { } else if (state->x + i <= state->xsize) {
/* This string fits into current line. */ /* This string fits into current line. */
memcpy(out, p, i); memcpy(out, p, i);
out += i; out += i;
state->x += i; state->x += i;
if (state->x == state->xsize) { if (state->x == state->xsize) {
NEWLINE(state, context); NEWLINE(state, context);
} }
continue; continue;
} }
/* No shortcut, copy pixel by pixel */ /* No shortcut, copy pixel by pixel */
for (c = 0; c < i; c++) { for (c = 0; c < i; c++) {
*out++ = p[c]; *out++ = p[c];
if (++state->x >= state->xsize) { if (++state->x >= state->xsize) {
NEWLINE(state, context); NEWLINE(state, context);
} }
} }
} }
return ptr - buffer; return ptr - buffer;

View File

@ -5,11 +5,11 @@
* encoder for uncompressed GIF data * encoder for uncompressed GIF data
* *
* history: * history:
* 97-01-05 fl created (writes uncompressed data) * 97-01-05 fl created (writes uncompressed data)
* 97-08-27 fl fixed off-by-one error in buffer size test * 97-08-27 fl fixed off-by-one error in buffer size test
* 98-07-09 fl added interlace write support * 98-07-09 fl added interlace write support
* 99-02-07 fl rewritten, now uses a run-length encoding strategy * 99-02-07 fl rewritten, now uses a run-length encoding strategy
* 99-02-08 fl improved run-length encoding for long runs * 99-02-08 fl improved run-length encoding for long runs
* *
* Copyright (c) Secret Labs AB 1997-99. * Copyright (c) Secret Labs AB 1997-99.
* Copyright (c) Fredrik Lundh 1997. * Copyright (c) Fredrik Lundh 1997.
@ -145,17 +145,17 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
if (!state->state) { if (!state->state) {
/* place a clear code in the output buffer */ /* place a clear code in the output buffer */
context->bitbuffer = CLEAR_CODE; context->bitbuffer = CLEAR_CODE;
context->bitcount = 9; context->bitcount = 9;
state->count = FIRST_CODE; state->count = FIRST_CODE;
if (context->interlace) { if (context->interlace) {
context->interlace = 1; context->interlace = 1;
context->step = 8; context->step = 8;
} else } else
context->step = 1; context->step = 1;
context->last = -1; context->last = -1;
@ -169,152 +169,152 @@ ImagingGifEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
for (;;) for (;;)
switch (state->state) { switch (state->state) {
case INIT: case INIT:
case ENCODE: case ENCODE:
/* identify and store a run of pixels */ /* identify and store a run of pixels */
if (state->x == 0 || state->x >= state->xsize) { if (state->x == 0 || state->x >= state->xsize) {
if (!context->interlace && state->y >= state->ysize) { if (!context->interlace && state->y >= state->ysize) {
state->state = ENCODE_EOF; state->state = ENCODE_EOF;
break;
}
if (context->flush) {
state->state = FLUSH;
break;
}
/* get another line of data */
state->shuffle(
state->buffer,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize
);
state->x = 0;
if (state->state == INIT) {
/* preload the run-length buffer and get going */
context->last = state->buffer[0];
context->count = state->x = 1;
state->state = ENCODE;
}
/* step forward, according to the interlace settings */
state->y += context->step;
while (context->interlace && state->y >= state->ysize)
switch (context->interlace) {
case 1:
state->y = 4;
context->interlace = 2;
break; break;
case 2:
context->step = 4;
state->y = 2;
context->interlace = 3;
break;
case 3:
context->step = 2;
state->y = 1;
context->interlace = 0;
break;
default:
/* just make sure we don't loop forever */
context->interlace = 0;
} }
} if (context->flush) {
state->state = FLUSH;
break;
}
this = state->buffer[state->x++]; /* get another line of data */
state->shuffle(
state->buffer,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize
);
if (this == context->last) state->x = 0;
context->count++;
else {
EMIT_RUN(label1);
context->last = this;
context->count = 1;
}
break;
if (state->state == INIT) {
/* preload the run-length buffer and get going */
context->last = state->buffer[0];
context->count = state->x = 1;
state->state = ENCODE;
}
case ENCODE_EOF: /* step forward, according to the interlace settings */
state->y += context->step;
while (context->interlace && state->y >= state->ysize)
switch (context->interlace) {
case 1:
state->y = 4;
context->interlace = 2;
break;
case 2:
context->step = 4;
state->y = 2;
context->interlace = 3;
break;
case 3:
context->step = 2;
state->y = 1;
context->interlace = 0;
break;
default:
/* just make sure we don't loop forever */
context->interlace = 0;
}
/* write the final run */
EMIT_RUN(label2);
/* write an end of image marker */
EMIT(EOF_CODE);
/* empty the bit buffer */
while (context->bitcount > 0) {
if (!emit(context, (UINT8) context->bitbuffer)) {
state->errcode = IMAGING_CODEC_MEMORY;
return 0;
} }
context->bitbuffer >>= 8;
context->bitcount -= 8;
}
/* flush the last block, and exit */ this = state->buffer[state->x++];
if (context->block) {
GIFENCODERBLOCK* block;
block = context->flush;
while (block && block->next)
block = block->next;
if (block)
block->next = context->block;
else
context->flush = context->block;
context->block = NULL;
}
state->state = EXIT; if (this == context->last)
context->count++;
else {
EMIT_RUN(label1);
context->last = this;
context->count = 1;
}
break;
/* fall through... */
case EXIT: case ENCODE_EOF:
case FLUSH:
while (context->flush) { /* write the final run */
EMIT_RUN(label2);
/* get a block from the flush queue */ /* write an end of image marker */
block = context->flush; EMIT(EOF_CODE);
if (block->size > 0) { /* empty the bit buffer */
while (context->bitcount > 0) {
if (!emit(context, (UINT8) context->bitbuffer)) {
state->errcode = IMAGING_CODEC_MEMORY;
return 0;
}
context->bitbuffer >>= 8;
context->bitcount -= 8;
}
/* make sure it fits into the output buffer */ /* flush the last block, and exit */
if (bytes < block->size+1) if (context->block) {
GIFENCODERBLOCK* block;
block = context->flush;
while (block && block->next)
block = block->next;
if (block)
block->next = context->block;
else
context->flush = context->block;
context->block = NULL;
}
state->state = EXIT;
/* fall through... */
case EXIT:
case FLUSH:
while (context->flush) {
/* get a block from the flush queue */
block = context->flush;
if (block->size > 0) {
/* make sure it fits into the output buffer */
if (bytes < block->size+1)
return ptr - buf;
ptr[0] = block->size;
memcpy(ptr+1, block->data, block->size);
ptr += block->size+1;
bytes -= block->size+1;
}
context->flush = block->next;
if (context->free)
free(context->free);
context->free = block;
}
if (state->state == EXIT) {
/* this was the last block! */
if (context->free)
free(context->free);
state->errcode = IMAGING_CODEC_END;
return ptr - buf; return ptr - buf;
}
ptr[0] = block->size; state->state = ENCODE;
memcpy(ptr+1, block->data, block->size); break;
}
ptr += block->size+1;
bytes -= block->size+1;
}
context->flush = block->next;
if (context->free)
free(context->free);
context->free = block;
}
if (state->state == EXIT) {
/* this was the last block! */
if (context->free)
free(context->free);
state->errcode = IMAGING_CODEC_END;
return ptr - buf;
}
state->state = ENCODE;
break;
}
} }

View File

@ -5,7 +5,7 @@
* decoder for hex encoded image data * decoder for hex encoded image data
* *
* history: * history:
* 96-05-16 fl Created * 96-05-16 fl Created
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -16,9 +16,9 @@
#include "Imaging.h" #include "Imaging.h"
#define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\ #define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\
(v >= 'a' && v <= 'f') ? v - 'a' + 10 :\ (v >= 'a' && v <= 'f') ? v - 'a' + 10 :\
(v >= 'A' && v <= 'F') ? v - 'A' + 10 : -1) (v >= 'A' && v <= 'F') ? v - 'A' + 10 : -1)
int int
ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes) ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
@ -30,38 +30,38 @@ ImagingHexDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
for (;;) { for (;;) {
if (bytes < 2) if (bytes < 2)
return ptr - buf; return ptr - buf;
a = HEX(ptr[0]); a = HEX(ptr[0]);
b = HEX(ptr[1]); b = HEX(ptr[1]);
if (a < 0 || b < 0) { if (a < 0 || b < 0) {
ptr++; ptr++;
bytes--; bytes--;
} else { } else {
ptr += 2; ptr += 2;
bytes -= 2; bytes -= 2;
state->buffer[state->x] = (a<<4) + b; state->buffer[state->x] = (a<<4) + b;
if (++state->x >= state->bytes) { if (++state->x >= state->bytes) {
/* Got a full line, unpack it */ /* Got a full line, unpack it */
state->shuffle((UINT8*) im->image[state->y], state->buffer, state->shuffle((UINT8*) im->image[state->y], state->buffer,
state->xsize); state->xsize);
state->x = 0; state->x = 0;
if (++state->y >= state->ysize) { if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */ /* End of file (errcode = 0) */
return -1; return -1;
} }
} }
} }
} }
} }

View File

@ -30,7 +30,7 @@ void
ImagingHistogramDelete(ImagingHistogram h) ImagingHistogramDelete(ImagingHistogram h)
{ {
if (h->histogram) if (h->histogram)
free(h->histogram); free(h->histogram);
free(h); free(h);
} }
@ -60,115 +60,115 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void* minmax)
FLOAT32 fmin, fmax, scale; FLOAT32 fmin, fmax, scale;
if (!im) if (!im)
return ImagingError_ModeError(); return ImagingError_ModeError();
if (imMask) { if (imMask) {
/* Validate mask */ /* Validate mask */
if (im->xsize != imMask->xsize || im->ysize != imMask->ysize) if (im->xsize != imMask->xsize || im->ysize != imMask->ysize)
return ImagingError_Mismatch(); return ImagingError_Mismatch();
if (strcmp(imMask->mode, "1") != 0 && strcmp(imMask->mode, "L") != 0) if (strcmp(imMask->mode, "1") != 0 && strcmp(imMask->mode, "L") != 0)
return ImagingError_ValueError("bad transparency mask"); return ImagingError_ValueError("bad transparency mask");
} }
h = ImagingHistogramNew(im); h = ImagingHistogramNew(im);
if (imMask) { if (imMask) {
/* mask */ /* mask */
if (im->image8) { if (im->image8) {
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
for (y = 0; y < im->ysize; y++) for (y = 0; y < im->ysize; y++)
for (x = 0; x < im->xsize; x++) for (x = 0; x < im->xsize; x++)
if (imMask->image8[y][x] != 0) if (imMask->image8[y][x] != 0)
h->histogram[im->image8[y][x]]++; h->histogram[im->image8[y][x]]++;
ImagingSectionLeave(&cookie); ImagingSectionLeave(&cookie);
} else { /* yes, we need the braces. C isn't Python! */ } else { /* yes, we need the braces. C isn't Python! */
if (im->type != IMAGING_TYPE_UINT8) { if (im->type != IMAGING_TYPE_UINT8) {
ImagingHistogramDelete(h); ImagingHistogramDelete(h);
return ImagingError_ModeError(); return ImagingError_ModeError();
} }
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
for (y = 0; y < im->ysize; y++) { for (y = 0; y < im->ysize; y++) {
UINT8* in = (UINT8*) im->image32[y]; UINT8* in = (UINT8*) im->image32[y];
for (x = 0; x < im->xsize; x++) for (x = 0; x < im->xsize; x++)
if (imMask->image8[y][x] != 0) { if (imMask->image8[y][x] != 0) {
h->histogram[(*in++)]++;
h->histogram[(*in++)+256]++;
h->histogram[(*in++)+512]++;
h->histogram[(*in++)+768]++;
} else
in += 4;
}
ImagingSectionLeave(&cookie);
}
} else {
/* mask not given; process pixels in image */
if (im->image8) {
ImagingSectionEnter(&cookie);
for (y = 0; y < im->ysize; y++)
for (x = 0; x < im->xsize; x++)
h->histogram[im->image8[y][x]]++;
ImagingSectionLeave(&cookie);
} else {
switch (im->type) {
case IMAGING_TYPE_UINT8:
ImagingSectionEnter(&cookie);
for (y = 0; y < im->ysize; y++) {
UINT8* in = (UINT8*) im->image[y];
for (x = 0; x < im->xsize; x++) {
h->histogram[(*in++)]++; h->histogram[(*in++)]++;
h->histogram[(*in++)+256]++; h->histogram[(*in++)+256]++;
h->histogram[(*in++)+512]++; h->histogram[(*in++)+512]++;
h->histogram[(*in++)+768]++; h->histogram[(*in++)+768]++;
} } else
} in += 4;
ImagingSectionLeave(&cookie); }
break; ImagingSectionLeave(&cookie);
case IMAGING_TYPE_INT32: }
if (!minmax) { } else {
ImagingHistogramDelete(h); /* mask not given; process pixels in image */
return ImagingError_ValueError("min/max not given"); if (im->image8) {
}
if (!im->xsize || !im->ysize)
break;
memcpy(&imin, minmax, sizeof(imin));
memcpy(&imax, ((char*)minmax) + sizeof(imin), sizeof(imax));
if (imin >= imax)
break;
ImagingSectionEnter(&cookie); ImagingSectionEnter(&cookie);
scale = 255.0F / (imax - imin); for (y = 0; y < im->ysize; y++)
for (y = 0; y < im->ysize; y++) { for (x = 0; x < im->xsize; x++)
INT32* in = im->image32[y]; h->histogram[im->image8[y][x]]++;
for (x = 0; x < im->xsize; x++) { ImagingSectionLeave(&cookie);
i = (int) (((*in++)-imin)*scale); } else {
if (i >= 0 && i < 256) switch (im->type) {
h->histogram[i]++; case IMAGING_TYPE_UINT8:
ImagingSectionEnter(&cookie);
for (y = 0; y < im->ysize; y++) {
UINT8* in = (UINT8*) im->image[y];
for (x = 0; x < im->xsize; x++) {
h->histogram[(*in++)]++;
h->histogram[(*in++)+256]++;
h->histogram[(*in++)+512]++;
h->histogram[(*in++)+768]++;
}
} }
} ImagingSectionLeave(&cookie);
ImagingSectionLeave(&cookie);
break;
case IMAGING_TYPE_FLOAT32:
if (!minmax) {
ImagingHistogramDelete(h);
return ImagingError_ValueError("min/max not given");
}
if (!im->xsize || !im->ysize)
break; break;
memcpy(&fmin, minmax, sizeof(fmin)); case IMAGING_TYPE_INT32:
memcpy(&fmax, ((char*)minmax) + sizeof(fmin), sizeof(fmax)); if (!minmax) {
if (fmin >= fmax) ImagingHistogramDelete(h);
break; return ImagingError_ValueError("min/max not given");
ImagingSectionEnter(&cookie);
scale = 255.0F / (fmax - fmin);
for (y = 0; y < im->ysize; y++) {
FLOAT32* in = (FLOAT32*) im->image32[y];
for (x = 0; x < im->xsize; x++) {
i = (int) (((*in++)-fmin)*scale);
if (i >= 0 && i < 256)
h->histogram[i]++;
} }
} if (!im->xsize || !im->ysize)
ImagingSectionLeave(&cookie); break;
break; memcpy(&imin, minmax, sizeof(imin));
memcpy(&imax, ((char*)minmax) + sizeof(imin), sizeof(imax));
if (imin >= imax)
break;
ImagingSectionEnter(&cookie);
scale = 255.0F / (imax - imin);
for (y = 0; y < im->ysize; y++) {
INT32* in = im->image32[y];
for (x = 0; x < im->xsize; x++) {
i = (int) (((*in++)-imin)*scale);
if (i >= 0 && i < 256)
h->histogram[i]++;
}
}
ImagingSectionLeave(&cookie);
break;
case IMAGING_TYPE_FLOAT32:
if (!minmax) {
ImagingHistogramDelete(h);
return ImagingError_ValueError("min/max not given");
}
if (!im->xsize || !im->ysize)
break;
memcpy(&fmin, minmax, sizeof(fmin));
memcpy(&fmax, ((char*)minmax) + sizeof(fmin), sizeof(fmax));
if (fmin >= fmax)
break;
ImagingSectionEnter(&cookie);
scale = 255.0F / (fmax - fmin);
for (y = 0; y < im->ysize; y++) {
FLOAT32* in = (FLOAT32*) im->image32[y];
for (x = 0; x < im->xsize; x++) {
i = (int) (((*in++)-fmin)*scale);
if (i >= 0 && i < 256)
h->histogram[i]++;
}
}
ImagingSectionLeave(&cookie);
break;
} }
} }
} }

View File

@ -39,41 +39,41 @@
/* For System that are not Windows, we'll need to define these. */ /* For System that are not Windows, we'll need to define these. */
#if SIZEOF_SHORT == 2 #if SIZEOF_SHORT == 2
#define INT16 short #define INT16 short
#elif SIZEOF_INT == 2 #elif SIZEOF_INT == 2
#define INT16 int #define INT16 int
#else #else
#define INT16 short /* most things works just fine anyway... */ #define INT16 short /* most things works just fine anyway... */
#endif #endif
#if SIZEOF_SHORT == 4 #if SIZEOF_SHORT == 4
#define INT32 short #define INT32 short
#elif SIZEOF_INT == 4 #elif SIZEOF_INT == 4
#define INT32 int #define INT32 int
#elif SIZEOF_LONG == 4 #elif SIZEOF_LONG == 4
#define INT32 long #define INT32 long
#else #else
#error Cannot find required 32-bit integer type #error Cannot find required 32-bit integer type
#endif #endif
#if SIZEOF_LONG == 8 #if SIZEOF_LONG == 8
#define INT64 long #define INT64 long
#elif SIZEOF_LONG_LONG == 8 #elif SIZEOF_LONG_LONG == 8
#define INT64 long #define INT64 long
#endif #endif
#define INT8 signed char #define INT8 signed char
#define UINT8 unsigned char #define UINT8 unsigned char
#define UINT16 unsigned INT16 #define UINT16 unsigned INT16
#define UINT32 unsigned INT32 #define UINT32 unsigned INT32
#endif #endif
/* assume IEEE; tweak if necessary (patches are welcome) */ /* assume IEEE; tweak if necessary (patches are welcome) */
#define FLOAT16 UINT16 #define FLOAT16 UINT16
#define FLOAT32 float #define FLOAT32 float
#define FLOAT64 double #define FLOAT64 double
#ifdef _MSC_VER #ifdef _MSC_VER
typedef signed __int64 int64_t; typedef signed __int64 int64_t;

View File

@ -14,13 +14,13 @@
typedef struct { typedef struct {
struct jpeg_error_mgr pub; /* "public" fields */ struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */ jmp_buf setjmp_buffer; /* for return to caller */
} JPEGERROR; } JPEGERROR;
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Decoder */ /* Decoder */
typedef struct { typedef struct {
struct jpeg_source_mgr pub; struct jpeg_source_mgr pub;
@ -56,7 +56,7 @@ typedef struct {
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Encoder */ /* Encoder */
typedef struct { typedef struct {
struct jpeg_destination_mgr pub; struct jpeg_destination_mgr pub;

View File

@ -14,7 +14,7 @@
#define BUFFER_SIZE OPJ_J2K_STREAM_CHUNK_SIZE #define BUFFER_SIZE OPJ_J2K_STREAM_CHUNK_SIZE
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Decoder */ /* Decoder */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
typedef struct { typedef struct {
@ -44,7 +44,7 @@ typedef struct {
} JPEG2KDECODESTATE; } JPEG2KDECODESTATE;
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Encoder */ /* Encoder */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
typedef struct { typedef struct {

View File

@ -301,14 +301,14 @@ ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t by
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
int ImagingJpegDecodeCleanup(ImagingCodecState state){ int ImagingJpegDecodeCleanup(ImagingCodecState state){
/* called to free the decompression engine when the decode terminates /* called to free the decompression engine when the decode terminates
due to a corrupt or truncated image due to a corrupt or truncated image
*/ */
JPEGSTATE* context = (JPEGSTATE*) state->context; JPEGSTATE* context = (JPEGSTATE*) state->context;
/* Clean up */ /* Clean up */
jpeg_destroy_decompress(&context->cinfo); jpeg_destroy_decompress(&context->cinfo);
return -1; return -1;
} }
#endif #endif

View File

@ -78,7 +78,7 @@ error(j_common_ptr cinfo)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Encoder */ /* Encoder */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
int int
@ -88,24 +88,24 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
int ok; int ok;
if (setjmp(context->error.setjmp_buffer)) { if (setjmp(context->error.setjmp_buffer)) {
/* JPEG error handler */ /* JPEG error handler */
jpeg_destroy_compress(&context->cinfo); jpeg_destroy_compress(&context->cinfo);
state->errcode = IMAGING_CODEC_BROKEN; state->errcode = IMAGING_CODEC_BROKEN;
return -1; return -1;
} }
if (!state->state) { if (!state->state) {
/* Setup compression context (very similar to the decoder) */ /* Setup compression context (very similar to the decoder) */
context->cinfo.err = jpeg_std_error(&context->error.pub); context->cinfo.err = jpeg_std_error(&context->error.pub);
context->error.pub.error_exit = error; context->error.pub.error_exit = error;
jpeg_create_compress(&context->cinfo); jpeg_create_compress(&context->cinfo);
jpeg_buffer_dest(&context->cinfo, &context->destination); jpeg_buffer_dest(&context->cinfo, &context->destination);
context->extra_offset = 0; context->extra_offset = 0;
/* Ready to encode */ /* Ready to encode */
state->state = 1; state->state = 1;
} }
@ -115,212 +115,212 @@ ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
switch (state->state) { switch (state->state) {
case 1: case 1:
context->cinfo.image_width = state->xsize; context->cinfo.image_width = state->xsize;
context->cinfo.image_height = state->ysize; context->cinfo.image_height = state->ysize;
switch (state->bits) { switch (state->bits) {
case 8: case 8:
context->cinfo.input_components = 1; context->cinfo.input_components = 1;
context->cinfo.in_color_space = JCS_GRAYSCALE; context->cinfo.in_color_space = JCS_GRAYSCALE;
break; break;
case 24: case 24:
context->cinfo.input_components = 3; context->cinfo.input_components = 3;
if (strcmp(im->mode, "YCbCr") == 0) if (strcmp(im->mode, "YCbCr") == 0)
context->cinfo.in_color_space = JCS_YCbCr; context->cinfo.in_color_space = JCS_YCbCr;
else else
context->cinfo.in_color_space = JCS_RGB; context->cinfo.in_color_space = JCS_RGB;
break; break;
case 32: case 32:
context->cinfo.input_components = 4; context->cinfo.input_components = 4;
context->cinfo.in_color_space = JCS_CMYK; context->cinfo.in_color_space = JCS_CMYK;
#ifdef JCS_EXTENSIONS #ifdef JCS_EXTENSIONS
if (strcmp(context->rawmode, "RGBX") == 0) if (strcmp(context->rawmode, "RGBX") == 0)
context->cinfo.in_color_space = JCS_EXT_RGBX; context->cinfo.in_color_space = JCS_EXT_RGBX;
#endif #endif
break; break;
default: default:
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
} }
/* Compressor configuration */ /* Compressor configuration */
jpeg_set_defaults(&context->cinfo); jpeg_set_defaults(&context->cinfo);
/* Use custom quantization tables */ /* Use custom quantization tables */
if (context->qtables) { if (context->qtables) {
int i; int i;
int quality = 100; int quality = 100;
int last_q = 0; int last_q = 0;
if (context->quality != -1) { if (context->quality != -1) {
quality = context->quality; quality = context->quality;
} }
for (i = 0; i < context->qtablesLen; i++) { for (i = 0; i < context->qtablesLen; i++) {
// TODO: Should add support for none baseline // TODO: Should add support for none baseline
jpeg_add_quant_table(&context->cinfo, i, &context->qtables[i * DCTSIZE2], jpeg_add_quant_table(&context->cinfo, i, &context->qtables[i * DCTSIZE2],
quality, TRUE); quality, TRUE);
context->cinfo.comp_info[i].quant_tbl_no = i; context->cinfo.comp_info[i].quant_tbl_no = i;
last_q = i; last_q = i;
} }
if (context->qtablesLen == 1) { if (context->qtablesLen == 1) {
// jpeg_set_defaults created two qtables internally, but we only wanted one. // jpeg_set_defaults created two qtables internally, but we only wanted one.
jpeg_add_quant_table(&context->cinfo, 1, &context->qtables[0], jpeg_add_quant_table(&context->cinfo, 1, &context->qtables[0],
quality, TRUE); quality, TRUE);
} }
for (i = last_q; i < context->cinfo.num_components; i++) { for (i = last_q; i < context->cinfo.num_components; i++) {
context->cinfo.comp_info[i].quant_tbl_no = last_q; context->cinfo.comp_info[i].quant_tbl_no = last_q;
} }
} else if (context->quality != -1) { } else if (context->quality != -1) {
jpeg_set_quality(&context->cinfo, context->quality, 1); jpeg_set_quality(&context->cinfo, context->quality, 1);
} }
/* Set subsampling options */ /* Set subsampling options */
switch (context->subsampling) switch (context->subsampling)
{ {
case 0: /* 1x1 1x1 1x1 (4:4:4) : None */ case 0: /* 1x1 1x1 1x1 (4:4:4) : None */
{ {
context->cinfo.comp_info[0].h_samp_factor = 1; context->cinfo.comp_info[0].h_samp_factor = 1;
context->cinfo.comp_info[0].v_samp_factor = 1; context->cinfo.comp_info[0].v_samp_factor = 1;
context->cinfo.comp_info[1].h_samp_factor = 1; context->cinfo.comp_info[1].h_samp_factor = 1;
context->cinfo.comp_info[1].v_samp_factor = 1; context->cinfo.comp_info[1].v_samp_factor = 1;
context->cinfo.comp_info[2].h_samp_factor = 1; context->cinfo.comp_info[2].h_samp_factor = 1;
context->cinfo.comp_info[2].v_samp_factor = 1; context->cinfo.comp_info[2].v_samp_factor = 1;
break; break;
} }
case 1: /* 2x1, 1x1, 1x1 (4:2:2) : Medium */ case 1: /* 2x1, 1x1, 1x1 (4:2:2) : Medium */
{ {
context->cinfo.comp_info[0].h_samp_factor = 2; context->cinfo.comp_info[0].h_samp_factor = 2;
context->cinfo.comp_info[0].v_samp_factor = 1; context->cinfo.comp_info[0].v_samp_factor = 1;
context->cinfo.comp_info[1].h_samp_factor = 1; context->cinfo.comp_info[1].h_samp_factor = 1;
context->cinfo.comp_info[1].v_samp_factor = 1; context->cinfo.comp_info[1].v_samp_factor = 1;
context->cinfo.comp_info[2].h_samp_factor = 1; context->cinfo.comp_info[2].h_samp_factor = 1;
context->cinfo.comp_info[2].v_samp_factor = 1; context->cinfo.comp_info[2].v_samp_factor = 1;
break; break;
} }
case 2: /* 2x2, 1x1, 1x1 (4:2:0) : High */ case 2: /* 2x2, 1x1, 1x1 (4:2:0) : High */
{ {
context->cinfo.comp_info[0].h_samp_factor = 2; context->cinfo.comp_info[0].h_samp_factor = 2;
context->cinfo.comp_info[0].v_samp_factor = 2; context->cinfo.comp_info[0].v_samp_factor = 2;
context->cinfo.comp_info[1].h_samp_factor = 1; context->cinfo.comp_info[1].h_samp_factor = 1;
context->cinfo.comp_info[1].v_samp_factor = 1; context->cinfo.comp_info[1].v_samp_factor = 1;
context->cinfo.comp_info[2].h_samp_factor = 1; context->cinfo.comp_info[2].h_samp_factor = 1;
context->cinfo.comp_info[2].v_samp_factor = 1; context->cinfo.comp_info[2].v_samp_factor = 1;
break; break;
} }
default: default:
{ {
/* Use the lib's default */ /* Use the lib's default */
break; break;
} }
} }
if (context->progressive) if (context->progressive)
jpeg_simple_progression(&context->cinfo); jpeg_simple_progression(&context->cinfo);
context->cinfo.smoothing_factor = context->smooth; context->cinfo.smoothing_factor = context->smooth;
context->cinfo.optimize_coding = (boolean) context->optimize; context->cinfo.optimize_coding = (boolean) context->optimize;
if (context->xdpi > 0 && context->ydpi > 0) { if (context->xdpi > 0 && context->ydpi > 0) {
context->cinfo.density_unit = 1; /* dots per inch */ context->cinfo.density_unit = 1; /* dots per inch */
context->cinfo.X_density = context->xdpi; context->cinfo.X_density = context->xdpi;
context->cinfo.Y_density = context->ydpi; context->cinfo.Y_density = context->ydpi;
} }
switch (context->streamtype) { switch (context->streamtype) {
case 1: case 1:
/* tables only -- not yet implemented */ /* tables only -- not yet implemented */
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
case 2: case 2:
/* image only */ /* image only */
jpeg_suppress_tables(&context->cinfo, TRUE); jpeg_suppress_tables(&context->cinfo, TRUE);
jpeg_start_compress(&context->cinfo, FALSE); jpeg_start_compress(&context->cinfo, FALSE);
/* suppress extra section */ /* suppress extra section */
context->extra_offset = context->extra_size; context->extra_offset = context->extra_size;
break; break;
default: default:
/* interchange stream */ /* interchange stream */
jpeg_start_compress(&context->cinfo, TRUE); jpeg_start_compress(&context->cinfo, TRUE);
break; break;
} }
state->state++; state->state++;
/* fall through */ /* fall through */
case 2: case 2:
// check for exif len + 'APP1' header bytes // check for exif len + 'APP1' header bytes
if (context->rawExifLen + 5 > context->destination.pub.free_in_buffer){ if (context->rawExifLen + 5 > context->destination.pub.free_in_buffer){
break;
}
//add exif header
if (context->rawExifLen > 0){
jpeg_write_marker(&context->cinfo, JPEG_APP0+1,
(unsigned char*)context->rawExif, context->rawExifLen);
}
state->state++;
/* fall through */
case 3:
if (context->extra) {
/* copy extra buffer to output buffer */
unsigned int n = context->extra_size - context->extra_offset;
if (n > context->destination.pub.free_in_buffer)
n = context->destination.pub.free_in_buffer;
memcpy(context->destination.pub.next_output_byte,
context->extra + context->extra_offset, n);
context->destination.pub.next_output_byte += n;
context->destination.pub.free_in_buffer -= n;
context->extra_offset += n;
if (context->extra_offset >= context->extra_size)
state->state++;
else
break; break;
} else }
state->state++; //add exif header
if (context->rawExifLen > 0){
jpeg_write_marker(&context->cinfo, JPEG_APP0+1,
(unsigned char*)context->rawExif, context->rawExifLen);
}
case 4: state->state++;
if (1024 > context->destination.pub.free_in_buffer){ /* fall through */
case 3:
if (context->extra) {
/* copy extra buffer to output buffer */
unsigned int n = context->extra_size - context->extra_offset;
if (n > context->destination.pub.free_in_buffer)
n = context->destination.pub.free_in_buffer;
memcpy(context->destination.pub.next_output_byte,
context->extra + context->extra_offset, n);
context->destination.pub.next_output_byte += n;
context->destination.pub.free_in_buffer -= n;
context->extra_offset += n;
if (context->extra_offset >= context->extra_size)
state->state++;
else
break;
} else
state->state++;
case 4:
if (1024 > context->destination.pub.free_in_buffer){
break;
}
ok = 1;
while (state->y < state->ysize) {
state->shuffle(state->buffer,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize);
ok = jpeg_write_scanlines(&context->cinfo, &state->buffer, 1);
if (ok != 1)
break;
state->y++;
}
if (ok != 1)
break;
state->state++;
/* fall through */
case 5:
/* Finish compression */
if (context->destination.pub.free_in_buffer < 100)
break;
jpeg_finish_compress(&context->cinfo);
/* Clean up */
if (context->extra) {
free(context->extra);
context->extra = NULL;
}
if (context->rawExif) {
free(context->rawExif);
context->rawExif = NULL;
}
if (context->qtables) {
free(context->qtables);
context->qtables = NULL;
}
jpeg_destroy_compress(&context->cinfo);
/* if (jerr.pub.num_warnings) return BROKEN; */
state->errcode = IMAGING_CODEC_END;
break; break;
}
ok = 1;
while (state->y < state->ysize) {
state->shuffle(state->buffer,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize);
ok = jpeg_write_scanlines(&context->cinfo, &state->buffer, 1);
if (ok != 1)
break;
state->y++;
}
if (ok != 1)
break;
state->state++;
/* fall through */
case 5:
/* Finish compression */
if (context->destination.pub.free_in_buffer < 100)
break;
jpeg_finish_compress(&context->cinfo);
/* Clean up */
if (context->extra) {
free(context->extra);
context->extra = NULL;
}
if (context->rawExif) {
free(context->rawExif);
context->rawExif = NULL;
}
if (context->qtables) {
free(context->qtables);
context->qtables = NULL;
}
jpeg_destroy_compress(&context->cinfo);
/* if (jerr.pub.num_warnings) return BROKEN; */
state->errcode = IMAGING_CODEC_END;
break;
} }

View File

@ -17,7 +17,7 @@
#include "Imaging.h" #include "Imaging.h"
#define CLIPF(v) ((v <= 0.0) ? 0 : (v >= 255.0F) ? 255 : (UINT8) v) #define CLIPF(v) ((v <= 0.0) ? 0 : (v >= 255.0F) ? 255 : (UINT8) v)
Imaging Imaging
@ -28,47 +28,47 @@ ImagingConvertMatrix(Imaging im, const char *mode, float m[])
/* Assume there's enough data in the buffer */ /* Assume there's enough data in the buffer */
if (!im) if (!im)
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
if (strcmp(mode, "L") == 0 && im->bands == 3) { if (strcmp(mode, "L") == 0 && im->bands == 3) {
imOut = ImagingNewDirty("L", im->xsize, im->ysize); imOut = ImagingNewDirty("L", im->xsize, im->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
for (y = 0; y < im->ysize; y++) { for (y = 0; y < im->ysize; y++) {
UINT8* in = (UINT8*) im->image[y]; UINT8* in = (UINT8*) im->image[y];
UINT8* out = (UINT8*) imOut->image[y]; UINT8* out = (UINT8*) imOut->image[y];
for (x = 0; x < im->xsize; x++) { for (x = 0; x < im->xsize; x++) {
float v = m[0]*in[0] + m[1]*in[1] + m[2]*in[2] + m[3] + 0.5; float v = m[0]*in[0] + m[1]*in[1] + m[2]*in[2] + m[3] + 0.5;
out[x] = CLIPF(v); out[x] = CLIPF(v);
in += 4; in += 4;
} }
} }
} else if (strlen(mode) == 3 && im->bands == 3) { } else if (strlen(mode) == 3 && im->bands == 3) {
imOut = ImagingNewDirty(mode, im->xsize, im->ysize); imOut = ImagingNewDirty(mode, im->xsize, im->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
for (y = 0; y < im->ysize; y++) { for (y = 0; y < im->ysize; y++) {
UINT8* in = (UINT8*) im->image[y]; UINT8* in = (UINT8*) im->image[y];
UINT8* out = (UINT8*) imOut->image[y]; UINT8* out = (UINT8*) imOut->image[y];
for (x = 0; x < im->xsize; x++) { for (x = 0; x < im->xsize; x++) {
float v0 = m[0]*in[0] + m[1]*in[1] + m[2]*in[2] + m[3] + 0.5; float v0 = m[0]*in[0] + m[1]*in[1] + m[2]*in[2] + m[3] + 0.5;
float v1 = m[4]*in[0] + m[5]*in[1] + m[6]*in[2] + m[7] + 0.5; float v1 = m[4]*in[0] + m[5]*in[1] + m[6]*in[2] + m[7] + 0.5;
float v2 = m[8]*in[0] + m[9]*in[1] + m[10]*in[2] + m[11] + 0.5; float v2 = m[8]*in[0] + m[9]*in[1] + m[10]*in[2] + m[11] + 0.5;
out[0] = CLIPF(v0); out[0] = CLIPF(v0);
out[1] = CLIPF(v1); out[1] = CLIPF(v1);
out[2] = CLIPF(v2); out[2] = CLIPF(v2);
in += 4; out += 4; in += 4; out += 4;
} }
} }
} else } else
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
return imOut; return imOut;
} }

View File

@ -8,7 +8,7 @@
* FIXME: Maybe this should be implemented using ImagingPoint() * FIXME: Maybe this should be implemented using ImagingPoint()
* *
* history: * history:
* 95-11-27 fl: Created * 95-11-27 fl: Created
* *
* Copyright (c) Fredrik Lundh 1995. * Copyright (c) Fredrik Lundh 1995.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -27,15 +27,15 @@ ImagingNegative(Imaging im)
int x, y; int x, y;
if (!im) if (!im)
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
for (y = 0; y < im->ysize; y++) for (y = 0; y < im->ysize; y++)
for (x = 0; x < im->linesize; x++) for (x = 0; x < im->linesize; x++)
imOut->image[y][x] = ~im->image[y][x]; imOut->image[y][x] = ~im->image[y][x];
return imOut; return imOut;
} }

View File

@ -5,7 +5,7 @@
* offset an image in x and y directions * offset an image in x and y directions
* *
* history: * history:
* 96-07-22 fl: Created * 96-07-22 fl: Created
* 98-11-01 cgw@pgt.com: Fixed negative-array index bug * 98-11-01 cgw@pgt.com: Fixed negative-array index bug
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
@ -25,11 +25,11 @@ ImagingOffset(Imaging im, int xoffset, int yoffset)
Imaging imOut; Imaging imOut;
if (!im) if (!im)
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize); imOut = ImagingNewDirty(im->mode, im->xsize, im->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
ImagingCopyPalette(imOut, im); ImagingCopyPalette(imOut, im);
@ -37,25 +37,25 @@ ImagingOffset(Imaging im, int xoffset, int yoffset)
xoffset %= im->xsize; xoffset %= im->xsize;
xoffset = im->xsize - xoffset; xoffset = im->xsize - xoffset;
if (xoffset < 0) if (xoffset < 0)
xoffset += im->xsize; xoffset += im->xsize;
yoffset %= im->ysize; yoffset %= im->ysize;
yoffset = im->ysize - yoffset; yoffset = im->ysize - yoffset;
if (yoffset < 0) if (yoffset < 0)
yoffset += im->ysize; yoffset += im->ysize;
#define OFFSET(image)\ #define OFFSET(image)\
for (y = 0; y < im->ysize; y++)\ for (y = 0; y < im->ysize; y++)\
for (x = 0; x < im->xsize; x++) {\ for (x = 0; x < im->xsize; x++) {\
int yi = (y + yoffset) % im->ysize;\ int yi = (y + yoffset) % im->ysize;\
int xi = (x + xoffset) % im->xsize;\ int xi = (x + xoffset) % im->xsize;\
imOut->image[y][x] = im->image[yi][xi];\ imOut->image[y][x] = im->image[yi][xi];\
} }
if (im->image8) if (im->image8)
OFFSET(image8) OFFSET(image8)
else else
OFFSET(image32) OFFSET(image32)
return imOut; return imOut;
} }

View File

@ -5,7 +5,7 @@
* decoder for PackBits image data. * decoder for PackBits image data.
* *
* history: * history:
* 96-04-19 fl Created * 96-04-19 fl Created
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -18,7 +18,7 @@
int int
ImagingPackbitsDecode(Imaging im, ImagingCodecState state, ImagingPackbitsDecode(Imaging im, ImagingCodecState state,
UINT8* buf, Py_ssize_t bytes) UINT8* buf, Py_ssize_t bytes)
{ {
UINT8 n; UINT8 n;
UINT8* ptr; UINT8* ptr;
@ -28,65 +28,65 @@ ImagingPackbitsDecode(Imaging im, ImagingCodecState state,
for (;;) { for (;;) {
if (bytes < 1) if (bytes < 1)
return ptr - buf; return ptr - buf;
if (ptr[0] & 0x80) { if (ptr[0] & 0x80) {
if (ptr[0] == 0x80) { if (ptr[0] == 0x80) {
/* Nop */ /* Nop */
ptr++; bytes--; ptr++; bytes--;
continue; continue;
} }
/* Run */ /* Run */
if (bytes < 2) if (bytes < 2)
return ptr - buf; return ptr - buf;
for (n = 257 - ptr[0]; n > 0; n--) { for (n = 257 - ptr[0]; n > 0; n--) {
if (state->x >= state->bytes) { if (state->x >= state->bytes) {
/* state->errcode = IMAGING_CODEC_OVERRUN; */ /* state->errcode = IMAGING_CODEC_OVERRUN; */
break; break;
} }
state->buffer[state->x++] = ptr[1]; state->buffer[state->x++] = ptr[1];
} }
ptr += 2; bytes -= 2; ptr += 2; bytes -= 2;
} else { } else {
/* Literal */ /* Literal */
n = ptr[0]+2; n = ptr[0]+2;
if (bytes < n) if (bytes < n)
return ptr - buf; return ptr - buf;
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
if (state->x >= state->bytes) { if (state->x >= state->bytes) {
/* state->errcode = IMAGING_CODEC_OVERRUN; */ /* state->errcode = IMAGING_CODEC_OVERRUN; */
break; break;
} }
state->buffer[state->x++] = ptr[i]; state->buffer[state->x++] = ptr[i];
} }
ptr += n; bytes -= n; ptr += n; bytes -= n;
} }
if (state->x >= state->bytes) { if (state->x >= state->bytes) {
/* Got a full line, unpack it */ /* Got a full line, unpack it */
state->shuffle((UINT8*) im->image[state->y + state->yoff] + state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->buffer, state->xoff * im->pixelsize, state->buffer,
state->xsize); state->xsize);
state->x = 0; state->x = 0;
if (++state->y >= state->ysize) { if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */ /* End of file (errcode = 0) */
return -1; return -1;
} }
} }
} }
} }

View File

@ -5,13 +5,13 @@
* decoder for uncompressed PCD image data. * decoder for uncompressed PCD image data.
* *
* history: * history:
* 96-05-10 fl Created * 96-05-10 fl Created
* 96-05-18 fl New tables * 96-05-18 fl New tables
* 97-01-25 fl Use PhotoYCC unpacker * 97-01-25 fl Use PhotoYCC unpacker
* *
* notes: * notes:
* This driver supports uncompressed PCD modes only * This driver supports uncompressed PCD modes only
* (resolutions up to 768x512). * (resolutions up to 768x512).
* *
* Copyright (c) Fredrik Lundh 1996-97. * Copyright (c) Fredrik Lundh 1996-97.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -37,42 +37,42 @@ ImagingPcdDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
for (;;) { for (;;) {
/* We need data for two full lines before we can do anything */ /* We need data for two full lines before we can do anything */
if (bytes < chunk) if (bytes < chunk)
return ptr - buf; return ptr - buf;
/* Unpack first line */ /* Unpack first line */
out = state->buffer; out = state->buffer;
for (x = 0; x < state->xsize; x++) { for (x = 0; x < state->xsize; x++) {
out[0] = ptr[x]; out[0] = ptr[x];
out[1] = ptr[(x+4*state->xsize)/2]; out[1] = ptr[(x+4*state->xsize)/2];
out[2] = ptr[(x+5*state->xsize)/2]; out[2] = ptr[(x+5*state->xsize)/2];
out += 3; out += 3;
} }
state->shuffle((UINT8*) im->image[state->y], state->shuffle((UINT8*) im->image[state->y],
state->buffer, state->xsize); state->buffer, state->xsize);
if (++state->y >= state->ysize) if (++state->y >= state->ysize)
return -1; /* This can hardly happen */ return -1; /* This can hardly happen */
/* Unpack second line */ /* Unpack second line */
out = state->buffer; out = state->buffer;
for (x = 0; x < state->xsize; x++) { for (x = 0; x < state->xsize; x++) {
out[0] = ptr[x+state->xsize]; out[0] = ptr[x+state->xsize];
out[1] = ptr[(x+4*state->xsize)/2]; out[1] = ptr[(x+4*state->xsize)/2];
out[2] = ptr[(x+5*state->xsize)/2]; out[2] = ptr[(x+5*state->xsize)/2];
out += 3; out += 3;
} }
state->shuffle((UINT8*) im->image[state->y], state->shuffle((UINT8*) im->image[state->y],
state->buffer, state->xsize); state->buffer, state->xsize);
if (++state->y >= state->ysize) if (++state->y >= state->ysize)
return -1; return -1;
ptr += chunk; ptr += chunk;
bytes -= chunk; bytes -= chunk;
} }
} }

View File

@ -5,7 +5,7 @@
* decoder for PCX image data. * decoder for PCX image data.
* *
* history: * history:
* 95-09-14 fl Created * 95-09-14 fl Created
* *
* Copyright (c) Fredrik Lundh 1995. * Copyright (c) Fredrik Lundh 1995.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -31,59 +31,59 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
for (;;) { for (;;) {
if (bytes < 1) if (bytes < 1)
return ptr - buf; return ptr - buf;
if ((*ptr & 0xC0) == 0xC0) { if ((*ptr & 0xC0) == 0xC0) {
/* Run */ /* Run */
if (bytes < 2) if (bytes < 2)
return ptr - buf; return ptr - buf;
n = ptr[0] & 0x3F; n = ptr[0] & 0x3F;
while (n > 0) { while (n > 0) {
if (state->x >= state->bytes) { if (state->x >= state->bytes) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
break; break;
} }
state->buffer[state->x++] = ptr[1]; state->buffer[state->x++] = ptr[1];
n--; n--;
} }
ptr += 2; bytes -= 2; ptr += 2; bytes -= 2;
} else { } else {
/* Literal */ /* Literal */
state->buffer[state->x++] = ptr[0]; state->buffer[state->x++] = ptr[0];
ptr++; bytes--; ptr++; bytes--;
} }
if (state->x >= state->bytes) { if (state->x >= state->bytes) {
if (state->bytes % state->xsize && state->bytes > state->xsize) { if (state->bytes % state->xsize && state->bytes > state->xsize) {
int bands = state->bytes / state->xsize; int bands = state->bytes / state->xsize;
int stride = state->bytes / bands; int stride = state->bytes / bands;
int i; int i;
for (i=1; i< bands; i++) { // note -- skipping first band for (i=1; i< bands; i++) { // note -- skipping first band
memmove(&state->buffer[i*state->xsize], memmove(&state->buffer[i*state->xsize],
&state->buffer[i*stride], &state->buffer[i*stride],
state->xsize); state->xsize);
}
}
/* Got a full line, unpack it */
state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->buffer,
state->xsize);
state->x = 0;
if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */
return -1;
} }
} }
/* Got a full line, unpack it */
state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->buffer,
state->xsize);
state->x = 0;
if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */
return -1;
}
}
} }
} }

View File

@ -139,7 +139,7 @@ ImagingPoint(Imaging imIn, const char* mode, const void* table)
void (*point)(Imaging imIn, Imaging imOut, im_point_context* context); void (*point)(Imaging imIn, Imaging imOut, im_point_context* context);
if (!imIn) if (!imIn)
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
if (!mode) if (!mode)
mode = imIn->mode; mode = imIn->mode;
@ -152,7 +152,7 @@ ImagingPoint(Imaging imIn, const char* mode, const void* table)
imOut = ImagingNew(mode, imIn->xsize, imIn->ysize); imOut = ImagingNew(mode, imIn->xsize, imIn->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
/* find appropriate handler */ /* find appropriate handler */
if (imIn->type == IMAGING_TYPE_UINT8) { if (imIn->type == IMAGING_TYPE_UINT8) {
@ -210,11 +210,11 @@ ImagingPointTransform(Imaging imIn, double scale, double offset)
if (!imIn || (strcmp(imIn->mode, "I") != 0 && if (!imIn || (strcmp(imIn->mode, "I") != 0 &&
strcmp(imIn->mode, "I;16") != 0 && strcmp(imIn->mode, "I;16") != 0 &&
strcmp(imIn->mode, "F") != 0)) strcmp(imIn->mode, "F") != 0))
return (Imaging) ImagingError_ModeError(); return (Imaging) ImagingError_ModeError();
imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize); imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
if (!imOut) if (!imOut)
return NULL; return NULL;
switch (imIn->type) { switch (imIn->type) {
case IMAGING_TYPE_INT32: case IMAGING_TYPE_INT32:
@ -254,7 +254,7 @@ ImagingPointTransform(Imaging imIn, double scale, double offset)
} }
ImagingSectionLeave(&cookie); ImagingSectionLeave(&cookie);
break; break;
} }
/* FALL THROUGH */ /* FALL THROUGH */
default: default:
ImagingDelete(imOut); ImagingDelete(imOut);

View File

@ -5,7 +5,7 @@
* decoder for raw (uncompressed) image data * decoder for raw (uncompressed) image data
* *
* history: * history:
* 96-03-07 fl rewritten * 96-03-07 fl rewritten
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -29,28 +29,28 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
if (state->state == 0) { if (state->state == 0) {
/* Initialize context variables */ /* Initialize context variables */
/* get size of image data and padding */ /* get size of image data and padding */
state->bytes = (state->xsize * state->bits + 7) / 8; state->bytes = (state->xsize * state->bits + 7) / 8;
if (rawstate->stride) { if (rawstate->stride) {
rawstate->skip = rawstate->stride - state->bytes; rawstate->skip = rawstate->stride - state->bytes;
if (rawstate->skip < 0) { if (rawstate->skip < 0) {
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
} }
} else { } else {
rawstate->skip = 0; rawstate->skip = 0;
} }
/* check image orientation */ /* check image orientation */
if (state->ystep < 0) { if (state->ystep < 0) {
state->y = state->ysize-1; state->y = state->ysize-1;
state->ystep = -1; state->ystep = -1;
} else } else
state->ystep = 1; state->ystep = 1;
state->state = LINE; state->state = LINE;
} }
@ -58,38 +58,38 @@ ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
for (;;) { for (;;) {
if (state->state == SKIP) { if (state->state == SKIP) {
/* Skip padding between lines */ /* Skip padding between lines */
if (bytes < rawstate->skip) if (bytes < rawstate->skip)
return ptr - buf; return ptr - buf;
ptr += rawstate->skip; ptr += rawstate->skip;
bytes -= rawstate->skip; bytes -= rawstate->skip;
state->state = LINE; state->state = LINE;
} }
if (bytes < state->bytes) if (bytes < state->bytes)
return ptr - buf; return ptr - buf;
/* Unpack data */ /* Unpack data */
state->shuffle((UINT8*) im->image[state->y + state->yoff] + state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, ptr, state->xsize); state->xoff * im->pixelsize, ptr, state->xsize);
ptr += state->bytes; ptr += state->bytes;
bytes -= state->bytes; bytes -= state->bytes;
state->y += state->ystep; state->y += state->ystep;
if (state->y < 0 || state->y >= state->ysize) { if (state->y < 0 || state->y >= state->ysize) {
/* End of file (errcode = 0) */ /* End of file (errcode = 0) */
return -1; return -1;
} }
state->state = SKIP; state->state = SKIP;
} }

View File

@ -9,8 +9,8 @@
* in ImageFile.py, but it should be solved here instead. * in ImageFile.py, but it should be solved here instead.
* *
* history: * history:
* 96-04-30 fl created * 96-04-30 fl created
* 97-01-03 fl fixed padding * 97-01-03 fl fixed padding
* *
* Copyright (c) Fredrik Lundh 1996-97. * Copyright (c) Fredrik Lundh 1996-97.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -27,60 +27,60 @@ ImagingRawEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
if (!state->state) { if (!state->state) {
/* The "count" field holds the stride, if specified. Fix /* The "count" field holds the stride, if specified. Fix
things up so "bytes" is the full size, and "count" the things up so "bytes" is the full size, and "count" the
packed size */ packed size */
if (state->count > 0) { if (state->count > 0) {
int bytes = state->count; int bytes = state->count;
/* stride must not be less than real size */ /* stride must not be less than real size */
if (state->count < state->bytes) { if (state->count < state->bytes) {
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return -1; return -1;
} }
state->count = state->bytes; state->count = state->bytes;
state->bytes = bytes; state->bytes = bytes;
} else } else
state->count = state->bytes; state->count = state->bytes;
/* The "ystep" field specifies the orientation */ /* The "ystep" field specifies the orientation */
if (state->ystep < 0) { if (state->ystep < 0) {
state->y = state->ysize-1; state->y = state->ysize-1;
state->ystep = -1; state->ystep = -1;
} else } else
state->ystep = 1; state->ystep = 1;
state->state = 1; state->state = 1;
} }
if (bytes < state->bytes) { if (bytes < state->bytes) {
state->errcode = IMAGING_CODEC_CONFIG; state->errcode = IMAGING_CODEC_CONFIG;
return 0; return 0;
} }
ptr = buf; ptr = buf;
while (bytes >= state->bytes) { while (bytes >= state->bytes) {
state->shuffle(ptr, (UINT8*) im->image[state->y + state->yoff] + state->shuffle(ptr, (UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize); state->xoff * im->pixelsize, state->xsize);
if (state->bytes > state->count) if (state->bytes > state->count)
/* zero-pad the buffer, if necessary */ /* zero-pad the buffer, if necessary */
memset(ptr + state->count, 0, state->bytes - state->count); memset(ptr + state->count, 0, state->bytes - state->count);
ptr += state->bytes; ptr += state->bytes;
bytes -= state->bytes; bytes -= state->bytes;
state->y += state->ystep; state->y += state->ystep;
if (state->y < 0 || state->y >= state->ysize) { if (state->y < 0 || state->y >= state->ysize) {
state->errcode = IMAGING_CODEC_END; state->errcode = IMAGING_CODEC_END;
break; break;
} }
} }

View File

@ -6,7 +6,7 @@
* *
* history: * history:
* 2017-07-28 mb fixed for images larger than 64KB * 2017-07-28 mb fixed for images larger than 64KB
* 2017-07-20 mb created * 2017-07-20 mb created
* *
* Copyright (c) Mickael Bonfill 2017. * Copyright (c) Mickael Bonfill 2017.
* *
@ -101,7 +101,7 @@ static int expandrow2(UINT8* dest, const UINT8* src, int n, int z, int xsize)
int int
ImagingSgiRleDecode(Imaging im, ImagingCodecState state, ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
UINT8* buf, Py_ssize_t bytes) UINT8* buf, Py_ssize_t bytes)
{ {
UINT8 *ptr; UINT8 *ptr;
SGISTATE *c; SGISTATE *c;

View File

@ -5,8 +5,8 @@
* decoder for Targa RLE data. * decoder for Targa RLE data.
* *
* history: * history:
* 97-01-04 fl created * 97-01-04 fl created
* 98-09-11 fl don't one byte per pixel; take orientation into account * 98-09-11 fl don't one byte per pixel; take orientation into account
* *
* Copyright (c) Fredrik Lundh 1997. * Copyright (c) Fredrik Lundh 1997.
* Copyright (c) Secret Labs AB 1997-98. * Copyright (c) Secret Labs AB 1997-98.
@ -20,7 +20,7 @@
int int
ImagingTgaRleDecode(Imaging im, ImagingCodecState state, ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
UINT8* buf, Py_ssize_t bytes) UINT8* buf, Py_ssize_t bytes)
{ {
int n, depth; int n, depth;
UINT8* ptr; UINT8* ptr;
@ -29,14 +29,14 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
if (state->state == 0) { if (state->state == 0) {
/* check image orientation */ /* check image orientation */
if (state->ystep < 0) { if (state->ystep < 0) {
state->y = state->ysize-1; state->y = state->ysize-1;
state->ystep = -1; state->ystep = -1;
} else } else
state->ystep = 1; state->ystep = 1;
state->state = 1; state->state = 1;
} }
@ -44,22 +44,22 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
for (;;) { for (;;) {
if (bytes < 1) if (bytes < 1)
return ptr - buf; return ptr - buf;
if (ptr[0] & 0x80) { if (ptr[0] & 0x80) {
/* Run (1 + pixelsize bytes) */ /* Run (1 + pixelsize bytes) */
if (bytes < 1 + depth) if (bytes < 1 + depth)
break; break;
n = depth * ((ptr[0] & 0x7f) + 1); n = depth * ((ptr[0] & 0x7f) + 1);
if (state->x + n > state->bytes) { if (state->x + n > state->bytes) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
if (depth == 1) if (depth == 1)
memset(state->buffer + state->x, ptr[1], n); memset(state->buffer + state->x, ptr[1], n);
@ -70,38 +70,38 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
} }
ptr += 1 + depth; ptr += 1 + depth;
bytes -= 1 + depth; bytes -= 1 + depth;
} else { } else {
/* Literal (1+n+1 bytes block) */ /* Literal (1+n+1 bytes block) */
n = depth * (ptr[0] + 1); n = depth * (ptr[0] + 1);
if (bytes < 1 + n) if (bytes < 1 + n)
break; break;
if (state->x + n > state->bytes) { if (state->x + n > state->bytes) {
state->errcode = IMAGING_CODEC_OVERRUN; state->errcode = IMAGING_CODEC_OVERRUN;
return -1; return -1;
} }
memcpy(state->buffer + state->x, ptr + 1, n); memcpy(state->buffer + state->x, ptr + 1, n);
ptr += 1 + n; ptr += 1 + n;
bytes -= 1 + n; bytes -= 1 + n;
} }
state->x += n; state->x += n;
if (state->x >= state->bytes) { if (state->x >= state->bytes) {
/* Got a full line, unpack it */ /* Got a full line, unpack it */
state->shuffle((UINT8*) im->image[state->y + state->yoff] + state->shuffle((UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->buffer, state->xoff * im->pixelsize, state->buffer,
state->xsize); state->xsize);
state->x = 0; state->x = 0;
state->y += state->ystep; state->y += state->ystep;
@ -110,7 +110,7 @@ ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
return -1; return -1;
} }
} }
} }

View File

@ -28,17 +28,17 @@
#define _PIL_LIBTIFF_ #define _PIL_LIBTIFF_
typedef struct { typedef struct {
tdata_t data; /* tdata_t == void* */ tdata_t data; /* tdata_t == void* */
toff_t loc; /* toff_t == uint32 */ toff_t loc; /* toff_t == uint32 */
tsize_t size; /* tsize_t == int32 */ tsize_t size; /* tsize_t == int32 */
int fp; int fp;
uint32 ifd; /* offset of the ifd, used for multipage uint32 ifd; /* offset of the ifd, used for multipage
* Should be uint32 for libtiff 3.9.x * Should be uint32 for libtiff 3.9.x
* uint64 for libtiff 4.0.x * uint64 for libtiff 4.0.x
*/ */
TIFF *tiff; /* Used in write */ TIFF *tiff; /* Used in write */
toff_t eof; toff_t eof;
int flrealloc;/* may we realloc */ int flrealloc;/* may we realloc */
} TIFFSTATE; } TIFFSTATE;
@ -55,7 +55,7 @@ extern int ImagingLibTiffSetField(ImagingCodecState state, ttag_t tag, ...);
*/ */
/* /*
#define VA_ARGS(...) __VA_ARGS__ #define VA_ARGS(...) __VA_ARGS__
#define TRACE(args) fprintf(stderr, VA_ARGS args) #define TRACE(args) fprintf(stderr, VA_ARGS args)
*/ */

View File

@ -998,7 +998,7 @@ unpackI16N_I16B(UINT8* out, const UINT8* in, int pixels){
UINT8* tmp = (UINT8*) out; UINT8* tmp = (UINT8*) out;
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
C16B; C16B;
in += 2; tmp += 2; in += 2; tmp += 2;
} }
} }
@ -1008,7 +1008,7 @@ unpackI16N_I16(UINT8* out, const UINT8* in, int pixels){
UINT8* tmp = (UINT8*) out; UINT8* tmp = (UINT8*) out;
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
C16L; C16L;
in += 2; tmp += 2; in += 2; tmp += 2;
} }
} }
@ -1053,7 +1053,7 @@ unpackI12_I16(UINT8* out, const UINT8* in, int pixels){
memcpy(out, &pixel, sizeof(pixel)); memcpy(out, &pixel, sizeof(pixel));
#endif #endif
in += 3; out+=2; in += 3; out+=2;
} }
if (i == pixels-1) { if (i == pixels-1) {
pixel = (((UINT16) in[0]) << 4 ) + (in[1] >>4); pixel = (((UINT16) in[0]) << 4 ) + (in[1] >>4);
@ -1306,7 +1306,7 @@ static struct {
/* greyscale w. alpha */ /* greyscale w. alpha */
{"LA", "LA", 16, unpackLA}, {"LA", "LA", 16, unpackLA},
{"LA", "LA;L", 16, unpackLAL}, {"LA", "LA;L", 16, unpackLAL},
/* greyscale w. alpha premultiplied */ /* greyscale w. alpha premultiplied */
{"La", "La", 16, unpackLA}, {"La", "La", 16, unpackLA},
@ -1447,16 +1447,16 @@ static struct {
{"YCbCr", "YCbCrK", 32, copy4}, {"YCbCr", "YCbCrK", 32, copy4},
/* LAB Color */ /* LAB Color */
{"LAB", "LAB", 24, ImagingUnpackLAB}, {"LAB", "LAB", 24, ImagingUnpackLAB},
{"LAB", "L", 8, band0}, {"LAB", "L", 8, band0},
{"LAB", "A", 8, band1}, {"LAB", "A", 8, band1},
{"LAB", "B", 8, band2}, {"LAB", "B", 8, band2},
/* HSV Color */ /* HSV Color */
{"HSV", "HSV", 24, ImagingUnpackRGB}, {"HSV", "HSV", 24, ImagingUnpackRGB},
{"HSV", "H", 8, band0}, {"HSV", "H", 8, band0},
{"HSV", "S", 8, band1}, {"HSV", "S", 8, band1},
{"HSV", "V", 8, band2}, {"HSV", "V", 8, band2},
/* integer variations */ /* integer variations */
{"I", "I", 32, copy4}, {"I", "I", 32, copy4},
@ -1505,11 +1505,11 @@ static struct {
{"I;16B", "I;16B", 16, copy2}, {"I;16B", "I;16B", 16, copy2},
{"I;16L", "I;16L", 16, copy2}, {"I;16L", "I;16L", 16, copy2},
{"I;16", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian. {"I;16", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
{"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian. {"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
{"I;16B", "I;16N", 16, unpackI16N_I16B}, {"I;16B", "I;16N", 16, unpackI16N_I16B},
{"I;16", "I;12", 12, unpackI12_I16}, // 12 bit Tiffs stored in 16bits. {"I;16", "I;12", 12, unpackI12_I16}, // 12 bit Tiffs stored in 16bits.
{NULL} /* sentinel */ {NULL} /* sentinel */
}; };

View File

@ -5,7 +5,7 @@
* code to convert and unpack PhotoYCC data * code to convert and unpack PhotoYCC data
* *
* history: * history:
* 97-01-25 fl Moved from PcdDecode.c * 97-01-25 fl Moved from PcdDecode.c
* *
* Copyright (c) Fredrik Lundh 1996-97. * Copyright (c) Fredrik Lundh 1996-97.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -116,12 +116,12 @@ static INT16 GR[] = { 127, 126, 125, 124, 123, 122, 121, 121, 120, 119,
-85, -86, -87, -88, -89, -90, -91, -92, -93, -94, -94, -95, -96, -97, -85, -86, -87, -88, -89, -90, -91, -92, -93, -94, -94, -95, -96, -97,
-98, -99, -100, -101, -102, -103, -104, -105, -106, -107, -107, -108 }; -98, -99, -100, -101, -102, -103, -104, -105, -106, -107, -107, -108 };
#define R 0 #define R 0
#define G 1 #define G 1
#define B 2 #define B 2
#define A 3 #define A 3
#define YCC2RGB(rgb, y, cb, cr) {\ #define YCC2RGB(rgb, y, cb, cr) {\
int l = L[y];\ int l = L[y];\
int r = l + CR[cr];\ int r = l + CR[cr];\
int g = l + GR[cr] + GB[cb];\ int g = l + GR[cr] + GB[cb];\
@ -137,9 +137,9 @@ ImagingUnpackYCC(UINT8* out, const UINT8* in, int pixels)
int i; int i;
/* PhotoYCC triplets */ /* PhotoYCC triplets */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
YCC2RGB(out, in[0], in[1], in[2]); YCC2RGB(out, in[0], in[1], in[2]);
out[A] = 255; out[A] = 255;
out += 4; in += 3; out += 4; in += 3;
} }
} }
@ -149,14 +149,14 @@ ImagingUnpackYCCA(UINT8* out, const UINT8* in, int pixels)
int i; int i;
/* PhotoYCC triplets plus premultiplied alpha */ /* PhotoYCC triplets plus premultiplied alpha */
for (i = 0; i < pixels; i++) { for (i = 0; i < pixels; i++) {
/* Divide by alpha */ /* Divide by alpha */
UINT8 rgb[3]; UINT8 rgb[3];
rgb[0] = (in[3] == 0) ? 0 : (((int) in[0] * 255) / in[3]); rgb[0] = (in[3] == 0) ? 0 : (((int) in[0] * 255) / in[3]);
rgb[1] = (in[3] == 0) ? 0 : (((int) in[1] * 255) / in[3]); rgb[1] = (in[3] == 0) ? 0 : (((int) in[1] * 255) / in[3]);
rgb[2] = (in[3] == 0) ? 0 : (((int) in[2] * 255) / in[3]); rgb[2] = (in[3] == 0) ? 0 : (((int) in[2] * 255) / in[3]);
/* Convert non-multiplied data to RGB */ /* Convert non-multiplied data to RGB */
YCC2RGB(out, rgb[0], rgb[1], rgb[2]); YCC2RGB(out, rgb[0], rgb[1], rgb[2]);
out[A] = in[3]; out[A] = in[3];
out += 4; in += 4; out += 4; in += 4;
} }
} }

View File

@ -5,7 +5,7 @@
* decoder for XBM hex image data * decoder for XBM hex image data
* *
* history: * history:
* 96-04-13 fl Created * 96-04-13 fl Created
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -16,9 +16,9 @@
#include "Imaging.h" #include "Imaging.h"
#define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\ #define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\
(v >= 'a' && v <= 'f') ? v - 'a' + 10 :\ (v >= 'a' && v <= 'f') ? v - 'a' + 10 :\
(v >= 'A' && v <= 'F') ? v - 'A' + 10 : 0) (v >= 'A' && v <= 'F') ? v - 'A' + 10 : 0)
int int
ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes) ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
@ -28,53 +28,53 @@ ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t byt
UINT8* ptr; UINT8* ptr;
if (!state->state) if (!state->state)
state->state = SKIP; state->state = SKIP;
ptr = buf; ptr = buf;
for (;;) { for (;;) {
if (state->state == SKIP) { if (state->state == SKIP) {
/* Skip forward until next 'x' */ /* Skip forward until next 'x' */
while (bytes > 0) { while (bytes > 0) {
if (*ptr == 'x') if (*ptr == 'x')
break; break;
ptr++; ptr++;
bytes--; bytes--;
} }
if (bytes == 0) if (bytes == 0)
return ptr - buf; return ptr - buf;
state->state = BYTE; state->state = BYTE;
} }
if (bytes < 3) if (bytes < 3)
return ptr - buf; return ptr - buf;
state->buffer[state->x] = (HEX(ptr[1])<<4) + HEX(ptr[2]); state->buffer[state->x] = (HEX(ptr[1])<<4) + HEX(ptr[2]);
if (++state->x >= state->bytes) { if (++state->x >= state->bytes) {
/* Got a full line, unpack it */ /* Got a full line, unpack it */
state->shuffle((UINT8*) im->image[state->y], state->buffer, state->shuffle((UINT8*) im->image[state->y], state->buffer,
state->xsize); state->xsize);
state->x = 0; state->x = 0;
if (++state->y >= state->ysize) { if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */ /* End of file (errcode = 0) */
return -1; return -1;
} }
} }
ptr += 3; ptr += 3;
bytes -= 3; bytes -= 3;
state->state = SKIP; state->state = SKIP;
} }

View File

@ -5,7 +5,7 @@
* encoder for Xbm data * encoder for Xbm data
* *
* history: * history:
* 96-11-01 fl created * 96-11-01 fl created
* *
* Copyright (c) Fredrik Lundh 1996. * Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997. * Copyright (c) Secret Labs AB 1997.
@ -27,79 +27,79 @@ ImagingXbmEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
if (!state->state) { if (!state->state) {
/* 8 pixels are stored in no more than 6 bytes */ /* 8 pixels are stored in no more than 6 bytes */
state->bytes = 6*(state->xsize+7)/8; state->bytes = 6*(state->xsize+7)/8;
state->state = 1; state->state = 1;
} }
if (bytes < state->bytes) { if (bytes < state->bytes) {
state->errcode = IMAGING_CODEC_MEMORY; state->errcode = IMAGING_CODEC_MEMORY;
return 0; return 0;
} }
ptr = buf; ptr = buf;
while (bytes >= state->bytes) { while (bytes >= state->bytes) {
state->shuffle(state->buffer, state->shuffle(state->buffer,
(UINT8*) im->image[state->y + state->yoff] + (UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize); state->xoff * im->pixelsize, state->xsize);
if (state->y < state->ysize-1) { if (state->y < state->ysize-1) {
/* any line but the last */ /* any line but the last */
for (n = 0; n < state->xsize; n += 8) { for (n = 0; n < state->xsize; n += 8) {
i = state->buffer[n/8]; i = state->buffer[n/8];
*ptr++ = '0'; *ptr++ = '0';
*ptr++ = 'x'; *ptr++ = 'x';
*ptr++ = hex[(i>>4)&15]; *ptr++ = hex[(i>>4)&15];
*ptr++ = hex[i&15]; *ptr++ = hex[i&15];
*ptr++ = ','; *ptr++ = ',';
bytes -= 5; bytes -= 5;
if (++state->count >= 79/5) { if (++state->count >= 79/5) {
*ptr++ = '\n'; *ptr++ = '\n';
bytes--; bytes--;
state->count = 0; state->count = 0;
} }
} }
state->y++; state->y++;
} else { } else {
/* last line */ /* last line */
for (n = 0; n < state->xsize; n += 8) { for (n = 0; n < state->xsize; n += 8) {
i = state->buffer[n/8]; i = state->buffer[n/8];
*ptr++ = '0'; *ptr++ = '0';
*ptr++ = 'x'; *ptr++ = 'x';
*ptr++ = hex[(i>>4)&15]; *ptr++ = hex[(i>>4)&15];
*ptr++ = hex[i&15]; *ptr++ = hex[i&15];
if (n < state->xsize-8) { if (n < state->xsize-8) {
*ptr++ = ','; *ptr++ = ',';
if (++state->count >= 79/5) { if (++state->count >= 79/5) {
*ptr++ = '\n'; *ptr++ = '\n';
bytes--; bytes--;
state->count = 0; state->count = 0;
} }
} else } else
*ptr++ = '\n'; *ptr++ = '\n';
bytes -= 5; bytes -= 5;
} }
state->errcode = IMAGING_CODEC_END; state->errcode = IMAGING_CODEC_END;
break; break;
} }
} }
return ptr - buf; return ptr - buf;

View File

@ -12,10 +12,10 @@
/* modes */ /* modes */
#define ZIP_PNG 0 /* continuous, filtered image data */ #define ZIP_PNG 0 /* continuous, filtered image data */
#define ZIP_PNG_PALETTE 1 /* non-continuous data, disable filtering */ #define ZIP_PNG_PALETTE 1 /* non-continuous data, disable filtering */
#define ZIP_TIFF_PREDICTOR 2 /* TIFF, with predictor */ #define ZIP_TIFF_PREDICTOR 2 /* TIFF, with predictor */
#define ZIP_TIFF 3 /* TIFF, without predictor */ #define ZIP_TIFF 3 /* TIFF, without predictor */
typedef struct { typedef struct {
@ -39,24 +39,24 @@ typedef struct {
/* PRIVATE CONTEXT (set by decoder/encoder) */ /* PRIVATE CONTEXT (set by decoder/encoder) */
z_stream z_stream; /* (de)compression stream */ z_stream z_stream; /* (de)compression stream */
UINT8* previous; /* previous line (allocated) */ UINT8* previous; /* previous line (allocated) */
int last_output; /* # bytes last output by inflate */ int last_output; /* # bytes last output by inflate */
/* Compressor specific stuff */ /* Compressor specific stuff */
UINT8* prior; /* filter storage (allocated) */ UINT8* prior; /* filter storage (allocated) */
UINT8* up; UINT8* up;
UINT8* average; UINT8* average;
UINT8* paeth; UINT8* paeth;
UINT8* output; /* output data */ UINT8* output; /* output data */
int prefix; /* size of filter prefix (0 for TIFF data) */ int prefix; /* size of filter prefix (0 for TIFF data) */
int interlaced; /* is the image interlaced? (PNG) */ int interlaced; /* is the image interlaced? (PNG) */
int pass; /* current pass of the interlaced image (PNG) */ int pass; /* current pass of the interlaced image (PNG) */
} ZIPSTATE; } ZIPSTATE;

View File

@ -23,7 +23,7 @@
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Class */ /* Class */
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
@ -44,7 +44,7 @@ _outline_new(void)
self = PyObject_New(OutlineObject, &OutlineType); self = PyObject_New(OutlineObject, &OutlineType);
if (self == NULL) if (self == NULL)
return NULL; return NULL;
self->outline = ImagingOutlineNew(); self->outline = ImagingOutlineNew();
@ -69,7 +69,7 @@ PyOutline_AsOutline(PyObject* outline)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Factories */ /* Factories */
PyObject* PyObject*
PyOutline_Create(PyObject* self, PyObject* args) PyOutline_Create(PyObject* self, PyObject* args)
@ -82,14 +82,14 @@ PyOutline_Create(PyObject* self, PyObject* args)
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Methods */ /* Methods */
static PyObject* static PyObject*
_outline_move(OutlineObject* self, PyObject* args) _outline_move(OutlineObject* self, PyObject* args)
{ {
float x0, y0; float x0, y0;
if (!PyArg_ParseTuple(args, "ff", &x0, &y0)) if (!PyArg_ParseTuple(args, "ff", &x0, &y0))
return NULL; return NULL;
ImagingOutlineMove(self->outline, x0, y0); ImagingOutlineMove(self->outline, x0, y0);
@ -102,7 +102,7 @@ _outline_line(OutlineObject* self, PyObject* args)
{ {
float x1, y1; float x1, y1;
if (!PyArg_ParseTuple(args, "ff", &x1, &y1)) if (!PyArg_ParseTuple(args, "ff", &x1, &y1))
return NULL; return NULL;
ImagingOutlineLine(self->outline, x1, y1); ImagingOutlineLine(self->outline, x1, y1);
@ -115,7 +115,7 @@ _outline_curve(OutlineObject* self, PyObject* args)
{ {
float x1, y1, x2, y2, x3, y3; float x1, y1, x2, y2, x3, y3;
if (!PyArg_ParseTuple(args, "ffffff", &x1, &y1, &x2, &y2, &x3, &y3)) if (!PyArg_ParseTuple(args, "ffffff", &x1, &y1, &x2, &y2, &x3, &y3))
return NULL; return NULL;
ImagingOutlineCurve(self->outline, x1, y1, x2, y2, x3, y3); ImagingOutlineCurve(self->outline, x1, y1, x2, y2, x3, y3);
@ -158,35 +158,35 @@ static struct PyMethodDef _outline_methods[] = {
}; };
static PyTypeObject OutlineType = { static PyTypeObject OutlineType = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"Outline", /*tp_name*/ "Outline", /*tp_name*/
sizeof(OutlineObject), /*tp_size*/ sizeof(OutlineObject), /*tp_size*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
/* methods */ /* methods */
(destructor)_outline_dealloc, /*tp_dealloc*/ (destructor)_outline_dealloc,/*tp_dealloc*/
0, /*tp_print*/ 0, /*tp_print*/
0, /*tp_getattr*/ 0, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
0, /*tp_compare*/ 0, /*tp_compare*/
0, /*tp_repr*/ 0, /*tp_repr*/
0, /*tp_as_number */ 0, /*tp_as_number */
0, /*tp_as_sequence */ 0, /*tp_as_sequence */
0, /*tp_as_mapping */ 0, /*tp_as_mapping */
0, /*tp_hash*/ 0, /*tp_hash*/
0, /*tp_call*/ 0, /*tp_call*/
0, /*tp_str*/ 0, /*tp_str*/
0, /*tp_getattro*/ 0, /*tp_getattro*/
0, /*tp_setattro*/ 0, /*tp_setattro*/
0, /*tp_as_buffer*/ 0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/ Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/ 0, /*tp_doc*/
0, /*tp_traverse*/ 0, /*tp_traverse*/
0, /*tp_clear*/ 0, /*tp_clear*/
0, /*tp_richcompare*/ 0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/ 0, /*tp_weaklistoffset*/
0, /*tp_iter*/ 0, /*tp_iter*/
0, /*tp_iternext*/ 0, /*tp_iternext*/
_outline_methods, /*tp_methods*/ _outline_methods, /*tp_methods*/
0, /*tp_members*/ 0, /*tp_members*/
0, /*tp_getset*/ 0, /*tp_getset*/
}; };