Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

cacaview.c 20 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * cacaview image viewer for libcaca
  3. * Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #if defined(HAVE_IMLIB2_H)
  28. # include <Imlib2.h>
  29. #else
  30. # include <stdio.h>
  31. #endif
  32. #if defined(HAVE_SLEEP)
  33. # include <windows.h>
  34. #endif
  35. #include "caca.h"
  36. /* Local macros */
  37. #define STATUS_DITHERING 1
  38. #define STATUS_ANTIALIASING 2
  39. #define STATUS_BACKGROUND 3
  40. #define ZOOM_FACTOR 1.08f
  41. #define ZOOM_MAX 50
  42. #define PAD_STEP 0.15
  43. /* Local functions */
  44. static void set_zoom(int);
  45. static void load_image(char const *);
  46. static void unload_image(void);
  47. static void draw_checkers(int, int, int, int);
  48. #if !defined(HAVE_IMLIB2_H)
  49. static int freadint(FILE *);
  50. static int freadshort(FILE *);
  51. static int freadchar(FILE *);
  52. #endif
  53. /* Local variables */
  54. #if defined(HAVE_IMLIB2_H)
  55. Imlib_Image image = NULL;
  56. #endif
  57. char *pixels = NULL;
  58. struct caca_bitmap *bitmap = NULL;
  59. unsigned int w, h, depth, bpp, rmask, gmask, bmask, amask;
  60. #if !defined(HAVE_IMLIB2_H)
  61. unsigned int red[256], green[256], blue[256], alpha[256];
  62. #endif
  63. float zoomtab[ZOOM_MAX + 1];
  64. float xfactor = 1.0, yfactor = 1.0, dx = 0.5, dy = 0.5;
  65. int zoom = 0, fullscreen = 0, ww, wh;
  66. int main(int argc, char **argv)
  67. {
  68. int quit = 0, update = 1, help = 0, status = 0;
  69. int reload = 0;
  70. char **list = NULL;
  71. int current = 0, items = 0, opts = 1;
  72. int i;
  73. /* Initialise libcaca */
  74. if(caca_init())
  75. {
  76. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  77. return 1;
  78. }
  79. /* Set the window title */
  80. caca_set_window_title("cacaview");
  81. ww = caca_get_width();
  82. wh = caca_get_height();
  83. /* Fill the zoom table */
  84. zoomtab[0] = 1.0;
  85. for(i = 0; i < ZOOM_MAX; i++)
  86. zoomtab[i + 1] = zoomtab[i] * 1.08;
  87. /* Load items into playlist */
  88. for(i = 1; i < argc; i++)
  89. {
  90. /* Skip options except after `--' */
  91. if(opts && argv[i][0] == '-')
  92. {
  93. if(argv[i][1] == '-' && argv[i][2] == '\0')
  94. opts = 0;
  95. continue;
  96. }
  97. /* Add argv[i] to the list */
  98. if(items)
  99. list = realloc(list, (items + 1) * sizeof(char *));
  100. else
  101. list = malloc(sizeof(char *));
  102. list[items] = argv[i];
  103. items++;
  104. reload = 1;
  105. }
  106. /* Go ! */
  107. while(!quit)
  108. {
  109. unsigned int const event_mask = CACA_EVENT_KEY_PRESS
  110. | CACA_EVENT_RESIZE
  111. | CACA_EVENT_MOUSE_PRESS;
  112. unsigned int event, new_status = 0, new_help = 0;
  113. if(update)
  114. event = caca_get_event(event_mask);
  115. else
  116. event = caca_wait_event(event_mask);
  117. while(event)
  118. {
  119. if(event & CACA_EVENT_MOUSE_PRESS)
  120. {
  121. if((event & 0x00ffffff) == 1)
  122. {
  123. if(items) current = (current + 1) % items;
  124. reload = 1;
  125. }
  126. else if((event & 0x00ffffff) == 2)
  127. {
  128. if(items) current = (items + current - 1) % items;
  129. reload = 1;
  130. }
  131. }
  132. else if(event & CACA_EVENT_KEY_PRESS) switch(event & 0x00ffffff)
  133. {
  134. case 'n':
  135. case 'N':
  136. if(items) current = (current + 1) % items;
  137. reload = 1;
  138. break;
  139. case 'p':
  140. case 'P':
  141. if(items) current = (items + current - 1) % items;
  142. reload = 1;
  143. break;
  144. case 'f':
  145. case 'F':
  146. fullscreen = ~fullscreen;
  147. update = 1;
  148. set_zoom(zoom);
  149. break;
  150. case 'b':
  151. i = 1 + caca_get_feature(CACA_BACKGROUND);
  152. if(i > CACA_BACKGROUND_MAX) i = CACA_BACKGROUND_MIN;
  153. caca_set_feature(i);
  154. new_status = STATUS_BACKGROUND;
  155. update = 1;
  156. break;
  157. case 'B':
  158. i = -1 + caca_get_feature(CACA_BACKGROUND);
  159. if(i < CACA_BACKGROUND_MIN) i = CACA_BACKGROUND_MAX;
  160. caca_set_feature(i);
  161. new_status = STATUS_BACKGROUND;
  162. update = 1;
  163. break;
  164. case 'a':
  165. i = 1 + caca_get_feature(CACA_ANTIALIASING);
  166. if(i > CACA_ANTIALIASING_MAX) i = CACA_ANTIALIASING_MIN;
  167. caca_set_feature(i);
  168. new_status = STATUS_ANTIALIASING;
  169. update = 1;
  170. break;
  171. case 'A':
  172. i = -1 + caca_get_feature(CACA_ANTIALIASING);
  173. if(i < CACA_ANTIALIASING_MIN) i = CACA_ANTIALIASING_MAX;
  174. caca_set_feature(i);
  175. new_status = STATUS_ANTIALIASING;
  176. update = 1;
  177. break;
  178. case 'd':
  179. i = 1 + caca_get_feature(CACA_DITHERING);
  180. if(i > CACA_DITHERING_MAX) i = CACA_DITHERING_MIN;
  181. caca_set_feature(i);
  182. new_status = STATUS_DITHERING;
  183. update = 1;
  184. break;
  185. case 'D':
  186. i = -1 + caca_get_feature(CACA_DITHERING);
  187. if(i < CACA_DITHERING_MIN) i = CACA_DITHERING_MAX;
  188. caca_set_feature(i);
  189. new_status = STATUS_DITHERING;
  190. update = 1;
  191. break;
  192. case '+':
  193. update = 1;
  194. set_zoom(zoom + 1);
  195. break;
  196. case '-':
  197. update = 1;
  198. set_zoom(zoom - 1);
  199. break;
  200. case 'x':
  201. case 'X':
  202. update = 1;
  203. set_zoom(0);
  204. break;
  205. case 'k':
  206. case 'K':
  207. case CACA_KEY_UP:
  208. if(yfactor > 1.0) dy -= PAD_STEP / yfactor;
  209. if(dy < 0.0) dy = 0.0;
  210. update = 1;
  211. break;
  212. case 'j':
  213. case 'J':
  214. case CACA_KEY_DOWN:
  215. if(yfactor > 1.0) dy += PAD_STEP / yfactor;
  216. if(dy > 1.0) dy = 1.0;
  217. update = 1;
  218. break;
  219. case 'h':
  220. case 'H':
  221. case CACA_KEY_LEFT:
  222. if(xfactor > 1.0) dx -= PAD_STEP / xfactor;
  223. if(dx < 0.0) dx = 0.0;
  224. update = 1;
  225. break;
  226. case 'l':
  227. case 'L':
  228. case CACA_KEY_RIGHT:
  229. if(xfactor > 1.0) dx += PAD_STEP / xfactor;
  230. if(dx > 1.0) dx = 1.0;
  231. update = 1;
  232. break;
  233. case '?':
  234. new_help = !help;
  235. update = 1;
  236. break;
  237. case 'q':
  238. case 'Q':
  239. quit = 1;
  240. break;
  241. }
  242. else if(event == CACA_EVENT_RESIZE)
  243. {
  244. caca_refresh();
  245. ww = caca_get_width();
  246. wh = caca_get_height();
  247. update = 1;
  248. set_zoom(zoom);
  249. }
  250. if(status || new_status)
  251. status = new_status;
  252. if(help || new_help)
  253. help = new_help;
  254. event = caca_get_event(CACA_EVENT_KEY_PRESS);
  255. }
  256. if(items && reload)
  257. {
  258. char *buffer;
  259. int len = strlen(" Loading `%s'... ") + strlen(list[current]);
  260. if(len < ww + 1)
  261. len = ww + 1;
  262. buffer = malloc(len);
  263. sprintf(buffer, " Loading `%s'... ", list[current]);
  264. buffer[ww] = '\0';
  265. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  266. caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer);
  267. caca_refresh();
  268. ww = caca_get_width();
  269. wh = caca_get_height();
  270. unload_image();
  271. load_image(list[current]);
  272. reload = 0;
  273. /* Reset image-specific runtime variables */
  274. dx = dy = 0.5;
  275. update = 1;
  276. set_zoom(0);
  277. free(buffer);
  278. }
  279. caca_clear();
  280. if(!items)
  281. {
  282. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  283. caca_printf(ww / 2 - 5, wh / 2, " No image. ");
  284. }
  285. else if(!pixels)
  286. {
  287. #if defined(HAVE_IMLIB2_H)
  288. # define ERROR_STRING " Error loading `%s'. "
  289. #else
  290. # define ERROR_STRING " Error loading `%s'. Only BMP is supported. "
  291. #endif
  292. char *buffer;
  293. int len = strlen(ERROR_STRING) + strlen(list[current]);
  294. if(len < ww + 1)
  295. len = ww + 1;
  296. buffer = malloc(len);
  297. sprintf(buffer, ERROR_STRING, list[current]);
  298. buffer[ww] = '\0';
  299. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  300. caca_putstr((ww - strlen(buffer)) / 2, wh / 2, buffer);
  301. free(buffer);
  302. }
  303. else
  304. {
  305. float xdelta, ydelta;
  306. int y, height;
  307. y = fullscreen ? 0 : 1;
  308. height = fullscreen ? wh : wh - 3;
  309. xdelta = (xfactor > 1.0) ? dx : 0.5;
  310. ydelta = (yfactor > 1.0) ? dy : 0.5;
  311. draw_checkers(ww * (1.0 - xfactor) / 2,
  312. y + height * (1.0 - yfactor) / 2,
  313. ww * (1.0 + xfactor) / 2,
  314. y + height * (1.0 + yfactor) / 2);
  315. caca_draw_bitmap(ww * (1.0 - xfactor) * xdelta,
  316. y + height * (1.0 - yfactor) * ydelta,
  317. ww * (xdelta + (1.0 - xdelta) * xfactor),
  318. y + height * (ydelta + (1.0 - ydelta) * yfactor),
  319. bitmap, pixels);
  320. }
  321. if(!fullscreen)
  322. {
  323. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  324. caca_draw_line(0, 0, ww - 1, 0, ' ');
  325. caca_draw_line(0, wh - 2, ww - 1, wh - 2, '-');
  326. caca_putstr(0, 0, "q:Quit np:Next/Prev +-x:Zoom "
  327. "hjkl:Move d:Dithering a:Antialias");
  328. caca_putstr(ww - strlen("?:Help"), 0, "?:Help");
  329. caca_printf(3, wh - 2, "cacaview %s", VERSION);
  330. caca_printf(ww - 14, wh - 2,
  331. "(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);
  332. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
  333. caca_draw_line(0, wh - 1, ww - 1, wh - 1, ' ');
  334. switch(status)
  335. {
  336. case STATUS_ANTIALIASING:
  337. caca_printf(0, wh - 1, "Antialiasing: %s",
  338. caca_get_feature_name(caca_get_feature(CACA_ANTIALIASING)));
  339. break;
  340. case STATUS_DITHERING:
  341. caca_printf(0, wh - 1, "Dithering: %s",
  342. caca_get_feature_name(caca_get_feature(CACA_DITHERING)));
  343. break;
  344. case STATUS_BACKGROUND:
  345. caca_printf(0, wh - 1, "Background: %s",
  346. caca_get_feature_name(caca_get_feature(CACA_BACKGROUND)));
  347. break;
  348. }
  349. }
  350. if(help)
  351. {
  352. caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
  353. caca_putstr(ww - 25, 2, " +: zoom in ");
  354. caca_putstr(ww - 25, 3, " -: zoom out ");
  355. caca_putstr(ww - 25, 4, " x: reset zoom ");
  356. caca_putstr(ww - 25, 5, " ---------------------- ");
  357. caca_putstr(ww - 25, 6, " hjkl: move view ");
  358. caca_putstr(ww - 25, 7, " arrows: move view ");
  359. caca_putstr(ww - 25, 8, " ---------------------- ");
  360. caca_putstr(ww - 25, 9, " a: antialiasing method ");
  361. caca_putstr(ww - 25, 10, " d: dithering method ");
  362. caca_putstr(ww - 25, 11, " b: background mode ");
  363. caca_putstr(ww - 25, 12, " ---------------------- ");
  364. caca_putstr(ww - 25, 13, " ?: help ");
  365. caca_putstr(ww - 25, 14, " q: quit ");
  366. }
  367. caca_refresh();
  368. update = 0;
  369. }
  370. /* Clean up */
  371. unload_image();
  372. caca_end();
  373. return 0;
  374. }
  375. static void set_zoom(int new_zoom)
  376. {
  377. int height;
  378. zoom = new_zoom;
  379. if(zoom > ZOOM_MAX) zoom = ZOOM_MAX;
  380. if(zoom < -ZOOM_MAX) zoom = -ZOOM_MAX;
  381. ww = caca_get_width();
  382. height = fullscreen ? wh : wh - 3;
  383. xfactor = (zoom < 0) ? 1.0 / zoomtab[-zoom] : zoomtab[zoom];
  384. yfactor = xfactor * ww / height * h / w
  385. * caca_get_height() / caca_get_width()
  386. * caca_get_window_width() / caca_get_window_height();
  387. if(yfactor > xfactor)
  388. {
  389. float tmp = xfactor;
  390. xfactor = tmp * tmp / yfactor;
  391. yfactor = tmp;
  392. }
  393. }
  394. static void unload_image(void)
  395. {
  396. #if defined(HAVE_IMLIB2_H)
  397. if(image)
  398. imlib_free_image();
  399. image = NULL;
  400. pixels = NULL;
  401. #else
  402. if(pixels)
  403. free(pixels);
  404. pixels = NULL;
  405. #endif
  406. if(bitmap)
  407. caca_free_bitmap(bitmap);
  408. bitmap = NULL;
  409. }
  410. static void load_image(char const *name)
  411. {
  412. #if defined(HAVE_IMLIB2_H)
  413. /* Load the new image */
  414. image = imlib_load_image(name);
  415. if(!image)
  416. return;
  417. imlib_context_set_image(image);
  418. pixels = (char *)imlib_image_get_data_for_reading_only();
  419. w = imlib_image_get_width();
  420. h = imlib_image_get_height();
  421. rmask = 0x00ff0000;
  422. gmask = 0x0000ff00;
  423. bmask = 0x000000ff;
  424. amask = 0xff000000;
  425. bpp = 32;
  426. depth = 4;
  427. /* Create the libcaca bitmap */
  428. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  429. rmask, gmask, bmask, amask);
  430. if(!bitmap)
  431. {
  432. imlib_free_image();
  433. image = NULL;
  434. }
  435. #else
  436. /* Try to load a BMP file */
  437. FILE *fp;
  438. unsigned int i, colors, offset, tmp, planes;
  439. fp = fopen(name, "rb");
  440. if(!fp)
  441. return;
  442. if(freadshort(fp) != 0x4d42)
  443. {
  444. fclose(fp);
  445. return;
  446. }
  447. freadint(fp); /* size */
  448. freadshort(fp); /* reserved 1 */
  449. freadshort(fp); /* reserved 2 */
  450. offset = freadint(fp);
  451. tmp = freadint(fp); /* header size */
  452. if(tmp == 40)
  453. {
  454. w = freadint(fp);
  455. h = freadint(fp);
  456. planes = freadshort(fp);
  457. bpp = freadshort(fp);
  458. tmp = freadint(fp); /* compression */
  459. if(tmp != 0)
  460. {
  461. fclose(fp);
  462. return;
  463. }
  464. freadint(fp); /* sizeimage */
  465. freadint(fp); /* xpelspermeter */
  466. freadint(fp); /* ypelspermeter */
  467. freadint(fp); /* biclrused */
  468. freadint(fp); /* biclrimportantn */
  469. colors = (offset - 54) / 4;
  470. for(i = 0; i < colors && i < 256; i++)
  471. {
  472. blue[i] = freadchar(fp) * 16;
  473. green[i] = freadchar(fp) * 16;
  474. red[i] = freadchar(fp) * 16;
  475. alpha[i] = 0;
  476. freadchar(fp);
  477. }
  478. }
  479. else if(tmp == 12)
  480. {
  481. w = freadint(fp);
  482. h = freadint(fp);
  483. planes = freadshort(fp);
  484. bpp = freadshort(fp);
  485. colors = (offset - 26) / 3;
  486. for(i = 0; i < colors && i < 256; i++)
  487. {
  488. blue[i] = freadchar(fp);
  489. green[i] = freadchar(fp);
  490. red[i] = freadchar(fp);
  491. alpha[i] = 0;
  492. }
  493. }
  494. else
  495. {
  496. fclose(fp);
  497. return;
  498. }
  499. /* Fill the rest of the palette */
  500. for(i = colors; i < 256; i++)
  501. blue[i] = green[i] = red[i] = alpha[i] = 0;
  502. depth = (bpp + 7) / 8;
  503. /* Sanity check */
  504. if(!w || w > 0x10000 || !h || h > 0x10000 || planes != 1 /*|| bpp != 24*/)
  505. {
  506. fclose(fp);
  507. return;
  508. }
  509. /* Allocate the pixel buffer */
  510. pixels = malloc(w * h * depth);
  511. if(!pixels)
  512. {
  513. fclose(fp);
  514. return;
  515. }
  516. memset(pixels, 0, w * h * depth);
  517. /* Read the bitmap data */
  518. for(i = h; i--; )
  519. {
  520. unsigned int j, k, bits = 0;
  521. switch(bpp)
  522. {
  523. case 1:
  524. for(j = 0; j < w; j++)
  525. {
  526. k = j % 32;
  527. if(k == 0)
  528. bits = freadint(fp);
  529. pixels[w * i * depth + j] =
  530. (bits >> ((k & ~0xf) + 0xf - (k & 0xf))) & 0x1;
  531. }
  532. break;
  533. case 4:
  534. for(j = 0; j < w; j++)
  535. {
  536. k = j % 8;
  537. if(k == 0)
  538. bits = freadint(fp);
  539. pixels[w * i * depth + j] =
  540. (bits >> (4 * ((k & ~0x1) + 0x1 - (k & 0x1)))) & 0xf;
  541. }
  542. break;
  543. default:
  544. /* Works for 8bpp, but also for 16, 24 etc. */
  545. fread(pixels + w * i * depth, w * depth, 1, fp);
  546. /* Pad reads to 4 bytes */
  547. tmp = (w * depth) % 4;
  548. tmp = (4 - tmp) % 4;
  549. while(tmp--)
  550. freadchar(fp);
  551. break;
  552. }
  553. }
  554. switch(depth)
  555. {
  556. case 3:
  557. rmask = 0xff0000;
  558. gmask = 0x00ff00;
  559. bmask = 0x0000ff;
  560. amask = 0x000000;
  561. break;
  562. case 2: /* XXX: those are the 16 bits values */
  563. rmask = 0x7c00;
  564. gmask = 0x03e0;
  565. bmask = 0x001f;
  566. amask = 0x0000;
  567. break;
  568. case 1:
  569. default:
  570. bpp = 8;
  571. rmask = gmask = bmask = amask = 0;
  572. break;
  573. }
  574. fclose(fp);
  575. /* Create the libcaca bitmap */
  576. bitmap = caca_create_bitmap(bpp, w, h, depth * w,
  577. rmask, gmask, bmask, amask);
  578. if(!bitmap)
  579. {
  580. free(pixels);
  581. pixels = NULL;
  582. return;
  583. }
  584. if(bpp == 8)
  585. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  586. #endif
  587. }
  588. static void draw_checkers(int x1, int y1, int x2, int y2)
  589. {
  590. int xn, yn;
  591. if(x2 + 1 > (int)caca_get_width()) x2 = caca_get_width() - 1;
  592. if(y2 + 1 > (int)caca_get_height()) y2 = caca_get_height() - 1;
  593. for(yn = y1 > 0 ? y1 : 0; yn <= y2; yn++)
  594. for(xn = x1 > 0 ? x1 : 0; xn <= x2; xn++)
  595. {
  596. if((((xn - x1) / 5) ^ ((yn - y1) / 3)) & 1)
  597. caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_DARKGRAY);
  598. else
  599. caca_set_color(CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY);
  600. caca_putchar(xn, yn, ' ');
  601. }
  602. }
  603. #if !defined(HAVE_IMLIB2_H)
  604. static int freadint(FILE *fp)
  605. {
  606. unsigned char buffer[4];
  607. fread(buffer, 4, 1, fp);
  608. return ((int)buffer[3] << 24) | ((int)buffer[2] << 16)
  609. | ((int)buffer[1] << 8) | ((int)buffer[0]);
  610. }
  611. static int freadshort(FILE *fp)
  612. {
  613. unsigned char buffer[2];
  614. fread(buffer, 2, 1, fp);
  615. return ((int)buffer[1] << 8) | ((int)buffer[0]);
  616. }
  617. static int freadchar(FILE *fp)
  618. {
  619. unsigned char buffer;
  620. fread(&buffer, 1, 1, fp);
  621. return (int)buffer;
  622. }
  623. #endif