No Description https://ascii.town
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

png.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <err.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sysexits.h>
  21. static inline uint32_t pngCRCTable(uint8_t n) {
  22. static uint32_t table[256];
  23. if (table[1]) return table[n];
  24. for (int i = 0; i < 256; ++i) {
  25. table[i] = i;
  26. for (int j = 0; j < 8; ++j) {
  27. table[i] = (table[i] >> 1) ^ (table[i] & 1 ? 0xEDB88320 : 0);
  28. }
  29. }
  30. return table[n];
  31. }
  32. static uint32_t pngCRC;
  33. static inline void pngWrite(FILE *file, const uint8_t *ptr, uint32_t len) {
  34. if (!fwrite(ptr, len, 1, file)) err(EX_IOERR, "pngWrite");
  35. for (uint32_t i = 0; i < len; ++i) {
  36. pngCRC = pngCRCTable(pngCRC ^ ptr[i]) ^ (pngCRC >> 8);
  37. }
  38. }
  39. static inline void pngInt32(FILE *file, uint32_t n) {
  40. pngWrite(file, (uint8_t []) { n >> 24, n >> 16, n >> 8, n }, 4);
  41. }
  42. static inline void pngChunk(FILE *file, char type[static 4], uint32_t len) {
  43. pngInt32(file, len);
  44. pngCRC = ~0;
  45. pngWrite(file, (uint8_t *)type, 4);
  46. }
  47. enum {
  48. PNGGrayscale,
  49. PNGTruecolor = 2,
  50. PNGIndexed,
  51. PNGAlpha,
  52. };
  53. static inline void pngHead(
  54. FILE *file, uint32_t width, uint32_t height, uint8_t depth, uint8_t color
  55. ) {
  56. pngWrite(file, (uint8_t *)"\x89PNG\r\n\x1A\n", 8);
  57. pngChunk(file, "IHDR", 13);
  58. pngInt32(file, width);
  59. pngInt32(file, height);
  60. pngWrite(file, &depth, 1);
  61. pngWrite(file, &color, 1);
  62. pngWrite(file, (uint8_t []) { 0, 0, 0 }, 3);
  63. pngInt32(file, ~pngCRC);
  64. }
  65. static inline void pngPalette(FILE *file, const uint8_t *pal, uint32_t len) {
  66. pngChunk(file, "PLTE", len);
  67. pngWrite(file, pal, len);
  68. pngInt32(file, ~pngCRC);
  69. }
  70. enum {
  71. PNGNone,
  72. PNGSub,
  73. PNGUp,
  74. PNGAverage,
  75. PNGPaeth,
  76. };
  77. static inline void pngData(FILE *file, const uint8_t *data, uint32_t len) {
  78. uint32_t adler1 = 1, adler2 = 0;
  79. for (uint32_t i = 0; i < len; ++i) {
  80. adler1 = (adler1 + data[i]) % 65521;
  81. adler2 = (adler1 + adler2) % 65521;
  82. }
  83. uint32_t zlen = 2 + 5 * ((len + 0xFFFE) / 0xFFFF) + len + 4;
  84. pngChunk(file, "IDAT", zlen);
  85. pngWrite(file, (uint8_t []) { 0x08, 0x1D }, 2);
  86. for (; len > 0xFFFF; data += 0xFFFF, len -= 0xFFFF) {
  87. pngWrite(file, (uint8_t []) { 0x00, 0xFF, 0xFF, 0x00, 0x00 }, 5);
  88. pngWrite(file, data, 0xFFFF);
  89. }
  90. pngWrite(file, (uint8_t []) { 0x01, len, len >> 8, ~len, ~len >> 8 }, 5);
  91. pngWrite(file, data, len);
  92. pngInt32(file, adler2 << 16 | adler1);
  93. pngInt32(file, ~pngCRC);
  94. }
  95. static inline void pngTail(FILE *file) {
  96. pngChunk(file, "IEND", 0);
  97. pngInt32(file, ~pngCRC);
  98. }