Pillow/libImaging/SgiRleDecode.c

169 lines
4.3 KiB
C
Raw Normal View History

2017-07-22 10:34:06 +03:00
/*
* The Python Imaging Library.
* $Id$
*
* decoder for Sgi RLE data.
*
* history:
2017-07-28 22:58:17 +03:00
* 2017-07-28 mb fixed for images larger than 64KB
2017-07-28 01:51:10 +03:00
* 2017-07-20 mb created
2017-07-22 10:34:06 +03:00
*
* Copyright (c) Mickael Bonfill 2017.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
2017-07-28 01:51:10 +03:00
#include "Sgi.h"
2017-07-22 10:34:06 +03:00
2017-07-28 01:51:10 +03:00
#define SGI_HEADER_SIZE 512
#define RLE_COPY_FLAG 0x80
#define RLE_MAX_RUN 0x7f
2017-07-28 01:51:10 +03:00
static void read4B(uint32_t* dest, UINT8* buf)
2017-07-22 10:34:06 +03:00
{
2017-07-28 01:51:10 +03:00
*dest = (uint32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
2017-07-22 10:34:06 +03:00
}
2017-07-28 22:58:17 +03:00
static int expandrow(UINT8* dest, UINT8* src, int n, int z)
2017-07-22 10:34:06 +03:00
{
2017-07-27 00:01:45 +03:00
UINT8 pixel, count;
2017-07-28 01:51:10 +03:00
for (;n > 0; n--)
{
pixel = *src++;
if (n == 1 && pixel != 0)
return n;
count = pixel & RLE_MAX_RUN;
if (!count)
return count;
if (pixel & RLE_COPY_FLAG) {
while(count--) {
2017-07-28 22:58:17 +03:00
*dest = *src++;
dest += z;
2017-07-28 01:51:10 +03:00
}
2017-07-27 00:01:45 +03:00
}
2017-07-28 01:51:10 +03:00
else {
pixel = *src++;
while (count--) {
2017-07-28 22:58:17 +03:00
*dest = pixel;
dest += z;
2017-07-28 01:51:10 +03:00
}
}
2017-07-27 00:01:45 +03:00
}
2017-07-28 01:51:10 +03:00
return 0;
2017-07-22 10:34:06 +03:00
}
2017-07-28 22:58:17 +03:00
static int expandrow2(UINT16* dest, UINT16* src, int n, int z)
2017-07-22 10:34:06 +03:00
{
2017-07-28 01:51:10 +03:00
UINT8 pixel, count;
2017-07-29 00:12:46 +03:00
2017-07-28 01:51:10 +03:00
for (;n > 0; n--)
{
2017-07-29 00:12:46 +03:00
pixel = ((UINT8*)src)[1];
2017-07-28 01:51:10 +03:00
++src;
if (n == 1 && pixel != 0)
return n;
count = pixel & RLE_MAX_RUN;
if (!count)
return count;
if (pixel & RLE_COPY_FLAG) {
while(count--) {
2017-07-28 22:58:17 +03:00
*dest = *src++;
dest += z;
2017-07-28 01:51:10 +03:00
}
}
else {
while (count--) {
2017-07-28 22:58:17 +03:00
*dest = *src;
dest += z;
2017-07-28 01:51:10 +03:00
}
++src;
}
}
2017-07-28 01:51:10 +03:00
return 0;
}
2017-07-22 10:34:06 +03:00
2017-07-28 22:58:17 +03:00
2017-07-28 01:51:10 +03:00
int
ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
UINT8* buf, int bytes)
{
UINT8 *ptr;
SGISTATE *c;
2017-07-22 10:34:06 +03:00
2017-07-29 00:12:46 +03:00
/* Get all data from File descriptor */
c = (SGISTATE*)state->context;
2017-07-28 22:58:17 +03:00
_imaging_seek_pyFd(state->fd, 0L, SEEK_END);
c->bufsize = _imaging_tell_pyFd(state->fd);
c->bufsize -= SGI_HEADER_SIZE;
ptr = malloc(sizeof(UINT8) * c->bufsize);
2017-07-28 22:58:17 +03:00
_imaging_seek_pyFd(state->fd, SGI_HEADER_SIZE, SEEK_SET);
_imaging_read_pyFd(state->fd, (char*)ptr, c->bufsize);
2017-07-28 22:58:17 +03:00
2017-07-29 00:12:46 +03:00
/* decoder initialization */
state->count = 0;
2017-07-28 22:58:17 +03:00
state->y = 0;
if (state->ystep < 0)
state->y = im->ysize - 1;
else
state->ystep = 1;
2017-07-22 10:34:06 +03:00
2017-07-29 00:12:46 +03:00
/* Allocate memory for RLE tables and rows */
free(state->buffer);
state->buffer = malloc(sizeof(UINT8) * 2 * im->xsize * im->bands);
c->tablen = im->bands * im->ysize;
c->starttab = calloc(c->tablen, sizeof(uint32_t));
c->lengthtab = calloc(c->tablen, sizeof(uint32_t));
2017-07-22 10:34:06 +03:00
2017-07-29 00:12:46 +03:00
/* populate offsets table */
for (c->tabindex = 0, c->bufindex = 0; c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4)
read4B(&c->starttab[c->tabindex], &ptr[c->bufindex]);
2017-07-29 00:12:46 +03:00
/* populate lengths table */
for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(uint32_t); c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4)
read4B(&c->lengthtab[c->tabindex], &ptr[c->bufindex]);
2017-07-28 22:58:17 +03:00
state->count += c->tablen * sizeof(uint32_t) * 2;
2017-07-28 22:58:17 +03:00
2017-07-29 00:12:46 +03:00
/* read compressed rows */
for (c->rowno = 0; c->rowno < im->ysize; c->rowno++, state->y += state->ystep)
2017-07-28 01:51:10 +03:00
{
for (c->channo = 0; c->channo < im->bands; c->channo++)
2017-07-28 22:58:17 +03:00
{
c->rleoffset = c->starttab[c->rowno + c->channo * im->ysize];
c->rlelength = c->lengthtab[c->rowno + c->channo * im->ysize];
c->rleoffset -= SGI_HEADER_SIZE;
2017-07-28 22:58:17 +03:00
2017-07-29 00:12:46 +03:00
/* row decompression */
if (c->bpc ==1) {
if(expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands))
2017-07-28 22:58:17 +03:00
goto sgi_finish_decode;
2017-07-28 01:51:10 +03:00
}
2017-07-28 22:58:17 +03:00
else {
if(expandrow2((UINT16*)&state->buffer[c->channo * 2], (UINT16*)&ptr[c->rleoffset], c->rlelength, im->bands))
2017-07-28 22:58:17 +03:00
goto sgi_finish_decode;
2017-07-28 01:51:10 +03:00
}
2017-07-28 22:58:17 +03:00
state->count += c->rlelength;
2017-07-28 22:58:17 +03:00
}
2017-07-29 00:12:46 +03:00
/* store decompressed data in image */
2017-07-28 22:58:17 +03:00
state->shuffle((UINT8*)im->image[state->y], state->buffer, im->xsize);
2017-07-28 01:51:10 +03:00
}
2017-07-22 10:34:06 +03:00
c->bufsize++;
2017-07-22 10:34:06 +03:00
sgi_finish_decode: ;
2017-07-28 22:58:17 +03:00
free(c->starttab);
free(c->lengthtab);
2017-07-28 22:58:17 +03:00
free(ptr);
return state->count - c->bufsize;
2017-07-28 22:58:17 +03:00
}