File: etc1tool.cpp

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (581 lines) | stat: -rw-r--r-- 17,044 bytes parent folder | download | duplicates (4)
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
// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <png.h>
#include <ETC1/etc1.h>


int writePNGFile(const char* pOutput, png_uint_32 width, png_uint_32 height,
        const png_bytep pImageData, png_uint_32 imageStride);

const char* gpExeName;

static
void usage(const char* message, ...) {
    if (message) {
        va_list ap;
        va_start(ap, message);
        vfprintf(stderr, message, ap);
        va_end(ap);
        fprintf(stderr, "\n\n");
        fprintf(stderr, "usage:\n");
    }
    fprintf(
            stderr,
            "%s infile [--help | --encode | --encodeNoHeader | --decode] [--showDifference difffile] [-o outfile]\n",
            gpExeName);
    fprintf(stderr, "\tDefault is --encode\n");
    fprintf(stderr, "\t\t--help           print this usage information.\n");
    fprintf(stderr,
            "\t\t--encode         create an ETC1 file from a PNG file.\n");
    fprintf(
            stderr,
            "\t\t--encodeNoHeader create a raw ETC1 data file (without a header) from a PNG file.\n");
    fprintf(stderr,
            "\t\t--decode         create a PNG file from an ETC1 file.\n");
    fprintf(stderr,
            "\t\t--showDifference difffile    Write difference between original and encoded\n");
    fprintf(stderr,
            "\t\t                             image to difffile. (Only valid when encoding).\n");
    fprintf(stderr,
            "\tIf outfile is not specified, an outfile path is constructed from infile,\n");
    fprintf(stderr, "\twith the appropriate suffix (.pkm or .png).\n");
    exit(1);
}

// Returns non-zero if an error occured

static
int changeExtension(char* pPath, size_t pathCapacity, const char* pExtension) {
    size_t pathLen = strlen(pPath);
    size_t extensionLen = strlen(pExtension);
    if (pathLen + extensionLen + 1 > pathCapacity) {
        return -1;
    }

    // Check for '.' and '..'
    if ((pathLen == 1 && pPath[0] == '.') || (pathLen == 2 && pPath[0] == '.'
            && pPath[1] == '.') || (pathLen >= 2 && pPath[pathLen - 2] == '/'
            && pPath[pathLen - 1] == '.') || (pathLen >= 3
            && pPath[pathLen - 3] == '/' && pPath[pathLen - 2] == '.'
            && pPath[pathLen - 1] == '.')) {
        return -2;
    }

    int index;
    for (index = pathLen - 1; index > 0; index--) {
        char c = pPath[index];
        if (c == '/') {
            // No extension found. Append our extension.
            strcpy(pPath + pathLen, pExtension);
            return 0;
        } else if (c == '.') {
            strcpy(pPath + index, pExtension);
            return 0;
        }
    }

    // No extension or directory found. Append our extension
    strcpy(pPath + pathLen, pExtension);
    return 0;
}

void PNGAPI user_error_fn(png_structp /*png_ptr*/, png_const_charp message) {
    fprintf(stderr, "PNG error: %s\n", message);
}

void PNGAPI user_warning_fn(png_structp /*png_ptr*/, png_const_charp message) {
    fprintf(stderr, "PNG warning: %s\n", message);
}

// Return non-zero on error
int fwrite_big_endian_uint16(png_uint_32 data, FILE* pOut) {
    if (fputc(0xff & (data >> 8), pOut) == EOF) {
        return -1;
    }
    if (fputc(0xff & data, pOut) == EOF) {
        return -1;
    }
    return 0;
}

// Return non-zero on error
int fread_big_endian_uint16(png_uint_32* data, FILE* pIn) {
    int a, b;
    if ((a = fgetc(pIn)) == EOF) {
        return -1;
    }
    if ((b = fgetc(pIn)) == EOF) {
        return -1;
    }
    *data = ((0xff & a) << 8) | (0xff & b);
    return 0;
}

// Read a PNG file into a contiguous buffer.
// Returns non-zero if an error occurred.
// caller has to delete[] *ppImageData when done with the image.

int read_PNG_File(const char* pInput, etc1_byte** ppImageData,
        etc1_uint32* pWidth, etc1_uint32* pHeight) {
    FILE* pIn = NULL;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_infop end_info = NULL;
    png_bytep* row_pointers = NULL; // Does not need to be deallocated.
    png_uint_32 width = 0;
    png_uint_32 height = 0;
    png_uint_32 stride = 0;
    int result = -1;
    etc1_byte* pSourceImage = 0;

    if ((pIn = fopen(pInput, "rb")) == NULL) {
        fprintf(stderr, "Could not open input file %s for reading: %d\n",
                pInput, errno);
        goto exit;
    }

    static const size_t PNG_HEADER_SIZE = 8;
    png_byte pngHeader[PNG_HEADER_SIZE];
    if (fread(pngHeader, 1, PNG_HEADER_SIZE, pIn) != PNG_HEADER_SIZE) {
        fprintf(stderr, "Could not read PNG header from %s: %d\n", pInput,
                errno);
        goto exit;
    }

    if (png_sig_cmp(pngHeader, 0, PNG_HEADER_SIZE)) {
        fprintf(stderr, "%s is not a PNG file.\n", pInput);
        goto exit;
    }

    if (!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
            (png_voidp) NULL, user_error_fn, user_warning_fn))) {
        fprintf(stderr, "Could not initialize png read struct.\n");
        goto exit;
    }

    if (!(info_ptr = png_create_info_struct(png_ptr))) {
        fprintf(stderr, "Could not create info struct.\n");
        goto exit;
    }
    if (!(end_info = png_create_info_struct(png_ptr))) {
        fprintf(stderr, "Could not create end_info struct.\n");
        goto exit;
    }

    if (setjmp(png_jmpbuf(png_ptr))) {
        goto exit;
    }

    png_init_io(png_ptr, pIn);
    png_set_sig_bytes(png_ptr, PNG_HEADER_SIZE);
    png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY
            | PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_STRIP_ALPHA
            | PNG_TRANSFORM_PACKING, NULL);

    row_pointers = png_get_rows(png_ptr, info_ptr);
    {
        int bit_depth, color_type;
        png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
                &color_type, NULL, NULL, NULL);
    }

    stride = 3 * width;

    pSourceImage = new etc1_byte[stride * height];
    if (! pSourceImage) {
        fprintf(stderr, "Out of memory.\n");
        goto exit;
    }

    for (etc1_uint32 y = 0; y < height; y++) {
        memcpy(pSourceImage + y * stride, row_pointers[y], stride);
    }

    *pWidth = width;
    *pHeight = height;
    *ppImageData = pSourceImage;

    result = 0;
    exit:
    if (result) {
        delete[] pSourceImage;
    }
    if (png_ptr) {
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    }
    if (pIn) {
        fclose(pIn);
    }

    return result;
}

// Read a PNG file into a contiguous buffer.
// Returns non-zero if an error occurred.
// caller has to delete[] *ppImageData when done with the image.
int readPKMFile(const char* pInput, etc1_byte** ppImageData,
        etc1_uint32* pWidth, etc1_uint32* pHeight) {
    int result = -1;
    FILE* pIn = NULL;
    etc1_byte header[ETC_PKM_HEADER_SIZE];
    png_bytep pEncodedData = NULL;
    png_bytep pImageData = NULL;

    png_uint_32 width = 0;
    png_uint_32 height = 0;
    png_uint_32 stride = 0;
    png_uint_32 encodedSize = 0;

    if ((pIn = fopen(pInput, "rb")) == NULL) {
        fprintf(stderr, "Could not open input file %s for reading: %d\n",
                pInput, errno);
        goto exit;
    }

    if (fread(header, sizeof(header), 1, pIn) != 1) {
        fprintf(stderr, "Could not read header from input file %s: %d\n",
                pInput, errno);
        goto exit;
    }

    if (! etc1_pkm_is_valid(header)) {
        fprintf(stderr, "Bad header PKM header for input file %s\n", pInput);
        goto exit;
    }

    width = etc1_pkm_get_width(header);
    height = etc1_pkm_get_height(header);
    encodedSize = etc1_get_encoded_data_size(width, height);

    pEncodedData = new png_byte[encodedSize];
    if (!pEncodedData) {
        fprintf(stderr, "Out of memory.\n");
        goto exit;
    }

    if (fread(pEncodedData, encodedSize, 1, pIn) != 1) {
        fprintf(stderr, "Could not read encoded data from input file %s: %d\n",
                pInput, errno);
        goto exit;
    }

    fclose(pIn);
    pIn = NULL;

    stride = width * 3;
    pImageData = new png_byte[stride * height];
    if (!pImageData) {
        fprintf(stderr, "Out of memory.\n");
        goto exit;
    }

    etc1_decode_image(pEncodedData, pImageData, width, height, 3, stride);

    // Success
    result = 0;
    *ppImageData = pImageData;
    pImageData = 0;
    *pWidth = width;
    *pHeight = height;

    exit:
    delete[] pEncodedData;
    delete[] pImageData;
    if (pIn) {
        fclose(pIn);
    }

    return result;
}


// Encode the file.
// Returns non-zero if an error occurred.

int encode(const char* pInput, const char* pOutput, bool bEmitHeader, const char* pDiffFile) {
    FILE* pOut = NULL;
    etc1_uint32 width = 0;
    etc1_uint32 height = 0;
    etc1_uint32 encodedSize = 0;
    int result = -1;
    etc1_byte* pSourceImage = 0;
    etc1_byte* pEncodedData = 0;
    etc1_byte* pDiffImage = 0; // Used for differencing

    if (read_PNG_File(pInput, &pSourceImage, &width, &height)) {
        goto exit;
    }

    encodedSize = etc1_get_encoded_data_size(width, height);
    pEncodedData = new etc1_byte[encodedSize];
    if (!pEncodedData) {
        fprintf(stderr, "Out of memory.\n");
        goto exit;
    }

    etc1_encode_image(pSourceImage,
            width, height, 3, width * 3, pEncodedData);

    if ((pOut = fopen(pOutput, "wb")) == NULL) {
        fprintf(stderr, "Could not open output file %s: %d\n", pOutput, errno);
        goto exit;
    }

    if (bEmitHeader) {
        etc1_byte header[ETC_PKM_HEADER_SIZE];
        etc1_pkm_format_header(header, width, height);
        if (fwrite(header, sizeof(header), 1, pOut) != 1) {
            fprintf(stderr,
                    "Could not write header output file %s: %d\n",
                    pOutput, errno);
            goto exit;
        }
    }

    if (fwrite(pEncodedData, encodedSize, 1, pOut) != 1) {
        fprintf(stderr,
                "Could not write encoded data to output file %s: %d\n",
                pOutput, errno);
        goto exit;
    }

    fclose(pOut);
    pOut = NULL;

    if (pDiffFile) {
        etc1_uint32 outWidth;
        etc1_uint32 outHeight;
        if (readPKMFile(pOutput, &pDiffImage, &outWidth, &outHeight)) {
            goto exit;
        }
        if (outWidth != width || outHeight != height) {
            fprintf(stderr, "Output file has incorrect bounds: %u, %u != %u, %u\n",
                    outWidth, outHeight, width, height);
            goto exit;
        }
        const etc1_byte* pSrc = pSourceImage;
        etc1_byte* pDest = pDiffImage;
        etc1_uint32 size = width * height * 3;
        for (etc1_uint32 i = 0; i < size; i++) {
            int diff = *pSrc++ - *pDest;
            diff *= diff;
            diff <<= 3;
            if (diff < 0) {
                diff = 0;
            } else if (diff > 255) {
                diff = 255;
            }
            *pDest++ = (png_byte) diff;
        }
        writePNGFile(pDiffFile, outWidth, outHeight, pDiffImage, 3 * outWidth);
    }

    // Success
    result = 0;

    exit:
    delete[] pSourceImage;
    delete[] pEncodedData;
    delete[] pDiffImage;

    if (pOut) {
        fclose(pOut);
    }
    return result;
}

int writePNGFile(const char* pOutput, png_uint_32 width, png_uint_32 height,
        const png_bytep pImageData, png_uint_32 imageStride) {
    int result = -1;
    FILE* pOut = NULL;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;

    if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
            (png_voidp) NULL, user_error_fn, user_warning_fn)) || !(info_ptr
            = png_create_info_struct(png_ptr))) {
        fprintf(stderr, "Could not initialize PNG library for writing.\n");
        goto exit;
    }

    if (setjmp(png_jmpbuf(png_ptr))) {
        goto exit;
    }

    if ((pOut = fopen(pOutput, "wb")) == NULL) {
        fprintf(stderr, "Could not open output file %s: %d\n", pOutput, errno);
        goto exit;
    }

    png_init_io(png_ptr, pOut);

    png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
            PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
            PNG_FILTER_TYPE_DEFAULT);

    png_write_info(png_ptr, info_ptr);

    for (png_uint_32 y = 0; y < height; y++) {
        png_write_row(png_ptr, pImageData + y * imageStride);
    }
    png_write_end(png_ptr, info_ptr);

    result = 0;

    exit: if (png_ptr) {
        png_destroy_write_struct(&png_ptr, &info_ptr);
    }

    if (pOut) {
        fclose(pOut);
    }
    return result;
}

int decode(const char* pInput, const char* pOutput) {
    int result = -1;
    png_bytep pImageData = NULL;
    etc1_uint32 width = 0;
    etc1_uint32 height = 0;

    if (readPKMFile(pInput, &pImageData, &width, &height)) {
        goto exit;
    }

    if (writePNGFile(pOutput, width, height, pImageData, width * 3)) {
        goto exit;
    }

    // Success
    result = 0;

    exit: delete[] pImageData;

    return result;
}

void multipleEncodeDecodeCheck(bool* pbEncodeDecodeSeen) {
    if (*pbEncodeDecodeSeen) {
        usage("At most one occurrence of --encode --encodeNoHeader or --decode is allowed.\n");
    }
    *pbEncodeDecodeSeen = true;
}

int main(int argc, char** argv) {
    gpExeName = argv[0];
    const char* pInput = NULL;
    const char* pOutput = NULL;
    const char* pDiffFile = NULL;
    char* pOutputFileBuff = NULL;

    bool bEncodeDecodeSeen = false;
    bool bEncode = false;
    bool bEncodeHeader = false;
    bool bShowDifference = false;

    for (int i = 1; i < argc; i++) {
        const char* pArg = argv[i];
        if (pArg[0] == '-') {
            char c = pArg[1];
            switch (c) {
            case 'o':
                if (pOutput != NULL) {
                    usage("Only one -o flag allowed.");
                }
                if (i + 1 >= argc) {
                    usage("Expected outfile after -o");
                }
                pOutput = argv[++i];
                break;
            case '-':
                if (strcmp(pArg, "--encode") == 0) {
                    multipleEncodeDecodeCheck(&bEncodeDecodeSeen);
                    bEncode = true;
                    bEncodeHeader = true;
                } else if (strcmp(pArg, "--encodeNoHeader") == 0) {
                    multipleEncodeDecodeCheck(&bEncodeDecodeSeen);
                    bEncode = true;
                    bEncodeHeader = false;
                } else if (strcmp(pArg, "--decode") == 0) {
                    multipleEncodeDecodeCheck(&bEncodeDecodeSeen);
                } else if (strcmp(pArg, "--showDifference") == 0) {
                    if (bShowDifference) {
                        usage("Only one --showDifference option allowed.\n");
                    }
                    bShowDifference = true;
                    if (i + 1 >= argc) {
                        usage("Expected difffile after --showDifference");
                    }
                    pDiffFile = argv[++i];
                } else if (strcmp(pArg, "--help") == 0) {
                    usage( NULL);
                } else {
                    usage("Unknown flag %s", pArg);
                }

                break;
            default:
                usage("Unknown flag %s", pArg);
                break;
            }
        } else {
            if (pInput != NULL) {
                usage(
                        "Only one input file allowed. Already have %s, now see %s",
                        pInput, pArg);
            }
            pInput = pArg;
        }
    }

    if (!bEncodeDecodeSeen) {
        bEncode = true;
        bEncodeHeader = true;
    }
    if ((! bEncode) && bShowDifference) {
        usage("--showDifference is only valid when encoding.");
    }

    if (!pInput) {
        usage("Expected an input file.");
    }

    if (!pOutput) {
        const char* kDefaultExtension = bEncode ? ".pkm" : ".png";
        size_t buffSize = strlen(pInput) + strlen(kDefaultExtension) + 1;
        pOutputFileBuff = new char[buffSize];
        strcpy(pOutputFileBuff, pInput);
        if (changeExtension(pOutputFileBuff, buffSize, kDefaultExtension)) {
            usage("Could not change extension of input file name: %s\n", pInput);
        }
        pOutput = pOutputFileBuff;
    }

    if (bEncode) {
        encode(pInput, pOutput, bEncodeHeader, pDiffFile);
    } else {
        decode(pInput, pOutput);
    }

    delete[] pOutputFileBuff;

    return 0;
}