Pillow/src/libImaging/RawEncode.c

93 lines
2.2 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library.
* $Id$
*
* coder for raw data
*
* FIXME: This encoder will fail if the buffer is not large enough to
* hold one full line of data. There's a workaround for this problem
* in ImageFile.py, but it should be solved here instead.
*
* history:
2020-05-01 15:08:57 +03:00
* 96-04-30 fl created
* 97-01-03 fl fixed padding
2010-07-31 06:52:47 +04:00
*
* Copyright (c) Fredrik Lundh 1996-97.
* Copyright (c) Secret Labs AB 1997.
*
* See the README file for information on usage and redistribution. */
#include "Imaging.h"
int
ImagingRawEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
{
UINT8* ptr;
if (!state->state) {
2020-05-01 15:08:57 +03:00
/* The "count" field holds the stride, if specified. Fix
things up so "bytes" is the full size, and "count" the
packed size */
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (state->count > 0) {
int bytes = state->count;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* stride must not be less than real size */
if (state->count < state->bytes) {
state->errcode = IMAGING_CODEC_CONFIG;
return -1;
}
state->count = state->bytes;
state->bytes = bytes;
2020-05-11 00:46:12 +03:00
} else {
2020-05-01 15:08:57 +03:00
state->count = state->bytes;
2020-05-11 00:46:12 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* The "ystep" field specifies the orientation */
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (state->ystep < 0) {
state->y = state->ysize-1;
state->ystep = -1;
2020-05-10 12:56:36 +03:00
} else {
2020-05-01 15:08:57 +03:00
state->ystep = 1;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
state->state = 1;
2010-07-31 06:52:47 +04:00
}
if (bytes < state->bytes) {
2020-05-01 15:08:57 +03:00
state->errcode = IMAGING_CODEC_CONFIG;
return 0;
2010-07-31 06:52:47 +04:00
}
ptr = buf;
while (bytes >= state->bytes) {
2020-05-01 15:08:57 +03:00
state->shuffle(ptr, (UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize);
2010-07-31 06:52:47 +04:00
2020-05-10 12:56:36 +03:00
if (state->bytes > state->count) {
2020-05-01 15:08:57 +03:00
/* zero-pad the buffer, if necessary */
memset(ptr + state->count, 0, state->bytes - state->count);
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
ptr += state->bytes;
bytes -= state->bytes;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
state->y += state->ystep;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (state->y < 0 || state->y >= state->ysize) {
state->errcode = IMAGING_CODEC_END;
break;
}
2010-07-31 06:52:47 +04:00
}
return ptr - buf;
}