XbmDecode.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for XBM hex image data
  6. *
  7. * history:
  8. * 96-04-13 fl Created
  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. #define HEX(v) ((v >= '0' && v <= '9') ? v - '0' :\
  17. (v >= 'a' && v <= 'f') ? v - 'a' + 10 :\
  18. (v >= 'A' && v <= 'F') ? v - 'A' + 10 : 0)
  19. int
  20. ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
  21. {
  22. enum { BYTE = 1, SKIP };
  23. UINT8* ptr;
  24. if (!state->state)
  25. state->state = SKIP;
  26. ptr = buf;
  27. for (;;) {
  28. if (state->state == SKIP) {
  29. /* Skip forward until next 'x' */
  30. while (bytes > 0) {
  31. if (*ptr == 'x')
  32. break;
  33. ptr++;
  34. bytes--;
  35. }
  36. if (bytes == 0)
  37. return ptr - buf;
  38. state->state = BYTE;
  39. }
  40. if (bytes < 3)
  41. return ptr - buf;
  42. state->buffer[state->x] = (HEX(ptr[1])<<4) + HEX(ptr[2]);
  43. if (++state->x >= state->bytes) {
  44. /* Got a full line, unpack it */
  45. state->shuffle((UINT8*) im->image[state->y], state->buffer,
  46. state->xsize);
  47. state->x = 0;
  48. if (++state->y >= state->ysize) {
  49. /* End of file (errcode = 0) */
  50. return -1;
  51. }
  52. }
  53. ptr += 3;
  54. bytes -= 3;
  55. state->state = SKIP;
  56. }
  57. }