EpsEncode.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * encoder for EPS hex data
  6. *
  7. * history:
  8. * 96-04-19 fl created
  9. * 96-06-27 fl don't drop last block of encoded data
  10. *
  11. * notes:
  12. * FIXME: rename to HexEncode.c ??
  13. *
  14. * Copyright (c) Fredrik Lundh 1996.
  15. * Copyright (c) Secret Labs AB 1997.
  16. *
  17. * See the README file for information on usage and redistribution.
  18. */
  19. #include "Imaging.h"
  20. int
  21. ImagingEpsEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
  22. {
  23. enum { HEXBYTE=1, NEWLINE };
  24. const char *hex = "0123456789abcdef";
  25. UINT8* ptr = buf;
  26. UINT8* in, i;
  27. if (!state->state) {
  28. state->state = HEXBYTE;
  29. state->xsize *= im->pixelsize; /* Hack! */
  30. }
  31. in = (UINT8*) im->image[state->y];
  32. for (;;) {
  33. if (state->state == NEWLINE) {
  34. if (bytes < 1)
  35. break;
  36. *ptr++ = '\n';
  37. bytes--;
  38. state->state = HEXBYTE;
  39. }
  40. if (bytes < 2)
  41. break;
  42. i = in[state->x++];
  43. *ptr++ = hex[(i>>4)&15];
  44. *ptr++ = hex[i&15];
  45. bytes -= 2;
  46. /* Skip junk bytes */
  47. if (im->bands == 3 && (state->x & 3) == 3)
  48. state->x++;
  49. if (++state->count >= 79/2) {
  50. state->state = NEWLINE;
  51. state->count = 0;
  52. }
  53. if (state->x >= state->xsize) {
  54. state->x = 0;
  55. if (++state->y >= state->ysize) {
  56. state->errcode = IMAGING_CODEC_END;
  57. break;
  58. }
  59. in = (UINT8*) im->image[state->y];
  60. }
  61. }
  62. return ptr - buf;
  63. }