home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / GFX / Converter / MPIMAGE51.LZX / si / ConvertMPImage.c
Encoding:
C/C++ Source or Header  |  1996-10-02  |  13.7 KB  |  496 lines

  1. /* Convert and display images */
  2.  
  3. /* mark@topic.demon.co.uk */
  4. /* mpaddock@cix.compulink.co.uk */
  5.  
  6. /* This is freely distributable */
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/dos.h>
  10. #include <proto/intuition.h>
  11. #include <proto/graphics.h>
  12. #include <proto/utility.h>
  13.  
  14. #include <dos/dos.h>
  15.  
  16. #include <math.h>
  17.  
  18. /* EGS Stuff    */
  19. #include <egs/clib/egs_protos.h>
  20. #include <egs/pragmas/egs_pragmas.h>
  21.  
  22. #include <egs/clib/egsintui_protos.h>
  23. #include <egs/pragmas/egsintui_pragmas.h>
  24.  
  25. #include <egs/clib/egsgfx_protos.h>
  26. #include <egs/pragmas/egsgfx_pragmas.h>
  27.  
  28. #include <egs/egsintui.h>
  29.  
  30. extern struct Library *EGSIntuiBase    = NULL;
  31. extern struct Library *EGSGfxBase    = NULL;
  32. extern struct Library *EGSBase        = NULL;
  33.  
  34. #include <libraries/MPImage.h>
  35. #include <pragmas/MPImage_pragmas.h>
  36. #include <clib/MPImage_protos.h>
  37. #include <dos.h>
  38. #include <string.h>
  39. #include <dos/rdargs.h>
  40.  
  41. #define TEMPLATE "FROM/A,X/N/K,Y/N/K,MINX/N/K,MINY/N/K,MAXX/N/K,MAXY/N/K,PUBSCREEN/K,EGS/S,NOREMAP/S,CLONE/S,TO/K,FORMAT/K,FORCEGREY/S,PALETTE/K,COLOURS/K/N,12BIT/S,LINEAR/S,WHITE0/S,MODENAME/K,NOPROGRESS/S,GUI/S"
  42.  
  43. #define OPT_FILE            0
  44. #define OPT_X                1
  45. #define OPT_Y                2
  46. #define OPT_MINX            3
  47. #define OPT_MINY            4
  48. #define OPT_MAXX            5
  49. #define OPT_MAXY            6
  50. #define OPT_SCREEN        7
  51. #define OPT_EGS            8
  52. #define OPT_NOREMAP        9
  53. #define OPT_CLONE            10
  54. #define OPT_TO                11
  55. #define OPT_FORMAT        12
  56. #define OPT_FORCEGREY    13
  57. #define OPT_PALETTE        14
  58. #define OPT_COLOURS        15
  59. #define OPT_12BIT            16
  60. #define OPT_LINEAR        17
  61. #define OPT_WHITE0        18
  62. #define OPT_MODENAME        19
  63. #define OPT_PROGRESS        20
  64. #define OPT_GUI            21
  65.  
  66. #define OPT_COUNT            22
  67.  
  68. extern long __oslibversion=39;
  69.  
  70. extern long __stack = 16000;
  71.  
  72. struct Library *MPImageBase;
  73.  
  74. const char Version[]="$VER: ConvertMPImage 5.1 (1.10.96)";
  75.  
  76. UBYTE NewMap256[256] = {  1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
  77.                                   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  78.                                   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  79.                                   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  80.                                   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  81.                                   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  82.                                   96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
  83.                                  112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
  84.                                  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  85.                                  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
  86.                                  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
  87.                                  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
  88.                                  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
  89.                                  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
  90.                                  224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
  91.                                  240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,  0
  92.                                 };
  93. UBYTE NewMap16[16] = {  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,  0
  94.                             };
  95.  
  96. int
  97. main(int argc,char **argv) {
  98.     BOOL FromWB = FALSE;
  99.     struct RDArgs *rdargs = NULL;
  100.     LONG opts[OPT_COUNT] = {
  101.         0
  102.     };
  103.     int resx = RETURN_OK;
  104.     struct EasyStruct es = {
  105.         sizeof(struct EasyStruct),
  106.         0,
  107.         "ConvertMPImage",
  108.         NULL,
  109.         "Ok"
  110.     };
  111.     struct MPImage *MPi;
  112.     struct Screen *screen;
  113.     struct Window *win;
  114.     struct IntuiMessage *msg;
  115.     BOOL done;
  116.     struct EI_NewWindow    EGS_NewWindow = {0};    // EGS New window
  117.     struct EI_Window        *EGS_Win;                // EGS Window
  118.     struct EI_EIntuiMsg     *EGSmsg;                    // Message from the EGS windows
  119.     ULONG newx=0,newy=0;
  120.  
  121.     if (argc == 0) {
  122.         argc = _WBArgc;
  123.         argv = _WBArgv;
  124.         FromWB = TRUE;
  125.         if (argc < 2) {
  126.             es.es_TextFormat = (char *)"Double Click with an Image to run";
  127.             EasyRequestArgs(NULL,&es,NULL,NULL);
  128.             return RETURN_WARN;
  129.         }
  130.         opts[OPT_FILE] = (LONG)argv[1];
  131.         
  132.     }
  133.     else {
  134.         if (!(rdargs = ReadArgs((char *)TEMPLATE, opts, NULL))) {
  135.             PrintFault(IoErr(), NULL);
  136.             return RETURN_ERROR;
  137.         }
  138.         if (opts[OPT_GUI]) {
  139.             FromWB=TRUE;
  140.         }
  141.     }
  142.     if (MPImageBase = OpenLibrary("MPImage.library",5)) {
  143.         SetMPImageScreen((char *)opts[OPT_SCREEN],opts[OPT_PROGRESS]?0:MPIF_PROGRESS);
  144.         if (opts[OPT_TO] || opts[OPT_FORMAT]) {
  145.             if (MPi=LoadMPImage((UBYTE *)opts[OPT_FILE],NULL,
  146.                                         (opts[OPT_FORCEGREY] ? MPIF_FORCEGREY : MPIF_GREY) |
  147.                                         (opts[OPT_LINEAR] ? MPIF_LINEARGREY : 0))) {
  148.                 if (opts[OPT_MINX]) {
  149.                     if (MPi->Width < *((ULONG *)opts[OPT_MINX])) {
  150.                         newx = *((ULONG *)opts[OPT_MINX]);
  151.                     }
  152.                 }
  153.                 if (opts[OPT_MAXX]) {
  154.                     if (MPi->Width > *((ULONG *)opts[OPT_MAXX])) {
  155.                         newx = *((ULONG *)opts[OPT_MAXX]);
  156.                     }
  157.                 }
  158.                 if (!opts[OPT_MINX] && !opts[OPT_MAXX]) {
  159.                     if (opts[OPT_X]) {
  160.                         newx = *((ULONG *)opts[OPT_X]);
  161.                     }
  162.                 }
  163.                 if (opts[OPT_MINY]) {
  164.                     if (MPi->Height < *((ULONG *)opts[OPT_MINY])) {
  165.                         newy = *((ULONG *)opts[OPT_MINY]);
  166.                     }
  167.                 }
  168.                 if (opts[OPT_MAXY]) {
  169.                     if (MPi->Height > *((ULONG *)opts[OPT_MAXY])) {
  170.                         newy = *((ULONG *)opts[OPT_MAXY]);
  171.                     }
  172.                 }
  173.                 if (!opts[OPT_MINY] && !opts[OPT_MAXY]) {
  174.                     if (opts[OPT_Y]) {
  175.                         newy = *((ULONG *)opts[OPT_Y]);
  176.                     }
  177.                 }
  178.                 if (newx || newy) {
  179.                     if (!newx) {
  180.                         newx = MPi->Width;
  181.                     }
  182.                     if (!newy) {
  183.                         newy = MPi->Height;
  184.                     }
  185.                     if (!RescaleMPImage(MPi,newx,newy)) {
  186.                         if (FromWB) {
  187.                             es.es_TextFormat = MPImageErrorMessage();
  188.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  189.                         }
  190.                         else {
  191.                             Printf(MPImageErrorMessage());
  192.                             Printf("\n");
  193.                         }
  194.                         resx = RETURN_FAIL;
  195.                     }
  196.                 }
  197.                 if (!resx) {
  198.                     UBYTE *GreyMap = NULL;
  199.                     if (opts[OPT_WHITE0]) {
  200.                         if (!opts[OPT_FORMAT] || !stricmp((UBYTE *)opts[OPT_FORMAT],MPI_BW16)) {
  201.                             GreyMap = NewMap16;
  202.                         }
  203.                         else {
  204.                             if (!stricmp((UBYTE *)opts[OPT_FORMAT],MPI_BW256)) {
  205.                                 GreyMap = NewMap256;
  206.                             }
  207.                         }
  208.                     }
  209.                     if (!SaveMPImage((UBYTE *)opts[OPT_TO],
  210.                                         MPi->Red, MPi->Green, MPi->Blue,
  211.                                         MPi->Width, MPi->Height,
  212.                                         MPIS_MODENAME,opts[OPT_MODENAME],
  213.                                         MPIS_FORMAT,opts[OPT_FORMAT],
  214.                                         MPIS_PALETTE,opts[OPT_PALETTE],
  215.                                         opts[OPT_COLOURS]?MPIS_COLOURS:TAG_IGNORE,opts[OPT_COLOURS] ? *((ULONG *)opts[OPT_COLOURS]) : 0,
  216.                                         MPIS_12BIT, opts[OPT_12BIT],
  217.                                         MPIS_LINEAR, opts[OPT_LINEAR],
  218.                                         MPIS_GREYMAP, GreyMap,
  219.                                         TAG_END)) {
  220.                         if (FromWB) {
  221.                             es.es_TextFormat = MPImageErrorMessage();
  222.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  223.                         }
  224.                         else {
  225.                             Printf(MPImageErrorMessage());
  226.                             Printf("\n");
  227.                         }
  228.                         resx = RETURN_FAIL;
  229.                     }
  230.                 }
  231.                 FreeMPImage(MPi);
  232.             }
  233.             else {
  234.                 if (FromWB) {
  235.                     es.es_TextFormat = MPImageErrorMessage();
  236.                     EasyRequestArgs(NULL,&es,NULL,NULL);
  237.                 }
  238.                 else {
  239.                     Printf(MPImageErrorMessage());
  240.                     Printf("\n");
  241.                 }
  242.                 resx = RETURN_FAIL;
  243.             }
  244.         }
  245.         else {
  246.             if (opts[OPT_EGS]) {
  247.                 if ((EGSBase = OpenLibrary((char *)"egs.library",0)) &&
  248.                      (EGSGfxBase = OpenLibrary((char *)"egsgfx.library",0)) &&
  249.                     (EGSIntuiBase = OpenLibrary((char *)"egsintui.library", 0))) {
  250.                    screen = NULL;
  251.                     if (MPi=LoadMPImage((UBYTE *)opts[OPT_FILE],screen,MPIF_EGS)) {
  252.                         if (opts[OPT_MINX]) {
  253.                             if (MPi->Width < *((ULONG *)opts[OPT_MINX])) {
  254.                                 newx = *((ULONG *)opts[OPT_MINX]);
  255.                             }
  256.                         }
  257.                         if (opts[OPT_MAXX]) {
  258.                             if (MPi->Width > *((ULONG *)opts[OPT_MAXX])) {
  259.                                 newx = *((ULONG *)opts[OPT_MAXX]);
  260.                             }
  261.                         }
  262.                         if (!opts[OPT_MINX] && !opts[OPT_MAXX]) {
  263.                             if (opts[OPT_X]) {
  264.                                 newx = *((ULONG *)opts[OPT_X]);
  265.                             }
  266.                         }
  267.                         if (opts[OPT_MINY]) {
  268.                             if (MPi->Height < *((ULONG *)opts[OPT_MINY])) {
  269.                                 newy = *((ULONG *)opts[OPT_MINY]);
  270.                             }
  271.                         }
  272.                         if (opts[OPT_MAXY]) {
  273.                             if (MPi->Height > *((ULONG *)opts[OPT_MAXY])) {
  274.                                 newy = *((ULONG *)opts[OPT_MAXY]);
  275.                             }
  276.                         }
  277.                         if (!opts[OPT_MINY] && !opts[OPT_MAXY]) {
  278.                             if (opts[OPT_Y]) {
  279.                                 newy = *((ULONG *)opts[OPT_Y]);
  280.                             }
  281.                         }
  282.                         if (newx || newy) {
  283.                             if (!newx) {
  284.                                 newx = MPi->Width;
  285.                             }
  286.                             if (!newy) {
  287.                                 newy = MPi->Height;
  288.                             }
  289.                             if (!RescaleMPImage(MPi,newx,newy)) {
  290.                                 if (FromWB) {
  291.                                     es.es_TextFormat = MPImageErrorMessage();
  292.                                     EasyRequestArgs(NULL,&es,NULL,NULL);
  293.                                 }
  294.                                 else {
  295.                                     Printf(MPImageErrorMessage());
  296.                                     Printf("\n");
  297.                                 }
  298.                                 resx = RETURN_FAIL;
  299.                             }
  300.                         }
  301.                         if (!resx) {
  302.                             EGS_NewWindow.LeftEdge = 0;
  303.                             EGS_NewWindow.TopEdge = 20;
  304.                             EGS_NewWindow.Width = MPi->Width;
  305.                             EGS_NewWindow.Height = MPi->Height;
  306.                             EGS_NewWindow.MinWidth = 20;
  307.                             EGS_NewWindow.MinHeight = 20;
  308.                             EGS_NewWindow.MaxWidth = MPi->Width;
  309.                             EGS_NewWindow.MaxHeight = MPi->Height;
  310.                             EGS_NewWindow.Screen = NULL;
  311.                             EGS_NewWindow.Bordef.SysGadgets = EI_WINDOWSIZE | EI_WINDOWFRONT | EI_WINDOWFLIP |
  312.                                                                          EI_WINDOWARROWL | EI_WINDOWARROWR | EI_WINDOWARROWU | EI_WINDOWARROWD |
  313.                                                                          EI_WINDOWSCROLLH | EI_WINDOWSCROLLV | EI_WINDOWDRAG |
  314.                                                                          EI_WINDOWCLOSE;
  315.                             EGS_NewWindow.FirstGadgets = NULL;
  316.                             EGS_NewWindow.Title = (char *)opts[OPT_FILE];
  317.                             EGS_NewWindow.Flags = EI_GIMMEZEROZERO | EI_SUPER_BITMAP | EI_WINDOWACTIVE | EI_REPORTMOUSE | EI_QUICKSCROLL | EI_RMBTRAP;
  318.                             EGS_NewWindow.IDCMPFlags = EI_iCLOSEWINDOW;
  319.                             EGS_NewWindow.Port = NULL;
  320.                             EGS_NewWindow.Menu = NULL;
  321.                             EGS_NewWindow.Render = NULL;
  322.                             if (EGS_Win = EI_OpenWindow(&EGS_NewWindow)) {
  323.                                 E_ActivateEGSScreen();
  324.                                 EG_CopyBitMapRastPort(MPi->EGS_BitMap,EGS_Win->RPort,0,0,MPi->Width,MPi->Height,0,0);
  325.                                 done = FALSE;
  326.                                 while    (!done) {
  327.                                     WaitPort(EGS_Win->UserPort);
  328.                                     while (EGSmsg = (struct EI_EIntuiMsg *)GetMsg(EGS_Win->UserPort)) {
  329.                                         switch (EGSmsg->Class) {
  330.                                         case EI_iCLOSEWINDOW:
  331.                                             done = TRUE;
  332.                                             break;
  333.                                         default:
  334.                                             break;
  335.                                         }
  336.                                         ReplyMsg((struct Message *)EGSmsg); 
  337.                                     }
  338.                                 }
  339.                                 EI_CloseWindow(EGS_Win);
  340.                             }
  341.                         }
  342.                         FreeMPImage(MPi);
  343.                     }
  344.                     else {
  345.                         if (FromWB) {
  346.                             es.es_TextFormat = MPImageErrorMessage();
  347.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  348.                         }
  349.                         else {
  350.                             Printf(MPImageErrorMessage());
  351.                             Printf("\n");
  352.                         }
  353.                         resx = RETURN_FAIL;
  354.                     }
  355.                 }
  356.                 if (EGSIntuiBase) {
  357.                     CloseLibrary(EGSIntuiBase);
  358.                 }
  359.                 if (EGSGfxBase) {
  360.                     CloseLibrary(EGSGfxBase);
  361.                 }
  362.                 if (EGSBase) {
  363.                     CloseLibrary(EGSBase);
  364.                 }
  365.             }
  366.             else {
  367.                 if (screen = LockPubScreen((UBYTE *)opts[OPT_SCREEN])) {
  368.                     if (MPi=LoadMPImage((UBYTE *)opts[OPT_FILE],screen,
  369.                                 (opts[OPT_CLONE] ? MPIF_CLONEBITMAP : 0) | 
  370.                                 (opts[OPT_NOREMAP] ? MPIF_NOREMAP : 0))) {
  371.                         if (opts[OPT_MINX]) {
  372.                             if (MPi->Width < *((ULONG *)opts[OPT_MINX])) {
  373.                                 newx = *((ULONG *)opts[OPT_MINX]);
  374.                             }
  375.                         }
  376.                         if (opts[OPT_MAXX]) {
  377.                             if (MPi->Width > *((ULONG *)opts[OPT_MAXX])) {
  378.                                 newx = *((ULONG *)opts[OPT_MAXX]);
  379.                             }
  380.                         }
  381.                         if (!opts[OPT_MINX] && !opts[OPT_MAXX]) {
  382.                             if (opts[OPT_X]) {
  383.                                 newx = *((ULONG *)opts[OPT_X]);
  384.                             }
  385.                         }
  386.                         if (opts[OPT_MINY]) {
  387.                             if (MPi->Height < *((ULONG *)opts[OPT_MINY])) {
  388.                                 newy = *((ULONG *)opts[OPT_MINY]);
  389.                             }
  390.                         }
  391.                         if (opts[OPT_MAXY]) {
  392.                             if (MPi->Height > *((ULONG *)opts[OPT_MAXY])) {
  393.                                 newy = *((ULONG *)opts[OPT_MAXY]);
  394.                             }
  395.                         }
  396.                         if (!opts[OPT_MINY] && !opts[OPT_MAXY]) {
  397.                             if (opts[OPT_Y]) {
  398.                                 newy = *((ULONG *)opts[OPT_Y]);
  399.                             }
  400.                         }
  401.                         if (newx || newy) {
  402.                             if (!newx) {
  403.                                 newx = MPi->Width;
  404.                             }
  405.                             if (!newy) {
  406.                                 newy = MPi->Height;
  407.                             }
  408.                             if (!RescaleMPImage(MPi,newx,newy)) {
  409.                                 if (FromWB) {
  410.                                     es.es_TextFormat = MPImageErrorMessage();
  411.                                     EasyRequestArgs(NULL,&es,NULL,NULL);
  412.                                 }
  413.                                 else {
  414.                                     Printf(MPImageErrorMessage());
  415.                                     Printf("\n");
  416.                                 }
  417.                                 resx = RETURN_FAIL;
  418.                             }
  419.                         }
  420.                         if (!resx) {
  421.                             if (win = OpenWindowTags(NULL,
  422.                                                         WA_CloseGadget, TRUE,
  423.                                                         WA_PubScreen, screen,
  424.                                                         WA_SuperBitMap, MPi->BitMap,
  425.                                                         WA_IDCMP, IDCMP_CLOSEWINDOW,
  426.                                                         WA_InnerWidth, MPi->Width,
  427.                                                         WA_InnerHeight, MPi->Height,
  428.                                                         WA_Title, opts[OPT_FILE],
  429.                                                         WA_DragBar, TRUE,
  430.                                                         WA_NoCareRefresh, TRUE,
  431.                                                         WA_AutoAdjust, TRUE,
  432.                                                         WA_GimmeZeroZero,TRUE,
  433.                                                         TAG_END)) {
  434.                                 done = FALSE;
  435.                                 while    (!done) {
  436.                                     WaitPort(win->UserPort);
  437.                                      while (msg = (struct IntuiMessage *)GetMsg(win->UserPort)) {
  438.                                          switch (msg->Class) {
  439.                                          case IDCMP_CLOSEWINDOW:
  440.                                              // Close window
  441.                                             done = TRUE;
  442.                                              break;
  443.                                          default:
  444.                                              // unknown message
  445.                                              break;
  446.                                              }
  447.                                         ReplyMsg((struct Message *)msg);
  448.                                      }
  449.                                 }    
  450.                                 CloseWindow(win);
  451.                             }
  452.                         }
  453.                         FreeMPImage(MPi);
  454.                     }
  455.                     else {
  456.                         if (FromWB) {
  457.                             es.es_TextFormat = MPImageErrorMessage();
  458.                             EasyRequestArgs(NULL,&es,NULL,NULL);
  459.                         }
  460.                         else {
  461.                             Printf(MPImageErrorMessage());
  462.                             Printf("\n");
  463.                         }
  464.                         resx = RETURN_FAIL;
  465.                     }
  466.                     UnlockPubScreen(NULL,screen);
  467.                 }
  468.                 else {
  469.                     if (FromWB) {
  470.                         es.es_TextFormat = "Failure locking Public Screen %s";
  471.                         EasyRequest(NULL,&es,NULL,(UBYTE *)opts[OPT_SCREEN]);
  472.                     }
  473.                     else {
  474.                         Printf("Failure locking Public Screen %s\n",(UBYTE *)opts[OPT_SCREEN]);
  475.                     }
  476.                 }
  477.             }
  478.         }
  479.         CloseLibrary(MPImageBase);
  480.     }
  481.     else {
  482.         if (FromWB) {
  483.             es.es_TextFormat = (char *)"Error opening MPImage.library";
  484.             EasyRequestArgs(NULL,&es,NULL,NULL);
  485.         }
  486.         else {
  487.             Printf((char *)"Error opening MPImage.library\n");
  488.         }
  489.         resx = RETURN_FAIL;
  490.     }
  491.     if (rdargs) {
  492.         FreeArgs(rdargs);
  493.     }
  494.     return resx;
  495. }
  496.