Pillow
92 строки · 2.0 Кб
1/*
2* The Python Imaging Library.
3* $Id$
4*
5* decoder for raw (uncompressed) image data
6*
7* history:
8* 96-03-07 fl rewritten
9*
10* Copyright (c) Fredrik Lundh 1996.
11* Copyright (c) Secret Labs AB 1997.
12*
13* See the README file for information on usage and redistribution.
14*/
15
16#include "Imaging.h"17
18#include "Raw.h"19
20int
21ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {22enum { LINE = 1, SKIP };23RAWSTATE *rawstate = state->context;24
25UINT8 *ptr;26
27if (state->state == 0) {28/* Initialize context variables */29
30/* get size of image data and padding */31state->bytes = (state->xsize * state->bits + 7) / 8;32if (rawstate->stride) {33rawstate->skip = rawstate->stride - state->bytes;34if (rawstate->skip < 0) {35state->errcode = IMAGING_CODEC_CONFIG;36return -1;37}38} else {39rawstate->skip = 0;40}41
42/* check image orientation */43if (state->ystep < 0) {44state->y = state->ysize - 1;45state->ystep = -1;46} else {47state->ystep = 1;48}49
50state->state = LINE;51}52
53ptr = buf;54
55for (;;) {56if (state->state == SKIP) {57/* Skip padding between lines */58
59if (bytes < rawstate->skip) {60return ptr - buf;61}62
63ptr += rawstate->skip;64bytes -= rawstate->skip;65
66state->state = LINE;67}68
69if (bytes < state->bytes) {70return ptr - buf;71}72
73/* Unpack data */74state->shuffle(75(UINT8 *)im->image[state->y + state->yoff] + state->xoff * im->pixelsize,76ptr,77state->xsize78);79
80ptr += state->bytes;81bytes -= state->bytes;82
83state->y += state->ystep;84
85if (state->y < 0 || state->y >= state->ysize) {86/* End of file (errcode = 0) */87return -1;88}89
90state->state = SKIP;91}92}
93