RawEncode.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. UINT8 *ptr;
  23. if (!state->state) {
  24. /* The "count" field holds the stride, if specified. Fix
  25. things up so "bytes" is the full size, and "count" the
  26. packed size */
  27. if (state->count > 0) {
  28. int bytes = state->count;
  29. /* stride must not be less than real size */
  30. if (state->count < state->bytes) {
  31. state->errcode = IMAGING_CODEC_CONFIG;
  32. return -1;
  33. }
  34. state->count = state->bytes;
  35. state->bytes = bytes;
  36. } else {
  37. state->count = state->bytes;
  38. }
  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. }
  46. state->state = 1;
  47. }
  48. if (bytes < state->bytes) {
  49. state->errcode = IMAGING_CODEC_CONFIG;
  50. return 0;
  51. }
  52. ptr = buf;
  53. while (bytes >= state->bytes) {
  54. state->shuffle(
  55. ptr,
  56. (UINT8 *)im->image[state->y + state->yoff] + state->xoff * im->pixelsize,
  57. state->xsize);
  58. if (state->bytes > state->count) {
  59. /* zero-pad the buffer, if necessary */
  60. memset(ptr + state->count, 0, state->bytes - state->count);
  61. }
  62. ptr += state->bytes;
  63. bytes -= state->bytes;
  64. state->y += state->ystep;
  65. if (state->y < 0 || state->y >= state->ysize) {
  66. state->errcode = IMAGING_CODEC_END;
  67. break;
  68. }
  69. }
  70. return ptr - buf;
  71. }