RawEncode.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * coder for raw data
  6. *
  7. * FIXME: This encoder will fail if the buffer is not large enough to
  8. * hold one full line of data. There's a workaround for this problem
  9. * in ImageFile.py, but it should be solved here instead.
  10. *
  11. * history:
  12. * 96-04-30 fl created
  13. * 97-01-03 fl fixed padding
  14. *
  15. * Copyright (c) Fredrik Lundh 1996-97.
  16. * Copyright (c) Secret Labs AB 1997.
  17. *
  18. * See the README file for information on usage and redistribution. */
  19. #include "Imaging.h"
  20. int
  21. ImagingRawEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
  22. {
  23. UINT8* ptr;
  24. if (!state->state) {
  25. /* The "count" field holds the stride, if specified. Fix
  26. things up so "bytes" is the full size, and "count" the
  27. packed size */
  28. if (state->count > 0) {
  29. int bytes = state->count;
  30. /* stride must not be less than real size */
  31. if (state->count < state->bytes) {
  32. state->errcode = IMAGING_CODEC_CONFIG;
  33. return -1;
  34. }
  35. state->count = state->bytes;
  36. state->bytes = bytes;
  37. } else
  38. state->count = state->bytes;
  39. /* The "ystep" field specifies the orientation */
  40. if (state->ystep < 0) {
  41. state->y = state->ysize-1;
  42. state->ystep = -1;
  43. } else
  44. state->ystep = 1;
  45. state->state = 1;
  46. }
  47. if (bytes < state->bytes) {
  48. state->errcode = IMAGING_CODEC_CONFIG;
  49. return 0;
  50. }
  51. ptr = buf;
  52. while (bytes >= state->bytes) {
  53. state->shuffle(ptr, (UINT8*) im->image[state->y + state->yoff] +
  54. state->xoff * im->pixelsize, state->xsize);
  55. if (state->bytes > state->count)
  56. /* zero-pad the buffer, if necessary */
  57. memset(ptr + state->count, 0, state->bytes - state->count);
  58. ptr += state->bytes;
  59. bytes -= state->bytes;
  60. state->y += state->ystep;
  61. if (state->y < 0 || state->y >= state->ysize) {
  62. state->errcode = IMAGING_CODEC_END;
  63. break;
  64. }
  65. }
  66. return ptr - buf;
  67. }