SgiRleDecode.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for Sgi RLE data.
  6. *
  7. * history:
  8. * 2017-07-28 mb fixed for images larger than 64KB
  9. * 2017-07-20 mb created
  10. *
  11. * Copyright (c) Mickael Bonfill 2017.
  12. *
  13. * See the README file for information on usage and redistribution.
  14. */
  15. #include "Imaging.h"
  16. #include "Sgi.h"
  17. #define SGI_HEADER_SIZE 512
  18. #define RLE_COPY_FLAG 0x80
  19. #define RLE_MAX_RUN 0x7f
  20. static void read4B(UINT32* dest, UINT8* buf)
  21. {
  22. *dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
  23. }
  24. static int expandrow(UINT8* dest, UINT8* src, int n, int z, int xsize)
  25. {
  26. UINT8 pixel, count;
  27. for (;n > 0; n--)
  28. {
  29. pixel = *src++;
  30. if (n == 1 && pixel != 0)
  31. return n;
  32. count = pixel & RLE_MAX_RUN;
  33. if (!count)
  34. return count;
  35. if (count > xsize) {
  36. return -1;
  37. }
  38. if (pixel & RLE_COPY_FLAG) {
  39. while(count--) {
  40. *dest = *src++;
  41. dest += z;
  42. }
  43. }
  44. else {
  45. pixel = *src++;
  46. while (count--) {
  47. *dest = pixel;
  48. dest += z;
  49. }
  50. }
  51. }
  52. return 0;
  53. }
  54. static int expandrow2(UINT8* dest, const UINT8* src, int n, int z, int xsize)
  55. {
  56. UINT8 pixel, count;
  57. for (;n > 0; n--)
  58. {
  59. pixel = src[1];
  60. src+=2;
  61. if (n == 1 && pixel != 0)
  62. return n;
  63. count = pixel & RLE_MAX_RUN;
  64. if (!count)
  65. return count;
  66. if (count > xsize) {
  67. return -1;
  68. }
  69. if (pixel & RLE_COPY_FLAG) {
  70. while(count--) {
  71. memcpy(dest, src, 2);
  72. src += 2;
  73. dest += z * 2;
  74. }
  75. }
  76. else {
  77. while (count--) {
  78. memcpy(dest, src, 2);
  79. dest += z * 2;
  80. }
  81. src+=2;
  82. }
  83. }
  84. return 0;
  85. }
  86. int
  87. ImagingSgiRleDecode(Imaging im, ImagingCodecState state,
  88. UINT8* buf, Py_ssize_t bytes)
  89. {
  90. UINT8 *ptr;
  91. SGISTATE *c;
  92. int err = 0;
  93. int status;
  94. /* Get all data from File descriptor */
  95. c = (SGISTATE*)state->context;
  96. _imaging_seek_pyFd(state->fd, 0L, SEEK_END);
  97. c->bufsize = _imaging_tell_pyFd(state->fd);
  98. c->bufsize -= SGI_HEADER_SIZE;
  99. ptr = malloc(sizeof(UINT8) * c->bufsize);
  100. if (!ptr) {
  101. return IMAGING_CODEC_MEMORY;
  102. }
  103. _imaging_seek_pyFd(state->fd, SGI_HEADER_SIZE, SEEK_SET);
  104. _imaging_read_pyFd(state->fd, (char*)ptr, c->bufsize);
  105. /* decoder initialization */
  106. state->count = 0;
  107. state->y = 0;
  108. if (state->ystep < 0) {
  109. state->y = im->ysize - 1;
  110. } else {
  111. state->ystep = 1;
  112. }
  113. if (im->xsize > INT_MAX / im->bands ||
  114. im->ysize > INT_MAX / im->bands) {
  115. err = IMAGING_CODEC_MEMORY;
  116. goto sgi_finish_decode;
  117. }
  118. /* Allocate memory for RLE tables and rows */
  119. free(state->buffer);
  120. state->buffer = NULL;
  121. /* malloc overflow check above */
  122. state->buffer = calloc(im->xsize * im->bands, sizeof(UINT8) * 2);
  123. c->tablen = im->bands * im->ysize;
  124. c->starttab = calloc(c->tablen, sizeof(UINT32));
  125. c->lengthtab = calloc(c->tablen, sizeof(UINT32));
  126. if (!state->buffer ||
  127. !c->starttab ||
  128. !c->lengthtab) {
  129. err = IMAGING_CODEC_MEMORY;
  130. goto sgi_finish_decode;
  131. }
  132. /* populate offsets table */
  133. for (c->tabindex = 0, c->bufindex = 0; c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4)
  134. read4B(&c->starttab[c->tabindex], &ptr[c->bufindex]);
  135. /* populate lengths table */
  136. for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(UINT32); c->tabindex < c->tablen; c->tabindex++, c->bufindex+=4)
  137. read4B(&c->lengthtab[c->tabindex], &ptr[c->bufindex]);
  138. state->count += c->tablen * sizeof(UINT32) * 2;
  139. /* read compressed rows */
  140. for (c->rowno = 0; c->rowno < im->ysize; c->rowno++, state->y += state->ystep)
  141. {
  142. for (c->channo = 0; c->channo < im->bands; c->channo++)
  143. {
  144. c->rleoffset = c->starttab[c->rowno + c->channo * im->ysize];
  145. c->rlelength = c->lengthtab[c->rowno + c->channo * im->ysize];
  146. c->rleoffset -= SGI_HEADER_SIZE;
  147. if (c->rleoffset + c->rlelength > c->bufsize) {
  148. state->errcode = IMAGING_CODEC_OVERRUN;
  149. return -1;
  150. }
  151. /* row decompression */
  152. if (c->bpc ==1) {
  153. status = expandrow(&state->buffer[c->channo], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
  154. }
  155. else {
  156. status = expandrow2(&state->buffer[c->channo * 2], &ptr[c->rleoffset], c->rlelength, im->bands, im->xsize);
  157. }
  158. if (status == -1) {
  159. state->errcode = IMAGING_CODEC_OVERRUN;
  160. return -1;
  161. } else if (status == 1) {
  162. goto sgi_finish_decode;
  163. }
  164. state->count += c->rlelength;
  165. }
  166. /* store decompressed data in image */
  167. state->shuffle((UINT8*)im->image[state->y], state->buffer, im->xsize);
  168. }
  169. c->bufsize++;
  170. sgi_finish_decode: ;
  171. free(c->starttab);
  172. free(c->lengthtab);
  173. free(ptr);
  174. if (err != 0){
  175. return err;
  176. }
  177. return state->count - c->bufsize;
  178. }