summary refs log tree commit diff
path: root/bin/pngo.c
blob: 31daf40e7f185847e808667aaa2ece35ec6497e2 (plain) (blame)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* Copyright (c) 2018, Curtis McEnroe <programble@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <arpa/inet.h>
#include <assert.h>
#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <zlib.h>

#define PACKED __attribute__((packed))
#define PAIR(a, b) ((uint16_t)(a) << 8 | (uint16_t)(b))

#define CRC_INIT (crc32(0, Z_NULL, 0))

static const char *path;
static FILE *file;
static uint32_t crc;

static void readExpect(void *ptr, size_t size, const char *expect) {
    fread(ptr, size, 1, file);
    if (ferror(file)) err(EX_IOERR, "%s", path);
    if (feof(file)) errx(EX_DATAERR, "%s: missing %s", path, expect);
    crc = crc32(crc, ptr, size);
}

static void writeExpect(const void *ptr, size_t size) {
    fwrite(ptr, size, 1, file);
    if (ferror(file)) err(EX_IOERR, "%s", path);
    crc = crc32(crc, ptr, size);
}

static const uint8_t SIGNATURE[8] = {
    0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n'
};

static void readSignature(void) {
    uint8_t signature[8];
    readExpect(signature, 8, "signature");
    if (0 != memcmp(signature, SIGNATURE, 8)) {
        errx(EX_DATAERR, "%s: invalid signature", path);
    }
}

static void writeSignature(void) {
    writeExpect(SIGNATURE, sizeof(SIGNATURE));
}

struct PACKED Chunk {
    uint32_t size;
    char type[4];
};

static bool ancillary(struct Chunk chunk) {
    return chunk.type[0] & 0x20;
}

static const char *typeStr(struct Chunk chunk) {
    static char buf[5];
    memcpy(buf, chunk.type, 4);
    return buf;
}

static struct Chunk readChunk(void) {
    struct Chunk chunk;
    readExpect(&chunk, sizeof(chunk), "chunk");
    chunk.size = ntohl(chunk.size);
    crc = crc32(CRC_INIT, (Byte *)chunk.type, sizeof(chunk.type));
    return chunk;
}

static void skipChunk(struct Chunk chunk) {
    int error = fseek(file, chunk.size + 4, SEEK_CUR);
    if (error) err(EX_IOERR, "%s", path);
}

static void writeChunk(struct Chunk chunk) {
    chunk.size = htonl(chunk.size);
    writeExpect(&chunk, sizeof(chunk));
    crc = crc32(CRC_INIT, (Byte *)chunk.type, sizeof(chunk.type));
}

static void readCrc(void) {
    uint32_t expected = crc;
    uint32_t found;
    readExpect(&found, sizeof(found), "CRC32");
    found = ntohl(found);
    if (found != expected) {
        errx(
            EX_DATAERR, "%s: expected CRC32 %08X, found %08X",
            path, expected, found
        );
    }
}

static void writeCrc(void) {
    uint32_t net = htonl(crc);
    writeExpect(&net, sizeof(net));
}

static struct PACKED {
    uint32_t width;
    uint32_t height;
    uint8_t depth;
    enum PACKED {
        GRAYSCALE       = 0,
        TRUECOLOR       = 2,
        INDEXED         = 3,
        GRAYSCALE_ALPHA = 4,
        TRUECOLOR_ALPHA = 6,
    } color;
    enum PACKED { DEFLATE } compression;
    enum PACKED { ADAPTIVE } filter;
    enum PACKED { PROGRESSIVE, ADAM7 } interlace;
} header;

static size_t lineSize(void) {
    switch (header.color) {
        case GRAYSCALE:       return (header.width * 1 * header.depth + 7) / 8;
        case TRUECOLOR:       return (header.width * 3 * header.depth + 7) / 8;
        case INDEXED:         return (header.width * 1 * header.depth + 7) / 8;
        case GRAYSCALE_ALPHA: return (header.width * 2 * header.depth + 7) / 8;
        case TRUECOLOR_ALPHA: return (header.width * 4 * header.depth + 7) / 8;
        default: abort();
    }
}

static size_t dataSize(void) {
    return (1 + lineSize()) * header.height;
}

static void readHeader(void) {
    struct Chunk ihdr = readChunk();
    if (0 != memcmp(ihdr.type, "IHDR", 4)) {
        errx(EX_DATAERR, "%s: expected IHDR, found %s", path, typeStr(ihdr));
    }
    if (ihdr.size != sizeof(header)) {
        errx(
            EX_DATAERR, "%s: expected IHDR size %zu, found %u",
            path, sizeof(header), ihdr.size
        );
    }
    readExpect(&header, sizeof(header), "header");
    readCrc();

    header.width = ntohl(header.width);
    header.height = ntohl(header.height);

    if (!header.width) errx(EX_DATAERR, "%s: invalid width 0", path);
    if (!header.height) errx(EX_DATAERR, "%s: invalid height 0", path);
    switch (PAIR(header.color, header.depth)) {
        case PAIR(GRAYSCALE, 1):
        case PAIR(GRAYSCALE, 2):
        case PAIR(GRAYSCALE, 4):
        case PAIR(GRAYSCALE, 8):
        case PAIR(GRAYSCALE, 16):
        case PAIR(TRUECOLOR, 8):
        case PAIR(TRUECOLOR, 16):
        case PAIR(INDEXED, 1):
        case PAIR(INDEXED, 2):
        case PAIR(INDEXED, 4):
        case PAIR(INDEXED, 8):
        case PAIR(GRAYSCALE_ALPHA, 8):
        case PAIR(GRAYSCALE_ALPHA, 16):
        case PAIR(TRUECOLOR_ALPHA, 8):
        case PAIR(TRUECOLOR_ALPHA, 16):
            break;
        default:
            errx(
                EX_DATAERR, "%s: invalid color type %hhu and bit depth %hhu",
                path, header.color, header.depth
            );
    }
    if (header.compression != DEFLATE) {
        errx(
            EX_DATAERR, "%s: invalid compression method %hhu",
            path, header.compression
        );
    }
    if (header.filter != ADAPTIVE) {
        errx(EX_DATAERR, "%s: invalid filter method %hhu", path, header.filter);
    }
    if (header.interlace > ADAM7) {
        errx(EX_DATAERR, "%s: invalid interlace method %hhu", path, header.interlace);
    }
}

static void writeHeader(void) {
    struct Chunk ihdr = { .size = sizeof(header), .type = "IHDR" };
    writeChunk(ihdr);
    header.width = htonl(header.width);
    header.height = htonl(header.height);
    writeExpect(&header, sizeof(header));
    writeCrc();
    header.width = ntohl(header.width);
    header.height = ntohl(header.height);
}

static struct {
    uint32_t len;
    uint8_t entries[256][3];
} palette;

static uint16_t paletteIndex(const uint8_t *rgb) {
    uint16_t i;
    for (i = 0; i < palette.len; ++i) {
        if (0 == memcmp(palette.entries[i], rgb, 3)) break;
    }
    return i;
}

static bool paletteAdd(const uint8_t *rgb) {
    uint16_t i = paletteIndex(rgb);
    if (i < palette.len) return true;
    if (i == 256) return false;
    memcpy(palette.entries[palette.len++], rgb, 3);
    return true;
}

static void readPalette(void) {
    struct Chunk chunk;
    for (;;) {
        chunk = readChunk();
        if (0 == memcmp(chunk.type, "PLTE", 4)) {
            break;
        } else if (ancillary(chunk)) {
            skipChunk(chunk);
        } else {
            errx(
                EX_DATAERR, "%s: expected PLTE chunk, found %s",
                path, typeStr(chunk)
            );
        }
    }
    if (chunk.size % 3) {
        errx(EX_DATAERR, "%s: PLTE size %u not divisible by 3", path, chunk.size);
    }

    palette.len = chunk.size / 3;
    readExpect(palette.entries, chunk.size, "palette data");
    readCrc();
}

static void writePalette(void) {
    struct Chunk plte = { .size = 3 * palette.len, .type = "PLTE" };
    writeChunk(plte);
    writeExpect(palette.entries, plte.size);
    writeCrc();
}

static uint8_t *data;

static void allocData(void) {
    data = malloc(dataSize());
    if (!data) err(EX_OSERR, "malloc(%zu)", dataSize());
}

static void readData(void) {
    struct z_stream_s stream = { .next_out = data, .avail_out = dataSize() };
    int error = inflateInit(&stream);
    if (error != Z_OK) errx(EX_SOFTWARE, "%s: inflateInit: %s", path, stream.msg);

    for (;;) {
        struct Chunk chunk = readChunk();
        if (0 == memcmp(chunk.type, "IDAT", 4)) {
            uint8_t idat[chunk.size];
            readExpect(idat, sizeof(idat), "image data");
            readCrc();

            stream.next_in = idat;
            stream.avail_in = sizeof(idat);
            int error = inflate(&stream, Z_SYNC_FLUSH);
            if (error == Z_STREAM_END) break;
            if (error != Z_OK) errx(EX_DATAERR, "%s: inflate: %s", path, stream.msg);

        } else if (0 == memcmp(chunk.type, "IEND", 4)) {
            errx(EX_DATAERR, "%s: missing IDAT chunk", path);
        } else if (ancillary(chunk)) {
            skipChunk(chunk);
        } else {
            errx(
                EX_CONFIG, "%s: unsupported critical chunk %s",
                path, typeStr(chunk)
            );
        }
    }

    inflateEnd(&stream);
    if (stream.total_out != dataSize()) {
        errx(
            EX_DATAERR, "%s: expected data size %zu, found %lu",
            path, dataSize(), stream.total_out
        );
    }
}

static void writeData(void) {
    uLong size = compressBound(dataSize());
    uint8_t deflate[size];
    int error = compress2(deflate, &size, data, dataSize(), Z_BEST_COMPRESSION);
    if (error != Z_OK) errx(EX_SOFTWARE, "%s: compress2: %d", path, error);

    struct Chunk idat = { .size = size, .type = "IDAT" };
    writeChunk(idat);
    writeExpect(deflate, size);
    writeCrc();
}

static void writeEnd(void) {
    struct Chunk iend = { .size = 0, .type = "IEND" };
    writeChunk(iend);
    writeCrc();
}

enum PACKED Filter {
    NONE,
    SUB,
    UP,
    AVERAGE,
    PAETH,
};
#define FILTER_COUNT (PAETH + 1)

struct Bytes {
    uint8_t x;
    uint8_t a;
    uint8_t b;
    uint8_t c;
};

static uint8_t paethPredictor(struct Bytes f) {
    int32_t p = (int32_t)f.a + (int32_t)f.b - (int32_t)f.c;
    int32_t pa = abs(p - (int32_t)f.a);
    int32_t pb = abs(p - (int32_t)f.b);
    int32_t pc = abs(p - (int32_t)f.c);
    if (pa <= pb && pa <= pc) return f.a;
    if (pb <= pc) return f.b;
    return f.c;
}

static uint8_t recon(enum Filter type, struct Bytes f) {
    switch (type) {
        case NONE:    return f.x;
        case SUB:     return f.x + f.a;
        case UP:      return f.x + f.b;
        case AVERAGE: return f.x + ((uint32_t)f.a + (uint32_t)f.b) / 2;
        case PAETH:   return f.x + paethPredictor(f);
        default:      abort();
    }
}

static uint8_t filt(enum Filter type, struct Bytes f) {
    switch (type) {
        case NONE:    return f.x;
        case SUB:     return f.x - f.a;
        case UP:      return f.x - f.b;
        case AVERAGE: return f.x - ((uint32_t)f.a + (uint32_t)f.b) / 2;
        case PAETH:   return f.x - paethPredictor(f);
        default:      abort();
    }
}

static struct Line {
    enum Filter type;
    uint8_t data[];
} **lines;

static void allocLines(void) {
    lines = calloc(header.height, sizeof(*lines));
    if (!lines) err(EX_OSERR, "calloc(%u, %zu)", header.height, sizeof(*lines));
}

static void scanlines(void) {
    size_t stride = 1 + lineSize();
    for (uint32_t y = 0; y < header.height; ++y) {
        lines[y] = (struct Line *)&data[y * stride];
        if (lines[y]->type >= FILTER_COUNT) {
            errx(EX_DATAERR, "%s: invalid filter type %hhu", path, lines[y]->type);
        }
    }
}

static struct Bytes origBytes(uint32_t y, size_t i) {
    size_t pixelSize = lineSize() / header.width;
    if (!pixelSize) pixelSize = 1;
    bool a = (i >= pixelSize), b = (y > 0), c = (a && b);
    return (struct Bytes) {
        .x = lines[y]->data[i],
        .a = a ? lines[y]->data[i - pixelSize] : 0,
        .b = b ? lines[y - 1]->data[i] : 0,
        .c = c ? lines[y - 1]->data[i - pixelSize] : 0,
    };
}

static void reconData(void) {
    for (uint32_t y = 0; y < header.height; ++y) {
        for (size_t i = 0; i < lineSize(); ++i) {
            lines[y]->data[i] =
                recon(lines[y]->type, origBytes(y, i));
        }
        lines[y]->type = NONE;
    }
}

static void filterData(void) {
    if (header.color == INDEXED || header.depth < 8) return;
    for (uint32_t y = header.height - 1; y < header.height; --y) {
        uint8_t filter[FILTER_COUNT][lineSize()];
        uint32_t heuristic[FILTER_COUNT] = {0};
        enum Filter minType = NONE;
        for (enum Filter type = NONE; type < FILTER_COUNT; ++type) {
            for (size_t i = 0; i < lineSize(); ++i) {
                filter[type][i] = filt(type, origBytes(y, i));
                heuristic[type] += abs((int8_t)filter[type][i]);
            }
            if (heuristic[type] < heuristic[minType]) minType = type;
        }
        lines[y]->type = minType;
        memcpy(lines[y]->data, filter[minType], lineSize());
    }
}

static void discardAlpha(void) {
    if (header.color != GRAYSCALE_ALPHA && header.color != TRUECOLOR_ALPHA) return;
    size_t sampleSize = header.depth / 8;
    size_t pixelSize = sampleSize * (header.color == GRAYSCALE_ALPHA ? 2 : 4);
    size_t colorSize = pixelSize - sampleSize;
    for (uint32_t y = 0; y < header.height; ++y) {
        for (uint32_t x = 0; x < header.width; ++x) {
            for (size_t i = 0; i < sampleSize; ++i) {
                if (lines[y]->data[x * pixelSize + colorSize + i] != 0xFF) return;
            }
        }
    }

    uint8_t *ptr = data;
    for (uint32_t y = 0; y < header.height; ++y) {
        *ptr++ = lines[y]->type;
        for (uint32_t x = 0; x < header.width; ++x) {
            memmove(ptr, &lines[y]->data[x * pixelSize], colorSize);
            ptr += colorSize;
        }
    }
    header.color = (header.color == GRAYSCALE_ALPHA) ? GRAYSCALE : TRUECOLOR;
    scanlines();
}

static void discardColor(void) {
    if (header.color != TRUECOLOR && header.color != TRUECOLOR_ALPHA) return;
    size_t sampleSize = header.depth / 8;
    size_t pixelSize = sampleSize * (header.color == TRUECOLOR ? 3 : 4);
    for (uint32_t y = 0; y < header.height; ++y) {
        for (uint32_t x = 0; x < header.width; ++x) {
            uint8_t *r = &lines[y]->data[x * pixelSize];
            uint8_t *g = r + sampleSize;
            uint8_t *b = g + sampleSize;
            if (0 != memcmp(r, g, sampleSize)) return;
            if (0 != memcmp(g, b, sampleSize)) return;
        }
    }

    uint8_t *ptr = data;
    for (uint32_t y = 0; y < header.height; ++y) {
        *ptr++ = lines[y]->type;
        for (uint32_t x = 0; x < header.width; ++x) {
            uint8_t *pixel = &lines[y]->data[x * pixelSize];
            memmove(ptr, pixel, sampleSize);
            ptr += sampleSize;
            if (header.color == TRUECOLOR_ALPHA) {
                memmove(ptr, pixel + 3 * sampleSize, sampleSize);
                ptr += sampleSize;
            }
        }
    }
    header.color = (header.color == TRUECOLOR) ? GRAYSCALE : GRAYSCALE_ALPHA;
    scanlines();
}

static void indexColor(void) {
    if (header.color != TRUECOLOR || header.depth != 8) return;
    for (uint32_t y = 0; y < header.height; ++y) {
        for (uint32_t x = 0; x < header.width; ++x) {
            if (!paletteAdd(&lines[y]->data[x * 3])) return;
        }
    }

    uint8_t *ptr = data;
    for (uint32_t y = 0; y < header.height; ++y) {
        *ptr++ = lines[y]->type;
        for (uint32_t x = 0; x < header.width; ++x) {
            *ptr++ = paletteIndex(&lines[y]->data[x * 3]);
        }
    }
    header.color = INDEXED;
    scanlines();
}

static void reduceDepth8(void) {
    if (header.color != GRAYSCALE && header.color != INDEXED) return;
    if (header.depth != 8) return;
    if (header.color == GRAYSCALE) {
        for (uint32_t y = 0; y < header.height; ++y) {
            for (size_t i = 0; i < lineSize(); ++i) {
                uint8_t a = lines[y]->data[i];
                if ((a >> 4) != (a & 0x0F)) return;
            }
        }
    } else if (palette.len > 16) {
        return;
    }

    uint8_t *ptr = data;
    for (uint32_t y = 0; y < header.height; ++y) {
        *ptr++ = lines[y]->type;
        for (size_t i = 0; i < lineSize(); i += 2) {
            uint8_t iByte = lines[y]->data[i];
            uint8_t jByte = (i + 1 < lineSize()) ? lines[y]->data[i + 1] : 0;
            uint8_t a = iByte & 0x0F;
            uint8_t b = jByte & 0x0F;
            *ptr++ = a << 4 | b;
        }
    }
    header.depth = 4;
    scanlines();
}

static void reduceDepth4(void) {
    if (header.depth != 4) return;
    if (header.color == GRAYSCALE) {
        for (uint32_t y = 0; y < header.height; ++y) {
            for (size_t i = 0; i < lineSize(); ++i) {
                uint8_t a = lines[y]->data[i] >> 4;
                uint8_t b = lines[y]->data[i] & 0x0F;
                if ((a >> 2) != (a & 0x03)) return;
                if ((b >> 2) != (b & 0x03)) return;
            }
        }
    } else if (palette.len > 4) {
        return;
    }

    uint8_t *ptr = data;
    for (uint32_t y = 0; y < header.height; ++y) {
        *ptr++ = lines[y]->type;
        for (size_t i = 0; i < lineSize(); i += 2) {
            uint8_t iByte = lines[y]->data[i];
            uint8_t jByte = (i + 1 < lineSize()) ? lines[y]->data[i + 1] : 0;
            uint8_t a = iByte >> 4 & 0x03, b = iByte & 0x03;
            uint8_t c = jByte >> 4 & 0x03, d = jByte & 0x03;
            *ptr++ = a << 6 | b << 4 | c << 2 | d;
        }
    }
    header.depth = 2;
    scanlines();
}

static void reduceDepth2(void) {
    if (header.depth != 2) return;
    if (header.color == GRAYSCALE) {
        for (uint32_t y = 0; y < header.height; ++y) {
            for (size_t i = 0; i < lineSize(); ++i) {
                uint8_t a = lines[y]->data[i] >> 6;
                uint8_t b = lines[y]->data[i] >> 4 & 0x03;
                uint8_t c = lines[y]->data[i] >> 2 & 0x03;
                uint8_t d = lines[y]->data[i] & 0x03;
                if ((a >> 1) != (a & 0x01)) return;
                if ((b >> 1) != (b & 0x01)) return;
                if ((c >> 1) != (c & 0x01)) return;
                if ((d >> 1) != (d & 0x01)) return;
            }
        }
    } else if (palette.len > 2) {
        return;
    }

    uint8_t *ptr = data;
    for (uint32_t y = 0; y < header.height; ++y) {
        *ptr++ = lines[y]->type;
        for (size_t i = 0; i < lineSize(); i += 2) {
            uint8_t iByte = lines[y]->data[i];
            uint8_t jByte = (i + 1 < lineSize()) ? lines[y]->data[i + 1] : 0;
            uint8_t a = iByte >> 6 & 0x01, b = iByte >> 4 & 0x01;
            uint8_t c = iByte >> 2 & 0x01, d = iByte & 0x01;
            uint8_t e = jByte >> 6 & 0x01, f = jByte >> 4 & 0x01;
            uint8_t g = jByte >> 2 & 0x01, h = jByte & 0x01;
            *ptr++ = a << 7 | b << 6 | c << 5 | d << 4 | e << 3 | f << 2 | g << 1 | h;
        }
    }
    header.depth = 1;
    scanlines();
}

static void reduceDepth(void) {
    reduceDepth8();
    reduceDepth4();
    reduceDepth2();
}

static void optimize(const char *inPath, const char *outPath) {
    if (inPath) {
        path = inPath;
        file = fopen(path, "r");
        if (!file) err(EX_NOINPUT, "%s", path);
    } else {
        path = "(stdin)";
        file = stdin;
    }

    readSignature();
    readHeader();
    if (header.interlace != PROGRESSIVE) {
        errx(
            EX_CONFIG, "%s: unsupported interlace method %hhu",
            path, header.interlace
        );
    }
    if (header.color == INDEXED) readPalette();
    allocData();
    readData();
    fclose(file);

    allocLines();
    scanlines();
    reconData();

    discardAlpha();
    discardColor();
    indexColor();
    reduceDepth();
    filterData();
    free(lines);

    if (outPath) {
        path = outPath;
        file = fopen(path, "w");
        if (!file) err(EX_CANTCREAT, "%s", path);
    } else {
        path = "(stdout)";
        file = stdout;
    }

    writeSignature();
    writeHeader();
    if (header.color == INDEXED) writePalette();
    writeData();
    writeEnd();
    free(data);

    int error = fclose(file);
    if (error) err(EX_IOERR, "%s", path);
}

int main(int argc, char *argv[]) {
    bool stdio = false;
    char *output = NULL;

    int opt;
    while (0 < (opt = getopt(argc, argv, "co:"))) {
        switch (opt) {
            case 'c': stdio = true; break;
            case 'o': output = optarg; break;
            default: return EX_USAGE;
        }
    }

    if (argc - optind == 1 && (output || stdio)) {
        optimize(argv[optind], output);
    } else if (optind < argc) {
        for (int i = optind; i < argc; ++i) {
            optimize(argv[i], argv[i]);
        }
    } else {
        optimize(NULL, output);
    }

    return EX_OK;
}