-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cpp
More file actions
547 lines (463 loc) · 17.1 KB
/
Copy pathFileManager.cpp
File metadata and controls
547 lines (463 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "FileManager.h"
#include <fstream>
#include <iostream>
#include "MyString.h"
#include "MyVector.h"
FileManager::FileManager()
{
}
FileManager& FileManager::getInstance()
{
static FileManager instance;
return instance;
}
// Reads the entire file into a binary buffer
bool FileManager::readToFileBuffer(const MyString& path, MyVector<char>& outBuffer)
{
std::ifstream file(path.c_str(), std::ios::binary);
if (!file.is_open()) {
std::cout << "Error: could not open file: " << path << "\n";
return false;
}
// Get file size - seek to the end and read position
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg); // return to the beginning
if (size <= 0) {
std::cout << "Error: empty file\n";
return false;
}
outBuffer.resize(static_cast<size_t>(size)); // allocate memory
file.read(outBuffer.data(), size); // read everything at once
if (!file.good()) {
std::cout << "Error: failed to read file\n";
return false;
}
file.close();
return true;
}
// Writes a buffer to a file.
bool FileManager::writeFromFileBuffer(const MyString& path, const MyVector<char>& inBuffer)
{
std::ofstream file(path.c_str(), std::ios::binary);
if (!file.is_open()) return false;
file.write(inBuffer.data(), inBuffer.size());
file.close();
return !file.fail();
}
// Skips spaces, tabs, newlines and comments
void FileManager::skipWhitespaceAndComments(const MyVector<char>& buffer, int& pos)
{
while (pos < (int)buffer.size()) {
if (buffer[pos] == ' ' || buffer[pos] == '\t' || buffer[pos] == '\n' || buffer[pos] == '\r') {
pos++;
continue;
}
if (buffer[pos] == '#') {
// comment - skip everything until \n
while (pos < (int)buffer.size() && buffer[pos] != '\n') {
pos++;
}
continue;
}
break; // found a meaningful character
}
}
// Reads an integer from the buffer, first skipping whitespace/comments.
// Used for reading width, height, maxVal and pixel data in ASCII formats.
int FileManager::readInt(const MyVector<char>& buffer, int& pos)
{
skipWhitespaceAndComments(buffer, pos);
int result = 0;
bool negative = false;
if (pos < (int)buffer.size() && buffer[pos] == '-') {
negative = true;
pos++;
}
if (pos >= (int)buffer.size() || !std::isdigit(buffer[pos])) {
std::cout << "Error: expected number at position " << pos << "\n";
return -1;
}
// Build the number character by character: '5' -> 5, '3' -> 53, etc.
while (pos < (int)buffer.size() && std::isdigit(buffer[pos])) {
result = result * 10 + (buffer[pos] - '0');
pos++;
}
return negative ? -result : result;
}
// Loading an image from a file
// 1. Read the entire file into a buffer
// 2. Find the type "P1".."P6" after possible comments
// 3. Determine PType and call the corresponding parser
bool FileManager::load(Image* image)
{
if (!image) {
std::cout << "Error: image pointer is null\n";
return false;
}
MyVector<char> buffer;
if (!readToFileBuffer(image->path, buffer)) {
return false;
}
if (buffer.size() < 2) {
std::cout << "Error: file too small to be NETpbm\n";
return false;
}
// Skip comments and whitespace before the type
int pos = 0;
skipWhitespaceAndComments(buffer, pos);
// Read the "magic number" (P1, P2, ..., P6)
if (pos + 1 >= (int)buffer.size() || buffer[pos] != 'P' || buffer[pos + 1] < '1' || buffer[pos + 1] > '6') {
std::cout << "Error: not a NETpbm file (expected P1-P6)\n";
return false;
}
image->PType = buffer[pos + 1] - '0';
pos += 2;
// Parse depending on P type
switch (image->PType) {
case 1: parseP1(image, buffer, pos); break;
case 2: parseP2(image, buffer, pos); break;
case 3: parseP3(image, buffer, pos); break;
case 4: parseP4(image, buffer, pos); break;
case 5: parseP5(image, buffer, pos); break;
case 6: parseP6(image, buffer, pos); break;
default:
std::cout << "Error: unknown P-type: P" << image->PType << "\n";
return false;
}
std::cout << "Loaded: " << image->getName() << " (P" << image->PType
<< ", " << image->width << "x" << image->height << ")\n";
return true;
}
// ---- Saving ----
// Any image is saved in binary format (P4, P5, P6)
static void writeIntToBuffer(int val, MyVector<char>& buf) {
if (val == 0) { buf.push_back('0'); return; }
char digits[16];
int len = 0;
while (val > 0) {
digits[len++] = '0' + (val % 10);
val /= 10;
}
for (int i = len - 1; i >= 0; i--) buf.push_back(digits[i]); // reverse
}
// Saves the image to a file. Automatically selects format:
// P4 for P1/P4, P5 for P2/P5, P6 for P3/P6
bool FileManager::save(Image* image, const MyString& savePath)
{
if (!image || !image->isLoaded()) {
std::cout << "Error: image not loaded, cannot save\n";
return false;
}
MyVector<char> outBuffer;
int imagePType = image->GetPType();
if (imagePType == 1 || imagePType == 4) {
// Black-and-white → P4
image->setPType(4);
saveP4(image, outBuffer);
}
else if (imagePType == 2 || imagePType == 5) {
// Grayscale → P5
image->setPType(5);
saveP5(image, outBuffer);
}
else {
// Color → P6
image->setPType(6);
saveP6(image, outBuffer);
}
if (!writeFromFileBuffer(savePath, outBuffer)) {
std::cout << "Error: failed to write file: " << savePath << "\n";
return false;
}
std::cout << "Saved: " << savePath << " (P" << image->PType
<< ", " << image->width << "x" << image->height << ")\n";
return true;
}
// Saves in P4 (binary black-and-white).
// Each row is packed: 8 pixels -> 1 byte.
// MSB of the byte = leftmost pixel
void FileManager::saveP4(Image* image, MyVector<char>& outBuffer)
{
// Header: "P4\nwidth height\n"
outBuffer.push_back('P');
outBuffer.push_back('4');
outBuffer.push_back('\n');
writeIntToBuffer(image->width, outBuffer);
outBuffer.push_back(' ');
writeIntToBuffer(image->height, outBuffer);
outBuffer.push_back('\n');
int bytesPerRow = (image->width + 7) / 8;
for (int y = 0; y < image->height; y++) {
for (int x = 0; x < bytesPerRow; x++) {
unsigned char byteVal = 0;
for (int bit = 0; bit < 8; bit++) {
int pixelX = x * 8 + bit;
if (pixelX < image->width) {
int pixelIndex = y * image->width + pixelX;
if (image->pixels[pixelIndex].g == 1) {
byteVal |= (1 << (7 - bit));
}
}
}
outBuffer.push_back(byteVal);
}
}
}
// Saves in P5 (binary grayscale).
// Each pixel - 1 byte (grayscale value).
void FileManager::saveP5(Image* image, MyVector<char>& outBuffer)
{
// Header: "P5\nwidth height\n255\n"
outBuffer.push_back('P');
outBuffer.push_back('5');
outBuffer.push_back('\n');
writeIntToBuffer(image->width, outBuffer);
outBuffer.push_back(' ');
writeIntToBuffer(image->height, outBuffer);
outBuffer.push_back('\n');
outBuffer.push_back('2');
outBuffer.push_back('5');
outBuffer.push_back('5');
outBuffer.push_back('\n');
int totalPixels = image->width * image->height;
for (int i = 0; i < totalPixels; i++) {
unsigned char gray = static_cast<unsigned char>(image->pixels[i].r);
outBuffer.push_back(gray);
}
}
// Saves in P6 (binary full-color RGB).
// Each pixel - 3 consecutive bytes: R, G, B.
void FileManager::saveP6(Image* image, MyVector<char>& outBuffer)
{
// Header: "P6\nwidth height\n255\n"
outBuffer.push_back('P');
outBuffer.push_back('6');
outBuffer.push_back('\n');
writeIntToBuffer(image->width, outBuffer);
outBuffer.push_back(' ');
writeIntToBuffer(image->height, outBuffer);
outBuffer.push_back('\n');
outBuffer.push_back('2');
outBuffer.push_back('5');
outBuffer.push_back('5');
outBuffer.push_back('\n');
int totalPixels = image->width * image->height;
for (int i = 0; i < totalPixels; i++) {
outBuffer.push_back(static_cast<unsigned char>(image->pixels[i].r));
outBuffer.push_back(static_cast<unsigned char>(image->pixels[i].g));
outBuffer.push_back(static_cast<unsigned char>(image->pixels[i].b));
}
}
// ---- Parsers for loading ----
// P1: ASCII black-and-white (0 = white, 1 = black).
// Read width, height, then characters '0'/'1' separated by whitespace
void FileManager::parseP1(Image* image, const MyVector<char>& buffer, int& pos)
{
image->width = readInt(buffer, pos);
image->height = readInt(buffer, pos);
image->maxVal = 1;
image->pixels.clear();
image->pixels.reserve(image->width * image->height);
int pixelCount = 0;
int totalPixels = image->width * image->height;
// Read characters, skipping whitespace, until we have totalPixels
while (pixelCount < totalPixels && pos < (int)buffer.size()) {
skipWhitespaceAndComments(buffer, pos);
if (pos >= (int)buffer.size()) break;
if (buffer[pos] == '0' || buffer[pos] == '1') {
short gray = buffer[pos] - '0';
image->pixels.push_back(Pixel(gray));
pos++;
pixelCount++;
} else {
pos++; // skip garbage characters
}
}
if (pixelCount != totalPixels) {
std::cout << "Warning: P1 file ended early (" << pixelCount
<< "/" << totalPixels << " pixels)\n";
}
}
// P2: ASCII grayscale.
// Read width, height, maxVal. Then read numbers via readInt().
// Each number - grayscale value from 0 to maxVal.
// Scale to 0..255: gray = val * 255 / maxVal.
void FileManager::parseP2(Image* image, const MyVector<char>& buffer, int& pos)
{
image->width = readInt(buffer, pos);
image->height = readInt(buffer, pos);
image->maxVal = readInt(buffer, pos); // maximum value in the file (may not be 255)
if (image->maxVal <= 0) {
std::cout << "Error: invalid maxVal for P2\n";
return;
}
image->pixels.clear();
image->pixels.reserve(image->width * image->height);
int pixelCount = 0;
int totalPixels = image->width * image->height;
while (pixelCount < totalPixels && pos < (int)buffer.size()) {
int val = readInt(buffer, pos);
if (val < 0) break;
// Scale: if file stores 0..65535, then val * 255 / maxVal gives 0..255
short gray = static_cast<short>(val * 255 / image->maxVal);
image->pixels.push_back(Pixel(gray));
pixelCount++;
}
if (pixelCount != totalPixels) {
std::cout << "Warning: P2 file ended early (" << pixelCount
<< "/" << totalPixels << " pixels)\n";
}
}
// P3: ASCII color (RGB).
// Read width, height, maxVal. Then read R, G, B.
// Each channel is scaled to 0..255
void FileManager::parseP3(Image* image, const MyVector<char>& buffer, int& pos)
{
image->width = readInt(buffer, pos);
image->height = readInt(buffer, pos);
image->maxVal = readInt(buffer, pos);
if (image->maxVal <= 0) {
std::cout << "Error: invalid maxVal for P3\n";
return;
}
image->pixels.clear();
image->pixels.reserve(image->width * image->height);
int pixelCount = 0;
int totalPixels = image->width * image->height;
// Read 3 numbers (R, G, B) per pixel
while (pixelCount < totalPixels && pos < (int)buffer.size()) {
int r = readInt(buffer, pos);
if (r < 0) break;
int g = readInt(buffer, pos);
if (g < 0) break;
int b = readInt(buffer, pos);
if (b < 0) break;
short red = static_cast<short>(r * 255 / image->maxVal);
short green = static_cast<short>(g * 255 / image->maxVal);
short blue = static_cast<short>(b * 255 / image->maxVal);
image->pixels.push_back(Pixel(red, green, blue));
pixelCount++;
}
if (pixelCount != totalPixels) {
std::cout << "Warning: P3 file ended early (" << pixelCount
<< "/" << totalPixels << " pixels)\n";
}
}
// P4: Binary black-and-white (1 bit per pixel).
// After the header comes binary data: each row is packed into ceil(width/8) bytes.
// MSB of the first byte = leftmost pixel of the row.
void FileManager::parseP4(Image* image, const MyVector<char>& buffer, int& pos)
{
image->width = readInt(buffer, pos);
image->height = readInt(buffer, pos);
image->maxVal = 1;
image->pixels.clear();
image->pixels.reserve(image->width * image->height);
skipWhitespaceAndComments(buffer, pos);
int bytesPerRow = (image->width + 7) / 8; // how many bytes per row
// Go through all rows and pixels, unpacking bits
for (int y = 0; y < image->height; y++) {
for (int x = 0; x < image->width; x++) {
int byteIndex = pos + x / 8; // which byte the pixel is in
int bitIndex = 7 - (x % 8); // which bit (MSB = 7, LSB = 0)
if (byteIndex >= (int)buffer.size()) {
std::cout << "Warning: P4 file ended early at row " << y << "\n";
return;
}
short gray = (buffer[byteIndex] >> bitIndex) & 1; // extract bit
image->pixels.push_back(Pixel(gray));
}
pos += bytesPerRow; // move to the next row
}
}
// P5: Binary grayscale.
// After the header comes binary data:
// - if maxVal <= 255 - 1 byte per pixel
// - if maxVal > 255 - 2 bytes per pixel
void FileManager::parseP5(Image* image, const MyVector<char>& buffer, int& pos)
{
image->width = readInt(buffer, pos);
image->height = readInt(buffer, pos);
image->maxVal = readInt(buffer, pos);
if (image->maxVal <= 0) {
std::cout << "Error: invalid maxVal for P5\n";
return;
}
image->pixels.clear();
image->pixels.reserve(image->width * image->height);
skipWhitespaceAndComments(buffer, pos);
int totalPixels = image->width * image->height;
if (image->maxVal <= 255) {
for (int i = 0; i < totalPixels; i++) {
if (pos >= (int)buffer.size()) {
std::cout << "Warning: P5 file ended early (" << i
<< "/" << totalPixels << ")\n";
return;
}
short gray = static_cast<short>((unsigned char)buffer[pos] * 255 / image->maxVal);
image->pixels.push_back(Pixel(gray));
pos++;
}
} else {
for (int i = 0; i < totalPixels; i++) {
if (pos + 1 >= (int)buffer.size()) {
std::cout << "Warning: P5 file ended early (" << i
<< "/" << totalPixels << ")\n";
return;
}
int val = ((unsigned char)buffer[pos] << 8) | (unsigned char)buffer[pos + 1];
short gray = static_cast<short>(val * 255 / image->maxVal);
image->pixels.push_back(Pixel(gray));
pos += 2;
}
}
}
// P6: Binary color (RGB)
// - if maxVal <= 255 - 3 bytes per pixel (R, G, B)
// - if maxVal > 255 - 6 bytes per pixel
void FileManager::parseP6(Image* image, const MyVector<char>& buffer, int& pos)
{
image->width = readInt(buffer, pos);
image->height = readInt(buffer, pos);
image->maxVal = readInt(buffer, pos);
if (image->maxVal <= 0) {
std::cout << "Error: invalid maxVal for P6\n";
return;
}
image->pixels.clear();
image->pixels.reserve(image->width * image->height);
skipWhitespaceAndComments(buffer, pos);
int totalPixels = image->width * image->height;
if (image->maxVal <= 255) {
for (int i = 0; i < totalPixels; i++) {
if (pos + 2 >= (int)buffer.size()) {
std::cout << "Warning: P6 file ended early (" << i
<< "/" << totalPixels << ")\n";
return;
}
short r = static_cast<short>((unsigned char)buffer[pos] * 255 / image->maxVal);
short g = static_cast<short>((unsigned char)buffer[pos + 1] * 255 / image->maxVal);
short b = static_cast<short>((unsigned char)buffer[pos + 2] * 255 / image->maxVal);
image->pixels.push_back(Pixel(r, g, b));
pos += 3;
}
} else {
for (int i = 0; i < totalPixels; i++) {
if (pos + 5 >= (int)buffer.size()) {
std::cout << "Warning: P6 file ended early (" << i
<< "/" << totalPixels << ")\n";
return;
}
int r = ((unsigned char)buffer[pos] << 8) | (unsigned char)buffer[pos + 1];
int g = ((unsigned char)buffer[pos + 2] << 8) | (unsigned char)buffer[pos + 3];
int b = ((unsigned char)buffer[pos + 4] << 8) | (unsigned char)buffer[pos + 5];
image->pixels.push_back(Pixel(
static_cast<short>(r * 255 / image->maxVal),
static_cast<short>(g * 255 / image->maxVal),
static_cast<short>(b * 255 / image->maxVal)
));
pos += 6;
}
}
}