Skip to content

Commit d7a3f29

Browse files
authored
Merge pull request #51 from lordnn/master
Refactored `CachedIO`.
2 parents 5170b17 + 704bc60 commit d7a3f29

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

Source/Plugins/PluginRAS.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class CachedIO {
9595
CachedIO(FreeImageIO *io, fi_handle handle) : io_{ io }, handle_{ handle } {
9696
}
9797

98+
bool ReadData(uint8_t *buf, uint32_t length, FIBOOL rle);
99+
100+
private:
98101
bool getByte(uint8_t &res) {
99102
if (sz_ <= pos_) {
100103
sz_ = io_->read_proc(cache_, 1, std::size(cache_), handle_);
@@ -111,58 +114,54 @@ class CachedIO {
111114
return 0 < io_->read_proc(buffer, size, count, handle_);
112115
}
113116

114-
private:
115117
uint8_t cache_[1024];
116118
FreeImageIO *io_{};
117119
fi_handle handle_{};
118-
uint32_t pos_{}, sz_{};
120+
uint32_t pos_{}, sz_{}, remaining_{};
121+
uint8_t repchar_{};
119122
};
120123

121-
static bool
122-
ReadData(CachedIO &cio, uint8_t *buf, uint32_t length, FIBOOL rle) {
124+
bool CachedIO::ReadData(uint8_t *buf, uint32_t length, FIBOOL rle) {
123125
// Read either Run-Length Encoded or normal image data
124126

125-
static uint8_t repchar{};
126-
static uint32_t remaining{};
127-
128127
if (rle) {
129128
// Run-length encoded read
130129

131130
while (length) {
132-
if (remaining) {
133-
const auto len{ std::min(remaining, length) };
134-
std::memset(buf, repchar, len);
131+
if (remaining_) {
132+
const auto len{ std::min(remaining_, length) };
133+
std::memset(buf, repchar_, len);
135134
buf += len;
136135
length -= len;
137-
remaining -= len;
136+
remaining_ -= len;
138137
} else {
139-
if (!cio.getByte(repchar)) {
138+
if (!this->getByte(repchar_)) {
140139
break;
141140
}
142141

143-
if (repchar == RESC) {
142+
if (RESC == repchar_) {
144143
uint8_t tmp{};
145-
if (!cio.getByte(tmp)) {
144+
if (!this->getByte(tmp)) {
146145
break;
147146
}
148147

149148
if (0 == tmp) {
150149
*(buf++)= RESC;
151150
--length;
152151
} else {
153-
if (!cio.getByte(repchar)) {
152+
if (!this->getByte(repchar_)) {
154153
break;
155154
}
156155

157-
remaining = tmp + 1;
158-
const auto len{ std::min(remaining, length) };
159-
std::memset(buf, repchar, len);
156+
remaining_ = tmp + 1;
157+
const auto len{ std::min(remaining_, length) };
158+
std::memset(buf, repchar_, len);
160159
buf += len;
161160
length -= len;
162-
remaining -= len;
161+
remaining_ -= len;
163162
}
164163
} else {
165-
*(buf++)= repchar;
164+
*(buf++)= repchar_;
166165
--length;
167166
}
168167
}
@@ -171,7 +170,7 @@ ReadData(CachedIO &cio, uint8_t *buf, uint32_t length, FIBOOL rle) {
171170
} else {
172171
// Normal read
173172

174-
return cio.read_proc(buf, length, 1);
173+
return this->read_proc(buf, length, 1);
175174
}
176175
}
177176

@@ -413,12 +412,12 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
413412
for (y = 0; y < header.height; y++) {
414413
bits = FreeImage_GetScanLine(dib.get(), header.height - 1 - y);
415414

416-
if (!ReadData(cio, bits, linelength, rle)) {
415+
if (!cio.ReadData(bits, linelength, rle)) {
417416
break;
418417
}
419418

420419
if (fill) {
421-
if (!ReadData(cio, &fillchar, fill, rle)) {
420+
if (!cio.ReadData(&fillchar, fill, rle)) {
422421
break;
423422
}
424423
}
@@ -435,7 +434,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
435434
for (y = 0; y < header.height; y++) {
436435
bits = FreeImage_GetScanLine(dib.get(), header.height - 1 - y);
437436

438-
if (!ReadData(cio, buf.get(), header.width * 3, rle)) {
437+
if (!cio.ReadData(buf.get(), header.width * 3, rle)) {
439438
break;
440439
}
441440

@@ -460,7 +459,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
460459
}
461460

462461
if (fill) {
463-
if (!ReadData(cio, &fillchar, fill, rle)) {
462+
if (!cio.ReadData(&fillchar, fill, rle)) {
464463
return nullptr;
465464
}
466465
}
@@ -477,7 +476,7 @@ Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
477476
for (y = 0; y < header.height; y++) {
478477
bits = FreeImage_GetScanLine(dib.get(), header.height - 1 - y);
479478

480-
if (!ReadData(cio, buf.get(), header.width * 4, rle)) {
479+
if (!cio.ReadData(buf.get(), header.width * 4, rle)) {
481480
break;
482481
}
483482

0 commit comments

Comments
 (0)