XbmEncode.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * encoder for Xbm data
  6. *
  7. * history:
  8. * 96-11-01 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. int
  17. ImagingXbmEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
  18. {
  19. const char *hex = "0123456789abcdef";
  20. UINT8* ptr = buf;
  21. int i, n;
  22. if (!state->state) {
  23. /* 8 pixels are stored in no more than 6 bytes */
  24. state->bytes = 6*(state->xsize+7)/8;
  25. state->state = 1;
  26. }
  27. if (bytes < state->bytes) {
  28. state->errcode = IMAGING_CODEC_MEMORY;
  29. return 0;
  30. }
  31. ptr = buf;
  32. while (bytes >= state->bytes) {
  33. state->shuffle(state->buffer,
  34. (UINT8*) im->image[state->y + state->yoff] +
  35. state->xoff * im->pixelsize, state->xsize);
  36. if (state->y < state->ysize-1) {
  37. /* any line but the last */
  38. for (n = 0; n < state->xsize; n += 8) {
  39. i = state->buffer[n/8];
  40. *ptr++ = '0';
  41. *ptr++ = 'x';
  42. *ptr++ = hex[(i>>4)&15];
  43. *ptr++ = hex[i&15];
  44. *ptr++ = ',';
  45. bytes -= 5;
  46. if (++state->count >= 79/5) {
  47. *ptr++ = '\n';
  48. bytes--;
  49. state->count = 0;
  50. }
  51. }
  52. state->y++;
  53. } else {
  54. /* last line */
  55. for (n = 0; n < state->xsize; n += 8) {
  56. i = state->buffer[n/8];
  57. *ptr++ = '0';
  58. *ptr++ = 'x';
  59. *ptr++ = hex[(i>>4)&15];
  60. *ptr++ = hex[i&15];
  61. if (n < state->xsize-8) {
  62. *ptr++ = ',';
  63. if (++state->count >= 79/5) {
  64. *ptr++ = '\n';
  65. bytes--;
  66. state->count = 0;
  67. }
  68. } else
  69. *ptr++ = '\n';
  70. bytes -= 5;
  71. }
  72. state->errcode = IMAGING_CODEC_END;
  73. break;
  74. }
  75. }
  76. return ptr - buf;
  77. }