591 行
18 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * oric.c: Oric Atmos import/export functions
  16. */
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. /* Image dimensions and recursion depth. DEPTH = 2 is a reasonable value,
  25. * DEPTH = 3 gives good quality, and higher values may improve the results
  26. * even more but at the cost of significantly longer computation times. */
  27. #define WIDTH 240
  28. #define HEIGHT 200
  29. #define DEPTH 2
  30. static int read_screen(char const *name, uint8_t *screen);
  31. static void write_screen(float const *data, uint8_t *screen);
  32. pipi_image_t *pipi_load_oric(char const *name)
  33. {
  34. static uint8_t const pal[32] =
  35. {
  36. 0x00, 0x00, 0x00, 0xff,
  37. 0x00, 0x00, 0xff, 0xff,
  38. 0x00, 0xff, 0x00, 0xff,
  39. 0x00, 0xff, 0xff, 0xff,
  40. 0xff, 0x00, 0x00, 0xff,
  41. 0xff, 0x00, 0xff, 0xff,
  42. 0xff, 0xff, 0x00, 0xff,
  43. 0xff, 0xff, 0xff, 0xff,
  44. };
  45. pipi_image_t *img;
  46. pipi_pixels_t *p;
  47. uint8_t *screen, *data;
  48. int x, y, i;
  49. screen = malloc(WIDTH * HEIGHT / 6);
  50. if(read_screen(name, screen) < 0)
  51. {
  52. free(screen);
  53. return NULL;
  54. }
  55. img = pipi_new(WIDTH, HEIGHT);
  56. p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  57. data = p->pixels;
  58. for(y = 0; y < HEIGHT; y++)
  59. {
  60. int bg = 0, fg = 7;
  61. for(x = 0; x < 40; x++)
  62. {
  63. int col;
  64. uint8_t c = screen[y * 40 + x];
  65. if(c & 0x40)
  66. {
  67. for(i = 0; i < 6; i++)
  68. {
  69. col = (c & (1 << (5 - i))) ? (c & 0x80) ? 7 - fg : fg
  70. : (c & 0x80) ? 7 - bg : bg;
  71. memcpy(data + (y * WIDTH + x * 6 + i) * 4,
  72. pal + 4 * col, 4);
  73. }
  74. }
  75. else if((c & 0x60) == 0x00)
  76. {
  77. if(c & 0x10)
  78. bg = c & 0x7;
  79. else
  80. fg = c & 0x7;
  81. col = (c & 0x80) ? 7 - bg : bg;
  82. for(i = 0; i < 6; i++)
  83. memcpy(data + (y * WIDTH + x * 6 + i) * 4,
  84. pal + 4 * col, 4);
  85. }
  86. /* else: invalid sequence */
  87. }
  88. }
  89. free(screen);
  90. img->codec_priv = NULL;
  91. img->wrap = 0;
  92. img->u8 = 1;
  93. return img;
  94. }
  95. int pipi_save_oric(pipi_image_t *img, char const *name)
  96. {
  97. pipi_image_t *tmp = NULL;
  98. pipi_pixels_t *p;
  99. float *data;
  100. uint8_t *screen;
  101. FILE *fp;
  102. size_t len;
  103. len = strlen(name);
  104. if(len < 4 || name[len - 4] != '.'
  105. || toupper(name[len - 3]) != 'T'
  106. || toupper(name[len - 2]) != 'A'
  107. || toupper(name[len - 1]) != 'P')
  108. return -1;
  109. fp = fopen(name, "w");
  110. if(!fp)
  111. return -1;
  112. fwrite("\x16\x16\x16\x16\x24", 1, 5, fp);
  113. fwrite("\x00\xff\x80\x00\xbf\x3f\xa0\x00\x00", 1, 9, fp);
  114. fwrite(name, 1, len - 4, fp);
  115. fwrite("\x00", 1, 1, fp);
  116. if(img->w != WIDTH || img->h != HEIGHT)
  117. {
  118. tmp = pipi_resize(img, WIDTH, HEIGHT);
  119. p = pipi_getpixels(tmp, PIPI_PIXELS_RGBA_F);
  120. }
  121. else
  122. p = pipi_getpixels(img, PIPI_PIXELS_RGBA_F);
  123. data = p->pixels;
  124. screen = malloc(WIDTH * HEIGHT / 6);
  125. write_screen(data, screen);
  126. if(tmp)
  127. {
  128. free(screen);
  129. pipi_free(tmp);
  130. }
  131. fwrite(screen, 1, WIDTH * HEIGHT / 6, fp);
  132. fclose(fp);
  133. free(screen);
  134. return 0;
  135. }
  136. /*
  137. * XXX: The following functions are local.
  138. */
  139. static int read_screen(char const *name, uint8_t *screen)
  140. {
  141. FILE *fp;
  142. int ch;
  143. fp = fopen(name, "r");
  144. if(!fp)
  145. return -1;
  146. /* Skip the sync bytes */
  147. ch = fgetc(fp);
  148. if(ch != 0x16)
  149. goto syntax_error;
  150. while((ch = fgetc(fp)) == 0x16)
  151. ;
  152. if(ch != 0x24)
  153. goto syntax_error;
  154. /* Skip the header, ignoring the last byte’s value */
  155. if(fgetc(fp) != 0x00 || fgetc(fp) != 0xff || fgetc(fp) != 0x80
  156. || fgetc(fp) != 0x00 || fgetc(fp) != 0xbf || fgetc(fp) != 0x3f
  157. || fgetc(fp) != 0xa0 || fgetc(fp) != 0x00 || fgetc(fp) == EOF)
  158. goto syntax_error;
  159. /* Skip the file name, including trailing nul char */
  160. for(;;)
  161. {
  162. ch = fgetc(fp);
  163. if(ch == EOF)
  164. goto syntax_error;
  165. if(ch == 0x00)
  166. break;
  167. }
  168. /* Read screen data */
  169. if(fread(screen, 1, WIDTH * HEIGHT / 6, fp) != WIDTH * HEIGHT / 6)
  170. goto syntax_error;
  171. fclose(fp);
  172. return 0;
  173. syntax_error:
  174. fclose(fp);
  175. return -1;
  176. }
  177. /* Error diffusion table, similar to Floyd-Steinberg. I choose not to
  178. * propagate 100% of the error, because doing so creates awful artifacts
  179. * (full lines of the same colour, massive colour bleeding) for unclear
  180. * reasons. Atkinson dithering propagates 3/4 of the error, which is even
  181. * less than our 31/32. I also choose to propagate slightly more in the
  182. * X direction to avoid banding effects due to rounding errors.
  183. * It would be interesting, for future versions of this software, to
  184. * propagate the error to the second line, too. But right now I find it far
  185. * too complex to do.
  186. *
  187. * +-------+-------+
  188. * | error |FS0/FSX|
  189. * +-------+-------+-------+
  190. * |FS1/FSX|FS2/FSX|FS3/FSX|
  191. * +-------+-------+-------+
  192. */
  193. #define FS0 15
  194. #define FS1 6
  195. #define FS2 9
  196. #define FS3 1
  197. #define FSX 32
  198. /* The simple Oric RGB palette, made of the 8 Neugebauer primary colours. Each
  199. * colour is repeated 6 times so that we can point to the palette to paste
  200. * whole blocks of 6 pixels. It’s also organised so that palette[7-x] is the
  201. * RGB negative of palette[x], and screen command X uses palette[X & 7]. */
  202. #define o 0x0000
  203. #define X 0xffff
  204. static const int palette[8][6 * 3] =
  205. {
  206. { o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o, o },
  207. { X, o, o, X, o, o, X, o, o, X, o, o, X, o, o, X, o, o },
  208. { o, X, o, o, X, o, o, X, o, o, X, o, o, X, o, o, X, o },
  209. { X, X, o, X, X, o, X, X, o, X, X, o, X, X, o, X, X, o },
  210. { o, o, X, o, o, X, o, o, X, o, o, X, o, o, X, o, o, X },
  211. { X, o, X, X, o, X, X, o, X, X, o, X, X, o, X, X, o, X },
  212. { o, X, X, o, X, X, o, X, X, o, X, X, o, X, X, o, X, X },
  213. { X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X },
  214. };
  215. /* Set new background and foreground colours according to the given command. */
  216. static inline void domove(uint8_t command, uint8_t *bg, uint8_t *fg)
  217. {
  218. if((command & 0x78) == 0x00)
  219. *fg = command & 0x7;
  220. else if((command & 0x78) == 0x10)
  221. *bg = command & 0x7;
  222. }
  223. /* Clamp pixel value to avoid colour bleeding. Deactivated because it
  224. * does not give satisfactory results. */
  225. #define CLAMP 0x1000
  226. static inline int clamp(int p)
  227. {
  228. #if 0
  229. /* FIXME: doesn’t give terribly good results on eg. eatme.png */
  230. if(p < - CLAMP) return - CLAMP;
  231. if(p > 0xffff + CLAMP) return 0xffff + CLAMP;
  232. #endif
  233. return p;
  234. }
  235. /* Compute the perceptual error caused by replacing the input pixels "in"
  236. * with the output pixels "out". "inerr" is the diffused error that should
  237. * be applied to "in"’s first pixel. "outerr" will hold the diffused error
  238. * to apply after "in"’s last pixel upon next call. The return value does
  239. * not mean much physically; it is one part of the algorithm where you need
  240. * to play a bit in order to get appealing results. That’s how image
  241. * processing works, dude. */
  242. static inline int geterror(int const *in, int const *inerr,
  243. int const *out, int *outerr)
  244. {
  245. int tmperr[9 * 3];
  246. int i, c, ret = 0;
  247. /* 9 cells: 1 for the end of line, 8 for the errors below */
  248. memcpy(tmperr, inerr, 3 * sizeof(int));
  249. memset(tmperr + 3, 0, 8 * 3 * sizeof(int));
  250. for(i = 0; i < 6; i++)
  251. {
  252. for(c = 0; c < 3; c++)
  253. {
  254. /* Experiment shows that this is important at small depths */
  255. int a = clamp(in[i * 3 + c] + tmperr[c]);
  256. int b = out[i * 3 + c];
  257. tmperr[c] = (a - b) * FS0 / FSX;
  258. tmperr[c + (i * 3 + 3)] += (a - b) * FS1 / FSX;
  259. tmperr[c + (i * 3 + 6)] += (a - b) * FS2 / FSX;
  260. tmperr[c + (i * 3 + 9)] += (a - b) * FS3 / FSX;
  261. ret += (a - b) / 256 * (a - b) / 256;
  262. }
  263. }
  264. for(i = 0; i < 4; i++)
  265. {
  266. for(c = 0; c < 3; c++)
  267. {
  268. /* Experiment shows that this is important at large depths */
  269. int a = ((in[i * 3 + c] + in[i * 3 + 3 + c]
  270. + in[i * 3 + 6 + c]) / 3);
  271. int b = ((out[i * 3 + c] + out[i * 3 + 3 + c]
  272. + out[i * 3 + 6 + c]) / 3);
  273. ret += (a - b) / 256 * (a - b) / 256;
  274. }
  275. }
  276. /* Using the diffused error as a perceptual error component is stupid,
  277. * because that’s not what it is at all, but I found that it helped a
  278. * bit in some cases. */
  279. for(i = 0; i < 3; i++)
  280. ret += tmperr[i] / 256 * tmperr[i] / 256;
  281. memcpy(outerr, tmperr, 3 * sizeof(int));
  282. return ret;
  283. }
  284. static uint8_t bestmove(int const *in, uint8_t bg, uint8_t fg,
  285. int const *errvec, int depth, int maxerror,
  286. int *error, int *out)
  287. {
  288. int voidvec[3], nvoidvec[3], bestrgb[6 * 3], tmprgb[6 * 3], tmpvec[3];
  289. int const *voidrgb, *nvoidrgb, *vec, *rgb;
  290. int besterror, curerror, suberror, statice, voide, nvoide;
  291. int i, j, c;
  292. uint8_t command, bestcommand;
  293. /* Precompute error for the case where we change the foreground colour
  294. * and hence only print the background colour or its negative */
  295. voidrgb = palette[bg];
  296. voide = geterror(in, errvec, voidrgb, voidvec);
  297. nvoidrgb = palette[7 - bg];
  298. nvoide = geterror(in, errvec, nvoidrgb, nvoidvec);
  299. /* Precompute sub-error for the case where we print pixels (and hence
  300. * don’t change the palette). It’s not the exact error because we should
  301. * be propagating the error to the first pixel here. */
  302. if(depth > 0)
  303. {
  304. int tmp[3] = { 0, 0, 0 };
  305. bestmove(in + 6 * 3, bg, fg, tmp, depth - 1, maxerror, &statice, NULL);
  306. }
  307. /* Check every likely command:
  308. * 0-7: change foreground to 0-7
  309. * 8-15: change foreground to 0-7, print negative background
  310. * 16-23: change background to 0-7
  311. * 24-31: change background to 0-7, print negative background
  312. * 32: normal stuff
  313. * 33: inverse video stuff */
  314. besterror = 0x7ffffff;
  315. bestcommand = 0x10;
  316. memcpy(bestrgb, voidrgb, 6 * 3 * sizeof(int));
  317. for(j = 0; j < 34; j++)
  318. {
  319. static uint8_t const lookup[] =
  320. {
  321. 0x00, 0x04, 0x01, 0x05, 0x02, 0x06, 0x03, 0x07,
  322. 0x80, 0x84, 0x81, 0x85, 0x82, 0x86, 0x83, 0x87,
  323. 0x10, 0x14, 0x11, 0x15, 0x12, 0x16, 0x13, 0x17,
  324. 0x90, 0x94, 0x91, 0x95, 0x92, 0x96, 0x93, 0x97,
  325. 0x40, 0xc0
  326. };
  327. uint8_t newbg = bg, newfg = fg;
  328. command = lookup[j];
  329. domove(command, &newbg, &newfg);
  330. /* Keeping bg and fg is useless, because we could use standard
  331. * pixel printing instead */
  332. if((command & 0x40) == 0x00 && newbg == bg && newfg == fg)
  333. continue;
  334. /* I *think* having newfg == newbg is useless, too, but I don’t
  335. * want to miss some corner case where swapping bg and fg may be
  336. * interesting, so we continue anyway. */
  337. #if 0
  338. /* Bit 6 off and bit 5 on seems illegal */
  339. if((command & 0x60) == 0x20)
  340. continue;
  341. /* Bits 6 and 5 off and bit 3 on seems illegal */
  342. if((command & 0x68) == 0x08)
  343. continue;
  344. #endif
  345. if((command & 0xf8) == 0x00)
  346. {
  347. curerror = voide;
  348. rgb = voidrgb;
  349. vec = voidvec;
  350. }
  351. else if((command & 0xf8) == 0x80)
  352. {
  353. curerror = nvoide;
  354. rgb = nvoidrgb;
  355. vec = nvoidvec;
  356. }
  357. else if((command & 0xf8) == 0x10)
  358. {
  359. rgb = palette[newbg];
  360. curerror = geterror(in, errvec, rgb, tmpvec);
  361. vec = tmpvec;
  362. }
  363. else if((command & 0xf8) == 0x90)
  364. {
  365. rgb = palette[7 - newbg];
  366. curerror = geterror(in, errvec, rgb, tmpvec);
  367. vec = tmpvec;
  368. }
  369. else
  370. {
  371. int const *bgcolor, *fgcolor;
  372. if((command & 0x80) == 0x00)
  373. {
  374. bgcolor = palette[bg]; fgcolor = palette[fg];
  375. }
  376. else
  377. {
  378. bgcolor = palette[7 - bg]; fgcolor = palette[7 - fg];
  379. }
  380. memcpy(tmpvec, errvec, 3 * sizeof(int));
  381. curerror = 0;
  382. for(i = 0; i < 6; i++)
  383. {
  384. int vec1[3], vec2[3];
  385. int smalle1 = 0, smalle2 = 0;
  386. memcpy(vec1, tmpvec, 3 * sizeof(int));
  387. memcpy(vec2, tmpvec, 3 * sizeof(int));
  388. for(c = 0; c < 3; c++)
  389. {
  390. int delta1, delta2;
  391. delta1 = clamp(in[i * 3 + c] + tmpvec[c]) - bgcolor[c];
  392. vec1[c] = delta1 * FS0 / FSX;
  393. smalle1 += delta1 / 256 * delta1;
  394. delta2 = clamp(in[i * 3 + c] + tmpvec[c]) - fgcolor[c];
  395. vec2[c] = delta2 * FS0 / FSX;
  396. smalle2 += delta2 / 256 * delta2;
  397. }
  398. if(smalle1 < smalle2)
  399. {
  400. memcpy(tmpvec, vec1, 3 * sizeof(int));
  401. memcpy(tmprgb + i * 3, bgcolor, 3 * sizeof(int));
  402. }
  403. else
  404. {
  405. memcpy(tmpvec, vec2, 3 * sizeof(int));
  406. memcpy(tmprgb + i * 3, fgcolor, 3 * sizeof(int));
  407. command |= (1 << (5 - i));
  408. }
  409. }
  410. /* Recompute full error */
  411. curerror += geterror(in, errvec, tmprgb, tmpvec);
  412. rgb = tmprgb;
  413. vec = tmpvec;
  414. }
  415. if(curerror > besterror)
  416. continue;
  417. /* Try to avoid bad decisions now that will have a high cost
  418. * later in the line by making the next error more important than
  419. * the current error. */
  420. curerror = curerror * 3 / 4;
  421. if(depth == 0)
  422. suberror = 0; /* It’s the end of the tree */
  423. else if((command & 0x68) == 0x00)
  424. {
  425. bestmove(in + 6 * 3, newbg, newfg, vec, depth - 1,
  426. besterror - curerror, &suberror, NULL);
  427. #if 0
  428. /* Slight penalty for colour changes; they're hard to revert. The
  429. * value of 2 was determined empirically. 1.5 is not enough and
  430. * 3 is too much. */
  431. if(newbg != bg)
  432. suberror = suberror * 10 / 8;
  433. else if(newfg != fg)
  434. suberror = suberror * 9 / 8;
  435. #endif
  436. }
  437. else
  438. suberror = statice;
  439. if(curerror + suberror < besterror)
  440. {
  441. besterror = curerror + suberror;
  442. bestcommand = command;
  443. memcpy(bestrgb, rgb, 6 * 3 * sizeof(int));
  444. }
  445. }
  446. *error = besterror;
  447. if(out)
  448. memcpy(out, bestrgb, 6 * 3 * sizeof(int));
  449. return bestcommand;
  450. }
  451. static void write_screen(float const *data, uint8_t *screen)
  452. {
  453. int *src, *srcl, *dst, *dstl;
  454. int stride, x, y, depth, c;
  455. stride = (WIDTH + 1) * 3;
  456. src = malloc((WIDTH + 1) * (HEIGHT + 1) * 3 * sizeof(int));
  457. memset(src, 0, (WIDTH + 1) * (HEIGHT + 1) * 3 * sizeof(int));
  458. dst = malloc((WIDTH + 1) * (HEIGHT + 1) * 3 * sizeof(int));
  459. memset(dst, 0, (WIDTH + 1) * (HEIGHT + 1) * 3 * sizeof(int));
  460. /* Import pixels into our custom format */
  461. for(y = 0; y < HEIGHT; y++)
  462. for(x = 0; x < WIDTH; x++)
  463. for(c = 0; c < 3; c++)
  464. src[y * stride + x * 3 + c] =
  465. 0xffff * data[(y * WIDTH + x) * 4 + (2 - c)];
  466. /* Let the fun begin */
  467. for(y = 0; y < HEIGHT; y++)
  468. {
  469. uint8_t bg = 0, fg = 7;
  470. //fprintf(stderr, "\rProcessing... %i%%", (y * 100 + 99) / HEIGHT);
  471. for(x = 0; x < WIDTH; x += 6)
  472. {
  473. int errvec[3] = { 0, 0, 0 };
  474. int dummy, i;
  475. uint8_t command;
  476. depth = (x + DEPTH < WIDTH) ? DEPTH : (WIDTH - x) / 6 - 1;
  477. srcl = src + y * stride + x * 3;
  478. dstl = dst + y * stride + x * 3;
  479. /* Recursively compute and apply best command */
  480. command = bestmove(srcl, bg, fg, errvec, depth, 0x7fffff,
  481. &dummy, dstl);
  482. /* Propagate error */
  483. for(c = 0; c < 3; c++)
  484. {
  485. for(i = 0; i < 6; i++)
  486. {
  487. int error = srcl[i * 3 + c] - dstl[i * 3 + c];
  488. srcl[i * 3 + c + 3] =
  489. clamp(srcl[i * 3 + c + 3] + error * FS0 / FSX);
  490. srcl[i * 3 + c + stride - 3] += error * FS1 / FSX;
  491. srcl[i * 3 + c + stride] += error * FS2 / FSX;
  492. srcl[i * 3 + c + stride + 3] += error * FS3 / FSX;
  493. }
  494. for(i = -1; i < 7; i++)
  495. srcl[i * 3 + c + stride] = clamp(srcl[i * 3 + c + stride]);
  496. }
  497. /* Iterate */
  498. domove(command, &bg, &fg);
  499. /* Write byte to file */
  500. screen[y * (WIDTH / 6) + (x / 6)] = command;
  501. }
  502. }
  503. //fprintf(stderr, " done.\n");
  504. }