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

  1. Newsgroups: comp.sources.misc
  2. From: nuevos@hp400.ccu.uniovi.es (Raul y Quique)
  3. Subject: v38i057:  tovcr - Record/Display images into the Video Frame, Part01/04
  4. Message-ID: <csm-v38i057=tovcr.093829@sparky.Sterling.COM>
  5. X-Md4-Signature: d3612c61ba6e335d0bacc2cccda9f103
  6. Sender: kent@sparky.sterling.com (Kent Landfield)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 14:39:50 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 57
  13. Archive-name: tovcr/part01
  14. Environment: SGI
  15.  
  16. tovcr is a program to show/record into a video deck using the SGI's Video 
  17. FRamer and V-lan controlled hardware.
  18.  
  19. We had a problem, a great problem, in our Dept. when we began to record 
  20. frames into our Betacam, the time !!!. An uncompressed image spent 40 
  21. seconds to be recorded ( wow ! ), and this is inacceptable if you have
  22. 500 frames. 
  23.  
  24. tovcr supports several image file formats and it's very fast ( now we
  25. need 8 seconds to record a COMPRESSED frame ).
  26.  
  27. Currently file formats supported:
  28.  
  29.     i.     Alias "pix"
  30.     ii.    Wavefront's RLA
  31.     iii.    URT's RLE (*)
  32.     iv.    Aldus' TIFF (*)
  33.     v.    SGI's internal RGB
  34.     vi.    Trevision's TARGA
  35.     vii.    Compuserve's GIF
  36.     viii.    ZSoft's PCX
  37.     ix.    JPEG (*)
  38.     x.    PBM/PGM/PPM
  39.  
  40. Also, it has impleted a few digital image effects:
  41.  
  42.     i.    resample
  43.     ii.    blur
  44.     iii.    chroma
  45.     iv.    paste
  46.  
  47. Refer to manual pages for more information.
  48.  
  49. Raul Rivero
  50. Mathematics Dept.
  51. University of Oviedo
  52. ( nuevos@carreras.ccu.uniovi.es )
  53. ------------------------
  54. #! /bin/sh
  55. # This is a shell archive.  Remove anything before this line, then feed it
  56. # into a shell via "sh file" or similar.  To overwrite existing files,
  57. # type "sh file -c".
  58. # Contents:  README README.code tovcr.1 tovcr.c.A
  59. # Wrapped by kent@sparky on Sun Jul 11 19:05:24 1993
  60. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  61. echo If this archive is complete, you will see the following message:
  62. echo '          "shar: End of archive 1 (of 4)."'
  63. if test -f 'README' -a "${1}" != "-c" ; then 
  64.   echo shar: Will not clobber existing file \"'README'\"
  65. else
  66.   echo shar: Extracting \"'README'\" \(1977 characters\)
  67.   sed "s/^X//" >'README' <<'END_OF_FILE'
  68. X
  69. X'tovcr' is a program to show/record into a video deck using the SGI's Video 
  70. XFRamer and V-lan controlled hardware.
  71. X
  72. XWe had a problem, a great problem, in our Dept. when we began to record 
  73. Xframes into our Betacam, the time !!!. An uncompressed image spent 40 
  74. Xseconds to be recorded ( wow ! ), and this is inacceptable if you have
  75. X500 frames. 
  76. X
  77. X'tovcr' supports several image file formats and it's very fast ( now we
  78. Xneed 8 seconds to record a COMPRESSED frame ).
  79. X
  80. XCurrently file formats supported:
  81. X
  82. X    i.     Alias "pix"
  83. X
  84. X    ii.    Wavefront's RLA
  85. X
  86. X    iii.    URT's RLE (*)
  87. X
  88. X    iv.    Aldus' TIFF (*)
  89. X
  90. X    v.    SGI's internal RGB
  91. X
  92. X    vi.    Trevision's TARGA
  93. X
  94. X    vii.    Compruserve's GIF
  95. X
  96. X    viii.    ZSoft's PCX
  97. X
  98. X    ix.    JPEG (*)
  99. X
  100. X    x.    PBM/PGM/PPM
  101. X
  102. X
  103. XAlso, it has impleted a few digital image effects:
  104. X
  105. X    i.    resample
  106. X
  107. X    ii.    blur
  108. X
  109. X    iii.    chroma
  110. X
  111. X    iv.    paste
  112. X
  113. X
  114. XRefer to manual pages for more information.
  115. X
  116. X
  117. X'tovcr' was derived from the LUG Library ( Libreria de Utilidades Graficas,
  118. XGraphic Utilities Library ). LUG supports more file formats, viewers on the
  119. Xmost important architectures and too much digital image editing ( but not 
  120. Xnecessaries here ). You can get a copy from telva.ccu.uniovi.es ( 156.35.31.31,
  121. X/pub/uniovi/mathdept/lug-1.0.tar.Z ).
  122. X
  123. X
  124. X(*)
  125. X        This is supported if you have the:
  126. X
  127. X                - Utah Raster Toolkit by Spencer W. Thomas
  128. X                        princeton.edu           /pub/Graphics/urt
  129. X                        telva.ccu.uniovi.es     /pub/graphics/Image/urt*
  130. X
  131. X                - LibTIFF by Sam Leffler
  132. X                        sgi.com                 /pub/sgi/libtiff/libtiff*
  133. X                        telva.ccu.uniovi.es     /pub/graphics/Image/libtiff*
  134. X
  135. X                - LibJPEG by THomas G. Lane
  136. X                        ftp.uu.net              /graphics/jpeg/jpeg*
  137. X            telva.ccu.uniovi.es    /pub/graphics/Image/jpeg/jpeg*
  138. X
  139. X        ... and check the paths of the Makefile.
  140. X
  141. XThanks to all them.
  142. X
  143. X-----
  144. XRaul Rivero
  145. XMathematics Dept.
  146. XUniversity of Oviedo
  147. X( nuevos@carreras.ccu.uniovi.es )
  148. END_OF_FILE
  149.   if test 1977 -ne `wc -c <'README'`; then
  150.     echo shar: \"'README'\" unpacked with wrong size!
  151.   fi
  152.   # end of 'README'
  153. fi
  154. if test -f 'README.code' -a "${1}" != "-c" ; then 
  155.   echo shar: Will not clobber existing file \"'README.code'\"
  156. else
  157.   echo shar: Extracting \"'README.code'\" \(3357 characters\)
  158.   sed "s/^X//" >'README.code' <<'END_OF_FILE'
  159. X
  160. XYou must read this file if you wanna understand the code of the 
  161. Xprogram.
  162. X
  163. XFirst of all, the program has been configured to the PAL system. This
  164. Xis controlled when you open the connection with the video framer
  165. X( the 'open_video' subroutine ), two #define fix these names ( 
  166. XDEFAULT_VFR_BUFFER and OPTIONAL_VFR_BUFFER ), our default buffer name
  167. Xis "r_y_625" ( Betacam ) and optional is "rgb_625". You need change
  168. Xthis if you use another video system.
  169. X
  170. XIf you change the system, also probably you need change the XSIZE and
  171. XYSIZE defines. These fix the size of the image. Our render program sets
  172. Xa size of 780x576 for the PAL system, but when we recorded that image 
  173. Xinto the Betacam it was cuted ( you get it ?, the image, not the 
  174. XBetacam ). So, we applied proof and error and fix 'our' PAL system
  175. Xsize to 710x576. So if you change the video norm ...
  176. X
  177. XIn the PAL system, we have 25 frames per second but in NTSC there are
  178. X30 f.p.s. So you need change the FPS define ( PAL => 24, NTSC => 29, 
  179. Xthe last frame, not the number of frames ).
  180. X
  181. XThere is another important define, CHROMABORDER. It is what the 
  182. Xprogram understand as transparent ( from 0 to CHROMABORDER, where this
  183. Xnumber is r+g+b ).
  184. X
  185. XThe program works whit compressed files but it's very important how
  186. Xyou ( well, the program ) uncompressed it. There are two procces
  187. Xworking:
  188. X
  189. Xi.    
  190. X    The parent process handles the video recorder. When a frame
  191. X    has been recorded, it sends a signal to the child ( then a new
  192. X    image can be loaded into the VFR ), release a new child and
  193. X    waits for the first child to record that frame.
  194. X
  195. Xii.    
  196. X    The child process gets ready and load a new frame into VFR 
  197. X    ( when the parent process allows it ).
  198. X
  199. X
  200. X, and now is when how you uncompress the file is very important. If
  201. Xyou use a system(...) library subroutine to execute the uncompress
  202. Xprocess what you really get is something like:
  203. X
  204. X        if ( fork() )
  205. X            wait( NULL );
  206. X        else exe...( "uncompress", ... );
  207. X
  208. Xso, the child is wait-ing. Now the problem: if the child is wait-ing
  209. Xwhen the pattern sends a signal ( and altough this is catched ) the
  210. Xwait is released ( but the uncompress isn't finished !!! ). The 
  211. Xsolution is the 'Uncompress' subroutine of the program, the child 
  212. Xwaits for the end of the uncompress process and only is released by
  213. Xthis. Ok ? ( so, if you use another subroutine to uncompress files,
  214. Xplease check this or you can get a 'beatiful' core ).
  215. X
  216. XBetter algorithms could be implemented for some digital editing
  217. Xfunctions ( examples: blur and resample ) but what we wanna was
  218. Xspeed ( when you have 500 frames, well ... the speed of the program
  219. Xis the most important thing ). But if you can do better ...
  220. X
  221. XAlso, some supported file format readers could be improved.
  222. X
  223. XWell, you know, nothing is perfect and I'll wait your comments.
  224. X
  225. XThe code has a lot of comments, so you can refer to it for further
  226. Xexplanations.
  227. X
  228. X
  229. XWhen we record an animation, we always need something different: a
  230. Xmoving background, a super, a blur with a mask, etc. So we always
  231. Xchange the code of tovcr, but this is the base. It's too easy adapt
  232. Xit for what we need. You get it ?, it's too important understand the
  233. Xcode because it's not a closed utility.
  234. X
  235. X
  236. XPlease, mail me if you have some question, suggestion, you find
  237. Xa bug, etc ...
  238. X
  239. X
  240. XEnjoy !.
  241. X
  242. X-----
  243. XRaul Rivero
  244. XMathematics Dept.
  245. XUniversity of Oviedo
  246. X( nuevos@carreras.cuu.uniovi.es )
  247. END_OF_FILE
  248.   if test 3357 -ne `wc -c <'README.code'`; then
  249.     echo shar: \"'README.code'\" unpacked with wrong size!
  250.   fi
  251.   # end of 'README.code'
  252. fi
  253. if test -f 'tovcr.1' -a "${1}" != "-c" ; then 
  254.   echo shar: Will not clobber existing file \"'tovcr.1'\"
  255. else
  256.   echo shar: Extracting \"'tovcr.1'\" \(3635 characters\)
  257.   sed "s/^X//" >'tovcr.1' <<'END_OF_FILE'
  258. X.\"
  259. X.\" tovcr's man pages.
  260. X.\" Copyright (c) 1992, University of Oviedo and Raul Rivero.
  261. X.\"
  262. X.TH tovcr 1 "Mon Aug 17 1992" 1
  263. X.UC 4
  264. X.SH NAME
  265. Xtovcr \- Record/Display images into the Video Frame.
  266. X.SH SYNOPSIS
  267. X.B tovcr
  268. X[
  269. X.B \-vcrisntuwgjpafx\!
  270. X]
  271. X.I inputfile
  272. X[
  273. X.I inputfile
  274. X]
  275. X.TP
  276. X.B tovcr
  277. X[
  278. X.B \-vcrisntuwgjpafx\!
  279. X]
  280. X.I incode number_of_frames inputfile
  281. X[
  282. X.I inputfile
  283. X[...] ]
  284. X.TP
  285. X.B tovcr
  286. X[
  287. X.B \-vcrisntuwgjpafx\!
  288. X]
  289. X.I incode start end inputfile
  290. X[
  291. X.I inputfile
  292. X[...] ]
  293. X.SH DESCRIPTION
  294. XThis program show/record images into the Video Framer with
  295. Xhigher speed than original utilities. Also, you can use compressed (
  296. X.IR compress (1)
  297. X) files, a lot of supported formats, composition and other facilities.
  298. X.PP
  299. XFirst use format load images into the Video Framer, no record.
  300. X.PP
  301. XSecond format load and record the given frame
  302. X.I number_of_frames
  303. Xtimes into the video recorder ( by example if you use the PAL system
  304. Xyou need 25 to record one second this frame ).
  305. X.PP
  306. XThe last format is used in animation, you have several frames
  307. X( starting at
  308. X.I start
  309. Xand ending at
  310. X.I end
  311. X) being numbered
  312. X.I inputfile\.number[.Z].
  313. XDuration per frame will be 1/25 seconds.
  314. X.PP
  315. XIf you give several sequences ( names ), the first of all will
  316. Xbe the basea and the rest will be pasted over as a chroma ( be careful
  317. Xwith the \fItransparent\fP color ).
  318. X.PP
  319. XThe size of frames has been fixed to
  320. X.I 710x576
  321. Xpixels. This is a valid format to \fIR-Y\ 625\fP ( \fIBetacam's\ PAL\fP ).
  322. XSo, it's possible you need change this values if you use other video
  323. Xformat.
  324. X.SH OPTIONS
  325. X.TP
  326. X.B \-v
  327. XVerbose output.
  328. X.TP
  329. X.B \-b
  330. XBlur the goal image before download to the Video Framer.
  331. X.TP
  332. X.B \-c
  333. XCenter the image, will be not adjusted to the internal given
  334. Xresolution. By default, the image will be \fIresampled\FP if
  335. Xits size is not equal to that format, and the image will cover
  336. Xall area of the video display.
  337. X.TP
  338. X.B \-r
  339. XThe output buffer will be configured to rgb_625 ( internally invoques
  340. Xnext option ).
  341. X.TP
  342. X.B \-i
  343. XReset the Video Framer and, by default, the r_y_625 format is
  344. Xconfigured. It's necesary if you had used another format ( by exmaple
  345. Xif previous option had been used ).
  346. X.TP
  347. X.B \-s
  348. XBefore the requested image will be loaded into the Video Framer
  349. Xbuffer, an \fIsuper\fP will be layed over it. Really, the super
  350. Xis another image fixed intermally ( you would get this efect if
  351. Xyou would give that name as the last parameter ).
  352. X.TP
  353. X.B \-n
  354. XIf the size of the input image was lower than the required then
  355. Xit will be \fIresampled\fP and a \fIblur\fP effect will be aplied ( not 
  356. Xif the size it's greater ).
  357. X.TP
  358. X.B \-t
  359. XInput file format will be TIFF.
  360. X.TP
  361. X.B \-u
  362. XInput file format will be the Utah's Univ. RLE.
  363. X.TP
  364. X.B \-w
  365. XInput file format will be the Wavefront's RLA.
  366. X.TP
  367. X.B \-g
  368. XInput file format will be the internal SGI format.
  369. X.TP
  370. X.B \-a
  371. XInput file format will be Targa.
  372. X.TP
  373. X.B \-f
  374. XInput file format will be the Compuserve's GIF
  375. X.TP
  376. X.B \-x
  377. XInput file format will be ZSoft's PCX.
  378. X.TP
  379. X.B \-j
  380. XInput file format will be JPEG.
  381. X.TP
  382. X.B \-p
  383. XInput file format will be the Jef Poskanzer's PBM/PGM/PPM
  384. X.TP
  385. X.B \-!
  386. XWhat about this program ?!.
  387. X.SH WARNING
  388. XThis version had been configured to the PAL system. If you use
  389. Xother system you need change \fIadd_incode\fP and the name of
  390. Xthe Video Framer buffer.
  391. X.PP
  392. XIf you get the message \fIEditing aborted\fP, don't worry. You
  393. Xcan continue from the last frame recorded. It appears as 
  394. Xthe V-Lan could not synchronize with the video recorder. I don't
  395. Xknow why.
  396. X
  397. X.SH SEE ALSO
  398. X.IR lug (3G),
  399. X.I Videoframer\ Programers\ Manual,
  400. X.IR urt (1),
  401. X.IR tiff (1),
  402. X.IR jpeg (1),
  403. X.IR pbm (1).
  404. X.SH AUTHOR
  405. XRaul Rivero, Mathematics Department, University of Oviedo.
  406. END_OF_FILE
  407.   if test 3635 -ne `wc -c <'tovcr.1'`; then
  408.     echo shar: \"'tovcr.1'\" unpacked with wrong size!
  409.   fi
  410.   # end of 'tovcr.1'
  411. fi
  412. if test -f 'tovcr.c.A' -a "${1}" != "-c" ; then 
  413.   echo shar: Will not clobber existing file \"'tovcr.c.A'\"
  414. else
  415.   echo shar: Extracting \"'tovcr.c.A'\" \(45778 characters\)
  416.   sed "s/^X//" >'tovcr.c.A' <<'END_OF_FILE'
  417. X/*
  418. X * This software is copyrighted as noted below.  It may be freely copied,
  419. X * modified, and redistributed, provided that the copyright notice is
  420. X * preserved on all copies.
  421. X *
  422. X * There is no warranty or other guarantee of fitness for this software,
  423. X * it is provided solely "as is".  Bug reports or fixes may be sent
  424. X * to the author, who may or may not act on them as he desires.
  425. X *
  426. X * You may not include this software in a program or other software product
  427. X * without supplying the source, or without informing the end-user that the
  428. X * source is available for no extra charge.
  429. X *
  430. X * If you modify this software, you should include a notice giving the
  431. X * name of the person performing the modification, the date of modification,
  432. X * and the reason for such modification.
  433. X */
  434. X/*
  435. X * tovcr.c - display/record images on a Video Recorder using the SGI VFR.
  436. X *
  437. X * Author:      Raul Rivero
  438. X *              Mathematics Dept.
  439. X *              University of Oviedo
  440. X * Date:        Mon Aug 17 1992
  441. X * Copyright (c) 1992, Raul Rivero
  442. X *
  443. X */
  444. X/*
  445. X * Revisions:
  446. X *
  447. X * 1.1
  448. X *      Raul Rivero
  449. X *      Mathematics Dept.
  450. X *      University of Oviedo
  451. X *      Mon May 17 1993
  452. X *      ( nuevos@carreras.ccu.uniovi.es )
  453. X *
  454. X *      i.  New file formats supported ( JPEG, PBM/PGM/PPM, 24bits-PCX ).
  455. X *      ii. Some little fixes.
  456. X *
  457. X * -----
  458. X */
  459. X
  460. X#include <stdio.h>
  461. X#include <math.h>
  462. X#include <signal.h>
  463. X#include <string.h>
  464. X#include <sys/types.h>
  465. X#include <sys/wait.h>
  466. X#include <sys/stat.h>
  467. X#include "image.h"
  468. X#include "vframer.h"
  469. X
  470. X#ifdef iTIFF
  471. X#   include "tiffio.h"
  472. X#   define GetField(h, t, v)       if ( !TIFFGetField(h, t, v ) ) error(18);
  473. X#endif  /* iTIFF */
  474. X
  475. X#ifdef iRLE
  476. X#   include "rle.h"
  477. X#   define PSEUDOCOLOR             1
  478. X#   define DIRECTCOLOR             2
  479. X#   define TRUECOLOR               3
  480. X#   define GRAYSCALE               4
  481. X#endif  /* iRLE */
  482. X
  483. X#ifdef iJPEG
  484. X#   include <jinclude.h>
  485. X#endif  /* iJPEG */
  486. X
  487. X/*
  488. X * The magic flag.
  489. X */
  490. X#define LUGUSED                     12345   /* why not ? */
  491. X
  492. X#define byte                        unsigned char
  493. X#define ushort                      unsigned short
  494. X#define Fread(p, s, n, f)           if ( fread(p, s, n, f) != n ) error(3)
  495. X#define Fwrite(p, s, n, f)          if ( fwrite(p, s, n, f) != n ) error(4)
  496. X#define VPRINTF                     if (verbose) fprintf
  497. X#define VFLUSH                      if (verbose) fflush
  498. X#define NOBLUR                      0
  499. X#define BLUR                        1
  500. X#define CHROMABORDER                20
  501. X#define PREROLLTIME                 "3:00"
  502. X#define POSTROLLTIME                "1:00"
  503. X#define AUTOINCREMENT               1
  504. X
  505. X/*
  506. X * This is the name of the 'super' image. Be careful with
  507. X * the file format.
  508. X */
  509. X#define SUPERNAME                   "/usr/a3demo/bin/fuentes/super"
  510. X
  511. X/* More esasy */
  512. X#define equal(s, t)                 ( strstr(s,t) != NULL ? 1 : 0 )
  513. X
  514. X/*
  515. X * ==========================================
  516. X *  REMEMBER!!!
  517. X *      I'm working with R-Y PAL, but you ?.
  518. X * ==========================================
  519. X *
  520. X * Be careful with the next 3 blocks of defines !!!.
  521. X */
  522. X
  523. X/*
  524. X * The image will be adjusted to ...
  525. X *
  526. X */
  527. X#define XFBUFFER                    780
  528. X#define YFBUFFER                    576
  529. X#define XSIZE                       710
  530. X#define YSIZE                       576
  531. X#define VFR_LINESIZE                1024
  532. X
  533. X/*
  534. X * Video FRamer default buffer names.
  535. X */
  536. X#define DEFAULT_VFR_BUFFER          "r_y_625"
  537. X#define OPTIONAL_VFR_BUFFER         "rgb_625"
  538. X
  539. X/*
  540. X * Last frame into SMPTE ( PAL has 25 frames per
  541. X * second ==> 24 ).
  542. X */
  543. X#define FPS                         24
  544. X
  545. X/*
  546. X * TGA's defines.
  547. X */
  548. X#define TGAINTERLACED(a)            ( (a) & 0xc0 )
  549. X#define TGAFLIP(a)                  ( ((a) & 0x20) ? 0 : 1)
  550. X#define TGA_MAPPED                  1
  551. X#define TGA_RGB                     2
  552. X#define TGA_RLE_MAPPED              9
  553. X#define TGA_RLE_RGB                 10
  554. X
  555. X/*
  556. X * GIF's defines.
  557. X */
  558. X#define GIFHEADER                   "GIF87a"
  559. X#define GIFIMGSEPAR                 ','
  560. X#define ENDGIF                      ';'
  561. X#define GIFEXISTCOLOR               (1 << 7)
  562. X#define GIFINTERLAZE                (1 << 6)
  563. X
  564. X/*
  565. X * PCX's defines
  566. X */
  567. X#define PCX_HEADERSIZE              128
  568. X#define PCX_MAGICNUMBER             0x0a
  569. X#define PCX_256COLORS               0x0c
  570. X#define PCX_COMPRESS                0xc0
  571. X
  572. X#ifdef USE_STDLIB_H
  573. X#   include <stdlib.h>
  574. X#else
  575. X#   ifdef VOID_STAR
  576. X        extern void *malloc();
  577. X#   else
  578. X        extern char *malloc();
  579. X#   endif /* VOID_STAR */
  580. X    extern void free();
  581. X#endif /* USE_STDLIB_H */
  582. X
  583. X#ifndef BSD
  584. X#   define bzero(s, n)             memset((s), 0, (n))
  585. X#   define bcopy(f, t, c)          memcpy((t), (f), (c))
  586. X#endif /* BSD */
  587. X
  588. X/*
  589. X * Pointer to funtions.
  590. X */
  591. Xtypedef int (* ifunptr)();
  592. X
  593. X/*
  594. X * My bitmap header.
  595. X */
  596. Xtypedef struct {
  597. X        int xsize, ysize;
  598. X        int depth;
  599. X        int colors;
  600. X        byte *r, *g, *b;
  601. X        byte *cmap;
  602. X        int magic;
  603. X} bitmap_hdr;
  604. X
  605. X/*
  606. X * Wavefront's RLA format headers.
  607. X */
  608. Xtypedef struct {
  609. X   short left, right, bottom, top;
  610. X} WINDOW_S;
  611. X
  612. Xtypedef struct {
  613. X   WINDOW_S     window;
  614. X   WINDOW_S     act_window;
  615. X   short        frame;
  616. X   short        storage_type;
  617. X   short        num_chan;
  618. X   short        num_matte;
  619. X   short        num_aux;
  620. X   short        aux_mask;
  621. X   char         gamma[16];
  622. X   char         red_pri[24];
  623. X   char         green_pri[24];
  624. X   char         blue_pri[24];
  625. X   char         white_pt[24];
  626. X   long         job_num;
  627. X   char         name[128];
  628. X   char         desc[128];
  629. X   char         program[64];
  630. X   char         machine[32];
  631. X   char         user[32];
  632. X   char         date[20];
  633. X   char         aspect[32];
  634. X   char         chan[32];
  635. X   char         space[128];
  636. X} RLA_HEADER;
  637. X
  638. X/*
  639. X * Targa header.
  640. X */
  641. Xtypedef struct {
  642. X        byte   num_id;
  643. X        byte   cmap_type;
  644. X        byte   image_type;
  645. X        ushort cmap_orign;
  646. X        ushort cmap_length;
  647. X        byte   cmap_entry_size;
  648. X        ushort xorig, yorig;
  649. X        ushort xsize, ysize;
  650. X        byte   pixel_size;
  651. X        byte   image_descriptor;
  652. X} tga_hdr;
  653. X
  654. X/*
  655. X * Alias header.
  656. X */
  657. Xtypedef struct {
  658. X        short xsize, ysize;
  659. X        short xinit, yinit;
  660. X        short depth;
  661. X} alias_hdr;
  662. X
  663. Xtypedef byte color_map[3];
  664. X
  665. X/*
  666. X * Prototypes ( only what is necesary ).
  667. X */
  668. XFILE *Fopen();
  669. Xchar *Malloc();
  670. Xlong filelen();
  671. Xchar *read_file();
  672. Xbyte *create_bw_pallete();
  673. Xbyte *zoom();
  674. Xbyte *blur();
  675. Xint fparent_ready();
  676. Xint fchild_ready();
  677. XVFR_DEV *open_video();
  678. Xint vfr_not_ready();
  679. Xint error_from_other();
  680. Xint press_break();
  681. Xchar *Vfr_Vlan_Cmd();
  682. Xbyte *shorttobyte();
  683. Xbyte *read_gif_screen_hdr();
  684. Xbyte *read_gif_image_hdr();
  685. Xbyte *unblockgif();
  686. Xbyte *flip();
  687. X#ifdef iJPEG
  688. X    void get_jpeg_header();
  689. X    void get_jpeg_cmap();
  690. X    void get_jpeg_row();
  691. X    void end_put_jpeg();
  692. X    void d_ui_method_selection();
  693. X#endif  /* iJPEG */
  694. X
  695. X/*
  696. X * This pointer contents the funtion to read the
  697. X * input image file format.
  698. X */
  699. Xifunptr read_image;
  700. X
  701. X/*
  702. X * Current formats supported.
  703. X */
  704. Xint read_alias_file();
  705. Xint read_rla_file();
  706. Xint read_sgi_file();
  707. Xint read_tga_file();
  708. Xint read_gif_file();
  709. Xint read_pcx_file();
  710. Xint read_pbm_file();
  711. X#ifdef iTIFF
  712. X    int read_tiff_file();
  713. X#endif
  714. X#ifdef iRLE
  715. X    int read_rle_file();
  716. X#endif
  717. X#ifdef iJPEG
  718. X    int read_jpeg_file();
  719. X#endif
  720. X
  721. X/*
  722. X * The name of the program and an auxiliar file name
  723. X * to uncompress input file ( if compressed ).
  724. X */
  725. Xchar *MY_NAME;
  726. Xchar aux_file[40];
  727. X
  728. X/* Used by the GIF reader */
  729. Xint lug_read_code_position;
  730. X
  731. X/* Used by the JPEG reader */
  732. X#ifdef iJPEG
  733. X    static bitmap_hdr *jpeg_image;
  734. X    byte *jpeg_r, *jpeg_g, *jpeg_b;
  735. X#endif  /* iJPEG */
  736. X
  737. X/* IDs for child and parent process */
  738. Xint cpid, parent_pid;
  739. X
  740. X/* We'll simulate semaphores with ... */
  741. Xint child_ready, parent_ready;
  742. X
  743. X/*
  744. X * Flags ...
  745. X */
  746. Xint verbose = 0;
  747. Xint super = 0;
  748. Xint center = 0;
  749. Xint resetvfr = 0;
  750. Xint rgbvfr = 0;
  751. Xint blurflag = 0;
  752. Xint record_sequence = 0;
  753. Xint forceblur = 0;
  754. X
  755. Xmain(argc, argv)
  756. Xint argc;
  757. Xchar **argv;
  758. X{
  759. X  register int i;
  760. X  char *inpoint;
  761. X  int start, end;
  762. X
  763. X  MY_NAME = argv[0];
  764. X  parent_pid = getpid();
  765. X
  766. X  /* Default is Alias "pix" format */
  767. X  read_image = read_alias_file;
  768. X
  769. X  /*
  770. X   * Get options ( some day I'll build a procedure ).
  771. X   */
  772. X  if ( argc > 1 ) {             /* else core on SGI */
  773. X   while ( argv[1][0] == '-' ) {
  774. X      for ( i = 1; argv[1][i]; i++ ) {
  775. X        switch ( argv[1][i] ) {
  776. X          case 'v':
  777. X                verbose++;
  778. X                break;
  779. X          case 'b':
  780. X                forceblur++;
  781. X                break;
  782. X          case 'c':
  783. X                center++;
  784. X                break;
  785. X          case 's':
  786. X                super++;
  787. X                break;
  788. X          case 'i':
  789. X                resetvfr++;
  790. X                break;
  791. X          case 'r':
  792. X                rgbvfr++;
  793. X                resetvfr++;
  794. X                break;
  795. X          case 'n':
  796. X                blurflag++;;
  797. X                break;
  798. X#ifdef iTIFF
  799. X          case 't':
  800. X                read_image = read_tiff_file;
  801. X                break;
  802. X#endif
  803. X#ifdef iRLE
  804. X          case 'u':
  805. X                read_image = read_rle_file;
  806. X                break;
  807. X#endif
  808. X#ifdef iJPEG
  809. X          case 'j':
  810. X                read_image = read_jpeg_file;
  811. X                break;
  812. X#endif
  813. X          case 'w':
  814. X                read_image = read_rla_file;
  815. X                break;
  816. X          case 'g':
  817. X                read_image = read_sgi_file;
  818. X                break;
  819. X          case 'a':
  820. X                read_image = read_tga_file;
  821. X                break;
  822. X          case 'f':
  823. X                read_image = read_gif_file;
  824. X                break;
  825. X          case 'x':
  826. X                read_image = read_pcx_file;
  827. X                break;
  828. X          case 'p':
  829. X                read_image = read_pbm_file;
  830. X                break;
  831. X          case '!':
  832. X                print_copyright();
  833. X                break;
  834. X          default :
  835. X                error( 0 );
  836. X                break;
  837. X        }
  838. X      }
  839. X      argv++;
  840. X      argc--;
  841. X    }
  842. X  }
  843. X
  844. X  if ( argc < 2 )
  845. X    error( 0 );
  846. X
  847. X  /* Skip the command name */
  848. X  argv++;
  849. X
  850. X  /*
  851. X   * Auxiliar file name where uncompress images if
  852. X   * it's necesary.
  853. X   */
  854. X  sprintf(aux_file, "/usr/tmp/spix%d", parent_pid);
  855. X
  856. X  if ( is_a_incode( *argv ) ) {
  857. X    /*
  858. X     * We wanna record frames, so we read the incode.
  859. X     */
  860. X    inpoint = *argv++;
  861. X    start = Atoi( *argv++ );
  862. X    if ( isnumber( *argv ) ) {
  863. X      /*
  864. X       * Hey!, it's a number. Ok, we wanna record a
  865. X       * sequence of animated frames.
  866. X       */
  867. X      record_sequence++;
  868. X      end = Atoi( *argv++ );
  869. X      /* Number of images to fade */
  870. X      i = argc - 4;
  871. X    }else {
  872. X      /*
  873. X       * We wanna record only a frame.
  874. X       */
  875. X      end = start;
  876. X      i = argc - 3;
  877. X    }
  878. X
  879. X    /*
  880. X     * Ok, all ready ...
  881. X     */
  882. X    to_video( inpoint, start, end, i, argv );
  883. X  }else {
  884. X    /*
  885. X     * What we wanna is display a image into the VFR, don't
  886. X     * record it.
  887. X     */
  888. X    show_vfr_image( argv, argc - 1 );
  889. X  }
  890. X
  891. X  rm_compress();
  892. X}
  893. X
  894. Xshow_vfr_image( original_names, no_frames )
  895. Xchar **original_names;
  896. Xint no_frames;
  897. X{
  898. X  register int i;
  899. X  VFR_DEV *vfr;
  900. X  bitmap_hdr outbitmap;
  901. X  ifunptr save_read_interface;
  902. X
  903. X  /*
  904. X   * Connect with VFR interface.
  905. X   */
  906. X  vfr = ( VFR_DEV *) open_video();
  907. X
  908. X  /*
  909. X   * Create the background image.
  910. X   */
  911. X   create_solid_image( &outbitmap, XFBUFFER, YFBUFFER, 0, 0, 0 );
  912. X
  913. X   /*
  914. X    * Load all the imames.
  915. X    */
  916. X   for ( i = 0; i < no_frames; i++ ) {
  917. X     add_image( &outbitmap, original_names[i], center );
  918. X   }
  919. X
  920. X   if ( super ) {
  921. X     /*
  922. X      * Glue the super to end image ( but we need use
  923. X      * Alias interface ).
  924. X      */
  925. X     save_read_interface = read_image;
  926. X     read_image = read_alias_file;
  927. X     add_image( &outbitmap, SUPERNAME, 0 );
  928. X     /* Restores old interface */
  929. X     read_image = save_read_interface;
  930. X   }
  931. X
  932. X   /*
  933. X    * Outbitmap is the new image.
  934. X    */
  935. X   write_vfr( vfr, &outbitmap );
  936. X   freebitmap( &outbitmap );
  937. X}
  938. X
  939. Xto_video( inpoint, start, end, no_frames, original_names )
  940. Xchar *inpoint, **original_names;
  941. Xint start, end, no_frames;
  942. X{
  943. X  register int i;
  944. X  VFR_DEV *vfr;
  945. X  int point[4];
  946. X  char *buffer_names[20];
  947. X  char **frames_names;
  948. X  int autoincrement;
  949. X
  950. X  /*
  951. X   * Connect with VFR interface.
  952. X   */
  953. X  vfr = ( VFR_DEV *) open_video();
  954. X
  955. X  /*
  956. X   * Caugth errors from child and interrupt key.
  957. X   */
  958. X  signal( SIGINT, press_break );
  959. X
  960. X  /* Convert inpoint to numbers */
  961. X  convert_inpoint( inpoint, point );
  962. X
  963. X  if ( record_sequence ) {
  964. X    /*
  965. X     * We have a sequence of frames, so we'll build each
  966. X     * name ( and need memory ) ...
  967. X     */
  968. X    frames_names = buffer_names;
  969. X    malloc_names( no_frames, original_names, frames_names);
  970. X    creat_names( no_frames, original_names, frames_names, start );
  971. X    /* Time for each frame */
  972. X    autoincrement = AUTOINCREMENT;
  973. X  }else {
  974. X    /* Use original names */
  975. X    frames_names = original_names;
  976. X    /* Time for the frame */
  977. X    autoincrement = start;
  978. X  }
  979. X
  980. X  /*
  981. X   * Child can build the image.
  982. X   */
  983. X  cpid = release_child( vfr, no_frames, frames_names );
  984. X
  985. X  /*
  986. X   * Initialize the VLAN.
  987. X   */
  988. X  init_vlan( vfr, inpoint, autoincrement );
  989. X  /*
  990. X   * Now the child can put the image into VFR.
  991. X   */
  992. X  unlock_vfr( cpid );
  993. X
  994. X  for ( i = start; i <= end; i++ ) {
  995. X    /* Check if the video are ready, PAUSE */
  996. X    check_point( vfr );
  997. X    /* Wait for child ( the child put the image into the VFR ) */
  998. X    wait_for_children( cpid );
  999. X    /* PerForm */
  1000. X    (void)Vfr_Vlan_Cmd( vfr, "PF" );
  1001. X    write_current_frame( point, i );
  1002. X    if ( i != end ) {
  1003. X      /* Not the end, so creat next names */
  1004. X      creat_names( no_frames, original_names, frames_names, i+1 );
  1005. X      cpid = release_child( vfr, no_frames, frames_names );
  1006. X    }
  1007. X    /* Check if the video perform the record */
  1008. X    check_status( vfr );
  1009. X    /*
  1010. X     * If the video record the last frame then child
  1011. X     * can put current image into VFR.
  1012. X     */
  1013. X    unlock_vfr( cpid );
  1014. X  }
  1015. X
  1016. X  if ( record_sequence ) {
  1017. X    free_names( no_frames, frames_names );
  1018. X  }
  1019. X}
  1020. X
  1021. Xrelease_child ( vfr, no_frames, frames_names )
  1022. XVFR_DEV *vfr;
  1023. Xint no_frames;
  1024. Xchar **frames_names;
  1025. X{
  1026. X  bitmap_hdr outbitmap;
  1027. X  ifunptr save_read_interface;
  1028. X  int pid;
  1029. X  int i;
  1030. X
  1031. X  child_ready = parent_ready = 0;
  1032. X
  1033. X  switch( pid = fork() ) {
  1034. X    case -1 :
  1035. X              /*
  1036. X               * fork() cannot respawn.
  1037. X               */
  1038. X              error( 13 );
  1039. X              break;
  1040. X    case  0 :
  1041. X              signal( SIGUSR1, fparent_ready );
  1042. X              signal( SIGUSR2, error_from_other );
  1043. X              /*
  1044. X               * Create the background image.
  1045. X               */
  1046. X              create_solid_image( &outbitmap, XFBUFFER, YFBUFFER, 0, 0, 0 );
  1047. X
  1048. X              /*
  1049. X               * Load all the images.
  1050. X               */
  1051. X              for ( i = 0; i < no_frames; i++ ) {
  1052. X                add_image( &outbitmap, frames_names[i], center );
  1053. X              }
  1054. X
  1055. X              if ( super ) {
  1056. X                /*
  1057. X                 * Glue the super to end image ( but we need use
  1058. X                 * Alias interface ).
  1059. X                 */
  1060. X                save_read_interface = read_image;
  1061. X                read_image = read_alias_file;
  1062. X                add_image( &outbitmap, SUPERNAME, 0 );
  1063. X                /* Restores old interface */
  1064. X                read_image = save_read_interface;
  1065. X              }
  1066. X
  1067. X              /*
  1068. X               * Wait for parent.
  1069. X               */
  1070. X              /*if ( !parent_ready )
  1071. X                pause();*/
  1072. X              while ( !parent_ready );
  1073. X
  1074. X              /*
  1075. X               * Outbitmap is the new image. We'll use last argument
  1076. X               * as file name.
  1077. X               */
  1078. X              write_vfr( vfr, &outbitmap );
  1079. X              freebitmap( &outbitmap );
  1080. X
  1081. X              /*
  1082. X               * This is the end of the child so report it to
  1083. X               * the parent.
  1084. X               */
  1085. X              exit( 0 );
  1086. X              break;
  1087. X    default:
  1088. X              signal( SIGUSR2, error_from_other );
  1089. X              return pid;
  1090. X              break;
  1091. X  }
  1092. X}
  1093. X
  1094. Xadd_image( base, filename, flag )
  1095. Xbitmap_hdr *base;
  1096. Xchar *filename;
  1097. Xint flag;
  1098. X{
  1099. X  bitmap_hdr inbitmap;
  1100. X  bitmap_hdr palbitmap;
  1101. X
  1102. X  /*
  1103. X   * Read the image.
  1104. X   */
  1105. X  read_image( filename, &inbitmap );
  1106. X
  1107. X  /*
  1108. X   * Adjust file to PAL and glue it to end image.
  1109. X   */
  1110. X  if ( !flag ) {
  1111. X    adjust( &inbitmap, &palbitmap, XSIZE, YSIZE, blurflag );
  1112. X    chroma( base, &palbitmap );
  1113. X  }else center_image( base, &inbitmap );
  1114. X
  1115. X  /*
  1116. X   * Free buffers.
  1117. X   */
  1118. X  freebitmap( &inbitmap );
  1119. X  if( flag )
  1120. X    freebitmap( &palbitmap );
  1121. X}
  1122. X
  1123. X
  1124. X/**************************************
  1125. X *
  1126. X * SMPTE code handlers.
  1127. X *
  1128. X */
  1129. Xcalculate_outpoint( inpoint, outpoint, totalframes )
  1130. Xchar *inpoint;
  1131. Xchar *outpoint, totalframes;
  1132. X{
  1133. X  register int i;
  1134. X  int point[4];
  1135. X
  1136. X  /* Convert inpoint to numbers */
  1137. X  convert_inpoint( inpoint, point );
  1138. X
  1139. X  /* Calculate the outpoint */
  1140. X  for ( i = 0; i < totalframes; i++ )
  1141. X    add_incode( point );
  1142. X
  1143. X  /* Convert outpoint numbers to a SMTPE format code */
  1144. X  sprintf ( outpoint, "%.2d:%.2d:%.2d:%.2d\n",
  1145. X            point[0], point[1], point[2], point[3] );
  1146. X}
  1147. X
  1148. Xis_a_incode( inpoint )
  1149. Xchar *inpoint;
  1150. X{
  1151. X  int marks = 0;
  1152. X
  1153. X  while ( *inpoint )
  1154. X    if ( *inpoint++ == ':' )
  1155. X      marks++;
  1156. X
  1157. X  return( marks == 3 );
  1158. X}
  1159. X
  1160. Xconvert_inpoint( inpoint, point )
  1161. Xchar *inpoint;
  1162. Xint *point;
  1163. X{
  1164. X  sscanf( inpoint, "%d:%d:%d:%d", &point[0], &point[1],
  1165. X                                  &point[2], &point[3] );
  1166. X
  1167. X  if (  point[0] <  0 || point[1] <  0 || point[2] <  0 || point[3] < 0 ||
  1168. X        point[0] > 23 || point[1] > 59 || point[2] > 59 || point[3] > FPS )
  1169. X    error( 14 );
  1170. X}
  1171. X
  1172. Xwrite_current_frame( point, frame )
  1173. Xint *point;
  1174. Xint frame;
  1175. X{
  1176. X#if AUTOINCREMENT > 1
  1177. X  register int i;
  1178. X#endif
  1179. X
  1180. X  if ( record_sequence ) {
  1181. X    fprintf ( stdout, "Recording frame %d on %.2d:%.2d:%.2d:%.2d\n", frame,
  1182. X              point[0], point[1], point[2], point[3] );
  1183. X  }else {
  1184. X    fprintf ( stdout, "Recording frame on %.2d:%.2d:%.2d:%.2d\n",
  1185. X              point[0], point[1], point[2], point[3] );
  1186. X  }
  1187. X
  1188. X  /*
  1189. X   * If AUTOINCREMENT is greater than one then we need add more
  1190. X   * frames to the current, else we can skip this loop.
  1191. X   */
  1192. X#if AUTOINCREMENT > 1
  1193. X  for ( i = 0; i < AUTOINCREMENT; i++ )
  1194. X#endif
  1195. X    add_incode( point );
  1196. X}
  1197. X
  1198. Xadd_incode( point )
  1199. Xint *point;
  1200. X{
  1201. X  if ( point[3] == FPS ) {
  1202. X    point[3] = 0;
  1203. X    if ( point[2] == 59 ) {
  1204. X      point[2] = 0;
  1205. X      if ( point[1] == 59 ) {
  1206. X        point[1] = 0;
  1207. X        point[0]++;
  1208. X      }else point[1]++;
  1209. X    }else point[2]++;
  1210. X  }else point[3]++;
  1211. X}
  1212. X
  1213. X
  1214. X/**************************************
  1215. X *
  1216. X * File names builders.
  1217. X *
  1218. X */
  1219. Xmalloc_names( no_frames, original_names, frames_names )
  1220. Xint no_frames;
  1221. Xchar **original_names, **frames_names;
  1222. X{
  1223. X  register int i;
  1224. X
  1225. X  /* Allocate memory to sequence names */
  1226. X  for ( i = 0; i < no_frames; i++ ) {
  1227. X    frames_names[i] = (char *) Malloc( strlen(original_names[i] ) + 10 );
  1228. X  }
  1229. X}
  1230. X
  1231. Xcreat_names( no_frames, original_names, frames_names, current_frame )
  1232. Xint no_frames, current_frame;
  1233. Xchar **original_names, **frames_names;
  1234. X{
  1235. X  register int i;
  1236. X
  1237. X  /* Creat the names of frames */
  1238. X  for ( i = 0; i < no_frames; i++ ) {
  1239. X    sprintf( frames_names[i], "%s.%d", original_names[i], current_frame );
  1240. X  }
  1241. X}
  1242. X
  1243. Xfree_names( no_frames, frames_names )
  1244. Xint no_frames;
  1245. Xchar **frames_names;
  1246. X{
  1247. X  register int i;
  1248. X
  1249. X  /* Free sequence names */
  1250. X  for ( i = 0; i < no_frames; i++ ) {
  1251. X    Free( frames_names[i] );
  1252. X  }
  1253. X}
  1254. X
  1255. X
  1256. X/**************************************
  1257. X *
  1258. X * Signal handlers.
  1259. X *
  1260. X */
  1261. Xvfr_not_ready()
  1262. X{
  1263. X  /*
  1264. X   * The alarm has been actived, the VFR doesn't respond.
  1265. X   */
  1266. X  error( 15 );
  1267. X}
  1268. X
  1269. Xerror_from_other()
  1270. X{
  1271. X  /*
  1272. X   * The other process had an error.
  1273. X   */
  1274. X  exit( 1 );
  1275. X}
  1276. X
  1277. Xunlock_vfr( cpid )
  1278. Xint cpid;
  1279. X{
  1280. X  /*
  1281. X   * Child can put image into VFR.
  1282. X   */
  1283. X  kill( cpid, SIGUSR1 );
  1284. X}
  1285. X
  1286. Xwait_for_children( cpid )
  1287. Xint cpid;
  1288. X{
  1289. X  int rpid;
  1290. X  int status;
  1291. X
  1292. X  /*
  1293. X   * The parent are ready, but the child ?.
  1294. X   */
  1295. X
  1296. X  rpid = wait( &status );
  1297. X  if ( ! WIFEXITED( status ) )
  1298. X    error( 16 );
  1299. X  signal( SIGUSR2, SIG_DFL );
  1300. X}
  1301. X
  1302. Xpress_break()
  1303. X{
  1304. X  /*
  1305. X   * User press break, so we need kill
  1306. X   * child ( if exist ).
  1307. X   */
  1308. X  if ( getpid() == parent_pid ) {
  1309. X    kill( cpid, SIGKILL );
  1310. X  }
  1311. X
  1312. X  rm_compress();
  1313. X  exit( 0 );
  1314. X}
  1315. X
  1316. Xfparent_ready()
  1317. X{
  1318. X  parent_ready = 1;
  1319. X  signal( SIGUSR1, fparent_ready );
  1320. X}
  1321. X
  1322. Xfchild_ready()
  1323. X{
  1324. X  child_ready = 1;
  1325. X  signal( SIGUSR1, fchild_ready );
  1326. X}
  1327. X
  1328. X
  1329. X/**************************************
  1330. X *
  1331. X * Digital Image Editing procedures.
  1332. X *
  1333. X */
  1334. Xadjust(inbitmap, outbitmap, newx, newy, noblur)
  1335. Xbitmap_hdr *inbitmap;
  1336. Xbitmap_hdr *outbitmap;
  1337. Xint newx, newy;
  1338. Xint noblur;
  1339. X{
  1340. X
  1341. X  /* Really, is an image ? */
  1342. X  if ( inbitmap->magic != LUGUSED )
  1343. X    error( 21 );
  1344. X
  1345. X  /* An 8 bits image ? */
  1346. X  if ( inbitmap->depth <= 8)
  1347. X    error(7);
  1348. X
  1349. X  /* If PAL then return the same image */
  1350. X  if ( inbitmap->xsize == newx && inbitmap->ysize == newy ) {
  1351. X    copy_bitmap( inbitmap, outbitmap );
  1352. X  }else {
  1353. X    /*
  1354. X     * Fill new header ...
  1355. X     */
  1356. X    outbitmap->magic = LUGUSED;
  1357. X    outbitmap->xsize = newx;
  1358. X    outbitmap->ysize = newy;
  1359. X    outbitmap->depth = inbitmap->depth;         /* 24 planes */
  1360. X    outbitmap->colors = inbitmap->colors;
  1361. X
  1362. X    /*
  1363. X     * ... and zoom each component.
  1364. X     */
  1365. X    VPRINTF(stderr, "Zooming R\n");
  1366. X    outbitmap->r= zoom( inbitmap->xsize, inbitmap->ysize,
  1367. X                        newx, newy, inbitmap->r, noblur );
  1368. X    VPRINTF(stderr, "Zooming G\n");
  1369. X    outbitmap->g= zoom( inbitmap->xsize, inbitmap->ysize,
  1370. X                        newx, newy, inbitmap->g, noblur );
  1371. X    VPRINTF(stderr, "Zooming B\n");
  1372. X    outbitmap->b= zoom( inbitmap->xsize, inbitmap->ysize,
  1373. X                        newx, newy, inbitmap->b, noblur );
  1374. X  }
  1375. X}
  1376. X
  1377. Xbyte *zoom(oldx, oldy, newx, newy, buffer, noblur)
  1378. Xint oldx, oldy;
  1379. Xint newx, newy;
  1380. Xregister byte *buffer;
  1381. Xint noblur;
  1382. X{
  1383. X  register int i, j;
  1384. X  register byte *base, *i_base;
  1385. X  double relx, rely;
  1386. X  double nrelx, nrely;
  1387. X  byte *ptr;
  1388. X  byte *nbuffer;
  1389. X  double foldx, foldy;
  1390. X  double fnewx, fnewy;
  1391. X
  1392. X  /* Allocate memory for new zoomed image */
  1393. X  nbuffer = (byte *) Malloc( newx * newy );
  1394. X
  1395. X  /*
  1396. X   * We use double variables ( only once transformation ).
  1397. X   */
  1398. X  fnewx = (double) newx, fnewy= (double) newy;
  1399. X  foldx = (double) oldx, foldy= (double) oldy;
  1400. X
  1401. X  for ( i = 0; i< newy; i++ ) {
  1402. X    /* pointer to first pixel in current row */
  1403. X    base = nbuffer + (int) (newx*i);
  1404. X    /* scale row from 0 to 1 with the new image */
  1405. X    nrely = ((double) i) / fnewy;
  1406. X    /* get equal value in old image */
  1407. X    rely = nrely * foldy;
  1408. X    /* pointer to first pixel in that row */
  1409. X    i_base = buffer + ((int) rely) * oldx;
  1410. X    for ( j = 0; j < newx; j++ ) {
  1411. X      /* scale column from 0 to 1 ... */
  1412. X      nrelx = ((double) j) / fnewx;
  1413. X      /* then we get position of old image */
  1414. X      relx = nrelx * foldx;
  1415. X      ptr = i_base + (int) relx;
  1416. X      *base++ = *ptr;
  1417. X    }
  1418. X  }
  1419. X
  1420. X  /*
  1421. X   * If image is greater then blur ( if no_blur switch
  1422. X   * is actived ).
  1423. X   */
  1424. X  if ( (newx > oldx || newy > oldy) && !noblur ) {
  1425. X    /* Blur zoomed image */
  1426. X    ptr = blur( nbuffer, newx, newy );
  1427. X    /* We have a new [blur] image, free first */
  1428. X    Free( nbuffer );
  1429. X    return ptr;
  1430. X  }else {
  1431. X    return nbuffer;
  1432. X  }
  1433. X}
  1434. X
  1435. Xbyte *blur(buffer, xsize, ysize)
  1436. Xbyte *buffer;
  1437. Xint xsize, ysize;
  1438. X{
  1439. X  register int i;
  1440. X  int totalsize = xsize * ysize;
  1441. X  byte *nbuffer;
  1442. X  register byte *fin;
  1443. X  register byte *base, *line_before;    /* pointers to new [blured] image */
  1444. X  register byte *last, *next_line;      /* pointers to old [zoomed] image */
  1445. X
  1446. X  /* Allocate memory for new blured image */
  1447. X  nbuffer= (byte *) Malloc( totalsize );
  1448. X
  1449. X  /*
  1450. X   * First and last rows, and first and last pixels in
  1451. X   * each row are not modified, so we copy image into new
  1452. X   * buffer and keep all information.
  1453. X   */
  1454. X  bcopy( buffer, nbuffer, totalsize );
  1455. X
  1456. X  /* Skip first and last row */
  1457. X  ysize--;
  1458. X  for ( i = 1; i < ysize; i++ ) {
  1459. X    /* Skip first pixel of current line */
  1460. X    base = nbuffer + i*xsize + 1;
  1461. X    /* Point to current line */
  1462. X    last = buffer + i*xsize + 1;
  1463. X    /* Point to line before */
  1464. X    line_before = last - xsize;
  1465. X    /* Point to next line */
  1466. X    next_line = last + xsize;
  1467. X    /*
  1468. X     * Pointer to last pixel for being modified of the current
  1469. X     * line ( skip last pixel ).
  1470. X     */
  1471. X    fin = base + xsize - 1;
  1472. X    while ( base < fin ) {
  1473. X      /* Blur the current pixel */
  1474. X      *base++ = .4 * *last +
  1475. X                .1 * *(last-1) + .1 * *(last+1) +
  1476. X                .1 * *line_before + .1 * *next_line +
  1477. X                .05 * *(line_before-1) + .05 * *(line_before+1) +
  1478. X                .05 * *(next_line-1) + .05 * *(next_line+1);
  1479. X      /* Update pointers */
  1480. X      next_line++;
  1481. X      line_before++;
  1482. X      last++;
  1483. X    }
  1484. X  }
  1485. X
  1486. X  return nbuffer;
  1487. X}
  1488. X
  1489. Xflip_image(inbitmap, outbitmap)
  1490. Xbitmap_hdr *inbitmap;
  1491. Xbitmap_hdr *outbitmap;
  1492. X{
  1493. X
  1494. X  /* Really, is an image ? */
  1495. X  if ( inbitmap->magic != LUGUSED )
  1496. X    error( 21 );
  1497. X
  1498. X  VPRINTF(stderr, "Flipping image\n");
  1499. X
  1500. X  /* Fill our header */
  1501. X  outbitmap->magic = LUGUSED;
  1502. X  outbitmap->xsize = inbitmap->xsize;
  1503. X  outbitmap->ysize = inbitmap->ysize;
  1504. X  outbitmap->depth = inbitmap->depth;
  1505. X  outbitmap->colors = inbitmap->colors;
  1506. X
  1507. X  outbitmap->r = flip( inbitmap->r, outbitmap->xsize, outbitmap->ysize );
  1508. X  if ( outbitmap->depth > 8 ) {
  1509. X    /* 24 planes */
  1510. X    outbitmap->g = flip( inbitmap->g, outbitmap->xsize, outbitmap->ysize );
  1511. X    outbitmap->b = flip( inbitmap->b, outbitmap->xsize, outbitmap->ysize );
  1512. X  }else outbitmap->cmap = inbitmap->cmap;
  1513. X}
  1514. X
  1515. Xbyte *flip(buffer, xsize, ysize)
  1516. Xbyte *buffer;
  1517. Xint xsize, ysize;
  1518. X{
  1519. X  register int i, j;
  1520. X  int twolines;
  1521. X  byte *image, *ptr;
  1522. X
  1523. X  ptr = image = (byte *) Malloc( xsize * ysize );
  1524. X  twolines = 2 * xsize;
  1525. X  buffer += xsize * ysize - xsize;
  1526. X
  1527. X  for ( i = 0; i < ysize; i++) {
  1528. X    for ( j = 0; j < xsize; j++ ) {
  1529. X      *ptr++ = *buffer++;
  1530. X    }
  1531. X    buffer -= twolines;
  1532. X  }
  1533. X
  1534. X  return image;
  1535. X}
  1536. X
  1537. Xto24( inbitmap, outbitmap )
  1538. Xbitmap_hdr *inbitmap;
  1539. Xbitmap_hdr *outbitmap;
  1540. X{
  1541. X  int total_size;
  1542. X  color_map *map;
  1543. X  byte *r, *g, *b;
  1544. X  byte *base;
  1545. X  byte *end;
  1546. X
  1547. X  /* Really, is an image ? */
  1548. X  if ( inbitmap->magic != LUGUSED )
  1549. X    error( 21 );
  1550. X
  1551. X  /* Fill new header */
  1552. X  outbitmap->magic = LUGUSED;
  1553. X  outbitmap->xsize = inbitmap->xsize;
  1554. X  outbitmap->ysize = inbitmap->ysize;
  1555. X  outbitmap->depth = 24;
  1556. X  outbitmap->colors = ( 1 << outbitmap->depth );
  1557. X  total_size  = outbitmap->xsize * outbitmap->ysize;
  1558. X  r = outbitmap->r = (byte *) Malloc(total_size);
  1559. X  g = outbitmap->g = (byte *) Malloc(total_size);
  1560. X  b = outbitmap->b = (byte *) Malloc(total_size);
  1561. X
  1562. X  /* A pointer to cmap */
  1563. X  map  = (color_map *) inbitmap->cmap;
  1564. X  /* A pointer to original 8 bits buffer */
  1565. X  base = inbitmap->r;
  1566. X  /* A pointer to the end */
  1567. X  end = base + total_size;
  1568. X
  1569. X  VPRINTF(stderr, "Reconverting to 24 planes\n");
  1570. X  while ( base < end ) {
  1571. X    *r++ = map[*base][0];
  1572. X    *g++ = map[*base][1];
  1573. X    *b++ = map[*base][2];
  1574. X    base++;
  1575. X  }
  1576. X}
  1577. X
  1578. Xcreate_solid_image(image, xsize, ysize, r, g, b)
  1579. Xbitmap_hdr *image;
  1580. Xint xsize, ysize;
  1581. Xbyte r, g, b;
  1582. X{
  1583. X  int totalsize = xsize * ysize;
  1584. X
  1585. X  image->magic = LUGUSED;
  1586. X  image->xsize = xsize;
  1587. X  image->ysize = ysize;
  1588. X  image->depth = 24;
  1589. X  image->colors = ( 1 << image->depth );
  1590. X  image->r= (byte *) Malloc( totalsize );
  1591. X  image->g= (byte *) Malloc( totalsize );
  1592. X  image->b= (byte *) Malloc( totalsize );
  1593. X  if ( r )
  1594. X    memset( image->r, r, totalsize );
  1595. X  if ( g )
  1596. X    memset( image->g, g, totalsize );
  1597. X  if ( b )
  1598. X    memset( image->b, b, totalsize );
  1599. X}
  1600. X
  1601. Xcenter_image( base, super )
  1602. Xbitmap_hdr *base, *super;
  1603. X{
  1604. X  int xmid, ymid;
  1605. X
  1606. X  xmid = ( XSIZE / 2 ) - ( super->xsize / 2 );
  1607. X  ymid = ( YSIZE / 2 ) - ( super->ysize / 2 );
  1608. X
  1609. X  paste( base, super, xmid, ymid );
  1610. X}
  1611. X
  1612. Xpaste(base, super, xpos, ypos)
  1613. Xbitmap_hdr *base, *super;
  1614. Xint xpos, ypos;
  1615. X{
  1616. X  register int i, j;
  1617. X  byte *r1, *g1, *b1;
  1618. X  byte *r2, *g2, *b2;
  1619. X
  1620. X   /* Really, are images ? */
  1621. X  if ( base->magic != LUGUSED || super->magic != LUGUSED )
  1622. X    error( 21 );
  1623. X
  1624. X  r1 = base->r,  g1 = base->g,  b1 = base->b;
  1625. X  r2 = super->r, g2 = super->g, b2 = super->b;
  1626. X
  1627. X  /*
  1628. X   * Skip pre-paste rows.
  1629. X   */
  1630. X  for (i= 0; i< ypos; i++) {
  1631. X    r1 += base->xsize;
  1632. X    g1 += base->xsize;
  1633. X    b1 += base->xsize;
  1634. X  }
  1635. X
  1636. X  /*
  1637. X   * While we are inside surface to paste ...
  1638. X   */
  1639. X  for ( i = 0; i < super->ysize; i++ ) {
  1640. X    for ( j = 0; j< base->xsize; j++ )
  1641. X      if ( j < xpos  || j >= super->xsize+xpos ) {
  1642. X        r1++, g1++, b1++;
  1643. X      }else {
  1644. X        if ( (*r2 + *g2 + *b2) < CHROMABORDER ) {
  1645. X          r1++, g1++, b1++;
  1646. X          r2++, g2++, b2++;
  1647. X        }else {
  1648. X          *r1++ = *r2++;
  1649. X          *g1++ = *g2++;
  1650. X          *b1++ = *b2++;
  1651. X        }
  1652. X      }
  1653. X  }
  1654. X}
  1655. X
  1656. Xchroma( base, super )
  1657. Xbitmap_hdr *base;
  1658. Xbitmap_hdr *super;
  1659. X{
  1660. X  register int i, j;
  1661. X  byte *r1, *g1, *b1;
  1662. X  byte *r2, *g2, *b2;
  1663. X
  1664. X  /* Really, are images ? */
  1665. X  if ( base->magic != LUGUSED || super->magic != LUGUSED )
  1666. X    error( 21 );
  1667. X
  1668. X  /* Set pointers */
  1669. X  r1= base->r , g1= base->g , b1= base->b;
  1670. X  r2= super->r, g2= super->g, b2= super->b;
  1671. X
  1672. X  for ( i = 0; i < super->ysize; i++ ) {
  1673. X    for ( j = 0; j < base->xsize; j++ ) {
  1674. X      if ( j < super->xsize ) {
  1675. X        /* We are into super image. */
  1676. X        if ( (*r2 + *g2 + *b2) < CHROMABORDER ) {
  1677. X          r1++, g1++, b1++;
  1678. X          r2++, g2++, b2++;
  1679. X        }else {
  1680. X          *r1++ = *r2++;
  1681. X          *g1++ = *g2++;
  1682. X          *b1++ = *b2++;
  1683. X        }
  1684. X      }else {
  1685. X        /* Out of super borders */
  1686. X        r1++, g1++, b1++;
  1687. X      }
  1688. X    }
  1689. X  }
  1690. X}
  1691. X
  1692. Xbyte *create_bw_pallete()
  1693. X{
  1694. X  register int i;
  1695. X  byte *buffer = (byte *) Malloc( 3 * 256 );
  1696. X  byte *ptr;
  1697. X
  1698. X  /* I'll use a pointer */
  1699. X  ptr = buffer;
  1700. X  for (i = 0; i < 256; i++) {
  1701. X    *ptr++= (byte) i;   /* R */
  1702. X    *ptr++= (byte) i;   /* G */
  1703. X    *ptr++= (byte) i;   /* B */
  1704. X  }
  1705. X
  1706. X  return buffer;
  1707. X}
  1708. X
  1709. X
  1710. X/**************************************
  1711. X *
  1712. X * ALIAS interface
  1713. X *
  1714. X */
  1715. Xread_alias_file( name, bitmap )
  1716. Xchar *name;
  1717. Xbitmap_hdr *bitmap;
  1718. X{
  1719. X  FILE *handle;
  1720. X
  1721. X  /* Open the file descriptor */
  1722. X  if ( name != NULL )
  1723. X    handle = Fopen( name, "r" );
  1724. X  else handle = stdin;
  1725. X
  1726. X  /* Read the bitmap */
  1727. X  read_alias( handle, bitmap );
  1728. X
  1729. X  /* Close the file */
  1730. X  Fclose( handle );
  1731. X}
  1732. X
  1733. Xread_alias(handle, image)
  1734. XFILE *handle;
  1735. Xbitmap_hdr *image;
  1736. X{
  1737. X  register int i;
  1738. X  alias_hdr header;
  1739. X  int total_size;
  1740. X  register int xsize;
  1741. X  byte *end;
  1742. X  byte *r, *g, *b;
  1743. X  int can_read_all;
  1744. X  byte *buffer, *ptr;
  1745. X  int filesize;
  1746. X  int allplanes = 0;
  1747. X  int internalerror;
  1748. X  int cmapsize;
  1749. X
  1750. X  /*
  1751. X   * Alias "pix" is a bad format to read line per line. If every
  1752. X   * triplet is different to its next, then we read 4 bytes, 4 bytes ...
  1753. X   * ... and read a file of 1 Mb. with this form ... hmmmmmm !!!
  1754. X   * This is necesary if we are reading from stdin, but if we have
  1755. X   * a file, we'll read the file into memory ( one access to disk ).
  1756. X   */
  1757. X#ifndef DEC
  1758. X  can_read_all = (handle != stdin);     /* a regular file ? */
  1759. X  VPRINTF(stderr, "Reading Alias from %s\n",
  1760. X                  (can_read_all ? "file" : "stdin"));
  1761. X#else
  1762. X  can_read_all = 0;
  1763. X#endif
  1764. X
  1765. X  if (can_read_all) {
  1766. X    /*
  1767. X     * We are using a regular file ( not a pipe ), so we can
  1768. X     * read it into memory.
  1769. X     */
  1770. X    ptr= buffer= (byte *) read_file(handle, &filesize);
  1771. X    VPRINTF(stderr, "File size: %d\n", filesize);
  1772. X    /* Copy the header */
  1773. X    bcopy(ptr, &header, sizeof(alias_hdr));
  1774. X    ptr += sizeof(alias_hdr);           /* skip the header */
  1775. X  }else {
  1776. X    /*
  1777. X     * We are using stdin so ...
  1778. X     */
  1779. X    /* We need the header of Alias pix */
  1780. X    VPRINTF(stderr, "Reading Alias header\n");
  1781. X    read_alias_header(handle, &header);
  1782. X  }
  1783. X
  1784. X  /* Size in pixels */
  1785. X  total_size = header.xsize * header.ysize;
  1786. X
  1787. X  /* Fill our header */
  1788. X  image->magic  = LUGUSED;
  1789. X  image->xsize  = header.xsize;
  1790. X  image->ysize  = header.ysize;
  1791. X  image->depth  = header.depth;
  1792. X  image->colors = 1 << image->depth;
  1793. X  allplanes = ( image->depth > 8 );     /* an image with 24 planes ? */
  1794. X  VPRINTF(stderr, "Image size: %dx%d\n", image->xsize, image->ysize);
  1795. X  VPRINTF(stderr, "Depth: %d\n", image->depth);
  1796. X
  1797. X  /* Get some memory */
  1798. X  if ( allplanes ) {
  1799. X    /*
  1800. X     * We have a image with 24 planes, so we need three buffers to
  1801. X     * store it.
  1802. X     */
  1803. X    r= image->r = (byte *) Malloc(total_size);
  1804. X    g= image->g = (byte *) Malloc(total_size);
  1805. X    b= image->b = (byte *) Malloc(total_size);
  1806. X  }else {
  1807. X    /*
  1808. X     * The image has less than 256 colors, so we use one plane,
  1809. X     * for the bitmap, and the cmap.
  1810. X     */
  1811. X    r= image->r = (byte *) Malloc(total_size);
  1812. X    cmapsize= image->colors * 3;        /* size in bytes */
  1813. X    image->cmap = (byte *) Malloc(cmapsize);
  1814. X    /* Creating the cmap from file */
  1815. X    VPRINTF(stderr, "Creating cmap\n");
  1816. X    create_alias_cmap(image);
  1817. X  }
  1818. X
  1819. X  /*
  1820. X   * We are ready to uncompress Alias file.
  1821. X   */
  1822. X  VPRINTF(stderr, "Uncompressing Alias file\n");
  1823. X  if (can_read_all) {
  1824. X    /*
  1825. X     * We have the file into memory so we uncode it directly.
  1826. X     */
  1827. X    end = r + total_size;       /* the end of main buffer */
  1828. X    if ( allplanes )
  1829. X      uncode_alias24(ptr, r, g, b, end);
  1830. X    else uncode_alias(ptr, r, end);
  1831. X    /*
  1832. X     * We have read the file and uncompressed, so we can
  1833. X     * free the memory ( and exit ).
  1834. X     */
  1835. X    Free(buffer);
  1836. X  }else {
  1837. X    /*
  1838. X     * We'll read each line from Alias file ( stdin ) until we
  1839. X     * fill our buffers or we get an error.
  1840. X     */
  1841. X    xsize = image->xsize;
  1842. X    for (i= 0; i< image->ysize; i++) {
  1843. X      /*
  1844. X       * Read and decode the Alias raster information, two cases:
  1845. X       * with planes > 8 => a triplet (RGB), else a index.
  1846. X       */
  1847. X      if ( allplanes ) {
  1848. X        /* An image with 24 planes */
  1849. X        internalerror= read_line_alias24(handle, r, b, g, xsize);
  1850. X      }else {
  1851. X        /* An image with indexes to cmap */
  1852. X        internalerror= read_line_alias(handle, r, xsize);
  1853. X      }
  1854. X      if (internalerror) {
  1855. X        /* An error, so we exit */
  1856. X        fprintf(stderr, "Error while reading line %d\n", i);
  1857. X        return 1;
  1858. X      }else {
  1859. X        /* A line has been read, so we increment theirs pointers */
  1860. X        r += xsize;
  1861. X        if ( allplanes ) {
  1862. X          g += xsize;
  1863. X          b += xsize;
  1864. X        }
  1865. X      }
  1866. X    }
  1867. X  } /* end of reading from stdin */
  1868. X
  1869. X  /* Finish with no problems */
  1870. X  return 0;
  1871. X}
  1872. X
  1873. Xread_alias_header(handle, header)
  1874. XFILE *handle;
  1875. Xalias_hdr *header;
  1876. X{
  1877. X  byte buffer[10];
  1878. X
  1879. X  /* Read the header */
  1880. X  Fread(buffer, 10, 1, handle);
  1881. X
  1882. X  /* Fill the header structure */
  1883. X  header->xsize = ( buffer[0] << 8 ) | buffer[1];
  1884. X  header->ysize = ( buffer[2] << 8 ) | buffer[3];
  1885. X  header->xinit = ( buffer[4] << 8 ) | buffer[5];
  1886. X  header->yinit = ( buffer[6] << 8 ) | buffer[7];
  1887. X  header->depth = ( buffer[8] << 8 ) | buffer[9];
  1888. X
  1889. X  /* Check some things */
  1890. X  if ( header->xsize < 1 || header->xsize > 2560 ||
  1891. X       header->ysize < 1 || header->ysize > 2560 )
  1892. X    error(5);
  1893. X  if ( header->depth > 24 || header->depth < 1 )
  1894. X    error(5);
  1895. X}
  1896. X
  1897. Xread_line_alias24(handle, r, g, b, size)
  1898. XFILE *handle;
  1899. Xbyte *r, *g, *b;
  1900. Xregister int size;
  1901. X{
  1902. X  register int i;
  1903. X  register int count = 0;
  1904. X  byte *end;
  1905. X  byte buffer[4];
  1906. X
  1907. X  end = r + size;
  1908. X  while (count < size) {
  1909. X    /*
  1910. X     * Alias code format:  <repeat>BGR => 4 bytes.
  1911. X     */
  1912. X    if ( !fread(buffer, 4, 1, handle) )         /* read next buffer */
  1913. X      return 1;   /* hey !, an EOF here ?. Hmmmm, this is an error ! */
  1914. X
  1915. X    count += buffer[0];                 /* # of repetitions */
  1916. X    /* repeat 'buffer[0]' times these triplet */
  1917. X    for (i= 0; i< buffer[0]; i++) {
  1918. X      *r++ = buffer[3];
  1919. X      *g++ = buffer[1];
  1920. X      *b++ = buffer[2];
  1921. X    }
  1922. X    if ( r > end )
  1923. X      error(6);         /* uncompress more bytes than size */
  1924. X  }
  1925. X
  1926. X  /* No problems */
  1927. X  return 0;
  1928. X}
  1929. X
  1930. Xread_line_alias(handle, r, size)
  1931. XFILE *handle;
  1932. Xbyte *r;
  1933. Xregister int size;
  1934. X{
  1935. X  register int i;
  1936. X  register int count = 0;
  1937. X  byte *end;
  1938. X  byte buffer[2];
  1939. X
  1940. X  end = r + size;
  1941. X  while (count < size) {
  1942. X    /*
  1943. X     * Alias code format:  <repeat><index> => 2 bytes.
  1944. X     */
  1945. X    if ( !fread(buffer, 2, 1, handle) )         /* read next buffer */
  1946. X      return 1;   /* hey !, an EOF here ?. Hmmmm, this is an error ! */
  1947. X
  1948. X    count += buffer[0];                 /* # of repetitions */
  1949. X    /* repeat 'buffer[0]' times these index */
  1950. X    for (i= 0; i< buffer[0]; i++) {
  1951. X      *r++ = buffer[1];
  1952. X    }
  1953. X    if ( r > end )
  1954. X      error(6);         /* uncompress more bytes than size */
  1955. X  }
  1956. X
  1957. X  /* No problems */
  1958. X  return 0;
  1959. X}
  1960. X
  1961. Xcreate_alias_cmap(image)
  1962. Xbitmap_hdr *image;
  1963. X{
  1964. X  register int i;
  1965. X  byte *ptr;
  1966. X
  1967. X  /*
  1968. X   * Alias 8 bits files are b&w, so ...
  1969. X   */
  1970. X  ptr = (byte *) image->cmap;
  1971. X  for (i= 0; i< image->colors; i++) {
  1972. X    *ptr++ = i;
  1973. X    *ptr++ = i;
  1974. X    *ptr++ = i;
  1975. X  }
  1976. X}
  1977. X
  1978. Xuncode_alias24(ptr, rbuf, gbuf, bbuf, end)
  1979. Xbyte *ptr;
  1980. Xbyte *rbuf, *gbuf, *bbuf;
  1981. Xbyte *end;
  1982. X{
  1983. X  register int i;
  1984. X  byte r, g, b;
  1985. X
  1986. X  while ( rbuf < end ) {        /* while not end of buffer */
  1987. X    if ( (i= *ptr++) > 1 ) {            /* how mary repetitions ? */
  1988. X      b= *ptr++;
  1989. X      g= *ptr++;
  1990. X      r= *ptr++;
  1991. X      while( i-- ) {    /* copy these triplet 'i' times */
  1992. X        *rbuf++ = r;
  1993. X        *gbuf++ = g;
  1994. X        *bbuf++ = b;
  1995. X      }
  1996. X    }else {                     /* else, copy these triplet */
  1997. X      *bbuf++ = *ptr++;
  1998. X      *gbuf++ = *ptr++;
  1999. X      *rbuf++ = *ptr++;
  2000. X    }
  2001. X  }
  2002. X}
  2003. X
  2004. Xuncode_alias(ptr, rbuf, end)
  2005. Xbyte *ptr;
  2006. Xbyte *rbuf;
  2007. Xbyte *end;
  2008. X{
  2009. X  register int i;
  2010. X  byte r;
  2011. X
  2012. X  while ( rbuf < end ) {        /* while not end of buffer */
  2013. X    if ( (i= *ptr++) > 1 ) {            /* how mary repetitions ? */
  2014. X      r= *ptr++;
  2015. X      while( i-- ) {    /* copy these index 'i' times */
  2016. X        *rbuf++ = r;
  2017. X      }
  2018. X    }else {                     /* else, copy these index */
  2019. X      *rbuf++ = *ptr++;
  2020. X    }
  2021. X  }
  2022. X}
  2023. X
  2024. X
  2025. X/**************************************
  2026. X *
  2027. X * Wavefront's RLA interface.
  2028. X *
  2029. X */
  2030. Xread_rla_file( name, bitmap )
  2031. Xchar *name;
  2032. Xbitmap_hdr *bitmap;
  2033. X{
  2034. X  FILE *handle;
  2035. X
  2036. X  /* Open the file descriptor */
  2037. X  if ( name != NULL )
  2038. X    handle = Fopen( name, "r" );
  2039. X  else error( 20 );
  2040. X
  2041. X  /* Read the bitmap */
  2042. X  read_rla( handle, bitmap );
  2043. X
  2044. X  /* Close the file */
  2045. X  Fclose( handle );
  2046. X}
  2047. X
  2048. Xread_rla(handle, image)
  2049. XFILE *handle;
  2050. Xbitmap_hdr *image;
  2051. X{
  2052. X  register int i, j;
  2053. X  RLA_HEADER rla;
  2054. X  int totalsize;
  2055. X  byte *r, *g, *b;
  2056. X  int *offsets;
  2057. X  short no_bytes;
  2058. X  byte *ptr;
  2059. X  byte *raux, *gaux, *baux;
  2060. X  byte *rptr, *gptr, *bptr;
  2061. X
  2062. X  /*
  2063. X   * Read the RLA's header.
  2064. X   */
  2065. X  Fread(&rla, 740, 1, handle);
  2066. X  image->magic = LUGUSED;
  2067. X  image->xsize = rla.window.right - rla.window.left + 1;
  2068. X  image->ysize = rla.window.top - rla.window.bottom + 1;
  2069. X  image->depth = 24;
  2070. X  image->colors = ( 1 << image->depth );
  2071. X  totalsize= image->xsize * image->ysize;
  2072. X
  2073. X  if ( image->xsize < 1 || image->xsize > 2559 ||
  2074. X       image->ysize < 1 || image->ysize > 2559 )
  2075. X    error(5);
  2076. X
  2077. X  /*
  2078. X   * Allocate for bitmap components.
  2079. X   */
  2080. X  r = image->r = (byte *) Malloc(totalsize);
  2081. X  g = image->g = (byte *) Malloc(totalsize);
  2082. X  b = image->b = (byte *) Malloc(totalsize);
  2083. X
  2084. X  /* Some space to unpack scanlines */
  2085. X  raux = (byte *) Malloc(image->xsize);
  2086. X  gaux = (byte *) Malloc(image->xsize);
  2087. X  baux = (byte *) Malloc(image->xsize);
  2088. X  ptr  = (byte *) Malloc(2560);
  2089. X
  2090. X  /* Allocate space for offsets */
  2091. X  offsets = (int *) Malloc(image->ysize*sizeof(int));
  2092. X  Fread(offsets, sizeof(int), image->ysize, handle);
  2093. X
  2094. X  for (i = rla.window.top; i >= rla.window.bottom; i--) {
  2095. X    if ( i > rla.act_window.top || i < rla.act_window.bottom ) {
  2096. X      /* Outlines => zeros ( done with Malloc ) */
  2097. X      r += image->xsize;
  2098. X      g += image->xsize;
  2099. X      b += image->xsize;
  2100. X    }else {
  2101. X      /*
  2102. X       * Set pointer where offsets table says.
  2103. X       */
  2104. X      if (fseek(handle, (long) offsets[i - rla.act_window.bottom], 0))
  2105. X        error( 20 );
  2106. X      /*
  2107. X       * Uncode R channel.
  2108. X       */
  2109. X      Fread(&no_bytes, sizeof(short), 1, handle);
  2110. X      Fread(ptr, (int) no_bytes, 1, handle);
  2111. X      decodeRLA(ptr, raux, no_bytes);
  2112. X      /*
  2113. X       * Uncode G channel.
  2114. X       */
  2115. X      Fread(&no_bytes, sizeof(short), 1, handle);
  2116. X      Fread(ptr, (int) no_bytes, 1, handle);
  2117. X      decodeRLA(ptr, gaux, no_bytes);
  2118. X      /*
  2119. X       * Uncode B channel.
  2120. X       */
  2121. X      Fread(&no_bytes, sizeof(short), 1, handle);
  2122. X      Fread(ptr, (int) no_bytes, 1, handle);
  2123. X      decodeRLA(ptr, baux, no_bytes);
  2124. X
  2125. X      /*
  2126. X       * If you wanna you do the same task with matte ( but I
  2127. X       * don't mind that channel ).
  2128. X       */
  2129. X
  2130. X      /*
  2131. X       * We uncode the active window, now we fill the real window
  2132. X       * with these datas and aditional zeros.
  2133. X       */
  2134. X      rptr= raux;
  2135. X      gptr= gaux;
  2136. X      bptr= baux;
  2137. X      for (j = rla.window.left; j <= rla.window.right; j++) {
  2138. X        if ( j < rla.act_window.left || j > rla.act_window.right ) {
  2139. X          /* Outlines => zeros ( done with Malloc ) */
  2140. X          *r++, *g++, *b++;
  2141. X         }else {
  2142. X           *r++ = *rptr++;
  2143. X           *g++ = *gptr++;
  2144. X           *b++ = *bptr++;
  2145. X         }
  2146. X      }
  2147. X    }
  2148. X  }
  2149. X
  2150. X  /*
  2151. X   * Free memory.
  2152. X   */
  2153. X  Free(raux);
  2154. X  Free(gaux);
  2155. X  Free(baux);
  2156. X  Free(offsets);
  2157. X  Free(ptr);
  2158. X
  2159. X  /* no errors */
  2160. X  return 0;
  2161. X}
  2162. X
  2163. XdecodeRLA(buf_in, buf_out, len)
  2164. Xbyte    *buf_in;
  2165. Xbyte    *buf_out;
  2166. Xint     len;
  2167. X{
  2168. X   int  ct;
  2169. X   byte *end;
  2170. X
  2171. X   end = buf_out + len;
  2172. X   while(len > 0) {
  2173. X      ct = *buf_in++;
  2174. X      len--;
  2175. X      if (ct < 128) {
  2176. X        /* repeat pixel value ct+1 times */
  2177. X         while (ct-- >= 0)
  2178. X            *buf_out++ = *buf_in;
  2179. X         buf_in++;
  2180. X         len--;
  2181. X      }
  2182. X      else {
  2183. X        /* copy ct unencoded values */
  2184. X         for (ct = 256-ct; ct-- > 0; len--)
  2185. X            *buf_out++ = *buf_in++;
  2186. X      }
  2187. X      /* if ( buf_out > end )
  2188. X        error(6);*/
  2189. X   }
  2190. X}
  2191. X
  2192. X
  2193. X/**************************************
  2194. X *
  2195. X * SGI interface
  2196. X *
  2197. X */
  2198. Xread_sgi_file( name, bitmap )
  2199. Xchar *name;
  2200. Xbitmap_hdr *bitmap;
  2201. X{
  2202. X  register int i, j;
  2203. X  IMAGE *sgi_image;
  2204. X  byte *r, *g, *b;
  2205. X  short *sgibuf;
  2206. X  int totalsize;
  2207. X  int flag24;
  2208. X
  2209. X  VPRINTF(stderr, "Reading SGI image\n");
  2210. X
  2211. X  if ( (sgi_image = iopen( name, "r" )) == NULL )
  2212. X    error( 1 );
  2213. X  /* if ( sgi_image->zsize < 3 )
  2214. X      error( 7 ); */
  2215. X
  2216. X  /*
  2217. X   * Fill our header.
  2218. X   */
  2219. X  bitmap->magic = LUGUSED;
  2220. X  bitmap->xsize = sgi_image->xsize;
  2221. X  bitmap->ysize = sgi_image->ysize;
  2222. X  bitmap->depth = 8 * sgi_image->zsize;
  2223. X  bitmap->colors = ( 1 << bitmap->depth );
  2224. X  flag24 = (bitmap->depth > 8);
  2225. X
  2226. X  totalsize = bitmap->xsize * bitmap->ysize;
  2227. X  r = bitmap->r = (byte *) Malloc( totalsize );
  2228. X  if ( flag24 ) {
  2229. X    g = bitmap->g = (byte *) Malloc( totalsize );
  2230. X    b = bitmap->b = (byte *) Malloc( totalsize );
  2231. X  }else bitmap->cmap = (byte *) create_bw_pallete();
  2232. X
  2233. X  /*
  2234. X   * Allocate memory for sgi raster line.
  2235. X   */
  2236. X  sgibuf = (short *) Malloc( sizeof(short) * bitmap->xsize );
  2237. X
  2238. X  for ( i = 0; i < bitmap->ysize; i++ ) {
  2239. X    j = bitmap->ysize - 1 - i;
  2240. X    getrow( sgi_image, sgibuf, j, 0 );
  2241. X    (void) shorttobyte( sgibuf, r, bitmap->xsize );
  2242. X    r += bitmap->xsize;
  2243. X    if ( flag24 ) {
  2244. X      getrow( sgi_image, sgibuf, j, 1 );
  2245. X      (void) shorttobyte( sgibuf, g, bitmap->xsize );
  2246. X      g += bitmap->xsize;
  2247. X      getrow( sgi_image, sgibuf, j, 2 );
  2248. X      (void) shorttobyte( sgibuf, b, bitmap->xsize );
  2249. X      b += bitmap->xsize;
  2250. X    }
  2251. X  }
  2252. X
  2253. X  iclose( sgi_image );
  2254. X  Free( sgibuf );
  2255. X}
  2256. X
  2257. X
  2258. X/**************************************
  2259. X *
  2260. X * TARGA interface
  2261. X *
  2262. X */
  2263. Xread_tga_file( name, bitmap )
  2264. Xchar *name;
  2265. Xbitmap_hdr *bitmap;
  2266. X{
  2267. X  FILE *handle;
  2268. X  bitmap_hdr image;
  2269. X
  2270. X  /* Open the file descriptor */
  2271. X  if ( name != NULL )
  2272. X    handle = Fopen( name, "r" );
  2273. X  else handle = stdin;
  2274. X
  2275. X  /* Read the bitmap */
  2276. X  read_tga( handle, &image );
  2277. X
  2278. X  /* Close the file */
  2279. X  Fclose( handle );
  2280. X
  2281. X  /*
  2282. X   * Now we could have a 8 or 24 bits image, but 'tovcr'
  2283. X   * needs true color images, so check it...
  2284. X   */
  2285. X  if ( image.depth > 8 ){
  2286. X    copy_bitmap( &image, bitmap );
  2287. X  }else {
  2288. X    to24( &image, bitmap );
  2289. X  }
  2290. X
  2291. X  /* Free the tmp bitmap */
  2292. X  freebitmap( &image );
  2293. X}
  2294. X
  2295. Xread_tga(handle, image)
  2296. XFILE *handle;
  2297. Xbitmap_hdr *image;
  2298. X{
  2299. X  register int i;
  2300. X  tga_hdr tga;
  2301. X  int allplanes = 0;
  2302. X  int lessplanes = 0;
  2303. X  int forceallplanes = 0;
  2304. X  byte aux4[4];
  2305. X  int totalsize;
  2306. X  byte *ptr;
  2307. X  byte *r, *g, *b;
  2308. X  byte *aux;
  2309. X
  2310. X  /* Read the Tagar header */
  2311. X  read_tga_header( handle, &tga );
  2312. X
  2313. X  /* Fill our header */
  2314. X  image->magic = LUGUSED;
  2315. X  image->xsize = tga.xsize;
  2316. X  image->ysize = tga.ysize;
  2317. X  totalsize = image->xsize * image->ysize;
  2318. X  /*
  2319. X   * Targa format can be mapped but with moer than 256 colors,
  2320. X   * our mapped images only support 8 bits, so we need convert
  2321. X   * these images to 24 bits.
  2322. X   */
  2323. X  switch ( tga.image_type ) {
  2324. X    case TGA_RGB:
  2325. X    case TGA_RLE_RGB:
  2326. X                allplanes = 1;
  2327. X                break;
  2328. X    case TGA_MAPPED:
  2329. X    case TGA_RLE_MAPPED:
  2330. X                lessplanes = 1;
  2331. X                if ( tga.cmap_type != 1 )
  2332. X                  error( 10 );
  2333. X                if ( tga.cmap_length > 256 )
  2334. X                  forceallplanes = 1;
  2335. X                break;
  2336. X  }
  2337. X
  2338. X  /*
  2339. X   * Read the cmap info ( if exists ).
  2340. X   */
  2341. X  if ( tga.cmap_type ) {
  2342. X    VPRINTF(stderr, "Reading cmap info\n");
  2343. X    if ( allplanes ) {
  2344. X      /* We only need skip the cmap block */
  2345. X      for ( i = 0; i < tga.cmap_length; i++ )
  2346. X        read_tga_data( aux4, tga.cmap_entry_size, handle );
  2347. X    }else {
  2348. X      ptr = image->cmap = (byte *) Malloc( 3 * tga.cmap_length );
  2349. X      for ( i = 0; i < tga.cmap_length; i++) {
  2350. X        read_tga_data( ptr, tga.cmap_entry_size, handle );
  2351. X        ptr += 3;
  2352. X      }
  2353. X    }
  2354. X  }
  2355. X
  2356. X  /*
  2357. X   * Allocate memory for the bitmap and fill our header.
  2358. X   */
  2359. X  image->xsize = tga.xsize;
  2360. X  image->ysize = tga.ysize;
  2361. X  totalsize = image->xsize * image->ysize;
  2362. X  if ( allplanes || forceallplanes ) {
  2363. X    r = image->r = (byte *) Malloc( totalsize );
  2364. X    g = image->g = (byte *) Malloc( totalsize );
  2365. X    b = image->b = (byte *) Malloc( totalsize );
  2366. X    image->depth = 24;
  2367. X  }else {
  2368. X    r = image->r = (byte *) Malloc( totalsize );
  2369. X    image->depth = no_bits( tga.cmap_length ) + 1;
  2370. X  }
  2371. X  image->colors = ( 1 << image->depth );
  2372. X
  2373. X  /*
  2374. X   * Read the raster information.
  2375. X   */
  2376. X  if ( allplanes ) {
  2377. X    read_tga24( handle, r, g, b, &tga );
  2378. X
  2379. X  }else if ( forceallplanes )
  2380. X          read_tga_to24( handle, r, g, b, &tga, image->cmap );
  2381. X        else read_tga8( handle, r, &tga );
  2382. X
  2383. X  if ( TGAFLIP( tga.image_descriptor ) ) {
  2384. X    VPRINTF(stderr, "Flipping the image\n");
  2385. X    aux = (byte *) flip( r, image->xsize, image->ysize );
  2386. X    Free( r );
  2387. X    image->r = aux;
  2388. X    if ( allplanes ) {
  2389. X      /* A true color image, so we have G and B buffers */
  2390. X      aux = (byte *) flip( g, image->xsize, image->ysize );
  2391. X      Free( g );
  2392. X      image->g = aux;
  2393. X      aux = (byte *) flip( b, image->xsize, image->ysize );
  2394. X      Free( b );
  2395. X      image->b = aux;
  2396. X    }
  2397. X  }
  2398. X}
  2399. X
  2400. END_OF_FILE
  2401.   if test 45778 -ne `wc -c <'tovcr.c.A'`; then
  2402.     echo shar: \"'tovcr.c.A'\" unpacked with wrong size!
  2403.   elif test -f 'tovcr.c.B'; then
  2404.     echo shar: Combining  \"'tovcr.c'\" \(99170 characters\)
  2405.     cat 'tovcr.c.A' 'tovcr.c.B' > 'tovcr.c'
  2406.     if test 99170 -ne `wc -c <'tovcr.c'`; then
  2407.       echo shar: \"'tovcr.c'\" combined with wrong size!
  2408.     else
  2409.       rm tovcr.c.A tovcr.c.B 
  2410.     fi
  2411.   fi
  2412.   # end of 'tovcr.c.A'
  2413. fi
  2414. echo shar: End of archive 1 \(of 4\).
  2415. cp /dev/null ark1isdone
  2416. MISSING=""
  2417. for I in 1 2 3 4 ; do
  2418.     if test ! -f ark${I}isdone ; then
  2419.     MISSING="${MISSING} ${I}"
  2420.     fi
  2421. done
  2422. if test "${MISSING}" = "" ; then
  2423.     echo You have unpacked all 4 archives.
  2424.     rm -f ark[1-9]isdone
  2425. else
  2426.     echo You still must unpack the following archives:
  2427.     echo "        " ${MISSING}
  2428. fi
  2429. exit 0
  2430. exit 0 # Just in case...
  2431.