25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. 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. }