12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- /*
- * Copyright (C) 2018 Tom Li <tomli at tomli.me>.
- *
- * This software is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 3, or (at your option) any
- * later version.
- *
- * This software 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 COPYING for terms and conditions.
- *
- * GPL or LGPL does __not__ require attribution beyond preserving and
- * following the license, but if you find my code is useful or inspirational
- * to your own project, I'd be thankful if you attribute my name.
- */
- #include <stdint.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- int main(int argc, char **argv)
- {
- FILE *f;
- FILE *f2;
- long size;
- char name[256];
- if (argc != 2) {
- fprintf(stderr, "error: too many or no argument, should be\n");
- fprintf(stderr, "%s [A RGB565 INPUT FILE]\n", argv[0]);
- return 1;
- }
- strncpy(name, argv[1], 64);
- name[64] = '\0';
- f = fopen(name, "rb");
- fseek(f, 0, SEEK_END);
- size = ftell(f) / 2;
- fseek(f, 0, SEEK_SET);
- strcat(name, ".rle");
- f2 = fopen(name, "w+");
- //printf("size: %lu\n", size);
- uint16_t color = 0;
- uint16_t color2 = 0;
- uint8_t length = 1;
- for (long i = 0; i < size; i++) {
- fread(&color2, sizeof(color), 1, f);
- //printf("%x\n", color2);
- if (color == color2) {
- length++;
- //printf("%d\n", length);
- }
- //printf("%ld %ld\n", i, size);
- if (i != 0 && (color != color2 || length == 255 || i == size - 1)) {
- //printf("write -> %d %x\n", length, color);
- fwrite(&length, sizeof(length), 1, f2);
- fwrite(&color, sizeof(color), 1, f2);
- length = 1;
- }
- color = color2;
- }
- //printf("write -> %d %x\n", length, color);
- fwrite(&length, sizeof(length), 1, f2);
- fwrite(&color, sizeof(color), 1, f2);
- fclose(f);
- fclose(f2);
- }
|