home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma23.dms / ma23.adf / Filer / Rexx / Picasso.filer < prev    next >
Text File  |  1994-03-11  |  23KB  |  582 lines

  1. /*
  2.     $VER: Picasso.filer 1.5 (10.03.94)
  3.  
  4.     Author:
  5.         Michael Böhnisch (billy@uni-paderborn.de)   (mb)
  6.         Matthias Scheler (tron@lyssa.pb.owl.de)     (ms)
  7.  
  8.     Function:
  9.         ViewGIF,  the  GIF  viewer that accompanies the Picasso II graphics
  10.         board,  often  selects  wrong resolutions when displaying oversized
  11.         pictures.   E.g.   I´ve  got  a  picture,  600×800 in size, ViewGIF
  12.         displays  on  a  1024×768  screen,  cropping off the lower 34 rows.
  13.         Picasso.filer  scans  the  GIF file for the picture size and forces
  14.         ViewGIF into a better resolution.
  15.  
  16.         ViewJPEG  uses  640×480×24  by default, regardless whether it crops
  17.         the  image  or not.  I prefer seeing the whole image when possible,
  18.         so Picasso.filer allows 16 or 8 bits per pixel resolutions.  Bigger
  19.         pictures  may  take a while to display, especially on unaccelerated
  20.         Amigas.  Stay patient.
  21.  
  22.         ViewIFF   does   not  show  Multi  Palette  pictures  (PCHG  chunk)
  23.         correctly.  Picasso.filer provides a fallback to an external viewer
  24.         when such pictures are selected.
  25.         Pixels  on  the  Picasso  II board usually are square shaped, or in
  26.         other  words  have  an  X/Y  aspect  ratio  of  1.  This results in
  27.         distorted picture displays for special view modes like  "NTSC:LoRes
  28.         Lace".
  29.         Picasso.filer handles this by supplying one of the keywords DOUBLEX
  30.         or  DOUBLEY  to  ViewIFF  as  appropriate.  For extreme distortions
  31.         (e.g.   pictures for PAL:SuperHighres non-interlaced) a fallback to
  32.         an  external  external  viewer  on  conservative  Amiga  screens is
  33.         choosen.
  34.  
  35.     Requires:
  36.         Picasso II graphics board
  37.         ViewGIF, ViewJPEG and ViewIFF supplied with Picasso II
  38.         VT  2.0  by Thomas Krehbiel or any other standard IFF viewer.  Must
  39.         support  PCHG  Multi  Palette  pictures  if  you  want to view this
  40.         picture type.
  41.  
  42.     Call:
  43.         Picasso TYPE FILE
  44.  
  45.         Where  TYPE is one of GIF, IFF or JPG.  Picasso.filer relies on the
  46.         correctness of this info and does no further file type checking.
  47.  
  48.     Example for "Filer.RC":
  49.         REXXCLASS "#?","GIF8","Picasso GIF %s"
  50.         REXXCLASS "#?","??????JFIF","Picasso JPG %s"
  51.         REXXCLASS "#?","FORM????ILBM","Picasso IFF %s"
  52.  
  53.     ToDo:
  54.         IFF pictures with aspect ratios outside the range from 0.35 to 2.83
  55.         are displayed on standard Amiga screens.  Maybe some tool should be
  56.         used  to do a scale operation on the picture first and then forward
  57.         it to the Picasso viewer.
  58.  
  59.     History:
  60.         06.02.94    1.0 Initial Release                             (mb)
  61.         07.02.94    1.1 IFF & JPEG added                            (mb)
  62.         08.02.94    1.2 fallback for HAM/EHB pictures removed       (ms)
  63.         09.02.94    1.3 removed VT dependency for GIF
  64.                         removed VT dependency for IFF, any viewer
  65.                         wil do now
  66.                         added IFF PCHG fallback
  67.                         added fallback for non-square aspect ratios (mb)
  68.         10.02.94    1.4 used ViewIFF keywords DOUBLEX and DOUBLEY
  69.                         for a wider range of pictures displayed on
  70.                         the Picasso II                              (mb)
  71.         10.03.94    1.5 removed VT dependency for JPEG
  72.                         no need for rexxsupport.library any longer
  73.                         added user confirmation for oversized JPEGs (mb)
  74. */
  75.  
  76. /* -------------------------------------------------------------------- */
  77. /* External commands. Adjust these to your individual settings.         */
  78. /* -------------------------------------------------------------------- */
  79.  
  80. vtcommand = "VT"
  81. vgcommand = "ViewGIF"
  82. vjcommand = "ViewJPEG"
  83. vicommand = "ViewIFF"
  84.  
  85. /* -------------------------------------------------------------------- */
  86.  
  87. OPTIONS RESULTS                     /* we need response from filer      */
  88.  
  89. PARSE ARG TYPE PICFILE              /* get arguments                    */
  90. TYPE    = STRIP( TYPE )
  91. PICFILE = STRIP( PICFILE )
  92.  
  93. /* -------------------------------------------------------------------- */
  94. /* Default to Filer's AReXX port and prevent Filer from quitting        */
  95. /* -------------------------------------------------------------------- */
  96.  
  97. ADDRESS 'FilerRexx'
  98.  
  99. LOCKFILER
  100. Key = RESULT
  101.  
  102. /* -------------------------------------------------------------------- */
  103. /* Get source dircetory name, append "/" if it is not a device name.    */
  104. /* Tag name of file to view.                                            */
  105. /* -------------------------------------------------------------------- */
  106.  
  107. GETSOURCEPATH
  108. SrcPath = RESULT
  109.  
  110. IF RIGHT(SrcPath, 1) = ':' THEN
  111.     FileName = SrcPath || STRIP( PICFILE, B, X2C(22) )
  112. ELSE
  113.     FileName = SrcPath || '/' || STRIP( PICFILE, B, X2C(22) )
  114.  
  115. SELECT
  116.  
  117.     /* ---------------------------------------------------------------- */
  118.     /* Show GIF picture                                                 */
  119.     /* ---------------------------------------------------------------- */
  120.  
  121.     WHEN TYPE = 'GIF' THEN DO
  122.  
  123.         /* ------------------------------------------------------------ */
  124.         /* determine ViewGIF RESOLUTION parameter and call viewer       */
  125.         /* ------------------------------------------------------------ */
  126.  
  127.         CALL GIFSize
  128.         PARSE VAR RESULT xsize ysize depth
  129.         xsize = STRIP( xsize )
  130.         ysize = STRIP( ysize )
  131.         depth = STRIP( depth )
  132.  
  133.         res = 1600
  134.         IF ( xsize <= 1280 ) & ( ysize <= 1024 ) THEN res = 1280
  135.         IF ( xsize <= 1152 ) & ( ysize <=  900 ) THEN res = 1152
  136.         IF ( xsize <= 1120 ) & ( ysize <=  832 ) THEN res = 1120
  137.         IF ( xsize <= 1024 ) & ( ysize <=  768 ) THEN res = 1024
  138.         IF ( xsize <=  800 ) & ( ysize <=  600 ) THEN res =  800
  139.         IF ( xsize <=  640 ) & ( ysize <=  480 ) THEN res =  640
  140.         IF ( xsize <=  320 ) & ( ysize <=  240 ) THEN res =  320
  141.  
  142.         HISTORY 'Picasso: GIF "' || FileName || '"' xsize || '×' || ysize || '×' || depth
  143.  
  144.         SHELL COMMAND vgcommand RESOLUTION res CENTER FileName '>NIL:'
  145.  
  146.     END
  147.  
  148.     /* ---------------------------------------------------------------- */
  149.     /* Show JPG picture                                                 */
  150.     /* ---------------------------------------------------------------- */
  151.  
  152.     WHEN TYPE = 'JPG' THEN DO
  153.  
  154.         /* ------------------------------------------------------------ */
  155.         /* Determine JPEG picture size                                  */
  156.         /* ------------------------------------------------------------ */
  157.  
  158.         CALL JPGSize
  159.         PARSE VAR RESULT xsize ysize depth
  160.         xsize = STRIP( xsize )
  161.         ysize = STRIP( ysize )
  162.         depth = STRIP( depth )
  163.  
  164.         /* ------------------------------------------------------------ */
  165.         /* choose maximum number of colors available without cropping   */
  166.         /* and call ViewJPG                                             */
  167.         /* ------------------------------------------------------------ */
  168.  
  169.         dep = '8BIT'
  170.         IF ( xsize <= 1152 ) & ( ysize <=  900 ) THEN dep = '16BIT'
  171.         IF ( xsize <=  800 ) & ( ysize <=  600 ) THEN dep = '24BIT'
  172.  
  173.         banner = xsize || '×' || ysize || '×' || depth
  174.  
  175.         HISTORY 'Picasso: JPG "' || FileName || '"' banner '(' || dep || ')'
  176.  
  177.         /* ------------------------------------------------------------ */
  178.         /* Ask for confirmation if picture is "on the big side"         */
  179.         /* ------------------------------------------------------------ */
  180.  
  181.         display = 1
  182.         IF xsize * ysize > 1000000 THEN DO
  183.             txt =        "This operation may take a long time.|"
  184.             txt = txt || "      Shall I continue anyway?"
  185.  
  186.             QUESTBOX txt
  187.             display = RESULT
  188.         END
  189.  
  190.         IF display = 1 THEN SHELL COMMAND vjcommand dep FileName '>NIL:'
  191.  
  192.     END
  193.  
  194.     /* ---------------------------------------------------------------- */
  195.     /* Show IFF ILBM picture                                            */
  196.     /* ---------------------------------------------------------------- */
  197.  
  198.     WHEN TYPE = IFF THEN DO
  199.  
  200.         /* ------------------------------------------------------------ */
  201.         /* determine size and depth of the picture                      */
  202.         /* ------------------------------------------------------------ */
  203.  
  204.         CALL IFFSize
  205.         PARSE VAR RESULT xsize ysize depth ratio
  206.         xsize = STRIP( xsize )
  207.         ysize = STRIP( ysize )
  208.         depth = STRIP( depth )
  209.         ratio = STRIP( ratio )
  210.  
  211.         banner = xsize || '×' || ysize || '×' || depth
  212.  
  213.         /* ------------------------------------------------------------ */
  214.         /* Does the IFF file contain a PCHG chunk? These pictures may   */
  215.         /* have a dedicated palette for each scanline and currently are */
  216.         /* not displayed correctly by ViewIFF.                          */
  217.         /* ------------------------------------------------------------ */
  218.  
  219.         CALL PCHGTest
  220.         test = RESULT
  221.  
  222.         SELECT
  223.  
  224.             WHEN test = 'ERRR' THEN DO
  225.  
  226.                 /* ---------------------------------------------------- */
  227.                 /* An error occured, inform user and exit               */
  228.                 /* ---------------------------------------------------- */
  229.  
  230.                 HISTORY 'Picasso: error processing' FileName
  231.  
  232.             END
  233.  
  234.             WHEN test = 'PCHG' THEN DO
  235.  
  236.                 /* ---------------------------------------------------- */
  237.                 /* Display Multi-Palette picture                        */
  238.                 /* ---------------------------------------------------- */
  239.  
  240.                 HISTORY 'Picasso: IFF "' || FileName || '"' banner '(PCHG)'
  241.                 SHELL COMMAND vtcommand FileName '>NIL:'
  242.  
  243.             END
  244.  
  245.             /* -------------------------------------------------------- */
  246.             /* do some correction of aspect ratios in number range      */
  247.             /* 0.35 = sqrt(0.5) ... 2.83 = 2 * sqrt(2)                  */
  248.             /* by ViewIFF options DOUBLEX and DOUBLEY                   */
  249.             /* -------------------------------------------------------- */
  250.  
  251.             WHEN ( ratio < 0.71 ) & ( ratio >= 0.35 ) THEN DO
  252.  
  253.                 /* ---------------------------------------------------- */
  254.                 /* Pixels are about half as wide than tall              */
  255.                 /* ---------------------------------------------------- */
  256.  
  257.                 HISTORY 'Picasso: IFF "' || FileName || '"' banner '(X:Y =' ratio || ')'
  258.                 SHELL COMMAND vicommand CENTER MODE DOUBLEY FileName '>NIL:'
  259.                 
  260.             END
  261.  
  262.             WHEN ( ratio < 1.41 ) & ( ratio >= 0.71 ) THEN DO
  263.  
  264.                 /* ---------------------------------------------------- */
  265.                 /* Take this for square, some distortion tolerated      */
  266.                 /* ---------------------------------------------------- */
  267.  
  268.                 HISTORY 'Picasso: IFF "' || FileName || '"' banner '(X:Y =' ratio || ')'
  269.                 SHELL COMMAND vicommand CENTER FileName '>NIL:'
  270.                 
  271.             END
  272.  
  273.             WHEN ( ratio < 2.83 ) & ( ratio >= 1.41 ) THEN DO
  274.  
  275.                 /* ---------------------------------------------------- */
  276.                 /* Pixels are about twice as wide than tall             */
  277.                 /* ---------------------------------------------------- */
  278.  
  279.                 HISTORY 'Picasso: IFF "' || FileName || '"' banner '(X:Y =' ratio || ')'
  280.                 SHELL COMMAND vicommand CENTER MODE DOUBLEX FileName '>NIL:'
  281.                 
  282.             END
  283.  
  284.             OTHERWISE DO
  285.  
  286.                 /* ---------------------------------------------------- */
  287.                 /* Fall back to externalviewer if outside aspect bounds */
  288.                 /* This should happen very rarely, e.g. for pictures in */
  289.                 /* Superhires without interlace (aspect ratio ~ 4) or   */
  290.                 /* Multiscan productivity with interlace (aspect ratio  */
  291.                 /* ~ 0.25)                                              */
  292.                 /* ---------------------------------------------------- */
  293.  
  294.                 HISTORY  'Picasso: IFF "' || FileName || '"' banner '(X:Y =' ratio || 'FB)'
  295.                 SHELL COMMAND vtcommand FileName '>NIL:'
  296.  
  297.             END
  298.  
  299.         END
  300.  
  301.     END
  302.  
  303.     /* ---------------------------------------------------------------- */
  304.     /* unknown type                                                     */
  305.     /* ---------------------------------------------------------------- */
  306.  
  307.     OTHERWISE DO
  308.  
  309.         /* ------------------------------------------------------------ */
  310.         /* Wrong type parameter, inform user                            */
  311.         /* ------------------------------------------------------------ */
  312.  
  313.         HISTORY 'Picasso: Type' TYPE 'not supported'
  314.  
  315.     END
  316.             
  317. END
  318.  
  319. /* -------------------------------------------------------------------- */
  320. /* re-allow quitting Filer                                              */
  321. /* -------------------------------------------------------------------- */
  322.  
  323. UNLOCKFILER Key
  324.  
  325. EXIT
  326.  
  327. /************************************************************************/
  328. /* JPGSize : subroutine parses a JFIF file and returns the picture's    */
  329. /*           dimensions                                                 */
  330. /************************************************************************/
  331.  
  332. JPGSize: PROCEDURE EXPOSE FileName
  333.  
  334.     M_SOF0 = X2C( C0 )      /* SOF code for arithmetic encoding         */
  335.     M_SOF1 = X2C( C1 )      /* SOF code for baseline implementation     */
  336.     M_SOF9 = X2C( C9 )      /* SOF code for non-baseline Huffmann file  */
  337.  
  338.     IF OPEN( jpg, FileName, 'READ' ) THEN DO
  339.  
  340.         /* ------------------------------------------------------------ */
  341.         /* skip over SOI marker                                         */
  342.         /* ------------------------------------------------------------ */
  343.  
  344.         SEEK( jpg, 2, 'BEGIN' )     /* point to JFIF APP0 block         */
  345.  
  346.         /* ------------------------------------------------------------ */
  347.         /* Loop through data blocks until EOF or a SOF block is found   */
  348.         /* ------------------------------------------------------------ */
  349.  
  350.         DO UNTIL EOF( jpg )
  351.  
  352.             IF READCH( jpg ) ~= X2C( FF ) THEN RETURN
  353.  
  354.             /* -------------------------------------------------------- */
  355.             /* Get block type marker                                    */
  356.             /* -------------------------------------------------------- */
  357.  
  358.             type = READCH( jpg )
  359.  
  360.             /* -------------------------------------------------------- */
  361.             /* Test for SOF block                                       */
  362.             /* -------------------------------------------------------- */
  363.  
  364.             IF type = M_SOF0 | type = M_SOF1 | type = M_SOF9 THEN DO
  365.  
  366.                 /* ---------------------------------------------------- */
  367.                 /* Skip to size information words                       */
  368.                 /* ---------------------------------------------------- */
  369.  
  370.                 SEEK( jpg, 3, 'CURRENT' )
  371.  
  372.                 /* ---------------------------------------------------- */
  373.                 /* read size information and return to calling proc     */
  374.                 /* ---------------------------------------------------- */
  375.  
  376.                 hi = C2D( READCH( jpg ) )
  377.                 lo = C2D( READCH( jpg ) )
  378.                 h  = lo + 256 * hi
  379.  
  380.                 hi = C2D( READCH( jpg ) )
  381.                 lo = C2D( READCH( jpg ) )
  382.                 w  = lo + 256 * hi
  383.  
  384.                 CLOSE( jpg )
  385.  
  386.                 RETURN w h 24
  387.             END
  388.  
  389.             /* -------------------------------------------------------- */
  390.             /* No SOF, skip to next marker                              */
  391.             /* -------------------------------------------------------- */
  392.  
  393.             hi = C2D( READCH( jpg ) )
  394.             lo = C2D( READCH( jpg ) )
  395.  
  396.             SEEK( jpg, lo + 256 * hi - 2, 'CURRENT' )
  397.         END
  398.  
  399.         CLOSE( jpg )
  400.  
  401.     END
  402.  
  403.     RETURN
  404.  
  405. /************************************************************************/
  406. /* GIFSize : subroutine parses a GIF file and returns the picture's     */
  407. /*           dimensions                                                 */
  408. /************************************************************************/
  409.  
  410. GIFSize: PROCEDURE EXPOSE FileName
  411.  
  412.     IF OPEN( gif, FileName, 'READ' ) THEN DO
  413.  
  414.         /* ------------------------------------------------------------ */
  415.         /* extract colour depth information                             */
  416.         /* ------------------------------------------------------------ */
  417.  
  418.         SEEK( gif, 10, 'BEGIN' )    /* point to resolution flag reg.    */
  419.  
  420.         rf = C2D( READCH( gif ) )   /* resolution flag register         */
  421.         r  = ( rf // 8 ) + 1        /* extract bits/pixel               */
  422.  
  423.         SEEK( gif, 13, 'BEGIN' )    /* point behind screen descriptor   */
  424.  
  425.         /* ------------------------------------------------------------ */
  426.         /* skip global colour map if present                            */
  427.         /* ------------------------------------------------------------ */
  428.  
  429.         IF rf >= 128 THEN SEEK( gif, 3 * 2**r, 'CURRENT' )
  430.  
  431.         /* ------------------------------------------------------------ */
  432.         /* skip extension block if present                              */
  433.         /* ------------------------------------------------------------ */
  434.  
  435.         IF READCH( gif ) = '!' THEN DO          /* read header          */
  436.  
  437.             SEEK( gif, 1, 'CURRENT' )           /* skip function code   */
  438.             DO UNTIL len = 0                    /* up to last block     */
  439.                 len = READCH( gif )             /* get data block size  */
  440.                 SEEK( gif, len, 'CURRENT' )     /* skip data block      */
  441.             END
  442.             SKIP( gif, 1, 'CURRENT' )           /* skip next header     */
  443.  
  444.         END
  445.             
  446.         /* ------------------------------------------------------------ */
  447.         /* The image descriptor block is reached, at last. Now let's    */
  448.         /* look for the actual image size...                            */
  449.         /* ------------------------------------------------------------ */
  450.  
  451.         ELSE DO
  452.  
  453.             SEEK( gif, 4, 'CURRENT' )           /* skip topleft coords  */
  454.  
  455.             /* -------------------------------------------------------- */
  456.             /* all numbers are stored in INTEL format, lo-byte first    */
  457.             /* -------------------------------------------------------- */
  458.  
  459.             lo = C2D( READCH( gif ) )           /* get width            */
  460.             hi = C2D( READCH( gif ) )
  461.             w  = lo + 256 * hi
  462.  
  463.             lo = C2D( READCH( gif ) )           /* get height           */
  464.             hi = C2D( READCH( gif ) )
  465.             h  = lo + 256 * hi
  466.  
  467.         END
  468.  
  469.         CLOSE( gif )
  470.  
  471.         RETURN w h r
  472.  
  473.     END
  474.  
  475.     RETURN
  476.  
  477. /************************************************************************/
  478. /* IFFSize : subroutine parses an IFF ILBM file and returns the picture */
  479. /*           size and pixel aspect ratio                                */
  480. /************************************************************************/
  481.  
  482. IFFSize: PROCEDURE EXPOSE FileName
  483.  
  484.     IF OPEN( iff, FileName, 'READ' ) THEN DO
  485.  
  486.         /* ------------------------------------------------------------ */
  487.         /* File starts with FORM????ILBM.                               */
  488.         /* ------------------------------------------------------------ */
  489.  
  490.         len = 12                    /* offset for first chunk in file   */
  491.  
  492.         DO UNTIL type = 'BMHD'
  493.  
  494.             /* -------------------------------------------------------- */
  495.             /* seek to next chunk                                       */
  496.             /* -------------------------------------------------------- */
  497.  
  498.             SEEK( iff, len, 'CURRENT' )
  499.  
  500.             /* -------------------------------------------------------- */
  501.             /* parse out chunk type and length                          */
  502.             /* -------------------------------------------------------- */
  503.  
  504.             type = READCH( iff, 4 )
  505.             len  =             C2D( READCH( iff ) )
  506.             len  = len * 256 + C2D( READCH( iff ) )
  507.             len  = len * 256 + C2D( READCH( iff ) )
  508.             len  = len * 256 + C2D( READCH( iff ) )
  509.  
  510.             IF len // 2 = 1 THEN len = len + 1  /* add 1 for odd length */
  511.  
  512.         END
  513.  
  514.         /* ------------------------------------------------------------ */
  515.         /* We're now positioned on a BMHD chunk.                        */
  516.         /* ------------------------------------------------------------ */
  517.  
  518.         hi = C2D( READCH( iff ) )   /* read image width                 */
  519.         lo = C2D( READCH( iff ) )
  520.         w  = lo + 256 * hi
  521.  
  522.         hi = C2D( READCH( iff ) )   /* read image height                */
  523.         lo = C2D( READCH( iff ) )
  524.         h  = lo + 256 * hi
  525.  
  526.         SEEK( iff, 4, 'CURRENT' )   /* seek to nPlanes entry            */
  527.  
  528.         d  = C2D( READCH( iff ) )   /* number of bitplanes (depth)      */
  529.  
  530.         SEEK( iff, 5, 'CURRENT' )   /* seek to aspect ratio entries     */
  531.  
  532.         xa = C2D( READCH( iff ) )   /* xAspect                          */
  533.         ya = C2D( READCH( iff ) )   /* yAspect                          */
  534.  
  535.         NUMERIC DIGITS 3            /* sometimes less is more...        */
  536.         a  = xa / ya                /* Aspect ratio                     */
  537.         NUMERIC DIGITS
  538.  
  539.         CLOSE( iff )                /* clean up                         */
  540.  
  541.         RETURN w h d a
  542.     END
  543.  
  544.     RETURN
  545.  
  546. /************************************************************************/
  547. /* PCHGTest : Returns true if IFFFile contains a PCHG chunk and thus,   */
  548. /*            is not correctly displayed by ViewIFF.                    */
  549. /************************************************************************/
  550.  
  551. PCHGTest: PROCEDURE EXPOSE FileName
  552.  
  553.     IF OPEN( iff, FileName, "READ" ) THEN DO
  554.  
  555.         len = 12
  556.  
  557.         DO UNTIL ( Type = 'PCHG' ) | EOF( iff )
  558.  
  559.             SEEK( iff, len, 'CURRENT' )
  560.  
  561.             type = READCH( iff, 4 )
  562.             b1   = C2D( READCH( iff ) ) * 2**24
  563.             b2   = C2D( READCH( iff ) ) * 2**16
  564.             b3   = C2D( READCH( iff ) ) * 2**8
  565.             b4   = C2D( READCH( iff ) )
  566.             len  = b1 + b2 + b3 + b4
  567.  
  568.             IF len // 2 = 1 THEN len = len + 1  /* add 1 for odd length */
  569.  
  570.         END
  571.  
  572.         CLOSE( iff )
  573.  
  574.         IF Type = 'PCHG' THEN
  575.             RETURN 'PCHG'
  576.         ELSE
  577.             RETURN 'NORM'
  578.  
  579.     END
  580.  
  581.     RETURN 'ERROR'
  582.