/*
 * PBM Saver for ImageFX
 *
 */

#include <exec/types.h>
#include <dos/dos.h>
#include <libraries/iffparse.h>
#include <devices/clipboard.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <string.h>

#include <scan/modall.h>
#include <scan/loadsave.h>

enum {
   B_SavePBM,
   TXT_COUNT
};

/*
 * SM_SaveTrue() - Write out a 24-bit true color image file, no palette.
 */

BOOL __saveds __asm SM_SaveTrue (register __a0 char *fname,
                                 register __a1 struct Buffer *buf,
                                 register __d0 int id,
                                 register __a2 LONG *args)
{
   UBYTE fbuf[256];
   struct BIO *handle;
   int i, j;
   UBYTE *red, *grn, *blu;
   BOOL grey;

   BeginBar (GetStr(B_SavePBM, "Save PBM"), buf->Height, TRUE);

   handle = BOpen(fname, MODE_NEWFILE, 0);
   if (handle == NULL) { EndBar(NULL); return(FALSE); }

   SetError(ERR_Write);

   grey = (buf->Depth == 1);
   if (grey) {
      msprintf (fbuf, "P5\n%ld\n%ld\n255\n", buf->Width, buf->Height);
   }
   else {
      msprintf (fbuf, "P6\n%ld\n%ld\n255\n", buf->Width, buf->Height);
   }
   if (BWrite (handle, fbuf, strlen(fbuf)) < strlen(fbuf)) goto err;

   for (j = 0; j < buf->Height; j++) {
      if (Bar(j)) {
         SetError(ERR_UserCancel);
         goto err;
      }
      GetBufLine(buf, &red, &grn, &blu, j);
      for (i = 0; i < buf->Width; i++) {
         if (!BPutc(handle, *red++)) goto err;
         if (!grey) {
            if (!BPutc(handle, *grn++)) goto err;
            if (!BPutc(handle, *blu++)) goto err;
         }
      }
   }

   BClose(handle);

   EndBar(NULL);

   return(TRUE);

err:
   BClose(handle);
   DeleteFile(fname);
   EndBar(NULL);
   return(FALSE);
}

/*
 * SM_SaveMapped() - Write out a color mapped image file, with
 *                   palette information.
 */

BOOL __saveds __asm SM_SaveMapped (register __a0 char *fname,
                                   register __a1 struct MappedImage *img,
                                   register __d0 int id)
{
   return(FALSE);
}


/*
 * SM_SavePalette() - Write out only 24-bit palette information.
 */

BOOL __saveds __asm SM_SavePalette (register __a0 char *fname,
                                    register __a1 struct Palette *palette,
                                    register __d0 int id)
{
   return(FALSE);
}


static
struct SaveFormat saveformats[] = {
   { "PBM", SAV_TRUE | SAV_NOMASK,   0 },
   { NULL }
};

struct SaveFormat * __saveds SM_Signatures (void)
{
   return (saveformats);
}


/**********************************************************************\

               Library Functions and Initialization Stuff

\**********************************************************************/

extern ULONG LibOpen (void);
extern ULONG LibClose (void);
extern ULONG LibExpunge (void);
extern ULONG LibNull (void);

ULONG FuncTable[] = {
   /* These four MUST be present */
   (ULONG) LibOpen,
   (ULONG) LibClose,
   (ULONG) LibExpunge,
   (ULONG) LibNull,

   (ULONG) 0,
   (ULONG) SM_SaveTrue,
   (ULONG) SM_SaveMapped,
   (ULONG) SM_SavePalette,
   (ULONG) SM_Signatures,

   /* End with -1L */
   (ULONG) -1L
};

UBYTE LibraryID[]    = "$VER: PBM Saver 1.03.01 (12.11.92)";
UBYTE LibraryType    = NT_SAVER;

long __saveds __stdargs UserOpen (struct ModuleBase *modbase)
{
   modbase->NumGads = 0;
   modbase->NewGad = NULL;
   modbase->Language = "Saver_PBM";
   modbase->LangCount = TXT_COUNT;
   return(TRUE);
}

long __saveds __stdargs UserClose (struct ModuleBase *modbase)
{
   return(TRUE);
}

