home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 351.lha / iff.library_v1.6 / shopict.c < prev    next >
C/C++ Source or Header  |  1990-02-26  |  11KB  |  448 lines

  1. #define NOLIB 1
  2. /*-------------------------------------------------------------------------+
  3.  |                                       |
  4.  | Name:    shopict.c                               |
  5.  | Purpose: example program on how to use some of the features of       |
  6.  |        iff.library                            |
  7.  |                                       |
  8.  | Author:  RWA                    Date: 8/89           |
  9.  +-------------------------------------------------------------------------*/
  10. #include <devices/printer.h>
  11. #include <libraries/dosextens.h>
  12. #include <intuition/intuition.h>
  13. #include <workbench/workbench.h>
  14. #include <workbench/startup.h>
  15. #include <workbench/icon.h>
  16. #include <functions.h>
  17. #include <stdio.h>
  18. #ifndef NOLIB
  19. #include "ctype.h"
  20. #include "string.h"
  21. #endif
  22. #include "ifflib.h"
  23.  
  24. /* Aztec definitions of this have a bug (1<< instead of 1L<<)*/
  25. #define SIGBREAKF_CTRLC        (1L<<SIGBREAKB_CTRL_C)
  26. #define SIGBREAKF_CTRLD        (1L<<SIGBREAKB_CTRL_D)
  27. #define SIGBREAKF_CTRLE        (1L<<SIGBREAKB_CTRL_E)
  28. #define SIGBREAKF_CTRLF        (1L<<SIGBREAKB_CTRL_F)
  29.  
  30. /* Signals that indicate a break was sent from the user. */
  31. #define SIGBREAK \
  32.    (SIGBREAKF_CTRLC|SIGBREAKF_CTRLD|SIGBREAKF_CTRLE|SIGBREAKF_CTRLF)
  33.  
  34. void *IntuitionBase, *GfxBase, *IFFBase;
  35.  
  36. #ifdef NOLIB
  37. extern void *IconBase;
  38. #else
  39. void *IconBase;
  40. long _math = 0;
  41. #endif
  42.  
  43. /* This is a mouse pointer image that looks like a 3.5 inch floppy disk,
  44.  * it appears while the picture is being saved. */
  45.  
  46. #define DISKPTRHEIGHT 13L
  47.  
  48. unsigned short disk_pointer[30] =
  49. {
  50. 0x0000, 0x0000,
  51.  
  52. 0x1ff0, 0xe00c,
  53. 0x1f30, 0xe0ce,
  54. 0x1f30, 0xe0cf,
  55. 0x1f30, 0xe0cf,
  56. 0x1f30, 0xe0cf,
  57. 0x1ff0, 0xe00f,
  58. 0x0000, 0xffff,
  59. 0x0000, 0xffff,
  60. 0x0000, 0xffff,
  61. 0x0000, 0xffff,
  62. 0x0000, 0xffff,
  63. 0x0000, 0xbfff,
  64. 0x0000, 0xffff,
  65.  
  66. 0x0000, 0x0000,
  67. };
  68.  
  69. /* This is a mouse pointer image that represents a printer,
  70.  * it is displayed while the picture is being printed. */
  71.  
  72. #define PRINTERPTRHEIGHT 13L
  73.  
  74. unsigned short printer_pointer[30] =
  75. {
  76. 0x0000, 0x0000,
  77.  
  78. 0x07fc, 0x0000,
  79. 0x04fc, 0x0000,
  80. 0x07fc, 0x0000,
  81. 0x0404, 0x0000,
  82. 0x07fc, 0x0000,
  83. 0x1c07, 0x0000,
  84. 0x27fd, 0x1802,
  85. 0x4003, 0x3ffc,
  86. 0xfffd, 0x0002,
  87. 0x8005, 0x7ffa,
  88. 0x8005, 0x7ffa,
  89. 0x8005, 0x7ffa,
  90. 0xfffe, 0x0000,
  91.  
  92. 0x0000, 0x0000,
  93. };
  94.  
  95.  
  96. struct NewScreen ns =
  97. {
  98. 0,0, 0,0, 0,  /* LE, TE, W, H, D */
  99. 0,0,          /* pns */
  100. 0,          /* ViewModes */
  101. CUSTOMSCREEN | SCREENBEHIND | SCREENQUIET, /* type */
  102. 0L,          /* font */
  103. (UBYTE *)"View",       /* title */
  104. 0L,          /* gadgets */
  105. 0L,          /* bitmap */
  106. };
  107.  
  108. struct NewWindow nw =
  109. {
  110. 0,0,0,0,       /* LE,TE,W,H */
  111. 0,0,           /* pns */
  112. RAWKEY | MOUSEBUTTONS,          /* IDCMP */
  113. SIMPLE_REFRESH | BACKDROP | BORDERLESS | ACTIVATE | RMBTRAP, /* flags */
  114. 0L,           /* Gadget */
  115. 0L,           /* checkmark */
  116. (UBYTE *)"",   /* title */
  117. 0L,           /* screen */
  118. 0L,           /* bitmap */
  119. 0,0,           /* min */
  120. 0,0,           /* max */
  121. CUSTOMSCREEN,  /* type */
  122. };
  123.  
  124. UWORD colors[4096];
  125. UWORD ncolors;
  126.  
  127. void p(hdr)
  128. register BitMapHeader *hdr;
  129. {
  130. printf("width: %d,  height: %d\n",(long)hdr->w,(long)hdr->h);
  131. printf("x: %d, y: %d\n",(long)hdr->x,(long)hdr->y);
  132. printf("nPlanes: %d\n",(long)hdr->nPlanes);
  133. printf("masking: %d\n",(long)hdr->masking);
  134. printf("compression: %d\n",(long)hdr->compression);
  135. printf("transparentColor: %d\n",(long)hdr->transparentColor);
  136. printf("xAspect: %d, yAspect:%d\n",
  137.    (long)hdr->xAspect,(long)hdr->yAspect);
  138. printf("pageWidth: %d, pageHeight: %d\n",
  139.    (long)hdr->pageWidth,(long)hdr->pageHeight);
  140.  
  141. if( hdr->viewmodes != GETVIEWMODES )
  142.    {
  143.    printf("ViewModes: 0");
  144.    if( hdr->viewmodes & PFBA ) printf("|PFBA");
  145.    if( hdr->viewmodes & DUALPF ) printf("|DUALPF");
  146.    if( hdr->viewmodes & HIRES ) printf("|HIRES");
  147.    if( hdr->viewmodes & LACE ) printf("|LACE");
  148.    if( hdr->viewmodes & HAM ) printf("|HAM");
  149.    if( hdr->viewmodes & VP_HIDE ) printf("|VP_HIDE");
  150.    if( hdr->viewmodes & GENLOCK_VIDEO ) printf("|GENLOCK_VIDEO");
  151.    if( hdr->viewmodes & GENLOCK_AUDIO ) printf("|GENLOCK_AUDIO");
  152.    if( hdr->viewmodes & EXTRA_HALFBRITE ) printf("|EXTRA_HALFBRITE");
  153.    printf("\n");
  154.    }
  155. }
  156.  
  157. /* Gives help to CLI users */
  158. void helpme()
  159. {
  160. static char *help[] =
  161. {
  162.       "            ShoPict IFF Viewer help\n",
  163. "CLI SYNTAX:  ShoPict [-v] [-t<toolname>] <pictures> <pictures> ...",
  164. "             -v prints stats on the file",
  165. "             -t<toolname> makes icon tool = <toolname>",
  166. "             The wild-card characters * and ? are accepted.\n",
  167. "From WBench: Set the default tool to ShoPict in the info file.",
  168. "             Multiple icons can be \"shift selected\"\n",
  169. "While viewing:",
  170. "             The 'I' key writes an icon for the picture.",
  171. "             The 'P' key prints the picture.",
  172. "             The 'S' key saves the picture with an icon to",
  173. "             RAM:temp.pic",
  174. 0L,
  175. };
  176. register char **s;
  177. for( s = help; *s; s++ )
  178.    printf("%s\n",*s);
  179. }
  180.  
  181. /* cleans out a message port */
  182.  
  183. void CleanPort(port)
  184. register struct MsgPort *port;
  185. {
  186. struct IntuiMessage *msg;
  187. while( msg = (struct IntuiMessage *)GetMsg(port) )
  188.    ReplyMsg((struct Message *)msg);
  189. }
  190.  
  191.  
  192. short showpicture(fname,toolname,verbose)
  193. register char *fname;
  194. register char *toolname;
  195. short verbose;
  196. {
  197. extern long open_prtdev(), DumpRPort();
  198. extern short PutProject();
  199. extern struct Image FaceOn_img, FaceOff_img;
  200.  
  201. BitMapHeader hdr;
  202. register struct IntuiMessage *i;
  203. register struct Window *w = 0L;
  204. register struct Screen *s = 0L;
  205. UWORD *clrs = colors, its_cool = 1, prt = 0, save = 0, icn = 0;
  206. struct BitMap *bm;
  207. register ULONG signals, waitsig, isig;
  208. register short rv = 0;
  209. struct NewWindow _nw;
  210. struct NewScreen _ns;
  211. static char *tooltypes[] = {"FILETYPE=ILBM",0L,};
  212.  
  213. if( fname )
  214.    {
  215.    hdr.viewmodes = GETVIEWMODES;
  216.    if( !QueryIFF(fname,&hdr) )
  217.       {
  218.       if( verbose )
  219.      p(&hdr);
  220.       _nw = nw;
  221.       _ns = ns;
  222.  
  223.       _ns.Width = hdr.pageWidth;
  224.       _ns.Height = hdr.pageHeight;
  225.       _ns.Depth = hdr.nPlanes;
  226.  
  227.       /* Most IFF pictures don't contain ViewModes,
  228.        * so the viewmodes variable will be un-changed.
  229.        * also older versions of the IFF.LIBRARY don't
  230.        * support the ViewModes variable.  */
  231.       if( hdr.viewmodes == GETVIEWMODES )
  232.      {
  233.      if( hdr.pageWidth > 384 && hdr.nPlanes < 5 ) _ns.ViewModes |= HIRES;
  234.      if( hdr.pageHeight > 240 ) _ns.ViewModes |= LACE;
  235.      if( hdr.nPlanes > 5 ) _ns.ViewModes |= HAM;
  236.      }
  237.       else _ns.ViewModes = hdr.viewmodes;
  238.  
  239.       /* open the screen */
  240.       if( !(s = OpenScreen(&_ns)) )
  241.      {
  242.      rv = 20;
  243.      goto abort;
  244.      }
  245.  
  246.       /* this piece of code handles over-scanned pictures */
  247.       if( _ns.ViewModes & HIRES )
  248.      {
  249.      if( _ns.Width > 640 ) s->ViewPort.DxOffset = -32;
  250.      }
  251.       else if( _ns.Width > 320 ) s->ViewPort.DxOffset = -16;
  252.       if( _ns.ViewModes & LACE )
  253.      {
  254.      if( _ns.Height > 400 ) s->ViewPort.DyOffset = -32;
  255.      }
  256.       else if( _ns.Height > 200 ) s->ViewPort.DyOffset = -16;
  257.       if( s->ViewPort.DxOffset || s->ViewPort.DyOffset ) RethinkDisplay();
  258.  
  259.       /* set up and open the new window */
  260.       bm = &s->BitMap;
  261.       _nw.MaxWidth = _nw.MinWidth = _nw.Width = hdr.w;
  262.       _nw.MinHeight = _nw.MaxHeight = _nw.Height = hdr.h;
  263.       _nw.Screen = s;
  264.       if( !(w = OpenWindow(&_nw)) )
  265.      {
  266.      rv = 21;
  267.      goto abort;
  268.      }
  269.  
  270.       /* load the iff picture */
  271.       if( rv = GetIFF_bitmap(fname,&bm,&clrs,&ncolors,0) )
  272.      goto abort;
  273.  
  274.       /* load the colors into the screen's view port */
  275.       LoadRGB4(&s->ViewPort,clrs,(long)ncolors);
  276.  
  277.       /* bring the screen to the front */
  278.       ScreenToFront(s);
  279.  
  280.       isig = (1L << w->UserPort->mp_SigBit);
  281.       waitsig = isig | SIGBREAK;
  282.  
  283.       /* input loop */
  284.       while(its_cool)
  285.      {
  286.      signals = Wait(waitsig);
  287.      if( signals & isig )
  288.         {
  289.         while( i = (struct IntuiMessage *)GetMsg(w->UserPort) )
  290.            {
  291.            if( i->Class == RAWKEY )
  292.           {
  293.           switch(i->Code)
  294.              {
  295.              case 0x40: its_cool = 0; rv = 0; break;
  296.              case 0x19: prt = 1; break;
  297.              case 0x21: save = 1; break;
  298.              case 0x17: icn = 1; break;
  299.              default: break;
  300.              }
  301.           }
  302.            ReplyMsg((struct Message *)i);
  303.            }
  304.  
  305.         /* this part prints the picture if the user presses
  306.          * the 'P' key. */
  307.         if( prt && (!(signals & SIGBREAK)) )
  308.            {
  309.            SetPointer(w,(short *)printer_pointer,PRINTERPTRHEIGHT,16L,0L,0L);
  310.            if( !open_prtdev() )
  311.           {
  312.           DumpRPort(&s->RastPort,&s->ViewPort,(UWORD)0,(UWORD)0,
  313.           hdr.w,hdr.h,0L,0L,(UWORD)(SPECIAL_FULLCOLS|SPECIAL_FULLROWS));
  314.           close_prtdev();
  315.           }
  316.            ClearPointer(w);
  317.            prt = 0;
  318.            CleanPort(w->UserPort);
  319.            }
  320.  
  321.         /* This saves the picture if the user presses the 'S' key. */
  322.         if( save && (!(signals & SIGBREAK)) )
  323.            {
  324.            SetPointer(w,(short *)disk_pointer,DISKPTRHEIGHT,16L,0L,0L);
  325.            if( rv = PutIFF_screen("ram:temp.pic",s) )
  326.           goto abort;
  327.            if( rv = PutProject("ram:temp.pic",toolname,
  328.             &FaceOff_img,&FaceOn_img,tooltypes,0L) )
  329.           goto abort;
  330.            ClearPointer(w);
  331.            save = 0;
  332.            CleanPort(w->UserPort);
  333.            }
  334.  
  335.         /* This writes an icon for the picture if 'I' is pressed. */
  336.         if( icn && (!(signals & SIGBREAK)) )
  337.            {
  338.            SetPointer(w,(short *)disk_pointer,DISKPTRHEIGHT,16L,0L,0L);
  339.            if( rv = PutProject(fname,toolname,
  340.             &FaceOff_img,&FaceOn_img,tooltypes,0L) )
  341.           goto abort;
  342.            ClearPointer(w);
  343.            icn = 0;
  344.            CleanPort(w->UserPort);
  345.            }
  346.         }
  347.  
  348.      if( signals & SIGBREAK )
  349.         {
  350.         rv = 10;
  351.         its_cool = 0;
  352.         }
  353.      }
  354.  
  355.       CleanPort(w->UserPort);
  356.       abort:
  357.       if( rv )
  358.      printf("Program Aborted code: %u\n",rv);
  359.       if( w ) CloseWindow(w);
  360.       if( s ) CloseScreen(s);
  361.       }
  362.    }
  363.  
  364. return(rv);
  365. }
  366.  
  367. main(argc,argv)
  368. #ifndef NOLIB
  369. long argc;    /* called by _main with 32 bit ints */
  370. #else
  371. short argc;
  372. #endif
  373. char *argv[];
  374. {
  375. extern void *OpenLibrary(), CloseLibrary();
  376.  
  377. extern short QueryIFF();
  378. extern struct WBStartup *WBenchMsg;
  379. char *ptr, *scdir(), *toolname = "c:shopict";
  380. register struct WBArg *wa;
  381. register short i, verbose;
  382. register int rv;
  383.  
  384. if( argc > 1 || argc == 0 )
  385.    {
  386.    if( !(GfxBase = OpenLibrary("graphics.library",0L)) )
  387.       goto abort;
  388.    if( !(IntuitionBase = OpenLibrary("intuition.library",0L)) )
  389.       goto abort;
  390.    if( !(IFFBase = OpenLibrary(IFFNAME,IFFVERSION)) )
  391.       goto abort;
  392.    if( !(IconBase = OpenLibrary(ICONNAME,0L)) )
  393.       goto abort;
  394.  
  395.     if( argc ) /* From CLI */
  396.       {
  397.       for( i = 1, verbose = 0; i < argc; i++)
  398.      {
  399.      if( argv[i][0] == '-' )
  400.         {
  401.         if( toupper(argv[i][1]) == 'V' )
  402.           {
  403.           verbose = 1;
  404.           continue;
  405.           }
  406.         else if( toupper(argv[i][1]) ==  'T' )
  407.           {
  408.           toolname = &argv[i][2];
  409.           continue;
  410.           }
  411.         }
  412.  
  413.      if( strpbrk(argv[i],"*?") )
  414.         {
  415.         while( ptr = scdir(argv[i]) )
  416.            if( rv = showpicture(ptr,toolname,verbose) )
  417.           goto abort;
  418.         }
  419.      else if( rv = showpicture(argv[i],toolname,verbose) )
  420.         goto abort;
  421.      }
  422.       }
  423.     else  /* From Workbench */
  424.       {
  425.       wa = WBenchMsg->sm_ArgList;
  426.       for( wa++, i = 1, verbose = 0; i < WBenchMsg->sm_NumArgs; i++, wa++)
  427.      {
  428.      if( wa->wa_Lock )
  429.         {
  430.         CurrentDir((struct FileLock *)wa->wa_Lock);
  431.         if( rv = showpicture(wa->wa_Name,toolname,verbose) )
  432.            break;
  433.         }
  434.      }
  435.       }
  436.    rv = 0;
  437.  
  438.    abort:
  439.    if( IconBase ) CloseLibrary(IconBase);
  440.    if( IFFBase ) CloseLibrary(IFFBase);
  441.    if( IntuitionBase ) CloseLibrary(IntuitionBase);
  442.    if( GfxBase ) CloseLibrary(GfxBase);
  443.    }
  444. else
  445.    helpme();
  446. _exit(rv);
  447. }
  448.