home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 98 / af098a.adf / archives / HDProgs.lzx / AGA-Morph-V1.3 / Source / AGA-Morph-Disk.c < prev    next >
C/C++ Source or Header  |  1997-04-14  |  34KB  |  1,009 lines

  1. /*----------------------------------------------------------------------------*
  2.  *                                                                            *
  3.  *  AGA-Morph-Disk.c V1.3                                                     *
  4.  *                                                                            *
  5.  *----------------------------------------------------------------------------*/
  6.  
  7. /*----------------------------------------------------------------------------*/
  8.  
  9. #include <libraries/diskfont.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/classes.h>
  12. #include <intuition/pointerclass.h>
  13. #include <datatypes/pictureclass.h>
  14. #include <datatypes/pictureclassext.h>
  15. #include <graphics/gfx.h>
  16. #include <exec/memory.h>
  17. #include <libraries/asl.h>
  18. #include <clib/macros.h>
  19.  
  20. #include "AGA-Morph-Language.h"
  21. #include "AGA-Morph-Struct.h"
  22.  
  23. /*----------------------------------------------------------------------------*/
  24.  
  25. extern struct BitMap *BitMap;
  26. extern struct BitMapHeader *BitMapHeader;
  27. extern struct DPoint Points;
  28. extern struct IntuiText FileError,LoadPicError,MemoryError;
  29. extern struct MPoint *Point1,*Point2;
  30. extern struct RastPort *VirtRast;
  31. extern struct Window *CommandWindow;
  32. extern BPTR file;
  33. extern char *Name;
  34. extern LONG NumColours,x,xmax,y,ymax,i,j,k;
  35. extern Object LPicture;
  36. extern UBYTE Compr,*PlaneBuf[24],*Colours;
  37. extern ULONG b,c,g,ModeID,r,*PixelBuf;
  38.  
  39. /*----------------------------------------------------------------------------*/
  40.  
  41. void LoadPoints();
  42. void SavePoints();
  43. void RemLines(ULONG num);                                /* Internal use only */
  44. BYTE AllocLines(ULONG num,ULONG w);                      /* Internal use only */
  45. BYTE ReadLine(ULONG num,ULONG w);                        /* Internal use only */
  46. BYTE LoadIlbm(struct Pic *Picture,char *Name);           /* Internal use only */
  47. void LoadPic(struct Pic *Picture,STRPTR Title);
  48. void SavePic(struct Pic *Picture,STRPTR Title);
  49.  
  50. /*----------------------------------------------------------------------------*/
  51.  
  52. void
  53. LoadPoints()
  54. {
  55.   struct FileLock *lock;
  56.   struct request *FileRequester;
  57.   BPTR File;
  58.   struct FPoint Buff;
  59.   if (FileRequester=(struct request *)AllocAslRequestTags(ASL_FileRequest,
  60.                                                           ASLFR_TitleText,  Load_Points,
  61.                                                           ASLFR_Window,     CommandWindow,
  62.                                                           ASLFR_DoPatterns, TRUE,
  63.                                                           TAG_DONE))
  64.   {
  65.     if (AslRequest(FileRequester,0))
  66.     {
  67.       DeletePoints();
  68.       lock=(struct FileLock *)CurrentDir(Lock(((struct FileRequester *) FileRequester)->rf_Dir,ACCESS_READ));
  69.       Name=((struct FileRequester *) FileRequester)->rf_File;
  70.       if (File=Open(Name,MODE_OLDFILE))
  71.       {
  72.         if (Read(File,&Buff,sizeof(Buff)))
  73.         {
  74.           if (Points.First=(struct MPoint *)AllocVec(sizeof(struct MPoint),0))
  75.           {
  76.             Point1=Points.First;
  77.             Point1->P=Buff;
  78.             Point1->Next=0;
  79.             Points.NumPoints=1;
  80.           }
  81.           else
  82.           {
  83.             ShowError(&MemoryError);
  84.           }
  85.           while(Read(File,&Buff,sizeof(Buff)))
  86.           {
  87.             if (Point1->Next=(struct MPoint *)AllocVec(sizeof(struct MPoint),0))
  88.             {
  89.               Point1=Point1->Next;
  90.               Point1->P=Buff;
  91.               Point1->Next=0;
  92.               Points.NumPoints++;
  93.             }
  94.             else
  95.             {
  96.               ShowError(&MemoryError);
  97.             }
  98.           }
  99.         }
  100.         Close(File);
  101.       }
  102.       else
  103.       {
  104.         ShowError(&FileError);
  105.       }
  106.       UnLock(CurrentDir(lock));
  107.     }
  108.     FreeAslRequest(FileRequester);
  109.   }
  110.   else
  111.   {
  112.     ShowError(&MemoryError);
  113.   }
  114.   RethinkSelectedPoint();
  115. }
  116.  
  117. /*----------------------------------------------------------------------------*/
  118.  
  119. void
  120. SavePoints()
  121. {
  122.   struct request *FileRequester;
  123.   struct FileLock *lock;
  124.   BPTR File;
  125.   if (FileRequester=(struct request *)AllocAslRequestTags(ASL_FileRequest,
  126.                                                           ASLFR_TitleText,  Save_Points,
  127.                                                           ASLFR_Window,     CommandWindow,
  128.                                                           ASLFR_DoPatterns, TRUE,
  129.                                                           TAG_DONE))
  130.   {
  131.     if (AslRequest(FileRequester,0))
  132.     {
  133.       lock=(struct FileLock *)CurrentDir(Lock(((struct FileRequester *) FileRequester)->rf_Dir,ACCESS_READ));
  134.       Name=((struct FileRequester *) FileRequester)->rf_File;
  135.       if (File=Open(Name,MODE_NEWFILE))
  136.       {
  137.         Point1=Points.First;
  138.         while(Point1)
  139.         {
  140.           Write(File,&(Point1->P),sizeof(Point1->P));
  141.           Point1=Point1->Next;
  142.         }
  143.         Close(File);
  144.       }
  145.       else
  146.       {
  147.         ShowError(&FileError);
  148.       }
  149.       UnLock(CurrentDir(lock));
  150.     }
  151.     FreeAslRequest(FileRequester);
  152.   }
  153.   else
  154.   {
  155.     ShowError(&MemoryError);
  156.   }
  157. }
  158.  
  159. /*----------------------------------------------------------------------------*/
  160.  
  161. void
  162. RemLines(ULONG num)
  163. {
  164.   for (i=0;i<num;i++)
  165.   {
  166.     FreeVec(PlaneBuf[i]);
  167.     PlaneBuf[i]=0;
  168.   }
  169.   if (PixelBuf)
  170.   {
  171.     FreeVec(PixelBuf);
  172.     PixelBuf=0;
  173.   }
  174. }
  175.  
  176. /*----------------------------------------------------------------------------*/
  177.  
  178. BYTE
  179. AllocLines(ULONG num,ULONG w)
  180. {
  181.   for (i=0;i<num;i++)
  182.   {
  183.     if (!(PlaneBuf[i]=(UBYTE *)AllocVec(((w+15) >> 4) << 1,0)))
  184.     {
  185.       if (i>0) RemLines(i);
  186.       return 0;
  187.     }
  188.   }
  189.   if (!(PixelBuf=(ULONG *)AllocVec(((w+15) >> 4) << 6,0)))
  190.   {
  191.     RemLines(num);
  192.     return 0;
  193.   }
  194.   return 1;
  195. }
  196.  
  197. /*----------------------------------------------------------------------------*/
  198.  
  199. BYTE
  200. ReadLine(ULONG num,ULONG w)
  201. {
  202.   LONG ww,l;
  203.   BYTE code;
  204.   UBYTE color;
  205.   WORD cod;
  206.   char text[10];
  207.   ww=((w+15) >> 4) << 1;
  208.   if (Compr)
  209.   {
  210.     for (j=0;j<num;j++)
  211.     {
  212.       k=0;
  213.       while (k<ww)
  214.       {
  215.         Read(file,&code,1);
  216.         if (code>=0)
  217.         {
  218.           cod=code+1;
  219.           if (k+cod<=ww)
  220.           {
  221.             Read(file,(PlaneBuf[j]+k),cod);
  222.           }
  223.           k=k+cod;
  224.         }
  225.         else
  226.         {
  227.           if (code==-128)
  228.           {
  229.             /* NOP */
  230.           }
  231.           else
  232.           {
  233.             cod=1-code;
  234.             Read(file,&color,1);
  235.             if (k+cod<=ww)
  236.             {
  237.               for (l=0;l<cod;l++)
  238.               {
  239.                 *(PlaneBuf[j]+k+l)=color;
  240.               }
  241.             }
  242.             k=k+cod;
  243.           }
  244.         }
  245.       }
  246.       if (k>ww)
  247.       {
  248.         return 0;
  249.       }
  250.     }
  251.   }
  252.   else
  253.   {
  254.     for (j=0;j<num;j++)
  255.     {
  256.       Read(file,PlaneBuf[j],ww);
  257.     }
  258.   }
  259.   for (j=0;j<ww;j++)
  260.   {
  261.     *(PixelBuf+j*8)=0;
  262.     *(PixelBuf+j*8+1)=0;
  263.     *(PixelBuf+j*8+2)=0;
  264.     *(PixelBuf+j*8+3)=0;
  265.     *(PixelBuf+j*8+4)=0;
  266.     *(PixelBuf+j*8+5)=0;
  267.     *(PixelBuf+j*8+6)=0;
  268.     *(PixelBuf+j*8+7)=0;
  269.     for (k=0;k<num;k++)
  270.     {
  271.       *(PixelBuf+j*8)=(*(PixelBuf+j*8)) | (*(PlaneBuf[k]+j) >> 7) << k;
  272.       *(PixelBuf+j*8+1)=(*(PixelBuf+j*8+1)) | ((*(PlaneBuf[k]+j) >> 6) & 1) << k;
  273.       *(PixelBuf+j*8+2)=(*(PixelBuf+j*8+2)) | ((*(PlaneBuf[k]+j) >> 5) & 1) << k;
  274.       *(PixelBuf+j*8+3)=(*(PixelBuf+j*8+3)) | ((*(PlaneBuf[k]+j) >> 4) & 1) << k;
  275.       *(PixelBuf+j*8+4)=(*(PixelBuf+j*8+4)) | ((*(PlaneBuf[k]+j) >> 3) & 1) << k;
  276.       *(PixelBuf+j*8+5)=(*(PixelBuf+j*8+5)) | ((*(PlaneBuf[k]+j) >> 2) & 1) << k;
  277.       *(PixelBuf+j*8+6)=(*(PixelBuf+j*8+6)) | ((*(PlaneBuf[k]+j) >> 1) & 1) << k;
  278.       *(PixelBuf+j*8+7)=(*(PixelBuf+j*8+7)) | (*(PlaneBuf[k]+j) & 1) << k;
  279.     }
  280.   }
  281.   return 1;
  282. }
  283.  
  284. /*----------------------------------------------------------------------------*/
  285.  
  286. BYTE
  287. LoadIlbm(struct Pic *Picture,char *Name)
  288. {
  289.   char text[10];
  290.   UBYTE Buffer[5],Planes,ret;
  291.   ULONG Chunk;
  292.   WORD WBuffer[2],xdim,ydim;
  293.   LONG Bmhd,Camg,Cmap,Body,Length,FLength,Pos;
  294.   Bmhd=0; Camg=0; Cmap=0; Body=0;
  295.   if (file=Open(Name,MODE_OLDFILE))
  296.   {
  297.     Read(file,&Chunk,4);
  298.     if (Chunk==0x464F524D)
  299.     {
  300.       Read(file,&FLength,4);
  301.       Read(file,&Chunk,4);
  302.       if (Chunk==0x494C424D)
  303.       {
  304.         Pos=12;
  305.         FLength=FLength-4;
  306.         while((!Body) && (FLength>0))
  307.         {
  308.           Read(file,&Chunk,4);
  309.           Pos=Pos+8;
  310.           FLength=FLength-8;
  311.           if (Chunk==0x424D4844) Bmhd=Pos;
  312.           if (Chunk==0x43414D47) Camg=Pos;
  313.           if (Chunk==0x434D4150) Cmap=Pos;
  314.           if (Chunk==0x424F4459) Body=Pos;
  315.           Read(file,&Length,4);
  316.           Length=((Length+1) >> 1) << 1;
  317.           Seek(file,Length,OFFSET_CURRENT);
  318.           Pos=Pos+Length;
  319.           FLength=FLength-Length;
  320.         }
  321.         if ((Bmhd) && (Body))
  322.         {
  323.           if (Camg)
  324.           {
  325.             Seek(file,Camg,OFFSET_BEGINNING);
  326.             Read(file,&ModeID,4);
  327.           }
  328.           else
  329.           {
  330.             ModeID=0;
  331.           }
  332.           Seek(file,Bmhd,OFFSET_BEGINNING);
  333.           Read(file,&WBuffer[0],4);
  334.           xdim=WBuffer[0];
  335.           ydim=WBuffer[1];
  336.           Read(file,&Buffer[0],4);
  337.           Read(file,&Buffer[0],4);
  338.           Planes=Buffer[0];
  339.           if (Planes>24) Planes=24;
  340.           Compr=Buffer[2];
  341.           if (AllocLines(Planes,xdim))
  342.           {
  343.             if (Planes==24)
  344.             {
  345.               if (Picture->r)
  346.               {
  347.                 FreeVec(Picture->r);
  348.                 Picture->r=0;
  349.               }
  350.               if (Picture->g)
  351.               {
  352.                 FreeVec(Picture->g);
  353.                 Picture->g=0;
  354.               }
  355.               if (Picture->b)
  356.               {
  357.                 FreeVec(Picture->b);
  358.                 Picture->b=0;
  359.               }
  360.               Picture->width=xdim;
  361.               Picture->height=ydim;
  362.               Seek(file,Body,OFFSET_BEGINNING);
  363.               if (Picture->r=(UBYTE *)AllocVec(xdim*ydim,0))
  364.               {
  365.                 if (Picture->g=(UBYTE *)AllocVec(xdim*ydim,0))
  366.                 {
  367.                   if (Picture->b=(UBYTE *)AllocVec(xdim*ydim,0))
  368.                   {
  369.                     for (i=0;i<ydim;i++)
  370.                     {
  371.                       if (!(ReadLine(Planes,xdim)))
  372.                       {
  373.                         Close(file);
  374.                         return 0;
  375.                       }
  376.                       for (j=0;j<xdim;j++)
  377.                       {
  378.                         *(Picture->b+j+i*xdim)=(*(PixelBuf+j) >> 16) & 0xFF;
  379.                         *(Picture->g+j+i*xdim)=(*(PixelBuf+j) >> 8) & 0xFF;
  380.                         *(Picture->r+j+i*xdim)=*(PixelBuf+j) & 0xFF;
  381.                       }
  382.                     }
  383.                   }
  384.                   else
  385.                   {
  386.                     FreeVec(Picture->r); Picture->r=0;
  387.                     FreeVec(Picture->g); Picture->g=0;
  388.                     Picture->width=0;
  389.                     Picture->height=0;
  390.                     ShowError(&MemoryError);
  391.                   }
  392.                 }
  393.                 else
  394.                 {
  395.                   FreeVec(Picture->r); Picture->r=0;
  396.                   Picture->width=0;
  397.                   Picture->height=0;
  398.                   ShowError(&MemoryError);
  399.                 }
  400.               }
  401.               else
  402.               {
  403.                 Picture->width=0;
  404.                 Picture->height=0;
  405.                 ShowError(&MemoryError);
  406.               }
  407.               ret=1;
  408.             }
  409.             else
  410.             {
  411.               if (Cmap)
  412.               {
  413.                 Seek(file,Cmap,OFFSET_BEGINNING);
  414.                 NumColours=2 << Planes;
  415.                 if (Colours=(UBYTE *)AllocVec(3*NumColours,0))
  416.                 {
  417.                   Read(file,Colours,NumColours*3);
  418.                   if (Picture->r)
  419.                   {
  420.                     FreeVec(Picture->r);
  421.                     Picture->r=0;
  422.                   }
  423.                   if (Picture->g)
  424.                   {
  425.                     FreeVec(Picture->g);
  426.                     Picture->g=0;
  427.                   }
  428.                   if (Picture->b)
  429.                   {
  430.                     FreeVec(Picture->b);
  431.                     Picture->b=0;
  432.                   }
  433.                   Picture->width=xdim;
  434.                   Picture->height=ydim;
  435.                   Seek(file,Body,OFFSET_BEGINNING);
  436.                   if (Picture->r=(UBYTE *)AllocVec(xdim*ydim,0))
  437.                   {
  438.                     if (Picture->g=(UBYTE *)AllocVec(xdim*ydim,0))
  439.                     {
  440.                       if (Picture->b=(UBYTE *)AllocVec(xdim*ydim,0))
  441.                       {
  442.                         for (i=0;i<ydim;i++)
  443.                         {
  444.                           if (!(ReadLine(Planes,xdim)))
  445.                           {
  446.                             Close(file);
  447.                             return 0;
  448.                           }
  449.                           if (ModeID & HAM_KEY)
  450.                           {
  451.                             r=(*(Colours) >> 16) & 0xFF;
  452.                             g=(*(Colours) >> 8) & 0xFF;
  453.                             b=*(Colours) & 0xFF;
  454.                             for (j=0;j<xdim;j++)
  455.                             {
  456.                               if (Planes==6)
  457.                               {
  458.                                 if (*(PixelBuf+j)<16)
  459.                                 {
  460.                                   r=*(Colours+*(PixelBuf+j)*3);
  461.                                   g=*(Colours+*(PixelBuf+j)*3+1);
  462.                                   b=*(Colours+*(PixelBuf+j)*3+2);
  463.                                   *(Picture->r+j+i*xdim)=r;
  464.                                   *(Picture->g+j+i*xdim)=g;
  465.                                   *(Picture->b+j+i*xdim)=b;
  466.                                 }
  467.                                 else
  468.                                 {
  469.                                   if (*(PixelBuf+j)<32)
  470.                                   {
  471.                                     b=(*(PixelBuf+j)-16) << 4;
  472.                                     *(Picture->r+j+i*xdim)=r;
  473.                                     *(Picture->g+j+i*xdim)=g;
  474.                                     *(Picture->b+j+i*xdim)=b;
  475.                                   }
  476.                                   else
  477.                                   {
  478.                                     if (*(PixelBuf+j)<48)
  479.                                     {
  480.                                       r=(*(PixelBuf+j)-32) << 4;
  481.                                       *(Picture->r+j+i*xdim)=r;
  482.                                       *(Picture->g+j+i*xdim)=g;
  483.                                       *(Picture->b+j+i*xdim)=b;
  484.                                     }
  485.                                     else
  486.                                     {
  487.                                       g=(*(PixelBuf+j)-48) << 4;
  488.                                       *(Picture->r+j+i*xdim)=r;
  489.                                       *(Picture->g+j+i*xdim)=g;
  490.                                       *(Picture->b+j+i*xdim)=b;
  491.                                     }
  492.                                   }
  493.                                 }
  494.                               }
  495.                               else
  496.                               {
  497.                                 if (*(PixelBuf+j)<64)
  498.                                 {
  499.                                   r=*(Colours+*(PixelBuf+j)*3);
  500.                                   g=*(Colours+*(PixelBuf+j)*3+1);
  501.                                   b=*(Colours+*(PixelBuf+j)*3+2);
  502.                                   *(Picture->r+j+i*xdim)=r;
  503.                                   *(Picture->g+j+i*xdim)=g;
  504.                                   *(Picture->b+j+i*xdim)=b;
  505.                                 }
  506.                                 else
  507.                                 {
  508.                                   if (*(PixelBuf+j)<128)
  509.                                   {
  510.                                     b=(*(PixelBuf+j)-64) << 2;
  511.                                     *(Picture->r+j+i*xdim)=r;
  512.                                     *(Picture->g+j+i*xdim)=g;
  513.                                     *(Picture->b+j+i*xdim)=b;
  514.                                   }
  515.                                   else
  516.                                   {
  517.                                     if (*(PixelBuf+j)<192)
  518.                                     {
  519.                                       r=(*(PixelBuf+j)-128) << 2;
  520.                                       *(Picture->r+j+i*xdim)=r;
  521.                                       *(Picture->g+j+i*xdim)=g;
  522.                                       *(Picture->b+j+i*xdim)=b;
  523.                                     }
  524.                                     else
  525.                                     {
  526.                                       g=(*(PixelBuf+j)-192) << 2;
  527.                                       *(Picture->r+j+i*xdim)=r;
  528.                                       *(Picture->g+j+i*xdim)=g;
  529.                                       *(Picture->b+j+i*xdim)=b;
  530.                                     }
  531.                                   }
  532.                                 }
  533.                               }
  534.                             }
  535.                           }
  536.                           else
  537.                           {
  538.                             for (j=0;j<xdim;j++)
  539.                             {
  540.                               *(Picture->r+j+i*xdim)=*(Colours+*(PixelBuf+j)*3);
  541.                               *(Picture->g+j+i*xdim)=*(Colours+*(PixelBuf+j)*3+1);
  542.                               *(Picture->b+j+i*xdim)=*(Colours+*(PixelBuf+j)*3+2);
  543.                             }
  544.                           }
  545.                         }
  546.                       }
  547.                       else
  548.                       {
  549.                         FreeVec(Picture->r); Picture->r=0;
  550.                         FreeVec(Picture->g); Picture->g=0;
  551.                         Picture->width=0;
  552.                         Picture->height=0;
  553.                         ShowError(&MemoryError);
  554.                       }
  555.                     }
  556.                     else
  557.                     {
  558.                       FreeVec(Picture->r); Picture->r=0;
  559.                       Picture->width=0;
  560.                       Picture->height=0;
  561.                       ShowError(&MemoryError);
  562.                     }
  563.                   }
  564.                   else
  565.                   {
  566.                     Picture->width=0;
  567.                     Picture->height=0;
  568.                     ShowError(&MemoryError);
  569.                   }
  570.                   FreeVec(Colours);
  571.                   Colours=0;
  572.                   ret=1;
  573.                 }
  574.                 else
  575.                 {
  576.                   ShowError(&MemoryError);
  577.                 }
  578.               }
  579.               else
  580.               {
  581.                 ret=0;
  582.               }
  583.             }
  584.             RemLines(Planes);
  585.           }
  586.           else
  587.           {
  588.             ShowError(&MemoryError);
  589.             ret=1;
  590.           }
  591.         }
  592.         else
  593.         {
  594.           ret=0;
  595.         }
  596.       }
  597.       else
  598.       {
  599.         ret=0;
  600.       }
  601.     }
  602.     else
  603.     {
  604.       ret=0;
  605.     }
  606.     Close(file);
  607.   }
  608.   else
  609.   {
  610.     ret=0;
  611.   }
  612.   return(ret);
  613. }
  614.  
  615. /*----------------------------------------------------------------------------*/
  616.  
  617. void
  618. LoadPic(struct Pic *Picture,STRPTR Title)
  619. {
  620.   struct request *FileRequester;
  621.   struct FileLock *lock;
  622.   if (FileRequester=(struct request *)AllocAslRequestTags(ASL_FileRequest,
  623.                                                           ASLFR_TitleText,  Title,
  624.                                                           ASLFR_Window,     CommandWindow,
  625.                                                           ASLFR_DoPatterns, TRUE,
  626.                                                           TAG_DONE))
  627.   {
  628.     if (AslRequest(FileRequester,0))
  629.     {
  630.       lock=(struct FileLock *)CurrentDir(Lock(((struct FileRequester *) FileRequester)->rf_Dir,ACCESS_READ));
  631.       Name=((struct FileRequester *) FileRequester)->rf_File;
  632.       if (!(LoadIlbm(Picture,Name)))
  633.       {
  634.         if (LPicture=NewDTObject(Name,
  635.                                  DTA_SourceType,  DTST_FILE,
  636.                                  DTA_GroupID,     GID_PICTURE,
  637.                                  PDTA_Remap,      FALSE,
  638.                                  TAG_DONE))
  639.         {
  640.           if (DoMethod(LPicture,DTM_PROCLAYOUT,NULL,TRUE))
  641.           {
  642.             if (GetDTAttrs(LPicture,
  643.                 PDTA_BitMapHeader, &BitMapHeader,
  644.                 PDTA_ModeID,       &ModeID,
  645.                 TAG_DONE))
  646.             {
  647.               if (BitMapHeader->bmh_Depth<=8)
  648.               {
  649.                 if (GetDTAttrs(LPicture,
  650.                     PDTA_CRegs,        &Colours,
  651.                     PDTA_NumColors,    &NumColours,
  652.                     PDTA_BitMap,       &BitMap,
  653.                     TAG_DONE))
  654.                 {
  655.                   if (VirtRast=(struct RastPort *)AllocVec(sizeof(*VirtRast),0))
  656.                   {
  657.                     InitRastPort(VirtRast);
  658.                     VirtRast->BitMap=BitMap;
  659.                     Picture->width=BitMapHeader->bmh_Width;
  660.                     Picture->height=BitMapHeader->bmh_Height;
  661.                     xmax=Picture->width;
  662.                     ymax=Picture->height;
  663.                     if (Picture->r)
  664.                     {
  665.                       FreeVec(Picture->r);
  666.                       Picture->r=0;
  667.                     }
  668.                     if (Picture->g)
  669.                     {
  670.                       FreeVec(Picture->g);
  671.                       Picture->g=0;
  672.                     }
  673.                     if (Picture->b)
  674.                     {
  675.                       FreeVec(Picture->b);
  676.                       Picture->b=0;
  677.                     }
  678.                     if (Picture->r=(UBYTE *)AllocVec((Picture->width)*(Picture->height),0))
  679.                     {
  680.                       if (Picture->g=(UBYTE *)AllocVec((Picture->width)*(Picture->height),0))
  681.                       {
  682.                         if (Picture->b=(UBYTE *)AllocVec((Picture->width)*(Picture->height),0))
  683.                         {
  684.                           if (BitMap->Depth<=24)
  685.                           {
  686.                             if (ModeID & HAM_KEY)
  687.                             {
  688.                               if (BitMap->Depth==8)
  689.                               {
  690.                                 for (y=0;y<ymax;y++)
  691.                                 {
  692.                                   r=*(Colours) >> 24;
  693.                                   g=*(Colours+1) >> 24;
  694.                                   b=*(Colours+2) >> 24;
  695.                                   for (x=0;x<xmax;x++)
  696.                                   {
  697.                                     c=ReadPixel(VirtRast,x,y);
  698.                                     if (c<64)
  699.                                     {
  700.                                       r=*(Colours+c*3) >> 24;
  701.                                       *((Picture->r)+x+y*xmax)=r;
  702.                                       g=*(Colours+c*3+1) >> 24;
  703.                                       *((Picture->g)+x+y*xmax)=g;
  704.                                       b=*(Colours+c*3+2) >> 24;
  705.                                       *((Picture->b)+x+y*xmax)=b;
  706.                                     }
  707.                                     else
  708.                                     {
  709.                                       if (c<128)
  710.                                       {
  711.                                         b=(b & 3)+((c-64)*4);
  712.                                         *((Picture->r)+x+y*xmax)=r;
  713.                                         *((Picture->g)+x+y*xmax)=g;
  714.                                         *((Picture->b)+x+y*xmax)=b;
  715.                                       }
  716.                                       else
  717.                                       {
  718.                                         if (c<192)
  719.                                         {
  720.                                           r=(r & 3)+((c-128)*4);
  721.                                           *((Picture->r)+x+y*xmax)=r;
  722.                                           *((Picture->g)+x+y*xmax)=g;
  723.                                           *((Picture->b)+x+y*xmax)=b;
  724.                                         }
  725.                                         else
  726.                                         {
  727.                                           g=(g & 3)+((c-192)*4);
  728.                                           *((Picture->r)+x+y*xmax)=r;
  729.                                           *((Picture->g)+x+y*xmax)=g;
  730.                                           *((Picture->b)+x+y*xmax)=b;
  731.                                         }
  732.                                       }
  733.                                     }
  734.                                   }
  735.                                 }
  736.                               }
  737.                               else
  738.                               {
  739.                                 for (y=0;y<ymax;y++)
  740.                                 {
  741.                                   r=*(Colours) >> 24;
  742.                                   g=*(Colours+1) >> 24;
  743.                                   b=*(Colours+2) >> 24;
  744.                                   for (x=0;x<xmax;x++)
  745.                                   {
  746.                                     c=ReadPixel(VirtRast,x,y);
  747.                                     if (c<16)
  748.                                     {
  749.                                       r=*(Colours+c*3) >> 24;
  750.                                       *((Picture->r)+x+y*xmax)=r;
  751.                                       g=*(Colours+c*3+1) >> 24;
  752.                                       *((Picture->g)+x+y*xmax)=g;
  753.                                       b=*(Colours+c*3+2) >> 24;
  754.                                       *((Picture->b)+x+y*xmax)=b;
  755.                                     }
  756.                                     else
  757.                                     {
  758.                                       if (c<32)
  759.                                       {
  760.                                         b=((c-16)*16);
  761.                                         *((Picture->r)+x+y*xmax)=r;
  762.                                         *((Picture->g)+x+y*xmax)=g;
  763.                                         *((Picture->b)+x+y*xmax)=b;
  764.                                       }
  765.                                       else
  766.                                       {
  767.                                         if (c<48)
  768.                                         {
  769.                                           r=((c-32)*16);
  770.                                           *((Picture->r)+x+y*xmax)=r;
  771.                                           *((Picture->g)+x+y*xmax)=g;
  772.                                           *((Picture->b)+x+y*xmax)=b;
  773.                                         }
  774.                                         else
  775.                                         {
  776.                                           g=((c-48)*16);
  777.                                           *((Picture->r)+x+y*xmax)=r;
  778.                                           *((Picture->g)+x+y*xmax)=g;
  779.                                           *((Picture->b)+x+y*xmax)=b;
  780.                                         }
  781.                                       }
  782.                                     }
  783.                                   }
  784.                                 }
  785.                               }
  786.                             }
  787.                             else
  788.                             {
  789.                               for (y=0;y<ymax;y++)
  790.                               {
  791.                                 for (x=0;x<xmax;x++)
  792.                                 {
  793.                                   c=ReadPixel(VirtRast,x,y);
  794.                                   *((Picture->r)+x+y*xmax)=*(Colours+c*3) >> 24;
  795.                                   *((Picture->g)+x+y*xmax)=*(Colours+c*3+1) >> 24;
  796.                                   *((Picture->b)+x+y*xmax)=*(Colours+c*3+2) >> 24;
  797.                                 }
  798.                               }
  799.                             }
  800.                           }
  801.                           else
  802.                           {
  803.                             ShowError(&LoadPicError);
  804.                           }
  805.                         }
  806.                         else
  807.                         {
  808.                           FreeVec(Picture->r); Picture->r=0;
  809.                           FreeVec(Picture->g); Picture->g=0;
  810.                           ShowError(&MemoryError);
  811.                         }
  812.                       }
  813.                       else
  814.                       {
  815.                         FreeVec(Picture->r); Picture->r=0;
  816.                         ShowError(&MemoryError);
  817.                       }
  818.                     }
  819.                     else
  820.                     {
  821.                       ShowError(&MemoryError);
  822.                     }
  823.                   }
  824.                   else
  825.                   {
  826.                     ShowError(&MemoryError);
  827.                   }
  828.                   FreeVec(VirtRast);
  829.                 }
  830.                 else
  831.                 {
  832.                   ShowError(&LoadPicError);
  833.                 }
  834.               }
  835.               else
  836.               {
  837.                 ShowError(&LoadPicError);
  838.               }
  839.             }
  840.             else
  841.             {
  842.               ShowError(&LoadPicError);
  843.             }
  844.           }
  845.           else
  846.           {
  847.             ShowError(&LoadPicError);
  848.           }
  849.           DisposeDTObject(LPicture);
  850.         }
  851.         else
  852.         {
  853.           ShowError(&LoadPicError);
  854.         }
  855.       }
  856.       UnLock(CurrentDir(lock));
  857.     }
  858.     FreeAslRequest(FileRequester);
  859.   }
  860.   else
  861.   {
  862.     ShowError(&MemoryError);
  863.   }
  864. }
  865.  
  866. /*----------------------------------------------------------------------------*/
  867.  
  868. void
  869. SavePic(struct Pic *Picture,STRPTR Title)
  870. {
  871.   struct request *FileRequester;
  872.   struct FileLock *lock;
  873.   LONG ww,Length,Buff;
  874.   BYTE cod;
  875.   UWORD BMHD[]={0x0000,0x0014,0x0000,0x0000,0x0000,0x0000,0x1800,0x0100,0x0000,
  876.                 0x0000,0x0000,0x0000};
  877.   if (FileRequester=(struct request *)AllocAslRequestTags(ASL_FileRequest,
  878.                                                           ASLFR_TitleText,  Title,
  879.                                                           ASLFR_Window,     CommandWindow,
  880.                                                           ASLFR_DoPatterns, TRUE,
  881.                                                           TAG_DONE))
  882.   {
  883.     if (AslRequest(FileRequester,0))
  884.     {
  885.       lock=(struct FileLock *)CurrentDir(Lock(((struct FileRequester *) FileRequester)->rf_Dir,ACCESS_READ));
  886.       Name=((struct FileRequester *) FileRequester)->rf_File;
  887.       if (file=Open(Name,MODE_NEWFILE))
  888.       {
  889.         Length=0;
  890.         Buff=0x464F524D;
  891.         Write(file,&Buff,4);
  892.         Buff=0x00000000;
  893.         Write(file,&Buff,4);
  894.         Buff=0x494C424D;
  895.         Write(file,&Buff,4);
  896.         Buff=0x424D4844;
  897.         Write(file,&Buff,4);
  898.         BMHD[2]=Picture->width; BMHD[10]=Picture->width;
  899.         BMHD[3]=Picture->height; BMHD[11]=Picture->height;
  900.         Write(file,&BMHD[0],24);
  901.         Buff=0x424F4459;
  902.         Write(file,&Buff,4);
  903.         Buff=0x00000000;
  904.         Write(file,&Buff,4);
  905.         ww=((Picture->width+15) >> 4) << 1;
  906.         if (AllocLines(24,Picture->width))
  907.         {
  908.           for (y=0;y<Picture->height;y++)
  909.           {
  910.             for (x=0;x<ww;x++)
  911.             {
  912.               for (i=0;i<8;i++)
  913.               {
  914.                 *(PixelBuf+x*8+i)=*(Picture->b+i+x*8+y*Picture->width) << 16;
  915.                 *(PixelBuf+x*8+i)=*(PixelBuf+x*8+i) | *(Picture->g+i+x*8+y*Picture->width) << 8;
  916.                 *(PixelBuf+x*8+i)=*(PixelBuf+x*8+i) | *(Picture->r+i+x*8+y*Picture->width);
  917.               }
  918.               for (i=0;i<24;i++)
  919.               {
  920.                 *(PlaneBuf[i]+x)=(((*(PixelBuf+x*8) >> i) & 1) << 7);
  921.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+1) >> i) & 1) << 6);
  922.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+2) >> i) & 1) << 5);
  923.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+3) >> i) & 1) << 4);
  924.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+4) >> i) & 1) << 3);
  925.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+5) >> i) & 1) << 2);
  926.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+6) >> i) & 1) << 1);
  927.                 *(PlaneBuf[i]+x)=*(PlaneBuf[i]+x) | (((*(PixelBuf+x*8+7) >> i) & 1));
  928.               }
  929.             }
  930.             for (i=0;i<24;i++)
  931.             {
  932.               x=0;
  933.               while(x<ww)
  934.               {
  935.                 if (x+1==ww)
  936.                 {
  937.                   cod=0;
  938.                   Write(file,&cod,1);
  939.                   Write(file,PlaneBuf[i]+x,1);
  940.                   x=x+cod+1;
  941.                   Length=Length+2;
  942.                 }
  943.                 else
  944.                 {
  945.                   if (*(PlaneBuf[i]+x)==*(PlaneBuf[i]+x+1))
  946.                   {
  947.                     cod=-1;
  948.                     while ((cod >-127) && (x-cod+1<ww) && (*(PlaneBuf[i]+x-cod)==*(PlaneBuf[i]+x-cod+1)))
  949.                     {
  950.                       cod=cod-1;
  951.                     }
  952.                     Write(file,&cod,1);
  953.                     Write(file,PlaneBuf[i]+x,1);
  954.                     x=x-cod+1;
  955.                     Length=Length+2;
  956.                   }
  957.                   else
  958.                   {
  959.                     cod=0;
  960.                     while ((cod <126) && (x+cod+1<ww) && (!(*(PlaneBuf[i]+x+cod+1)==*(PlaneBuf[i]+x+cod+2))))
  961.                     {
  962.                       cod=cod+1;
  963.                     }
  964.                     Write(file,&cod,1);
  965.                     Write(file,PlaneBuf[i]+x,cod+1);
  966.                     x=x+cod+1;
  967.                     Length=Length+cod+2;
  968.                   }
  969.                 }
  970.               }
  971.             }
  972.           }
  973.           if (Length & 1)
  974.           {
  975.             cod=128;
  976.             Write(file,&cod,1);
  977.             Length=Length+1;
  978.           }
  979.           Seek(file,44,OFFSET_BEGINNING);
  980.           Write(file,&Length,4);
  981.           Seek(file,4,OFFSET_BEGINNING);
  982.           Length=Length+40;
  983.           Write(file,&Length,4);
  984.           RemLines(24);
  985.         }
  986.         else
  987.         {
  988.           ShowError(&MemoryError);
  989.         }
  990.         Close(file);
  991.       }
  992.       else
  993.       {
  994.         ShowError(&FileError);
  995.       }
  996.       UnLock(CurrentDir(lock));
  997.     }
  998.     FreeAslRequest(FileRequester);
  999.   }
  1000.   else
  1001.   {
  1002.     ShowError(&MemoryError);
  1003.   }
  1004. }
  1005.  
  1006. /*----------------------------------------------------------------------------*/
  1007.  
  1008. /* End of Text */
  1009.