home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / board / SPSbugfix.lha / SavePubScreen256.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-30  |  7.8 KB  |  338 lines

  1. /*
  2.     NAME    SavePubScreen256 - find the given (public-)screen and save it to a file.
  3.  
  4. >>>    WHY FOR GOD'S SAKE A ``NEW'' SAVER ?
  5.  
  6.     ADVANTAGES
  7.  
  8.     - this one handles big screens, 1280x1024x8 is *NO*
  9.       problem ( if you have enough HD space, ofcourse )
  10.  
  11.     - this one could handle 256 colors (8bit)
  12.  
  13.     DISADVANTAGES
  14.  
  15.     - this one could **ONLY** handle 256 colors (8bit)
  16.  
  17.     - its neither fast in chunky->planar converting nor 
  18.       has it fast buffered file i/o. this one is only
  19.       functional NOT efficient.
  20.     
  21.     - because of its slowlyness the grabber freezes the
  22.       publicscreen via a global ILock().
  23.       this could cause system damage in several cases.
  24.  
  25.     EXAMPLE USAGE
  26.     
  27.     SavePubScreen256 FILE Media:Pictures/Workbench/today.IFF PUBSCREEN Workbench
  28.     PackIFF FILE Media:Pictures/Workbench/today.IFF COMPRESSION 1 [*]
  29.     
  30.                                 [*] not included in this package
  31.  
  32.     COPYRIGHT NOTICE AND AUTHOR
  33.  
  34.     this program was written by
  35.     
  36. >>>    dbalster@uni-paderborn.de (Daniel Balster)
  37.  
  38.     just finger me and my .plan tells you WHERE i am
  39.     right now. <try out one of the following computers:
  40.     elba,hawaii,jamaika,...,london,madrid,...,donald,
  41.     dagobert,daisy,... etc.>
  42.     
  43.     DISCLAIMER
  44.     
  45.     if you dislike this program and its agreements
  46.     DO NOT USE it. i will not be responsible for any
  47.     kind of damage the usage of this program will
  48.     cause. this could be a massive dataloss if this
  49.     program fails, be warned. (i never experienced
  50.     this case, anyhow)
  51.  
  52.     MARKETING AND SPREADING    NOTICE
  53.     
  54.     this source is limited freeware, this means:
  55.     
  56.     - it is allowed to change portions of the sourcecode to
  57.       fit personal purposes
  58.     
  59.     - it is allowed to reuse portions of the sourcecode in
  60.       personal applications
  61.       
  62.     - if you reuse portions of the sourcecode and your application
  63.       is going to be published under the concepts of freeware,giftware,
  64.       etc. everything is ok if you put it onto archives like aminet.
  65.     
  66.     - if you reuse portions of the sourcecode and your application
  67.       is going to be published under the concepts of shareware,testware,
  68.       etc. you must give me a registered copy, i will send you a request.
  69.  
  70.     - any commercial usage is not prohibited - i will send you a request
  71.       to get a free copy of your program.
  72.  
  73.     - if you put this program on some kind of compilation archived media
  74.       like DISKs, CDs, TAPEs or ROMs you MUST send me a copy on request.
  75.       important note: i know aminet offers this service - i don't want
  76.       a CD for this little hack - i will upload better programs (which are
  77.       already forming). ALL others (mainly ripping their packages out of
  78.       the aminet) HAVE TO send me a ``freebie''.
  79.  
  80.     sorry for this std disclaimer... 
  81. */
  82.  
  83. #include <string.h>
  84.  
  85. #include <exec/exec.h>
  86. #include <dos/dos.h>
  87. #include <intuition/intuition.h>
  88. #include <datatypes/pictureclass.h>
  89. #include <proto/exec.h>
  90. #include <proto/dos.h>
  91. #include <proto/intuition.h>
  92. #include <proto/graphics.h>
  93.  
  94. #define DOSTEMPLATE "FILE/A,PUBSCREEN/A"
  95.  
  96. struct {
  97.     STRPTR    file;
  98.     STRPTR    pubscreen;
  99.     ULONG    pad[14];
  100. } args;
  101.  
  102. BPTR file;
  103.  
  104. ULONG iffsize;
  105.  
  106. BOOL writeBODY (struct Screen *scr)
  107. {
  108.     int i,j,k;
  109.     UBYTE *line,*zeile;
  110.     struct RastPort *rp, tmprp;
  111.     struct BitMap *bmp;
  112.     BOOL erg=FALSE;
  113.     struct Layer_Info *li;
  114.  
  115.     rp = &(scr->RastPort);
  116.  
  117.     li = &scr->LayerInfo;
  118.     LockLayers(li);
  119.  
  120.     if(bmp = AllocBitMap(scr->Width,1,8,0,NULL))
  121.     {
  122.  
  123.     CopyMem(rp,&tmprp,sizeof(struct RastPort));
  124.     tmprp.Layer = NULL;
  125.     tmprp.BitMap = bmp;
  126.     
  127.     /* ich glaube nicht das jemand einen 777x555 Screen hat, also
  128.        gehe ich immer von 8-teilbaren Breiten aus */
  129.     
  130.     if(zeile = AllocVec (scr->Width/8,MEMF_PUBLIC|MEMF_CLEAR))
  131.     {
  132.     if(line = AllocVec (scr->Width,MEMF_PUBLIC))
  133.     {
  134.         ULONG sizeBODY = scr->Width*scr->Height;
  135.  
  136.         Write(file,"BODY",4);
  137.         Write(file,&(sizeBODY),4);
  138.  
  139.         for (i=0;i<scr->Height;i++)
  140.         {
  141.             /* mit readpixel umgeht man das ätzende poken */
  142.             /* ausserdem bleibe ich gfxcard-unabhängig */
  143.             /* und man kann sogar amiga-aa screens damit speichern */
  144.         
  145.             ReadPixelArray8(rp,0,i,scr->Width-1,i,line,&tmprp);
  146.             
  147.             /* den folgenden teil bitte überlesen... ;( */
  148.             /* ich hatte keine lust/zeit einen effizienteren */
  149.             /* chunky2planar algorithmus hier einzubauen, ausserdem */
  150.             /* fehlt das packing usw. das stück ``algorithmus'' ist */
  151.             /* in wenigen minuten entstanden und scheint zu funktionieren */
  152.             
  153.             for (k=7;k>-1;k--)
  154.             {
  155.                 UBYTE mask = 128>>k;
  156.             
  157.                 memset(zeile,0,scr->Width/8);
  158.             
  159.                 for (j=0;j<scr->Width;j++)    /* for each bit... */
  160.                 {
  161.                     if (line[j] & mask)
  162.                     {
  163.                         zeile[j/8] |= (128>>(j%8));
  164.                     }
  165.                 }
  166.                 Write(file,zeile,scr->Width/8);
  167.             }
  168.             
  169.             erg=TRUE;
  170.         }
  171.         
  172.         iffsize += 8 + sizeBODY;
  173.  
  174.         FreeVec(line);
  175.     }
  176.     else PutStr("could not allocate memory for a temporary line #1\n");
  177.  
  178.         FreeVec(zeile);
  179.     }
  180.     else PutStr("could not allocate memory for a temporary line #2\n");
  181.     
  182.         FreeBitMap(bmp);
  183.     }
  184.     else PutStr("could not allocate memory for a temporary BitMap\n");
  185.  
  186.     UnlockLayers(li);
  187.  
  188.     return erg;
  189. }
  190.  
  191. BOOL writeANNO (VOID)
  192. {
  193.     UBYTE *string =    "File was written by ChunkyScreenSaver V1.0 \n";
  194.     ULONG sizeANNO = strlen(string);
  195.  
  196.     if(Write(file,"ANNO",4)!=4)goto ANNOerror;
  197.     if(Write(file,&(sizeANNO),4)!=4)goto ANNOerror;
  198.     if(Write(file,string,sizeANNO)!=sizeANNO)goto ANNOerror;
  199.  
  200.     iffsize += 8 + sizeANNO;
  201.  
  202.     return TRUE;
  203. ANNOerror:
  204.     return FALSE;
  205. }
  206.  
  207. BOOL writeCAMG (struct Screen *scr)
  208. {
  209.     ULONG sizeCAMG = 4, CAMG = 0;
  210.  
  211.     if(Write(file,"CAMG",4)!=4)goto CAMGerror;
  212.     if(Write(file,&(sizeCAMG),4)!=4)goto CAMGerror;
  213.     if(Write(file,&(CAMG),4)!=4)goto CAMGerror;
  214.     
  215.     iffsize += 8 + sizeCAMG;
  216.     
  217.     return TRUE;
  218. CAMGerror:    
  219.     return FALSE;
  220. }
  221.  
  222. BOOL writeCMAP (struct Screen *scr)
  223. {
  224.     int i;
  225.     ULONG sizeCMAP = 256*3;
  226.     ULONG table[256*3];
  227.     UBYTE red,green,blue;
  228.  
  229.     Write(file,"CMAP",4);
  230.     Write(file,&(sizeCMAP),4);
  231.  
  232.     GetRGB32(scr->ViewPort.ColorMap,0,256,(ULONG*)&(table));
  233.  
  234.     for (i=0;i<256;i++) /* save 256 color image... */
  235.     {
  236.         red    = table[(i*3)+0] >> 24;
  237.         green    = table[(i*3)+1] >> 24;
  238.         blue    = table[(i*3)+2] >> 24;
  239.         
  240.         if (Write(file,&(red),1)!=1) goto CMAPerror;
  241.         if (Write(file,&(green),1)!=1) goto CMAPerror;
  242.         if (Write(file,&(blue),1)!=1) goto CMAPerror;
  243.     }
  244.  
  245.     iffsize += 8 + sizeCMAP;
  246.  
  247.     return TRUE;
  248.  
  249. CMAPerror:
  250.     return FALSE;
  251. }
  252.  
  253. BOOL writeBMHD (struct Screen *scr)
  254. {
  255.     struct BitMapHeader BMHD;
  256.     ULONG sizeBMHD = sizeof(struct BitMapHeader);
  257.  
  258.     BMHD.bmh_Width        =    scr->Width;
  259.     BMHD.bmh_Height        =    scr->Height;
  260.     BMHD.bmh_Left        =    0;
  261.     BMHD.bmh_Top        =    0;
  262.     BMHD.bmh_Depth        =    8;
  263.     BMHD.bmh_Masking    =    mskNone;
  264.     BMHD.bmh_Compression    =    cmpNone;
  265.     BMHD.bmh_Pad        =    0;
  266.     BMHD.bmh_Transparent    =    0;
  267.     BMHD.bmh_XAspect    =    0;
  268.     BMHD.bmh_YAspect    =    0;
  269.     BMHD.bmh_PageWidth    =    scr->Width;
  270.     BMHD.bmh_PageHeight    =    scr->Height;
  271.  
  272.     if(Write(file,"BMHD",4)!=4) goto BMHDerror;
  273.     if(Write(file,&(sizeBMHD),4)!=4) goto BMHDerror;
  274.     if(Write(file,&(BMHD),sizeof(struct BitMapHeader))!=sizeof(struct BitMapHeader)) goto BMHDerror;
  275.  
  276.     iffsize += 8 + sizeBMHD;
  277.  
  278.     return TRUE;
  279.  
  280. BMHDerror:
  281.     return FALSE;
  282. }
  283.  
  284. BOOL writeFORM (VOID)
  285. {
  286.     iffsize = 4;
  287.  
  288.     if(Write(file,"FORM",4)!=4)goto FORMerror;
  289.     if(Write(file,&(iffsize),4)!=4)goto FORMerror;
  290.     if(Write(file,"ILBM",4)!=4)goto FORMerror;
  291.  
  292.     return TRUE;
  293. FORMerror:    
  294.     return FALSE;
  295. }
  296.  
  297. int main (void)
  298. {
  299.     struct Screen *scr;
  300.     struct RDArgs *rdargs;
  301.  
  302.     if (rdargs = ReadArgs ((UBYTE*)DOSTEMPLATE,(LONG*)&args,NULL))
  303.     {
  304.         if (file = Open(args.file,MODE_NEWFILE))
  305.         {
  306.             if (scr = LockPubScreen(args.pubscreen))
  307.             {
  308.                 if (writeFORM())
  309.                 if (writeBMHD(scr))
  310.                 if (writeCMAP(scr))
  311.                 if (writeCAMG(scr))
  312.                 if (writeANNO())
  313.                 if (writeBODY(scr)) { /* beep ? */ }
  314.                 else PutStr("BODY error\n");
  315.                 else PutStr("ANNO error\n");
  316.                 else PutStr("CAMG error\n");
  317.                 else PutStr("CMAP error\n");
  318.                 else PutStr("BMHD error\n");
  319.                 else PutStr("FORM error\n");
  320.             
  321.                 Seek(file,4,OFFSET_BEGINNING);
  322.                 Write(file,&(iffsize),4);
  323.             
  324.                 UnlockPubScreen(NULL,scr);
  325.             }
  326.             else PutStr("could not lock requested public screen\n");
  327.         
  328.             Close(file);
  329.         }
  330.         else PrintFault (IoErr(),NULL);
  331.     
  332.         FreeArgs(rdargs);
  333.     }
  334.     else PrintFault (IoErr(),NULL);
  335.  
  336.     return RETURN_OK;
  337. }
  338.