TgaRleEncode.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "Imaging.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. static int
  5. comparePixels(const UINT8 *buf, int x, int bytesPerPixel) {
  6. buf += x * bytesPerPixel;
  7. return memcmp(buf, buf + bytesPerPixel, bytesPerPixel) == 0;
  8. }
  9. int
  10. ImagingTgaRleEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
  11. UINT8 *dst;
  12. int bytesPerPixel;
  13. if (state->state == 0) {
  14. if (state->ystep < 0) {
  15. state->ystep = -1;
  16. state->y = state->ysize - 1;
  17. } else {
  18. state->ystep = 1;
  19. }
  20. state->state = 1;
  21. }
  22. dst = buf;
  23. bytesPerPixel = (state->bits + 7) / 8;
  24. while (1) {
  25. int flushCount;
  26. /*
  27. * state->count is the numbers of bytes in the packet,
  28. * excluding the 1-byte descriptor.
  29. */
  30. if (state->count == 0) {
  31. UINT8 *row;
  32. UINT8 descriptor;
  33. int startX;
  34. assert(state->x <= state->xsize);
  35. /* Make sure we have space for the descriptor. */
  36. if (bytes < 1) {
  37. break;
  38. }
  39. if (state->x == state->xsize) {
  40. state->x = 0;
  41. state->y += state->ystep;
  42. if (state->y < 0 || state->y >= state->ysize) {
  43. state->errcode = IMAGING_CODEC_END;
  44. break;
  45. }
  46. }
  47. if (state->x == 0) {
  48. state->shuffle(
  49. state->buffer,
  50. (UINT8 *)im->image[state->y + state->yoff] +
  51. state->xoff * im->pixelsize,
  52. state->xsize);
  53. }
  54. row = state->buffer;
  55. /* Start with a raw packet for 1 px. */
  56. descriptor = 0;
  57. startX = state->x;
  58. state->count = bytesPerPixel;
  59. if (state->x + 1 < state->xsize) {
  60. int maxLookup;
  61. int isRaw;
  62. isRaw = !comparePixels(row, state->x, bytesPerPixel);
  63. ++state->x;
  64. /*
  65. * A packet can contain up to 128 pixels;
  66. * 2 are already behind (state->x points to
  67. * the second one).
  68. */
  69. maxLookup = state->x + 126;
  70. /* A packet must not span multiple rows. */
  71. if (maxLookup > state->xsize - 1) {
  72. maxLookup = state->xsize - 1;
  73. }
  74. if (isRaw) {
  75. while (state->x < maxLookup) {
  76. if (!comparePixels(row, state->x, bytesPerPixel)) {
  77. ++state->x;
  78. } else {
  79. /* Two identical pixels will go to RLE packet. */
  80. --state->x;
  81. break;
  82. }
  83. }
  84. state->count += (state->x - startX) * bytesPerPixel;
  85. } else {
  86. descriptor |= 0x80;
  87. while (state->x < maxLookup) {
  88. if (comparePixels(row, state->x, bytesPerPixel)) {
  89. ++state->x;
  90. } else {
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. /*
  97. * state->x currently points to the last pixel to be
  98. * included in the packet. The pixel count in the
  99. * descriptor is 1 less than actual number of pixels in
  100. * the packet, that is, state->x == startX if we encode
  101. * only 1 pixel.
  102. */
  103. descriptor += state->x - startX;
  104. *dst++ = descriptor;
  105. --bytes;
  106. /* Advance to past-the-last encoded pixel. */
  107. ++state->x;
  108. }
  109. assert(bytes >= 0);
  110. assert(state->count > 0);
  111. assert(state->x > 0);
  112. assert(state->count <= state->x * bytesPerPixel);
  113. if (bytes == 0) {
  114. break;
  115. }
  116. flushCount = state->count;
  117. if (flushCount > bytes) {
  118. flushCount = bytes;
  119. }
  120. memcpy(
  121. dst, state->buffer + (state->x * bytesPerPixel - state->count), flushCount);
  122. dst += flushCount;
  123. bytes -= flushCount;
  124. state->count -= flushCount;
  125. }
  126. return dst - buf;
  127. }