main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * meg4/tests/converter/main.c
  3. *
  4. * Copyright (C) 2023 bzt
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * @brief Tests MEG-4 import/export capabilities
  21. *
  22. */
  23. #define _POSIX_C_SOURCE 199309L /* needed for timespec and nanosleep() */
  24. #include <stdio.h>
  25. #include "../../src/meg4.h"
  26. #include "editors.h"
  27. #define STB_IMAGE_WRITE_IMPLEMENTATION
  28. #define STBI_WRITE_ONLY_PNG
  29. #define STBI_WRITE_NO_FAILURE_STRINGS
  30. #define STBI_WRITE_NO_SIMD
  31. #define STBI_WRITE_NO_STDIO
  32. #define STBIW_ASSERT(x)
  33. #include "../../src/stb_image_write.h"
  34. #define STB_IMAGE_IMPLEMENTATION
  35. #define STBI_ONLY_PNG
  36. #define STBI_NO_FAILURE_STRINGS
  37. #define STBI_NO_SIMD
  38. #define STBI_NO_STDIO
  39. #define STBI_ASSERT(x)
  40. #include "../../src/stb_image.h"
  41. #include "../../src/floppy.h"
  42. meg4_t meg4;
  43. uint8_t *meg4_font = NULL, *meg4_defwaves = NULL, *meg4_init = NULL;
  44. uint32_t meg4_init_len = 0;
  45. char meg4_title[64], meg4_author[64], meg4_pro = 0;
  46. void meg4_switchmode(int mode) { (void)mode; }
  47. void meg4_recalcmipmap(void) { }
  48. int gpio_init(uint8_t *buf, int len) { (void)buf; (void)len; return 0; }
  49. void meg4_recalcfont(int s, int e)
  50. {
  51. uint8_t *fnt = meg4.font + 8 * s, *ptr = meg4.font + 8 * 65536 + s;
  52. int i, x, y, l, r;
  53. if(s < 0 || s > 0xffff || e < 0 || e > 0xffff || e < s) return;
  54. memset(ptr, 0, e - s + 1);
  55. for(i = s; i <= e; i++, fnt += 8, ptr++) {
  56. if(i == 32 || i == 160) { *ptr = 0x30; continue; }
  57. for(l = 7, r = y = 0; y < 8; y++)
  58. for(x = 0; x < 8; x++)
  59. if(fnt[y] & (1 << x)) {
  60. if(l > x) l = x;
  61. if(r < x) r = x;
  62. }
  63. if(l > r) l = r;
  64. *ptr = (r << 4) | l;
  65. }
  66. }
  67. uint8_t meg4_palidx(uint8_t *rgba)
  68. {
  69. uint8_t ret = 0, *b;
  70. int i, dr, dg, db, d, dm = 0x7fffffff;
  71. for(i = 0; i < 256 && dm > 0; i++) {
  72. b = (uint8_t*)&meg4.mmio.palette[i];
  73. if(rgba[0] == b[0] && rgba[1] == b[1] && rgba[2] == b[2]) return i;
  74. db = rgba[2] > b[2] ? rgba[2] - b[2] : b[2] - rgba[2];
  75. dg = rgba[1] > b[1] ? rgba[1] - b[1] : b[1] - rgba[1];
  76. dr = rgba[0] > b[0] ? rgba[0] - b[0] : b[0] - rgba[0];
  77. d = ((dr*dr) << 1) + (db*db) + ((dg*dg) << 2);
  78. if(d < dm) { dm = d; ret = i; }
  79. }
  80. return ret;
  81. }
  82. char *meg4_utf8(char *str, uint32_t *out)
  83. {
  84. if((*str & 128) != 0) {
  85. if(!(*str & 32)) { *out = ((*str & 0x1F)<<6)|(*(str+1) & 0x3F); str += 1; } else
  86. if(!(*str & 16)) { *out = ((*str & 0xF)<<12)|((*(str+1) & 0x3F)<<6)|(*(str+2) & 0x3F); str += 2; } else
  87. if(!(*str & 8)) { *out = ((*str & 0x7)<<18)|((*(str+1) & 0x3F)<<12)|((*(str+2) & 0x3F)<<6)|(*(str+3) & 0x3F); str += 3; }
  88. else *out = 0;
  89. } else *out = *str;
  90. return str + 1;
  91. }
  92. void sound_addwave(int wave) { (void)wave; }
  93. void code_init(void) { }
  94. int overlay_idx = 0;
  95. int main_cfgsave(char *cfg, uint8_t *buf, int len) { (void)cfg; (void)buf; (void)len; return 1; }
  96. uint8_t* main_readfile(char *file, int *size)
  97. {
  98. uint8_t *data = NULL;
  99. FILE *f;
  100. if(!file || !*file || !size) return NULL;
  101. *size = 0;
  102. f = fopen(file, "rb");
  103. if(f) {
  104. fseek(f, 0L, SEEK_END);
  105. *size = (int)ftell(f);
  106. fseek(f, 0L, SEEK_SET);
  107. data = (uint8_t*)malloc(*size);
  108. if(data) {
  109. memset(data, 0, *size);
  110. if((int)fread(data, 1, *size, f) != *size) { free(data); data = NULL; *size = 0; }
  111. }
  112. fclose(f);
  113. }
  114. return data;
  115. }
  116. int main_writefile(char *file, uint8_t *buf, int size)
  117. {
  118. int ret = 0;
  119. FILE *f;
  120. if(!file || !*file || !buf || size < 1) return 0;
  121. f = fopen(file, "wb");
  122. if(f) {
  123. ret = ((int)fwrite(buf, 1, size, f) == size);
  124. fclose(f);
  125. }
  126. return ret;
  127. }
  128. int main_savefile(const char *name, uint8_t *buf, int len) { return main_writefile((char*)name, buf, len); }
  129. /**
  130. * Log messages
  131. */
  132. void main_log(int lvl, const char* fmt, ...)
  133. {
  134. __builtin_va_list args;
  135. __builtin_va_start(args, fmt);
  136. (void)lvl;
  137. printf("meg4: "); vprintf(fmt, args); printf("\r\n");
  138. __builtin_va_end(args);
  139. }
  140. /**
  141. * Import files
  142. */
  143. int main_import(char *name, uint8_t *buf, int len)
  144. {
  145. uint32_t s;
  146. uint8_t *end = buf + len - 12, *floppy = NULL;
  147. if(!buf) return 0;
  148. if(!memcmp(buf, "\x89PNG", 4) && ((buf[18] << 8) | buf[19]) == 210 && ((buf[22] << 8) | buf[23]) == 220) {
  149. for(buf += 8; buf < end; buf += s + 12) {
  150. s = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
  151. if(!memcmp(buf + 4, "flPy", 4) && stbiw__crc32(buf + 4, s + 4) == (uint32_t)((buf[8+s]<<24)|(buf[9+s]<<16)|(buf[10+s]<<8)|buf[11+s])) {
  152. floppy = (uint8_t*)stbi_zlib_decode_malloc_guesssize_headerflag((const char *)buf + 8, s, 65536, &len, 1);
  153. break;
  154. }
  155. }
  156. if(!floppy) return 0;
  157. s = meg4_deserialize(floppy, len);
  158. free(floppy);
  159. return s;
  160. } else
  161. return meg4_import(name, buf, len, 0);
  162. }
  163. /**
  164. * Initialize stuff because we haven't turn on the MEG-4
  165. */
  166. void main_init(char *fn)
  167. {
  168. char *v = MEG4_VERSION;
  169. int c, i, j, k, l, m, n, s;
  170. uint8_t *font, *ptr, *frg, *buf, *o;
  171. memset(&meg4, 0, sizeof(meg4));
  172. meg4.mmio.fwver[0] = atoi(v); while(*v != '.') { v++; } v++;
  173. meg4.mmio.fwver[1] = atoi(v); while(*v != '.') { v++; } v++;
  174. meg4.mmio.fwver[2] = atoi(v);
  175. memcpy(meg4.mmio.palette, default_pal, sizeof(meg4.mmio.palette));
  176. if((v = strrchr(fn, '/')) || (v = strrchr(fn, '\\'))) v++; else v = fn;
  177. strncpy(meg4_title, v, sizeof(meg4_title) - 1);
  178. if((v = strrchr(meg4_title, '.'))) *v = 0;
  179. /* just in case, if someone wants to import MIDI files */
  180. meg4_defwaves = (uint8_t*)stbi_zlib_decode_malloc_guesssize_headerflag((const char *)binary_sounds_mod, sizeof(binary_sounds_mod), 65536, &i, 1);
  181. /* uncompress and set default font */
  182. meg4_font = (uint8_t*)malloc(9 * 65536);
  183. if(meg4_font) {
  184. memset(meg4_font, 0, 9 * 65536);
  185. ptr = (uint8_t*)binary_default_sfn + 3;
  186. i = *ptr++; ptr += 6; if(i & 4) { k = *ptr++; k += (*ptr++ << 8); ptr += k; } if(i & 8) { while(*ptr++ != 0); }
  187. if(i & 16) { while(*ptr++ != 0); } j = sizeof(binary_default_sfn) - (size_t)(ptr - binary_default_sfn);
  188. font = (uint8_t*)stbi_zlib_decode_malloc_guesssize_headerflag((const char*)ptr, j, 65536, &s, 0);
  189. if(font) {
  190. for(buf = font + le32toh(*((uint32_t*)(font + 16))), c = 0; c < 0x010000 && buf < font + s; c++) {
  191. if(buf[0] == 0xFF) { c += 65535; buf++; } else
  192. if((buf[0] & 0xC0) == 0xC0) { j = (((buf[0] & 0x3F) << 8) | buf[1]); c += j; buf += 2; } else
  193. if((buf[0] & 0xC0) == 0x80) { j = (buf[0] & 0x3F); c += j; buf++; } else {
  194. ptr = buf + 6; o = meg4_font + c * 8;
  195. for(i = n = 0; i < buf[1]; i++, ptr += buf[0] & 0x40 ? 6 : 5) {
  196. if(ptr[0] == 255 && ptr[1] == 255) continue;
  197. frg = font + (buf[0] & 0x40 ? ((ptr[5] << 24) | (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]) :
  198. ((ptr[4] << 16) | (ptr[3] << 8) | ptr[2]));
  199. if((frg[0] & 0xE0) != 0x80) continue;
  200. o += (int)(ptr[1] - n); n = ptr[1]; k = ((frg[0] & 0x0F) + 1) << 3; j = frg[1] + 1; frg += 2;
  201. for(m = 1; j; j--, n++, o++)
  202. for(l = 0; l < k; l++, m <<= 1) {
  203. if(m > 0x80) { frg++; m = 1; }
  204. if(*frg & m) *o |= m;
  205. }
  206. }
  207. buf += 6 + buf[1] * (buf[0] & 0x40 ? 6 : 5);
  208. }
  209. }
  210. free(font);
  211. }
  212. memcpy(meg4.font, meg4_font, 8 * 65536);
  213. meg4_recalcfont(0, 0xffff);
  214. memcpy(meg4_font + 8 * 65536, meg4.font + 8 * 65536, 65536);
  215. /* clear control codes */
  216. memset(meg4.font, 0, 8 * 32);
  217. memset(meg4.font + 8 * 65536, 0, 32);
  218. memset(meg4.font + 8 * 128, 0, 8 * 32);
  219. memset(meg4.font + 8 * 65536 + 128, 0, 32);
  220. }
  221. }
  222. /**
  223. * Main function
  224. */
  225. int main(int argc, char **argv)
  226. {
  227. uint8_t *buf;
  228. int len, isbinary = 0, i = 1;
  229. /* load input */
  230. if(argc < 1 || !argv[1]) {
  231. printf("MEG-4 Converter by bzt Copyright (C) 2023 GPLv3+\r\n\r\n");
  232. printf("%s [-b] <somefile> [output.zip]\r\n", argv[0]);
  233. exit(1);
  234. }
  235. if(argv[1][0] == '-') { isbinary = 1; i++; }
  236. buf = main_readfile(argv[i], &len);
  237. main_init(argv[1]);
  238. printf("importing...\n");
  239. /* import */
  240. if(!main_import(argv[i], buf, len)) {
  241. free(buf); free(meg4_defwaves); free(meg4_font);
  242. printf("unable to load file\n");
  243. exit(1);
  244. }
  245. free(buf);
  246. printf("exporting...\n");
  247. /* export */
  248. meg4_export(argv[i + 1] ? argv[i + 1] : "output.zip", isbinary);
  249. /* free resources */
  250. free(meg4_defwaves);
  251. free(meg4_font);
  252. return 0;
  253. }