1
0

rle.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "rle.h"
  2. static bool CheckOverflow(size_t total_size, int current_position, int read_size)
  3. {
  4. if (read_size > (int)total_size) // check separate to avoid overflow
  5. return true;
  6. if (((int)total_size - read_size) < current_position)
  7. return true;
  8. return false;
  9. }
  10. void RLE16(const uint8_t *rle, size_t rle_size_bytes, uint16_t *video_frame, size_t video_frame_size, int stride)
  11. {
  12. int input = 0;
  13. int output = 0;
  14. video_frame_size >>= 1; // divide by 2 since we're indexing as uint16_t
  15. int next_line = output + stride;
  16. while (input < (int)rle_size_bytes && output < (int)video_frame_size)
  17. {
  18. if (CheckOverflow(rle_size_bytes, input, 2)) // we always read at least two bytes
  19. break;
  20. uint8_t b0 = rle[input++];
  21. if (b0)
  22. {
  23. if (CheckOverflow(rle_size_bytes, input, 2))
  24. break;
  25. if (CheckOverflow(video_frame_size, output, b0))
  26. {
  27. b0 = (uint8_t)(video_frame_size - output);
  28. }
  29. uint16_t pixel = *(uint16_t *)(&rle[input]);
  30. input += 2;
  31. while (b0--)
  32. {
  33. memcpy(&video_frame[output], &pixel, 2);
  34. output++;
  35. }
  36. }
  37. else
  38. {
  39. uint8_t b1 = rle[input++];
  40. if (b1 == 0)
  41. {
  42. output = next_line;
  43. next_line = output + stride;
  44. }
  45. else if (b1 == 1)
  46. {
  47. return;
  48. }
  49. else if (b1 == 2)
  50. {
  51. if (CheckOverflow(rle_size_bytes, input, 2))
  52. break;
  53. uint8_t p1 = rle[input++];
  54. uint8_t p2 = rle[input++];
  55. output += p1;
  56. output += p2*stride;
  57. next_line += p2*stride;
  58. }
  59. else
  60. {
  61. if (CheckOverflow(rle_size_bytes, input, b1*2))
  62. break;
  63. if (CheckOverflow(video_frame_size, output, b1))
  64. break;
  65. for (uint8_t i=0;i!=b1;i++)
  66. {
  67. video_frame[output++] = *(uint16_t *)(&rle[input]);
  68. input+=2;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. void RLE8(const uint8_t *rle, size_t rle_size_bytes, uint8_t *video_frame, size_t video_frame_size, int stride)
  75. {
  76. int input = 0;
  77. int output = 0;
  78. int next_line = output + stride;
  79. while (input < (int)rle_size_bytes && output < (int)video_frame_size)
  80. {
  81. if (CheckOverflow(rle_size_bytes, input, 2)) // we always read at least two bytes
  82. break;
  83. uint8_t b0 = rle[input++];
  84. if (b0)
  85. {
  86. if (CheckOverflow(video_frame_size, output, b0))
  87. {
  88. b0 = (uint8_t)(video_frame_size - output);
  89. }
  90. uint8_t pixel = rle[input++];
  91. memset(&video_frame[output], pixel, b0);
  92. output+=b0;
  93. }
  94. else
  95. {
  96. uint8_t b1 = rle[input++];
  97. if (b1 == 0)
  98. {
  99. output = next_line;
  100. next_line = output + stride;
  101. }
  102. else if (b1 == 1)
  103. {
  104. break;
  105. }
  106. else if (b1 == 2)
  107. {
  108. if (CheckOverflow(rle_size_bytes, input, 2))
  109. break;
  110. uint8_t p1 = rle[input++];
  111. uint8_t p2 = rle[input++];
  112. output += p1;
  113. output += p2*stride;
  114. next_line += p2*stride;
  115. }
  116. else
  117. {
  118. if (CheckOverflow(rle_size_bytes, input, b1))
  119. break;
  120. if (CheckOverflow(video_frame_size, output, b1))
  121. break;
  122. memcpy(&video_frame[output], &rle[input], b1);
  123. input += b1;
  124. output += b1;
  125. if (b1 & 1)
  126. input++;
  127. }
  128. }
  129. }
  130. }