RawDecode.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "Imaging.h"
  16. #include "Raw.h"
  17. int
  18. ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
  19. {
  20. enum { LINE = 1, SKIP };
  21. RAWSTATE* rawstate = state->context;
  22. UINT8* ptr;
  23. if (state->state == 0) {
  24. /* Initialize context variables */
  25. /* get size of image data and padding */
  26. state->bytes = (state->xsize * state->bits + 7) / 8;
  27. if (rawstate->stride) {
  28. rawstate->skip = rawstate->stride - state->bytes;
  29. if (rawstate->skip < 0) {
  30. state->errcode = IMAGING_CODEC_CONFIG;
  31. return -1;
  32. }
  33. } else {
  34. rawstate->skip = 0;
  35. }
  36. /* check image orientation */
  37. if (state->ystep < 0) {
  38. state->y = state->ysize-1;
  39. state->ystep = -1;
  40. } else
  41. state->ystep = 1;
  42. state->state = LINE;
  43. }
  44. ptr = buf;
  45. for (;;) {
  46. if (state->state == SKIP) {
  47. /* Skip padding between lines */
  48. if (bytes < rawstate->skip)
  49. return ptr - buf;
  50. ptr += rawstate->skip;
  51. bytes -= rawstate->skip;
  52. state->state = LINE;
  53. }
  54. if (bytes < state->bytes)
  55. return ptr - buf;
  56. /* Unpack data */
  57. state->shuffle((UINT8*) im->image[state->y + state->yoff] +
  58. state->xoff * im->pixelsize, ptr, state->xsize);
  59. ptr += state->bytes;
  60. bytes -= state->bytes;
  61. state->y += state->ystep;
  62. if (state->y < 0 || state->y >= state->ysize) {
  63. /* End of file (errcode = 0) */
  64. return -1;
  65. }
  66. state->state = SKIP;
  67. }
  68. }