TgaRleEncode.c 4.2 KB

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