home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / iffconverter / loadsavepicture.c.bak < prev    next >
Text File  |  1997-01-07  |  14KB  |  320 lines

  1. /*
  2. **     $VER: LoadSavePicture.c V0.01 (19-06-95)
  3. **
  4. **     Author:  Gerben Venekamp
  5. **     Updates: 19-06-95  Version 0.01       Intial module
  6. **
  7. **  LoadSavePicture.c this is where a picture gets loaded or pictures
  8. **  get loaded. Also saving picture/pictures is handled here.
  9. **
  10. */
  11.  
  12.  
  13. #include <datatypes/pictureclass.h>
  14. #include <exec/execbase.h>
  15. #include <exec/memory.h>
  16. #include <exec/libraries.h>
  17. #include <proto/asl.h>
  18. #include <proto/dos.h>
  19. #include <proto/iffparse.h>
  20.  
  21. #include "IFFConverter.h"
  22.  
  23.  
  24. // Defining variables
  25. char * FileName;
  26. struct ExecBase *SysBase;
  27. BPTR LoadFileName_lock;
  28.  
  29.  
  30. // Defining prototypes
  31. void LoadPicture(enum FileModeType);
  32. static void GetAslFileNameLength(struct FileRequester *, ULONG *);
  33. BOOL GetFileName(char *, enum);
  34.  
  35.  
  36. /*
  37. **  LoadPicture(fm_FileMode)
  38. **
  39. **     Loads one or more pictures. When 'FileMode' is 'Single',
  40. **     'LoadPicture' will load a single picture and displays it. In all
  41. **     other cases 'LoadPicture' will load the pictures, ask the place
  42. **     to save them, convert and save them one by one.
  43. **
  44. **  pre:  fm_FileMode - Depending on 'fm_FileMode', 'LoadPicture' acts
  45. **                      differently. When 'fm_FileMode' equals:
  46. **                        Single   - An IFF ILBM picture is loaded and
  47. **                                   displayed. User is able to adjust the
  48. **                                   clipping reagon and then save the
  49. **                                   clipped picture.
  50. **                        Sequence - Loads a sequence of IFF ILBM pictures.
  51. **                                   File name is determined by a basename
  52. **                                   followed by a dot and then a number
  53. **                                   consisting out of 4 digits. You can
  54. **                                   select a clipping reagon for the first
  55. **                                   picture. All other pictures will use
  56. **                                   this clipping.
  57. **                        Multiple - Loads one or more IFF ILBM pictures
  58. **                                   and saves them automaticly. No clipping
  59. **                                   is possible.
  60. **                        Dir      - All files in a particular directory
  61. **                                   are tested for IFF ILBM. If so, then
  62. **                                   the picture is automaticly saved. So,
  63. **                                   no clipping is possible.
  64. **  post: None.
  65. **
  66. */
  67. void LoadPicture(enum FileModeType fm_FileMode)
  68. {
  69.    UWORD register Temp_Width;
  70.    UWORD register Temp_Height;
  71.    UWORD register Temp_Depth;
  72.    ULONG register Temp_Size;
  73.  
  74.    LONG rc;
  75.    struct IFFHandle *iff = NULL;
  76.  
  77.    if( GetFileName(FileName) )
  78.       {
  79.          if(iff = AllocIFF())
  80.          {
  81.             if(iff->iff_Stream = OpenFromLock(LoadFileName_lock))
  82.             {
  83.                InitIFFasDOS(iff);
  84.                if(!OpenIFF(iff, IFFF_READ))
  85.                {
  86.                   if(!ParseIFF(iff, IFFPARSE_STEP))
  87.                   {
  88.                      struct ContextNode *cn = NULL;
  89.                      
  90.                      if((cn = CurrentChunk(iff)) && (cn->cn_ID == ID_FORM))
  91.                      {
  92.                         long propArray[] = { ID_ILBM, ID_CAMG,
  93.                                              ID_ILBM, ID_BMHD,
  94.                                              ID_ILBM, ID_CMAP,
  95.                                              ID_ILBM, ID_BODY
  96.                         };
  97.                         
  98.                         if(!PropChunks(iff, propArray, 4))
  99.                         {
  100.                            if(!StopOnExit(iff, ID_ILBM, ID_FORM))
  101.                            {
  102.                               rc = ParseIFF(iff, IFFPARSE_SCAN);
  103.                               if(rc==0 || rc==IFFERR_EOC)
  104.                               {
  105.                                  struct StoredProperty *sp;
  106.                                  
  107.                                  if(sp = FindProp(iff, ID_ILBM, ID_BMHD))
  108.                                  {
  109.                                     struct BitMapHeader *BitMapHdr = sp->sp_Data;
  110.                                     
  111.                                     if(sp = FindProp(iff, ID_ILBM, ID_CAMG))
  112.                                     {
  113.                                        ULONG DisplayMode = *(ULONG *) sp->sp_Data;
  114.                                        
  115.                                        if(sp = FindProp(iff, ID_ILBM, ID_CMAP))
  116.                                        {
  117.                                           APTR CMapData = sp->sp_Data;
  118.                                           
  119.                                           if(sp = FindProp(iff, ID_ILBM, ID_BODY))
  120.                                           {
  121.                                              switch( RebuildViewScreen(BitMapHdr, DisplayMode, CMapData, sp->sp_Data) )
  122.                                              {
  123.                                                 case RVS_Okay:
  124.                                                    PictureValid = TRUE;
  125.                                                    
  126.                                                    Temp_Width  = BitMapHdr->bmh_Width;
  127.                                                    Temp_Height = BitMapHdr->bmh_Height;
  128.                                                    Temp_Depth  = BitMapHdr->bmh_Depth;
  129.                                                    Temp_Size   = ((Temp_Width + 7) >> 3) * Temp_Height * Temp_Depth;
  130.                                                    
  131.                                                    PicWidth  = Temp_Width;
  132.                                                    PicHeight = Temp_Height;
  133.                                                    PicSize   = ClipSize   = Temp_Size;
  134.                                                    PicDepth  = Temp_Depth;
  135.                                                    ClipLeft  = 0;
  136.                                                    ClipTop   = 0;
  137.                                                    
  138.                                                    ClipWidth  = Temp_Width  - 1;
  139.                                                    ClipHeight = Temp_Height - 1;
  140.                                                    
  141.                                                    // Mark as first clipping
  142.                                                    OldClipLeft = -1;
  143.                                                    
  144.                                                    UpdateDimentions(GD_PicWidth,   PicWidth,
  145.                                                                     GD_PicHeight,  PicHeight,
  146.                                                                     GD_PicDepth,   PicDepth,
  147.                                                                     GD_PicSize,    PicSize,
  148.                                                                     GD_ClipWidth,  ClipWidth + 1,
  149.                                                                     GD_ClipHeight, ClipHeight + 1,
  150.                                                                     GD_ClipLeft,   ClipLeft,
  151.                                                                     GD_ClipTop,    ClipTop,
  152.                                                                     GD_ClipSize,   ClipSize,
  153.                                                                     GD_Sentinal);
  154.                                                    
  155.                                                    UpdateGadgets(GD_Save,       &EnableGadget,
  156.                                                                  GD_ClipWidth,  &EnableGadget,
  157.                                                                  GD_ClipHeight, &EnableGadget,
  158.                                                                  GD_ClipLeft,   &EnableGadget,
  159.                                                                  GD_ClipTop,    &EnableGadget,
  160.                                                                  TAG_DONE);
  161.                                                    break;
  162.                                                 case RVS_PictureFailure:
  163.                                                 case RVS_NoWindow_PictureOkay:
  164.                                                 case RVS_NoWindow_PictureFailure:
  165.                                                 case RVS_BlackScreen:
  166.                                                 case RVS_NoScreen:
  167.                                                 case RVS_NoColourMap:;
  168.                                              }
  169.                                           }
  170.                                           else
  171.                                              ErrorHandler( IFFerror_NotFound, "BODY Chunk" );
  172.                                        }
  173.                                        else
  174.                                           ErrorHandler( IFFerror_NotFound, "CMAP Chunk" );
  175.                                     }
  176.                                     else
  177.                                        ErrorHandler( IFFerror_NotFound, "CMAG Chunk" );
  178.                                  }
  179.                                  else
  180.                                     ErrorHandler( IFFerror_NotFound, "BitMapHeader" );
  181.                               }
  182.                               else
  183.                                  ErrorHandler( IFFerror_Fail, "IFFParse" );
  184.                            }
  185.                            else
  186.                               ErrorHandler( IFFerror_Fail, "StopOnExit" );
  187.                         }
  188.                         else
  189.                            ErrorHandler( IFFerror_Fail, "PropChunks" );
  190.                      }
  191.                      else
  192.                         ErrorHandler( IFFerror_NoIFFErr, NULL );
  193.                   }
  194.                   else
  195.                      ErrorHandler( IFFerror_Fail, "ParseIFF" );
  196.                   
  197.                   CloseIFF(iff);
  198.                }
  199.                else
  200.                   ErrorHandler( IFFerror_Fail, "OpenIFF" );
  201.                
  202.                Close(iff->iff_Stream);
  203.             }
  204.             else
  205.                ErrorHandler( IFFerror_Fail, "OpenFromLock" );
  206.             
  207.             FreeIFF(iff);
  208.          }
  209.          else
  210.             ErrorHandler( IFFerror_Fail, "AllocIFF" );
  211.       }
  212.    // Remember that when a 'FileName' could not been gotten, a message
  213.    // has been displayed to notify the user. So no addtional actions
  214.    // are required at this stage.
  215.    
  216. }
  217.  
  218.  
  219. /*
  220. **  succes = GetFileName(FileName, gfn_Mode)
  221. **
  222. **     Gets the file name through an Asl requester. If 
  223. **
  224. **  pre:  gfn_Mode - Determine to get the load or save file name.
  225. **                   gfn_Load - Get the load file name.
  226. **                   gfn_Save - Get the save file name.
  227. **  post: FileName - Pointer to the FileName.
  228. **        succes - TRUE  if 'FileName' has been gotten,
  229. **                 FALSE if 'FileName' hasn't been gotten.
  230. **
  231. */
  232. BOOL GetFileName(char * FileName, enum gfn_Mode)
  233. {
  234.    BOOL Retry;
  235.    char * Source;
  236.    STRPTR Destination;
  237.    
  238.    do {
  239.       Retry = FALSE;
  240.       
  241.       if(AslRequest(Asl_FRLoad, NULL))
  242.       {
  243.          // Free possible allocated memory.
  244.          FreeThisMem(&LoadFileName, LoadFileNameSize);
  245.          
  246.          // Get the filename length inorder to create a buffer which
  247.          // will hold the entier filename, including its path.
  248.          GetAslFileNameLength(Asl_FRLoad, &LoadFileNameSize);
  249.          
  250.          // Allocate appropiate memory for filename buffer.
  251.          AllocThisMem(&LoadFileName, LoadFileNameSize, MEMF_CLEAR);
  252.          
  253.          // Setup 'Source' and 'Destination' for copying path.
  254.          Source = Asl_FRLoad->fr_Drawer;
  255.          Destination = LoadFileName;
  256.          
  257.          // Copy path of FileName.
  258.          while(* Destination++ = * Source++);
  259.          
  260.          // Concatenate 'Path' and 'FileName' to make one name.
  261.          AddPart( (STRPTR)LoadFileName, Asl_FRLoad->fr_File, LoadFileNameSize);
  262.          
  263.          if(!(LoadFileName_lock = Lock( (STRPTR)LoadFileName, ACCESS_READ)))
  264.             Retry=DisplayError(PanelWindow, NULL, ESW_Title, EST_LockErr, ESG_RetryCancel, LoadFileName);
  265.       }
  266.       else
  267.       {
  268.          // AslRequest returnd FALSE. Check for KickStart Version for addition checking.
  269.          if( SysBase->LibNode.lib_Version >= 38)
  270.          {
  271.             // KickStart V38+. Now we can check whether the user cancelled
  272.             // the job, or a problem caused AlsRequest to return FALSE.
  273.             switch( IoErr() )
  274.             {
  275.                case 0:
  276.                   // User cancelled the job.
  277.                   break;
  278.                case ERROR_NO_FREE_STORE:
  279.                   DisplayError(PanelWindow, NULL, ESW_Title, EST_AslNoFreeStore, ESG_Okay, NULL);
  280.                   CleanExit(RETURN_FAIL);
  281.                   break;
  282.                case ERROR_NO_MORE_ENTRIES:
  283.                   DisplayError(PanelWindow, NULL, ESW_Title, EST_AslNoMoreEntries, ESG_Okay, NULL);
  284.                   break;
  285.             }
  286.          }
  287.          // AslRequest just returned FALSE and pre V38 there's nothing we can do.
  288.          return(FALSE);
  289.       }
  290.    } while(Retry);
  291.  
  292.    return(TRUE);
  293. }
  294.  
  295.  
  296. /*
  297. **  GetAslFileNameLength(Asl_FileRequester, FileNameLength)
  298. **
  299. **     Calculate the length of a filename from an Asl_Requester.
  300. **
  301. **  pre:  Asl_FileRequester - Asl_FileRequester structure from which
  302. **                            to calculate the FileNameLength.
  303. **  post: FileNameLength - Calculated FileName length.
  304. **
  305. */
  306. static void GetAslFileNameLength(struct FileRequester *Asl_FileRequester, ULONG *FileNameLength)
  307. {
  308.    register UWORD i = 2;
  309.    char *Source = Asl_FRLoad->fr_File;
  310.  
  311.    while(*Source++ != 0)
  312.       i++;
  313.    
  314.    Source = Asl_FRLoad->fr_Drawer;
  315.    while(*Source++ != 0)
  316.       i++;
  317.    
  318.    *FileNameLength = (ULONG) i;
  319. }
  320.