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.
 
 
 
 
 
 

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