1-rgb565.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (C) 2018 Tom Li <tomli at tomli.me>.
  3. *
  4. * This software is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 3, or (at your option) any
  7. * later version.
  8. *
  9. * This software is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. * See COPYING for terms and conditions.
  14. *
  15. * GPL or LGPL does __not__ require attribution beyond preserving and
  16. * following the license, but if you find my code is useful or inspirational
  17. * to your own project, I'd be thankful if you attribute my name.
  18. */
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. int main(int argc, char **argv)
  24. {
  25. FILE *f;
  26. FILE *f2;
  27. long size;
  28. char name[256];
  29. if (argc != 2) {
  30. fprintf(stderr, "error: too many or no argument, should be\n");
  31. fprintf(stderr, "%s [A INPUT FILE]\n", argv[0]);
  32. return 1;
  33. }
  34. strncpy(name, argv[1], 64);
  35. name[64] = '\0';
  36. f = fopen(name, "rb");
  37. fseek(f, 0, SEEK_END);
  38. size = ftell(f);
  39. fseek(f, 1, SEEK_SET);
  40. strcat(name, ".rgb565");
  41. f2 = fopen(name, "w+");
  42. //printf("size: %lu\n", size);
  43. uint16_t r, g, b;
  44. uint16_t color;
  45. uint8_t done = 0;
  46. for (long i = 1; i < size; i += 3) {
  47. done |= !fread(&r, 1, 1, f);
  48. done |= !fread(&g, 1, 1, f);
  49. done |= !fread(&b, 1, 1, f);
  50. if (done) {
  51. break;
  52. }
  53. b = (((uint8_t) b) >> 3) & 0x1F;
  54. g = ((((uint8_t) g) >> 2) & 0x3F) << 5;
  55. r = ((((uint8_t) r) >> 3) & 0x1F) << 11;
  56. color = (r | g | b);
  57. fwrite(&color, sizeof(color), 1, f2);
  58. }
  59. fclose(f);
  60. fclose(f2);
  61. }