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.
 
 
 
 
 
 

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