RawDecode.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. enum { LINE = 1, SKIP };
  20. RAWSTATE *rawstate = state->context;
  21. UINT8 *ptr;
  22. if (state->state == 0) {
  23. /* Initialize context variables */
  24. /* get size of image data and padding */
  25. state->bytes = (state->xsize * state->bits + 7) / 8;
  26. if (rawstate->stride) {
  27. rawstate->skip = rawstate->stride - state->bytes;
  28. if (rawstate->skip < 0) {
  29. state->errcode = IMAGING_CODEC_CONFIG;
  30. return -1;
  31. }
  32. } else {
  33. rawstate->skip = 0;
  34. }
  35. /* check image orientation */
  36. if (state->ystep < 0) {
  37. state->y = state->ysize - 1;
  38. state->ystep = -1;
  39. } else {
  40. state->ystep = 1;
  41. }
  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. }
  51. ptr += rawstate->skip;
  52. bytes -= rawstate->skip;
  53. state->state = LINE;
  54. }
  55. if (bytes < state->bytes) {
  56. return ptr - buf;
  57. }
  58. /* Unpack data */
  59. state->shuffle(
  60. (UINT8 *)im->image[state->y + state->yoff] + state->xoff * im->pixelsize,
  61. ptr,
  62. state->xsize);
  63. ptr += state->bytes;
  64. bytes -= state->bytes;
  65. state->y += state->ystep;
  66. if (state->y < 0 || state->y >= state->ysize) {
  67. /* End of file (errcode = 0) */
  68. return -1;
  69. }
  70. state->state = SKIP;
  71. }
  72. }