79{
80 static constexpr int minRunLen = 4;
81 int cur = 0;
82 int begRun;
83 int runCount;
84 int oldRunCount;
85 int nonRunCount;
86
87 quint8 buf[2];
88
89 while (cur < nBytes) {
90 begRun = cur;
91
92 runCount = oldRunCount = 0;
93 while ((runCount < minRunLen) && (begRun < nBytes)) {
94 begRun += runCount;
95 oldRunCount = runCount;
96 runCount = 1;
97 while ((begRun + runCount < nBytes) && (runCount < 127) && (data[begRun] == data[begRun + runCount])) {
98 runCount++;
99 }
100 }
101
102 if ((oldRunCount > 1) && (oldRunCount == begRun - cur)) {
103 buf[0] = 128 + oldRunCount;
104 buf[1] = data[cur];
105 rleBuffer.append(reinterpret_cast<const char *>(buf), sizeof(buf));
106 cur = begRun;
107 }
108
109 while (cur < begRun) {
110 nonRunCount = begRun - cur;
111 if (nonRunCount > 128) {
112 nonRunCount = 128;
113 }
114 buf[0] = nonRunCount;
115 rleBuffer.append(buf[0]);
116 rleBuffer.append(reinterpret_cast<const char *>(data + cur), sizeof(data[0]) * nonRunCount);
117 cur += nonRunCount;
118 }
119
120 if (runCount >= minRunLen) {
121 buf[0] = 128 + runCount;
122 buf[1] = data[begRun];
123 rleBuffer.append(reinterpret_cast<const char *>(buf), sizeof(buf));
124 cur += runCount;
125 }
126 }
127}