Skip to content

Commit 0435d5c

Browse files
committed
Refactored CachedIO.
1 parent 0a86fa5 commit 0435d5c

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

Source/Plugins/PluginRAS.cpp

Lines changed: 16 additions & 17 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,20 +114,16 @@ 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

@@ -136,21 +135,21 @@ ReadData(CachedIO &cio, uint8_t *buf, uint32_t length, FIBOOL rle) {
136135
length -= len;
137136
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

@@ -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)