home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / iff / spec / iff.txt
Text File  |  1994-06-01  |  199KB  |  4,611 lines

  1.  
  2. A Quick Introduction to IFF
  3.  
  4. Jerry Morrison, Electronic Arts
  5. 10-17-88
  6.  
  7.  
  8. IFF is the Amiga-standard "Interchange File Format", designed to work across 
  9. many machines.
  10.  
  11.  
  12. Why IFF?
  13.  
  14. Did you ever have this happen to your picture file?
  15.  
  16.    You can't load it into another paint program.
  17.    You need a converter to adopt to "ZooPaint" release 2.0 or a new 
  18.       hardware feature.
  19.    You must "export" and "import" to use it in a page layout program.
  20.    You can't move it to another brand of computer.
  21.  
  22. What about interchanging musical scores, digitized audio, and other data?  It 
  23. seems the only thing that does interchange well is plain ASCII text files.
  24.  
  25. It's inexcusable. And yet this is "normal" in MS-DOS.
  26.  
  27.  
  28. What is IFF?
  29.  
  30. IFF, the "Interchange File Format" standard, encourages multimedia interchange 
  31. between different programs and different computers.  It supports long-lived, 
  32. extensible data.  It's great for composite files like a page layout file that 
  33. includes photos, an animation file that includes music, and a library of 
  34. sound effects.
  35.  
  36. IFF is a 2-level standard.  The first layer is the "wrapper" or "envelope" 
  37. structure for all IFF files.  Technically, it's the syntax.  The second layer 
  38. defines particular IFF file types such as ILBM (standard raster pictures), 
  39. ANIM (animation), SMUS (simple musical score), and 8SVX (8-bit sampled audio 
  40. voice).
  41.  
  42. IFF is also a design idea: programs should use interchange formats for their 
  43. everyday storage.
  44.  
  45. This way, users rarely need converters and import/export commands to change 
  46. software releases, application programs, or hardware.
  47.  
  48.  
  49. What's the trick?
  50.  
  51. File compatibility is easy to achieve if programmers let go of one notion--
  52. dumping internal data structures to disk.  A program's internal data structures 
  53. should really be suited to what the program does and how it works.  What's 
  54. "best" changes as the program evolves new functions and methods.  But a disk 
  55. format should be suited to storage and interchange.
  56.  
  57. Once we design internal formats and disk formats for their own separate 
  58. purposes, the rest is easy.  Reading and writing become behind-the-scenes 
  59. conversions.  But two conversions hidden in each program is much better than 
  60. a pile of conversion programs.
  61.  
  62.  
  63. Does this seem strange?  It's what ASCII text programs do!  Text editors use 
  64. line tables, piece tables, gaps, and other structures for fast editing and 
  65. searching.  Text generators and consumers construct and parse files.  That's 
  66. why the ASCII standard works so well.
  67.  
  68. Also, every file must be self-sufficient.  E.g., a picture file has to include 
  69. its size and number of bits/pixel.
  70.  
  71.  
  72. What's an IFF file look like?
  73.  
  74. IFF is based on data blocks called "chunks". Here's an example color map
  75. chunk:
  76.  
  77.                          +------------------+ in an ILBM file, CMAP means
  78.           char typeID[4] | 'CMAP'           | "color map"
  79.                          |------------------+ 
  80.   unsigned long dataSize |  48              | 48 data bytes
  81.                          |------------------+
  82.              char data[] |  0,0,0,255,      | 16 3-byte color values: black, 
  83.                          |  255,255,255...  | white, ....
  84.                          +------------------+
  85.  
  86.                          +------------------+ in an ILBM file, CMAP means
  87.                          | 'CMAP'           | "color map"
  88.                          |------------------+ 
  89.                          |  48              | 48 data bytes
  90.                          |------------------+
  91.                          |  0,0,0,255,      | 16 3-byte color values: black, 
  92.                          |  255,255,255...  | white, ....
  93.                          +------------------+
  94.  
  95. A chunk is made of a 4-character type identifier, a 32 bit data byte count, 
  96. and the data bytes. It's like a Macintosh "resource" with a 32-bit size.
  97.  
  98. Fine points:
  99.  
  100. oEvery 16- and 32-bit number is stored in 68000 byte order--highest 
  101. byte first.
  102.  
  103. An Intel CPU must reverse the 2- or 4-byte sequence of each number. 
  104. This applies to chunk dataSize fields and to numbers inside chunk 
  105. data.  It does not affect character strings and byte data because you 
  106. can't reverse a 1-byte sequence.  But it does affect the 32-bit math 
  107. used in IFF's MakeID macro.  The standard does allow CPU specific byte 
  108. ordering hidden within a chunk itself, but the practice is discouraged.
  109.  
  110. oEvery 16- and 32-bit number is stored on an even address.
  111.  
  112. oEvery odd-length chunk must be followed by a 0 pad byte.  This pad 
  113. byte is not counted in dataSize.
  114.  
  115. oAn ID is made of 4 ASCII characters in the range " " (space, hex 20) 
  116. through "~" (tilde, hex 7E).  Leading spaces are not permitted.
  117.  
  118. oIDs are compared using a quick 32-bit equality test.  Case matters.
  119.  
  120.  
  121. A chunk typically holds a C structure, Pascal record, or an array.  For 
  122. example, an 'ILBM' picture has a 'BMHD' bitmap header chunk (a structure) and 
  123. a 'BODY' raster body chunk (an array).
  124.  
  125.  
  126. To construct an IFF file, just put a file type ID (like 'ILBM') into a wrapper 
  127. chunk called a 'FORM' (Think "FILE").  Inside that wrapper place chunks one 
  128. after another (with pad bytes as needed).  The chunk size always tells you 
  129. how many more bytes you need to skip over to get to the next chunk.
  130.  
  131.  
  132.           +------------------+
  133.           |                  |
  134.           |  'FORM'          |  FORM is a special chunk ID
  135.           |                  |
  136.           +------------------+
  137.           |   24070          |  24070 data bytes
  138.        -- +------------------+ 
  139.        |  |                  |
  140.        |  |  'ILBM'          |  FORM type is ILBM
  141.        |  |                  |
  142.        |  | +--------------+ |
  143.        |  | |  'BMHD'      | |  A BMHD bitmap header chunk 
  144.        |  | |--------------| |  (20 data bytes)
  145.        |  | |   20         | |
  146.        |  | |--------------| |
  147.        |  | |  320, 200,   | |
  148.        |  | |  0 ....      | |
  149.        |  | +--------------+ |
  150.        |  |                  |
  151.        |  | +--------------+ |
  152.        |  | |  'CMAP'      | |  A CMAP color map chunk
  153. 24070 <   | |--------------| |  (21 data bytes = 1 pad)
  154. bytes  |  | |   21         | |
  155.        |  | |--------------| |
  156.        |  | |  0, 0, 0,    | |
  157.        |  | |  255 ....    | |
  158.        |  | +--------------+ |
  159.        |  |   0              |  A pad byte
  160.        |  | +--------------+ |
  161.        |  | |  'BODY'      | |
  162.        |  | |--------------| |  A BODY raster body chunk
  163.        |  | |  24000       | |  (24000 data bytes)
  164.        |  | |--------------| |
  165.        |  | |  0, 0, 0.... | |
  166.        |  | +--------------+ |
  167.        -- +------------------+
  168.  
  169.  
  170. A FORM always contains one 4-character FORM type ID (a file type, in this 
  171. case 'ILBM') followed by any number of data chunks.  In this example, the FORM 
  172. type is 'ILBM', which stands for "InterLeaved BitMap".  (ILBM is an IFF 
  173. standard for bitplane raster pictures.)  This example has 3 chunks.  Note the 
  174. pad byte after the odd length chunk.
  175.  
  176. Within FORMs ILBM, 'BMHD' identifies a bitmap header chunk, 'CMAP' a color 
  177. map, and 'BODY' a raster body.  In general, the chunk IDs in a FORM are 
  178. local to the FORM type ID.  The  exceptions are the 4 global chunk IDs 'FORM', 
  179. 'LIST', 'CAT ', and 'PROP'.  (A FORM may contain other FORM chunks.  E.g., an 
  180. animation FORM might contain picture FORMs and sound FORMs.)
  181.  
  182.  
  183.  
  184. How to read an IFF file?
  185.  
  186. Given the C subroutine "GetChunkHeader()":
  187.  
  188. /* Skip any remaining bytes of the current chunk, skip any pad byte, 
  189. and read the next chunk header. Returns the chunk ID or END_MARK. */
  190.  
  191. ID GetChunkHeader();
  192.  
  193. we read the chunks in a FORM ILBM with a loop like this:
  194.  
  195.  
  196. do
  197.   switch (id = GetChunkHeader())
  198.     {
  199.     case 'CMAP': ProcessCMAP(); break;
  200.     case 'BMHD': ProcessBMHD(); break;
  201.     case 'BODY': ProcessBODY(); break;
  202.     /* default: just ignore the chunk */
  203.     }
  204.   until (id == END_MARK);
  205.  
  206. This loop processes each chunk by dispatching to a routine that reads the 
  207. specific type of chunk data.  We don't assume a particular order of chunks. 
  208. This is a simple parser.  Note that even if you have fully processed a chunk, 
  209. you should respect it's chunk size, even if the size is larger than you 
  210. expected.
  211.  
  212. This sample ignores important details like I/O errors.  There are also higher-
  213. level errors to check, e.g., if we hit END_MARK without reading a BODY, we 
  214. didn't get a picture.
  215.  
  216. Every IFF file is a 'FORM', 'LIST', or 'CAT ' chunk.  You can recognize an IFF 
  217. file by those first 4 bytes.  ('FORM' is far and away the most common.  We'll 
  218. get to LIST and CAT below.)  If the file contains a FORM, dispatch on the FORM 
  219. type ID to a chunk-reader loop like the one above.
  220.  
  221.  
  222. File extensibility
  223.  
  224. IFF files are extensible and forward/backward compatible:
  225.  
  226. oChunk contents should be designed for compatibility across 
  227. environments and for longevity.  Every chunk should have a path for 
  228. future expansion; at minimum this will be an unused bit or two.
  229.  
  230. oThe standards team for a FORM type can extend one of the chunks that 
  231. contains a structure by appending new, optional structure fields.
  232.  
  233. oAnyone can define new FORM types as well as new chunk types within a 
  234. FORM type.  Storing private chunks within a FORM is OK, but be sure 
  235. to register your activities with Commodore Applications and Technical 
  236. Support.
  237.  
  238. oA chunk can be superseded by a new chunk type, e.g., to store more 
  239. bits per RGB color register. New programs can output the old chunk 
  240. (for backward compatibility) along with the new chunk.
  241.  
  242. oIf you must change data in an incompatible way, change the chunk ID or 
  243. the FORM type ID.
  244.  
  245.  
  246.  
  247.  
  248. Advanced Topics: CAT, LIST, and PROP (not all that important)
  249.  
  250. Sometimes you want to put several "files" into one, such as a picture library.
  251. This is what CAT is for.  It "concatenates" FORM and LIST chunks.
  252.  
  253.           +--------------------+
  254.           |                    |
  255.           |   'CAT '           |  concatenation
  256.           |                    |
  257.           +--------------------+
  258.           |   48160            |  48160 data bytes
  259.           +--------------------+ 
  260.           |                    |
  261.           |   'ILBM'           |  This concatenation contains FORMs ILBM
  262.           |                    |
  263.           |  +--------------+  |
  264.           |  |  'FORM'      |  |  A FORM ILBM
  265.           |  |--------------|  |
  266.           |  |   24070      |  |
  267.           |  |--------------|  |
  268.           |  |  'ILBM'      |  |
  269.           |  |--------------|  |
  270.           |  |  ....        |  |
  271.           |  +--------------+  |
  272.           |                    |
  273.           |  +--------------+  |
  274.           |  |  'FORM'      |  |  Another FORM ILBM
  275.           |  |--------------|  |
  276.           |  |   24070      |  |
  277.           |  |--------------|  |
  278.           |  |  'ILBM'      |  |
  279.           |  |--------------|  |
  280.           |  |  ....        |  |
  281.           |  +--------------+  |
  282.           |                    |
  283.           +--------------------+
  284.  
  285.  
  286. This example CAT holds two ILBMs. It can be shown outline-style:
  287.  
  288. CAT ILBM
  289. ..FORM ILBM   \
  290. ....BMHD      | a complete FORM ILBM picture
  291. ....CMAP      |
  292. ....BODY      /
  293. ..FORM ILBM
  294. ....BMHD
  295. ....CMAP
  296. ....BODY
  297.  
  298. Sometimes you want to share the same color map across many pictures. LIST 
  299. and PROP do this:
  300.  
  301. LIST ILBM
  302. ..PROP ILBM   default properties for FORMs ILBM
  303. ....CMAP      an ILBM CMAP chunk (there could be a BMHD chunk here, too)
  304. ..FORM ILBM
  305. ....BMHD      (there could be a CMAP here to override the default)
  306. ....BODY
  307. ..FORM ILBM
  308. ....BMHD      (there could be a CMAP here to override the default)
  309. ....BODY
  310.  
  311.  
  312. A LIST holds PROPs and FORMs (and occasionally LISTs and CATs).  A PROP ILBM 
  313. contains default data (in the above example, just one CMAP chunk) for all 
  314. FORMs ILBM in the LIST.  Any FORM may override the PROP-defined default with 
  315. its own CMAP.  All PROPs must appear at the beginning of a LIST.  Each FORM 
  316. type standardizes (among other things) which of its chunks are "property 
  317. chunks" (may appear in PROPs) and which are "data chunks" (may not appear 
  318. in PROPs).
  319.  
  320.  
  321.  
  322.  
  323.  
  324. "EA IFF 85" Standard for Interchange Format Files
  325.  
  326.  
  327. Document Date:January 14, 1985  (Updated Oct, 1988 Commodore-Amiga, Inc.)
  328. From:Jerry Morrison, Electronic Arts
  329. Status:Released to the public domain, and in use
  330.  
  331.  
  332. 1.  Introduction
  333.  
  334. Standards are Good for Software Developers
  335.  
  336. As home computer hardware evolves into better and better media machines, the 
  337. demand increases for higher quality, more detailed data.  Data development 
  338. gets more expensive, requires more expertise and better tools, and has to be 
  339. shared across projects.  Think about several ports of a product on one CD-ROM 
  340. with 500M Bytes of common data!
  341.  
  342. Development tools need standard interchange file formats.  Imagine scanning 
  343. in images of "player" shapes, transferring them to an image enhancement 
  344. package, moving them to a paint program for touch up, then incorporating them 
  345. into a game.  Or writing a theme song with a Macintosh score editor and 
  346. incorporating it into an Amiga game.  The data must at times be transformed, 
  347. clipped, filled out, and moved across machine kinds.  Media projects will 
  348. depend on data transfer from graphic, music, sound effect, animation, and 
  349. script tools.
  350.  
  351.  
  352. Standards are Good for Software Users
  353.  
  354. Customers should be able to move their own data between independently 
  355. developed software products.  And they should be able to buy data libraries 
  356. usable across many such products.  The types of data objects to exchange are 
  357. open-ended and include plain and formatted text, raster and structured 
  358. graphics, fonts, music, sound effects, musical instrument descriptions, 
  359. and animation.
  360.  
  361. The problem with expedient file formats--typically memory dumps is that 
  362. they're too provincial.  By designing data for one particular use (such as a 
  363. screen snapshot), they preclude future expansion (would you like a full page 
  364. picture?  a multi-page document?).  In neglecting the possibility that other 
  365. programs might read their data, they fail to save contextual information
  366. (how many bit planes?  what resolution?).  Ignoring that other programs might 
  367. create such files, they're intolerant of extra data (a different picture 
  368. editor may want to save a texture palette with the image), missing data (such 
  369. as no color map), or minor variations (perhaps a smaller image).  In practice, 
  370. a filed representation should rarely mirror an in-memory representation.  The
  371. former should be designed for longevity; the latter to optimize the 
  372. manipulations of a particular program.  The same filed data will be read into 
  373. different memory formats by different programs.
  374.  
  375. The IFF philosophy: "A little behind-the-scenes conversion when programs read 
  376. and write files is far better than NxM explicit conversion utilities for 
  377. highly specialized formats".
  378.  
  379. So we need some standardization for data interchange among development tools 
  380. and products.  The more developers that adopt a standard, the better for all 
  381. of us and our customers.
  382.  
  383.  
  384.  
  385. Here is "EA IFF 1985"
  386.  
  387. Here is our offering: Electronic Arts' IFF standard for Interchange File 
  388. Format.  The full name is "EA IFF 1985".  Alternatives and justifications 
  389. are included for certain choices.  Public domain subroutine packages and 
  390. utility programs are available to make it easy to write and use IFF-compatible 
  391. programs.
  392.  
  393. Part 1 introduces the standard.  Part 2 presents its requirements and 
  394. background.  Parts 3, 4, and 5 define the primitive data types, FORMs, and 
  395. LISTs, respectively, and how to define new high level types.  Part 6 
  396. specifies the top level file structure.  Section 7 lists names of the group
  397. responsible for this standard.  Appendix A is included for quick reference 
  398. and Appendix B.
  399.  
  400.  
  401. References
  402.  
  403. American National Standard Additional Control Codes for Use with ASCII, ANSI 
  404. standard 3.64-1979 for an 8-bit character set.  See also ISO standard 2022 
  405. and ISO/DIS standard 6429.2.
  406.  
  407. The C Programming Language, Brian W. Kernighan and Dennis M. Ritchie, Bell 
  408. Laboratories.  Prentice-Hall, Englewood Cliffs, NJ, 1978.
  409.  
  410. C, A Reference Manual, Samuel P. Harbison and Guy L. Steele Jr., Tartan 
  411. Laboratories.  Prentice-Hall, Englewood Cliffs, NJ, 1984.
  412.  
  413. Compiler Construction, An Advanced Course, edited by F. L. Bauer and J. Eickel
  414. (Springer-Verlag, 1976).  This book is one of many sources for information on 
  415. recursive descent parsing.
  416.  
  417. DIF Technical Specification (c) 1981 by Software Arts, Inc.  DIF(tm) is the 
  418. format for spreadsheet data interchange developed by Software Arts, Inc.  
  419. DIF(tm) is a trademark of Software Arts, Inc.
  420.  
  421. "FTXT" IFF Formatted Text, from Electronic Arts.  IFF supplement document for 
  422. a text format.
  423.  
  424. "ILBM" IFF Interleaved Bitmap, from Electronic Arts.  IFF supplement document 
  425. for a raster image format.
  426.  
  427. M68000 16/32-Bit Microprocessor Programmer's Reference Manual (c) 1984, 1982, 
  428. 1980, 1979 by Motorola, Inc.
  429.  
  430. PostScript Language Manual (c) 1984 Adobe Systems Incorporated.
  431. PostScript(tm) is a trademark of Adobe Systems, Inc.
  432. Times and Helvetica. are registered trademarks of Allied Corporation.
  433.  
  434. Inside Macintosh (c) 1982, 1983, 1984, 1985 Apple Computer, Inc., a programmer's
  435. reference manual.
  436. Apple. is a trademark of Apple Computer, Inc.
  437. MacPaint(tm) is a trademark of Apple Computer, Inc.
  438. Macintosh(tm) is a trademark licensed to Apple Computer, Inc.
  439.  
  440. InterScript: A Proposal for a Standard for the Interchange of Editable 
  441. Documents (c) 1984 Xerox Corporation.  Introduction to InterScript (c) 1985 
  442. Xerox Corporation.
  443.  
  444. Amiga. is a registered trademark of Commodore-Amiga, Inc.
  445.  
  446. Electronics Arts(tm) is a trademark of Electronic Arts.
  447.  
  448.  
  449.  
  450.  
  451. 2.  Background for Designers
  452.  
  453. Part 2 is about the background, requirements, and goals for the standard.  
  454. It's geared for people who want to design new types of IFF objects.  People 
  455. just interested in using the standard may wish to quickly scan this section.
  456.  
  457.  
  458. What Do We Need?
  459.  
  460. A standard should be long on prescription and short on overhead.  It should 
  461. give lots of rules for designing programs and data files for synergy.  But 
  462. neither the programs nor the files should cost too much more than the 
  463. expedient variety.  Although we are looking to a future with CD-ROMs and
  464. perpendicular recording, the standard must work well on floppy disks.
  465.  
  466. For program portability, simplicity, and efficiency, formats should be 
  467. designed with more than one implementation style in mind.  It ought to be 
  468. possible to read one of many objects in a file without scanning all the 
  469. preceding data.   (In practice, pure stream I/O is adequate although random 
  470. access makes it easier to write files.)  Some programs need to read and play 
  471. out their data in real time, so we need good compromises between generality 
  472. and efficiency.
  473.  
  474. As much as we need standards, they can't hold up product schedules.  So we 
  475. also need a kind of decentralized extensibility where any software developer 
  476. can define and refine new object types without some "standards authority" in 
  477. the loop.  Developers must be able to extend existing formats in a forward- 
  478. and backward-compatible way.  A central repository for design information and 
  479. example programs can help us take full advantage of the standard.
  480.  
  481. For convenience, data formats should heed the restrictions of various 
  482. processors and environments.  For example, word-alignment greatly helps 68000 
  483. access at insignificant cost to 8088 programs.
  484.  
  485. Other goals include the ability to share common elements over a list of 
  486. objects and the ability to construct composite objects. 
  487.  
  488. And finally, "Simple things should be simple and complex things should be 
  489. possible" - Alan Kay.
  490.  
  491.  
  492. Think Ahead
  493.  
  494. Let's think ahead and build programs that read and write files for each other 
  495. and for programs yet to be designed.  Build data formats to last for future 
  496. computers so long as the overhead is acceptable.  This extends the usefulness 
  497. and life of today's programs and data.
  498.  
  499. To maximize interconnectivity, the standard file structure and the specific 
  500. object formats must all be general and extensible.  Think ahead when designing 
  501. an object.  File formats should serve many purposes and allow many programs to 
  502. store and read back all the information they need; even squeeze in custom 
  503. data.  Then a programmer can store the available data and is encouraged to
  504. include fixed contextual details.  Recipient programs can read the needed 
  505. parts, skip unrecognized stuff, default missing data, and use the stored 
  506. context to help transform the data as needed.
  507.  
  508.  
  509.  
  510. Scope
  511.  
  512. IFF addresses these needs by defining a standard file structure, some initial 
  513. data object types, ways to define new types, and rules for accessing these 
  514. files.  We can accomplish a great deal by writing programs according to this 
  515. standard, but do not expect direct compatibility with existing software.  
  516. We'll need conversion programs to bridge the gap from the old world.
  517.  
  518. IFF is geared for computers that readily process information in 8-bit bytes.  
  519. It assumes a "physical layer" of data storage and transmission that reliably 
  520. maintains "files" as sequences of 8-bit bytes.  The standard treats a "file" 
  521. as a container of data bytes and is independent of how to find a file and 
  522. whether it has a byte count.
  523.  
  524. This standard does not by itself implement a clipboard for cutting and pasting 
  525. data between programs.  A clipboard needs software to mediate access, and 
  526. provide a notification mechanism so updates and requests for data can be 
  527. detected.
  528.  
  529.  
  530. Data Abstraction
  531.  
  532. The basic problem is how to represent information  in a way that's program-
  533. independent, compiler- independent, machine-independent, and device-independent.
  534.  
  535. The computer science approach is "data abstraction", also known as "objects", 
  536. "actors", and "abstract data types".  A data abstraction has a "concrete 
  537. representation" (its storage format), an "abstract representation" (its 
  538. capabilities and uses), and access procedures that isolate all the calling 
  539. software from the concrete representation.  Only the access procedures touch 
  540. the data storage.  Hiding mutable details behind an interface is called 
  541. "information hiding".  What is hidden are the non-portable details of 
  542. implementing the object, namely the selected storage representation and 
  543. algorithms for manipulating it.
  544.  
  545. The power of this approach is modularity.  By adjusting the access procedures 
  546. we can extend and restructure the data without impacting the interface or its 
  547. callers.  Conversely, we can extend and restructure the interface and callers 
  548. without making existing data obsolete.  It's great for interchange!
  549.  
  550. But we seem to need the opposite: fixed file formats for all programs to 
  551. access.  Actually, we could file data abstractions ("filed objects") by 
  552. storing the data and access procedures together.  We'd have to encode the 
  553. access procedures in a standard machine-independent programming language a la 
  554. PostScript.  Even with this, the interface can't evolve freely since we can't 
  555. update all copies of the access procedures.  So we'll have to design our 
  556. abstract representations for limited evolution and occasional revolution 
  557. (conversion).
  558.  
  559. In any case, today's microcomputers can't practically store true data 
  560. abstractions.  They can do the next best thing: store arbitrary types of data 
  561. in "data chunks", each with a type identifier and a length count.  The type 
  562. identifier is a reference by name to the access procedures (any local 
  563. implementation).  The length count enables storage-level object operations 
  564. like "copy" and "skip to next" independent of object type or contents.
  565.  
  566. Chunk writing is straightforward.  Chunk reading requires a trivial parser to 
  567. scan each chunk and dispatch to the proper access/conversion procedure.  
  568. Reading chunks nested inside other chunks may require recursion, but no look 
  569. ahead or backup.
  570.  
  571. That's the main idea of IFF.  There are, of course, a few other details....
  572.  
  573.  
  574.  
  575. Previous Work
  576.  
  577. Where our needs are similar, we borrow from existing standards.
  578.  
  579. Our basic need to move data between independently developed programs is 
  580. similar to that addressed by the Apple Macintosh desk scrap or "clipboard" 
  581. [Inside Macintosh chapter "Scrap Manager"].  The Scrap Manager works closely 
  582. with the Resource Manager, a handy filer and swapper for data objects (text 
  583. strings, dialog window templates, pictures, fonts?) including types yet to be 
  584. designed [Inside Macintosh chapter "Resource Manager"].  The Resource Manager
  585. is akin to Smalltalk's object swapper.
  586.  
  587. We will probably write a Macintosh desk accessory that converts IFF files to 
  588. and from the Macintosh clipboard for quick and easy interchange with programs 
  589. like MacPaint and Resource Mover.
  590.  
  591. Macintosh uses a simple and elegant scheme of four-character "identifiers" to 
  592. identify resource types, clipboard format types, file types, and file creator 
  593. programs.  Alternatives are unique ID numbers assigned by a central authority 
  594. or by hierarchical authorities, unique ID numbers generated by algorithm, 
  595. other fixed length character strings, and variable length strings.  Character 
  596. string identifiers double as readable signposts in data files and programs.  
  597. The choice of 4 characters is a good tradeoff between storage space, 
  598. fetch/compare/store time, and name space size.  We'll honor Apple's designers 
  599. by adopting this scheme.
  600.  
  601. "PICT" is a good example of a standard structured graphics format (including 
  602. raster images) and its many uses [Inside Macintosh chapter "QuickDraw"].  
  603. Macintosh provides QuickDraw routines in ROM to create, manipulate, and 
  604. display PICTs.  Any application can create a PICT by simply asking QuickDraw 
  605. to record a sequence of drawing commands.  Since it's just as easy to ask 
  606. QuickDraw to render a PICT to a screen or a printer, it's very effective to 
  607. pass them betweenprograms, say from an illustrator to a word processor.  An 
  608. important feature is the ability to store "comments" in a PICT which QuickDraw 
  609. will ignore.  (Actually, it passes them to your optional custom "comment 
  610. handler".)
  611.  
  612. PostScript, Adobe System's print file standard, is a more general way to 
  613. represent any print image (which is a specification for putting marks on 
  614. paper) [PostScript Language Manual].  In fact, PostScript is a full-fledged 
  615. programming language.  To interpret a PostScript program is to render a 
  616. document on a raster output device.  The language is defined in layers: a 
  617. lexical layer of identifiers, constants, and operators; a layer of reverse 
  618. polish semantics including scope rules and a way to define new subroutines; 
  619. and a printing-specific layer of built-in identifiers and operators for 
  620. rendering graphic images.  It is clearly a powerful (Turing equivalent) image
  621. definition language.  PICT and a subset of PostScript are candidates for 
  622. structured graphics standards.
  623.  
  624. A PostScript document can be printed on any raster output device (including 
  625. a display) but cannot generally be edited.  That's because the original 
  626. flexibility and constraints have been discarded.  Besides, a PostScript 
  627. program may use arbitrary computation to supply parameters like placement and 
  628. size to each operator.  A QuickDraw PICT, in comparison, is a more restricted
  629. format of graphic primitives parameterized by constants.  So a PICT can be 
  630. edited at the level of the primitives, e.g., move or thicken a line.  It cannot 
  631. be edited at the higher level of, say, the bar chart data which generated the 
  632. picture.
  633.  
  634. PostScript has another limitation: not all kinds of data amount to marks on 
  635. paper.  A musical instrument description is one example.  PostScript is just 
  636. not geared for such uses.
  637.  
  638.  
  639. "DIF" is another example of data being stored in a general format usable by 
  640. future programs [DIF Technical Specification].  DIF is a format for 
  641. spreadsheet data interchange.  DIF and PostScript are both expressed in plain 
  642. ASCII text files.  This is very handy for printing, debugging, experimenting, 
  643. and transmitting across modems.  It can have substantial cost in compaction 
  644. and read/write work, depending on use.  We won't store IFF files this way but 
  645. we could define an ASCII alternate representation with a converter program.
  646.  
  647. InterScript is the Xerox standard for interchange of editable documents 
  648. [Introduction to InterScript].  It approaches a harder problem: How to 
  649. represent editable word processor documents that may contain formatted text, 
  650. pictures, cross-references like figure numbers, and even highly specialized 
  651. objects like mathematical equations?  InterScript aims to define one standard 
  652. representation for each kind of information.  Each InterScript-compatible 
  653. editor is supposed to preserve the objects it doesn't understand and even 
  654. maintain nested cross-references.  So a simple word processor would let you 
  655. edit the text of a fancy document without discarding the equations or 
  656. disrupting the equation numbers.
  657.  
  658. Our task is similarly to store high level information and preserve as much 
  659. content as practical while moving it between programs.  But we need to span a 
  660. larger universe of data types and cannot expect to centrally define them all.  
  661. Fortunately, we don't need to make programs preserve information that they 
  662. don't understand.  And for better or worse, we don't have to tackle general-
  663. purpose cross-references yet.
  664.  
  665.  
  666. 3.  Primitive Data Types
  667.  
  668. Atomic components such as integers and characters that are interpretable 
  669. directly by the CPU are specified in one format for all processors.  We chose 
  670. a format that's the same as used by the Motorola MC68000 processor [M68000 
  671. 16/32-Bit Microprocessor Programmer's Reference Manual].  The high byte and 
  672. high word of a number are stored first.
  673.  
  674. N.B.: Part 3 dictates the format for "primitive" data types where--and only 
  675. where--used in the overall file structure.  The number of such occurrences of 
  676. dictated formats will be small enough that the costs of conversion, storage, 
  677. and management of processor-specific files would far exceed the costs of 
  678. conversion during I/O by "foreign" programs.  A particular data chunk may be
  679. specified with a different format for its internal primitive types or with 
  680. processor or environment specific variants if necessary to optimize local 
  681. usage.  Since that hurts data interchange, it's not recommended.  (Cf. 
  682. Designing New Data Sections, in Part 4.)
  683.  
  684.  
  685. Alignment
  686.  
  687. All data objects larger than a byte are aligned on even byte addresses 
  688. relative to the start of the file.  This may require padding.  Pad bytes are 
  689. to be written as zeros, but don't count on that when reading.
  690.  
  691. This means that every odd-length "chunk" must be padded so that the next one 
  692. will fall on an even boundary.  Also, designers of structures to be stored in 
  693. chunks should include pad fields where needed to align every field larger than 
  694. a byte.  For best efficiency, long word data should be arranged on long word 
  695. (4 byte) boundaries.  Zeros should be stored in all the pad bytes.
  696.  
  697.  
  698. Justification: Even-alignment causes a little extra work for files that are 
  699. used only on certain processors but allows 68000 programs to construct and 
  700. scan the data in memory and do block I/O.  Any 16-bit or greater CPU will have 
  701. faster access to aligned data.  You just add an occasional pad field to data 
  702. structures that you're going to block read/write or else stream read/write an
  703. extra byte.  And the same source code works on all processors.  Unspecified 
  704. alignment, on the other hand, would force 68000 programs to (dis)assemble 
  705. word and long word data one byte at a time.  Pretty cumbersome in a high level 
  706. language.  And if you don't conditionally compile that step out for other 
  707. processors, you won't gain anything.
  708.  
  709.  
  710. Numbers
  711.  
  712. Numeric types supported are two's complement binary integers in the format 
  713. used by the MC68000 processor--high byte first, high word first--the reverse 
  714. of 8088 and 6502 format.
  715.  
  716. UBYTE 8 bits unsigned
  717. WORD16 bits signed
  718. UWORD16 bits unsigned
  719. LONG32 bits signed
  720.  
  721. The actual type definitions depend on the CPU and the compiler.  In this 
  722. document, we'll express data type definitions in the C programming language.  
  723. [See C, A Reference Manual.]  In 68000 Lattice C:
  724.  
  725. typedef unsigned charUBYTE;/*  8 bits unsigned*/
  726. typedef shortWORD;/* 16 bits signed*/
  727. typedef unsigned shortUWORD;/* 16 bits unsigned*/
  728. typedef longLONG;/* 32 bits signed*/
  729.  
  730.  
  731. Characters
  732.  
  733. The following character set is assumed wherever characters are used, e.g., in 
  734. text strings, IDs, and TEXT chunks (see below).  Characters are encoded in 
  735. 8-bit ASCII.  Characters in the range NUL (hex 0) through DEL (hex 7F) are 
  736. well defined by the 7-bit ASCII standard.  IFF uses the graphic group " " 
  737. (SP, hex 20) through "~" (hex 7E).
  738.  
  739. Most of the control character group hex 01 through hex 1F have no standard 
  740. meaning in IFF.  The control character LF (hex 0A) is defined as a "newline" 
  741. character.  It denotes an intentional line break, that is, a paragraph or line 
  742. terminator.  (There is no way to store an automatic line break.  That is 
  743. strictly a function of the margins in the environment the text is placed.) 
  744. The controlcharacter ESC (hex 1B) is a reserved escape character under the 
  745. rules of ANSI standard 3.64-1979 American National Standard Additional 
  746. Control Codes for Use with ASCII, ISO standard 2022, and ISO/DIS standard 
  747. 6429.2.
  748.  
  749. Characters in the range hex 7F through hex FF are not globally defined in IFF. 
  750. They are best left reserved for future standardization.  (Note that the FORM 
  751. type FTXT (formatted text) defines the meaning of these characters within FTXT 
  752. forms.)  In particular, character values hex 7F through hex 9F are control 
  753. codes while characters hex A0 through hex FF are extended graphic characters 
  754. like E, as per the ISO and ANSI standards cited above.  [See the supplementary
  755. document "FTXT" IFF Formatted Text.]
  756.  
  757.  
  758. Dates
  759.  
  760. A "creation date" is defined as the date and time a stream of data bytes was 
  761. created.  (Some systems call this a "last modified date".)  Editing some data 
  762. changes its creation date.  Moving the data between volumes or machines does 
  763. not.
  764.  
  765. The IFF standard date format will be one of those used in MS-DOS, Macintosh, 
  766. or AmigaDOS (probably a 32-bit unsigned number of seconds since a reference 
  767. point).  Issue: Investigate these three.
  768.  
  769.  
  770. Type IDs
  771.  
  772. A "type ID", "property name", "FORM type", or any other IFF identifier is a 
  773. 32-bit value: the concatenation of four ASCII characters in the range " " (SP, 
  774. hex 20) through "~" (hex 7E).  Spaces (hex 20) should not precede printing 
  775. characters; trailing spaces are OK.  Control characters are forbidden.
  776.  
  777. typedef CHAR ID[4];
  778.  
  779. IDs are compared using a simple 32-bit case-dependent equality test.  FORM 
  780. type IDs are restricted.  Since they may be stored in filename extensions 
  781. lower case letters and punctuation marks are forbidden.  Trailing spaces are OK.
  782.  
  783. Carefully choose those four characters when you pick a new ID.  Make them 
  784. mnemonic so programmers can look at an interchange format file and figure 
  785. out what kind of data it contains.  The name space makes it possible for 
  786. developers scattered around the globe to generate ID values with minimal 
  787. collisions so long as they choose specific names like "MUS4" instead of 
  788. general ones like "TYPE" and "FILE".
  789.  
  790. Commodore Applications and Technical Support has undertaken the task of 
  791. maintaining the registry of FORM type IDs and format descriptions.  See the 
  792. IFF registry document for more information.
  793.  
  794. Sometimes it's necessary to make data format changes that aren't backward 
  795. compatible.  As much as we work for compatibility, unintended interactions 
  796. can develop.  Since IDs are used to denote data formats in IFF, new IDs are 
  797. chosen to denote revised formats.  Since programs won't read chunks whose IDs 
  798. they don't recognize (see Chunks, below), the new IDs keep old programs from
  799. stumbling over new data.  The conventional way to chose a "revision" ID is to 
  800. increment the last character if it's a digit or else change the last character 
  801. to a digit.  E.g., first and second revisions of the ID "XY" would be "XY1" 
  802. and "XY2".  Revisions of "CMAP" would be "CMA1" and "CMA2".
  803.  
  804.  
  805. Chunks
  806.  
  807. Chunks are the building blocks in the IFF structure.  The form expressed as 
  808. a C typedef is:
  809.  
  810.     typedef struct {
  811. IDckID;/* 4 character ID */
  812. LONGckSize;/* sizeof(ckData) */
  813. UBYTEckData[/* ckSize */];
  814. } Chunk;
  815.  
  816.  
  817.  
  818. We can diagram an example chunk --a "CMAP" chunk containing 12 data bytes --
  819. like this:
  820.  
  821.                         +------------------+   -
  822.         ckID:           | 'CMAP'           |   |
  823.                         |------------------+   |
  824.         ckSize:         |  12              |   |
  825.                     -   |------------------+   |
  826.         ckData:     |   |  0, 0, 0, 32     |  20 bytes
  827.                    12   |------------------+   |
  828.                   bytes |  0, 0, 64, 0     |   |
  829.                     |   |------------------+   |
  830.                     |   |  0, 0, 64, 0     |   |
  831.                     -   +------------------+   -
  832.  
  833.  
  834. That's 4 bytes of ckID, 4 bytes of ckSize and 12 data bytes.  The total space 
  835. used is 20 bytes.
  836.  
  837. The ckID identifies the format and purpose of the chunk.  As a rule, a 
  838. program must recognize ckID to interpret ckData.  It should skip over all 
  839. unrecognized chunks.  The ckID also serves as a format version number as long 
  840. as we pick new IDs to identify new formats of ckData (see above).
  841.  
  842. The following ckIDs are universally reserved to identify chunks with 
  843. particular IFF meanings: "LIST", "FORM", "PROP", "CAT ", and "    ".  The 
  844. special ID "    " (4 spaces) is a ckID for "filler" chunks, that is, chunks 
  845. that fill space but have no meaningful contents.  The IDs "LIS1" through 
  846. "LIS9", "FOR1" through "FOR9", and "CAT1" through "CAT9" are reserved for 
  847. future "version number" variations.  All IFF-compatible software must account 
  848. for these chunk IDs.
  849.  
  850. The ckSize is a logical block size--how many data bytes are in ckData.  If 
  851. ckData is an odd number of bytes long, a 0 pad byte follows which is not 
  852. included in ckSize.  (Cf. Alignment.)  A chunk's total physical size is ckSize 
  853. rounded up to an even number plus the size of the header.  So the smallest 
  854. chunk is 8 bytes long with ckSize = 0.  For the sake of following chunks, 
  855. programs must respect every chunk's ckSize as a virtual end-of-file for 
  856. reading its ckData even if that data is malformed, e.g., if nested contents 
  857. are truncated.
  858.  
  859. We can describe the syntax of a chunk as a regular expression with "#" 
  860. representing the ckSize, the length of the following {braced} bytes.  The 
  861. "[0]" represents a sometimes needed pad byte.  (The regular expressions in 
  862. this document are collected in Appendix A along with an explanation of 
  863. notation.)
  864.  
  865. Chunk::= ID #{ UBYTE* } [0]
  866.  
  867. One chunk output technique is to stream write a chunk header, stream write 
  868. the chunk contents, then random access back to the header to fill in the size. 
  869. Another technique is to make a preliminary pass over the data to compute the 
  870. size, then write it out all at once.
  871.  
  872.  
  873. Strings, String Chunks, and String Properties
  874.  
  875. In a string of ASCII text, linefeed (0x0A) denotes a forced line break 
  876. (paragraph or line terminator).  Other control characters are not used.  (Cf. 
  877. Characters.)  For maximum compatibility with line editors, two linefeed 
  878. characters are often used to indicate a paragraph boundary.
  879.  
  880.  
  881. The ckID for a chunk that contains a string of plain, unformatted text is 
  882. "TEXT".  As a practical matter, a text string should probably not be longer 
  883. than 32767 bytes.  The standard allows up to 2^31 - 1 bytes.  The ckID "TEXT" 
  884. is globally reserved for this use.
  885.  
  886. When used as a data property (see below), a text string chunk may be 0 to 255 
  887. characters long.  Such a string is readily converted to a C string or a Pascal 
  888. STRING[255].  The ckID of a property must have a unique property name, not  
  889. "TEXT".
  890.  
  891. When used as a part of a chunk or data property, restricted C string format is 
  892. normally used.  That means 0 to 255 characters followed by a NULL byte (ASCII 
  893. value 0).
  894.  
  895.  
  896. Data Properties (advanced topic)
  897.  
  898. Data properties specify attributes for following (non-property) chunks.  A 
  899. data property essentially says "identifier = value", for example "XY = (10, 
  900. 200)", telling something about following chunks.  Properties may only appear 
  901. inside data sections ("FORM" chunks, cf. Data Sections) and property sections 
  902. ("PROP" chunks, cf. Group PROP).
  903.  
  904. The form of a data property is a type of Chunk.  The ckID is a property name 
  905. as well as a property type.  The ckSize should be small since data properties 
  906. are intended to be accumulated in RAM when reading a file.  (256 bytes is a 
  907. reasonable upper bound.)  Syntactically:
  908.  
  909. Property::= Chunk
  910.  
  911. When designing a data object, use properties to describe context information 
  912. like the size of an image, even if they don't vary in your program.  Other 
  913. programs will need this information.
  914.  
  915. Think of property settings as assignments to variables in a programming 
  916. language.  Multiple assignments are redundant and local assignments 
  917. temporarily override global assignments.  The order of assignments doesn't 
  918. matter as long as they precede the affected chunks.  (Cf. LISTs, CATs, and 
  919. Shared Properties.)
  920.  
  921. Each object type (FORM type) is a local name space for property IDs.  Think of 
  922. a "CMAP" property in a "FORM ILBM" as the qualified ID "ILBM.CMAP".  A "CMAP" 
  923. inside some other type of FORM may not have the same meaning.  Property IDs 
  924. specified when an object type is designed (and therefore known to all clients) 
  925. are called "standard" while specialized ones added later are "nonstandard".
  926.  
  927.  
  928. Links
  929.  
  930. Issue: A standard mechanism for "links" or "cross references" is very 
  931. desirable for things like combining images and sounds into animations.  
  932. Perhaps we'll define "link" chunks within FORMs that refer to other FORMs or 
  933. to specific chunks within the same and other FORMs.  This needs further work.  
  934. EA IFF 1985 has no standard link mechanism.
  935.  
  936. For now, it may suffice to read a list of, say, musical instruments, and then 
  937. just refer to them within a musical score by  sequence  number.
  938.  
  939.  
  940.  
  941. File References
  942.  
  943. Issue: We may need a standard form for references to other files.  A "file ref"
  944. could name a directory and a file in the same type of operating system as the 
  945. reference's originator.  Following the reference would expect the file to be 
  946. on some mounted volume, or perhaps the same directory as the file that made 
  947. the reference.  In a network environment, a file reference could name a server, 
  948. too.
  949.  
  950. Issue: How can we express operating-system independent file references?
  951.  
  952. Issue: What about a means to reference a portion of another file?  Would this 
  953. be a "file ref" plus a reference to a "link" within the target file?
  954.  
  955.  
  956. 4.  Data Sections
  957.  
  958. The first thing we need of a file is to check: Does it contain IFF data and, 
  959. if so, does it contain the kind of data we're looking for?  So we come to the 
  960. notion of a "data section".
  961.  
  962. A "data section" or IFF "FORM" is one self-contained "data object" that might 
  963. be stored in a file by itself.  It is one high level data object such as a 
  964. picture or a sound effect, and generally contains a grouping of chunks.  The 
  965. IFF structure "FORM" makes it self-identifying.  It could be a composite
  966. object like a musical score with nested musical instrument descriptions.
  967.  
  968.  
  969. Group FORM
  970.  
  971. A data section is a chunk with ckID "FORM" and this arrangement:
  972.  
  973. FORM   ::= "FORM" #{ FormType (LocalChunk | FORM | LIST | CAT)* }
  974. FormType   ::= ID
  975. LocalChunk ::= Property | Chunk
  976.  
  977. The ID "FORM" is a syntactic keyword like "struct" in C.  Think of a "struct 
  978. ILBM" containing a field "CMAP".  If you see "FORM" you will know to expect a 
  979. FORM type ID (the structure name, "ILBM" in this example) and a particular 
  980. contents arrangement or "syntax" (local chunks, FORMs, LISTs, and CATs).  A 
  981. "FORM ILBM", in particular, might contain a local chunk "CMAP", an "ILBM.CMAP" 
  982. (to use a qualified name).
  983.  
  984. So the chunk ID "FORM" indicates a data section.  It implies that the chunk 
  985. contains an ID and some number of nested chunks.  In reading a FORM, like any 
  986. other chunk, programs must respect its ckSize as a virtual end-of-file for 
  987. reading its contents, even if they're truncated.
  988.  
  989. The FORM type is a restricted ID that may not contain lower case letters or 
  990. punctuation characters.  (Cf. Type IDs.  Cf. Single Purpose Files.)
  991.  
  992. The type-specific information in a FORM is composed of its "local chunks": 
  993. data properties and other chunks.  Each FORM type is a local name space for 
  994. local chunk IDs.  So "CMAP" local chunks in other FORM types may be unrelated 
  995. to "ILBM.CMAP".  More than that, each FORM type defines semantic scope.  If 
  996. you know what a FORM ILBM is, you will know what an ILBM.CMAP is.
  997.  
  998. Local chunks defined when the FORM type is designed (and therefore known to 
  999. all clients of this type) are called "standard" while specialized ones added 
  1000. later are "nonstandard".
  1001.  
  1002.  
  1003. Among the local chunks, property chunks give settings for various details 
  1004. like text font while the other chunks supply the essential information.  This 
  1005. distinction is not clear cut.  A property setting can be cancelled by a later 
  1006. setting of the same property.  E.g., in the sequence:
  1007.  
  1008. prop1 = x  (Data A)  prop1 = z  prop1 = y (Data B)
  1009.  
  1010. prop1 is = x for Data A, and y for Data B.  The setting prop1 = z has no effect.
  1011.  
  1012. For clarity, the universally reserved chunk IDs "LIST", "FORM", "PROP", 
  1013. "CAT ", "    ", "LIS1" through "LIS9", "FOR1" through "FOR9", and "CAT1" 
  1014. through "CAT9" may not be FORM type IDs.
  1015.  
  1016. Part 5, below, talks about grouping FORMs into LISTs and CATs.  They let you 
  1017. group a bunch of FORMs but don't impose any particular meaning or constraints 
  1018. on the grouping.  Read on.
  1019.  
  1020.  
  1021. Composite FORMs
  1022.  
  1023. A FORM chunk inside a FORM is a full-fledged data section.  This means you can 
  1024. build a composite object such as a multi-frame animation sequence by nesting 
  1025. available picture FORMs and sound effect FORMs.  You can insert additional 
  1026. chunks with information like frame rate and frame count.
  1027.  
  1028. Using composite FORMs, you leverage on existing programs that create and edit 
  1029. the component FORMs.  Those editors may even look into your composite object 
  1030. to copy out its type of component.  Such editors are not allowed to replace 
  1031. their component objects within your composite object.  That's because the IFF 
  1032. standard lets you specify consistency requirements for the composite FORM such 
  1033. as maintaining a count or a directory of the components.  Only programs that 
  1034. are written to uphold the rules of your FORM type may create or modify such 
  1035. FORMs.
  1036.  
  1037. Therefore, in designing a program that creates composite objects, you are 
  1038. strongly requested to provide a facility for your users to import and export 
  1039. the nested FORMs.  Import and export could move the data through a clipboard 
  1040. or a file.
  1041.  
  1042. Here are several existing FORM types and rules for defining new ones:
  1043.  
  1044. FTXT
  1045.  
  1046. An FTXT data section contains text with character formatting information 
  1047. like fonts and faces.  It has no paragraph or document formatting information 
  1048. like margins and page headers.  FORM FTXT is well matched to the text 
  1049. representation in Amiga's Intuition environment.  See the supplemental 
  1050. document "FTXT" IFF Formatted Text.
  1051.  
  1052. ILBM
  1053.  
  1054. "ILBM" is an InterLeaved BitMap image with color map; a machine-independent 
  1055. format for raster images.  FORM ILBM is the standard image file format for 
  1056. the Commodore-Amiga computer and is useful in other environments, too.  See 
  1057. the supplemental document "ILBM" IFF Interleaved Bitmap.
  1058.  
  1059. PICS
  1060.  
  1061. The data chunk inside a "PICS" data section has ID "PICT" and holds a 
  1062. QuickDraw picture.  Issue: Allow more than one PICT in a PICS?  See Inside 
  1063. Macintosh chapter "QuickDraw" for details on PICTs and how to create and 
  1064. display them on the Macintosh computer.
  1065.  
  1066.  
  1067. The only standard property for PICS is "XY", an optional property that 
  1068. indicates the position of the PICT relative to "the big picture".  The 
  1069. contents of an XY is a QuickDraw Point.
  1070.  
  1071. Note: PICT may be limited to Macintosh use, in which case there'll be another 
  1072. format for structured graphics in other environments.
  1073.  
  1074.  
  1075. Other Macintosh Resource Types
  1076.  
  1077. Some other Macintosh resource types could be adopted for use within IFF files; 
  1078. perhaps MWRT, ICN, ICN#, and STR#.
  1079.  
  1080. Issue: Consider the candidates and reserve some more IDs.
  1081.  
  1082.  
  1083. Designing New Data Sections
  1084.  
  1085. Supplemental documents will define additional object types.  A supplement 
  1086. needs to specify the object's purpose, its FORM type ID, the IDs and formats 
  1087. of standard local chunks, and rules for generating and interpreting the data.  
  1088. It's a good idea to supply typedefs and an example source program that 
  1089. accesses the new object.  See "ILBM" IFF Interleaved Bitmap for such an example.
  1090.  
  1091. Anyone can pick a new FORM type ID but should reserve it with Commodore
  1092. Applications and Technical Support (CATS) at their earliest convenience.  
  1093. While decentralized  format definitions and extensions are possible in IFF, 
  1094. our preference is to get design consensus by committee, implement a program 
  1095. to read and write it, perhaps tune the format before it becomes locked in 
  1096. stone, and then publish the format with example code.  Some organization 
  1097. should remain in charge of answering questions and coordinating extensions 
  1098. to the format.
  1099.  
  1100. If it becomes necessary to incompatibly revise the design of some data 
  1101. section, its FORM type ID will serve as a version number (Cf. Type IDs).  E.g., 
  1102. a revised "VDEO" data section could be called "VDE1".  But try to get by with 
  1103. compatible revisions within the existing FORM type.
  1104.  
  1105. In a new FORM type, the rules for primitive data types and word-alignment (Cf. 
  1106. Primitive Data Types) may be overridden for the contents of its local chunks--
  1107. but not for the chunk structure itself--if your documentation spells out the 
  1108. deviations.  If machine-specific type variants are needed, e.g., to store vast 
  1109. numbers of integers in reverse bit order, then outline the conversion 
  1110. algorithm and indicate the variant inside each file, perhaps via different 
  1111. FORM types.  Needless to say, variations should be minimized.
  1112.  
  1113. In designing a FORM type, encapsulate all the data that other programs will 
  1114. need to interpret your files.  E.g., a raster graphics image should specify the 
  1115. image size even if your program always uses 320 x 200 pixels x 3 bitplanes.  
  1116. Receiving programs are then empowered to append or clip the image rectangle, 
  1117. to add or drop bitplanes, etc.  This enables a lot more compatibility.
  1118.  
  1119. Separate the central data (like musical notes) from more specialized 
  1120. information (like note beams) so simpler programs can extract the central 
  1121. parts during read-in.  Leave room for expansion so other programs can squeeze 
  1122. in new kinds of information (like lyrics).  And remember to keep the property 
  1123. chunks manageably short--let's say <= 256 bytes.
  1124.  
  1125.  
  1126. When designing a data object, try to strike a good tradeoff between a super-
  1127. general format and a highly-specialized one.  Fit the details to at least one 
  1128. particular need, for example a raster image might as well store pixels in the 
  1129. current machine's scan order.  But add the kind of generality that makes the 
  1130. format usable with foreseeable hardware and software.  E.g., use a whole byte 
  1131. for each red, green, and blue color value even if this year's computer has 
  1132. only 4-bit video DACs.  Think ahead and help other programs so long as the 
  1133. overhead is acceptable.  E.g., run compress a raster by scan line rather than 
  1134. as a unit so future programs can swap images by scan line to and from 
  1135. secondary storage.
  1136.  
  1137. Try to design a general purpose "least common multiple" format that 
  1138. encompasses the needs of many programs without getting too complicated.  Be 
  1139. sure to leave provisions for future expansion.  Let's coalesce our uses around 
  1140. a few such formats widely separated in the vast design space.  Two factors 
  1141. make this flexibility and simplicity practical.  First, file storage space is 
  1142. getting very plentiful, so compaction is not always a priority.  Second, 
  1143. nearly any locally-performed data conversion work during file reading and 
  1144. writing will be cheap compared to the I/O time.
  1145.  
  1146. It must be OK to copy a LIST or FORM or CAT intact, e.g., to incorporate it 
  1147. into a composite FORM.  So any kind of internal references within a FORM must 
  1148. be relative references.  They could be relative to the start of the containing 
  1149. FORM, relative from the referencing chunk, or a sequence number into a 
  1150. collection.
  1151.  
  1152. With composite FORMs, you leverage on existing programs that create and edit 
  1153. the components.  If you write a program that creates composite objects, please 
  1154. provide a facility for users to import and export the nested FORMs.
  1155.  
  1156. Finally, don't forget to specify all implied rules in detail.
  1157.  
  1158.  
  1159. 5.  LISTs, CATs, and Shared Properties (Advanced topics)
  1160.  
  1161. Data often needs to be grouped together, for example, consider a list of icons. 
  1162. Sometimes a trick like arranging little images into a big raster works, but 
  1163. generally they'll need to be structured as a first class group.  The objects 
  1164. "LIST" and "CAT " are IFF-universal mechanisms for this purpose.  Note: LIST 
  1165. and CAT are advanced topics the first time reader will want to skip.
  1166.  
  1167. Property settings sometimes need to be shared over a list of similar objects.  
  1168. E.g., a list of icons may share one color map.  LIST provides a means called 
  1169. "PROP" to do this.  One purpose of a LIST is to define the scope of a PROP.  
  1170. A "CAT ", on the other hand, is simply a concatenation of objects.
  1171.  
  1172. Simpler programs may skip LISTs and PROPs altogether and just handle FORMs and 
  1173. CATs.  All "fully-conforming" IFF programs also know about "CAT ", "LIST", and 
  1174. "PROP".  Any program that reads a FORM inside a LIST must process shared PROPs 
  1175. to correctly interpret that FORM.
  1176.  
  1177.  
  1178. Group CAT
  1179.  
  1180. A CAT is just an untyped group of data objects.
  1181.  
  1182. Structurally, a CAT is a chunk with chunk ID "CAT " containing a "contents 
  1183. type" ID followed by the nested objects.  The ckSize of each contained chunk 
  1184. is essentially a relative pointer to the next one.
  1185.  
  1186. CAT::= "CAT " #{ ContentsType (FORM | LIST | CAT)* }
  1187. ContentsType    ::= ID-- a hint or an "abstract data type" ID
  1188.  
  1189.  
  1190. In reading a CAT, like any other chunk, programs must respect its ckSize as a 
  1191. virtual end-of-file for reading the nested objects even if they're malformed 
  1192. or truncated.
  1193.  
  1194. The "contents type" following the CAT's ckSize indicates what kind of FORMs 
  1195. are inside.  So a CAT of ILBMs would store "ILBM" there.  It's just a hint.  
  1196. It may be used to store an "abstract data type".  A CAT could just have blank 
  1197. contents ID ("    ") if it contains more than one kind of FORM.
  1198.  
  1199. CAT defines only the format of the group.  The group's meaning is open to 
  1200. interpretation.  This is like a list in LISP: the structure of cells is 
  1201. predefined but the meaning of the contents as, say, an association list 
  1202. depends on use.  If you need a group with an enforced meaning (an "abstract 
  1203. datatype" or Smalltalk "subclass"), some consistency constraints, or 
  1204. additional data chunks, use a composite FORM instead (Cf. Composite FORMs).
  1205.  
  1206. Since a CAT just means a concatenation of objects, CATs are rarely nested.  
  1207. Programs should really merge CATs rather than nest them.
  1208.  
  1209.  
  1210. Group LIST
  1211.  
  1212. A LIST defines a group very much like CAT but it also gives a scope for PROPs 
  1213. (see below).  And unlike CATs, LISTs should not be merged without 
  1214. understanding their contents.
  1215.  
  1216. Structurally, a LIST is a chunk with ckID "LIST" containing a "contents type" 
  1217. ID, optional shared properties, and the nested contents (FORMs, LISTs, and 
  1218. CATs), in that order.  The ckSize of each contained chunk is a relative 
  1219. pointer to the next one.  A LIST is not an arbitrary linked list--the cells 
  1220. are simply concatenated.
  1221.  
  1222. LIST     ::= "LIST" #{ ContentsType PROP* (FORM | LIST | CAT)* }
  1223. ContentsType ::= ID
  1224.  
  1225.  
  1226. Group PROP
  1227.  
  1228. PROP chunks may appear in LISTs (not in FORMs or CATs).  They supply shared 
  1229. properties for the FORMs in that LIST.  This ability to elevate some property 
  1230. settings to shared status for a list of forms is useful for both indirection 
  1231. and compaction.  E.g., a list of images with the same size and colors can share 
  1232. one "size" property and one "color map" property.  Individual FORMs can
  1233. override the shared settings.
  1234.  
  1235. The contents of a PROP is like a FORM with no data chunks:
  1236.  
  1237. PROP::= "PROP" #{ FormType Property* }
  1238.  
  1239. It means, "Here are the shared properties for FORM type <FormType>".
  1240.  
  1241. A LIST may have at most one PROP of a FORM type, and all the PROPs must appear
  1242. before any of the FORMs or nested LISTs and CATs.  You can have subsequences 
  1243. of FORMs sharing properties by making each subsequence a LIST.
  1244.  
  1245.  
  1246. Scoping: Think of property settings as variable bindings in nested blocks of a 
  1247. programming language.  In C this would look like:
  1248.  
  1249. #define Roman0
  1250. #define Helvetica1
  1251.  
  1252. void main()
  1253. {
  1254. int font=Roman;/* The global default */
  1255. {
  1256. printf("The font number is %d\n",font);
  1257. }
  1258. {
  1259. int font=Helvetica;/* local setting */
  1260. printf("The font number is %d\n",font);
  1261. }
  1262. {
  1263. printf("The font number is %d\n",font);
  1264. }
  1265. }
  1266.  
  1267.  
  1268. /*
  1269.  * Sample output:The font number is 0
  1270.  *The font number is 1
  1271.  *The font number is 0
  1272.  */
  1273.  
  1274.  
  1275.  
  1276.  
  1277. An IFF file could contain:
  1278.  
  1279. LIST {
  1280. PROP TEXT {
  1281. FONT {TimesRoman}/* shared setting*/
  1282. }
  1283.  
  1284. FORM TEXT {
  1285. FONT {Helvetica}/* local setting*/
  1286. CHRS {Hello }/* uses font Helvetica*/
  1287. }
  1288.  
  1289. FORM TEXT {
  1290. CHRS {there.}/* uses font TimesRoman*/
  1291. }
  1292. }
  1293.  
  1294.  
  1295. The shared property assignments selectively override the reader's global 
  1296. defaults, but only for FORMs within the group.  A FORM's own property 
  1297. assignments selectively override the global and group-supplied values.  So 
  1298. when reading an IFF file, keep property settings on a stack.  They are 
  1299. designed to be small enough to hold in main memory.
  1300.  
  1301. Shared properties are semantically equivalent to copying those properties into 
  1302. each of the nested FORMs right after their FORM type IDs.
  1303.  
  1304.  
  1305.  
  1306. Properties for LIST
  1307.  
  1308. Optional "properties for LIST" store the origin of the list's contents in a 
  1309. PROP chunk for the pseudo FORM type "LIST".  They are the properties 
  1310. originating program "OPGM", processor family "OCPU", computer type "OCMP", 
  1311. computer serial number or network address "OSN ", and user name "UNAM".  In 
  1312. our imperfect world, these could be called upon to distinguish between 
  1313. unintended variations of a data format or to work around bugs in particular 
  1314. originating/receiving program pairs.  Issue: Specify the format of these 
  1315. properties.
  1316.  
  1317. A creation date could also be stored in a property, but let's ask that file 
  1318. creating, editing, and transporting programs maintain the correct date in the 
  1319. local file system.  Programs that move files between machine types are 
  1320. expected to copy across the creation dates.
  1321.  
  1322.  
  1323. 6.  Standard File Structure
  1324.  
  1325. File Structure Overview
  1326.  
  1327. An IFF file is just a single chunk of type FORM, LIST, or CAT.  Therefore an 
  1328. IFF file can be recognized by its first 4 bytes: "FORM", "LIST", or "CAT ".  
  1329. Any file contents after the chunk's end are to be ignored.  (Some file 
  1330. transfer programs add garbage to the end of transferred files.  This 
  1331. specification protects against such common damage).
  1332.  
  1333. The simplest IFF file would be one that does no more than encapsulate some 
  1334. binary data (perhaps even an old-fashioned single-purpose binary file).  Here 
  1335. is a binary dump of such a minimal IFF example:
  1336.  
  1337. 0000: 464F524D 0000001A 534E4150 43524143    FORM....SNAPCRAC
  1338. 0010: 0000000D 68656C6C 6F2C776F 726C6421    ....hello,world!
  1339. 0020: 0A00                                   ..                    
  1340.  
  1341. The first 4 bytes indicate this is a "FORM"; the most common IFF top level 
  1342. structure.  The following 4 bytes indicate that the contents totals 26 bytes.  
  1343. The form type is listed as "SNAP".
  1344.  
  1345. Our form "SNAP" contains only one chunk at the moment; a chunk of type "CRAC". 
  1346. >From the size ($0000000D) the amount of data must be 13 bytes.  In this case, 
  1347. the data happens to correspond to the ASCII string "hello, world!<lf>".   
  1348. Since the number 13 is odd, a zero pad byte is added to the file.  At any time 
  1349. new chunks could be added to form SNAP without affecting any other aspect of
  1350. the file (other than the form size).  It's that simple.
  1351.  
  1352. Since an IFF file can be a group of objects, programs that read/write single 
  1353. objects can communicate to an extent with programs that read/write groups.  
  1354. You're encouraged to write programs that handle all the objects in a LIST or 
  1355. CAT.  A graphics editor, for example, could process a list of pictures as a 
  1356. multiple page document, one page at a time.
  1357.  
  1358. Programs should enforce IFF's syntactic rules when reading and writing files.  
  1359. Users should be told when a file is corrupt.  This ensures robust data 
  1360. transfer.  For minor damage, you may wish to give the user the option of using 
  1361. the suspect data, or cancelling.  Presumably a user could read in a damaged 
  1362. file, then save whatever was salvaged to a valid file.  The public domain IFF 
  1363. reader/writer subroutine package does some syntatic checks for you.  A utility
  1364. program"IFFCheck" is available that scans an IFF file and checks it for 
  1365. conformance to IFF's syntactic rules.  IFFCheck also prints an outline of the 
  1366. chunks in the file, showing the ckID and ckSize of each.  This is quite handy 
  1367. when building IFF programs.  Example programs are also available to show 
  1368. details of reading and writing IFF files.
  1369.  
  1370.  
  1371. A merge program "IFFJoin" will be available that logically appends IFF files 
  1372. into a single CAT group.  It "unwraps" each input file that is a CAT so that 
  1373. the combined file isn't nested CATs.
  1374.  
  1375. If we need to revise the IFF standard, the three anchoring IDs will be used as 
  1376. "version numbers".  That's why IDs "FOR1" through "FOR9", "LIS1" through 
  1377. "LIS9", and "CAT1" through "CAT9" are reserved.
  1378.  
  1379. IFF formats are designed for reasonable performance with floppy disks.  We 
  1380. achieve considerable simplicity in the formats and programs by relying on the 
  1381. host file system rather than defining universal grouping structures like 
  1382. directories for LIST contents.  On huge storage systems, IFF files could be 
  1383. leaf nodes in a file structure like a B-tree.  Let's hope the host file system
  1384. implements that for us!
  1385.  
  1386. There are two kinds of IFF files: single purpose files and scrap files.  They 
  1387. differ in the interpretation of multiple data objects and in the file's 
  1388. external type.
  1389.  
  1390.  
  1391. Single Purpose Files
  1392.  
  1393. A single purpose IFF file is for normal "document" and "archive" storage.  
  1394. This is in contrast with "scrap files" (see below) and temporary backing 
  1395. storage (non-interchange files).
  1396.  
  1397. The external file type (or filename extension, depending on the host file 
  1398. system) indicates the file's contents.  It's generally the FORM type of the 
  1399. data contained, hence the restrictions on FORM type IDs.
  1400.  
  1401. Programmers and users may pick an "intended use" type as the filename 
  1402. extension to make it easy to filter for the relevant files in a filename 
  1403. requester.  This is actually a "subclass" or "subtype" that conveniently 
  1404. separates files of the same FORM type that have different uses.  Programs 
  1405. cannot demand conformity to its expected subtypes without overly restricting 
  1406. data interchange since they cannot know about the subtypes to be used by 
  1407. future programs that users will want to exchange data with.
  1408.  
  1409. Issue: How to generate 3-letter MS-DOS extensions from 4-letter FORM type IDs?
  1410.  
  1411. Most single purpose files will be a single FORM (perhaps a composite FORM like
  1412. a musical score containing nested FORMs like musical instrument descriptions). 
  1413. If it's a LIST or a CAT, programs should skip over unrecognized objects to 
  1414. read the recognized ones or the first recognized one.  Then a program that 
  1415. can read a single purpose file can read something out of a "scrap file", too.
  1416.  
  1417.  
  1418. Scrap Files (not currently used)
  1419.  
  1420. A "scrap file" is for maximum interconnectivity in getting data between 
  1421. programs; the core of a clipboard function.  Scrap files may have type "IFF " 
  1422. or filename extension ".IFF".
  1423.  
  1424. A scrap file is typically a CAT containing alternate representations of the 
  1425. same basic information.  Include as many alternatives as you can readily 
  1426. generate.  This redundancy improves interconnectivity in situations where we 
  1427. can't make all programs read and write super-general formats.  [Inside 
  1428. Macintosh chapter "Scrap Manager".]  E.g., a graphically-annotated musical score
  1429. might be supplemented by a stripped down 4-voice melody and by a text (the 
  1430. lyrics).
  1431.  
  1432.  
  1433. The originating program should write the alternate representations in order of 
  1434. "preference": most preferred (most comprehensive) type to least preferred 
  1435. (least comprehensive) type.  A receiving program should either use the first 
  1436. appearing type that it understands or search for its own "preferred" type.
  1437.  
  1438. A scrap file should have at most one alternative of any type.  (A LIST of same 
  1439. type objects is OK as one of the alternatives.)  But don't count on this when 
  1440. reading; ignore extra sections of a type.  Then a program that reads scrap 
  1441. files can read something out of single purpose files.
  1442.  
  1443.  
  1444. Rules for Reader Programs
  1445.  
  1446. Here are some notes on building programs that read IFF files.  For LIST and 
  1447. PROP work, you should also read up on recursive descent parsers.  [See, for 
  1448. example, Compiler Construction, An Advanced Course.]
  1449.  
  1450. oThe standard is very flexible so many programs can exchange data.  
  1451. This implies a program has to scan the file and react to what's 
  1452. actually there in whatever order it appears.  An IFF reader program 
  1453. is a parser.
  1454.  
  1455. oFor interchange to really work, programs must be willing to do some 
  1456. conversion during read-in.  If the data isn't exactly what you expect, 
  1457. say, the raster is smaller than those created by your program, then 
  1458. adjust it.  Similarly, your program could crop a large picture, add 
  1459. or drop bitplanes, or create/discard a mask plane.  The program 
  1460. should give up gracefully on data that it can't convert.
  1461.  
  1462. oIf it doesn't start with "FORM", "LIST", or "CAT ", it's not an IFF-85 
  1463. file.
  1464.  
  1465. oFor any chunk you encounter, you must recognize its type ID to 
  1466. understand its contents.
  1467.  
  1468. oFor any FORM chunk you encounter, you must recognize its FORM type ID 
  1469. to understand the contained "local chunks".  Even if you don't 
  1470. recognize the FORM type, you can still scan it for nested FORMs, 
  1471. LISTs, and CATs of interest.
  1472.  
  1473. oDon't forget to skip the implied pad byte after every odd-length chunk, 
  1474. this is not  included in the chunk count!
  1475.  
  1476. oChunk types LIST, FORM, PROP, and CAT are generic groups.  They always 
  1477. contain a subtype ID followed by chunks.
  1478.  
  1479. oReaders ought to handle a CAT of FORMs in a file.  You may treat the 
  1480. FORMs like document pages to sequence through, or just use the first 
  1481. FORM.
  1482.  
  1483. oMany  IFF readers completely skip LISTs.  "Fully IFF-conforming" 
  1484. readers are those that handle LISTs, even if just to read the first 
  1485. FORM from a file.  If you do look into a LIST, you must process
  1486. shared properties (in PROP chunks) properly.  The idea is to get the 
  1487. correct data or none at all.
  1488.  
  1489. oThe nicest readers are willing to look into unrecognized FORMs for 
  1490. nested FORM types that they do recognize.  For example, a musical 
  1491. score may contain nested instrument descriptions and animation or 
  1492. desktop publishing files may contain still pictures.   This extra 
  1493. step is highly recommended.
  1494.  
  1495.  
  1496. Note to programmers: Processing PROP chunks is not simple! You'll need some 
  1497. background in interpreters with stack frames.  If this is foreign to you, 
  1498. build programs that read/write only one FORM per file.  For the more intrepid
  1499. programmers, the next paragraph summarizes how to process LISTs and PROPs.  
  1500.  
  1501. Allocate a stack frame for every LIST and FORM you encounter and initialize it 
  1502. by copying the stack frame of the parent LIST or FORM.  At the top level, 
  1503. you'll need a stack frame initialized to your program's global defaults.  
  1504. While reading each LIST or FORM, store all encountered properties into the 
  1505. current stack frame.  In the example ShowILBM, each stack frame has a place
  1506. for a bitmap header property ILBM.BMHD and a color map property ILBM.CMAP.  
  1507. When you finally get to the ILBM's BODY chunk, use the property settings 
  1508. accumulated in the current stack frame.
  1509.  
  1510. An alternate implementation would just remember PROPs encountered, forgetting 
  1511. each on reaching the end of its scope (the end of the containing LIST).  When 
  1512. a FORM XXXX is encountered, scan the chunks in all remembered PROPs XXXX, in 
  1513. order, as if they appeared before the chunks actually in the FORM XXXX.  This 
  1514. gets trickier if you read FORMs inside of FORMs.
  1515.  
  1516.  
  1517. Rules for Writer Programs
  1518.  
  1519. Here are some notes on building programs that write IFF files, which is much 
  1520. easier than reading them.  
  1521.  
  1522. oAn IFF file is a single FORM, LIST, or CAT chunk.
  1523.  
  1524. oAny IFF-85 file must start with the 4 characters "FORM", "LIST", or 
  1525. "CAT ", followed by a LONG ckSize.  There should be no data after 
  1526. the chunk end.
  1527.  
  1528. oChunk types LIST, FORM, PROP, and CAT are generic.  They always 
  1529. contain a subtype ID followed by chunks.  These three IDs are 
  1530. universally reserved, as are "LIS1" through "LIS9", "FOR1" through 
  1531. "FOR9", "CAT1" through "CAT9", and "    ".
  1532.  
  1533. oDon't forget to write a 0 pad byte after each odd-length chunk.
  1534.  
  1535. oDo not try to edit a file that you don't know how to create.  Programs 
  1536. may look into a file and copy out nested FORMs of types that they 
  1537. recognize, but they should not edit and replace the nested FORMs and 
  1538. not add or remove them.  Breaking these rules could make the 
  1539. containing structure inconsistent.  You may write a new file 
  1540. containing items you copied, or copied and modified, but don't copy 
  1541. structural parts you don't understand.
  1542.  
  1543. oYou must adhere to the syntax descriptions in Appendix A.  E.g., PROPs 
  1544. may only appear inside LISTs.
  1545.  
  1546.  
  1547. There are at least four common techniques for writing an IFF group:
  1548.  
  1549. (1)  build the data in a file mapped into virtual memory.
  1550. (2)  build the data in memory blocks and use block I/O.
  1551. (3)  stream write the data piecemeal and (don't forget!) random 
  1552. access back to set the group (or FORM) length count.
  1553. (4)  make a preliminary pass to compute the length count then stream 
  1554. write the data.
  1555.  
  1556.  
  1557. Issue: The standard disallows "blind" chunk copying for consistency reasons.  
  1558. Perhaps we can define a ckID convention for chunks that are OK to replicate 
  1559. without knowledge of the contents.  Any such chunks would need to be 
  1560. internally consistent, and not be bothered by changed external references.
  1561.  
  1562. Issue: Stream-writing an IFF FORM can be inconvenient.  With random access 
  1563. files one can write all the chunks then go back to fix up the FORM size.  With 
  1564. stream access, the FORM size must be calculated before the file is written.  
  1565. When compression is involved, this can be slow or inconvenient.  Perhaps we 
  1566. can define an "END " chunk.  The stream writer would use -1 ($FFFFFFFF) as 
  1567. the FORM size.  The reader would follow each chunk, when the reader reaches 
  1568. an "END ", it would terminate the last -1 sized chunk.  Certain new IFF FORMs 
  1569. could require that readers understand "END ".
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575. Appendix A.  Reference
  1576.  
  1577.  
  1578. Type Definitions
  1579.  
  1580. The following C typedefs describe standard IFF structures.  Declarations to 
  1581. use in practice will vary with the CPU and compiler.  For example, 68000 
  1582. Lattice C produces efficient comparison code if we define ID as a "LONG".  A 
  1583. macro "MakeID" builds these IDs at compile time.
  1584.  
  1585.  
  1586.  
  1587. /* Standard IFF types, expressed in 68000 Lattice C.*/
  1588.  
  1589. typedef unsigned char UBYTE;/*  8 bits unsigned*/
  1590. typedef short WORD;/* 16 bits signed*/
  1591. typedef unsigned short UWORD;/* 16 bits unsigned*/
  1592. typedef long LONG;/* 32 bits signed*/
  1593.  
  1594. typedef char ID[4];/* 4 chars in ' ' through '~' */
  1595.  
  1596. typedef struct {
  1597.   IDckID;
  1598.   LONGckSize;/* sizeof(ckData)*/
  1599.   UBYTEckData[/* ckSize */];
  1600.   } Chunk;
  1601.  
  1602.  
  1603. /* ID typedef and builder for 68000 Lattice C. */
  1604. typedef LONG ID; /* 4 chars in ' ' through '~'*/
  1605.  
  1606. #define MakeID(a,b,c,d) ( (a)<<24 | (b)<<16 | (c)<<8 | (d) )
  1607.  
  1608. /* Globally reserved IDs. */
  1609. #define ID_FORM   MakeID('F','O','R','M')
  1610. #define ID_LIST   MakeID('L','I','S','T')
  1611. #define ID_PROP   MakeID('P','R','O','P')
  1612. #define ID_CAT    MakeID('C','A','T',' ')
  1613. #define ID_FILLER MakeID(' ',' ',' ',' ')
  1614.  
  1615.  
  1616. Syntax Definitions
  1617.  
  1618.  
  1619. Here's a collection of the syntax definitions in this document.
  1620.  
  1621.  
  1622. Chunk     ::= ID #{ UBYTE* } [0]
  1623.  
  1624. Property     ::= Chunk
  1625.  
  1626. FORM     ::= "FORM" #{ FormType (LocalChunk | FORM | LIST | CAT)* }
  1627. FormType     ::= ID
  1628. LocalChunk   ::= Property | Chunk
  1629.  
  1630. CAT     ::= "CAT " #{ ContentsType (FORM | LIST | CAT)* }
  1631. ContentsType ::= ID-- a hint or an "abstract data type" ID
  1632.  
  1633. LIST     ::= "LIST" #{ ContentsType PROP* (FORM | LIST | CAT)* }
  1634. PROP     ::= "PROP" #{ FormType Property* }
  1635.  
  1636.  
  1637.  
  1638.  
  1639. In this extended regular expression notation, the token "#" represents a count 
  1640. of the following {braced} data bytes.  Literal items are shown in "quotes", 
  1641. [square bracketed items] are optional, and "*" means 0 or more instances.  A 
  1642. sometimes-needed pad byte is shown as "[0]".
  1643.  
  1644.  
  1645. Example Diagrams
  1646.  
  1647. Here's a box diagram for an example IFF file, a raster image FORM ILBM.  This 
  1648. FORM contains a bitmap header property chunk BMHD, a color map property chunk 
  1649. CMAP, and a raster data chunk BODY.  This particular raster is 320 x 200 
  1650. pixels x 3 bit planes uncompressed.  The "0" after the CMAP chunk represents 
  1651. a zero pad byte; included since the CMAP chunk has an odd length.  The text 
  1652. to the right of the diagram shows the outline that would be printed by the 
  1653. IFFCheck utility program for this particular file.
  1654.  
  1655.  
  1656.           +----------------------------+
  1657.           |                            |
  1658.           |  'FORM'            24070   |  FORM 24070 ILBM
  1659.           |                            |
  1660.        -  +----------------------------+
  1661.        |  |                            |
  1662.        |  |    'ILBM'                  |  
  1663.        |  |                            |
  1664.        |  |  +----------------------+  |
  1665.        |  |  |  'BMHD'         20   |  |  .BMHD 20
  1666.        |  |  |----------------------|  |  
  1667.        |  |  |  320, 200, 0, 0, 3,  |  |
  1668.        |  |  |  0, 0, 0, ....       |  |
  1669.        |  |  +----------------------+  |
  1670.        |  |                            |
  1671. 24070 <   |  +----------------------+  |
  1672.  bytes |  |  |  'CMAP'         21   |  |  .CMAP 21
  1673.        |  |  |----------------------|  |  
  1674.        |  |  |  0, 0, 0; 32, 0, 0;  |  |
  1675.        |  |  |  64, 0, 0 ...        |  |
  1676.        |  |  +----------------------+  |
  1677.        |  |     0                      |  
  1678.        |  |  +----------------------+  |
  1679.        |  |  |  'BODY'      24000   |  |
  1680.        |  |  |----------------------|  |  .BODY 24000
  1681.        |  |  |  0, 0, 0....         |  |
  1682.        |  |  +----------------------+  |
  1683.        |  |                            |
  1684.        -- +----------------------------+
  1685.  
  1686.  
  1687.  
  1688. This second diagram shows a LIST of two FORMs ILBM sharing a common BMHD 
  1689. property and a common CMAP property.  Again, the text on the right is an 
  1690. outline a la IFFCheck.
  1691.  
  1692.  
  1693.         +------------------------------+
  1694.         | 'LIST'              48114    |
  1695.         +------------------------------+
  1696.         |                              |
  1697.         |   'ILBM'                     |
  1698.         |                              |
  1699.         |  +------------------------+  |
  1700.         |  |  'PROP'        62      |  | .PROP 62 ILBM
  1701.         |  +------------------------+  |
  1702.         |  |                        |  |
  1703.         |  |  'ILBM'                |  |
  1704.         |  | +--------------------+ |  |
  1705.         |  | |  'BMHD'       20   | |  | ..BMHD 20
  1706.         |  | |--------------------| |  |
  1707.         |  | |  320, 200, 0, 0, 3,| |  |
  1708.         |  | |  0, 0, 0, ....     | |  |
  1709.         |  | +--------------------+ |  |
  1710.         |  |                        |  |
  1711.         |  | +--------------------+ |  |
  1712.         |  | |  'CMAP'       21   | |  | ..CMAP 21
  1713.         |  | |--------------------| |  |
  1714.         |  | |  0, 0, 0; 32, 0, 0;| |  |
  1715.         |  | |  64, 0, 0 ....     | |  |
  1716.         |  | +--------------------+ |  |
  1717.         |  |   0                    |  |
  1718.         |  +------------------------+  |
  1719.         |                              |
  1720.         |                              |
  1721.         |  +------------------------+  |
  1722.         |  |  'FORM'        24012   |  | .FORM 24012 ILBM
  1723.         |  +------------------------+  |
  1724.         |  |                        |  |
  1725.         |  |  'ILBM'                |  |
  1726.         |  | +--------------------+ |  |
  1727.         |  | |  'BODY'    24000   | |  | ..BODY 24000
  1728.         |  | |--------------------| |  |
  1729.         |  | |  0, 0, 0....       | |  |
  1730.         |  | +--------------------+ |  |
  1731.         |  +------------------------+  |
  1732.         |                              |
  1733.         |                              |
  1734.         |  +------------------------+  |
  1735.         |  |  'FORM'        24012   |  | .FORM 24012 ILBM
  1736.         |  +------------------------+  |
  1737.         |  |                        |  |
  1738.         |  |  'ILBM'                |  |
  1739.         |  | +--------------------+ |  |
  1740.         |  | |  'BODY'    24000   | |  | ..BODY 24000
  1741.         |  | |--------------------| |  |
  1742.         |  | |  0, 0, 0....       | |  |
  1743.         |  | +--------------------+ |  |
  1744.         |  +------------------------+  |
  1745.         |                              |
  1746.         |                              |
  1747.         +------------------------------+
  1748.  
  1749.  
  1750.  
  1751. "ILBM" IFF Interleaved Bitmap
  1752.  
  1753. Date:January 17, 1986 (CRNG data updated Oct, 1988 by Jerry Morrison)
  1754. (Appendix E added and CAMG updated Oct, 1988 by Commodore-Amiga, Inc.)
  1755.  
  1756. From:Jerry Morrison, Electronic Arts
  1757.  
  1758. Status:Released and in use
  1759.  
  1760.  
  1761.  
  1762. 1. Introduction
  1763.  
  1764. "EA IFF 85" is Electronic Arts' standard for interchange format files.  "ILBM" 
  1765. is a format for a 2 dimensional raster graphics image, specifically an 
  1766. InterLeaved bitplane BitMap image with color map.  An ILBM is an IFF "data 
  1767. section" or "FORM type", which can be an IFF file or a part of one.  ILBM 
  1768. allows simple, highly portable raster graphic storage.
  1769.  
  1770. An ILBM is an archival representation designed for three uses.  First, a stand-
  1771. alone image that specifies exactly how to display itself (resolution, size, 
  1772. color map, etc.).  Second, an image intended to be merged into a bigger 
  1773. picture which has its own depth, color map, and so on.  And third, an empty 
  1774. image with a color map selection or "palette" for a paint program.  ILBM is 
  1775. also intended as a building block for composite IFF FORMs like "animation 
  1776. sequences" and "structured graphics".  Some uses of ILBM will be to preserve as 
  1777. much information as possible across disparate environments.  Other uses will be 
  1778. to store data for a single program or highly cooperative programs while 
  1779. maintaining subtle details.  So we're trying to accomplish a lot with this one 
  1780. format.
  1781.  
  1782. This memo is the IFF supplement for FORM ILBM.  Section 2 defines the purpose 
  1783. and format of property chunks bitmap header "BMHD", color map "CMAP", hotspot 
  1784. "GRAB", destination merge data "DEST", sprite information "SPRT", and 
  1785. Commodore Amiga viewport mode "CAMG".  Section 3 defines the standard data 
  1786. chunk "BODY".  These are the "standard" chunks.  Section 4 defines the non-
  1787. standard data chunks.  Additional specialized chunks like texture pattern can 
  1788. be added later.  The ILBM syntax is summarized in Appendix A as a regular 
  1789. expression and in Appendix B as a box diagram.  Appendix C explains the 
  1790. optional run encoding scheme.  Appendix D names the committee responsible for 
  1791. this FORM ILBM standard.
  1792.  
  1793. Details of the raster layout are given in part 3, "Standard Data Chunk".  Some 
  1794. elements are based on the Commodore Amiga hardware but generalized for use on 
  1795. other computers.  An alternative to ILBM would be appropriate for computers 
  1796. with true color data in each pixel, though the wealth of available ILBM images 
  1797. makes import and export important.
  1798.  
  1799. Reference:
  1800.  
  1801. "EA IFF 85" Standard for Interchange Format Files describes the underlying 
  1802. conventions for all IFF files.
  1803. Amiga. is a registered trademark of Commodore-Amiga, Inc.
  1804. Electronic Arts(tm) is a trademark of Electronic Arts.
  1805. Macintosh(tm) is a trademark licensed to Apple Computer, Inc.
  1806. MacPaint(tm) is a trademark of Apple Computer, Inc.
  1807.  
  1808.  
  1809.  
  1810. 2. Standard Properties
  1811.  
  1812. ILBM has several defined property chunks that act on the main data chunks.  
  1813. The required property "BMHD" and any optional properties must appear before 
  1814. any "BODY" chunk.  (Since an ILBM has only one BODY chunk, any following 
  1815. properties would be superfluous.)  Any of these properties may be shared over 
  1816. a LIST of several IBLMs by putting them in a PROP ILBM (See the EA IFF 85 
  1817. document).
  1818.  
  1819. BMHD
  1820.  
  1821. The required property "BMHD" holds a BitMapHeader as defined in the following 
  1822. documentation.  It describes the dimensions of the image, the encoding used, 
  1823. and other data necessary to understand the BODY chunk to follow.
  1824.  
  1825.     typedef UBYTE Masking;     /* Choice of masking technique. */
  1826.  
  1827.     #define mskNone                0
  1828.     #define mskHasMask             1
  1829.     #define mskHasTransparentColor 2
  1830.     #define mskLasso               3
  1831.  
  1832.     typedef UBYTE Compression;    /* Choice of compression algorithm 
  1833.        applied to the rows of all source and mask planes.  "cmpByteRun1"
  1834.        is the byte run encoding described in Appendix C.  Do not compress 
  1835.        across rows! */
  1836.     #define cmpNone        0
  1837.     #define cmpByteRun1    1
  1838.  
  1839.     typedef struct {
  1840.       UWORD       w, h;             /* raster width & height in pixels      */
  1841.       WORD        x, y;             /* pixel position for this image        */
  1842.       UBYTE       nPlanes;          /* # source bitplanes                   */
  1843.       Masking     masking;
  1844.       Compression compression;
  1845.       UBYTE       Flags;            /* CMAP flags (formerly pad1, unused)   */
  1846.       UWORD       transparentColor; /* transparent "color number" (sort of) */
  1847.       UBYTE       xAspect, yAspect; /* pixel aspect, a ratio width : height */
  1848.       WORD        pageWidth, pageHeight; /* source "page" size in pixels    */
  1849.     } BitMapHeader;
  1850.  
  1851.  
  1852. Fields are filed in the order shown.  The UBYTE fields are byte-packed (the C 
  1853. compiler must not add pad bytes to the structure).
  1854.  
  1855. The fields w and h indicate the size of the image rectangle in pixels.  Each 
  1856. row of the image is stored in an integral number of 16 bit words.  The number 
  1857. of words per row is words=((w+15)/16) or Ceiling(w/16).  The fields x and y 
  1858. indicate the desired position of this image within the destination picture. 
  1859. Some reader programs may ignore x and y.  A safe default for writing an ILBM 
  1860. is (x, y) = (0, 0).
  1861.  
  1862. The number of source bitplanes in the BODY chunk is stored in nPlanes.  An ILBM 
  1863. with a CMAP but no BODY and nPlanes = 0 is the recommended way to store a 
  1864. color map.
  1865.  
  1866. Note: Color numbers are color map index values formed by pixels in the 
  1867. destination bitmap, which may be deeper than nPlanes if a DEST chunk calls 
  1868. for merging the image into a deeper image.
  1869.  
  1870.  
  1871. The field masking indicates what kind of masking is to be used for this image. 
  1872. The value mskNone designates an opaque rectangular image.  The value mskHasMask 
  1873. means that a mask plane is interleaved with the bitplanes in the BODY chunk 
  1874. (see below).  The value mskHasTransparentColor indicates that pixels in the 
  1875. source planes matching transparentColor are to be considered "transparent". 
  1876. (Actually, transparentColor isn't a "color number" since it's matched with 
  1877. numbers formed by the source bitmap rather than the possibly deeper destination
  1878. bitmap.  Note that having a transparent color implies ignoring one of the color 
  1879. registers.  The value mskLasso indicates the reader may construct a mask by 
  1880. lassoing the image as in MacPaint(tm).  To do this, put a 1 pixel border of 
  1881. transparentColor around the image rectangle.  Then do a seed fill from this 
  1882. border.  Filled pixels are to be transparent.
  1883.  
  1884. Issue: Include in an appendix an algorithm for converting a transparent color 
  1885. to a mask plane, and maybe a lasso algorithm.
  1886.  
  1887. A code indicating the kind of data compression used is stored in compression. 
  1888. Beware that using data compression makes your data unreadable by programs that 
  1889. don't implement the matching decompression algorithm.  So we'll employ as few 
  1890. compression encodings as possible.  The run encoding byteRun1 is documented in 
  1891. Appendix C.
  1892.  
  1893. The field pad1 is a pad byte reserved for future use.  It must be set to 0 for 
  1894. consistency.
  1895.  
  1896. The transparentColor specifies which bit pattern means "transparent".  This 
  1897. only applies if masking is mskHasTransparentColor or mskLasso.  Otherwise, 
  1898. transparentColor should be 0 (see above).
  1899.  
  1900. The pixel aspect ratio is stored as a ratio in the two fields xAspect and 
  1901. yAspect.  This may be used by programs to compensate for different aspects 
  1902. or to help interpret the fields w, h, x, y, pageWidth, and pageHeight, which 
  1903. are in units of pixels.  The fraction xAspect/yAspect represents a pixel's 
  1904. width/height.  It's recommended that your programs store proper fractions in 
  1905. the BitMapHeader, but aspect ratios can always be correctly compared with the 
  1906. test:
  1907.  
  1908. xAspect * yDesiredAspect = yAspect * xDesiredAspect
  1909.  
  1910. Typical values for aspect ratio are width : height = 10 : 11  for an Amiga 320 
  1911. x 200 display and 1 : 1 for a Macintosh(tm) display.
  1912.  
  1913. The size in pixels of the source "page" (any raster device) is stored in 
  1914. pageWidth and pageHeight, e.g., (320, 200) for a low resolution Amiga display. 
  1915. This information might be used to scale an image or to automatically set the 
  1916. display format to suit the image.  Note that the image can be larger than the 
  1917. page.
  1918.  
  1919.  
  1920.  
  1921. CMAP
  1922.  
  1923. The optional (but encouraged) property "CMAP" stores color map data as 
  1924. triplets of red, green, and blue intensity values.  The n color map entries 
  1925. ("color registers") are stored in the order 0 through n-1, totaling 3n bytes. 
  1926. Thus n is the ckSize/3.  Normally, n would equal 2^(nPlanes).
  1927.  
  1928. A CMAP chunk contains a ColorMap array as defined below.  Note that these 
  1929. typedefs assume a C compiler that implements packed arrays of 3-byte elements.
  1930.  
  1931.     typedef struct {
  1932.         UBYTE red, green, blue;           /* color intensities 0..255 */
  1933.         } ColorRegister;                  /* size = 3 bytes           */
  1934.  
  1935.     typedef ColorRegister ColorMap[n];    /* size = 3n bytes          */
  1936.  
  1937. The color components red, green, and blue represent fractional intensity 
  1938. values in the range 0 through 255 256ths.  White is (255, 255, 255) and black 
  1939. is (0, 0, 0).  If your machine has less color resolution, use the high order 
  1940. bits.  Shift each field right on reading (or left on writing) and assign it 
  1941. to (from) a field in a local packed format like Color4, below.  This achieves 
  1942. automatic conversion of images across environments with different color 
  1943. resolutions.  On reading an ILBM, use defaults if the color map is absent or 
  1944. has fewer color registers than you need.  Ignore any extra color registers.  
  1945. (See Appendix E for a better way to write colors)
  1946.  
  1947. The example type Color4 represents the format of a color register in the 
  1948. memory of an Amiga with the original graphics chip set, i.e., 4 bit video 
  1949. DACs.  (The ":4" tells smarter C compilers to pack the field into 4 bits.)
  1950.  
  1951.     typedef struct {
  1952.         unsigned pad1 :4, red :4, green :4, blue :4;
  1953.         } Color4;                      /*   Amiga RAM format.  Not filed.  */
  1954.  
  1955. With the latest generation of Amigas, there is a new complication.  Amigas 
  1956. with the AA chip set support a full 8 bits of color resolution.  In order 
  1957. to distinguish older CMAPs with 4-bit values from newer CMAP chunks with full 
  1958. 8-bit values, use the high order bit of BMHD.Flags (1 << 7).  If this bit is 
  1959. set, it means this CMAP is a full 8-bit value.  If this bit is clear, it is 
  1960. probably an older 4-bit CMAP with values either left justified or scaled.
  1961.  
  1962. Remember that every chunk must be padded to an even length, so a color map 
  1963. with an odd number of entries would be followed by a 0 byte, not included 
  1964. in the ckSize.
  1965.  
  1966.  
  1967. GRAB
  1968.  
  1969. The optional property "GRAB" locates a "handle" or "hotspot" of the image 
  1970. relative to its upper left corner, e.g., when used as a mouse cursor or a 
  1971. "paint brush".  A GRAB chunk contains a Point2D.
  1972.  
  1973.     typedef struct {
  1974.         WORD x, y;         /* relative coordinates (pixels) */
  1975.         } Point2D;
  1976.  
  1977.  
  1978. DEST
  1979.  
  1980. The optional property "DEST" is a way to say how to scatter zero or more 
  1981. source bitplanes into a deeper destination image.  Some readers may ignore DEST.
  1982.  
  1983. The contents of a DEST chunk is DestMerge structure:
  1984.  
  1985.    typedef struct {
  1986.        UBYTE depth;      /* # bitplanes in the original source               */
  1987.        UBYTE pad1;       /* unused; for consistency put 0 here               */
  1988.        UWORD planePick;  /* how to scatter source bitplanes into destination */
  1989.        UWORD planeOnOff; /* default bitplane data for planePick              */
  1990.        UWORD planeMask;  /* selects which bitplanes to store into            */
  1991.        } DestMerge;
  1992.  
  1993.  
  1994. The low order depth number of bits in planePick, planeOnOff, and planeMask 
  1995. correspond one-to-one with destination bitplanes.  Bit 0 with bitplane 0, etc. 
  1996. (Any higher order bits should be ignored.)  "1" bits in planePick mean "put 
  1997. the next source bitplane into this bitplane", so the number of "1" bits should 
  1998. equal nPlanes.  "0" bits mean "put the corresponding bit from planeOnOff into 
  1999. this bitplane".  Bits in planeMask gate writing to the destination bitplane: 
  2000. "1" bits mean "write to this bitplane" while "0" bits mean "leave this 
  2001. bitplane alone".  The normal case (with no DEST property) is equivalent to 
  2002. planePick = planeMask = 2^(nPlanes) - 1.
  2003.  
  2004. Remember that color numbers are formed by pixels in the destination bitmap 
  2005. (depth planes deep) not in the source bitmap (nPlanes planes deep).
  2006.  
  2007.  
  2008. SPRT
  2009.  
  2010. The presence of an "SPRT" chunk indicates that this image is intended as a 
  2011. sprite.  It's up to the reader program to actually make it a sprite, if even 
  2012. possible, and to use or overrule the sprite precedence data inside the SPRT 
  2013. chunk:
  2014.  
  2015.     typedef UWORD SpritePrecedence; /* relative precedence, 0 is the highest */
  2016.  
  2017. Precedence 0 is the highest, denoting a sprite that is foremost.
  2018.  
  2019. Creating a sprite may imply other setup.  E.g., a 2 plane Amiga sprite would 
  2020. have transparentColor = 0.  Color registers 1, 2, and 3 in the CMAP would be 
  2021. stored into the correct hardware color registers for the hardware sprite 
  2022. number used, while CMAP color register 0 would be ignored.
  2023.  
  2024.  
  2025. CAMG
  2026.  
  2027. A "CAMG" chunk is specifically for Commodore Amiga ILBM's.  All Amiga-based 
  2028. reader and writer software should deal with CAMG.  The Amiga supports many 
  2029. different video display modes including interlace, Extra Halfbrite, hold and 
  2030. modify (HAM), plus a variety of new modes under the 2.0 operating system.
  2031. A CAMG chunk contains a single long word (length=4) which specifies the Amiga 
  2032. display mode of the picture.
  2033.  
  2034. Prior to 2.0, it was possible to express all available Amiga ViewModes in 16 
  2035. bits of flags (Viewport->Modes or NewScreen->ViewModes).  Old-style writers and 
  2036. readers place a 16-bit Amiga ViewModes value in the low word of the CAMG, and 
  2037. zeros in the high word.  The following Viewmode flags should always be 
  2038. removed from old-style 16-bit ViewModes values when writing or reading them:
  2039.  
  2040. EXTENDED_MODE | SPRITES | VP_HIDE | 
  2041.           GENLOCK_AUDIO | GENLOCK_VIDEO (=0x7102, mask=0x8EFD)
  2042.  
  2043. New ILBM readers and writers, should treat the full CAMG longword as a 32-bit 
  2044. ModeID to support new and future display modes.
  2045.  
  2046. New ILBM writers, when running under the 2.0 Amiga operating system, should 
  2047. directly store the full 32-bit return value of the graphics function 
  2048. GetVPModeID(vp) in the CAMG longword.  When running under 1.3, store a 16-bit 
  2049. Viewmodes value masked as described above.
  2050.  
  2051.  
  2052. ILBM readers should only mask bits out of a CAMG if the CAMG has a zero upper 
  2053. word (see exception below).  New ILBM readers, when running under 2.0, should 
  2054. then treat the 32-bit CAMG value as a ModeID, and should use the graphics 
  2055. ModeNotAvailable() function to determine if the mode is available.  If the 
  2056. mode is not available, fall back to another suitable display mode.  When 
  2057. running under 1.3, the low word of the CAMG may generally be used to open a 
  2058. compatible display.
  2059.  
  2060. Note that one popular graphics package stores junk in the upper word of the 
  2061. CAMG of brushes, and incorrect values (generally zero) in the low word.  You 
  2062. can screen for such junk values by testing for non-zero in the upper word of 
  2063. a ModeID in conjunction with the 0x00001000 bit NOT set in the low word.
  2064.  
  2065. The following code fragment demonstrates ILBM reader filtering of
  2066. inappropriate bits in 16-bit CAMG values.
  2067.  
  2068. #include <graphics/view.h>
  2069. #include <graphics/displayinfo.h>
  2070.  
  2071. /* Knock bad bits out of old-style CAMG modes before checking availability.
  2072.  * (some ILBM CAMG's have these bits set in old 1.3 modes, and should not)
  2073.  * If not an extended monitor ID, or if marked as extended but missing
  2074.  * upper 16 bits, screen out inappropriate bits now.
  2075.  */
  2076. if((!(modeid & MONITOR_ID_MASK)) ||
  2077.     ((modeid & EXTENDED_MODE)&&(!(modeid & 0xFFFF0000))))
  2078.    modeid &= 
  2079.       (~(EXTENDED_MODE|SPRITES|GENLOCK_AUDIO|GENLOCK_VIDEO|VP_HIDE));
  2080.  
  2081. /* Check for bogus CAMG like some brushes have, with junk in
  2082.  * upper word and extended bit NOT set not set in lower word.
  2083.  */
  2084. if((modeid & 0xFFFF0000)&&(!(modeid & EXTENDED_MODE)))
  2085.     {
  2086.     /* Bad CAMG, so ignore CAMG and determine a mode based on 
  2087.      * based on pagesize or aspect
  2088.      */
  2089.      modeid = NULL;
  2090.      if(wide >= 640) modeid |= HIRES;
  2091.      if(high >= 400) modeid |= LACE;
  2092.      }
  2093.  
  2094. /* Now, ModeNotAvailable() may be used to determine if the mode is available.
  2095.  *
  2096.  * If the mode is not available, you may prompt the user for a mode
  2097.  * choice, or search the 2.0 display database for an appropriate
  2098.  * replacement mode, or you may be able to get a relatively compatible
  2099.  * old display mode by masking out all bits except
  2100.  * HIRES | LACE | HAM | EXTRA_HALFBRITE
  2101.  */
  2102.  
  2103.    if((modeid & 0xFFFF0000)&&(!(modeid & 0x00001000)))
  2104. {
  2105. /* Then probably invalid ModeID */
  2106. }
  2107.  
  2108.  
  2109. 3. Standard "BODY" Data Chunk
  2110.  
  2111. Raster Layout
  2112.  
  2113. Raster scan proceeds left-to-right (increasing X) across scan lines, then 
  2114. top-to-bottom (increasing Y) down columns of scan lines.  The coordinate 
  2115. system is in units of pixels, where (0,0) is the upper left corner.
  2116.  
  2117. The raster is typically organized as bitplanes in memory.  The corresponding 
  2118. bits from each plane, taken together, make up an index into the color map 
  2119. which gives a color value for that pixel.  The first bitplane, plane 0, is 
  2120. the low order bit of these color indexes.
  2121.  
  2122. A scan line is made of one "row" from each bitplane.  A row is one plane's bits 
  2123. for one scan line, but padded out to a word (2 byte) boundary (not necessarily 
  2124. the first word boundary).  Within each row, successive bytes are displayed in 
  2125. order and the most significant bit of each byte is displayed first.
  2126.  
  2127. A "mask" is an optional "plane" of data the same size (w, h) as a bitplane.  
  2128. It tells how to "cut out" part of the image when painting it onto another 
  2129. image.  "One" bits in the mask mean "copy the corresponding pixel to the 
  2130. destination".  "Zero" mask bits mean "leave this destination pixel alone".  In 
  2131. other words, "zero" bits designate transparent pixels.
  2132.  
  2133. The rows of the different bitplanes and mask are interleaved in the file (see 
  2134. below).  This localizes all the information pertinent to each scan line.  It 
  2135. makes it much easier to transform the data while reading it to adjust the 
  2136. image size or depth.  It also makes it possible to scroll a big image by 
  2137. swapping rows directly from the file without the need for random-access to 
  2138. all the bitplanes.
  2139.  
  2140.  
  2141.  
  2142. BODY
  2143.  
  2144. The source raster  is stored in a "BODY" chunk.  This one chunk holds all 
  2145. bitplanes and the optional mask, interleaved by row.
  2146.  
  2147. The BitMapHeader, in a BMHD property chunk, specifies the raster's dimensions 
  2148. w, h, and nPlanes.  It also holds the masking field which indicates if there 
  2149. is a mask plane and the compression field which indicates the compression 
  2150. algorithm used.  This information is needed to interpret the BODY chunk, so 
  2151. the BMHD chunk must appear first.  While reading an ILBM's BODY, a program 
  2152. may convert the image to another size by filling (with transparentColor) or 
  2153. clipping.
  2154.  
  2155. The BODY's content is a concatenation of scan lines.  Each scan line is a 
  2156. concatenation of one row of data from each plane in order 0 through nPlanes-1 
  2157. followed by one row from the mask (if masking = hasMask ).  If the BitMapHeader 
  2158. field compression is cmpNone, all h rows are exactly (w+15)/16 words wide. 
  2159. Otherwise, every row is compressed according to the specified algorithm and 
  2160. the stored widths depend on the data compression.
  2161.  
  2162. Reader programs that require fewer bitplanes than appear in a particular ILBM 
  2163. file can combine planes or drop the high-order (later) planes.  Similarly, 
  2164. they may add bitplanes and/or discard the mask plane.
  2165.  
  2166. Do not compress across rows, and don't forget to compress the mask just like 
  2167. the bitplanes.  Remember to pad any BODY chunk that contains an odd number of 
  2168. bytes and skip the pad when reading.
  2169.  
  2170.  
  2171. 4. Nonstandard Data Chunks
  2172.  
  2173. The following data chunks were defined after various programs began using FORM 
  2174. ILBM so they are "nonstandard" chunks.  See the registry document for the 
  2175. latest information on additional non-standard chunks.
  2176.  
  2177.  
  2178. CRNG
  2179.  
  2180. A "CRNG" chunk contains "color register range" information.  It's used by 
  2181. Electronic Arts' Deluxe Paint program to identify a contiguous range of 
  2182. color registers for a "shade range" and color cycling.  There can be zero or 
  2183. more CRNG chunks in an ILBM, but all should appear before the BODY chunk. 
  2184. Deluxe Paint normally writes 4 CRNG chunks in an ILBM when the user asks it 
  2185. to "Save Picture".
  2186.  
  2187.     typedef struct {
  2188.         WORD  pad1;      /* reserved for future use; store 0 here    */
  2189.         WORD  rate;      /* color cycle rate                         */
  2190.         WORD  flags;     /* see below                                */
  2191.         UBYTE low, high; /* lower and upper color registers selected */
  2192.         } CRange;
  2193.  
  2194.  
  2195. The bits of the flags word are interpreted as follows: if the low bit is set 
  2196. then the cycle is "active", and if this bit is clear it is not active.  
  2197. Normally, color cycling is done so that colors move to the next higher 
  2198. position in the cycle, with the color in the high slot moving around to the
  2199. low slot.  If the second bit of the flags word is set, the cycle moves in the 
  2200. opposite direction.  As usual, the other bits of the flags word are reserved 
  2201. for future expansion.  Here are the masks to test these bits:
  2202.  
  2203. #define RNG_ACTIVE  1
  2204. #define RNG_REVERSE 2
  2205.  
  2206. The fields low and high indicate the range of color registers (color numbers) 
  2207. selected by this CRange.
  2208.  
  2209. The field active indicates whether color cycling is on or off.  Zero means off.
  2210.  
  2211. The field rate determines the speed at which the colors will step when color 
  2212. cycling is on.  The units are such that a rate of 60 steps per second is 
  2213. represented as 2^14 = 16384.  Slower rates can be obtained by linear scaling: 
  2214. for 30 steps/second, rate = 8192; for 1 step/second, rate = 16384/60 * 273.
  2215.  
  2216.  
  2217. CCRT
  2218.  
  2219. Commodore's Graphicraft program uses a similar chunk "CCRT" (for Color 
  2220. Cycling Range and Timing).  This chunk contains a CycleInfo structure.
  2221.  
  2222.     typedef struct {
  2223.         WORD  direction;    /* 0 = don't cycle.  1 = cycle forwards      */
  2224.                             /* (1, 2, 3). -1 = cycle backwards (3, 2, 1) */
  2225.         UBYTE start, end;   /* lower and upper color registers selected  */
  2226.         LONG  seconds;      /* # seconds between changing colors plus... */
  2227.         LONG  microseconds; /* # microseconds between changing colors    */
  2228.         WORD  pad;          /* reserved for future use; store 0 here     */
  2229.         } CycleInfo;
  2230.  
  2231. This is very similar to a CRNG chunk.  A program would probably only use one 
  2232. of these two methods of expressing color cycle data, new programs should use 
  2233. CRNG.  You could write out both if you want to communicate this information 
  2234. to both Deluxe Paint and Graphicraft.
  2235.  
  2236.  
  2237. Appendix A. ILBM Regular Expression
  2238.  
  2239. Here's a regular expression summary of the FORM ILBM syntax.  This could be an 
  2240. IFF file or a part of one.
  2241.  
  2242. ILBM ::= "FORM" #{    "ILBM" BMHD [CMAP] [GRAB] [DEST] [SPRT] [CAMG]
  2243.     CRNG* CCRT* [BODY]    }
  2244.  
  2245. BMHD ::= "BMHD" #{    BitMapHeader    }
  2246. CMAP ::= "CMAP" #{    (red green blue)*    } [0]
  2247. GRAB ::= "GRAB" #{    Point2D    }
  2248. DEST ::= "DEST" #{    DestMerge    }
  2249. SPRT ::= "SPRT" #{    SpritePrecedence    }
  2250. CAMG ::= "CAMG" #{    LONG    }
  2251.  
  2252. CRNG ::= "CRNG" #{    CRange    }
  2253. CCRT ::= "CCRT" #{    CycleInfo    }
  2254. BODY ::= "BODY" #{    UBYTE*    } [0]
  2255.  
  2256.  
  2257.  
  2258. The token "#" represents a ckSize LONG count of the following {braced} data 
  2259. bytes.  E.g., a BMHD's "#" should equal sizeof(BitMapHeader).  Literal strings 
  2260. are shown in "quotes", [square bracket items] are optional, and "*" means 0 
  2261. or more repetitions.  A sometimes-needed pad byte is shown as "[0]".
  2262.  
  2263. The property chunks BMHD, CMAP, GRAB, DEST, SPRT, CAMG and any CRNG and CCRT 
  2264. data chunks may actually be in any order but all must appear before the BODY 
  2265. chunk since ILBM readers usually stop as soon as they read the BODY.  If any 
  2266. of the 6 property chunks are missing, default values are inherited from any 
  2267. shared properties (if the ILBM appears inside an IFF LIST with PROPs) or from 
  2268. the reader program's defaults.  If any property appears more than once, the 
  2269. last occurrence before the BODY is the one that counts since that's the one 
  2270. that modifies the BODY.
  2271.  
  2272.  
  2273. Appendix B. ILBM Box Diagram
  2274.  
  2275. Here's a box diagram for a simple example: an uncompressed image 320 x 200 
  2276. pixels x 3 bitplanes.  The text to the right of the diagram shows the outline 
  2277. that would be printed by the Sift utility program for this particular file.
  2278.  
  2279.  
  2280.           +--------------------+
  2281.           |                    |
  2282.           |  'FORM'     24070  |  FORM 24070 ILBM
  2283.           |                    |
  2284.        -- +--------------------+ 
  2285.        |  |                    |
  2286.        |  |  'ILBM'            |
  2287.        |  |                    |
  2288.        |  | +----------------+ |
  2289.        |  | |  'BMHD'    20  | |  .BMHD 20
  2290.        |  | |----------------| |  
  2291.        |  | |  320, 200,     | |
  2292.        |  | |  0 ....        | |
  2293.        |  | +----------------+ |
  2294.        |  |                    |
  2295.        |  | +----------------+ |
  2296. 24070 <   | |  'CMAP'    21  | |  .CMAP 21
  2297.  bytes |  | |----------------| |  
  2298.        |  | |  0, 0, 0,      | |
  2299.        |  | |  255 ....      | |
  2300.        |  | +----------------+ |
  2301.        |  |   0                |  
  2302.        |  | +----------------+ |
  2303.        |  | |  'BODY'  24000 | |  .BODY 24000
  2304.        |  | |----------------| |  
  2305.        |  | |  0, 0, 0....   | |
  2306.        |  | +----------------+ |
  2307.        |  |                    |
  2308.        -- +--------------------+
  2309.  
  2310. The "0" after the CMAP chunk is a pad byte.
  2311.  
  2312.  
  2313.  
  2314. Appendix C. IFF Hints
  2315.  
  2316. Hints on ILBM files from Jerry Morrison, Oct 1988.  How to avoid some 
  2317. pitfalls when reading ILBM files:
  2318.  
  2319. oDon't ignore the BitMapHeader.masking field.  A bitmap with a mask 
  2320. (such as a partially-transparent DPaint brush or a DPaint picture with 
  2321. a stencil) will read as garbage if you don't de-interleave the mask.
  2322.  
  2323. oDon't assume all images are compressed.  Narrow images aren't usually 
  2324. run-compressed since that would actually make them longer.
  2325.  
  2326. oDon't assume a particular image size.  You may encounter overscan 
  2327. pictures and PAL pictures.
  2328.  
  2329. There's a better way to read a BODY than the example IFF code.  The GetBODY 
  2330. routine should call a GetScanline routine once per scan line, which calls a 
  2331. GetRow routine for each bitplane in the file.  This in turn calls a 
  2332. GetUnpackedBytes routine, which calls a GetBytes routine as needed and unpacks 
  2333. the result.  (If the picture is uncompressed, GetRow calls GetBytes directly.) 
  2334. Since theunpacker knows how many packed bytes to read, this avoids juggling 
  2335. buffers for a memory-to-memory UnPackBytes routine.
  2336.  
  2337. Caution: If you make many AmigaDOS calls to read or write a few bytes at a 
  2338. time, performance will be mud! AmigaDOS has a high overhead per call, even 
  2339. with RAM disk.  So use buffered read/write routines.
  2340.  
  2341. Different hardware display devices have different color resolutions:
  2342.  
  2343.     Device        R:G:B bits    maxColor
  2344.     -------       ----------    --------
  2345.     Mac SE            1              1
  2346.     IBM EGA       2:2:2              3
  2347.     Atari ST      3:3:3              7
  2348.     Amiga         4:4:4             15 
  2349.     CD-I          5:5:5             31
  2350.     IBM VGA       6:6:6             63
  2351.     Mac II        8:8:8            255
  2352.  
  2353. An ILBM CMAP defines 8 bits of Red, Green and Blue (i.e., 8:8:8 bits of R:G:B). 
  2354. When displaying on hardware which has less color resolution, just take the 
  2355. high order bits.  For example, to convert ILBM's 8-bit Red to the Amiga's 
  2356. 4-bit Red, right shift the data by 4 bits  (R4 := R8 >> 4).
  2357.  
  2358. To convert hardware colors to ILBM colors, the ILBM specification says just 
  2359. set the high bits (R8 := R4 << 4).  But you can transmit higher contrast to 
  2360. foreign display devices by scaling the data [0..maxColor] to the full range 
  2361. [0..255].  In other words, R8 := (Rn x 255) w maxColor.  (Example #1:  EGA 
  2362. color 1:2:3 scales to 85:170:255.  Example #2:  Amiga 15:7:0 scales to
  2363. 255:119:0).  This makes a big difference where maxColor is less than 15.  In 
  2364. the extreme case, Mac SE white (1) should be converted to ILBM white (255), 
  2365. not to ILBM gray (128).
  2366.  
  2367.  
  2368.  
  2369. CGA and EGA subtleties
  2370.  
  2371. IBM EGA colors in 350 scan line mode are 2:2:2 bits of R:G:B, stored in memory 
  2372. as xxR'G'B'RBG.  That's 3 low-order bits followed by 3 high-order bits.
  2373.  
  2374. IBM CGA colors are 4 bits stored in a byte as xxxxIRGB.  (EGA colors in 200 
  2375. scan line modes are the same as CGA colors, but stored in memory as xxxIxRGB.) 
  2376. That's 3 high-order bits (one for each of R, G, and B) plus one low-order "
  2377. Intensity" bit for all 3 components R, G, and B.  Exception: IBM monitors show 
  2378. IRGB = 0110 as brown, which is really the EGA color R:G:B = 2:1:0, not dark 
  2379. yellow 2:2:0.
  2380.  
  2381.  
  2382. 24-bit ILBMS
  2383.  
  2384. When storing deep images as ILBMs (such as images with 8 bits each of R,G, 
  2385. and B), the bits for each pixel represent an absolute RGB value for that 
  2386. pixel rather than an index into a limited color map.  The order for saving 
  2387. the bits is critical since a deep ILBM would not contain the usual CMAP of 
  2388. RGB values (such a CMAP would be too large and redundant).
  2389.  
  2390. To interpret these "deep" ILBMs, it is necessary to have a standard order in 
  2391. which the bits of the R, G, and B values will be stored.  A number of 
  2392. different orderings have already been used in deep ILBMs, so it was necessary 
  2393. to us chose one of these orderings as a standard.
  2394.  
  2395. The following bit ordering has been chosen as the default bit ordering for 
  2396. deep ILBMs.
  2397.  
  2398. Default standard deep ILBM bit ordering:
  2399. saved first -------------------------------------------------> saved last
  2400. R0 R1 R2 R3 R4 R5 R6 R7  G0 G1 G2 G3 G4 G5 G6 G7  B0 B1 B2 B3 B4 B5 B6 B7
  2401.  
  2402. One other existing deep bit ordering that you may encounter is the 21-bit 
  2403. NewTek format.
  2404.  
  2405. NewTek deep ILBM bit ordering:
  2406. saved first ------------------------------------------------------> saved last
  2407. R7 G7 B7  R6 G6 B6  R5 G5 B5  R4 G4 B4  R3 G3 B3  R2 G2 B2  R1 G1 B1  R0 G0 B0
  2408.  
  2409.  
  2410. Note that you may encounter CLUT chunks in deep ILBM's.  See the Third Party 
  2411. Specs appendix for more information on CLUT chunks.
  2412.  
  2413.  
  2414.  
  2415. Appendix D. ByteRun1 Run Encoding
  2416.  
  2417. The run encoding scheme byteRun1 is best described by pseudo code for the 
  2418. decoder Unpacker (called UnPackBits in the Macintosh(tm) toolbox):
  2419.  
  2420.   UnPacker:
  2421.     LOOP until produced the desired number of bytes
  2422.         Read the next source byte into n
  2423.         SELECT n FROM
  2424.             [0..127]   => copy the next n+1 bytes literally
  2425.             [-1..-127] => replicate the next byte -n+1 times
  2426.             -128       => no operation
  2427.             ENDCASE;
  2428.         ENDLOOP;
  2429.  
  2430. In the inverse routine Packer, it's best to encode a 2 byte repeat run as a 
  2431. replicate run except when preceded and followed by a literal run, in which 
  2432. case it's best to merge the three into one literal run.  Always encode 3 byte 
  2433. repeats as replicate runs.
  2434.  
  2435. Remember that each row of each scan line of a raster is separately packed.
  2436.  
  2437.  
  2438.  
  2439.  
  2440.  
  2441.            Intro to IFF Amiga ILBM Files and Amiga Viewmodes
  2442.            =================================================
  2443.  
  2444. The IFF (Interchange File Format) for graphic images on the Amiga is called 
  2445. FORM ILBM (InterLeaved BitMap).  It follows a standard parsable IFF format.
  2446.  
  2447. Sample hex dump of beginning of an ILBM:
  2448. ========================================
  2449.  
  2450. Important note!  You can NOT ever depend on any particular ILBM chunk being 
  2451. at any particular offset into the file!  IFF files are composed, in their 
  2452. simplest form, of chunks within a FORM.  Each chunk starts starts with a 
  2453. 4-letter chunkID, followed by a 32-bit length of the rest of the chunk.  You 
  2454. PARSE IFF files, skipping past unneeded or unknown chunks by seeking their 
  2455. length (+1 if odd length) to the next 4-letter chunkID.
  2456.  
  2457. 0000: 464F524D 00016418 494C424D 424D4844    FORM..d.ILBMBMHD
  2458. 0010: 00000014 01400190 00000000 06000100    .....@..........
  2459. 0020: 00000A0B 01400190 43414D47 00000004    .....@..CAMG....
  2460. 0030: 00000804 434D4150 00000030 001100EE    ....CMAP...0....
  2461. 0040: EEEE0000 22000055 33333355 55550033    .... ..P000PPP.0
  2462. 0050: 99885544 77777711 66EE2266 EE6688DD    ..P@ppp.`. `.`..
  2463. 0060: AAAAAAAA 99EECCCC CCDDAAEE 424F4459    ............BODY
  2464. 0070: 000163AC F8000F80 148A5544 2ABDEFFF    ..c.......UD*...  etc.
  2465.  
  2466. Interpretation:
  2467.  
  2468.       'F O R M' length  'I L B M''B M H D'<-start of BitMapHeader chunk
  2469. 0000: 464F524D 00016418 494C424D 424D4844    FORM..d.ILBMBMHD
  2470.  
  2471.        length  WideHigh XorgYorg PlMkCoPd <- Planes Mask Compression Pad
  2472. 0010: 00000014 01400190 00000000 06000100    .....@..........
  2473.  
  2474.       TranAspt PagwPagh 'C A M G' length  <- start of C-AMiGa View modes chunk
  2475. 0020: 00000A0B 01400190 43414D47 00000004    .....@..CAMG....
  2476.  
  2477.       Viewmode 'C M A P' length  R g b R  <- Viewmode 800=HAM | 4=LACE 
  2478. 0030: 00000804 434D4150 00000030 001100EE    ....CMAP...0....
  2479.  
  2480.       g b R g  b R g b  R g b R  g b R g  <- Rgb's are for reg0 thru regN
  2481. 0040: EEEE0000 22000055 33333355 55550033    .... ..P000PPP.0
  2482.  
  2483.       b R g b  R g b R  g b R g  b R g b
  2484. 0050: 99885544 77777711 66EE2266 EE6688DD    ..P@ppp.`. `.`..
  2485.  
  2486.       R g b R  g b R g  b R g b 'B O D Y' 
  2487. 0060: AAAAAAAA 99EECCCC CCDDAAEE 424F4459    ............BODY
  2488.  
  2489.        length   start of body data        <- Compacted (Compression=1 above)
  2490. 0070: 000163AC F8000F80 148A5544 2ABDEFFF    ..c.......UD*...
  2491. 0080: FFBFF800 0F7FF7FC FF04F85A 77AD5DFE    ...........Zw.].  etc.
  2492.  
  2493. Notes on CAMG Viewmodes:  HIRES=0x8000  LACE=0x4  HAM=0x800  HALFBRITE=0x80
  2494.  
  2495.  
  2496.  
  2497.  
  2498. Interpreting ILBMs
  2499. ==================
  2500.       
  2501. ILBM is a fairly simple IFF FORM.  All you really need to deal with to 
  2502. extract the image are the following chunks:
  2503.  
  2504. (Note - Also watch for AUTH Author chunks and (c) Copyright chunks
  2505.  and preserve any copyright information if you rewrite the ILBM)
  2506.  
  2507.    BMHD - info about the size, depth, compaction method
  2508.           (See interpreted hex dump above)
  2509.  
  2510.    CAMG - optional Amiga viewmodes chunk
  2511.           Most HAM and HALFBRITE ILBMs should have this chunk.  If no
  2512.           CAMG chunk is present, and image is 6 planes deep, assume
  2513.           HAM and you'll probably be right.  Some Amiga viewmodes
  2514.           flags are HIRES=0x8000, LACE=0x4, HAM=0x800, HALFBRITE=0x80.
  2515.   Note that new Amiga 2.0 ILBMs may have more complex 32-bit
  2516.   numbers (modeid) stored in the CAMG.  However, the bits
  2517.   described above should get you a compatible old viewmode.
  2518.   
  2519.    CMAP - RGB values for color registers 0 to n
  2520.           (each component left justified in a byte)
  2521.   If a deep ILBM (like 12 or 24 planes), there should be no CMAP
  2522.   and instead the BODY planes are interpreted as the bits of RGB
  2523.   in the order R0...Rn G0...Gn B0...Bn
  2524.  
  2525.    BODY - The pixel data, stored in an interleaved fashion as follows:
  2526.           (each line individually compacted if BMHD Compression = 1)
  2527.              plane 0 scan line 0
  2528.              plane 1 scan line 0
  2529.              plane 2 scan line 0
  2530.              ...
  2531.              plane n scan line 0
  2532.              plane 0 scan line 1
  2533.              plane 1 scan line 1
  2534.              etc.
  2535.  
  2536.  
  2537. Body Compression
  2538. ================
  2539.  
  2540. The BODY contains pixel data for the image.  Width, Height, and depth 
  2541. (Planes) is specified in the BMHD.
  2542.  
  2543. If the BMHD Compression byte is 0, then the scan line data is not compressed.
  2544. If Compression=1, then each scan line is individually compressed as follows:
  2545.  
  2546.    More than 2 bytes the same stored as BYTE code value n from  -1 to -127 
  2547.      followed by byte to be repeated (-n) + 1 times.
  2548.    Varied bytes stored as BYTE code n from 0 to 127 followed by n+1 bytes 
  2549.      of data.
  2550.    The byte code -128 is a NOP.
  2551.  
  2552.  
  2553.  
  2554.  
  2555. Interpreting the Scan Line Data:
  2556. ================================
  2557.  
  2558. If the ILBM is not HAM or HALFBRITE, then after parsing and uncompacting if 
  2559. necessary, you will have N planes of pixel data.  Color register used for 
  2560. each pixel is specified by looking at each pixel thru the planes.  I.e., 
  2561. if you have 5 planes, and the bit for a particular pixel is set in planes 
  2562. 0 and 3:
  2563.  
  2564.        PLANE     4 3 2 1 0
  2565.        PIXEL     0 1 0 0 1
  2566.  
  2567. then that pixel uses color register binary 01001 = 9
  2568.  
  2569. The RGB value for each color register is stored in the CMAP chunk of the 
  2570. ILBM, starting with register 0, with each register's RGB value stored as
  2571. one byte of R, one byte G, and one byte of B, with each component scaled
  2572. to 8-bits.  (ie. 4-bit Amiga R, G, and B components are each stored in the
  2573. high nibble of a byte.  The low nibble may also contain valid data if the 
  2574. color was stored with 8-bit-per-gun color resolution).
  2575.  
  2576.  
  2577. BUT - if the picture is HAM or HALFBRITE, it is interpreted differently.
  2578.                         ===    =========
  2579.  
  2580. Hopefully, if the picture is HAM or HALFBRITE, the package that saved it 
  2581. properly saved a CAMG chunk (look at a hex dump of your file with ACSII 
  2582. interpretation - you will see the chunks - they all start with a 4-ASCII-
  2583. character chunk ID).  If the picture is 6 planes deep and has no CAMG chunk, 
  2584. it is probably HAM.   If you see a CAMG chunk, the "CAMG" is followed by the 
  2585. 32-bit chunk length, and then the 32-bit Amiga Viewmode flags.  
  2586.  
  2587. HAM pics with a 16-bit CAMG will have the 0x800 bit set in CAMG ViewModes.
  2588. HALBRITE pics will have the 0x80 bit set.
  2589.  
  2590. To transport a HAM or HALFBRITE picture to another machine, you must 
  2591. understand how HAM and HALFBRITE work on the Amiga.
  2592.  
  2593.  
  2594. How Amiga HAM mode works:
  2595. =========================
  2596.  
  2597. Amiga HAM (Hold and Modify) mode lets the Amiga display all 4096 RGB values.  
  2598. In HAM mode, the bits in the two last planes describe an R G or B 
  2599. modification to the color of the previous pixel on the line to create the 
  2600. color of the current pixel.  So a 6-plane HAM picture has 4 planes for 
  2601. specifying absolute color pixels giving up to 16 absolute colors which would 
  2602. be specified in the ILBM CMAP chunk.  The bits in the last two planes are 
  2603. color modification bits which cause the Amiga, in HAM mode, to take the RGB 
  2604. value of the previous pixel (Hold and), substitute the 4 bits in planes 0-3 
  2605. for the previous color's R G or B component (Modify) and display the result 
  2606. for the current pixel.  If the first pixel of a scan line is a modification 
  2607. pixel, it modifies the RGB value of the border color (register 0).  The color 
  2608. modification bits in the last two planes (planes 4 and 5) are interpreted as 
  2609. follows:
  2610.  
  2611.    00 - no modification.  Use planes 0-3 as normal color register index
  2612.    10 - hold previous, replacing Blue component with bits from planes 0-3 
  2613.    01 - hold previous, replacing Red component with bits from planes 0-3
  2614.    11 - hold previous. replacing Green component with bits from planes 0-3
  2615.  
  2616.  
  2617.  
  2618. How Amiga HALFBRITE mode works:
  2619. ===============================
  2620.  
  2621. This one is simpler.  In HALFBRITE mode, the Amiga interprets the bit in the 
  2622. last plane as HALFBRITE modification.  The bits in the other planes are 
  2623. treated as normal color register numbers (RGB values for each color register 
  2624. is specified in the CMAP chunk).  If the bit in the last plane is set (1), 
  2625. then that pixel is displayed at half brightness.  This can provide up to 64 
  2626. absolute colors.
  2627.  
  2628.  
  2629. Other Notes:
  2630. ============
  2631.  
  2632. Amiga ILBMs images must be a even number of bytes wide.  Smaller images (such 
  2633. as brushes) are padded to an even byte width.
  2634.  
  2635. ILBMs created with Electronic Arts IBM and Amiga "DPaintII" packages are 
  2636. compatible (though you may have to use a '.lbm' filename extension on an 
  2637. IBM).  The ILBM graphic files may be transferred between the machines (or 
  2638. between the Amiga and IBM sides your Amiga if you have a CBM Bridgeboard 
  2639. card installed) and loaded into either package.
  2640.  
  2641.  
  2642. Color Lookup Table chunk
  2643.  
  2644.  
  2645. TITLE: CLUT IFF chunk proposal
  2646.  
  2647. "CLUT" IFF 8-Bit Color Look Up Table
  2648.  
  2649. Date:July 2, 1989
  2650. From:Justin V. McCormick
  2651. Status:Public Proposal
  2652. Supporting Software:  FG 2.0 by Justin V. McCormick for PP&S
  2653.  
  2654.  
  2655. Introduction:
  2656.  
  2657.   This memo describes the IFF supplement for the new chunk "CLUT".
  2658.  
  2659. Description:
  2660.  
  2661.   A CLUT (Color Look Up Table) is a special purpose data module
  2662. containing table with 256 8-bit entries.  Entries in this table
  2663. can be used directly as a translation for one 8-bit value to
  2664. another.
  2665.  
  2666. Purpose:
  2667.  
  2668.   To store 8-bit data look up tables in a simple format for
  2669. later retrieval.  These tables are used to translate or bias
  2670. 8-bit intensity, contrast, saturation, hue, color registers, or
  2671. other similar data in a reproducable manner.
  2672.  
  2673. Specifications:
  2674.  
  2675. /* Here is the IFF chunk ID macro for a CLUT chunk */
  2676. #define ID_CLUT MakeID('C','L','U','T')
  2677.  
  2678. /*
  2679.  * Defines for different flavors of 8-bit CLUTs.
  2680.  */
  2681. #define CLUT_MONO0L/* A Monochrome, contrast or intensity LUT */
  2682. #define CLUT_RED1L/* A LUT for reds */
  2683. #define CLUT_GREEN2L/* A LUT for greens */
  2684. #define CLUT_BLUE3L/* A LUT for blues */
  2685. #define CLUT_HUE4L/* A LUT for hues */
  2686. #define CLUT_SAT5L/* A LUT for saturations */
  2687. #define CLUT_UNUSED66L/* How about a Signed Data flag */
  2688. #define CLUT_UNUSED77L/* Or an Assumed Negative flag*/
  2689.  
  2690. /* All types > 7 are reserved until formally claimed */
  2691. #define CLUT_RESERVED_BITS 0xfffffff8L
  2692.  
  2693. /* The struct for Color Look-Up-Tables of all types */
  2694. typedef struct
  2695. {
  2696.   ULONG type;/* See above type defines */
  2697.   ULONG res0;/* RESERVED FOR FUTURE EXPANSION */
  2698.   UBYTE lut[256];/* The 256 byte look up table */
  2699. } ColorLUT;
  2700.  
  2701.  
  2702. CLUT Example:
  2703.  
  2704.   Normally, the CLUT chunk will appear after the BMHD of an FORM
  2705. ILBM before the BODY chunk, in the same "section" as CMAPs are
  2706. normally found.  However, a FORM may contain only CLUTs with no
  2707. other supporting information.
  2708.  
  2709.   As a general guideline, it is desirable to group all CLUTs
  2710. together in a form without other chunk types between them.
  2711. If you were using CLUTs to store RGB intensity corrections, you
  2712. would write three CLUTs in a row, R, G, then B.
  2713.  
  2714.   Here is a box diagram for a 320x200x8 image stored as an IFF ILBM
  2715. with a single CLUT chunk for intensity mapping:
  2716.  
  2717.       +-----------------------------------+   
  2718.       |'FORM'64284    |     FORM 64284 ILBM
  2719.       +-----------------------------------+   
  2720.       |'ILBM'    |     
  2721.       +-----------------------------------+   
  2722.       | +-------------------------------+ |   
  2723.       | | 'BMHD'20  | |     .BMHD 20
  2724.       | | 320, 200, 0, 0, 8, 0, 0, ...  | |   
  2725.       | | ------------------------------+ |   
  2726.       | | 'CLUT'264          | |     .CLUT 264
  2727.       | | 0, 0, 0; 32, 0, 0; 64,0,0; .. | |   
  2728.       | +-------------------------------+ |   
  2729.       | +-------------------------------+ |   
  2730.       | |'BODY'64000     | |     .BODY 64000
  2731.       | |0, 0, 0, ...  | |   
  2732.       | +-------------------------------+ |   
  2733.       +-----------------------------------+   
  2734.  
  2735.  
  2736. Design Notes:
  2737. -------------
  2738.  
  2739.   I have deliberately kept this chunk simple (KISS) to
  2740. facilitate implementation.  In particular, no provision is made
  2741. for expansion to 16-bit or 32-bit tables.  My reasoning is that
  2742. a 16-bit table can have 64K entries, and thus would benefit from
  2743. data compression.  My suggestion would be to propose another
  2744. chunk or FORM type better suited for large tables rather than
  2745. small ones like CLUT.
  2746.  
  2747.  
  2748.  
  2749. Newtek Dynamic Ham color chunks
  2750.  
  2751. Newtek for Digiview IV (dynamic Ham)
  2752.  
  2753. ILBM.DYCP - dynamic color palette
  2754. 3 longwords (file setup stuff)
  2755.  
  2756. ILBM.CTBL - array of words, one for each color (0rgb)
  2757. Dots per inch chunk
  2758.  
  2759.  
  2760.  
  2761. ILBM DPI chunk (1-16-90)
  2762. ==============
  2763.  
  2764. ILBM.DPI   Dots Per Inch   to allow output of an image at the
  2765. same resolution it was scanned at
  2766.  
  2767. typedef struct {
  2768. UWORD dpi_x;
  2769. UWORD dpi_y;
  2770. } DPIHeader ;
  2771.  
  2772. For example, an image scanned at horizontal resolution of
  2773. 240dpi and vertical resolution of 300dpi would be saved as:
  2774.  
  2775. 44504920 00000004 00F0 012C
  2776. D P I    size     dpi_x dpi_y
  2777.  
  2778.  
  2779. DPaint perspective chunk (EA)
  2780.  
  2781. IFF FORM / CHUNK DESCRIPTION
  2782. ============================
  2783.  
  2784. Form/Chunk ID:   Chunk DPPV  (DPaint II ILBM perspective chunk)
  2785. Date Submitted:  12/86
  2786. Submitted by:    Dan Silva 
  2787.  
  2788. Chunk Description:
  2789.  
  2790.    The DPPV chunk describes the perspective state in a DPaintII ILBM.
  2791.  
  2792. Chunk Spec:
  2793.  
  2794. /* The chunk identifier DPPV */
  2795. #define ID_DPPV    MakeID('D','P','P','V')
  2796.  
  2797. typedef LONG LongFrac;
  2798. typedef struct ( LongFrac x,y,z; )  LFPoint;
  2799. typedef LongFrac  APoint[3];
  2800.  
  2801. typedef union {
  2802.    LFPoint l;
  2803.    APoint  a;
  2804.    } UPoint;
  2805.  
  2806. /* values taken by variable rotType */
  2807. #define ROT_EULER  0
  2808. #define ROT_INCR   1
  2809.  
  2810. /* Disk record describing Perspective state */
  2811.  
  2812. typedef struct {
  2813.    WORD     rotType;           /* rotation type */
  2814.    WORD     iA, iB, iC;        /* rotation angles (in degrees) */
  2815.    LongFrac Depth;             /* perspective depth */
  2816.    WORD     uCenter, vCenter;  /* coords of center perspective,
  2817.                                 * relative to backing bitmap,
  2818.                                 * in Virtual coords
  2819.                                 */
  2820.    WORD     fixCoord;          /* which coordinate is fixed */
  2821.    WORD     angleStep;         /* large angle stepping amount */
  2822.    UPoint   grid;              /* gridding spacing in X,Y,Z */
  2823.    UPoint   gridReset;         /* where the grid goes on Reset */
  2824.    UPoint   gridBrCenter;      /* Brush center when grid was last on,
  2825.                                 * as reference point
  2826.                                 */
  2827.    UPoint   permBrCenter;      /* Brush center the last time the mouse
  2828.                                 * button was clicked, a rotation performed,
  2829.                                 * or motion along "fixed" axis
  2830.                                 */
  2831.    LongFrac rot[3][3];         /* rotation matrix */
  2832.    } PerspState;
  2833.  
  2834. SUPPORTING SOFTWARE
  2835. ===================
  2836. DPaint II   by Dan Silva for Electronic Arts
  2837.  
  2838.  
  2839.  
  2840. DPaint IV enhanced color cycle chunk (EA)
  2841.  
  2842. DRNG Chunk for FORM ILBM
  2843. ========================
  2844.  
  2845. Submitted by Lee Taran
  2846.  
  2847. Purpose:
  2848.  
  2849. Enhanced Color Cycling Capabilities
  2850. -------------------------------------
  2851.    * DPaintIV supports a new color cycling model which does NOT
  2852.      require that color cycles contain a contiguous range of color
  2853.      registers.
  2854.  
  2855.      For example:
  2856.        If your range looks like:  [1][3][8][2]
  2857.        then at each cycle tick
  2858.           temp = [2], 
  2859.           [2] = [8],
  2860.           [8] = [3],
  2861.           [3] = [1], 
  2862.           [1] = temp
  2863.  
  2864.    * You can now cycle a single register thru a series of rgb values.
  2865.      For example:
  2866.         If your range looks like: [1] [orange] [blue] [purple]
  2867.         then at each cycle tick color register 1 will take on the
  2868.         next color in the cycle.
  2869.          
  2870.         ie:  t=0:  [1] = curpal[1]
  2871.              t=1:  [1] = purple
  2872.              t=2:  [1] = blue
  2873.              t=3:  [1] = orange
  2874.              t=4:  goto t=0
  2875.  
  2876.    * You can combine rgb cycling with traditional color cycling.
  2877.      For example:
  2878.          Your range can look like:
  2879.              [1] [orange] [blue] [2] [green] [yellow]
  2880.  
  2881.          t=0: [1] = curpal[1], [2] = curpal[2]
  2882.          t=1: [1] = yellow,    [2] = blue
  2883.          t=2: [1] = green,     [2] = orange
  2884.          t=3: [1] = curpal[2], [2] = curpal[1]
  2885.          t=4: [1] = blue,      [2] = yellow
  2886.          t=5: [1] = orange,    [2] = green
  2887.          t=6: goto t=0
  2888.  
  2889. Note:
  2890.    * DPaint will save out an old style range CRNG if the range fits
  2891.      the CRNG model otherwise it will save out a DRNG chunk. 
  2892.    * no thought has been given (yet) to interlocking cycles
  2893.  
  2894.  
  2895.  
  2896. /* --------------------------------------------------------------------- 
  2897.  
  2898.  IFF Information:  DPaintIV DRNG chunk
  2899.  
  2900.           DRNG ::= "DRNG" # { DRange DColor* DIndex* }
  2901.                                                                       
  2902.  a <cell> is where the color or register appears within the range     
  2903.  
  2904.  The RNG_ACTIVE flags is set when the range is cyclable. A range 
  2905.  should only have the RNG_ACTIVE if it:
  2906.       1> contains at least one color register
  2907.       2> has a defined rate 
  2908.       3> has more than one color and/or color register
  2909.  If the above conditions are met then RNG_ACTIVE is a user/program 
  2910.  preference.  If the bit is NOT set the program should NOT cycle the
  2911.  range.
  2912.  
  2913.  The RNG_DP_RESERVED flags should always be 0!!!
  2914.  --------------------------------------------------------------------- */
  2915. typedef struct {
  2916.    UBYTE min;           /* min cell value */
  2917.    UBYTE max;           /* max cell value */
  2918.    SHORT rate;          /* color cycling rate, 16384 = 60 steps/second */
  2919.    SHORT flags;         /* 1=RNG_ACTIVE,4=RNG_DP_RESERVED */
  2920.    UBYTE ntrue;         /* number of DColor structs to follow */
  2921.    UBYTE nregs;         /* number of DIndex structs to follow */
  2922.    } DRange;           
  2923.  
  2924. typedef struct { UBYTE cell; UBYTE r,g,b; } DColor; /* true color cell */
  2925. typedef struct { UBYTE cell; UBYTE index; } DIndex; /* color register cell */
  2926.  
  2927.  
  2928.  
  2929. Encapsulated Postscript chunk
  2930.  
  2931. ILBM EPSF Chunk
  2932. ===============
  2933.  
  2934. Pixelations   Kevin Saltzman   617-277-5414
  2935.  
  2936. Chunk to hold encapsulated postscript
  2937.  
  2938. Used by PixelScript in their clip art.  Holds a postscript
  2939. representation of the ILBM's graphic image.
  2940.  
  2941. EPSF length
  2942.    ; Bounding box
  2943.    WORD lowerleftx;
  2944.    WORD lowerlefty;
  2945.    WORD upperrightx;
  2946.    WORD upperrighty;
  2947.    CHAR []    ; ascii postscript file
  2948.  
  2949.  
  2950.  
  2951. RGB image forms, Turbo Silver (Impulse)
  2952.  
  2953.                     FORM RGBN and FORM RGB8
  2954.     -----------------------
  2955.  
  2956. RGBN and RGB8 files are used in Impulse's Turbo Silver and Imagine.
  2957. They are almost identical to FORM ILBM's except for the BODY chunk
  2958. and slight differences in the BMHD chunk.
  2959.  
  2960. A CAMG chunk IS REQUIRED.
  2961.  
  2962. The BMHD chunk specfies the number of bitplanes as 13 for type RGBN
  2963. and 25 for type RGB8, and the compression type as 4.
  2964.  
  2965. The FORM RGBN uses 12 bit RGB values, and the FORM RGB8 uses
  2966. 24 bit RGB values.
  2967.  
  2968. The BODY chunk contains RGB values, a "genlock" bit, and repeat
  2969. counts.  In Silver, when "genlock" bit is set, a "zero color" is
  2970. written into the bitplanes for genlock video to show through.
  2971. In Diamond and Light24 (Impulse 12 & 24 bit paint programs),
  2972. the genlock bit is ignored if the file is loaded as a picture
  2973. (and the RGB color is used instead), and if the file is loaded
  2974. as a brush the genlock bit marks pixels that are not part of
  2975. the brush.
  2976.  
  2977. For both RGBN and RGB8 body chunks, each RGB value always has a
  2978. repeat count.  The values are written in different formats depending
  2979. on the magnitude of the repeat count.
  2980.  
  2981. For the RGBN BODY chunk:
  2982.  
  2983. For each RGB value, a WORD (16-bits) is written: with the
  2984. 12 RGB bits in the MSB (most significant bit) positions;
  2985. the "genlock" bit next; and then a 3 bit repeat count.  
  2986. If the repeat count is greater than 7, the 3-bit count is
  2987. zero, and a BYTE repeat count follows.  If the repeat count
  2988. is greater than 255, the BYTE count is zero, and a WORD
  2989. repeat count follows.  Repeat counts greater than 65536 are
  2990. not supported.
  2991.  
  2992. For the RGB8 body chunk:
  2993.  
  2994. For each RGB value, a LONG-word (32 bits) is written:
  2995. with the 24 RGB bits in the MSB positions; the "genlock"
  2996. bit next, and then a 7 bit repeat count.
  2997.  
  2998. In a previous version of this document, there appeared the
  2999. following line:
  3000.  
  3001. "If the repeat count is greater than 127, the same rules apply
  3002. as in the RGBN BODY."
  3003.  
  3004. But Impulse has never written more than a 7 bit repeat count,
  3005. and when Imagine and Light24 were written, they didn't support
  3006. reading anything but 7 bit counts.
  3007.  
  3008. Sample BODY code:
  3009.  
  3010. if(!count) {
  3011. if (Rgb8) {
  3012. fread (&w, 4, 1, RGBFile);
  3013. lock = w & 0x00000080;
  3014. rgb = w >> 8;
  3015. count = w & 0x0000007f;
  3016. } else {
  3017. w = (UWORD) getw (RGBFile);
  3018. lock = w & 8;
  3019. rgb = w >> 4;
  3020. count = w & 7;
  3021. }
  3022. if (!count)
  3023. if (!(count = (UBYTE) getc (RGBFile)))
  3024. count = (UWORD) getw (RGBFile);
  3025. }
  3026.  
  3027. The pixels are scanned from left to right across horizontal
  3028. lines, processing from top to bottom.  The (12 or 24 bit) RGB
  3029. values are stored with the red bits as the MSB's, the green
  3030. bits next, and the blue bits as the LSB's.
  3031.  
  3032. Special note:  As of this writing (Sep 88), Silver does NOT
  3033. support anything but black for color zero.
  3034.  
  3035.  
  3036.  
  3037. Cel animation form
  3038.  
  3039.                               A N I M
  3040.                   An IFF Format For CEL Animations
  3041.  
  3042.                     Revision date:  4 May 1988
  3043.  
  3044.                      prepared by:
  3045.                           SPARTA Inc.
  3046.                           23041 de la Carlota
  3047.                           Laguna Hills, Calif 92653
  3048.                           (714) 768-8161
  3049.                           contact: Gary Bonham
  3050.  
  3051.                      also by:
  3052.                           Aegis Development Co.
  3053.                           2115 Pico Blvd.
  3054.                           Santa Monica, Calif 90405
  3055.                           213) 392-9972
  3056.  
  3057.  
  3058. 1.0 Introduction
  3059.    
  3060.    The ANIM IFF format was developed at Sparta originally for the
  3061.    production of animated video sequences on the Amiga computer.  The
  3062.    intent was to be able to store, and play back, sequences of frames
  3063.    and to minimize both the storage space on disk (through compression)
  3064.    and playback time (through efficient de-compression algorithms).
  3065.    It was desired to maintain maximum compatibility with existing
  3066.    IFF formats and to be able to display the initial frame as a normal
  3067.    still IFF picture.
  3068.    
  3069.    Several compression schemes have been introduced in the ANIM format.
  3070.    Most of these are strictly of historical interest as the only one
  3071.    currently being placed in new code is the vertical run length encoded
  3072.    byte encoding developed by Jim Kent.
  3073.    
  3074.    1.1 ANIM Format Overview
  3075.       
  3076.       The general philosophy of ANIMs is to present the initial frame
  3077.       as a normal, run-length-encoded, IFF picture.  Subsequent
  3078.       frames are then described by listing only their differences
  3079.       from a previous frame.  Normally, the "previous" frame is two
  3080.       frames back as that is the frame remaining in the hidden 
  3081.       screen buffer when double-buffering is used.  To better
  3082.       understand this, suppose one has two screens, called A and B,
  3083.       and the ability to instantly switch the display from one to
  3084.       the other.  The normal playback mode is to load the initial
  3085.       frame into A and duplicate it into B.  Then frame A is displayed
  3086.       on the screen.  Then the differences for frame 2 are used to
  3087.       alter screen B and it is displayed.  Then the differences for
  3088.       frame 3 are used to alter screen A and it is displayed, and so
  3089.       on.  Note that frame 2 is stored as differences from frame 1,
  3090.       but all other frames are stored as differences from two frames
  3091.       back.
  3092.       
  3093.       ANIM is an IFF FORM and its basic format is as follows (this
  3094.       assumes the reader has a basic understanding of IFF format
  3095.       files):
  3096.                       FORM ANIM
  3097.                       . FORM ILBM         first frame
  3098.                       . . BMHD                normal type IFF data
  3099.                       . . ANHD                optional animation header
  3100.                                               chunk for timing of 1st frame.
  3101.                       . . CMAP
  3102.                       . . BODY
  3103.                       . FORM ILBM         frame 2
  3104.                       . . ANHD                animation header chunk
  3105.                       . . DLTA                delta mode data
  3106.                       . FORM ILBM         frame 3
  3107.                       . . ANHD
  3108.                       . . DLTA
  3109.                            ...
  3110.       
  3111.       The initial FORM ILBM can contain all the normal ILBM chunks,
  3112.       such as CRNG, etc.  The BODY will normally be a standard
  3113.       run-length-encoded data chunk (but may be any other legal
  3114.       compression mode as indicated by the BMHD).  If desired, an ANHD
  3115.       chunk can appear here to provide timing data for the first
  3116.       frame.  If it is here, the operation field should be =0.
  3117.       
  3118.       The subsequent FORMs ILBM contain an ANHD, instead of a BMHD,
  3119.       which duplicates some of BMHD and has additional parameters
  3120.       pertaining to the animation frame.  The DLTA chunk contains
  3121.       the data for the delta compression modes.  If the older XOR 
  3122.       compression mode is used, then a BODY chunk will be here.  In 
  3123.       addition, other chunks may be placed in each of these as deemed 
  3124.       necessary (and as code is placed in player programs to utilize 
  3125.       them).  A good example would be CMAP chunks to alter the color 
  3126.       palette.  A basic assumption in ANIMs is that the size of the 
  3127.       bitmap, and the display mode (e.g. HAM) will not change through 
  3128.       the animation.  Take care when playing an ANIM that if a CMAP 
  3129.       occurs with a frame, then the change must be applied to both buffers.
  3130.       
  3131.       Note that the DLTA chunks are not interleaved bitmap representations,
  3132.       thus the use of the ILBM form is inappropriate for these frames.  
  3133.       However, this inconsistency was not noted until there were a number
  3134.       of commercial products either released or close to release which
  3135.       generated/played this format.  Therefore, this is probably an
  3136.       inconsistency which will have to stay with us.
  3137.  
  3138.    1.2 Recording ANIMs
  3139.  
  3140.       To record an ANIM will require three bitmaps - one for creation of 
  3141.       the next frame, and two more for a "history" of the previous two 
  3142.       frames for performing the compression calculations (e.g. the delta 
  3143.       mode calculations).
  3144.       
  3145.       There are five frame-to-frame compression methods currently defined.  
  3146.       The first three are mainly for historical interest.  The product Aegis 
  3147.       VideoScape 3D utilizes the third method in version 1.0, but switched 
  3148.       to method 5 on 2.0.  This is the only instance known of a commercial 
  3149.       product generating ANIMs of any of the first three methods.  The 
  3150.       fourth method is a general short or long word compression scheme which 
  3151.       has several options including whether the compression is horizontal
  3152.       or vertical, and whether or not it is XOR format.  This offers a 
  3153.       choice to the user for the optimization of file size and/or playback 
  3154.       speed.  The fifth method is the byte vertical run length encoding as 
  3155.       designed by Jim Kent.  Do not confuse this with Jim's RIFF file format 
  3156.       which is different than ANIM.  Here we utilized his compression/
  3157.       decompression routines within the ANIM file structure.
  3158.       
  3159.       The following paragraphs give a general outline of each of the
  3160.       methods of compression currently included in this spec.
  3161.  
  3162.       1.2.1 XOR mode
  3163.          
  3164.          This mode is the original and is included here for historical
  3165.          interest.  In general, the delta modes are far superior.
  3166.          The creation of XOR mode is quite simple.  One simply
  3167.          performs an exclusive-or (XOR) between all corresponding
  3168.          bytes of the new frame and two frames back.  This results
  3169.          in a new bitmap with 0 bits wherever the two frames were
  3170.          identical, and 1 bits where they are different.  Then this
  3171.          new bitmap is saved using run-length-encoding.  A major
  3172.          obstacle of this mode is in the time consumed in performing
  3173.          the XOR upon reconstructing the image.
  3174.          
  3175.       1.2.2 Long Delta mode
  3176.          
  3177.          This mode stores the actual new frame long-words which are
  3178.          different, along with the offset in the bitmap.  The
  3179.          exact format is shown and discussed in section 2 below.
  3180.          Each plane is handled separately, with no data being saved
  3181.          if no changes take place in a given plane.  Strings of
  3182.          2 or more long-words in a row which change can be run
  3183.          together so offsets do not have to be saved for each one.
  3184.          
  3185.          Constructing this data chunk usually consists of having a buffer 
  3186.          to hold the data, and calculating the data as one compares the 
  3187.          new frame, long-word by long-word, with two frames back.
  3188.          
  3189.       1.2.3 Short Delta mode
  3190.          
  3191.          This mode is identical to the Long Delta mode except that
  3192.          short-words are saved instead of long-words.  In most
  3193.          instances, this mode results in a smaller DLTA chunk.
  3194.          The Long Delta mode is mainly of interest in improving
  3195.          the playback speed when used on a 32-bit 68020 Turbo Amiga.
  3196.          
  3197.       1.2.4 General Delta mode
  3198.  
  3199.          The above two delta compression modes were hastily put together.
  3200.          This mode was an attempt to provide a well-thought-out delta
  3201.          compression scheme.  Options provide for both short and long
  3202.          word compression, either vertical or horizontal compression,
  3203.          XOR mode (which permits reverse playback), etc.  About the time
  3204.          this was being finalized, the fifth mode, below, was developed
  3205.          by Jim Kent.  In practice the short-vertical-run-length-encoded
  3206.          deltas in this mode play back faster than the fifth mode (which
  3207.          is in essence a byte-vertical-run-length-encoded delta mode) but
  3208.          does not compress as well - especially for very noisy data such
  3209.          as digitized images.  In most cases, playback speed not being
  3210.          terrifically slower, the better compression (sometimes 2x) is
  3211.          preferable due to limited storage media in most machines.
  3212.  
  3213.          Details on this method are contained in section 2.2.2 below.
  3214.  
  3215.       1.2.5 Byte Vertical Compression
  3216.  
  3217.          This method does not offer the many options that method 4 offers,
  3218.          but is very successful at producing decent compression even for
  3219.          very noisy data such as digitized images.  The method was devised
  3220.          by Jim Kent and is utilized in his RIFF file format which is 
  3221.          different than the ANIM format.  The description of this method
  3222.          in this document is taken from Jim's writings.  Further, he has
  3223.          released both compression and decompression code to public domain.
  3224.          
  3225.          Details on this method are contained in section 2.2.3 below.
  3226.  
  3227.    1.3 Playing ANIMs
  3228.       
  3229.       Playback of ANIMs will usually require two buffers, as mentioned
  3230.       above, and double-buffering between them.  The frame data from
  3231.       the ANIM file is used to modify the hidden frame to the next
  3232.       frame to be shown.  When using the XOR mode, the usual run-
  3233.       length-decoding routine can be easily modified to do the 
  3234.       exclusive-or operation required.  Note that runs of zero bytes,
  3235.       which will be very common, can be ignored, as an exclusive or
  3236.       of any byte value to a byte of zero will not alter the original
  3237.       byte value.
  3238.       
  3239.       The general procedure, for all compression techniques, is to first
  3240.       decode the initial ILBM picture into the hidden buffer and double-
  3241.       buffer it into view.  Then this picture is copied to the other (now
  3242.       hidden) buffer.  At this point each frame is displayed with the
  3243.       same procedure.  The next frame is formed in the hidden buffer by
  3244.       applying the DLTA data (or the XOR data from the BODY chunk in the
  3245.       case of the first XOR method) and the new frame is double-buffered
  3246.       into view.  This process continues to the end of the file.
  3247.  
  3248.       A master colormap should be kept for the entire ANIM which would
  3249.       be initially set from the CMAP chunk in the initial ILBM.  This
  3250.       colormap should be used for each frame.  If a CMAP chunk appears
  3251.       in one of the frames, then this master colormap is updated and the
  3252.       new colormap applies to all frames until the occurrance of another
  3253.       CMAP chunk.
  3254.  
  3255.       Looping ANIMs may be constructed by simply making the last two frames
  3256.       identical to the first two.  Since the first two frames are special
  3257.       cases (the first being a normal ILBM and the second being a delta from
  3258.       the first) one can continually loop the anim by repeating from frame
  3259.       three.  In this case the delta for creating frame three will modify
  3260.       the next to the last frame which is in the hidden buffer (which is
  3261.       identical to the first frame), and the delta for creating frame four
  3262.       will modify the last frame which is identical to the second frame.
  3263.  
  3264.       Multi-File ANIMs are also supported so long as the first two frames
  3265.       of a subsequent file are identical to the last two frames of the
  3266.       preceeding file.  Upon reading subsequent files, the ILBMs for the
  3267.       first two frames are simply ignored, and the remaining frames are
  3268.       simply appended to the preceeding frames.  This permits splitting
  3269.       ANIMs across multiple floppies and also permits playing each section
  3270.       independently and/or editing it independent of the rest of the ANIM.
  3271.       
  3272.       Timing of ANIM playback is easily achieved using the vertical blank
  3273.       interrupt of the Amiga.  There is an example of setting up such
  3274.       a timer in the ROM Kernel Manual.  Be sure to remember the timer
  3275.       value when a frame is flipped up, so the next frame can be flipped
  3276.       up relative to that time.  This will make the playback independent
  3277.       of how long it takes to decompress a frame (so long as there is enough
  3278.       time between frames to accomplish this decompression).
  3279.  
  3280. 2.0 Chunk Formats
  3281.    2.1 ANHD Chunk
  3282.       The ANHD chunk consists of the following data structure:
  3283.       
  3284.            UBYTE operation  The compression method:
  3285.                             =0 set directly (normal ILBM BODY),
  3286.                             =1 XOR ILBM mode,
  3287.                             =2 Long Delta mode,
  3288.                             =3 Short Delta mode,
  3289.                             =4 Generalized short/long Delta mode,
  3290.                             =5 Byte Vertical Delta mode
  3291.                             =6 Stereo op 5 (third party)
  3292.                             =74 (ascii 'J') reserved for Eric Graham's
  3293.                                compression technique (details to be
  3294.                                released later).
  3295.  
  3296.            UBYTE mask      (XOR mode only - plane mask where each
  3297.                             bit is set =1 if there is data and =0
  3298.                             if not.)
  3299.            UWORD w,h       (XOR mode only - width and height of the
  3300.                             area represented by the BODY to eliminate
  3301.                             unnecessary un-changed data)
  3302.            WORD  x,y       (XOR mode only - position of rectangular
  3303.                             area representd by the BODY)
  3304.            ULONG abstime   (currently unused - timing for a frame
  3305.                             relative to the time the first frame
  3306.                             was displayed - in jiffies (1/60 sec))
  3307.            ULONG reltime   (timing for frame relative to time
  3308.                             previous frame was displayed - in
  3309.                             jiffies (1/60 sec))
  3310.            UBYTE interleave (unused so far - indicates how may frames
  3311.                              back this data is to modify.  =0 defaults
  3312.                              to indicate two frames back (for double
  3313.                              buffering). =n indicates n frames back.
  3314.                              The main intent here is to allow values
  3315.                              of =1 for special applications where
  3316.                              frame data would modify the immediately
  3317.                              previous frame)
  3318.            UBYTE pad0        Pad byte, not used at present.
  3319.            ULONG bits        32 option bits used by options=4 and 5.
  3320.                              At present only 6 are identified, but the
  3321.                              rest are set =0 so they can be used to
  3322.                              implement future ideas.  These are defined
  3323.                              for option 4 only at this point.  It is
  3324.                              recommended that all bits be set =0 for
  3325.                              option 5 and that any bit settings used in
  3326.                              the future (such as for XOR mode) be compatible 
  3327.                              with the option 4 bit settings.   Player code 
  3328.                              should check undefined bits in options 4 and 5
  3329.                              to assure they are zero.
  3330.  
  3331.                              The six bits for current use are:
  3332.  
  3333.                              bit #              set =0               set =1
  3334.                              ===============================================
  3335.                              0              short data           long data
  3336.                              1                 set                  XOR
  3337.                              2             separate info        one info list
  3338.                                            for each plane       for all planes
  3339.                              3               not RLC        RLC (run length coded)
  3340.                              4              horizontal           vertical
  3341.                              5           short info offsets   long info offsets
  3342.  
  3343.            UBYTE pad[16]     This is a pad for future use for future
  3344.                              compression modes.
  3345.       
  3346.    2.2 DLTA Chunk
  3347.       
  3348.       This chunk is the basic data chunk used to hold delta compression
  3349.       data.  The format of the data will be dependent upon the exact
  3350.       compression format selected.  At present there are two basic
  3351.       formats for the overall structure of this chunk.
  3352.  
  3353.       2.2.1 Format for methods 2 & 3
  3354.  
  3355.          This chunk is a basic data chunk used to hold the delta
  3356.          compression data.  The minimum size of this chunk is 32 bytes
  3357.          as the first 8 long-words are byte pointers into the chunk for
  3358.          the data for each of up to 8 bitplanes.  The pointer for the
  3359.          plane data starting immediately following these 8 pointers will
  3360.          have a value of 32 as the data starts in the 33-rd byte of the
  3361.          chunk (index value of 32 due to zero-base indexing).
  3362.       
  3363.          The data for a given plane consists of groups of data words.  In
  3364.          Long Delta mode, these groups consist of both short and long
  3365.          words - short words for offsets and numbers, and long words for
  3366.          the actual data.  In Short Delta mode, the groups are identical
  3367.          except data words are also shorts so all data is short words.
  3368.          Each group consists of a starting word which is an offset.  If
  3369.          the offset is positive then it indicates the increment in long
  3370.          or short words (whichever is appropriate) through the bitplane.
  3371.          In other words, if you were reconstructing the plane, you would
  3372.          start a pointer (to shorts or longs depending on the mode) to
  3373.          point to the first word of the bitplane.  Then the offset would
  3374.          be added to it and the following data word would be placed at
  3375.          that position.  Then the next offset would be added to the
  3376.          pointer and the following data word would be placed at that
  3377.          position.  And so on...  The data terminates with an offset
  3378.          equal to 0xFFFF.
  3379.       
  3380.          A second interpretation is given if the offset is negative.  In
  3381.          that case, the absolute value is the offset+2.  Then the 
  3382.          following short-word indicates the number of data words that
  3383.          follow.  Following that is the indicated number of contiguous
  3384.          data words (longs or shorts depending on mode) which are to
  3385.          be placed in contiguous locations of the bitplane.
  3386.       
  3387.          If there are no changed words in a given plane, then the pointer
  3388.          in the first 32 bytes of the chunk is =0.
  3389.       
  3390.       2.2.2 Format for method 4
  3391.          
  3392.          The DLTA chunk is modified slightly to have 16 long pointers at
  3393.          the start.  The first 8 are as before - pointers to the start of
  3394.          the data for each of the bitplanes (up to a theoretical max of 8
  3395.          planes).  The next 8 are pointers to the start of the offset/numbers
  3396.          data list.  If there is only one list of offset/numbers for all
  3397.          planes, then the pointer to that list is repeated in all positions
  3398.          so the playback code need not even be aware of it.  In fact, one
  3399.          could get fancy and have some bitplanes share lists while others
  3400.          have different lists, or no lists (the problems in these schemes
  3401.          lie in the generation, not in the playback).
  3402.  
  3403.          The best way to show the use of this format is in a sample playback
  3404.          routine.
  3405.  
  3406.             SetDLTAshort(bm,deltaword)
  3407.             struct BitMap *bm;
  3408.             WORD *deltaword;
  3409.             {
  3410.                int i;
  3411.                LONG *deltadata;
  3412.                WORD *ptr,*planeptr;
  3413.                register int s,size,nw;
  3414.                register WORD *data,*dest;
  3415.  
  3416.                deltadata = (LONG *)deltaword;
  3417.                nw = bm->BytesPerRow >>1;
  3418.  
  3419.                for (i=0;i<bm->Depth;i++) {
  3420.                   planeptr = (WORD *)(bm->Planes[i]);
  3421.                   data = deltaword + deltadata[i];
  3422.                   ptr  = deltaword + deltadata[i+8];
  3423.                   while (*ptr != 0xFFFF) {
  3424.                      dest = planeptr + *ptr++;
  3425.                      size = *ptr++;
  3426.                      if (size < 0) {
  3427.                         for (s=size;s<0;s++) {
  3428.                            *dest = *data;
  3429.                            dest += nw;
  3430.                         }
  3431.                         data++;
  3432.                      }
  3433.                      else {
  3434.                         for (s=0;s<size;s++) {
  3435.                            *dest = *data++;
  3436.                            dest += nw;
  3437.                         }
  3438.                      }
  3439.                   }
  3440.                }
  3441.                return(0);
  3442.             }
  3443.  
  3444.          The above routine is for short word vertical compression with
  3445.          run length compression.  The most efficient way to support 
  3446.          the various options is to replicate this routine and make 
  3447.          alterations for, say, long word or XOR.  The variable nw
  3448.          indicates the number of words to skip to go down the vertical
  3449.          column.  This one routine could easily handle horizontal
  3450.          compression by simply setting nw=1.  For ultimate playback
  3451.          speed, the core, at least, of this routine should be coded in
  3452.          assembly language.
  3453.  
  3454.       2.2.2 Format for method 5
  3455.  
  3456.          In this method the same 16 pointers are used as in option 4.
  3457.          The first 8 are pointers to the data for up to 8 planes.
  3458.          The second set of 8 are not used but were retained for several
  3459.          reasons.  First to be somewhat compatible with code for option
  3460.          4 (although this has not proven to be of any benefit) and 
  3461.          second, to allow extending the format for more bitplanes (code
  3462.          has been written for up to 12 planes).  
  3463.  
  3464.          Compression/decompression is performed on a plane-by-plane basis.
  3465.          For each plane, compression can be handled by the skip.c code
  3466.          (provided Public Domain by Jim Kent) and decompression can be
  3467.          handled by unvscomp.asm (also provided Public Domain by Jim Kent).
  3468.          
  3469.          Compression/decompression is performed on a plane-by-plane basis.
  3470.          The following description of the method is taken directly from
  3471.          Jim Kent's code with minor re-wording.  Please refer to Jim's
  3472.          code (skip.c and unvscomp.asm) for more details:
  3473.  
  3474.             Each column of the bitplane is compressed separately.
  3475.             A 320x200 bitplane would have 40 columns of 200 bytes each.
  3476.             Each column starts with an op-count followed by a number
  3477.             of ops.  If the op-count is zero, that's ok, it just means
  3478.             there's no change in this column from the last frame.
  3479.             The ops are of three classes, and followed by a varying
  3480.             amount of data depending on which class:
  3481.               1. Skip ops - this is a byte with the hi bit clear that
  3482.                  says how many rows to move the "dest" pointer forward,
  3483.                  ie to skip. It is non-zero.
  3484.               2. Uniq ops - this is a byte with the hi bit set.  The hi
  3485.                  bit is masked down and the remainder is a count of the
  3486.                  number of bytes of data to copy literally.  It's of
  3487.                  course followed by the data to copy.
  3488.               3. Same ops - this is a 0 byte followed by a count byte,
  3489.                  followed by a byte value to repeat count times.
  3490.             Do bear in mind that the data is compressed vertically rather
  3491.             than horizontally, so to get to the next byte in the destination
  3492.             we add the number of bytes per row instead of one!
  3493.  
  3494.  
  3495. ANIM brush format
  3496.  
  3497.                     Dpaint Anim Brush IFF Format
  3498.  
  3499.              From a description by the author of DPaint,
  3500.                       Dan Silva, Electronic Arts
  3501.  
  3502.  
  3503. The "Anim Brushes" of DPaint III are saved on disk in the IFF "ANIM" format.  
  3504. Basically, an ANIM Form consists of an initial ILBM which is the first frame 
  3505. of the animation, and any number of subsequent "ILBM"S (which aren't really 
  3506. ILBM's) each of which contains an ANHD animation header chunk and a DLTA chunk  
  3507. comprised of the encoded difference between a frame and a previous one.
  3508.  
  3509. To use ANIM terminology (for a description of the ANIM format, see the IFF 
  3510. Anim Spec, by Gary Bonham). Anim Brushes use a "type 5" encoding, which is 
  3511. a vertical, byte-oriented delta encoding (based on Jim Kent's RIFF).  The 
  3512. deltas have an interleave of 1, meaning deltas are computed between adjacent 
  3513. frames, rather than between frames 2 apart, which is the usual ANIM custom 
  3514. for the purpose of fast hardware page-flipping.  Also, the deltas use 
  3515. Exclusive Or to allow reversable play.
  3516.  
  3517. However, to my knowledge, all the existing Anim players in the Amiga world 
  3518. will only play type 5 "Anim"s which have an interleave of 0 (i.e. 2) and 
  3519. which use a Store operation rather than Exclusive Or, so no existing programs 
  3520. will read Anim Brushes anyway.  The job of modifying existing Anim readers 
  3521. to read Anim Brushes should be simplified, however.
  3522.  
  3523.  
  3524. Here is an outline of the structure of the IFF Form output by DPaint III as 
  3525. an "Anim Brush".  The IFF Reader should of course be flexible enough to 
  3526. tolerate variation in what chunks actually appear in the initial ILBM.
  3527.  
  3528.                   FORM ANIM
  3529.                       . FORM ILBM         first frame
  3530.                       . . BMHD        
  3531.                       . . CMAP
  3532.                       . . DPPS
  3533.                       . . GRAB
  3534.                       . . CRNG
  3535.                       . . CRNG
  3536.                       . . CRNG
  3537.                       . . CRNG
  3538.                       . . CRNG
  3539.                       . . CRNG
  3540.                       . . DPAN     my own little chunk.
  3541.                       . . CAMG
  3542.                       . . BODY
  3543.  
  3544.                       . FORM ILBM         frame 2
  3545.                       . . ANHD                animation header chunk
  3546.                       . . DLTA                delta mode data
  3547.  
  3548.                       . FORM ILBM         frame 3
  3549.                       . . ANHD                animation header chunk
  3550.                       . . DLTA                delta mode data
  3551.  
  3552.                       . FORM ILBM         frame 4
  3553.                       . . ANHD                animation header chunk
  3554.                       . . DLTA                delta mode data
  3555.        ...
  3556.                    . FORM ILBM         frame N
  3557.                       . . ANHD                animation header chunk
  3558.                       . . DLTA                delta mode data
  3559.  
  3560.  
  3561. --- Here is the format of the DPAN chunk:
  3562.  
  3563. typedef struct {
  3564.  UWORD version;   /* current version=4 */
  3565.  UWORD nframes;   /* number of frames in the animation.*/
  3566.  ULONG flags;   /* Not used */
  3567.  } DPAnimChunk;
  3568.  
  3569. The version number was necessary during development. At present all I look 
  3570. at is "nframes".
  3571.  
  3572.  
  3573. --- Here is the ANHD chunk format:
  3574.  
  3575. typedef struct {
  3576.  UBYTE operation;  /* =0  set directly
  3577.        =1  XOR ILBM mode,
  3578.        =2 Long Delta mode,
  3579.        =3 Short Delta mode
  3580.        =4 Generalize short/long Delta mode,
  3581.        =5 Byte Vertical Delta (riff)
  3582.        =74 (Eric Grahams compression mode)
  3583.    */
  3584.  UBYTE mask;      /* XOR ILBM only: plane mask where data is*/
  3585.  UWORD w,h;  
  3586.  WORD x,y;
  3587.  ULONG abstime;
  3588.  ULONG reltime;
  3589.  UBYTE interleave; /* 0 defaults to 2 */
  3590.  UBYTE pad0;   /* not used */
  3591.  ULONG bits;   /* meaning of bits:
  3592.     bit# =0                 =1
  3593.     0    short data         long data
  3594.     1    store              XOR
  3595.     2    separate info      one info for
  3596.          for each plane     for all planes
  3597.     3    not RLC            RLC (run length encoded)
  3598.     4    horizontal         vertical
  3599.     5    short info offsets long info offsets
  3600.    -------------------------*/
  3601.  UBYTE pad[16];
  3602.  } AnimHdr;
  3603.  
  3604.  
  3605. for Anim Brushes, I set:
  3606.  
  3607.  animHdr.operation = 5;  /* RIFF encoding */
  3608.  animHdr.interleave = 1;
  3609.  animHdr.w = curAnimBr.bmob.pict.box.w; 
  3610.  animHdr.h = curAnimBr.bmob.pict.box.h; 
  3611.  animHdr.reltime = 1;
  3612.  animHdr.abstime = 0;
  3613.  animHdr.bits = 4; /* indicating XOR */
  3614.  
  3615. -- everything else is set to 0.
  3616.  
  3617. NOTE: the "bits" field was actually intended ( by the original creator of 
  3618. the ANIM format, Gary Bonham of SPARTA, Inc.) for use with only with 
  3619. compression method 4. I am using bit 2 of the bits field to indicate the 
  3620. Exclusive OR operation in the context of method 5, which seems like a 
  3621. reasonable generalization. 
  3622.  
  3623.  
  3624. For an Anim Brush with 10 frames, there will be an initial frame followed 
  3625. by 10 Delta's (i.e ILBMS containing ANHD and DLTA chunks).  Applying the 
  3626. first Delta to the initial frame generates the second frame, applying the 
  3627. second Delta to the second frame generates the third frame, etc.  Applying 
  3628. the last Delta thus brings back the first frame.  
  3629.  
  3630. The DLTA chunk begins with 16 LONG plane offets, of which DPaint only uses 
  3631. the first 6 (at most).  These plane offsets are either the offset (in bytes) 
  3632. from the beginning of the DLTA chunk to the data for the corresponding plane, 
  3633. or Zero, if there was no change in that plane.  Thus the first plane offset 
  3634. is either 0 or 64.
  3635.  
  3636.  
  3637. (The following description of the method is based on Gary Bonham's rewording 
  3638. of Jim Kent's RIFF documentation.)
  3639.  
  3640.   Compression/decompression is performed on a plane-by-plane 
  3641.   basis.  
  3642.  
  3643.   Each byte-column of the bitplane is compressed separately.  A 
  3644.   320x200 bitplane would have 40 columns of 200 bytes each.  In 
  3645.   general, the bitplanes are always an even number of bytes wide, 
  3646.   so for instance a 17x20 bitplane would have 4 columns of 20 
  3647.   bytes each.
  3648.  
  3649.   Each column starts with an op-count followed by a number of 
  3650.   ops.  If the op-count is zero, that's ok, it just means there's 
  3651.   no change in this column from the last frame.  The ops are of 
  3652.   three kinds, and followed by a varying amount of data depending 
  3653.   on which kind:
  3654.  
  3655.      1. SKIP - this is a byte with the hi bit clear that   says 
  3656.         how many rows to move the "dest" pointer forward, ie to 
  3657.         skip. It is non-zero.
  3658.  
  3659.      2. DUMP - this is a byte with the hi bit set.  The hi bit is 
  3660.         masked off and the remainder is a count of the number of 
  3661.         bytes of data to XOR directly.  It is followed by the 
  3662.         bytes to copy.
  3663.  
  3664.      3. RUN - this is a 0 byte followed by a count byte, followed 
  3665.         by a byte value to repeat "count" times, XOR'ing it into 
  3666.         the destination.
  3667.  
  3668.   Bear in mind that the data is compressed vertically rather than 
  3669.   horizontally, so to get to the next byte in the destination  you 
  3670.   add the number of bytes per row instead of one.
  3671.  
  3672. The Format of DLTA chunks is as described in section 2.2.2 of the Anim Spec. 
  3673. The encoding for type 5 is described in section 2.2.3 of the Anim Spec.  
  3674.  
  3675.  
  3676.  
  3677.  
  3678.  
  3679. 2-D Object standard format
  3680.  
  3681. FORM DR2D
  3682.  
  3683. Description by Ross Cunniff and John Orr
  3684.  
  3685.  
  3686. A standard IFF FORM to describe 2D drawings has been sorely needed for
  3687. a long time.  Several commercial drawing packages have been available
  3688. for some time but none has established its file format as the Amiga
  3689. standard.  The absence of a 2D drawing standard hinders the
  3690. development of applications that use 2D drawings as it forces each
  3691. application to understand several private standards instead of a
  3692. single one.  Without a standard, data exchange for both the developer
  3693. and the user is difficult, if not impossible.
  3694.  
  3695. The DR2D FORM fills this void.  This FORM was developed by Taliesin,
  3696. Inc. for use as the native file format for their two-dimensional
  3697. structured drawing package, ProVector.  Saxon Industries and Soft
  3698. Logik Publishing Corporation are planning to support this new FORM in
  3699. the near future.
  3700.  
  3701. Many of the values stored in the DR2D FORM are stored as IEEE single
  3702. precision floating point numbers.  These numbers consist of 32 bits,
  3703. arranged as follows:
  3704.  
  3705.  _______________________________________________________________________
  3706. | s e e e e e e e | e m m m m m m m | m m m m m m m m | m m m m m m m m |
  3707.  -----------------------------------------------------------------------
  3708.   31            24  23            16  15            8   7             0
  3709.  
  3710.  
  3711. where: 
  3712.  
  3713. sis the sign of the number where 1 is negative and 0 is
  3714.   positive. 
  3715. eis the 8 bit exponent in excess 127 form.  This number
  3716.   is the power of two to which the mantissa is raised
  3717.   (Excess 127 form means that 127 is added to the
  3718.   exponent before packing it into the IEEE number.) 
  3719. mis the 23 bit mantissa.  It ranges from 1.0000000 to
  3720.   1.999999..., where the leading base-ten one is
  3721.   assumed. 
  3722.  
  3723. An IEEE single precision with the value of 0.0000000 has all its bits
  3724. cleared. 
  3725.  
  3726.  
  3727.  
  3728.  
  3729.  
  3730.  
  3731. The DR2D Chunks
  3732.  
  3733.  
  3734. FORM (0x464F524D)  /* All drawings are a FORM */
  3735.  
  3736. struct FORMstruct {
  3737.     ULONGID;    /* DR2D */
  3738.     ULONGSize;
  3739. };
  3740.  
  3741.  
  3742. DR2D (0x44523244)  /* ID of 2D drawing */
  3743.  
  3744.  
  3745. The DR2D chunks are broken up into three groups: the global drawing
  3746. attribute chunks, the object attribute chunks, and the object chunks.
  3747. The global drawing attribute chunks describe elements of a 2D drawing
  3748. that are common to many objects in the drawing.  Document preferences,
  3749. palette information, and custom fill patterns are typical
  3750. document-wide settings defined in global drawing attribute chunks.
  3751. The object attribute chunks are used to set certain properties of the
  3752. object chunk(s) that follows the object attribute chunk.  The current
  3753. fill pattern, dash pattern, and line color are all set using an object
  3754. attribute chunk.  Object chunks describe the actual DR2D drawing.
  3755. Polygons, text, and bitmaps are found in these chunks.
  3756.  
  3757.  
  3758.  
  3759. The Global Drawing Attribute Chunks
  3760.  
  3761. The following chunks describe global attributes of a DR2D document.
  3762.  
  3763. DRHD (0x44524844) /* Drawing header */
  3764.  
  3765. The DRHD chunk contains the upper left and lower right extremes of the
  3766. document in (X, Y) coordinates.  This chunk is required and should
  3767. only appear once in a document in the outermost layer of the DR2D file
  3768. (DR2Ds can be nested).
  3769.  
  3770. struct DRHDstruct {
  3771.     ULONGID;
  3772.     ULONGSize;    /* Always 16 */
  3773.     IEEEXLeft, YTop,
  3774. XRight, YBot;
  3775. };
  3776.  
  3777.  
  3778. The point (XLeft,YTop) is the upper left corner of the project and the
  3779. point (XRight,YBot) is its lower right corner.  These coordinates not
  3780. only supply the size and position of the document in a coordinate
  3781. system, they also supply the project's orientation.  If XLeft <
  3782. XRight, the X-axis increases toward the right.  If YTop < YBot, the
  3783. Y-axis increases toward the bottom.  Other combinations are possible;
  3784. for example in Cartesian coordinates, XLeft would be less than XRight
  3785. but YTop would be greater than YBot.
  3786.  
  3787.  
  3788.  
  3789.  
  3790.  
  3791. PPRF (0x50505249)   /* Page preferences */
  3792.  
  3793. The PPRF chunk contains preference settings for ProVector.  Although
  3794. this chunk is not required, its use is encouraged because it contains
  3795. some important environment information.
  3796.  
  3797. struct PPRFstruct {
  3798.     ULONGID;
  3799.     ULONGSize;
  3800.     charPrefs[Size];
  3801. };
  3802.  
  3803. DR2D stores preferences as a concatenation of several null-terminated
  3804. strings, in the Prefs[] array.  The strings can appear in any order.
  3805. The currently supported strings are:
  3806.  
  3807. Units=<unit-type>
  3808. Portrait=<boolean>
  3809. PageType=<page-type>
  3810. GridSize=<number>
  3811.  
  3812. where:
  3813. <unit-type> is either Inch, Cm, or Pica
  3814. <boolean>   is either True or False
  3815. <page-type> is either Standard, Legal, B4, B5, A3,
  3816.   A4, A5, or Custom 
  3817. <number>    is a floating-point number
  3818.  
  3819. The DR2D FORM does not require this chunk to explicitly state all the
  3820. possible preferences.  In the absence of any particular preference
  3821. string, a DR2D reader should fall back on the default value.  The
  3822. defaults are:
  3823.  
  3824. Units=Inch
  3825. Portrait=True
  3826. PageType=Standard
  3827. GridSize=1.0
  3828.  
  3829.  
  3830. CMAP (0x434D4150)   /* Color map (Same as ILBM CMAP) */
  3831.  
  3832. This chunk is identical to the ILBM CMAP chunk as described in the IFF
  3833. ILBM documentation.
  3834.  
  3835. struct CMAPstruct {
  3836.     ULONGID;
  3837.     ULONGSize;
  3838.     UBYTEColorMap[Size];
  3839. };
  3840.  
  3841. ColorMap is an array of 24-bit RGB color values.  The 24-bit value is
  3842. spread across three bytes, the first of which contains the red
  3843. intensity, the next contains the green intensity, and the third
  3844. contains the blue intensity.  Because DR2D stores its colors with
  3845. 24-bit accuracy, DR2D readers must not make the mistake that some ILBM
  3846. readers do in assuming the CMAP chunk colors correspond directly to
  3847. Amiga color registers.
  3848.  
  3849.  
  3850.  
  3851. FONS (0x464F4E53)   /* Font chunk (Same as FTXT FONS chunk) */
  3852.  
  3853. The FONS chunk contains information about a font used in the DR2D
  3854. FORM.  ProVector does not include support for Amiga fonts.  Instead,
  3855. ProVector uses fonts defined in the OFNT FORM which is documented
  3856. later in this article.
  3857.  
  3858. struct FONSstruct {
  3859.     ULONGID;
  3860.     ULONGSize;
  3861.     UBYTEFontID;/* ID the font is referenced by */
  3862.     UBYTEPad1;          /* Always 0 */
  3863.     UBYTEProportional;/* Is it proportional? */
  3864.     UBYTESerif;/* does it have serifs? */
  3865.     CHARName[Size-4];/* The name of the font */
  3866. };
  3867.  
  3868. The UBYTE FontID field is the number DR2D assigns to this font.
  3869. References to this font by other DR2D chunks are made using this
  3870. number.    
  3871.  
  3872. The Proportional and Serif fields indicate properties of this font.
  3873. Specifically, Proportional indicates if this font is proportional, and
  3874. Serif indicates if this font has serifs.  These two options were
  3875. created to allow for font substitution in case the specified font is
  3876. not available.  They are set according to these values:
  3877.  
  3878. 0The DR2D writer didn't know if this font is
  3879.   proportional/has serifs. 
  3880. 1No, this font is not proportional/does not have
  3881.   serifs. 
  3882. 2Yes, this font is proportional/does have serifs.
  3883.  
  3884. The last field, Name[], is a NULL terminated string containing the
  3885. name of the font. 
  3886.  
  3887.  
  3888. DASH (0x44415348)   /* Line dash pattern for edges */
  3889.  
  3890. This chunk describes the on-off dash pattern associated with a line.
  3891.  
  3892. struct DASHstruct {
  3893.     ULONGID;
  3894.     ULONGSize;
  3895.     USHORTDashID;/* ID of the dash pattern */
  3896.     USHORTNumDashes; /* Should always be even */
  3897.     IEEEDashes[NumDashes];  /* On-off pattern */
  3898. };
  3899.  
  3900. DashID is the number assigned to this specific dash pattern.
  3901. References to this dash pattern by other DR2D chunks are made using
  3902. this number.
  3903.  
  3904. The Dashes[] array contains the actual dash pattern.  The first number
  3905. in the array (element 0) is the length of the ``on'' portion of the
  3906. pattern.  The second number (element 1) specifies the ``off'' portion
  3907. of the pattern.  If there are more entries in the Dashes array, the
  3908. pattern will continue.  Even-index elements specify the length of an
  3909. ``on'' span, while odd-index elements specify the length of an ``off''
  3910. span.  There must be an even number of entries.  These lengths are not
  3911. in the same units as specified in the PPRF chunk, but are multiples of
  3912. the line width, so a line of width 2.5 and a dash pattern of 1.0, 2.0
  3913. would have an ``on'' span of length 1.0 x 2.5 = 2.5 followed by an
  3914. ``off'' span of length 2.0 x 2.5 = 5.  The following figure shows
  3915. several dash pattern examples.  Notice that for lines longer than the
  3916. dash pattern, the pattern repeats.
  3917.  
  3918.  
  3919. [figure 1 - dash patterns]
  3920.  
  3921. By convention, DashID 0 is reserved to mean `No line pattern at all',
  3922. i.e. the edges are invisible.  This DASH pattern should not be defined
  3923. by a DR2D DASH chunk.  Again by convention, a NumDashes of 0 means
  3924. that the line is solid.
  3925.  
  3926.  
  3927. AROW (0x41524F57)   /* An arrow-head pattern */
  3928.  
  3929. The AROW chunk describes an arrowhead pattern.  DR2D open polygons
  3930. (OPLY) can have arrowheads attached to their endpoints.  See the
  3931. description of the OPLY chunk later in this article for more
  3932. information on the OPLY chunk.
  3933.  
  3934.  
  3935. #define ARROW_FIRST  0x01 /* Draw an arrow on the OPLY's first point */
  3936. #define ARROW_LAST   0x02 /* Draw an arrow on the OPLY's last point */
  3937.  
  3938. struct AROWstruct {
  3939.     ULONGID;
  3940.     ULONGSize;
  3941.     UBYTEFlags;    /* Flags, from ARROW_*, above */
  3942.     UBYTEPad0;/* Should always 0 */
  3943.     USHORTArrowID;/* Name of the arrow head */
  3944.     USHORTNumPoints;
  3945.     IEEEArrowPoints[NumPoints*2];
  3946. };
  3947.  
  3948.  
  3949. The Flags field specifies which end(s) of an OPLY to place an
  3950. arrowhead based on the #defines above.  ArrowID is the number by which
  3951. an OPLY will reference this arrowhead pattern.
  3952.  
  3953. The coordinates in the array ArrowPoints[] define the arrowhead's
  3954. shape.  These points form a closed polygon.  See the section on the
  3955. OPLY/CPLY object chunks for a descriptionof how DR2D defines shapes.
  3956. The arrowhead is drawn in the same coordinate system relative to the
  3957. endpoint of the OPLY the arrowhead is attached to.  The arrowhead's
  3958. origin (0,0) coincides with the OPLY's endpoint.  DR2D assumes that
  3959. the arrowhead represented in the AROW chunk is pointing to the right
  3960. so the proper rotation can be applied to the arrowhead.  The arrow is
  3961. filled according to the current fill pattern set in the ATTR object
  3962. attribute chunk.
  3963.  
  3964.  
  3965.  
  3966. FILL (0x46494C4C)   /* Object-oriented fill pattern */
  3967.  
  3968. The FILL chunk defines a fill pattern.  This chunk is only valid
  3969. inside nested DR2D FORMs.  The GRUP object chunk section of this
  3970. article contans an example of the FILL chunk.
  3971.  
  3972. struct FILLstruct {
  3973.     ULONGID;
  3974.     ULONGSize;
  3975.     USHORTFillID;  /* ID of the fill */
  3976. };
  3977.  
  3978. FillID is the number by which the ATTR object attribute chunk
  3979. references fill patterns.  The FILL chunk must be the first chunk
  3980. inside a nested DR2D FORM.  A FILL is followed by one DR2D object plus
  3981. any of the object attribute chunks (ATTR, BBOX) associated with the
  3982. object.
  3983.  
  3984. [Figure 2 - fill patterns]
  3985.  
  3986.  
  3987. DR2D makes a ``tile'' out of the fill pattern, giving it a virtual
  3988. bounding box based on the extreme X and Y values of the FILL's object
  3989. (Fig. A).  The bounding box shown in Fig. A surrounding the pattern
  3990. (the two ellipses) is invisible to the user.  In concept, this
  3991. rectangle is pasted on the page left to right, top to bottom like
  3992. floor tiles (Fig. B).  Again, the bounding boxes are not visible.  The
  3993. only portion of this tiled pattern that is visible is the part that
  3994. overlaps the object (Fig. C) being filled.  The object's path is
  3995. called a clipping path, as it ``clips'' its shape from the tiled
  3996. pattern (Fig. D).  Note that the fill is only masked on top of
  3997. underlying objects, so any ``holes'' in the pattern will act as a
  3998. window, leaving visible underlying objects.
  3999.  
  4000.  
  4001. LAYR (0x4C415952)   /* Define a layer */
  4002.  
  4003. A DR2D project is broken up into one or more layers.  Each DR2D object
  4004. is in one of these layers.  Layers provide several useful features.
  4005. Any particular layer can be ``turned off'', so that the objects in the
  4006. layer are not displayed.  This eliminates the unnecessary display of
  4007. objects not currently needed on the screen.  Also, the user can lock a
  4008. layer to protect the layer's objects from accidental changes.
  4009.  
  4010. struct LAYRstruct {
  4011.     ULONG    ID;
  4012.     ULONG    Size;
  4013.     USHORT   LayerID;   /* ID of the layer */
  4014.     char     LayerName[16];   /* Null terminated and padded */
  4015.     UBYTE    Flags;   /* Flags, from LF_*, below */
  4016.     UBYTE    Pad0;   /* Always 0 */
  4017. };    
  4018.  
  4019. LayerID is the number assigned to this layer.  As the field's name
  4020. indicates, LayerName[] is the NULL terminated name of the layer.
  4021. Flags is a bit field who's bits are set according to the #defines
  4022. below:
  4023.  
  4024. #define LF_ACTIVE     0x01  /* Active for editing */
  4025. #define LF_DISPLAYED  0x02  /* Displayed on the screen */
  4026.  
  4027. If the LF_ACTIVE bit is set, this layer is unlocked.  A set
  4028. LF_DISPLAYED bit indicates that this layer is currently visible on the
  4029. screen.  A cleared LF_DISPLAYED bit implies that LF_ACTIVE is not set.
  4030. The reason for this is to keep the user from accidentally editing
  4031. layers that are invisible.
  4032.  
  4033.  
  4034. The Object Attribute Chunks
  4035.  
  4036.  
  4037. ATTR (0x41545452)   /* Object attributes */
  4038.  
  4039. The ATTR chunk sets various attributes for the objects that follow it.
  4040. The attributes stay in effect until the next ATTR changes the
  4041. attributes, or the enclosing FORM ends, whichever comes first.
  4042.  
  4043.  
  4044. /* Various fill types */
  4045. #define FT_NONE0    /* No fill*/
  4046. #define FT_COLOR    1    /* Fill with color from palette */
  4047. #define FT_OBJECTS2    /* Fill with tiled objects*/
  4048.  
  4049.  
  4050. struct ATTRstruct {
  4051.     ULONGID;
  4052.     ULONGSize;
  4053.     UBYTEFillType;    /* One of FT_*, above*/
  4054.     UBYTEJoinType;    /* One of JT_*, below*/
  4055.     UBYTEDashPattern; /* ID of edge dash pattern */
  4056.     UBYTEArrowHead;   /* ID of arrowhead to use  */
  4057.     USHORTFillValue;   /* Color or object with which to fill */
  4058.     USHORTEdgeValue;   /* Edge color index*/
  4059.     USHORTWhichLayer;  /* ID of layer it's in*/
  4060.     IEEEEdgeThick;   /* Line width*/
  4061. };
  4062.  
  4063.  
  4064.  
  4065. FillType specifies what kind of fill to use on this ATTR chunk's
  4066. objects.  A value of FT_NONE means that this ATTR chunk's objects are
  4067. not filled.  FT_COLOR indicates that the objects should be filled in
  4068. with a color.  That color's ID (from the CMAP chunk) is stored in the
  4069. FillValue field.  If FillType is equal to FT_OBJECTS, FillValue
  4070. contains the ID of a fill pattern defined in a FILL chunk.
  4071.  
  4072. JoinType determines which style of line join to use when connecting
  4073. the edges of line segments.  The field contains one of these four
  4074. values:
  4075.  
  4076. /* Join types */
  4077. #define JT_NONE   0    /* Don't do line joins */
  4078. #define JT_MITER  1    /* Mitered join */
  4079. #define JT_BEVEL  2    /* Beveled join */
  4080. #define JT_ROUND  3    /* Round join */
  4081.  
  4082. DashPattern and ArrowHead contain the ID of the dash pattern and arrow
  4083. head for this ATTR's objects.  A DashPattern of zero means that there
  4084. is no dash pattern so lines will be invisible.  If ArrowHead is 0,
  4085. OPLYs have no arrow head.  EdgeValue is the color of the line
  4086. segments.  WhichLayer contains the ID of the layer this ATTR's objects
  4087. are in.  EdgeThick is the width of this ATTR's line segments.
  4088.  
  4089.  
  4090.  
  4091. BBOX (0x42424F48)   /* Bounding box of next object in FORM */
  4092.  
  4093.  
  4094. The BBOX chunk supplies the dimensions and position of a bounding box
  4095. surrounding the DR2D object that follows this chunk in the FORM.  A
  4096. BBOX chunk can apply to a FILL or AROW as well as a DR2D object.  The
  4097. BBOX chunk appears just before its DR2D object, FILL, or AROW chunk.
  4098.  
  4099. struct BBOXstruct {
  4100.     ULONGID;
  4101.     ULONGSize;
  4102.     IEEEXMin, YMin,  /* Bounding box of obj. */
  4103. XMax, YMax;  /* including line width*/
  4104. };
  4105.  
  4106. In a Cartesian coordinate system, the point (XMin, YMin) is the
  4107. coordinate of the lower left hand corner of the bounding box and
  4108. (XMax, YMax) is the upper right.  These coordinates take into
  4109. consideration the width of the lines making up the bounding box.
  4110.  
  4111.  
  4112. XTRN (0x5854524E)   /* Externally controlled object */
  4113.  
  4114.  
  4115. The XTRN chunk was created primarily to allow ProVector to link DR2D
  4116. objects to ARexx functions. 
  4117.  
  4118. struct XTRNstruct {
  4119.     ULONG   ID;
  4120.     ULONG   Size;
  4121.     short   ApplCallBacks;             /* From #defines, below */
  4122.     short   ApplNameLength;
  4123.     char    ApplName[ApplNameLength];  /* Name of ARexx func to call */
  4124. };
  4125.  
  4126. ApplName[] contains the name of the ARexx script ProVector calls when
  4127. the user manipulates the object in some way.  The ApplCallBacks field
  4128. specifies the particular action that triggers calling the ARexx script
  4129. according to the #defines listed below.
  4130.  
  4131. /* Flags for ARexx script callbacks */
  4132. #define    X_CLONE     0x0001    /* The object has been cloned */
  4133. #define    X_MOVE      0x0002    /* The object has been moved */
  4134. #define    X_ROTATE    0x0004    /* The object has been rotated */
  4135. #define    X_RESIZE    0x0008    /* The object has been resized */
  4136. #define    X_CHANGE    0x0010    /* An attribute (see ATTR) of the 
  4137.                                     object has changed */
  4138. #define    X_DELETE    0x0020    /* The object has been deleted */
  4139. #define    X_CUT       0x0040    /* The object has been deleted, but
  4140.                                     stored in the clipboard */
  4141. #define    X_COPY      0x0080    /* The object has been copied to the
  4142.                                     clipboard */
  4143. #define    X_UNGROUP   0x0100    /* The object has been ungrouped */
  4144.  
  4145.  
  4146. For example, given the XTRN object:
  4147.  
  4148. FORM xxxx DR2D {
  4149. XTRN xxxx { X_RESIZE | X_MOVE, 10, "Dimension" }
  4150. ATTR xxxx { 0, 0, 1, 0, 0, 0, 0.0 }
  4151. FORM xxxx DR2D {
  4152. GRUP xxxx { 2 }
  4153. STXT xxxx { 0, 0.5, 1.0, 6.0, 5.0, 0.0, 4, "3.0" }
  4154. OPLY xxxx { 2, { 5.5, 5.5, 8.5, 5.5 } }
  4155. }
  4156. }
  4157.  
  4158. ProVector would call the ARexx script named Dimension if the user
  4159. resized or moved this object.  What exactly ProVector sends depends
  4160. upon what the user does to the object.  The following list shows what
  4161. string(s) ProVector sends according to which flag(s) are set.  The
  4162. parameters are described below.
  4163.  
  4164. X_CLONE     ``appl CLONE objID dx dy''
  4165. X_MOVE      ``appl MOVE objID dx dy''
  4166. X_ROTATE    ``appl ROTATE objID cx cy angle''
  4167. X_RESIZE    ``appl RESIZE objID cx cy sx sy''
  4168. X_CHANGE    ``appl CHANGE objID et ev ft fv ew jt fn''
  4169. X_DELETE    ``appl DELETE objID''
  4170. X_CUT       ``appl CUT objID''
  4171. X_COPY      ``appl COPY objID''
  4172. X_UNGROUP   ``appl UNGROUP objID''
  4173.  
  4174. where:
  4175. appl is the name of the ARexx script
  4176. CLONE, MOVE, ROTATE, RESIZE, etc. are literal strings
  4177. objID is the object ID that ProVector assigns to this object
  4178. (dx, dy) is the position offset of the CLONE or MOVE
  4179. (cx, cy) is the point around which the object is rotated or resized
  4180. angle is the angle (in degrees) the object is rotated
  4181. sx and sy are the scaling factors in the horizontal and
  4182.   vertical directions, respectively. 
  4183. et is the edge type (the dash pattern index)
  4184. ev is the edge value (the edge color index)
  4185. ft is the fill type
  4186. fv is the fill index
  4187. ew is the edge weight
  4188. jt is the join type
  4189. fn is the font name
  4190.  
  4191. The X_CHANGE message reflects changes to the attributes found in the
  4192. ATTR chunk. 
  4193.  
  4194. If the user resized the XTRN object shown above by factor of 2,
  4195. ProVector would call the ARexx script Dimension like this: 
  4196.  
  4197. Dimension RESIZE 1985427 7.0 4.75 2.0 2.0
  4198.  
  4199.  
  4200.  
  4201. The Object Chunks
  4202.  
  4203.  
  4204. The following chunks define the objects available in the DR2D FORM.
  4205.  
  4206.  
  4207. VBM  (0x56424D20)   /* Virtual BitMap */
  4208.  
  4209. The VBM chunk contains the position, dimensions, and file name of an
  4210. ILBM image. 
  4211.  
  4212.  
  4213. struct VBMstruct {
  4214.     IEEEXPos, YPos,   /* Virtual coords */
  4215. XSize, YSize, /* Virtual size */
  4216. Rotation;       /* in degrees */
  4217.     USHORTPathLen;        /* Length of dir path */
  4218.     charPath[PathLen];  /* Null-terminated path of file */
  4219. };
  4220.  
  4221.  
  4222. The coordinate (XPos, YPos) is the position of the upper left hand
  4223. corner of the bitmap and the XSize and YSize fields supply the x and y
  4224. dimensions to which the image should be scaled.  Rotation tells how
  4225. many degrees to rotate the ILBM around its upper left hand corner.
  4226. ProVector does not currently support rotation of bitmaps and will
  4227. ignore this value.  Path contains the name of the ILBM file and may
  4228. also contain a partial or full path to the file.  DR2D readers should
  4229. not assume the path is correct.  The full path to an ILBM on one
  4230. system may not match the path to the same ILBM on another system.  If
  4231. a DR2D reader cannot locate an ILBM file based on the full path name
  4232. or the file name itself (looking in the current directory), it should
  4233. ask the user where to find the image.
  4234.  
  4235.  
  4236.  
  4237. CPLY (0x43504C59)   /* Closed polygon */
  4238. OPLY (0x4F504C59)   /* Open polygon */
  4239.  
  4240. Polygons are the basic components of almost all 2D objects in the DR2D
  4241. FORM.  Lines, squares, circles, and arcs are all examples of DR2D
  4242. polygons.  There are two types of DR2D polygons, the open polygon
  4243. (OPLY) and the closed polygon (CPLY).  The difference between a closed
  4244. and open polygon is that the computer adds a line segment connecting
  4245. the endpoints of a closed polygon so that it is a continuous path.  An
  4246. open polygon's endpoints do not have to meet, like the endpoints of a
  4247. line segment.
  4248.  
  4249. struct POLYstruct {
  4250.     ULONGID;
  4251.     ULONGSize;
  4252.     USHORTNumPoints;
  4253.     IEEEPolyPoints[2*NumPoints];
  4254. };
  4255.  
  4256. The NumPoints field contains the number of points in the polygon and
  4257. the PolyPoints array contains the (X, Y) coordinates of the points of
  4258. the non-curved parts of polygons.  The even index elements are X
  4259. coordinates and the odd index elements are Y coordinates.
  4260.  
  4261. [Figure 3 - Bezier curves]
  4262.  
  4263. DR2D uses Bezier cubic sections, or cubic splines, to describe curves
  4264. in polygons.  A set of four coordinates (P1 through P4) defines the
  4265. shape of a cubic spline.  The first coordinate (P1) is the point where
  4266. the curve begins.  The line from the first to the second coordinate
  4267. (P1 to P2) is tangent to the curve at the first point.  The line from
  4268. P3 to P4 is tangent to the cubic section, where it ends at P4.
  4269.  
  4270. The coordinates describing the cubic section are stored in the
  4271. PolyPoints[] array with the coordinates of the normal points.  DR2D
  4272. inserts an indicator point before a set of cubic section points to
  4273. differentiate a normal point from the points that describe a curve.
  4274. An indicator point has an X value of 0xFFFFFFFF.  The indicator
  4275. point's Y value is a bit field.  If this bit field's low-order bit is
  4276. set, the points that follow the indicator point make up a cubic
  4277. section.
  4278.  
  4279. The second lowest order bit in the indicator point's bit field is the
  4280. MOVETO flag.  If this bit is set, the point (or set of cubic section
  4281. points) starts a new polygon, or subpolygon.  This subpolygon will
  4282. appear to be completely separate from other polygons but there is an
  4283. important connection between a polygon and its subpolygon.
  4284. Subpolygons make it possible to create holes in polygons.  An example
  4285. of a polygon with a hole is the letter ``O''.  The ``O'' is a filled
  4286. circular polygon with a smaller circular polygon within it.  The
  4287. reason the inner polygon isn't covered up when the outer polygon is
  4288. filled is that DR2D fills are done using the even-odd rule.
  4289.  
  4290. The even-odd rule determines if a point is ``inside'' a polygon by
  4291. drawing a ray outward from that point and counting the number of path
  4292. segments the ray crosses.  If the number is even, the point is outside
  4293. the object and shouldn't be filled.  Conversely, an odd number of
  4294. crossings means the point is inside and should be filled.  DR2D only
  4295. applies the even-odd rule to a polygon and its subpolygons, so no
  4296. other objects are considered in the calculations.
  4297.  
  4298. Taliesin, Inc. supplied the following algorithm to illustrate the
  4299. format of DR2D polygons.  OPLYs, CPLYs, AROWs, and ProVector's outline
  4300. fonts all use the same format:
  4301.  
  4302.  
  4303.         typedef union {
  4304.             IEEE num;
  4305.             LONG bits;
  4306.         } Coord;
  4307.  
  4308.         #define INDICATOR       0xFFFFFFFF
  4309.         #define IND_SPLINE      0x00000001
  4310.         #define IND_MOVETO      0x00000002
  4311.  
  4312.         /* A common pitfall in attempts to support DR2D has
  4313.                 been to fail to recognize the case when an
  4314.                 INDICATOR point indicates the following
  4315.                 coordinate to be the first point of BOTH a
  4316.                 Bezier cubic and a sub-polygon, ie. the
  4317.                 value of the flag = (IND_CURVE | IND_MOVETO) */
  4318.  
  4319.         Coord   Temp0, Temp1;
  4320.         int     FirstPoint, i, Increment;
  4321.  
  4322.         /* Initialize the path */
  4323.         NewPath();
  4324.         FirstPoint = 1;
  4325.  
  4326.         /* Draw the path */
  4327.         i = 0;
  4328.         while( i < NumPoints ) {
  4329.             Temp0.num = PolyPoints[2*i];    Temp1.num = PolyPoints[2*i + 1];
  4330.             if( Temp0.bits == INDICATOR ) {
  4331.                 /* Increment past the indicator */
  4332.                 Increment = 1;
  4333.                 if( Temp1.bits & IND_MOVETO ) {
  4334.                     /* Close and fill, if appropriate */
  4335.                     if( ID == CPLY ) {
  4336.                         FillPath();
  4337.                     }
  4338.                     else {
  4339.                         StrokePath();
  4340.                     }
  4341.  
  4342.                     /* Set up the new path */
  4343.                     NewPath();
  4344.                     FirstPoint = 1;
  4345.                 }
  4346.                 if( Temp1.bits & IND_CURVE ) {
  4347.                     /* The next 4 points are Bezier cubic control points */
  4348.                     if( FirstPoint )
  4349.                         MoveTo( PolyPoints[2*i + 2], PolyPoints[2*i + 3] );
  4350.                     else
  4351.                         LineTo( PolyPoints[2*i + 2], PolyPoints[2*i + 3] );
  4352.                     CurveTo(    PolyPoints[2*i + 4], PolyPoints[2*i + 5],
  4353.                                 PolyPoints[2*i + 6], PolyPoints[2*i + 7],
  4354.                                 PolyPoints[2*i + 8], PolyPoints[2*i + 9] );
  4355.                     FirstPoint = 0;
  4356.                     /* Increment past the control points */
  4357.                     Increment += 4;
  4358.                 }
  4359.             }
  4360.             else {
  4361.                 if( FirstPoint )
  4362.                     MoveTo(     PolyPoints[2*i], PolyPoints[2*i + 1] );
  4363.                 else
  4364.                     LineTo(     PolyPoints[2*i], PolyPoints[2*i + 1] );
  4365.                 FirstPoint = 0;
  4366.  
  4367.                 /* Increment past the last endpoint */
  4368.                 Increment = 1;
  4369.             }
  4370.  
  4371.             /* Add the increment */
  4372.             i += Increment;
  4373.         }
  4374.  
  4375.         /* Close the last path */
  4376.         if( ID == CPLY ) {
  4377.             FillPath();
  4378.         }
  4379.         else {
  4380.             StrokePath();
  4381.         }
  4382.  
  4383. GRUP (0x47525550)   /* Group */
  4384.  
  4385. The GRUP chunk combines several DR2D objects into one.  This chunk is
  4386. only valid inside nested DR2D FORMs, and must be the first chunk in
  4387. the FORM.
  4388.  
  4389. struct GROUPstruct {
  4390.     ULONGID;
  4391.     ULONGSize;
  4392.     USHORTNumObjs;
  4393. };
  4394.  
  4395. The NumObjs field contains the number of objects contained in this
  4396. group.  Note that the layer of the GRUP FORM overrides the layer of
  4397. objects within the GRUP.  The following example illustrates the layout
  4398. of the GRUP (and FILL) chunk.
  4399.  
  4400.        FORM { DR2D              /* Top-level drawing... */
  4401.                 DRHD { ... }    /* Confirmed by presence of DRHD chunk */
  4402.                 CMAP { ... }    /* Various other things... */
  4403.                 FONS { ... }
  4404.                 FORM { DR2D             /* A nested form... */
  4405.                         FILL { 1 }      /* Ah!  The fill-pattern table */
  4406.                         CPLY { ... }    /* with only 1 object */
  4407.                 }
  4408.                 FORM { DR2D             /* Yet another nested form */
  4409.                         GRUP { ..., 3 } /* Ah! A group of 3 objects */
  4410.                         TEXT { ... }
  4411.                         CPLY { ... }
  4412.                         OPLY { ... }
  4413.                 }
  4414.                 FORM { DR2D             /* Still another nested form */
  4415.                         GRUP { ..., 2 } /* A GRUP with 2 objects */
  4416.                         OPLY { ... }
  4417.                         TEXT { ... }
  4418.                 }
  4419.         }
  4420.  
  4421.  
  4422. STXT (0x53545854)   /* Simple text */
  4423.  
  4424. The STXT chunk contains a text string along with some information on
  4425. how and where to render the text. 
  4426.  
  4427. struct STXTstruct {
  4428.     ULONGID;
  4429.     ULONGSize;
  4430.     UBYTEPad0;/* Always 0 (for future expansion) */
  4431.     UBYTEWhichFont;      /* Which font to use */
  4432.     IEEECharW, CharH,  /* W/H of an individual char */
  4433. BaseX, BaseY,  /* Start of baseline */
  4434. Rotation;      /* Angle of text (in degrees) */
  4435.     USHORTNumChars;
  4436.     charTextChars[NumChars];
  4437. };
  4438.  
  4439. The text string is in the character array, TextChars[].  The ID of the
  4440. font used to render the text is WhichFont.  The font's ID is set in a
  4441. FONS chunk.  The starting point of the baseline of the text is (BaseX,
  4442. BaseY).  This is the point around which the text is rotated.  If the
  4443. Rotation field is zero (degrees), the text's baseline will originate
  4444. at (BaseX, BaseY) and move to the right.  CharW and CharH are used to
  4445. scale the text after rotation.  CharW is the average character width
  4446. and CharH is the average character height.  The CharW/H fields are
  4447. comparable to an X and Y font size.  
  4448.  
  4449.  
  4450.  
  4451. TPTH (0x54505448) /* A text string along a path */
  4452.  
  4453. This chunk defines a path (polygon) and supplies a string to render
  4454. along the edge of the path.
  4455.  
  4456. struct TPTHstruct {
  4457.     ULONG   ID;
  4458.     ULONG   Size;
  4459.     UBYTE   Justification;/* see defines, below */
  4460.     UBYTE   WhichFont;/* Which font to use */
  4461.     IEEE    CharW, CharH;/* W/H of an individual char*/
  4462.     USHORT  NumChars;   /* Number of chars in the string */
  4463.     USHORT  NumPoints;  /* Number of points in the path */
  4464.     char    TextChars[NumChars];/* PAD TO EVEN #! */
  4465.     IEEE    Path[2*NumPoints];  /* The path on which the text lies */
  4466. };
  4467.  
  4468. WhichFont contains the ID of the font used to render the text.
  4469. Justification controls how the text is justified on the line.
  4470. Justification can be one of the following values:
  4471.  
  4472. #define J_LEFT 0x00    /* Left justified */
  4473. #define J_RIGHT0x01   /* Right justified */
  4474. #define J_CENTER  0x02/* Center text */
  4475. #define J_SPREAD  0x03    /* Spread text across path */
  4476.  
  4477. CharW and CharH are the average width and height of the font
  4478. characters and are akin to X and Y font sizes, respectively.  A
  4479. negative FontH implies that the font is upsidedown.  Note that CharW
  4480. must not be negative.  NumChars is the number of characters in the
  4481. TextChars[] string, the string containing the text to be rendered.
  4482. NumPoints is the number of points in the Path[] array.  Path[] is the
  4483. path along which the text is rendered.  The path itself is not
  4484. rendered.  The points of Path[] are in the same format as the points
  4485. of a DR2D polygon.
  4486.  
  4487.  
  4488.  
  4489. A Simple DR2D Example
  4490.  
  4491. Here is a (symbolic) DR2D FORM:
  4492.  
  4493.     FORM { DR2D
  4494.             DRHD 16 { 0.0, 0.0, 10.0, 8.0 }
  4495.             CMAP  6 { 0,0,0, 255,255,255 }
  4496.             FONS  9 { 1, 0, 1, 0, "Roman" } 0
  4497.             DASH 12 { 1, 2, {1.0, 1.0} }
  4498.             ATTR 14 { 0, 0, 1, 0, 0, 0, 0, 0.0 }
  4499.             BBOX 16 { 2.0, 2.0, 8.0, 6.0 }
  4500.             FORM { DR2D
  4501.                   GRUP  2 { 2 }
  4502.                   BBOX 16 { 3.0, 4.0, 7.0, 5.0 }
  4503.                   STXT 36 { 0,1, 0.5, 1.0, 3.0, 5.0, 0.0, 12, "Hello, World" }
  4504.                   BBOX 16 { 2.0, 2.0, 8.0, 6.0 }
  4505.                   OPLY 42 { 5, {2.0,2.0, 8.0,2.0, 8.0,6.0, 2.0,6.0, 2.0,2.0 }
  4506.             }
  4507.     }
  4508.  
  4509.  
  4510. [Figure 4 - Simple DR2D drawing]
  4511.  
  4512.  
  4513. The OFNT FORM
  4514.  
  4515. OFNT(0x4F464E54)   /* ID of outline font file */
  4516.  
  4517. ProVector's outline fonts are stored in an IFF FORM called OFNT.  This
  4518. IFF is a separate file from a DR2D.  DR2D's FONS chunk refers only to
  4519. fonts defined in the OFNT form.
  4520.  
  4521.  
  4522. OFHD(0x4F464844)   /* ID of OutlineFontHeaDer */
  4523.  
  4524. This chunk contains some basic information on the font.
  4525.  
  4526. struct OFHDstruct {
  4527.     char   FontName[32];   /* Font name, null padded */
  4528.     short  FontAttrs;   /* See FA_*, below */
  4529.     IEEE   FontTop,   /* Typical height above baseline */
  4530.    FontBot,        /* Typical descent below baseline */
  4531.    FontWidth;      /* Typical width, i.e. of the letter O */
  4532. };
  4533.  
  4534. #define FA_BOLD      0x0001
  4535. #define FA_OBLIQUE   0x0002
  4536. #define FA_SERIF0x0004
  4537.  
  4538. The FontName field is a NULL terminated string containing the name of
  4539. this font.  FontAttrs is a bit field with flags for several font attributes.  
  4540. The flags, as defined above, are bold, oblique, and serif.  The unused 
  4541. higher order bits are reserved for later use.  The other fields describe the 
  4542. average dimensions of the characters in this font.  FontTop is the average 
  4543. height above the baseline, FontBot is the average descent below the baseline, 
  4544. and FontWidth is the average character width.
  4545.  
  4546.  
  4547. KERN(0x4B45524C)   /* Kerning pair */
  4548.  
  4549. The KERN chunk describes a kerning pair.  A kerning pair sets the
  4550. distance between a specific pair of characters.  
  4551.  
  4552. struct KERNstruct {
  4553.     short   Ch1, Ch2; /* The pair to kern (allows for 16 bits...) */
  4554.     IEEE    XDisplace,  /* Amount to displace -left +right */
  4555.     YDisplace;  /* Amount to displace -down +up */
  4556. };
  4557.  
  4558. The Ch1 and Ch2 fields contain the pair of characters to kern.  These 
  4559. characters are typically stored as ASCII codes.  Notice that OFNT stores 
  4560. the characters as a 16-bit value.  Normally, characters are stored as 8-bit 
  4561. values.  The wary programmer will be sure to cast assigns properly to avoid 
  4562. problems with assigning an 8-bit value to a 16-bit variable.  The remaining 
  4563. fields, XDisplace and YDisplace, supply the baseline shift from Ch1 to Ch2.
  4564.  
  4565.  
  4566. CHDF(0x43484446)  /* Character definition */
  4567.  
  4568. This chunk defines the shape of ProVector's outline fonts.  
  4569.  
  4570. struct CHDFstruct {
  4571.     short   Ch;/* The character we're defining (ASCII) */
  4572.     short   NumPoints;  /* The number of points in the definition */
  4573.     IEEE    XWidth,     /* Position for next char on baseline - X */
  4574.             YWidth;/* Position for next char on baseline - Y */
  4575.  /* IEEE    Points[2*NumPoints]*/    /* The actual points */
  4576. };
  4577.  
  4578.  
  4579. #defineINDICATOR   0xFFFFFFFF   /* If X == INDICATOR, Y is an action */
  4580. #defineIND_SPLINE  0x00000001   /* Next 4 pts are spline control pts */
  4581. #defineIND_MOVETO  0x00000002   /* Start new subpoly */
  4582. #define IND_STROKE  0x00000004   /* Stroke previous path */
  4583. #define IND_FILL    0x00000008   /* Fill previous path */
  4584.  
  4585. Ch is the value (normally ASCII) of the character outline this chunk
  4586. defines.  Like Ch1 and Ch2 in the KERN chunk, Ch is stored as a 16-bit
  4587. value.  (XWidth,YWidth) is the offset to the baseline for the
  4588. following character.  OFNT outlines are defined using the same method
  4589. used to define DR2D's polygons (see the description of OPLY/CPLY for
  4590. details).
  4591.  
  4592. Because the OFNT FORM does not have an ATTR chunk, it needed an
  4593. alternative to make fills and strokes possible.  There are two extra
  4594. bits used in font indicator points not found in polygon indicator
  4595. points, the IND_STROKE and IND_FILL bits (see defines above).  These
  4596. two defines describe how to render the current path when rendering
  4597. fonts.
  4598.  
  4599. The current path remains invisible until the path is either filled
  4600. and/or stroked.  When the IND_FILL bit is set, the currently defined
  4601. path is filled in with the current fill pattern (as specified in the
  4602. current ATTR chunk).  A set IND_STROKE bit indicates that the
  4603. currently defined path itself should be rendered.  The current ATTR's
  4604. chunk dictates the width of the line, as well as several other
  4605. attributes of the line.  These two bits apply only to the OFNT FORM
  4606. and should not be used in describing DR2D polygons.
  4607.  
  4608.  
  4609.  
  4610.  
  4611.