home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume38 / tovcr / part02 < prev    next >
Encoding:
Text File  |  1993-07-13  |  58.8 KB  |  2,356 lines

  1. Newsgroups: comp.sources.misc
  2. From: nuevos@hp400.ccu.uniovi.es (Raul y Quique)
  3. Subject: v38i058:  tovcr - Record/Display images into the Video Frame, Part02/04
  4. Message-ID: <1993Jul14.144011.25645@sparky.sterling.com>
  5. X-Md4-Signature: 83e94ef0571b920c54541ade6a6fce8e
  6. Sender: kent@sparky.sterling.com (Kent Landfield)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 14:40:11 GMT
  9. Approved: kent@sparky.sterling.com
  10.  
  11. Submitted-by: nuevos@hp400.ccu.uniovi.es (Raul y Quique)
  12. Posting-number: Volume 38, Issue 58
  13. Archive-name: tovcr/part02
  14. Environment: SGI
  15.  
  16. #! /bin/sh
  17. # This is a shell archive.  Remove anything before this line, then feed it
  18. # into a shell via "sh file" or similar.  To overwrite existing files,
  19. # type "sh file -c".
  20. # Contents:  Makefile tovcr.c.B
  21. # Wrapped by kent@sparky on Sun Jul 11 19:05:24 1993
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 2 (of 4)."'
  25. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'Makefile'\"
  27. else
  28.   echo shar: Extracting \"'Makefile'\" \(2104 characters\)
  29.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  30. X#
  31. X# Makefile for the 'tovcr' program.
  32. X#
  33. X# Author:    Raul Rivero
  34. X#               Mathematics Dept.
  35. X#               University of Oviedo
  36. X# Date:         Mon May 17 1993
  37. X# Copyright (c) 1993, Raul Rivero
  38. X#
  39. X
  40. X#
  41. X# The name of ... THE PROGRAM !!!
  42. X#
  43. XPRGNAME = tovcr 
  44. X
  45. X#
  46. X# If you have intalled the Spencer W. Thomas' URT define
  47. X# next variables ( and add -DiRLE to CCOPTIONS ), example:
  48. X#
  49. X#    URTINC = -I/remote/caballe/u/nuevos/correo/include/urt
  50. X#    URTLIB = -lrle
  51. X#
  52. X# , or leave undefined if you don't have it.
  53. X#
  54. XURTINC = -I/usr/include/rle 
  55. XURTLIB = -lrle 
  56. X
  57. X#
  58. X# If you have intalled the Sam Leffler's TIFF library define
  59. X# next variables ( and add -DiTIFF to CCOPTIONS ), example:
  60. X#
  61. X#    TIFFINC = -I/remote/caballe/u/nuevos/correo/include/tiff
  62. X#    TIFFLIB = -ltiff
  63. X#
  64. X# , or leave undefined if you don't have it.
  65. X#
  66. XTIFFINC = -I/usr/include/tiff 
  67. XTIFFLIB = -ltiff 
  68. X
  69. X#
  70. X# If you have intalled the Thomas G. Lane's JPEG library define
  71. X# next variables ( and add -DiJPEG to CCOPTIONS ), example:
  72. X#
  73. X#       JPEGINC = -I/remote/caballe/u/nuevos/correo/include/jpeg
  74. X#       JPEGLIB = -ljpeg
  75. X#
  76. X# , or leave undefined if you don't have it.
  77. X#
  78. XJPEGINC = -I/usr/include/jpeg 
  79. XJPEGLIB = -ljpeg 
  80. X
  81. X#
  82. X# Now define where search the libraries, example:
  83. X#
  84. X#       LDOPTIONS = -L /remote/caballe/u/nuevos/correo/lib
  85. X#
  86. XLDOPTIONS = -L /usr/local/lib 
  87. X
  88. X#
  89. X# Final include and library options ( don't touch ! ).
  90. X#
  91. XINCS =  $(URTINC) $(JPEGINC) $(TIFFINC) -I. -I/usr/video/vfr/src/inc -I /usr/include/gl
  92. XLIBS =  $(URTLIB) $(JPEGLIB) $(TIFFLIB) -limage -lvfr_s -lmalloc -lm 
  93. X
  94. X#
  95. X# Compiler flags.
  96. X#
  97. X# See what defines you need:
  98. X#
  99. X#       VOID_STAR       BSD     USE_STINGS      USE_STDLIB_H
  100. X#
  101. X# and don't forget the defines to turn on the graphic file formats.
  102. X#
  103. X# Example:
  104. X#
  105. X#       CCFLAGS = -O -DVOID_STAR -DBSD -DiRLE -DiTIFF -DiJPEG
  106. X#
  107. XCFLAGS = -O -DVOID_STAR -DBSD -DiRLE -DiTIFF -DiJPEG 
  108. X
  109. X#
  110. X# Object names.
  111. X#
  112. XOBJS =    tovcr.o    
  113. X
  114. Xall: $(PRGNAME)
  115. X
  116. X$(PRGNAME): $(OBJS)
  117. X    $(CC) -o $(PRGNAME) $(OBJS) $(LDOPTIONS) $(LIBS)
  118. X
  119. X.c.o:
  120. X    $(CC) -c $(CFLAGS) $(INCS) $<
  121. X
  122. Xclean:
  123. X    @ rm -f $(OBJS) core
  124. X
  125. Xarchive:
  126. X    (tar cvf ../tovcr.tar .; compress -v ../tovcr.tar)
  127. X
  128. END_OF_FILE
  129.   if test 2104 -ne `wc -c <'Makefile'`; then
  130.     echo shar: \"'Makefile'\" unpacked with wrong size!
  131.   fi
  132.   # end of 'Makefile'
  133. fi
  134. if test -f 'tovcr.c.B' -a "${1}" != "-c" ; then 
  135.   echo shar: Will not clobber existing file \"'tovcr.c.B'\"
  136. else
  137.   echo shar: Extracting \"'tovcr.c.B'\" \(53392 characters\)
  138.   sed "s/^X//" >'tovcr.c.B' <<'END_OF_FILE'
  139. Xread_tga24( handle, r, g, b, tga )
  140. XFILE *handle;
  141. Xbyte *r, *g, *b;
  142. Xtga_hdr *tga;
  143. X{
  144. X  register int i, j;
  145. X  int aux;
  146. X  byte *ptr;
  147. X  byte *buffer;
  148. X  int totalsize;
  149. X  int count = 0;
  150. X  byte repeat;
  151. X  byte rbyte, gbyte, bbyte;
  152. X  int code;
  153. X
  154. X  if ( tga->image_type < 9 ) {
  155. X    /* No RLE ! */
  156. X    VPRINTF(stderr, "Reading true color Targa image\n");
  157. X    buffer = (byte *) Malloc( tga->pixel_size * tga->xsize );
  158. X    for ( j = 0; j < tga->ysize; j++ ) {
  159. X#ifdef DEBUG
  160. X      VPRINTF(stderr, "Reading line %d\r", j); VFLUSH( stderr );
  161. X#endif
  162. X      fread( buffer, tga->xsize, tga->pixel_size, handle);
  163. X      ptr = buffer;
  164. X      if ( tga->pixel_size < 3 ) {
  165. X        for ( i = 0 ; i < tga->xsize; i++ ) {
  166. X          aux = ( ptr[1] << 8 ) | ptr[0];
  167. X          *r++ = (aux & 0x7c00) >> 7;
  168. X          *g++ = (aux & 0x03e0) >> 2;
  169. X          *b++ = (aux & 0x001F) << 3;
  170. X          ptr += 2;
  171. X        }
  172. X      }else {
  173. X        for ( i = 0 ; i < tga->xsize; i++ ) {
  174. X          *b++ = *ptr++;
  175. X          *g++ = *ptr++;
  176. X          *r++ = *ptr++;
  177. X          if ( tga->pixel_size == 4 )
  178. X            ptr++;
  179. X        }
  180. X      }
  181. X    }
  182. X#ifdef DEBUG
  183. X    VPRINTF( stderr, "\n" );
  184. X#endif
  185. X    Free( buffer );
  186. X  }else {
  187. X    VPRINTF(stderr, "Reading true color Targa RLE image\n");
  188. X    /* RLE */
  189. X    buffer = (byte *) Malloc( 256 * tga->pixel_size );
  190. X    totalsize = tga->xsize * tga->ysize;
  191. X    while ( count < totalsize ) {
  192. X      Fread( &repeat, 1, 1, handle );
  193. X      code = (repeat & 127) + 1;
  194. X      count += code;
  195. X      if ( (repeat & 128) ) {
  196. X        Fread( buffer, tga->pixel_size, 1, handle );
  197. X        if ( tga->pixel_size < 3 ) {
  198. X          aux = ( buffer[1] << 8 ) | buffer[0];
  199. X          rbyte = (aux & 0x7c00) >> 7;
  200. X          gbyte = (aux & 0x03e0) >> 2;
  201. X          bbyte = (aux & 0x001F) << 3;
  202. X        }else {
  203. X          rbyte = buffer[2];
  204. X          gbyte = buffer[1];
  205. X          bbyte = buffer[0];
  206. X        }
  207. X        memset( r, rbyte, code ); r += code;
  208. X        memset( g, gbyte, code ); g += code;
  209. X        memset( b, bbyte, code ); b += code;
  210. X      }else {
  211. X        Fread( buffer, tga->pixel_size, code, handle );
  212. X        ptr = buffer;
  213. X        while ( code-- ) {
  214. X          if ( tga->pixel_size < 3 ) {
  215. X            aux = ( ptr[1] << 8 ) | ptr[0];
  216. X            *r++ = (aux & 0x7c00) >> 7;
  217. X            *g++ = (aux & 0x03e0) >> 2;
  218. X            *b++ = (aux & 0x001F) << 3;
  219. X            ptr += 2;
  220. X          }else {
  221. X            *b++ = *ptr++;
  222. X            *g++ = *ptr++;
  223. X            *r++ = *ptr++;
  224. X            if ( tga->pixel_size == 4 )
  225. X              ptr++;
  226. X          }
  227. X        }
  228. X      } /* if ( (repeat ... ) ) */
  229. X    } /* while ( count ... ) */
  230. X    Free( buffer );
  231. X  }
  232. X}
  233. X
  234. Xread_tga_to24( handle, r, g, b, tga, cmap )
  235. XFILE *handle;
  236. Xbyte *r, *g, *b;
  237. Xtga_hdr *tga;
  238. Xbyte *cmap;
  239. X{
  240. X  fprintf(stderr, "Converting to 24 planes\n");
  241. X  error( 99 );
  242. X}
  243. X
  244. Xread_tga8( handle, r, tga, cmap )
  245. XFILE *handle;
  246. Xbyte *r;
  247. Xtga_hdr *tga;
  248. X{
  249. X  if ( tga->image_type == 1 ) {
  250. X    VPRINTF( stderr, "Reading mapped Targa image\n" );
  251. X    Fread( r, tga->xsize, tga->ysize, handle );
  252. X  }else {
  253. X    byte buffer, repeat;
  254. X    int totalsize = tga->xsize * tga->ysize;
  255. X    int count = 0;
  256. X    int code;
  257. X
  258. X    VPRINTF( stderr, "Reading mapped RLE Targa image\n" );
  259. X    /* RLE */
  260. X    while ( count < totalsize ) {
  261. X      Fread( &repeat, 1, 1, handle );
  262. X      code = (repeat & 127) + 1;
  263. X      count += code;
  264. X      if ( (repeat & 128) ) {
  265. X        Fread( &buffer, 1, 1, handle );
  266. X        memset( r, buffer, code ); r += code;
  267. X      }else {
  268. X        Fread( r, code, 1, handle );
  269. X        r += code;
  270. X      }
  271. X    } /* while ( count ... ) */
  272. X  }
  273. X}
  274. X
  275. Xread_tga_header(handle, tga)
  276. XFILE *handle;
  277. Xtga_hdr *tga;
  278. X{
  279. X  byte buffer[18];
  280. X
  281. X  Fread( buffer, 18, 1, handle );
  282. X
  283. X  /*
  284. X   * Bytes are in reverse order so ...
  285. X   */
  286. X  tga->num_id = buffer[0];
  287. X  tga->cmap_type = buffer[1];
  288. X  tga->image_type = buffer[2];
  289. X  tga->cmap_orign = ( buffer[4] << 8 ) | buffer[3];
  290. X  tga->cmap_length = ( buffer[6] << 8 ) | buffer[5];
  291. X  tga->cmap_entry_size = buffer[7] / 8;
  292. X  tga->xorig = ( buffer[9] << 8 ) | buffer[8];
  293. X  tga->yorig = ( buffer[11] << 8 ) | buffer[10];
  294. X  tga->xsize = ( buffer[13] << 8 ) | buffer[12];
  295. X  tga->ysize = ( buffer[15] << 8 ) | buffer[14];
  296. X  tga->pixel_size = buffer[16] / 8;     /* we'll use it directly */
  297. X  tga->image_descriptor = buffer[17];
  298. X
  299. X  VPRINTF(stderr, "Image type %d\n", tga->image_type);
  300. X  VPRINTF(stderr, "%s color map\n", (tga->cmap_type ? "With" : "Without"));
  301. X  VPRINTF(stderr, "Cmap origin: %d\n", tga->cmap_orign );
  302. X  VPRINTF(stderr, "Cmap length: %d\n", tga->cmap_length );
  303. X  VPRINTF(stderr, "Cmap entry size: %d\n", tga->cmap_entry_size );
  304. X  VPRINTF(stderr, "Image size: %dx%d\n", tga->xsize, tga->ysize );
  305. X  VPRINTF(stderr, "Pixel size: %d\n", tga->pixel_size );
  306. X  if ( TGAINTERLACED(tga->image_descriptor) )
  307. X    VPRINTF(stderr, "Interlace image\n");
  308. X  if ( TGAFLIP(tga->image_descriptor) )
  309. X    VPRINTF(stderr, "Flipped image\n");
  310. X
  311. X  /* Interlaced ? */
  312. X  if ( TGAINTERLACED(tga->image_descriptor) ) {
  313. X    fprintf( stderr, "Interlaced image\n");
  314. X    error( 99 );
  315. X  }
  316. X
  317. X  /*
  318. X   * Skip the identification.
  319. X   */
  320. X  if ( tga->num_id ) {
  321. X    VPRINTF( stderr, "Image identification: ");
  322. X    while ( tga->num_id ) {
  323. X      if ( verbose )
  324. X        putc( getc(handle), stderr );
  325. X      else (void) getc( handle );
  326. X      tga->num_id--;
  327. X    }
  328. X    VPRINTF( stderr, "\n" );
  329. X  }
  330. X
  331. X  /*
  332. X   * Check the image type.
  333. X   */
  334. X  switch ( tga->image_type ) {
  335. X    case TGA_RGB:
  336. X    case TGA_RLE_RGB:
  337. X    case TGA_MAPPED:
  338. X    case TGA_RLE_MAPPED:
  339. X                break;
  340. X    default:
  341. X                fprintf(stderr, "Image type %d\n", tga->image_type);
  342. X                error( 99 );
  343. X                break;
  344. X  }
  345. X}
  346. X
  347. Xread_tga_data( buffer, no_bytes, handle )
  348. Xbyte *buffer;
  349. Xint no_bytes;
  350. XFILE *handle;
  351. X{
  352. X  int aux1;
  353. X  byte aux[4];
  354. X
  355. X  Fread( aux, no_bytes, 1, handle );
  356. X
  357. X  if ( no_bytes < 3 ) {
  358. X    aux1 = ( aux[1] << 8 ) | aux[0];
  359. X    *buffer++ = (aux1 & 0x7c00) >> 7;
  360. X    *buffer++ = (aux1 & 0x03e0) >> 2;
  361. X    *buffer++ = (aux1 & 0x001F) << 3;
  362. X  }else {
  363. X     *buffer++ = aux[2];
  364. X     *buffer++ = aux[1];
  365. X     *buffer++ = aux[0];
  366. X  }
  367. X}
  368. X
  369. X
  370. X/**************************************
  371. X *
  372. X * GIF interface
  373. X *
  374. X */
  375. Xread_gif_file( name, image )
  376. Xchar *name;
  377. Xbitmap_hdr *image;
  378. X{
  379. X  FILE *handle;
  380. X  bitmap_hdr bitmap_8_bits;
  381. X
  382. X  /* Reset the ... 'read code position', ok ? */
  383. X  lug_read_code_position = 0;
  384. X
  385. X  /* Open the file */
  386. X  handle = Fopen( name, "r" );
  387. X  /* Read it */
  388. X  read_gif( handle, &bitmap_8_bits );
  389. X  /* Close it */
  390. X  Fclose( handle );
  391. X
  392. X  /*
  393. X   * Now we have a 8 bits bitmap, but
  394. X   * we nedd a TRUE color image.
  395. X   */
  396. X  to24( &bitmap_8_bits, image );
  397. X
  398. X  /* Free tmp bitmap */
  399. X  freebitmap( &bitmap_8_bits );
  400. X}
  401. X
  402. Xread_gif( handle, image )
  403. XFILE *handle;
  404. Xbitmap_hdr *image;
  405. X{
  406. X  int xsize, ysize;
  407. X  byte *globalcmap, *localcmap;
  408. X  int mask;
  409. X  int codesize;
  410. X  int totalsize;
  411. X  int globalcolors, localcolors;
  412. X  int localmask, globalmask;
  413. X
  414. X  /*
  415. X   * Read GIF headers ( we skip many parameters ).
  416. X   */
  417. X  read_gif_hdr( handle );
  418. X  globalcmap = read_gif_screen_hdr( handle, &globalcolors, &globalmask );
  419. X  localcmap = read_gif_image_hdr( handle, &localcolors, &localmask,
  420. X                                  &xsize, &ysize );
  421. X
  422. X  /*
  423. X   * We only use one color map.
  424. X   */
  425. X  if ( localcolors ) {
  426. X    mask = localmask;
  427. X    image->cmap = localcmap;
  428. X    if ( globalcolors )
  429. X      Free( globalcmap );
  430. X  }else
  431. X    if ( globalcolors ) {
  432. X      mask = globalmask;
  433. X      image->cmap = globalcmap;
  434. X    }else error( 13 );
  435. X
  436. X  /*
  437. X   *  Original ( root ) codesize.
  438. X   */
  439. X  codesize= fgetc( handle );
  440. X
  441. X  /*
  442. X   * Fill our header.
  443. X   */
  444. X  totalsize = xsize * ysize;
  445. X  image->magic = LUGUSED;
  446. X  image->xsize = xsize;
  447. X  image->ysize = ysize;
  448. X  image->colors = ( localcolors ? localcolors : globalcolors );
  449. X  image->depth = no_bits( image->colors ) + 1;
  450. X  image->r = (byte *) Malloc( totalsize );
  451. X  uncode_gif( handle, codesize, mask, image );
  452. X}
  453. X
  454. X#define READCODE()      { code = read_code( ptrblocks, datamask,    \
  455. X                                            &offset, codesize );    \
  456. X                          ptrblocks += offset; }
  457. X
  458. Xuncode_gif( handle, codesize, mask, image )
  459. XFILE *handle;
  460. Xint codesize, mask;
  461. Xbitmap_hdr *image;
  462. X{
  463. X  int endblock;
  464. X  int clearcode;
  465. X  int offset;
  466. X  int freecode;
  467. X  int code;
  468. X  int current, old;
  469. X  int datamask;
  470. X  int codetop;
  471. X  int orig_codesize;
  472. X  short *sufix, *prefix;
  473. X  byte *blocks, *ptrblocks;
  474. X  int count = 0;
  475. X  byte *stack;
  476. X  int k;
  477. X  int totalsize;
  478. X  int position;
  479. X
  480. X  /*
  481. X   * Allocate memory for internal buffers.
  482. X   */
  483. X  stack  = (byte *) Malloc( 4096 );
  484. X  sufix  = (short *) Malloc( 4096 * sizeof(short) );
  485. X  prefix = (short *) Malloc( 4096 * sizeof(short) );
  486. X
  487. X  /*
  488. X   * Compute predefined values for uncompress.
  489. X   */
  490. X  clearcode = (1 << codesize);
  491. X  endblock = clearcode + 1;
  492. X  freecode = clearcode + 2;
  493. X  orig_codesize = ++codesize;
  494. X  codetop = 1 << codesize;
  495. X  datamask = codetop - 1;
  496. X
  497. X  /*
  498. X   * Unblock the raster information.
  499. X   */
  500. X  ptrblocks = blocks = (byte *) unblockgif( handle );
  501. X
  502. X  totalsize = image->xsize * image->ysize;
  503. X
  504. X  /*
  505. X   * and now, ... UNPACK !!!.
  506. X   */
  507. X  VPRINTF( stderr, "Uncompressing GIF raster information\n" );
  508. X  READCODE();  /* read first code */
  509. X  while ( code != endblock ) {
  510. X    if ( code == clearcode ) {
  511. X      /*
  512. X       * Current code is clear so we reinitialize our tables.
  513. X       */
  514. X      codesize = orig_codesize;
  515. X      freecode = clearcode + 2;
  516. X      codetop = 1 << codesize;
  517. X      datamask = codetop - 1;
  518. X      READCODE();
  519. X      old = current = code;
  520. X      /* Next code to 'clear' is a root code */
  521. X      k = code & mask;
  522. X      position = push_gif( image->r, k );
  523. X    }else {
  524. X      /*
  525. X       * We have a normal code.
  526. X       */
  527. X      current = code;
  528. X      /*
  529. X       * If it's a new code then we need add the last translation.
  530. X       */
  531. X      if( !(code < freecode) ) {
  532. X        current = old;
  533. X        stack[count++] = (byte) k;
  534. X      }
  535. X
  536. X      /*
  537. X       * mask equals to 'last_root_code' so while current
  538. X       * code greater [than mask] we haven't a root code
  539. X       * and continue adding values into ioutput.
  540. X       */
  541. X      while ( current > mask ) {
  542. X        stack[count++] = sufix[current];
  543. X        current = prefix[current];
  544. X      }
  545. X      k = current & mask;
  546. X      stack[count] = k;
  547. X      for ( ; count >= 0; count-- )
  548. X        position = push_gif( image->r, (int) stack[count] );
  549. X      count = 0;
  550. X      prefix[freecode  ] = old;
  551. X      sufix [freecode++] = k;
  552. X      old = code;
  553. X      /*
  554. X       * Check if we use all posibles values ( for this
  555. X       * codesize ).
  556. X       */
  557. X      if ( freecode >= codetop && codesize != 12 ) {
  558. X        codesize++;
  559. X        codetop = 1 << codesize;
  560. X        datamask = codetop - 1;
  561. X      }
  562. X    }
  563. X    READCODE();
  564. X    if ( position > totalsize )
  565. X      error(6);
  566. X  }
  567. X
  568. X  /*
  569. X   * Free original GIF raster information and
  570. X   * internal buffers.
  571. X   */
  572. X  Free( blocks );
  573. X  Free( prefix );
  574. X  Free( sufix );
  575. X  Free( stack );
  576. X}
  577. X
  578. Xread_code( buffer, mask, offset, codesize )
  579. Xbyte *buffer;
  580. Xint mask;
  581. Xint *offset;
  582. Xint codesize;
  583. X{
  584. X  int aux = 0;
  585. X  int moveptr;
  586. X
  587. X  aux  = *buffer++;
  588. X  aux |= ( *buffer++ << 8 );
  589. X  if ( codesize > 7 )
  590. X    aux |= ( *buffer++ << 16 );
  591. X  aux >>= lug_read_code_position;
  592. X  lug_read_code_position += codesize;
  593. X  moveptr = lug_read_code_position / 8;
  594. X  if ( moveptr ) {
  595. X    *offset = moveptr;
  596. X    lug_read_code_position %= 8;
  597. X  }else *offset = 0;
  598. X
  599. X  return( aux & mask );
  600. X}
  601. X
  602. Xpush_gif(buffer, indexx)
  603. Xbyte *buffer;
  604. Xint indexx;
  605. X{
  606. X  static int offset;
  607. X  static byte *ptr;
  608. X  static byte *baseptr;
  609. X
  610. X  if ( baseptr != buffer ) {
  611. X    ptr = baseptr = buffer;
  612. X    offset = 0;
  613. X  }
  614. X
  615. X  *ptr++ = (byte) indexx;
  616. X  return ++offset;
  617. X}
  618. X
  619. Xread_gif_hdr( handle )
  620. XFILE *handle;
  621. X{
  622. X  char buffer[6];
  623. X
  624. X  Fread( buffer, 6, 1, handle);
  625. X  if ( strncmp( buffer, GIFHEADER, 6 ) )
  626. X    error( 5 );
  627. X}
  628. X
  629. Xbyte *read_gif_screen_hdr( handle, colors, mask )
  630. XFILE *handle;
  631. Xint *colors;
  632. Xint *mask;
  633. X{
  634. X  /* int swidth, sheight; */
  635. X  byte buffer[7];
  636. X  byte *cmap;
  637. X  int cmapflag;
  638. X  int bitsperpixel;
  639. X  /* int background; */
  640. X
  641. X  VPRINTF( stderr, "Reading GIF header\n" );
  642. X
  643. X  Fread( buffer, 7, 1, handle);
  644. X
  645. X  /* Screen size */
  646. X  /* swidth   = (buffer[1] << 8) | buffer[0]; */
  647. X  /* sheight  = (buffer[3] << 8) | buffer[2]; */
  648. X
  649. X  cmapflag = buffer[4] & GIFEXISTCOLOR;
  650. X  bitsperpixel = (buffer[4] & 7) + 1;
  651. X  *colors = 1 << bitsperpixel;
  652. X  *mask = *colors - 1;
  653. X  /* background= buffer[5]; */
  654. X
  655. X  if ( buffer[6] )
  656. X    error( 6 );
  657. X
  658. X  if ( cmapflag ) {
  659. X    cmap = (byte *) Malloc( 3 * *colors );
  660. X    Fread( cmap, *colors, 3, handle );
  661. X  }
  662. X
  663. X  return cmap;
  664. X}
  665. X
  666. Xbyte *read_gif_image_hdr( handle, colors, mask, width, height )
  667. XFILE *handle;
  668. Xint *colors;
  669. Xint *mask;
  670. Xint *width, *height;
  671. X{
  672. X  /* int left, right; */
  673. X  byte buffer[10];
  674. X  byte *cmap;
  675. X  int cmapflag;
  676. X  int interlace;
  677. X  int bitsperpixel;
  678. X
  679. X  VPRINTF( stderr, "Reading GIF image header\n" );
  680. X
  681. X  Fread( buffer, 10, 1, handle );
  682. X
  683. X  if ( buffer[0] != GIFIMGSEPAR)
  684. X    error( 5 );
  685. X
  686. X  /* left    = (buffer[2] << 8) | buffer[1]; */
  687. X  /* right   = (buffer[4] << 8) | buffer[3]; */
  688. X  *width  = (buffer[6] << 8) | buffer[5];
  689. X  *height = (buffer[8] << 8) | buffer[7];
  690. X  cmapflag  = buffer[9] & GIFEXISTCOLOR;
  691. X  interlace = buffer[9] & GIFINTERLAZE;
  692. X
  693. X  if ( interlace )
  694. X    error( 17 );
  695. X
  696. X  if ( cmapflag ) {
  697. X    bitsperpixel = ( buffer[9] & 7 ) + 1;
  698. X    *colors = 1 << bitsperpixel;
  699. X    *mask = *colors - 1;
  700. X    cmap = (byte *) Malloc( 3 * *colors );
  701. X    Fread( cmap, *colors, 3, handle );
  702. X  }else *colors = 0;
  703. X
  704. X  return cmap;
  705. X}
  706. X
  707. Xbyte *unblockgif( handle )
  708. XFILE *handle;
  709. X{
  710. X  long position;
  711. X  int totalsize;
  712. X  byte *out, *ptr;
  713. X  int size;
  714. X  int count = 0;
  715. X
  716. X  VPRINTF( stderr, "Unblocking GIF file\n" );
  717. X  /*
  718. X   * We need read the raster information and strip the block
  719. X   * size marks. GIF files can store more than one image but
  720. X   * this is not usually ( so we move handle to the end and
  721. X   * supose that size ).
  722. X   */
  723. X  position = ftell( handle );
  724. X  fseek( handle, 0L, 2 );
  725. X  totalsize = ftell( handle ) - position;
  726. X  fseek( handle, position, 0 );
  727. X
  728. X  ptr = out = (byte *) Malloc( totalsize );
  729. X
  730. X  /*
  731. X   * Format is:  <size><...block...><size><...block...>[...]<0>
  732. X   */
  733. X  size = fgetc( handle );
  734. X  while ( size ) {
  735. X    /* Check if no problems with space */
  736. X    count += size;
  737. X    if ( count > totalsize )
  738. X      error( 7 );
  739. X    /* No problems => read the block */
  740. X    Fread( ptr, size, 1, handle );
  741. X    ptr += size;
  742. X    size = fgetc( handle );
  743. X  }
  744. X
  745. X  return out;
  746. X}
  747. X
  748. X
  749. X#ifdef iJPEG
  750. X
  751. X/**************************************
  752. X *
  753. X * JPEG interface
  754. X *
  755. X */
  756. Xread_jpeg_file( name, bitmap )
  757. Xchar *name;
  758. Xbitmap_hdr *bitmap;
  759. X{
  760. X  FILE *handle;
  761. X
  762. X  /* Open the file descriptor */
  763. X  if ( name != NULL )
  764. X    handle = Fopen( name, "r" );
  765. X  else handle = stdin;
  766. X
  767. X  /* Read the bitmap */
  768. X  read_jpeg( handle, bitmap );
  769. X
  770. X  /* Close the file */
  771. X  Fclose( handle );
  772. X}
  773. X
  774. Xread_jpeg( handle, bitmap )
  775. XFILE *handle;
  776. Xbitmap_hdr *bitmap;
  777. X{
  778. X  struct Decompress_info_struct cinfo;
  779. X  struct Decompress_methods_struct dc_methods;
  780. X  struct External_methods_struct e_methods;
  781. X
  782. X  jpeg_image = bitmap;
  783. X
  784. X  cinfo.input_file = handle;
  785. X  cinfo.output_file = NULL;     /* if no actual output file involved */
  786. X
  787. X  /* Initialize the system-dependent method pointers. */
  788. X  cinfo.methods  = &dc_methods;
  789. X  cinfo.emethods = &e_methods;
  790. X
  791. X  /* Select std error/trace message & memory allocation routines */
  792. X  jselerror( &e_methods );
  793. X  jselmemmgr( &e_methods );
  794. X
  795. X  dc_methods.d_ui_method_selection = d_ui_method_selection;
  796. X
  797. X  /* Set up default decompression parameters. */
  798. X  j_d_defaults( &cinfo, TRUE );
  799. X
  800. X  /* Set up to read a JFIF or baseline-JPEG file. */
  801. X  jselrjfif( &cinfo );
  802. X
  803. X  /* Here we go! */
  804. X  jpeg_decompress( &cinfo );
  805. X}
  806. X
  807. Xvoid
  808. Xget_jpeg_header( cinfo )
  809. Xdecompress_info_ptr cinfo;
  810. X/* This routine should do any setup required */
  811. X{
  812. X  int totalsize = cinfo->image_width * cinfo->image_height;
  813. X
  814. X  VPRINTF( stderr, "Reading JPEG header\n" );
  815. X  /* Fill our bitmap header */
  816. X  jpeg_image->xsize  = cinfo->image_width;
  817. X  jpeg_image->ysize  = cinfo->image_height;
  818. X  jpeg_image->depth  = 24;
  819. X  jpeg_image->colors = ( 1 << jpeg_image->depth );
  820. X  jpeg_image->magic  = LUGUSED;
  821. X
  822. X  /* Alloc memory */
  823. X  jpeg_r = jpeg_image->r = (byte *) Malloc( totalsize );
  824. X  jpeg_g = jpeg_image->g = (byte *) Malloc( totalsize );
  825. X  jpeg_b = jpeg_image->b = (byte *) Malloc( totalsize );
  826. X}
  827. X
  828. Xvoid
  829. Xget_jpeg_cmap( cinfo, num_colors, colormap )
  830. Xdecompress_info_ptr cinfo; 
  831. Xint num_colors; 
  832. XJSAMPARRAY colormap;
  833. X/* Write the color map */
  834. X{
  835. X  /* You need not provide this routine if you always set cinfo->quantize_colors
  836. X   * FALSE; but a safer practice is to provide it and have it just print an
  837. X   * error message, like this:
  838. X   */
  839. X  fprintf(stderr, "get_jpeg_camp called: there's a bug here somewhere!\n");
  840. X}
  841. X
  842. Xvoid
  843. Xget_jpeg_row( cinfo, num_rows, pixel_data )
  844. Xdecompress_info_ptr cinfo;
  845. Xint num_rows;
  846. XJSAMPIMAGE pixel_data;
  847. X/* Write some rows of output data */
  848. X{
  849. X  register FILE * outfile = cinfo->output_file;
  850. X  register JSAMPROW ptr0, ptr1, ptr2;
  851. X  register long col;
  852. X  register int row;
  853. X
  854. X  VPRINTF( stderr, "\rUncoding JPEG ( position %d )",
  855. X           jpeg_r - jpeg_image->r );
  856. X  VFLUSH( stderr );
  857. X  for ( row = 0; row < num_rows; row++ ) {
  858. X    ptr0 = pixel_data[0][row];
  859. X    ptr1 = pixel_data[1][row];
  860. X    ptr2 = pixel_data[2][row];
  861. X    for ( col = 0; col < cinfo->image_width; col++ ) {
  862. X      *jpeg_r++ = GETJSAMPLE(*ptr0); ptr0++;    /* red */
  863. X      *jpeg_g++ = GETJSAMPLE(*ptr1); ptr1++;    /* green */
  864. X      *jpeg_b++ = GETJSAMPLE(*ptr2); ptr2++;    /* blue */
  865. X    }
  866. X  }
  867. X}
  868. X
  869. Xvoid
  870. Xend_put_jpeg( cinfo )
  871. Xdecompress_info_ptr cinfo;
  872. X/* Finish up at the end of the input */
  873. X{
  874. X  /* Defined only for compatibility reasons */
  875. X  VPRINTF( stderr, "\nJPEG process finished\n" );
  876. X}
  877. X
  878. Xvoid
  879. Xd_ui_method_selection( cinfo )
  880. Xdecompress_info_ptr cinfo;
  881. X{
  882. X  /* if grayscale input, force grayscale output; */
  883. X  /* else leave the output colorspace as set by main routine. */
  884. X  if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  885. X    cinfo->out_color_space = CS_GRAYSCALE;
  886. X
  887. X  /* select output routines */
  888. X  cinfo->methods->output_init = get_jpeg_header;
  889. X  cinfo->methods->put_color_map = get_jpeg_cmap;
  890. X  cinfo->methods->put_pixel_rows = get_jpeg_row;
  891. X  cinfo->methods->output_term = end_put_jpeg;
  892. X}
  893. X
  894. X#endif  /* iJPEG */
  895. X
  896. X
  897. X/**************************************
  898. X *
  899. X * PBM/PGM/PPM interface
  900. X *
  901. X */
  902. Xread_pbm_file( name, bitmap )
  903. Xchar *name;
  904. Xbitmap_hdr *bitmap;
  905. X{
  906. X  FILE *handle;
  907. X  bitmap_hdr image;
  908. X
  909. X  /* Open the file descriptor */
  910. X  if ( name != NULL )
  911. X    handle = Fopen( name, "r" );
  912. X  else handle = stdin;
  913. X
  914. X  /* Read the bitmap */
  915. X  read_pbm( handle, &image );
  916. X
  917. X  /* Close the file */
  918. X  Fclose( handle );
  919. X
  920. X  /*
  921. X   * Now we could have a 8 or 24 bits image, but 'tovcr'
  922. X   * needs true color images, so check it...
  923. X   */
  924. X  if ( image.depth > 8 ){
  925. X    copy_bitmap( &image, bitmap );
  926. X  }else {
  927. X    to24( &image, bitmap );
  928. X  }
  929. X
  930. X  /* Free the tmp bitmap */
  931. X  freebitmap( &image );
  932. X}
  933. X
  934. Xread_pbm(handle, image)
  935. XFILE *handle;
  936. Xbitmap_hdr *image;
  937. X{
  938. X  int type;
  939. X  int totalsize;
  940. X
  941. X  /* Is really a PBM file */
  942. X  if ( fgetc(handle) != 'P' )
  943. X    error( 5 );
  944. X
  945. X  /* Get its type */
  946. X  switch( type = fgetc(handle) - '0' ) {
  947. X    case 1:
  948. X    case 4:
  949. X                image->depth = 1;
  950. X                image->cmap = (byte *) Malloc( 6 );
  951. X                image->cmap[3] = image->cmap[4] = image->cmap[5] = 255;
  952. X                break;
  953. X    case 2:
  954. X    case 5:
  955. X                image->depth = 8;
  956. X                image->cmap = create_bw_pallete();
  957. X                break;
  958. X    case 3:
  959. X    case 6:
  960. X                image->depth = 24;
  961. X                break;
  962. X    default:
  963. X                fprintf( stderr, "Unknow PBM file type (%d)\n", type );
  964. X                error( 99 );
  965. X  }
  966. X
  967. X  /* Get the sizes and depth */
  968. X  image->colors = ( 1 << image->depth );
  969. X  image->magic = LUGUSED;
  970. X  skip_pbm( handle );
  971. X  fscanf( handle, "%d", &(image->xsize) );
  972. X  fscanf( handle, "%d", &(image->ysize) );
  973. X  totalsize = image->xsize * image->ysize;
  974. X
  975. X  /* Ok, we have it all.  Allocate memory */
  976. X  image->r = (byte *) Malloc( totalsize );
  977. X  if ( image->depth > 8 ) {
  978. X    image->g = (byte *) Malloc( totalsize );
  979. X    image->b = (byte *) Malloc( totalsize );
  980. X  }
  981. X
  982. X  if ( type < 4 ) {
  983. X    /* An ASCII file */
  984. X    fprintf( stderr, "The PBM/PGM/PPM ascii file format type is not supported.\n" );
  985. X    error( 99 );
  986. X  }else {
  987. X    register int i, j;
  988. X    byte *line, *ptr;
  989. X    byte *rb, *gb, *bb;
  990. X
  991. X    switch ( type ) {
  992. X      case 4:
  993. X                line = image->r;
  994. X                ptr  = line + totalsize;
  995. X                
  996. X                skip_pbm( handle );
  997. X                /* One byte has 8 pixels, so ... */
  998. X                while ( line < ptr ) {
  999. X                  i = fgetc( handle );
  1000. X                  for ( j = 7; j >= 0; j-- )
  1001. X                    *line++ =  i & ( 1 << j );
  1002. X                }
  1003. X                break;
  1004. X      case 5:
  1005. X                /* Skip the max. value */
  1006. X                fscanf( handle, "%d", &type );
  1007. X                skip_pbm( handle );
  1008. X
  1009. X                /* Read the raw data info */
  1010. X                Fread( image->r, totalsize, 1, handle );
  1011. X                break;
  1012. X      case 6:
  1013. X                /* Skip the max. value */
  1014. X                fscanf( handle, "%d", &type );
  1015. X                skip_pbm( handle );
  1016. X
  1017. X                /* Set pointers */
  1018. X                line = (byte *) Malloc( 3 * image->xsize );
  1019. X                rb = image->r;
  1020. X                gb = image->g;
  1021. X                bb = image->b;
  1022. X                /* Read each line and split RGB */
  1023. X                for ( i = 0; i < image->ysize; i++ ) {
  1024. X                  Fread( line, 3, image->xsize, handle );
  1025. X                  j = image->xsize, ptr = line;
  1026. X                  while ( j-- ) {
  1027. X                    *rb++ = *ptr++;
  1028. X                    *gb++ = *ptr++;
  1029. X                    *bb++ = *ptr++;
  1030. X                  }
  1031. X                }
  1032. X
  1033. X                /* Free memory */
  1034. X        Free( line );
  1035. X                break;
  1036. X    }
  1037. X  }
  1038. X  return 0;
  1039. X}
  1040. X
  1041. X/*
  1042. X * PBM files suport coments, so if a line begin with
  1043. X * a '#' char then ...
  1044. X */
  1045. Xskip_pbm( handle )
  1046. XFILE *handle;
  1047. X{
  1048. X  int c;
  1049. X
  1050. X  while ( !feof( handle ) ) {
  1051. X    /* Skip until next line */
  1052. X    while( fgetc( handle ) != '\n' );
  1053. X    /*
  1054. X     * If the first char is a '#' then we have
  1055. X     * a comment ( so we re-begin ), else return.
  1056. X     */
  1057. X    if ( (c = fgetc( handle )) != '#' ) {
  1058. X      ungetc( c, handle );
  1059. X      return;
  1060. X    }
  1061. X  }
  1062. X
  1063. X  /*
  1064. X   * Hmmm ???, if all the pbm image was comments I suppose
  1065. X   * an error, and you ?.
  1066. X   */
  1067. X  error( 19 );
  1068. X}
  1069. X
  1070. X
  1071. X/**************************************
  1072. X *
  1073. X * ZSoft's PCX interface
  1074. X *
  1075. X */
  1076. Xread_pcx_file( name, bitmap )
  1077. Xchar *name;
  1078. Xbitmap_hdr *bitmap;
  1079. X{
  1080. X  FILE *handle;
  1081. X  bitmap_hdr image;
  1082. X
  1083. X  /* Open the file */
  1084. X  handle = Fopen( name, "r" );
  1085. X  /* Read it */
  1086. X  read_pcx( handle, &image );
  1087. X  /* Close it */
  1088. X  Fclose( handle );
  1089. X
  1090. X  /*
  1091. X   * Now we could have a 8 or 24 bits image, but 'tovcr'
  1092. X   * needs true color images, so check it...
  1093. X   */
  1094. X  if ( image.depth > 8 ){
  1095. X    copy_bitmap( &image, bitmap );
  1096. X  }else {
  1097. X    to24( &image, bitmap );
  1098. X  }
  1099. X
  1100. X  /* Free the tmp bitmap */
  1101. X  freebitmap( &image );
  1102. X}
  1103. X
  1104. Xread_pcx( handle, image )
  1105. XFILE *handle;
  1106. Xbitmap_hdr *image;
  1107. X{
  1108. X  register int i, j, k;
  1109. X  byte header[ PCX_HEADERSIZE ];
  1110. X  int bitsperpixel;
  1111. X  int xsize, ysize;
  1112. X  int xmax, ymax;
  1113. X  int xmin, ymin;
  1114. X  byte version;
  1115. X  int bytesperline;
  1116. X  byte *r, *g, *b;
  1117. X  byte *buffer;
  1118. X  long position;
  1119. X  int mask;
  1120. X  int paletteinfo;
  1121. X  int truecolor = 0;
  1122. X
  1123. X  /*
  1124. X   * Read the header.
  1125. X   */
  1126. X  VPRINTF( stderr, "Reading PCX header\n" );
  1127. X  Fread( header, PCX_HEADERSIZE, 1, handle );
  1128. X  if ( header[0] != PCX_MAGICNUMBER )   /* a PCX file ? */
  1129. X    error( 5 );
  1130. X
  1131. X  /*
  1132. X   * Get some data from this header.
  1133. X   */
  1134. X  version = header[1];
  1135. X  VPRINTF( stderr, "PCX file version: %d\n", version );
  1136. X
  1137. X  if ( header[2] != 1 )         /* file with PCX RLE encode ? */
  1138. X    error( 9 );
  1139. X
  1140. X  bitsperpixel = ( header[3] == 1 ?  header[65] : header[3] );
  1141. X  VPRINTF( stderr, "Bits per pixel: %d\n", bitsperpixel );
  1142. X
  1143. X  xmin = (header[ 5] << 8) | header[ 4];
  1144. X  ymin = (header[ 7] << 8) | header[ 6];
  1145. X  xmax = (header[ 9] << 8) | header[ 8];
  1146. X  ymax = (header[11] << 8) | header[10];
  1147. X  xsize = xmax - xmin + 1;
  1148. X  ysize = ymax - ymin + 1;
  1149. X
  1150. X  bytesperline = (header[67] << 8) | header[66];
  1151. X  paletteinfo = (header[69] << 8) | header[68];
  1152. X  VPRINTF( stderr, "Bytes per line: %d\n", bytesperline );
  1153. X  VPRINTF( stderr, "Palete info: %d\n", paletteinfo );
  1154. X  /*
  1155. X   * Fill our header.
  1156. X   */
  1157. X  image->xsize = xsize;
  1158. X  image->ysize = ysize;
  1159. X  r = image->r = (byte *) Malloc( xsize * ysize );
  1160. X
  1161. X  /*
  1162. X   * Cmap info.
  1163. X   */
  1164. X  image->magic = LUGUSED;
  1165. X  image->depth = bitsperpixel;
  1166. X  image->colors = ( 1 << image->depth );
  1167. X  /*
  1168. X   * Read the cmap tag.
  1169. X   */
  1170. X  if ( version >= 5 && bitsperpixel == 8 ) {
  1171. X    /* We could have 256 colors, check it ! */
  1172. X    position = ftell( handle );
  1173. X    fseek( handle, -769L, 2 );
  1174. X    if ( getc( handle ) == PCX_256COLORS ) {
  1175. X      /* Really, we have 256 colors */
  1176. X      VPRINTF( stderr, "Reading additional color map\n" );
  1177. X      image->cmap = (byte *) Malloc( 3 * 256 );
  1178. X      Fread( image->cmap, 256, 3, handle );
  1179. X    }else {
  1180. X      /* 
  1181. X       * Wow !, we have a true color image. We need
  1182. X       * more memory.
  1183. X       */
  1184. X      truecolor = 1;
  1185. X      g = image->g = (byte *) Malloc( xsize * ysize );
  1186. X      b = image->b = (byte *) Malloc( xsize * ysize );
  1187. X      image->depth = 24;
  1188. X      image->colors = ( 1 << image->depth );
  1189. X    }
  1190. X    /* Go back to the original position */
  1191. X    fseek( handle, position, 0 );
  1192. X  }else {
  1193. X    /* We have (2**bitsperpixel) colors */
  1194. X    image->cmap = (byte *) Malloc( 3 * image->colors );
  1195. X    /*
  1196. X     * This cmap is stored on header space ( and we have
  1197. X     * this header, so we only nedd copy it ).
  1198. X     */
  1199. X    bcopy( &(header[16]), image->cmap, 3*image->colors );
  1200. X  }
  1201. X
  1202. X  /*
  1203. X   * Read the raster information.
  1204. X   */
  1205. X  VPRINTF( stderr, "Unpacking raster information\n");
  1206. X  if ( bitsperpixel > 4 ) {
  1207. X    /*
  1208. X     * Read a 'normal' image, one byte is one pixel.
  1209. X     */
  1210. X    for ( i = 0; i < ysize; i++ ) {
  1211. X      decodePCX( handle, r, bytesperline );
  1212. X      r += image->xsize;
  1213. X      if ( truecolor ) {
  1214. X        decodePCX( handle, g, bytesperline );
  1215. X        g += image->xsize;
  1216. X        decodePCX( handle, b, bytesperline );
  1217. X        b += image->xsize;
  1218. X      }
  1219. X    }
  1220. X  }else {
  1221. X    int totalbytes = bytesperline * bitsperpixel;
  1222. X    /*
  1223. X     * The image was built by planes, so we need
  1224. X     * join all planes ( arrggg ! ).
  1225. X     */
  1226. X    buffer = (byte *) Malloc( totalbytes );
  1227. X
  1228. X    for ( i = 0; i < ysize; i++ ) {
  1229. X      decodePCX( handle, buffer, totalbytes );
  1230. X      for ( j = 0; j < bitsperpixel; j++ ) {
  1231. X        /*
  1232. X         * Glue planes.
  1233. X         */
  1234. X        mask = 1 << j;
  1235. X        for ( k = 0; k < xsize; k++ ) {
  1236. X          if ( buffer[ (k+j*xsize) >> 3 ] & (0x80 >> (k & 0x0007 )) )
  1237. X            r[ k ] |= mask;
  1238. X        }
  1239. X      }
  1240. X      r += image->xsize;
  1241. X    }
  1242. X    Free( buffer );
  1243. X  }
  1244. X}
  1245. X
  1246. XdecodePCX( handle, buffer, linesize )
  1247. XFILE *handle;
  1248. Xbyte *buffer;
  1249. Xint linesize;
  1250. X{
  1251. X  int cin;
  1252. X  int count;
  1253. X
  1254. X  while ( linesize > 0 ) {
  1255. X    cin = getc( handle );
  1256. X    if ( (cin & PCX_COMPRESS) == PCX_COMPRESS ) {
  1257. X      /* A block compressed */
  1258. X      count = cin & 0x3f;       /* 00111111 */
  1259. X      if ( count > linesize )
  1260. X        error( 6 );
  1261. X
  1262. X      /* Get the real data to copy */
  1263. X      cin = getc( handle );
  1264. X      memset( buffer, cin, count );
  1265. X
  1266. X      /* Update pointers */
  1267. X      linesize -= count;
  1268. X      buffer += count;
  1269. X    }else {
  1270. X      /* Only one data */
  1271. X      *buffer++ = cin;
  1272. X      linesize--;
  1273. X    }
  1274. X  }
  1275. X}
  1276. X
  1277. X
  1278. X#ifdef iTIFF
  1279. X
  1280. X/**************************************
  1281. X *
  1282. X * TIFF interface
  1283. X *
  1284. X */
  1285. X
  1286. Xread_tiff_file( name, bitmap )
  1287. Xchar *name;
  1288. Xbitmap_hdr *bitmap;
  1289. X{
  1290. X  register int i, j;
  1291. X  TIFF *handle;
  1292. X  long xsize, ysize;
  1293. X  ushort samplesperpixel;
  1294. X  ushort bitspersample;
  1295. X  ushort photometric;
  1296. X  ushort planarconfig;
  1297. X  ushort *rTIFFcmap, *gTIFFcmap, *bTIFFcmap;
  1298. X  byte *r, *g, *b;
  1299. X  int totalsize;
  1300. X  byte *ptr;
  1301. X  byte *TIFFline;
  1302. X  bitmap_hdr image;
  1303. X
  1304. X  /*
  1305. X   * Open the file.
  1306. X   */
  1307. X  if ( name )
  1308. X    handle = TIFFOpen( name, "r" );
  1309. X  else handle = TIFFFdOpen( 0, "stdin", "r" );
  1310. X  if ( handle == NULL )
  1311. X    error( 1 );
  1312. X
  1313. X  /*
  1314. X   * Get TIFF configuration.
  1315. X   */
  1316. X  GetField( handle, TIFFTAG_IMAGEWIDTH,  &xsize );
  1317. X  GetField( handle, TIFFTAG_IMAGELENGTH, &ysize );
  1318. X  GetField( handle, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel );
  1319. X  GetField( handle, TIFFTAG_BITSPERSAMPLE, &bitspersample );
  1320. X  GetField( handle, TIFFTAG_PHOTOMETRIC, &photometric );
  1321. X  GetField( handle, TIFFTAG_PLANARCONFIG, &planarconfig );
  1322. X
  1323. X  /*
  1324. X   * ... ok. Fill our header.
  1325. X   */
  1326. X  image.magic = LUGUSED;
  1327. X  image.xsize = (int) xsize;
  1328. X  image.ysize = (int) ysize;
  1329. X  image.depth = ( samplesperpixel < 4 ? samplesperpixel : 3 ) * bitspersample;
  1330. X  image.colors = 1 << image.depth;
  1331. X
  1332. X  /*
  1333. X   * New LIBTIFF library has changed the calling parameters 
  1334. X   * of the TIFFPrintDirectory functions, so ... what version 
  1335. X   * are you using ?.
  1336. X   */
  1337. X  if ( verbose )
  1338. X#if TIFF_VERSION >= 42
  1339. X    TIFFPrintDirectory( handle, stderr,  0L );
  1340. X#else
  1341. X    TIFFPrintDirectory( handle, stderr, 1, 0, 0 );
  1342. X#endif
  1343. X  
  1344. X  /*
  1345. X   * Check if it's one of our supported formats ...
  1346. X   */
  1347. X  if ( bitspersample < 8 ) {
  1348. X    fprintf( stderr, "%d bits per pixel\n", bitspersample );
  1349. X    fprintf( stderr, "This release only support 8 bits per pixel\n");
  1350. X    exit( 99 );
  1351. X  }
  1352. X  switch ( photometric ) {
  1353. X    case PHOTOMETRIC_MINISBLACK :
  1354. X                /* Allocate memory for our cmap and build it */
  1355. X                ptr = image.cmap = (byte *) Malloc( 3 * image.colors );
  1356. X                for ( i = 0; i < image.colors; i++ ) {
  1357. X                  *ptr++ = i;
  1358. X                  *ptr++ = i;
  1359. X                  *ptr++ = i;
  1360. X                }
  1361. X                break;
  1362. X    case PHOTOMETRIC_MINISWHITE :
  1363. X                /* Allocate memory for our cmap and build it */
  1364. X                ptr = image.cmap = (byte *) Malloc( 3 * image.colors );
  1365. X                for ( i = 0; i < image.colors; i++ ) {
  1366. X                  *ptr++ = (byte) (255 - i);
  1367. X                  *ptr++ = (byte) (255 - i);
  1368. X                  *ptr++ = (byte) (255 - i);
  1369. X                }
  1370. X                break;
  1371. X    case PHOTOMETRIC_PALETTE :
  1372. X                /* Read the TIFF cmap */
  1373. X                if ( !TIFFGetField( handle, TIFFTAG_COLORMAP,
  1374. X                                    &rTIFFcmap, &gTIFFcmap, &bTIFFcmap ) )
  1375. X                  error( 9 );
  1376. X                /* Allocate memory for our cmap and get it */
  1377. X               ptr = image.cmap = (byte *) Malloc( 3 * image.colors );
  1378. X               for ( i = 0; i < image.colors; i++ ) {
  1379. X                  *ptr++ = ( rTIFFcmap[i] >> 8 );
  1380. X                  *ptr++ = ( gTIFFcmap[i] >> 8 );
  1381. X                  *ptr++ = ( bTIFFcmap[i] >> 8 );
  1382. X                }
  1383. X                break;
  1384. X    case PHOTOMETRIC_RGB :
  1385. X                break;
  1386. X    default:
  1387. X                fprintf( stderr, "Not supported photometric configuration\n");
  1388. X                error( 99 );
  1389. X  }
  1390. X  if (  image.depth > 8 ) {
  1391. X    switch ( planarconfig ) {
  1392. X      case PLANARCONFIG_CONTIG   :
  1393. X                break;
  1394. X      case PLANARCONFIG_SEPARATE :
  1395. X                fprintf( stderr, "Separate planar configuration is not supported, yet.\n");
  1396. X                error( 99 );
  1397. X                break;
  1398. X      default:
  1399. X                fprintf( stderr, "Unknown planar configuration\n");
  1400. X                error( 99 );
  1401. X    }
  1402. X  }
  1403. X
  1404. X
  1405. X  /*
  1406. X   * Allocate memory for bitmap.
  1407. X   */
  1408. X  totalsize = (int) (xsize * ysize);
  1409. X  r = image.r = (byte *) Malloc( totalsize );
  1410. X  if ( image.depth > 8 ) {
  1411. X    /* A real RGB image */
  1412. X    g = image.g = (byte *) Malloc( totalsize );
  1413. X    b = image.b = (byte *) Malloc( totalsize );
  1414. X  }
  1415. X
  1416. X  /*
  1417. X   * Get TIFF raster lines.
  1418. X   */
  1419. X  TIFFline = (byte *) Malloc( TIFFScanlineSize(handle) );
  1420. X  for ( i = 0; i < ysize; i++ ) {
  1421. X    TIFFReadScanline( handle, TIFFline, i, 0 );
  1422. X    for ( j = 0, ptr = TIFFline; j < xsize; j++ ) {
  1423. X      *r++ = *ptr++;
  1424. X      if ( image.depth > 8 ) {
  1425. X        *g++ = *ptr++;
  1426. X        *b++ = *ptr++;
  1427. X        if ( samplesperpixel == 4 ) {
  1428. X          /* Skip alpha channel */
  1429. X          ptr++;
  1430. X        }
  1431. X      }
  1432. X    }
  1433. X  }
  1434. X
  1435. X  /*
  1436. X   * Free our raster line and close the TIFF file.
  1437. X   */
  1438. X  Free( TIFFline );
  1439. X  TIFFClose( handle );
  1440. X
  1441. X  /*
  1442. X   * Now we could have a 8 or 24 bits image, but 'tovcr'
  1443. X   * needs true color images, so check it...
  1444. X   */
  1445. X  if ( image.depth > 8 ){
  1446. X    copy_bitmap( &image, bitmap );
  1447. X  }else {
  1448. X    to24( &image, bitmap );
  1449. X  }
  1450. X
  1451. X  /* Free the tmp bitmap */
  1452. X  freebitmap( &image );
  1453. X}
  1454. X
  1455. X#endif  /* iTIFF */
  1456. X
  1457. X
  1458. X#ifdef iRLE
  1459. X
  1460. X/**************************************
  1461. X *
  1462. X * Utah's RLE interface.
  1463. X *
  1464. X */
  1465. Xread_rle_file( name, bitmap )
  1466. Xchar *name;
  1467. Xbitmap_hdr *bitmap;
  1468. X{
  1469. X  FILE *handle;
  1470. X  bitmap_hdr image;
  1471. X
  1472. X  /* Open the file descriptor */
  1473. X  if ( name != NULL )
  1474. X    handle = Fopen( name, "r" );
  1475. X  else handle = stdin;
  1476. X
  1477. X  /* Read the bitmap */
  1478. X  read_rle( handle, &image );
  1479. X  /* Close the file */
  1480. X  Fclose( handle );
  1481. X
  1482. X  /*
  1483. X   * Now we could have a 8 or 24 bits image, but 'tovcr'
  1484. X   * needs true color images, so check it...
  1485. X   */
  1486. X  if ( image.depth > 8 ){
  1487. X    copy_bitmap( &image, bitmap );
  1488. X  }else {
  1489. X    to24( &image, bitmap );
  1490. X  }
  1491. X
  1492. X  /* Free the tmp bitmap */
  1493. X  freebitmap( &image );
  1494. X}
  1495. X
  1496. Xread_rle(handle, image)
  1497. XFILE *handle;
  1498. Xbitmap_hdr *image;
  1499. X{
  1500. X  register int i, j;
  1501. X  rle_pixel **row;
  1502. X  byte *r, *g, *b;
  1503. X  int totalsize;
  1504. X  color_map *map;
  1505. X  int type;
  1506. X  int two_lines;
  1507. X  int offset_last;
  1508. X
  1509. X  /*
  1510. X   * Read the file's configuration.
  1511. X   */
  1512. X  rle_dflt_hdr.rle_file = handle;
  1513. X  if ( rle_get_setup( &rle_dflt_hdr ) != RLE_SUCCESS )
  1514. X    error(3);
  1515. X
  1516. X  /*
  1517. X   * Fill our bitmap.
  1518. X   */
  1519. X  image->magic = LUGUSED;
  1520. X  image->xsize = rle_dflt_hdr.xmax - rle_dflt_hdr.xmin + 1;
  1521. X  image->ysize = rle_dflt_hdr.ymax - rle_dflt_hdr.ymin + 1;
  1522. X  image->depth = ( rle_dflt_hdr.ncolors < 3 ? rle_dflt_hdr.cmaplen : 24 );
  1523. X  image->colors = ( 1 << image->depth );
  1524. X  totalsize= image->xsize * image->ysize;
  1525. X  offset_last = totalsize - image->xsize;
  1526. X  two_lines = 2 * image->xsize;
  1527. X  VPRINTF(stderr, "Image size: %dx%d\n", image->xsize, image->ysize);
  1528. X  VPRINTF(stderr, "Image depth: %d\n", image->depth);
  1529. X  VPRINTF(stderr, "%s colormap\n", (rle_dflt_hdr.ncmap ? "With" : "Without"));
  1530. X
  1531. X  /*
  1532. X   * Check the coherence and image type.
  1533. X   */
  1534. X  VPRINTF(stderr, "Image type ");
  1535. X  switch ( rle_dflt_hdr.ncolors ) {
  1536. X    case  1: switch ( rle_dflt_hdr.ncmap ) {
  1537. X               case  0:
  1538. X                    type = GRAYSCALE;       /* 8 planes, no cmap */
  1539. X                    VPRINTF( stderr, "GRAYSCALE\n" );
  1540. X                    break;
  1541. X               case  3:
  1542. X                    type = PSEUDOCOLOR;     /* 8 planes, cmap */
  1543. X                    VPRINTF( stderr, "PSEUDOCOLOR\n" );
  1544. X                    break;
  1545. X               default:
  1546. X                    VPRINTF( stderr, "unkown\n" );
  1547. X                    error( 5 );
  1548. X                    break;
  1549. X             }
  1550. X             break;
  1551. X    case  3: switch ( rle_dflt_hdr.ncmap ) {
  1552. X               case  0:
  1553. X                    type = DIRECTCOLOR;     /* 24 planes, no cmap */
  1554. X                    VPRINTF( stderr, "DIRECTCOLOR\n" );
  1555. X                    break;
  1556. X               case  3:
  1557. X                    type = TRUECOLOR;       /* 24 planes, cmap */
  1558. X                    VPRINTF( stderr, "TRUECOLOR\n" );
  1559. X                    break;
  1560. X               default:
  1561. X                    VPRINTF( stderr, "unkown\n" );
  1562. X                    error(5);
  1563. X                    break;
  1564. X             }
  1565. X
  1566. X             break;
  1567. X    default: VPRINTF(stderr, "unkown\n");
  1568. X             error( 5 );
  1569. X             break;
  1570. X  }
  1571. X
  1572. X  /*
  1573. X   * Allocate some memory.
  1574. X   */
  1575. X  if ( rle_row_alloc(&rle_dflt_hdr, &row) < 0 )
  1576. X    error( 2 );
  1577. X  if ( image->depth > 8 || type == GRAYSCALE ) {
  1578. X    /*
  1579. X     * 24 planes => we need three components
  1580. X     * GRAYSCALE use 8 planes but it's defined like 24 planes
  1581. X     */
  1582. X    r= image->r = (byte *) Malloc( totalsize );
  1583. X    g= image->g = (byte *) Malloc( totalsize );
  1584. X    b= image->b = (byte *) Malloc( totalsize );
  1585. X    if ( type == TRUECOLOR ) {
  1586. X      image->colors = 256;      /* well, a trap to rlecmap_to_bit... */
  1587. X      rlecmap_to_bitmapcmap(&rle_dflt_hdr, image);
  1588. X      map = (color_map *) image->cmap;  /* will be more easy use it */
  1589. X    }
  1590. X  }else {
  1591. X    /* 8 planes => one component and cmap */
  1592. X    r = image->r = (byte *) Malloc( totalsize );
  1593. X    /* Convert rle cmap to bitmap cmap */
  1594. X    rlecmap_to_bitmapcmap( &rle_dflt_hdr, image );
  1595. X    map = (color_map *) image->cmap;    /* will be more easy use it */
  1596. X  }
  1597. X
  1598. X  /*
  1599. X   * Read the input image and convert it to a simple bitmap.
  1600. X   * RLE writes lines in reverse order, so we point to last
  1601. X   * line.
  1602. X   */
  1603. X  VPRINTF( stderr, "Uncompressing RLE file\n" );
  1604. X  r += offset_last;
  1605. X  g += offset_last;
  1606. X  b += offset_last;
  1607. X  for ( j = 0; j < image->ysize; j++ ) {
  1608. X    rle_getrow( &rle_dflt_hdr, row );
  1609. X    switch ( type ) {
  1610. X      case GRAYSCALE:
  1611. X      case DIRECTCOLOR:
  1612. X                for ( i = 0; i< image->xsize; i++ ) {
  1613. X                  *r++ = row[0][i];
  1614. X                  *g++ = row[1][i];
  1615. X                  *b++ = row[2][i];
  1616. X                }
  1617. X                break;
  1618. X      case PSEUDOCOLOR:
  1619. X                for ( i = 0; i<image->xsize; i++ ) {
  1620. X                  *r++ = row[0][i];
  1621. X                }
  1622. X                break;
  1623. X      case TRUECOLOR:
  1624. X                for ( i = 0; i< image->xsize; i++ ) {
  1625. X                  *r++ = map[row[0][i]][0];
  1626. X                  *g++ = map[row[1][i]][1];
  1627. X                  *b++ = map[row[2][i]][i];
  1628. X                }
  1629. X                break;
  1630. X      default:
  1631. X                error( 5 );
  1632. X                break;
  1633. X    }
  1634. X    /*
  1635. X     * Pointers to next byte of current line, so we substract
  1636. X     * two lines.
  1637. X     */
  1638. X    r -= two_lines;
  1639. X    g -= two_lines;
  1640. X    b -= two_lines;
  1641. X  }
  1642. X
  1643. X  /*
  1644. X   * TRUECOLOR has map of colors, but we'll not use it.
  1645. X   */
  1646. X  if ( type == TRUECOLOR )
  1647. X    Free( image->cmap );
  1648. X  rle_row_free( &rle_dflt_hdr, row );
  1649. X}
  1650. X
  1651. Xrlecmap_to_bitmapcmap(rle, bitmap)
  1652. Xrle_hdr *rle;
  1653. Xbitmap_hdr *bitmap;
  1654. X{
  1655. X  register int i;
  1656. X  rle_map *rch, *gch, *bch;
  1657. X  byte *ptr;
  1658. X
  1659. X  /* Allocate memory */
  1660. X  ptr= bitmap->cmap= (byte *) Malloc(bitmap->colors * 3);
  1661. X
  1662. X  /*
  1663. X   * We'll use 3 ptrs, first to R channel, second to G channel, ...
  1664. X   */
  1665. X  rch = rle->cmap;
  1666. X  gch = &(rle->cmap[bitmap->colors]);
  1667. X  bch = &(rle->cmap[2 * bitmap->colors]);
  1668. X  for (i= 0; i< bitmap->colors; i++) {
  1669. X    *ptr++ = (byte) (*rch++ >> 8);
  1670. X    *ptr++ = (byte) (*gch++ >> 8);
  1671. X    *ptr++ = (byte) (*bch++ >> 8);
  1672. X  }
  1673. X}
  1674. X
  1675. Xbitmapcmap_to_rlecmap(bitmap, rle)
  1676. Xbitmap_hdr *bitmap;
  1677. Xrle_hdr *rle;
  1678. X{
  1679. X  register int i;
  1680. X  rle_map *rch, *gch, *bch;
  1681. X  byte *ptr;
  1682. X
  1683. X  /* Allocate memory */
  1684. X  rle->cmap= (rle_map *) Malloc(bitmap->colors * 3 * sizeof(rle_map));
  1685. X
  1686. X  /*
  1687. X   * We'll use 3 ptrs, first to R channel, second to G channel, ...
  1688. X   */
  1689. X  ptr = bitmap->cmap;
  1690. X  rch = rle->cmap;
  1691. X  gch = &(rle->cmap[bitmap->colors]);
  1692. X  bch = &(rle->cmap[2 * bitmap->colors]);
  1693. X  for (i= 0; i< bitmap->colors; i++) {
  1694. X    *rch++ = (*ptr++ << 8);
  1695. X    *gch++ = (*ptr++ << 8);
  1696. X    *bch++ = (*ptr++ << 8);
  1697. X  }
  1698. X}
  1699. X
  1700. X#endif  /* iRLE */
  1701. X
  1702. X
  1703. X/**************************************
  1704. X *
  1705. X * Video FRamer and V-lan interface.
  1706. X *
  1707. X */
  1708. XVFR_DEV *open_video()
  1709. X{
  1710. X  char *video_mode;
  1711. X  VFR_DEV *vfr;
  1712. X  int flag;
  1713. X
  1714. X  /*
  1715. X   * Open the video. It can be reseted ( or not ).
  1716. X   */
  1717. X  video_mode = ( rgbvfr ? OPTIONAL_VFR_BUFFER : DEFAULT_VFR_BUFFER );
  1718. X  flag = ( resetvfr ? VFR_CUSTOM_SETUP : -VFR_CUSTOM_SETUP );
  1719. X  if ( !(vfr = vfr_open( DEFAULT_VFR_ADDR, flag, video_mode )))
  1720. X    error( 9 );
  1721. X
  1722. X  return vfr;
  1723. X}
  1724. X
  1725. Xwrite_vfr( vfr, bitmap )
  1726. XVFR_DEV *vfr;
  1727. Xbitmap_hdr *bitmap;
  1728. X{
  1729. X  register int y, x;
  1730. X  byte *ptr;
  1731. X  int totalsize = bitmap->ysize * bitmap->xsize;
  1732. X  byte *rbase, *gbase, *bbase;
  1733. X  byte *r, *g, *b;
  1734. X  int offset;
  1735. X
  1736. X  /* Really, is an image ? */
  1737. X  if ( bitmap->magic != LUGUSED )
  1738. X    error( 21 );
  1739. X
  1740. X  /*
  1741. X   * This subroutine only supports true color images, use
  1742. X   * 'to24' function if you have a mapped image before.
  1743. X   */
  1744. X  if ( bitmap->depth < 24 )
  1745. X    error( 7 );
  1746. X
  1747. X  /*
  1748. X   * If the we have to perform a blur before download the image
  1749. X   * to the VFR, ok do it!. Else set the pointers.
  1750. X   */
  1751. X  if ( forceblur ) {
  1752. X    r = rbase = blur( bitmap->r, bitmap->xsize, bitmap->ysize );
  1753. X    g = gbase = blur( bitmap->g, bitmap->xsize, bitmap->ysize );
  1754. X    b = bbase = blur( bitmap->b, bitmap->xsize, bitmap->ysize );
  1755. X  }else {
  1756. X    r = bitmap->r;
  1757. X    g = bitmap->g;
  1758. X    b = bitmap->b;
  1759. X  }
  1760. X
  1761. X  /* Pointers to VF's shadow memory */
  1762. X  ptr =  (byte *) VFR_DEVICE_PSHADOW( *vfr );
  1763. X  /* Unused space ( 4 because Alpha-R-G-B ) */
  1764. X  offset = 4 * ( VFR_LINESIZE - bitmap->xsize );
  1765. X
  1766. X  /*
  1767. X   * The VFR memory has the format:
  1768. X   *    1:  ----------line 1----------|----------line 2----------
  1769. X   *    2:  ----------line 3----------|----------line 4 ---------
  1770. X   *    3:  ...
  1771. X   *        [----- VFR lline size----][-----VFR line size ------]
  1772. X   *
  1773. X   * In other words, each VFR line contains two image lines and where
  1774. X   * each pixel is represented with Alpha-G-B-R.
  1775. X   *
  1776. X   * So ...
  1777. X   */
  1778. X  for ( y = 0; y < bitmap->ysize; y += 2 ) {
  1779. X    for ( x = 0; x < bitmap->xsize; x++ ) {
  1780. X      *ptr++ = 0;
  1781. X      *ptr++ = *b++;
  1782. X      *ptr++ = *g++;
  1783. X      *ptr++ = *r++;
  1784. X    }
  1785. X
  1786. X    /* Fill unused VFR line buffer with 0's */
  1787. X    bzero( ptr, offset );
  1788. X    ptr += offset;
  1789. X
  1790. X    for ( x = 0; x < bitmap->xsize; x++ ) {
  1791. X      *ptr++ = 0;
  1792. X      *ptr++ = *b++;
  1793. X      *ptr++ = *g++;
  1794. X      *ptr++ = *r++;
  1795. X    }
  1796. X
  1797. X    /* Fill unused ... */
  1798. X    bzero( ptr, offset );
  1799. X    ptr += offset;
  1800. X  }
  1801. X
  1802. X  /*
  1803. X   * Download.
  1804. X   */
  1805. X  if ( !vfr_convert_from_rgb( vfr, bitmap->xsize ) )
  1806. X    error( 10 );
  1807. X
  1808. X  /*
  1809. X   * If we blured the image then we need free the
  1810. X   * memory used.
  1811. X   */
  1812. X  if ( forceblur ) {
  1813. X    Free( rbase );
  1814. X    Free( gbase );
  1815. X    Free( bbase );
  1816. X  }
  1817. X}
  1818. X
  1819. Xinit_vlan( vfr, inpoint, autoincrement )
  1820. XVFR_DEV *vfr;
  1821. Xchar *inpoint;
  1822. Xint autoincrement;
  1823. X{
  1824. X  int i;
  1825. X  char *status;
  1826. X  char buffer[16];
  1827. X  char outpoint[16];
  1828. X
  1829. X  /* signal( SIGALRM, vfr_not_ready ); */
  1830. X  alarm( 30L );
  1831. X  do {
  1832. X    if ( (i= vfr_vlan_init( vfr, VFR_VLAN_625)) == -1 )
  1833. X      fprintf( stderr, "Error initializaing VFR\nRetrying\n");
  1834. X  }while ( i == -1 );
  1835. X  VPRINTF( stderr, "Video Framer ready\n" );
  1836. X
  1837. X  /* Selects Node 2 */
  1838. X  (void)Vfr_Vlan_Cmd( vfr, "ND2" );
  1839. X  /* Clear all registers */
  1840. X  (void)Vfr_Vlan_Cmd( vfr, "CL" );
  1841. X  /* Track Mode */
  1842. X  (void)Vfr_Vlan_Cmd( vfr, "TSV" );
  1843. X  /* Set pre-roll and post-roll */
  1844. X  sprintf( buffer, "PR%s", PREROLLTIME );
  1845. X  (void)Vfr_Vlan_Cmd( vfr, buffer );
  1846. X  sprintf( buffer, "PT%s", POSTROLLTIME );
  1847. X  (void)Vfr_Vlan_Cmd( vfr, buffer );
  1848. X  /* Sets inpoint */
  1849. X  sprintf( buffer, "SI%s", inpoint );
  1850. X  (void)Vfr_Vlan_Cmd( vfr, buffer );
  1851. X  /*
  1852. X   * VFR's buffer only support 1..99 for
  1853. X   * autoincrement, so if we need more we
  1854. X   * use a trick, an outpoint.
  1855. X   */
  1856. X  if ( autoincrement > 99 ) {
  1857. X    calculate_outpoint( inpoint, outpoint, autoincrement );
  1858. X    /* Sets outpoint */
  1859. X    sprintf( buffer, "SO%s", outpoint );
  1860. X    (void)Vfr_Vlan_Cmd( vfr, buffer );
  1861. X  }else {
  1862. X    /* Sets frame duration */
  1863. X    sprintf( buffer, "SD%d", autoincrement );
  1864. X    (void)Vfr_Vlan_Cmd( vfr, buffer );
  1865. X    /* Sets autoincrement */
  1866. X    sprintf( buffer, "AI%d", autoincrement );
  1867. X    (void)Vfr_Vlan_Cmd( vfr, buffer );
  1868. X  }
  1869. X  /* Goto Preroll position */
  1870. X  (void)Vfr_Vlan_Cmd( vfr, "GP" );
  1871. X
  1872. X  /* signal( SIGALRM, SIG_IGN ); */
  1873. X}
  1874. X
  1875. Xcheck_point( vfr )
  1876. XVFR_DEV *vfr;
  1877. X{
  1878. X  char *status;
  1879. X
  1880. X  /*
  1881. X   * Wait for PAUSE status.
  1882. X   */
  1883. X  do {
  1884. X    status = Vfr_Vlan_Cmd( vfr, "SR" );
  1885. X  }while( !equal( status, "PAUSE" ));
  1886. X}
  1887. X
  1888. Xcheck_status( vfr )
  1889. XVFR_DEV *vfr;
  1890. X{
  1891. X  char *status;
  1892. X  int count = 0;
  1893. X
  1894. X  /*
  1895. X   * Wait for DONE ( performed ) or ABORTED ( ... ).
  1896. X   */
  1897. X  do {
  1898. X    if ( !(count++ % 15) )
  1899. X      putchar('.'), fflush( stdout );
  1900. X    status = Vfr_Vlan_Cmd( vfr, "ES" );
  1901. X  }while ( !equal(status, "ABORTED") && !equal(status, "DONE") );
  1902. X
  1903. X  putchar('\r'), fflush(stdout);
  1904. X
  1905. X  if ( equal(status, "ABORTED") )
  1906. X    error( 11 );
  1907. X}
  1908. X
  1909. Xchar *Vfr_Vlan_Cmd( vfr, string )
  1910. XVFR_DEV *vfr;
  1911. Xchar *string;
  1912. X{
  1913. X  char *status;
  1914. X
  1915. X  status = vfr_vlan_cmd( vfr, string );
  1916. X#ifdef DEBUG
  1917. X  fprintf( stderr, "%s", status );
  1918. X#endif
  1919. X
  1920. X  return status;
  1921. X}
  1922. X
  1923. X
  1924. X/**************************************
  1925. X *
  1926. X * Library functions.
  1927. X *
  1928. X */
  1929. Xerror(code)
  1930. Xint code;
  1931. X{
  1932. X  char *usage = "\
  1933. XIncorrect parameters list\n\n\
  1934. XThree formats: \n\
  1935. X\t  i. %s [flags] <inputfile> [<inputfile> [...]]\n\
  1936. X\t ii. %s [flags] <incode> <number_of_frames> <inputfile> [<inputfile> [...]]\n\
  1937. X\tiii. %s [flags] <incode> <start> <end> <inputfile> [<inputfile> [...]]\n\n\
  1938. XWhere flags are:\n\
  1939. X\t-v --> verbose\n\
  1940. X\t-b --> blur the final image\n\
  1941. X\t-c --> center image, do not adjust to PAL\n\
  1942. X\t-r --> output is configured to rgb_625\n\
  1943. X\t-i --> initialize VFR\n\
  1944. X\t-s --> add the super\n\
  1945. X\t-n --> no blur image\n\
  1946. X\t-t --> input procedure configured to TIFF format\n\
  1947. X\t-j --> ...   ...       ...        .. JPEG format\n\
  1948. X\t-u --> ...   ...       ...        .. Utah's RLE format\n\
  1949. X\t-w --> ...   ...       ...        .. Wavefront's RLA format\n\
  1950. X\t-g --> ...   ...       ...        .. SGI's format\n\
  1951. X\t-a --> ...   ...       ...        .. Targa image format\n\
  1952. X\t-p --> ...   ...       ...        .. PBM/PGM/PPM format\n\
  1953. X\t-f --> ...   ...       ...        .. Compuserve's GIF file format\n\
  1954. X\t-x --> ...   ...       ...        .. ZSoft's PCX format\n\
  1955. X\t-! --> what about this program ?! \n";
  1956. X
  1957. X  fprintf(stderr, "%s: ", MY_NAME);
  1958. X  switch (code) {
  1959. X    case  0: fprintf(stderr, usage, MY_NAME, MY_NAME, MY_NAME );
  1960. X             break;
  1961. X    case  1: fprintf(stderr, "Cannot open file\n");
  1962. X             break;
  1963. X    case  2: fprintf(stderr, "Out of memory\n");
  1964. X             break;
  1965. X    case  3: fprintf(stderr, "Error while reading input file\n");
  1966. X             break;
  1967. X    case  4: fprintf(stderr, "Error while writing output file\n");
  1968. X             break;
  1969. X    case  5: fprintf(stderr, "Input file hasn't the selected format\n");
  1970. X             break;
  1971. X    case  6: fprintf(stderr, "File corrupt ( uncompress too bytes )\n");
  1972. X             break;
  1973. X    case  7: fprintf(stderr, "File is not a RGB image ( no 24 planes )\n");
  1974. X             break;
  1975. X    case  8: fprintf(stderr,
  1976. X                     "Uncompress failed or compressed file don't exist\n");
  1977. X             break;
  1978. X    case  9: fprintf(stderr, "Cannot open Video Framer\n");
  1979. X             break;
  1980. X    case 10: fprintf(stderr, "Error converting internal bitmap to Video Framer format\n");
  1981. X             break;
  1982. X    case 11: fprintf(stderr, "Editing aborted\n");
  1983. X             break;
  1984. X    case 12: fprintf(stderr, "Error returning child process\n");
  1985. X             break;
  1986. X    case 13: fprintf(stderr, "Cannot respawn the child process\n");
  1987. X             break;
  1988. X    case 14: fprintf(stderr, "Incode incorrect\n");
  1989. X             break;
  1990. X    case 15: fprintf(stderr, "VFR transportation timeout. Connected ?\n");
  1991. X             break;
  1992. X    case 16: fprintf(stderr, "It is very posible a core from the child\n");
  1993. X    case 17: fprintf(stderr, "Current image completed ?\n");
  1994. X             break;
  1995. X    case 18: fprintf(stderr, "Cannot get a TAG from TIFF file\n");
  1996. X             break;
  1997. X    case 19: fprintf(stderr, "Ooops, an empty PBM file ?\n");
  1998. X             break;
  1999. X    case 20: fprintf(stderr, "Unexpected end of file\n");
  2000. X             break;
  2001. X    case 21: fprintf(stderr, "Trying to use an unitializated image\n");
  2002. X             break;
  2003. X    case 99: fprintf(stderr, "Not ready\n");
  2004. X             break;
  2005. X    default: fprintf(stderr, "Unknown error code (%d)\n", code);
  2006. X             break;
  2007. X  }
  2008. X
  2009. X  if ( code ) {
  2010. X    /*
  2011. X     * Only if an internal error.
  2012. X     */
  2013. X    kill( (getpid() == parent_pid ? cpid : parent_pid), SIGUSR2 );
  2014. X  }
  2015. X
  2016. X  rm_compress();
  2017. X  exit(1);
  2018. X}
  2019. X
  2020. Xprint_copyright()
  2021. X{
  2022. X  char *msg = "\n\
  2023. Xtovcr ( v.1.1 ) - record images into a VCR using the SGI VFR.\n\
  2024. XDeveloped by Raul Rivero into the Mathematics Dept., University of Oviedo\n\n\
  2025. XWith this program you'll get HIGHER speed recording frames, chroma, center\n\
  2026. Xpaste, resample and others facilities.\n\n\
  2027. XSupported input formats:\n\n\
  2028. X\t* Pix ( Alias )  *** default ***\n\
  2029. X\t* TIFF ( using the Sam Leffler's TIFF library )\n\
  2030. X\t* RLE ( using the Spencer W. Thomas' URT library )\n\
  2031. X\t* RLA ( Wavefront )\n\
  2032. X\t* SGI ( internal Silicon Graphics file format )\n\
  2033. X\t* Targa ( Truevision )\n\
  2034. X\t* GIF ( Compuserve )\n\
  2035. X\t* PCX ( ZSoft )\n\
  2036. X\t* JPEG\n\
  2037. X\t* PBM/PGM/PPM\n\n\
  2038. XThis software is free and you can get a copy via anonymous ftp to\n\
  2039. Xtelva.ccu.uniovi.es ( 156.35.31.31, /uniovi/mathdept/src/tovcr-1.1.shar.Z )\n\
  2040. Xor via E-mail to nuevos@carreras.ccu.uniovi.es.\n\n\
  2041. XAll you get here is a minimal part of the LUG Library ( 'Libreria de\n\
  2042. XUtilidades Graficas', Graphic Utilities Library ). This supports several\n\
  2043. Xgraphic file formats, viewers on the most important architectures and\n\
  2044. Xdigital image manipulation. Also, it's free and you can get a copy from\n\
  2045. Xthe same site ( /uniovi/mathdept/src/liblug-1.0.tar.Z ).\n\n\
  2046. XThis program: (c) 1993, University of Oviedo && Raul Rivero\n\
  2047. XLUG Library:  (c) 1992, Raul Rivero\n\n";
  2048. X
  2049. X fputs( msg, stderr );
  2050. X exit( 1 );
  2051. X}
  2052. X
  2053. Xchar *Malloc(size)
  2054. Xint size;
  2055. X{
  2056. X  char *ptr;
  2057. X
  2058. X  if ((ptr = (char *) malloc(size)) == NULL)
  2059. X    error(2);
  2060. X
  2061. X  /*
  2062. X   * Usually compilers fill buffers with zeros,
  2063. X   * but ...
  2064. X   */
  2065. X  bzero( ptr, size );
  2066. X  return ptr;
  2067. X}
  2068. X
  2069. XFree( ptr )
  2070. Xvoid *ptr;
  2071. X{
  2072. X  if ( ptr != NULL )
  2073. X    free( ptr );
  2074. X}
  2075. X
  2076. XFILE *Fopen(name, mode)
  2077. Xchar *name;
  2078. Xchar *mode;
  2079. X{
  2080. X  FILE *handle;
  2081. X  int len= strlen(name) - 1;
  2082. X  int compress = 0;
  2083. X  char aux[132];
  2084. X  struct stat statbuf;
  2085. X
  2086. X  if ( mode[0] == 'r' ) {
  2087. X    /*
  2088. X     * Asking for reading, we check if file is compressed.
  2089. X     */
  2090. X    /* Is a compressed name ? */
  2091. X    compress = ( name[len] == 'Z' && name[len-1] == '.');
  2092. X
  2093. X    if (compress) {
  2094. X      /*
  2095. X       * File name is compressed.
  2096. X       */
  2097. X      Uncompress(name, aux_file);
  2098. X      if ( (handle = fopen(aux_file, mode)) == NULL) {
  2099. X        fputs( "Uncompress cannot create output file\n", stderr );
  2100. X        error( 1 );
  2101. X      }else {
  2102. X        stat( aux_file, &statbuf );
  2103. X        if ( statbuf.st_size < 1 )
  2104. X          error( 1 );
  2105. X      }
  2106. X    }else {
  2107. X      if ( (handle = fopen(name, mode)) == NULL ) {
  2108. X        /*
  2109. X         * Uncompress file failed, and compress ?
  2110. X         */
  2111. X        sprintf(aux, "%s.Z", name);
  2112. X        return( Fopen(aux, mode) );
  2113. X      }
  2114. X    }
  2115. X  }else {
  2116. X    /*
  2117. X     * Ask for writing ( we don't need check for
  2118. X     * compressed files ).
  2119. X     */
  2120. X    if ( (handle = fopen(name, mode)) == NULL)
  2121. X      error(1);
  2122. X  }
  2123. X
  2124. X  return handle;
  2125. X}
  2126. X
  2127. XFclose( handle )
  2128. XFILE *handle;
  2129. X{
  2130. X  if ( handle != stdout && handle != stdin )
  2131. X    return fclose( handle );
  2132. X  else return 0;
  2133. X}
  2134. X
  2135. XUncompress(name, aux_file)
  2136. Xchar *name, *aux_file;
  2137. X{
  2138. X  int pid, handle;
  2139. X
  2140. X  if ( pid = fork() ) {
  2141. X    /*
  2142. X     * Parent wait until the REAL 'end of days'
  2143. X     * of the child.
  2144. X     */
  2145. X    do{
  2146. X    }while( pid != wait(NULL) );
  2147. X  }else {
  2148. X    handle = creat( aux_file, 0644 );
  2149. X    close( 1 );
  2150. X    dup( handle );
  2151. X    execlp( "compress", "compress", "-dc", name, 0 );
  2152. X  }
  2153. X}
  2154. X
  2155. Xrm_compress()
  2156. X{
  2157. X  if ( access( aux_file, 0 ) == 0 )
  2158. X    unlink( aux_file );
  2159. X}
  2160. X
  2161. Xchar *read_file(handle, bytes)
  2162. XFILE *handle;
  2163. Xint *bytes;
  2164. X{
  2165. X  char *buffer;
  2166. X
  2167. X  /* Get size of file and allocate memory */
  2168. X  *bytes = (int) filelen(handle);
  2169. X  if ( *bytes > 0 ) {        /* Ok, it's a regular file. */
  2170. X    buffer = (char *) Malloc(*bytes);
  2171. X
  2172. X    /* Read it */
  2173. X    Fread(buffer, (int) (*bytes), 1, handle);
  2174. X  }else {            /* Oops!  It's a pipe. */
  2175. X    int n = 0, bufsize = 0;
  2176. X
  2177. X    /* Read in chunks of BUFSIZ. */
  2178. X    buffer = (char *) Malloc( BUFSIZ );
  2179. X    while ( (n = fread( buffer + bufsize, 1, BUFSIZ, handle )) == BUFSIZ ) {
  2180. X      bufsize += BUFSIZ;
  2181. X      buffer = (char *) realloc( buffer, bufsize + BUFSIZ );
  2182. X    }
  2183. X    if ( n >= 0 )
  2184. X      n += bufsize;
  2185. X    else
  2186. X      n = bufsize;
  2187. X    *bytes = n;
  2188. X  }
  2189. X
  2190. X  /* Return the buffer */
  2191. X  return buffer;
  2192. X}
  2193. X
  2194. Xlong filelen(handle)
  2195. XFILE *handle;
  2196. X{
  2197. X  long current_pos;
  2198. X  long len;
  2199. X
  2200. X  /* Save current position */
  2201. X  current_pos= ftell(handle);
  2202. X
  2203. X  /* Get len of file */
  2204. X  fseek(handle, 0, 2);
  2205. X  len= ftell(handle);
  2206. X
  2207. X  /* Restore position */
  2208. X  fseek(handle, current_pos, 0);
  2209. X
  2210. X  return len;
  2211. X}
  2212. X
  2213. Xfreebitmap(image)
  2214. Xbitmap_hdr *image;
  2215. X{
  2216. X
  2217. X  /* Really, is an image ? */
  2218. X  if ( image->magic != LUGUSED )
  2219. X    return 1;
  2220. X
  2221. X  switch ( image->depth ) {
  2222. X    case  8:
  2223. X            Free( image->r );
  2224. X            Free( image->cmap );
  2225. X            break;
  2226. X    case 24:
  2227. X            Free( image->r );
  2228. X            Free( image->g );
  2229. X            Free( image->b );
  2230. X            break;
  2231. X    default:
  2232. X            return 1;          /* an error, unkown depth */
  2233. X  }
  2234. X  return 0;
  2235. X}
  2236. X
  2237. Xcopy_bitmap( inbitmap, outbitmap)
  2238. Xbitmap_hdr *inbitmap, *outbitmap;
  2239. X{
  2240. X  int totalsize = inbitmap->xsize * inbitmap->ysize;
  2241. X
  2242. X  /* Really, is an image ? */
  2243. X  if ( inbitmap->magic != LUGUSED )
  2244. X    error( 21 );
  2245. X
  2246. X  outbitmap->magic = LUGUSED;
  2247. X  outbitmap->xsize = inbitmap->xsize;
  2248. X  outbitmap->ysize = inbitmap->ysize;
  2249. X  outbitmap->depth = inbitmap->depth;
  2250. X  outbitmap->colors = inbitmap->colors;
  2251. X  outbitmap->r = (byte *) Malloc( totalsize );
  2252. X  bcopy( inbitmap->r, outbitmap->r, totalsize );
  2253. X  if ( outbitmap->depth > 8 ) {
  2254. X    outbitmap->g = (byte *) Malloc( totalsize );
  2255. X    bcopy( inbitmap->g, outbitmap->g, totalsize );
  2256. X    outbitmap->b = (byte *) Malloc( totalsize );
  2257. X    bcopy( inbitmap->b, outbitmap->b, totalsize );
  2258. X  }else {
  2259. X    outbitmap->cmap = (byte *) Malloc( 3 * outbitmap->colors );
  2260. X    bcopy( inbitmap->cmap, outbitmap->cmap, 3*outbitmap->colors );
  2261. X  }
  2262. X}
  2263. X
  2264. XAtoi( string )
  2265. Xchar *string;
  2266. X{
  2267. X  int aux = -1234;
  2268. X
  2269. X  sscanf( string, "%d", &aux );
  2270. X  if ( aux == -1234 )
  2271. X    error( 0 );
  2272. X
  2273. X  return aux;
  2274. X}
  2275. X
  2276. Xisnumber( c )
  2277. Xchar *c;
  2278. X{
  2279. X  while ( *c )
  2280. X    if ( !isdigit(*c++) )
  2281. X      return 0;
  2282. X
  2283. X  return 1;
  2284. X}
  2285. X
  2286. Xbyte *shorttobyte( in, out, size )
  2287. Xshort *in;
  2288. Xbyte *out;
  2289. Xint size;
  2290. X{
  2291. X  byte *ptr = out;
  2292. X
  2293. X  /*
  2294. X   * If the output buffer is NULL then we need allocate
  2295. X   * memory, so ...
  2296. X   */
  2297. X  if ( out == NULL ) {
  2298. X    ptr = out = (byte *) Malloc( size );
  2299. X  }
  2300. X
  2301. X  while ( size-- ) {
  2302. X    *ptr++ = *in++;
  2303. X  }
  2304. X
  2305. X  return out;
  2306. X}
  2307. X
  2308. X
  2309. X/*
  2310. X * Return the number of bits ( -1 ) to represent a given
  2311. X * number of colors ( ex: 256 colors => 7 ).
  2312. X */
  2313. Xno_bits( colors )
  2314. Xint colors;
  2315. X{
  2316. X  register int bits= 0;
  2317. X
  2318. X  colors--;
  2319. X  while ( colors >> bits )
  2320. X    bits++;
  2321. X
  2322. X  return (bits-1);
  2323. X}
  2324. X
  2325. END_OF_FILE
  2326.   if test 53392 -ne `wc -c <'tovcr.c.B'`; then
  2327.     echo shar: \"'tovcr.c.B'\" unpacked with wrong size!
  2328.   elif test -f 'tovcr.c.A'; then 
  2329.     echo shar: Combining  \"'tovcr.c'\" \(99170 characters\) 
  2330.     cat 'tovcr.c.A' 'tovcr.c.B' > 'tovcr.c' 
  2331.     if test 99170 -ne `wc -c <'tovcr.c'`; then 
  2332.       echo shar: \"'tovcr.c'\" combined with wrong size! 
  2333.     else 
  2334.       rm tovcr.c.A tovcr.c.B 
  2335.     fi 
  2336.   fi
  2337.   # end of 'tovcr.c.B'
  2338. fi
  2339. echo shar: End of archive 2 \(of 4\).
  2340. cp /dev/null ark2isdone
  2341. MISSING=""
  2342. for I in 1 2 3 4 ; do
  2343.     if test ! -f ark${I}isdone ; then
  2344.     MISSING="${MISSING} ${I}"
  2345.     fi
  2346. done
  2347. if test "${MISSING}" = "" ; then
  2348.     echo You have unpacked all 4 archives.
  2349.     rm -f ark[1-9]isdone
  2350. else
  2351.     echo You still must unpack the following archives:
  2352.     echo "        " ${MISSING}
  2353. fi
  2354. exit 0
  2355. exit 0 # Just in case...
  2356.