ucmd_view.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* view: image viewer built on the img library and the ngui toolkit.
  2. *
  3. * Usage: view <file>. The command owns a canvas window, reads the whole file
  4. * through the kernel file service, decodes it into a per-process heap
  5. * (SYS_MEM_ALLOC), and draws it with fit/1:1/zoom/pan support. */
  6. #include "ucmd_lib.h"
  7. #include "ngui.h"
  8. #include "img.h"
  9. #include "img_file.h"
  10. #define VIEW_FILE_CAP (IMG_FILE_MAX + 1u)
  11. #define VIEW_INIT_HEAP (VIEW_FILE_CAP + 64u * 1024u)
  12. #define VIEW_HEAP_MAX (32u * 1024u * 1024u)
  13. #define VIEW_TOOLBAR_H 28
  14. #define VIEW_STATUS_H 18
  15. #define VIEW_BAR_H (VIEW_TOOLBAR_H + VIEW_STATUS_H)
  16. #define VIEW_ID_FIT 1
  17. #define VIEW_ID_ONE 2
  18. #define VIEW_ID_OUT 3
  19. #define VIEW_ID_IN 4
  20. #define VIEW_BG NGUI_RGB(24, 26, 32)
  21. #define VIEW_VIEW_BG NGUI_RGB(16, 17, 21)
  22. #define VIEW_CHK1 NGUI_RGB(46, 48, 54)
  23. #define VIEW_CHK2 NGUI_RGB(58, 60, 67)
  24. struct viewer {
  25. struct ngui ui;
  26. struct img_image img;
  27. struct img_mem mem;
  28. unsigned char *heap;
  29. unsigned int heap_size;
  30. unsigned int file_size;
  31. unsigned int iw, ih;
  32. unsigned int format;
  33. unsigned int zoom; /* 16.16 when fit == 0 */
  34. int fit;
  35. int off_x, off_y; /* destination top-left inside the viewport */
  36. int dragging;
  37. int drag_x, drag_y;
  38. int have_img;
  39. int loading;
  40. int error;
  41. int last_w, last_h;
  42. char err[64];
  43. char name[20];
  44. char title[20];
  45. int dirty;
  46. };
  47. static struct viewer v;
  48. static unsigned int s_len(const char *s)
  49. {
  50. unsigned int n = 0;
  51. while (s && s[n])
  52. n++;
  53. return n;
  54. }
  55. static void s_copy(char *d, const char *s, unsigned int cap)
  56. {
  57. unsigned int i = 0;
  58. if (!cap)
  59. return;
  60. while (s && s[i] && i + 1 < cap) {
  61. d[i] = s[i];
  62. i++;
  63. }
  64. d[i] = '\0';
  65. }
  66. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  67. {
  68. while (s && *s && *n + 1 < cap)
  69. d[(*n)++] = *s++;
  70. d[*n] = '\0';
  71. }
  72. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  73. unsigned int cap)
  74. {
  75. char tmp[12];
  76. unsigned int i = 0, j;
  77. do {
  78. tmp[i++] = (char)('0' + v % 10);
  79. v /= 10;
  80. } while (v && i < sizeof(tmp));
  81. for (j = 0; j < i && *n + 1 < cap; j++)
  82. d[(*n)++] = tmp[i - 1 - j];
  83. d[*n] = '\0';
  84. }
  85. static void base_name(const char *path, char *out, unsigned int cap)
  86. {
  87. unsigned int n = 0, i, start = 0;
  88. if (!path)
  89. path = "";
  90. for (i = 0; path[i]; i++)
  91. if (path[i] == '/')
  92. start = i + 1;
  93. for (i = start; path[i] && n + 1 < cap; i++)
  94. out[n++] = path[i];
  95. out[n] = '\0';
  96. if (!n)
  97. s_copy(out, "image", cap);
  98. }
  99. struct ngui_rect view_body(struct ngui *ui)
  100. {
  101. struct ngui_rect r = {0, 0, ui->w, ui->h};
  102. struct ngui_rect top, bottom;
  103. top = ngui_cut_top(&r, ui->h >= 140 ? VIEW_TOOLBAR_H : 22);
  104. bottom = ngui_cut_bottom(&r, ui->h >= 140 ? VIEW_STATUS_H : 14);
  105. (void)top;
  106. (void)bottom;
  107. return r;
  108. }
  109. static unsigned int view_scale(struct ngui *ui)
  110. {
  111. struct ngui_rect b = view_body(ui);
  112. unsigned int s;
  113. if (!v.have_img || b.w <= 0 || b.h <= 0)
  114. return 65536;
  115. if (!v.fit)
  116. return v.zoom ? v.zoom : 65536;
  117. s = img_fit_scale(v.iw, v.ih, (unsigned int)b.w, (unsigned int)b.h);
  118. if (s > 65536)
  119. s = 65536; /* don't enlarge a smaller image */
  120. return s ? s : 256;
  121. }
  122. static void clamp_view(struct ngui *ui, unsigned int scale)
  123. {
  124. struct ngui_rect b = view_body(ui);
  125. unsigned int sw, sh;
  126. if (!v.have_img) {
  127. v.off_x = v.off_y = 0;
  128. return;
  129. }
  130. sw = (unsigned int)(((unsigned long long)v.iw * scale) >> 16);
  131. sh = (unsigned int)(((unsigned long long)v.ih * scale) >> 16);
  132. if (sw < 1) sw = 1;
  133. if (sh < 1) sh = 1;
  134. if (sw <= (unsigned int)b.w)
  135. v.off_x = ((int)b.w - (int)sw) / 2;
  136. else {
  137. if (v.off_x > 0) v.off_x = 0;
  138. if (v.off_x < (int)b.w - (int)sw)
  139. v.off_x = (int)b.w - (int)sw;
  140. }
  141. if (sh <= (unsigned int)b.h)
  142. v.off_y = ((int)b.h - (int)sh) / 2;
  143. else {
  144. if (v.off_y > 0) v.off_y = 0;
  145. if (v.off_y < (int)b.h - (int)sh)
  146. v.off_y = (int)b.h - (int)sh;
  147. }
  148. if (v.fit) {
  149. v.off_x = ((int)b.w - (int)sw) / 2;
  150. v.off_y = ((int)b.h - (int)sh) / 2;
  151. }
  152. }
  153. static void view_set_scale(struct ngui *ui, unsigned int scale)
  154. {
  155. struct ngui_rect b = view_body(ui);
  156. unsigned int old = view_scale(ui);
  157. unsigned int sx, sy;
  158. int dx, dy;
  159. if (!v.have_img || b.w <= 0 || b.h <= 0)
  160. return;
  161. if (scale < 256) scale = 256;
  162. if (scale > 8u * 65536u) scale = 8u * 65536u;
  163. dx = b.w / 2 - v.off_x;
  164. dy = b.h / 2 - v.off_y;
  165. sx = ((unsigned int)dx << 16) / old;
  166. sy = ((unsigned int)dy << 16) / old;
  167. v.fit = 0;
  168. v.zoom = scale;
  169. v.off_x = (int)(b.w / 2 - (int)(((unsigned long long)sx * scale) >> 16));
  170. v.off_y = (int)(b.h / 2 - (int)(((unsigned long long)sy * scale) >> 16));
  171. clamp_view(ui, scale);
  172. }
  173. static void view_fit(void)
  174. {
  175. v.fit = 1;
  176. }
  177. static void view_one(struct ngui *ui)
  178. {
  179. struct ngui_rect b = view_body(ui);
  180. unsigned int sw;
  181. v.fit = 0;
  182. v.zoom = 65536;
  183. sw = v.iw;
  184. v.off_x = ((int)b.w - (int)sw) / 2;
  185. v.off_y = ((int)b.h - (int)v.ih) / 2;
  186. clamp_view(ui, v.zoom);
  187. }
  188. static void view_zoom_step(struct ngui *ui, int in)
  189. {
  190. unsigned int s = view_scale(ui);
  191. if (in)
  192. s = s + s / 4;
  193. else
  194. s = s - s / 5;
  195. view_set_scale(ui, s);
  196. }
  197. static void format_status(char *buf, unsigned int cap)
  198. {
  199. unsigned int n = 0;
  200. buf[0] = '\0';
  201. if (v.have_img) {
  202. unsigned int scale = view_scale(&v.ui);
  203. s_append(buf, &n, img_format_name(v.format), cap);
  204. s_append(buf, &n, " ", cap);
  205. s_append_uint(buf, &n, v.iw, cap);
  206. s_append(buf, &n, "x", cap);
  207. s_append_uint(buf, &n, v.ih, cap);
  208. s_append(buf, &n, " ", cap);
  209. if (v.file_size >= 1024 * 1024) {
  210. s_append_uint(buf, &n, v.file_size / (1024 * 1024), cap);
  211. s_append(buf, &n, " MB", cap);
  212. } else {
  213. s_append_uint(buf, &n, (v.file_size + 1023) / 1024, cap);
  214. s_append(buf, &n, " KB", cap);
  215. }
  216. s_append(buf, &n, " ", cap);
  217. if (v.fit)
  218. s_append(buf, &n, "fit ", cap);
  219. s_append_uint(buf, &n, (scale * 100u) >> 16, cap);
  220. s_append(buf, &n, "%", cap);
  221. } else {
  222. s_append(buf, &n, "view ", cap);
  223. s_append(buf, &n, v.name, cap);
  224. }
  225. }
  226. static void draw_message(struct ngui *ui, struct ngui_rect body,
  227. const char *msg)
  228. {
  229. int tw = (int)ngui_text_width(msg);
  230. int x = body.x + (body.w - tw) / 2;
  231. int y = body.y + body.h / 2 - 8;
  232. if (x < body.x + 6)
  233. x = body.x + 6;
  234. ngui_text(ui, x, y, msg, ui->theme.text);
  235. if (!v.loading) {
  236. const char *hint = "Esc closes";
  237. int hw = (int)ngui_text_width(hint);
  238. ngui_text(ui, body.x + (body.w - hw) / 2, y + 24, hint,
  239. ui->theme.text_dim);
  240. }
  241. }
  242. static void draw_toolbar(struct ngui *ui, struct ngui_rect r)
  243. {
  244. struct ngui_rect b;
  245. int x = r.x + 6;
  246. int by = r.y + (r.h - 20) / 2;
  247. int bw;
  248. ngui_fill_rect(ui, r, ui->theme.panel);
  249. ngui_fill_rect(ui, (struct ngui_rect){r.x, r.y + r.h - 1, r.w, 1},
  250. ui->theme.border);
  251. bw = 40;
  252. b = (struct ngui_rect){x, by, bw, 20};
  253. if (ngui_button(ui, VIEW_ID_FIT, b, "Fit")) {
  254. view_fit();
  255. v.dirty = 1;
  256. }
  257. x += bw + 5;
  258. b = (struct ngui_rect){x, by, 40, 20};
  259. if (ngui_button(ui, VIEW_ID_ONE, b, "1:1")) {
  260. view_one(ui);
  261. v.dirty = 1;
  262. }
  263. x += 45;
  264. b = (struct ngui_rect){x, by, 24, 20};
  265. if (ngui_button(ui, VIEW_ID_OUT, b, "-")) {
  266. view_zoom_step(ui, 0);
  267. v.dirty = 1;
  268. }
  269. x += 29;
  270. b = (struct ngui_rect){x, by, 24, 20};
  271. if (ngui_button(ui, VIEW_ID_IN, b, "+")) {
  272. view_zoom_step(ui, 1);
  273. v.dirty = 1;
  274. }
  275. x += 32;
  276. if (v.have_img && x + 70 < r.x + r.w) {
  277. char z[16];
  278. unsigned int n = 0;
  279. z[0] = '\0';
  280. s_append_uint(z, &n, (view_scale(ui) * 100u) >> 16, sizeof(z));
  281. s_append(z, &n, "%", sizeof(z));
  282. ngui_text(ui, x, r.y + (r.h - 16) / 2, z, ui->theme.text_dim);
  283. }
  284. }
  285. static void draw(struct ngui *ui)
  286. {
  287. struct ngui_rect all = {0, 0, ui->w, ui->h};
  288. struct ngui_rect r = all;
  289. struct ngui_rect toolbar, status, body;
  290. char line[96];
  291. toolbar = ngui_cut_top(&r, ui->h >= 140 ? VIEW_TOOLBAR_H : 22);
  292. status = ngui_cut_bottom(&r, ui->h >= 140 ? VIEW_STATUS_H : 14);
  293. body = r;
  294. ngui_fill_rect(ui, all, VIEW_BG);
  295. draw_toolbar(ui, toolbar);
  296. ngui_fill_rect(ui, status, ui->theme.panel);
  297. format_status(line, sizeof(line));
  298. ngui_text_ellipsis(ui, status, status.x + 6,
  299. status.y + (status.h - 16) / 2, line,
  300. ui->theme.text_dim);
  301. if (body.w <= 0 || body.h <= 0)
  302. return;
  303. ngui_fill_rect(ui, body, VIEW_VIEW_BG);
  304. if (!v.have_img) {
  305. if (v.error)
  306. draw_message(ui, body, v.err);
  307. else {
  308. char msg[48];
  309. s_copy(msg, "Loading ", sizeof(msg));
  310. {
  311. unsigned int n = s_len(msg);
  312. s_append(msg, &n, v.name, sizeof(msg));
  313. s_append(msg, &n, "...", sizeof(msg));
  314. }
  315. draw_message(ui, body, msg);
  316. }
  317. return;
  318. }
  319. {
  320. unsigned int scale = view_scale(ui);
  321. unsigned int sw =
  322. (unsigned int)(((unsigned long long)v.iw * scale) >> 16);
  323. unsigned int sh =
  324. (unsigned int)(((unsigned long long)v.ih * scale) >> 16);
  325. struct img_blit blit;
  326. if (!sw) sw = 1;
  327. if (!sh) sh = 1;
  328. clamp_view(ui, scale);
  329. blit.dst = ui->px;
  330. blit.dst_w = ui->w;
  331. blit.dst_h = ui->h;
  332. blit.clip_x = body.x;
  333. blit.clip_y = body.y;
  334. blit.clip_w = body.w;
  335. blit.clip_h = body.h;
  336. blit.x = body.x + v.off_x;
  337. blit.y = body.y + v.off_y;
  338. blit.scale = scale;
  339. blit.bg = VIEW_CHK1;
  340. blit.bg2 = VIEW_CHK2;
  341. blit.flags = IMG_BLIT_CHECKER;
  342. if (v.dragging)
  343. blit.flags |= IMG_BLIT_FAST;
  344. if (v.img.alpha)
  345. img_draw(&v.img, &blit);
  346. else {
  347. blit.flags &= ~IMG_BLIT_CHECKER;
  348. img_draw(&v.img, &blit);
  349. }
  350. (void)sw;
  351. (void)sh;
  352. }
  353. }
  354. static int create_window(unsigned int base_w, unsigned int base_h,
  355. const char *title, struct win_info *wi)
  356. {
  357. unsigned int w = base_w, h = base_h;
  358. if (w < 320) w = 320;
  359. if (h < 200) h = 200;
  360. if (w > 960) w = 960;
  361. if (h > 640) h = 640;
  362. for (;;) {
  363. if (u_win_create(w, h + VIEW_BAR_H, title, wi) == 0)
  364. return 0;
  365. if (w <= 240 && h <= 160)
  366. break;
  367. w = w * 4 / 5;
  368. h = h * 4 / 5;
  369. if (w < 240) w = 240;
  370. if (h < 160) h = 160;
  371. }
  372. return -1;
  373. }
  374. static void error_exit(const char *msg)
  375. {
  376. struct win_info wi;
  377. struct ngui ui;
  378. int quit = 0;
  379. if (u_win_create(480, 240 + VIEW_BAR_H, "view", &wi) != 0) {
  380. u_puts("view: ");
  381. u_puts(msg);
  382. u_puts("\n");
  383. u_exit(1);
  384. }
  385. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  386. v.error = 1;
  387. s_copy(v.err, msg, sizeof(v.err));
  388. v.have_img = 0;
  389. for (;;) {
  390. if (!ngui_begin(&ui)) {
  391. u_sleep_ms(10);
  392. continue;
  393. }
  394. {
  395. int k = ngui_key(&ui);
  396. if (k == NGUI_KEY_ESC || k == 'q' || k == 0x03)
  397. quit = 1;
  398. }
  399. draw(&ui);
  400. ngui_end(&ui);
  401. if (quit)
  402. break;
  403. u_sleep_ms(10);
  404. }
  405. u_win_close();
  406. u_exit(1);
  407. }
  408. void _start(const char *arg)
  409. {
  410. struct img_info info;
  411. struct win_info wi;
  412. struct img_options opt;
  413. unsigned int n, arena_off, heap_size;
  414. int r;
  415. if (!arg || !arg[0]) {
  416. u_puts("usage: view <file>\n");
  417. u_exit(1);
  418. }
  419. base_name(arg, v.name, sizeof(v.name));
  420. s_copy(v.title, "view ", sizeof(v.title));
  421. {
  422. unsigned int tn = s_len(v.title);
  423. s_append(v.title, &tn, v.name, sizeof(v.title));
  424. }
  425. v.heap = (unsigned char *)u_mem_alloc(VIEW_INIT_HEAP);
  426. if (!v.heap)
  427. error_exit("Not enough memory for the image");
  428. heap_size = VIEW_INIT_HEAP;
  429. n = (unsigned int)img_read_file(arg, v.heap, VIEW_FILE_CAP);
  430. if ((int)n < 0) {
  431. if ((int)n == IMG_ERR_TOOBIG)
  432. error_exit("Image file is larger than 4 MB");
  433. error_exit(img_error_string((int)n));
  434. }
  435. v.file_size = n;
  436. r = img_sniff(v.heap, n, &info);
  437. if (r != 0)
  438. error_exit(img_error_string(r));
  439. arena_off = (n + 15u) & ~15u;
  440. {
  441. unsigned int est = img_estimate(&info);
  442. unsigned long long need = (unsigned long long)arena_off + est;
  443. if (need > VIEW_HEAP_MAX)
  444. error_exit("Image needs more than 32 MB");
  445. if (need > heap_size) {
  446. if (u_mem_grow((unsigned int)need) != 0)
  447. error_exit("Not enough memory for the image");
  448. heap_size = (unsigned int)need;
  449. }
  450. }
  451. if (heap_size <= arena_off)
  452. error_exit("Not enough memory for the image");
  453. img_mem_init(&v.mem, v.heap + arena_off, heap_size - arena_off);
  454. v.iw = info.width;
  455. v.ih = info.height;
  456. v.format = info.format;
  457. if (create_window(info.width, info.height, v.title, &wi) != 0) {
  458. u_puts("view: no graphics window\n");
  459. u_exit(1);
  460. }
  461. ngui_init(&v.ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  462. v.fit = 1;
  463. v.zoom = 65536;
  464. v.loading = 1;
  465. v.dirty = 1;
  466. v.last_w = (int)wi.width;
  467. v.last_h = (int)wi.height;
  468. {
  469. struct ngui *ui = &v.ui;
  470. /* Draw the loading frame once; the input loop starts after decode. */
  471. ngui_begin(ui);
  472. draw(ui);
  473. ngui_end(ui);
  474. }
  475. opt.max_pixels = 0;
  476. r = img_decode(v.heap, n, &opt, &v.mem, &v.img);
  477. v.loading = 0;
  478. if (r != 0) {
  479. s_copy(v.err, img_error_string(r), sizeof(v.err));
  480. v.error = 1;
  481. v.have_img = 0;
  482. } else {
  483. v.have_img = 1;
  484. v.error = 0;
  485. v.iw = v.img.w;
  486. v.ih = v.img.h;
  487. v.format = v.img.format;
  488. v.fit = 1;
  489. }
  490. v.dirty = 1;
  491. for (;;) {
  492. struct ngui *ui = &v.ui;
  493. struct ngui_rect body = view_body(ui);
  494. int close = 0;
  495. int k;
  496. ngui_begin(ui);
  497. if (ui->w != v.last_w || ui->h != v.last_h) {
  498. /* size changed; redraw at the new dimensions */
  499. v.last_w = ui->w;
  500. v.last_h = ui->h;
  501. v.dirty = 1;
  502. }
  503. while ((k = ngui_key(ui)) != 0) {
  504. if (k == NGUI_KEY_ESC || k == 'q' || k == 0x03) {
  505. close = 1;
  506. break;
  507. } else if (k == 'f' || k == '0') {
  508. view_fit();
  509. v.dirty = 1;
  510. } else if (k == '1') {
  511. view_one(ui);
  512. v.dirty = 1;
  513. } else if (k == '+' || k == '=') {
  514. view_zoom_step(ui, 1);
  515. v.dirty = 1;
  516. } else if (k == '-' || k == '_') {
  517. view_zoom_step(ui, 0);
  518. v.dirty = 1;
  519. } else if (k == NGUI_KEY_LEFT || k == NGUI_KEY_RIGHT ||
  520. k == NGUI_KEY_UP || k == NGUI_KEY_DOWN) {
  521. if (v.have_img) {
  522. if (v.fit)
  523. view_set_scale(ui, view_scale(ui));
  524. if (k == NGUI_KEY_LEFT)
  525. v.off_x += 32;
  526. if (k == NGUI_KEY_RIGHT)
  527. v.off_x -= 32;
  528. if (k == NGUI_KEY_UP)
  529. v.off_y += 32;
  530. if (k == NGUI_KEY_DOWN)
  531. v.off_y -= 32;
  532. clamp_view(ui, view_scale(ui));
  533. v.dirty = 1;
  534. }
  535. }
  536. }
  537. if (close)
  538. break;
  539. if (ui->pressed && ui->press_x >= body.x && ui->press_y >= body.y &&
  540. ui->press_x < body.x + body.w && ui->press_y < body.y + body.h &&
  541. v.have_img) {
  542. v.dragging = 1;
  543. v.drag_x = ui->mouse_x;
  544. v.drag_y = ui->mouse_y;
  545. v.dirty = 1;
  546. }
  547. if (v.dragging) {
  548. if (!(ui->buttons & 1) || ui->released) {
  549. v.dragging = 0;
  550. v.dirty = 1;
  551. } else {
  552. int dx = ui->mouse_x - v.drag_x;
  553. int dy = ui->mouse_y - v.drag_y;
  554. if (dx || dy) {
  555. v.off_x += dx;
  556. v.off_y += dy;
  557. v.drag_x = ui->mouse_x;
  558. v.drag_y = ui->mouse_y;
  559. clamp_view(ui, view_scale(ui));
  560. v.dirty = 1;
  561. }
  562. }
  563. }
  564. if (ui->pressed || ui->released)
  565. v.dirty = 1;
  566. if (v.dirty) {
  567. v.dirty = 0;
  568. draw(ui);
  569. ngui_end_ex(ui, 1);
  570. } else {
  571. ngui_end_ex(ui, 0);
  572. }
  573. u_sleep_ms(10);
  574. }
  575. u_win_close();
  576. u_exit(0);
  577. }