2010-07-31 06:52:47 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library.
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* decoder for raw (uncompressed) image data
|
|
|
|
*
|
|
|
|
* history:
|
2020-05-01 15:08:57 +03:00
|
|
|
* 96-03-07 fl rewritten
|
2010-07-31 06:52:47 +04:00
|
|
|
*
|
|
|
|
* Copyright (c) Fredrik Lundh 1996.
|
|
|
|
* Copyright (c) Secret Labs AB 1997.
|
|
|
|
*
|
|
|
|
* See the README file for information on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
#include "Raw.h"
|
|
|
|
|
|
|
|
int
|
2021-01-03 06:17:51 +03:00
|
|
|
ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
|
2010-07-31 06:52:47 +04:00
|
|
|
enum { LINE = 1, SKIP };
|
2021-01-03 06:17:51 +03:00
|
|
|
RAWSTATE *rawstate = state->context;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
UINT8 *ptr;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (state->state == 0) {
|
2020-05-01 15:08:57 +03:00
|
|
|
/* Initialize context variables */
|
|
|
|
|
|
|
|
/* get size of image data and padding */
|
|
|
|
state->bytes = (state->xsize * state->bits + 7) / 8;
|
|
|
|
if (rawstate->stride) {
|
|
|
|
rawstate->skip = rawstate->stride - state->bytes;
|
|
|
|
if (rawstate->skip < 0) {
|
|
|
|
state->errcode = IMAGING_CODEC_CONFIG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rawstate->skip = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check image orientation */
|
|
|
|
if (state->ystep < 0) {
|
2021-01-03 06:17:51 +03:00
|
|
|
state->y = state->ysize - 1;
|
2020-05-01 15:08:57 +03:00
|
|
|
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
|
|
|
}
|
2020-05-01 15:08:57 +03:00
|
|
|
|
|
|
|
state->state = LINE;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
|
|
|
|
for (;;) {
|
2020-05-01 15:08:57 +03:00
|
|
|
if (state->state == SKIP) {
|
|
|
|
/* Skip padding between lines */
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (bytes < rawstate->skip) {
|
2020-05-01 15:08:57 +03:00
|
|
|
return ptr - buf;
|
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 += rawstate->skip;
|
|
|
|
bytes -= rawstate->skip;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
state->state = LINE;
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (bytes < state->bytes) {
|
2020-05-01 15:08:57 +03:00
|
|
|
return ptr - buf;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
/* Unpack data */
|
2021-01-03 06:17:51 +03:00
|
|
|
state->shuffle(
|
|
|
|
(UINT8 *)im->image[state->y + state->yoff] + state->xoff * im->pixelsize,
|
|
|
|
ptr,
|
|
|
|
state->xsize);
|
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) {
|
|
|
|
/* End of file (errcode = 0) */
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
state->state = SKIP;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
}
|