home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / datatypes / acbm_datatype / classbase.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-09  |  14.8 KB  |  471 lines

  1.  
  2. /*
  3. **
  4. **  $VER: classbase.c 1.3 (9.11.97)
  5. **  acbm.datatype 1.3
  6. **
  7. **  Library routines for a DataTypes class
  8. **
  9. **  Written 1996/97 by Roland 'Gizzy' Mainz
  10. **  Original example source from David N. Junod
  11. **
  12. */
  13.  
  14.  
  15. /* main includes */
  16. #include "classbase.h"
  17.  
  18. /****** acbm.datatype/MAIN ***************************************************
  19. *
  20. *    INTRODUCTION
  21. *        Datatypes class for IFF ACBM pictures. Based on the CBM datatypes
  22. *        example source/ documents written by David Junod and the AmigaBasic
  23. *        ACBM tools
  24. *
  25. *    REQUIREMENTS
  26. *        - You need at least Kick/WB 3.0.
  27. *
  28. *        - "datatypes/picture.datatype", >= V39.
  29. *
  30. *    USAGE
  31. *        If the datatypes descriptor file was activated, any attempt to load
  32. *        an IFF ACBM picture using GMultiView, MultiView, AmigaGuide or
  33. *        SwitchWindow will load and view the picture.
  34. *
  35. *        acbm.datatype supports also the clipboard as input, e.g.
  36. *        "MultiView CLIPBOARD", ClipView or SwitchWindow are able to show
  37. *        IFF ACBMs from clipboard.
  38. *
  39. *        If you want to save the current picture in acbm.datatype's local
  40. *        format, use MultiView's "Project/Save As..." menu (or GMultiView's
  41. *        "Project/Save As Raw...").
  42. *        acbm.datatype saves then the current picture.
  43. *
  44. *    INSTALLATION
  45. *        After unpacking this archive:
  46. *        Because this version does not include an Installer script, you have
  47. *        to do the installation manually through the shell:
  48. *
  49. *          - Unpack this archive and copy the "acbm.datatype" to
  50. *            SYS:Classes/DataTypes:
  51. *
  52. *     Copy CLONE FROM "acbm.datatype" TO "SYS:Classes/DataTypes/acbm.datatype"
  53. *
  54. *          - Then copy the datatypes descriptor into the DEVS:DataTypes
  55. *            directory.
  56. *            If the descriptor already exists, you should not replace it,
  57. *            otherwise you may loose "toolnodes" and other settings stored in
  58. *            the existing descriptor.
  59. *
  60. *     Copy CLONE FROM "ACBM(%|.info)" TO DEVS:Datatypes/
  61. *
  62. *    SOURCE
  63. *        This is the official datatypes.library V45 reference implementation
  64. *        of a picture.datatype subclass which supports saving in it's local
  65. *        format (such an encoder is used by "DTConvert" and other tools which
  66. *        uses DataTypes for conversion, e.g. DataType -> DataType (e.g. GIF
  67. *        -> IFF ACBM etc.)).
  68. *
  69. *        Source is fully included here.
  70. *
  71. *    AUTHOR
  72. *        If you want to blame me, report any bugs, or wants a new version
  73. *        send your letter to:
  74. *                        Roland Mainz
  75. *                        Hohenstaufenstraße 8
  76. *                        52388 Nörvenich
  77. *                        GERMANY
  78. *
  79. *        Phone: (+49)(0)2426/901568
  80. *        Fax:   (+49)(0)2426/901569
  81. *
  82. *        EMAIL is also available (if you want to send me attachments
  83. *        larger than 1MB (up to 5MB, more with my permission):
  84. *
  85. *        GISBURN@w-specht.rhein-ruhr.de
  86. *
  87. *        Up to December 1997 I'm reachable using this email address, too:
  88. *        Reinhold.A.Mainz@KBV.DE
  89. *
  90. *        | Please put your name and address in your mails !
  91. *        | German mailers should add their phone numbers.
  92. *        | See BUGS section above when submitting bug reports.
  93. *
  94. *        Sorry, but I can only look once a week for mails.
  95. *        If you don't hear something from me within three weeks, please
  96. *        send your mail again (but watch about new releases) (problems with
  97. *        this email port are caused by reconfigurations, hackers, network
  98. *        problems etc.).
  99. *
  100. *        The  entire  "acbm.datatype"  package  may  be  noncommercially
  101. *        redistributed, provided  that  the package  is always  distributed
  102. *        in it's complete  form (including it's documentation). A small
  103. *        copy fee  for media costs is okay but any kind of commercial
  104. *        distribution is strictly forbidden without my permission !
  105. *        Comments and suggestions how to improve this program are
  106. *        generally appreciated!
  107. *
  108. *        Thanks to David Junod, who wrote the datatypes environment and lots
  109. *        of the datatypes example code, Carolyn Scheppner and other people
  110. *        for their file formats, Matt Dillon for his DICE, Olaf 'Olsen' 
  111. *        Barthel for his help, ideas and some text clips from his 
  112. *        documentations.
  113. *
  114. ******************************************************************************
  115. *
  116. */
  117.  
  118.  
  119. /****** acbm.datatype/--datasheed-- ******************************************
  120. *
  121. *   NAME
  122. *       acbm.datatype -- data type for IFF ACBM pictures
  123. *
  124. *   SUPERCLASS
  125. *       picture.datatype
  126. *
  127. *   DESCRIPTION
  128. *       The acbm datatype, a sub-class of the picture.datatype, is
  129. *       used to load and save IFF ACBM pictures.
  130. *
  131. *       This class is offloaded from the datatypes.library V45 example code
  132. *       and is the reference implementation of a picture.datatype subclass
  133. *       (using V42 API) which supports saving in it's local format (as used
  134. *       by converters like "DTConvert").
  135. *
  136. *   METHODS
  137. *       OM_NEW -- Create a new picture object from a description file. The
  138. *           source may be a file or a clipboard unit. An empty object
  139. *           (using DTST_RAM) may also be created.
  140. *
  141. *       OM_UPDATE -- Perform an ICM_CHECKLOOP check, and if succesfull, the
  142. *           method will be executed like OM_SET downstairs.
  143. *
  144. *       OM_SET -- Pass msg to superclass and call GM_RENDER if retval from
  145. *           superclass was != 0UL.
  146. *
  147. *       DTM_WRITE -- Save data in local (IFF ACBM) or superclass (IFF ILBM)
  148. *           format.
  149. *
  150. *       All other methods are passed unchanged to superclass.
  151. *
  152. *   ATTRIBUTES
  153. *       Following attributes are set by the object and are READ-ONLY for
  154. *       applications (if the datatype is used for saving (e.g. the
  155. *       application creates an empty acbm.datatype object and fills
  156. *       in some required attributes as shown in the "DTConvert" example),
  157. *       these attributes are used by the encoder to create an IFF ACBM
  158. *       picture file):
  159. *
  160. *       DTA_ObjName         -- Set by an IFF NAME chunk. If missing,
  161. *                              DTA_ObjName will take DTA_Name's value.
  162. *       DTA_ObjAuthor       -- Set by an IFF AUTH chunk.
  163. *       DTA_ObjAnnotation   -- Set by an IFF ANNO chunk.
  164. *       DTA_ObjCopyright    -- Set by an IFF (C) chunk.
  165. *       DTA_ObjVersion      -- Set by an IFF FVER chunk.
  166. *       DTA_NominalHoriz    -- Same as PDTA_BitMapHeader -> bmh_Width
  167. *       DTA_NominalVert     -- Same as PDTA_BitMapHeader -> bmh_Height
  168. *       PDTA_BitMapHeader   -- Filled by the IFF ILBM BMHD chunk
  169. *       PDTA_ModeID         -- Set by an IFF ILBM/ACBM CAMG chunk. If missing
  170. *                              or the value here is 0, the datatype will
  171. *                              select a value based on BMHD information.
  172. *       PDTA_NumColors      -- Set by IFF ILBM/ACBM CMAP chunk
  173. *       PDTA_ColorRegisters -- Set by IFF ILBM/ACBM CMAP chunk
  174. *       PDTA_CRegs          -- Set by IFF ILBM/ACBM CMAP chunk
  175. *       PDTA_BitMap         -- The image itself, created from IFF ACBM ABIT
  176. *                              chunk and some IFF ILBM/ACBM BMHD infos.
  177. *
  178. *   BUGS
  179. *       - ModeID selecttion does not support SUPERHIRES.
  180. *
  181. *       - Images deeper than 8 planes are not fully supported yet; a colormap
  182. *         is everytimes expected.
  183. *
  184. *   TODO
  185. *       - Fixing the bugs above.
  186. *
  187. *       - Add GRAB support (e.g. IFF ILBM GRAB chunk and matching PDTA_Grab
  188. *         chunk).
  189. *
  190. *       - Add support for non-planar 8 bit deep bitmaps in the encoder.
  191. *
  192. *   HISTORY
  193. *       V1.1
  194. *         First public release.
  195. *
  196. *       V1.2
  197. *         - Recompiled with SAS/C 6.58. Should fix some mc68060 related
  198. *           problems.
  199. *
  200. *         - Added 020, 040, 060 versions and the matching makefile to show
  201. *           how to do this correctly (e.g. no mc68040 version attempts to run
  202. *           on a plane mc68000, it rejects opening it silently instead of
  203. *           crashing).
  204. *
  205. *         - Moved BOOPSI class definitions to "classdata.h".
  206. *
  207. *         - Added example "what to do" when no encoder is implemented
  208. *           (e.g. returns result == 0 and result2 == ERROR_NOT_IMPLEMENTED)
  209. *           if complied with the NO_ENCODER switch.
  210. *
  211. *         - Moved "iffparse.library"'s OpenLibrary and CloseLibrary from
  212. *           LibInit to LibOpen to handle it more dynamically...
  213. *
  214. *         - Added missing ObtainSemaphoreShared/ReleaseSemaphore locking to
  215. *           the encoder part.
  216. *
  217. *         - Fixed the bug that on rare conditions (error after the bitmap has
  218. *           been loaded) wasn't freed.
  219. *           Fixed.
  220. *
  221. *         - Improved iffparse.library handling; more speed and corrected
  222. *           error handling.
  223. *
  224. *         - Updates docs and source comments a little bit
  225. *
  226. *         - Fixed the bug that some generic IFF chunks were not loaded
  227. *           (like NAME, FVER, AUTH, (C) etc.).
  228. *           Fixed.
  229. *
  230. *         - The stack swapping code now allocates it's memory without the
  231. *           MEMF_CLEAR bit which results in a small speedup.
  232. *
  233. *         - Replaced BestModeIDA code by a simple flag setting code.
  234. *           picture.datatype does BestModeIDA if the requested mode does
  235. *           not exist.
  236. *
  237. *         - Now allocates the bitmap with the BMF_MINPLANES flag set. Now
  238. *           the datatype supports deeper images, but a colormap is still
  239. *           expected.
  240. *
  241. *         - Added missing MAIN section in the autodoc.
  242. *
  243. *       V1.3
  244. *         - Replaced stack swapping code by my "standard" module
  245. *
  246. *         - Improved the "smakefile" a little bit
  247. *
  248. *         - Minor code changes
  249. *
  250. *   SEE ALSO
  251. *       picture.datatype
  252. *
  253. *******************************************************************************
  254. *
  255. */
  256.  
  257.  
  258. /****** acbm.datatype/--input_format-- ****************************************
  259. *
  260. *    NAME
  261. *        IFF ACBM -- Amiga Contiguous BitMap
  262. *
  263. *    DESCRIPTION
  264. *        FORM ACBM has the same format as FORM ILBM except the normal BODY
  265. *        chunk (Interleaved Bitmap) is replaced by an ABIT chunk (Amiga
  266. *        BITplanes).
  267. *
  268. *        The ABIT chunk contains continous bitplane data. The chunk contains
  269. *        sequential data fro planes 0 through plane i.
  270. *
  271. *    SEE ALSO
  272. *        - ARKM Devices: IFF part
  273. *
  274. *        - iffparse.library autodocs
  275. *
  276. *        - AmigaBasic LoadACBM/SaveACBM
  277. *
  278. *    THANKS TO
  279. *        Carolyn Scheppner (CBM) for creating this format
  280. *
  281. *******************************************************************************
  282. *
  283. */
  284.  
  285.  
  286.  
  287. /*****************************************************************************/
  288.  
  289. DISPATCHERFLAGS
  290. struct IClass *ObtainACBMEngine( REGA6 struct ClassBase *cb )
  291. {
  292.     return( (cb -> cb_Lib . cl_Class) );
  293. }
  294.  
  295. /*****************************************************************************/
  296.  
  297. DISPATCHERFLAGS
  298. struct Library *LibInit( REGD0 struct ClassBase *cb, REGA0 BPTR seglist, REGA6 struct ExecBase *sysbase )
  299. {
  300.     cb -> cb_SegList = seglist;
  301.     cb -> cb_SysBase = sysbase;
  302.  
  303.     InitSemaphore( (&(cb -> cb_Lock)) );
  304.  
  305. #ifdef REQUIREDAFF
  306.     /* Check if the requested cpu/fpu is available */
  307.     if( ((sysbase -> AttnFlags) | REQUIREDAFF) != (sysbase -> AttnFlags) )
  308.     {
  309.       return( NULL );
  310.     }
  311. #endif /* REQUIREDAFF */
  312.  
  313.     if( (cb -> cb_SysBase -> LibNode . lib_Version) >= 39UL )
  314.     {
  315.       /* Obtain ROM libs */
  316.       if( cb -> cb_UtilityBase = OpenLibrary( "utility.library", 39UL ) )
  317.       {
  318.         if( cb -> cb_DOSBase = OpenLibrary( "dos.library", 39UL ) )
  319.         {
  320.           if( cb -> cb_GfxBase = OpenLibrary( "graphics.library", 39UL ) )
  321.           {
  322.             if( cb -> cb_IntuitionBase = OpenLibrary( "intuition.library", 39UL ) )
  323.             {
  324.               return( (&(cb -> cb_Lib . cl_Lib)) );
  325.  
  326. #ifdef COMMENTED_OUT
  327.               CloseLibrary( IntuitionBase );
  328. #endif /* COMMENTED_OUT */
  329.             }
  330.  
  331.             CloseLibrary( (cb -> cb_GfxBase) );
  332.           }
  333.  
  334.           CloseLibrary( (cb -> cb_DOSBase) );
  335.         }
  336.  
  337.         CloseLibrary( (cb -> cb_UtilityBase) );
  338.       }
  339.     }
  340.  
  341.     return( NULL );
  342. }
  343.  
  344. /*****************************************************************************/
  345.  
  346. DISPATCHERFLAGS
  347. LONG LibOpen( REGA6 struct ClassBase *cb )
  348. {
  349.     LONG retval = (LONG)cb;
  350.     BOOL success = TRUE;
  351.  
  352.     ObtainSemaphore( (&(cb -> cb_Lock)) );
  353.  
  354.     /* Use an internal use counter */
  355.     cb -> cb_Lib . cl_Lib . lib_OpenCnt++;
  356.     cb -> cb_Lib . cl_Lib . lib_Flags &= ~LIBF_DELEXP;
  357.  
  358.     if( (cb -> cb_Lib . cl_Lib . lib_OpenCnt) == 1U )
  359.     {
  360.       if( (cb -> cb_Lib . cl_Class) == NULL )
  361.       {
  362.         success = FALSE;
  363.  
  364.         if( cb -> cb_IFFParseBase = OpenLibrary( "iffparse.library", 39UL ) )
  365.         {
  366.           if( cb -> cb_DataTypesBase = OpenLibrary( "datatypes.library", 39UL ) )
  367.           {
  368.             if( cb -> cb_SuperClassBase = OpenLibrary( "datatypes/picture.datatype", 39UL ) )
  369.             {
  370.               if( cb -> cb_Lib . cl_Class = initClass( cb ) )
  371.               {
  372.                 success = TRUE;
  373.               }
  374.             }
  375.           }
  376.         }
  377.       }
  378.     }
  379.  
  380.     if( !success )
  381.     {
  382.       CloseLibrary( (cb -> cb_SuperClassBase) );
  383.       CloseLibrary( (cb -> cb_DataTypesBase) );
  384.       CloseLibrary( (cb -> cb_IFFParseBase) );
  385.  
  386.       cb -> cb_IFFParseBase = cb -> cb_DataTypesBase = cb -> cb_SuperClassBase = NULL;
  387.  
  388.       (cb -> cb_Lib . cl_Lib . lib_OpenCnt)--;
  389.  
  390.       retval = 0L;
  391.     }
  392.  
  393.     ReleaseSemaphore( (&(cb -> cb_Lock)) );
  394.  
  395.     return( retval );
  396. }
  397.  
  398. /*****************************************************************************/
  399.  
  400. DISPATCHERFLAGS
  401. LONG LibClose( REGA6 struct ClassBase *cb )
  402. {
  403.     LONG retval = 0L;
  404.  
  405.     ObtainSemaphore( (&(cb -> cb_Lock)) );
  406.  
  407.     if( cb -> cb_Lib . cl_Lib . lib_OpenCnt )
  408.     {
  409.       (cb -> cb_Lib . cl_Lib . lib_OpenCnt)--;
  410.     }
  411.  
  412.     if( ((cb -> cb_Lib . cl_Lib . lib_OpenCnt) == 0U) && (cb -> cb_Lib . cl_Class) )
  413.     {
  414.       if( FreeClass( (cb -> cb_Lib . cl_Class) ) )
  415.       {
  416.         cb -> cb_Lib . cl_Class = NULL;
  417.  
  418.         CloseLibrary( (cb -> cb_SuperClassBase) );
  419.         CloseLibrary( (cb -> cb_DataTypesBase) );
  420.         CloseLibrary( (cb -> cb_IFFParseBase) );
  421.       }
  422.       else
  423.       {
  424.         cb -> cb_Lib . cl_Lib . lib_Flags |= LIBF_DELEXP;
  425.       }
  426.     }
  427.  
  428.     ReleaseSemaphore( (&(cb -> cb_Lock)) );
  429.  
  430.     if( cb -> cb_Lib . cl_Lib . lib_Flags & LIBF_DELEXP )
  431.     {
  432.       retval = LibExpunge( cb );
  433.     }
  434.  
  435.     return( retval );
  436. }
  437.  
  438. /*****************************************************************************/
  439.  
  440. DISPATCHERFLAGS
  441. LONG LibExpunge( REGA6 struct ClassBase *cb )
  442. {
  443.     BPTR seg;
  444.  
  445.     if( cb -> cb_Lib . cl_Lib . lib_OpenCnt )
  446.     {
  447.       cb -> cb_Lib . cl_Lib . lib_Flags |= LIBF_DELEXP;
  448.  
  449.       seg = NULL;
  450.     }
  451.     else
  452.     {
  453.       Remove( (&(cb -> cb_Lib . cl_Lib . lib_Node)) );
  454.  
  455.       seg = cb -> cb_SegList;
  456.  
  457.       CloseLibrary( (cb -> cb_IntuitionBase) );
  458.       CloseLibrary( (cb -> cb_GfxBase) );
  459.       CloseLibrary( (cb -> cb_DOSBase) );
  460.       CloseLibrary( (cb -> cb_UtilityBase) );
  461.  
  462.       FreeMem( (APTR)((ULONG)(cb) - (ULONG)(cb -> cb_Lib . cl_Lib . lib_NegSize)), (ULONG)((cb -> cb_Lib . cl_Lib . lib_NegSize) + (cb -> cb_Lib . cl_Lib . lib_PosSize)) );
  463.     }
  464.  
  465.     return( (LONG)seg );
  466. }
  467.  
  468.  
  469.  
  470.  
  471.