PcxEncode.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * encoder for PCX data
  6. *
  7. * history:
  8. * 99-02-07 fl created
  9. *
  10. * Copyright (c) Fredrik Lundh 1999.
  11. * Copyright (c) Secret Labs AB 1999.
  12. *
  13. * See the README file for information on usage and redistribution.
  14. */
  15. #include "Imaging.h"
  16. enum { INIT, FETCH, ENCODE };
  17. /* we're reusing "ystep" to store the last value */
  18. #define LAST ystep
  19. int
  20. ImagingPcxEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
  21. UINT8 *ptr;
  22. int this;
  23. int bytes_per_line = 0;
  24. int padding = 0;
  25. int stride = 0;
  26. int bpp = 0;
  27. int planes = 1;
  28. int i;
  29. ptr = buf;
  30. if (!state->state) {
  31. /* sanity check */
  32. if (state->xsize <= 0 || state->ysize <= 0) {
  33. state->errcode = IMAGING_CODEC_END;
  34. return 0;
  35. }
  36. state->state = FETCH;
  37. }
  38. bpp = state->bits;
  39. if (state->bits == 24) {
  40. planes = 3;
  41. bpp = 8;
  42. }
  43. bytes_per_line = (state->xsize * bpp + 7) / 8;
  44. /* The stride here needs to be kept in sync with the version in
  45. PcxImagePlugin.py. If it's not, the header and the body of the
  46. image will be out of sync and bad things will happen on decode.
  47. */
  48. stride = bytes_per_line + (bytes_per_line % 2);
  49. padding = stride - bytes_per_line;
  50. for (;;) {
  51. switch (state->state) {
  52. case FETCH:
  53. /* get a line of data */
  54. if (state->y >= state->ysize) {
  55. state->errcode = IMAGING_CODEC_END;
  56. return ptr - buf;
  57. }
  58. state->shuffle(
  59. state->buffer,
  60. (UINT8 *)im->image[state->y + state->yoff] +
  61. state->xoff * im->pixelsize,
  62. state->xsize);
  63. state->y += 1;
  64. state->count = 1;
  65. state->LAST = state->buffer[0];
  66. state->x = 1;
  67. state->state = ENCODE;
  68. /* fall through */
  69. case ENCODE:
  70. /* compress this line */
  71. /* when we arrive here, "count" contains the number of
  72. bytes having the value of "LAST" that we've already
  73. seen */
  74. do {
  75. /* If we're encoding an odd width file, and we've
  76. got more than one plane, we need to pad each
  77. color row with padding bytes at the end. Since
  78. The pixels are stored RRRRRGGGGGBBBBB, so we need
  79. to have the padding be RRRRRPGGGGGPBBBBBP. Hence
  80. the double loop
  81. */
  82. while (state->x % bytes_per_line) {
  83. if (state->count == 63) {
  84. /* this run is full; flush it */
  85. if (bytes < 2) {
  86. return ptr - buf;
  87. }
  88. ptr[0] = 0xff;
  89. ptr[1] = state->LAST;
  90. ptr += 2;
  91. bytes -= 2;
  92. state->count = 0;
  93. }
  94. this = state->buffer[state->x];
  95. if (this == state->LAST) {
  96. /* extend the current run */
  97. state->x += 1;
  98. state->count += 1;
  99. } else {
  100. /* start a new run */
  101. if (state->count == 1 && (state->LAST < 0xc0)) {
  102. if (bytes < 1) {
  103. return ptr - buf;
  104. }
  105. ptr[0] = state->LAST;
  106. ptr += 1;
  107. bytes -= 1;
  108. } else {
  109. if (state->count > 0) {
  110. if (bytes < 2) {
  111. return ptr - buf;
  112. }
  113. ptr[0] = 0xc0 | state->count;
  114. ptr[1] = state->LAST;
  115. ptr += 2;
  116. bytes -= 2;
  117. }
  118. }
  119. state->LAST = this;
  120. state->count = 1;
  121. state->x += 1;
  122. }
  123. }
  124. /* end of line; flush the current run */
  125. if (state->count == 1 && (state->LAST < 0xc0)) {
  126. if (bytes < 1 + padding) {
  127. return ptr - buf;
  128. }
  129. ptr[0] = state->LAST;
  130. ptr += 1;
  131. bytes -= 1;
  132. } else {
  133. if (state->count > 0) {
  134. if (bytes < 2 + padding) {
  135. return ptr - buf;
  136. }
  137. ptr[0] = 0xc0 | state->count;
  138. ptr[1] = state->LAST;
  139. ptr += 2;
  140. bytes -= 2;
  141. }
  142. }
  143. /* add the padding */
  144. for (i = 0; i < padding; i++) {
  145. ptr[0] = 0;
  146. ptr += 1;
  147. bytes -= 1;
  148. }
  149. /* reset for the next color plane. */
  150. if (state->x < planes * bytes_per_line) {
  151. state->count = 1;
  152. state->LAST = state->buffer[state->x];
  153. state->x += 1;
  154. }
  155. } while (state->x < planes * bytes_per_line);
  156. /* read next line */
  157. state->state = FETCH;
  158. break;
  159. }
  160. }
  161. }