XbmEncode.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const char *hex = "0123456789abcdef";
  19. UINT8 *ptr = buf;
  20. int i, n;
  21. if (!state->state) {
  22. /* 8 pixels are stored in no more than 6 bytes */
  23. state->bytes = 6 * (state->xsize + 7) / 8;
  24. state->state = 1;
  25. }
  26. if (bytes < state->bytes) {
  27. state->errcode = IMAGING_CODEC_MEMORY;
  28. return 0;
  29. }
  30. ptr = buf;
  31. while (bytes >= state->bytes) {
  32. state->shuffle(
  33. state->buffer,
  34. (UINT8 *)im->image[state->y + state->yoff] + state->xoff * im->pixelsize,
  35. 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. }
  71. bytes -= 5;
  72. }
  73. state->errcode = IMAGING_CODEC_END;
  74. break;
  75. }
  76. }
  77. return ptr - buf;
  78. }