PcxEncode.c 5.6 KB

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