Index: trunk/savebmp.cpp
===================================================================
--- trunk/savebmp.cpp	(revision 2)
+++ trunk/savebmp.cpp	(revision 11)
@@ -20,11 +20,151 @@
 #include <time.h>
 
-// ** SaveBitmap ********************************************************** /*fold00*/
-
-VOID SaveBitmap (HBITMAP hbm, HPS hps)
+#include "gbm\gbm.h"
+#include "gbm\gbmmem.h"
+#include "model.h"
+
+#define  __PMPRINTF__
+#include "PMPRINTF.H"
+
+/* ------------------------------ */
+static int StrideOf(const MOD *mod)
 {
+    DebugHereIAm();
+	return ( ( mod->gbm.w * mod->gbm.bpp + 31 ) / 32 ) * 4;
+	}
+/*...e*/
+
+/*...sAllocateData:0:*/
+static BOOL AllocateData(MOD *mod)
+	{
+	const unsigned long stride = StrideOf(mod);
+	if ( (mod->pbData = (UCHAR*) gbmmem_malloc(stride * mod->gbm.h)) == NULL )
+            return FALSE;
+        DebugHereIAm();
+        return TRUE;
+	}
+/*...e*/
+
+/*...sModCreate:0:*/
+MOD_ERR ModCreate(
+	int w, int h, int bpp, const GBMRGB gbmrgb[],
+	MOD *modNew
+	)
+	{
+	modNew->gbm.w   = w;
+	modNew->gbm.h   = h;
+	modNew->gbm.bpp = bpp;
+	if ( gbmrgb != NULL && bpp != 24 )
+		memcpy(&(modNew->gbmrgb), gbmrgb, sizeof(GBMRGB) << bpp);
+	if ( !AllocateData(modNew) )
+            return MOD_ERR_MEM;
+        DebugHereIAm();
+	return MOD_ERR_OK;
+	}
+/*...e*/
+
+/*MOD_ERR ModCreateFromHPS(
+	HPS hps, int w, int h, int bpp, //lBitCountScreen > 8 ) ? 24 : lBitCountScreen
+	MOD *modNew
+        )*/
+/*...sModCreateFromHPS:0:*/
+MOD_ERR ModCreateFromHPS(
+	HPS hps, int w, int h, int bpp,
+	MOD *modNew
+	)
+	{
+	MOD_ERR mrc;
+
+    #pragma pack(2)
+	struct
+		{
+		BITMAPINFOHEADER2 bmp2;
+		RGB2 argb2Color[0x100];
+		} bm;
+    #pragma pack()
+
+	if ( (mrc = ModCreate(w, h, bpp, NULL, modNew)) != MOD_ERR_OK )
+		return mrc;
+        DebugHereIAm();
+	memset(&(bm.bmp2), 0, sizeof(bm.bmp2));
+	bm.bmp2.cbFix     = sizeof(BITMAPINFOHEADER2);
+	bm.bmp2.cx        = w;
+	bm.bmp2.cy        = h;
+	bm.bmp2.cBitCount = bpp;
+	bm.bmp2.cPlanes   = 1;
+	GpiQueryBitmapBits(hps, 0L, h, modNew->pbData, (BITMAPINFO2 *) &bm);
+
+	if ( bpp != 24 )
+		{
+		int i;
+		for ( i = 0; i < (1<<bpp); i++ )
+			{
+			modNew->gbmrgb[i].r = bm.argb2Color[i].bRed  ;
+			modNew->gbmrgb[i].g = bm.argb2Color[i].bGreen;
+			modNew->gbmrgb[i].b = bm.argb2Color[i].bBlue ;
+			}
+                }
+        DebugHereIAm();
+	return MOD_ERR_OK;
+	}
+/*...e*/
+
+/*MOD_ERR ModWriteToFile(
+	const MOD *mod,
+	const CHAR *szFn, const CHAR *szOpt // ""
+        )*/
+/*...sModWriteToFile:0:*/
+MOD_ERR ModWriteToFile(
+	const MOD *mod,
+	const CHAR *szFn, const CHAR *szOpt
+	)
+	{
+	GBM_ERR grc;
+	int fd, ft, flag = 0;
+	GBMFT gbmft;
+
+        if ( (grc = gbm_guess_filetype(szFn, &ft)) != GBM_ERR_OK ) {
+            return grc;
+        }
+        PmpfF(( "file type %i", ft ));
+        DebugHereIAm();
+        gbm_query_filetype(ft, &gbmft);
+        PmpfF(( "file type %i name %s flags %i bpp %i", ft, gbmft.short_name,
+               gbmft.flags, mod->gbm.bpp));
+	switch ( mod->gbm.bpp )
+		{
+		case 1:		flag = GBM_FT_W1;	break;
+		case 4:		flag = GBM_FT_W4;	break;
+		case 8:		flag = GBM_FT_W8;	break;
+		case 24:	flag = GBM_FT_W24;	break;
+		default:	flag = 0;	break;
+		}
+
+	if ( (gbmft.flags & flag) == 0 )
+            return MOD_ERR_SUPPORT;
+        DebugHereIAm();
+	if ( (fd = gbm_io_create(szFn, GBM_O_WRONLY)) == -1 )
+		return MOD_ERR_CREATE;
+        DebugHereIAm();
+	if ( (grc = gbm_write(szFn, fd, ft, &(mod->gbm), mod->gbmrgb, mod->pbData, szOpt)) != GBM_ERR_OK )
+		{
+		gbm_io_close(fd);
+                unlink(szFn);
+		return MOD_ERR_GBM(grc);
+		}
+        DebugHereIAm();
+	gbm_io_close(fd);
+        DebugHereIAm();
+	return MOD_ERR_OK;
+	}
+/*...e*/
+// ** SaveBitmap ********************************************************** /*FOLD00*/
+
+VOID SaveBitmap (HBITMAP hbm, HPS hps, int width, int height, int bitCount)
+{
 #ifdef _DOLOGMEM_
     LogMem("SaveBitmap", TRUE);
 #endif
+    MOD newmod;
 
     if( pset->QueryFileSaveStyle () == FSS_FORCEFILE )
@@ -32,5 +172,17 @@
         PSZ psz = pset->QueryForceSaveFile();
         psz = AddExtensionToFilename( psz );
-        SaveBitmapToFile( hbm, psz, hps );
+        //
+        /*if( ( pset->QueryFileFormat() == BMF_12 ) ||
+           ( pset->QueryFileFormat() == BMF_16 ) ||
+           ( pset->QueryFileFormat() == BMF_20 ) ) {
+            SaveBitmapToFile( hbm, psz, hps );
+            DebugHereIAm();
+        }
+        else { */
+            ModCreateFromHPS( hps, width, height,
+                             (bitCount > 8 ) ? 24 : bitCount,
+                             &newmod );
+            ModWriteToFile(&newmod, psz, "" );
+        //}
 #ifdef _DOLOGMEM_
         LogMem( "SaveBitmap-1", FALSE );
@@ -60,5 +212,18 @@
             LogDebug( "Before call to SaveBitmapToFile()" );
 #endif
-            SaveBitmapToFile (hbm, pset->QuerySaveFile (), hps);
+            //DebugHereIAm();
+            /*if( ( pset->QueryFileFormat() == BMF_12 ) ||
+               ( pset->QueryFileFormat() == BMF_16 ) ||
+               ( pset->QueryFileFormat() == BMF_20 ) ) {
+                DebugHereIAm();
+                SaveBitmapToFile( hbm, pset->QuerySaveFile (), hps );
+                DebugHereIAm();
+            }
+            else {*/
+                ModCreateFromHPS( hps, width, height,
+                                 (bitCount > 8 ) ? 24 : bitCount,
+                                 &newmod );
+                ModWriteToFile(&newmod, pset->QuerySaveFile (), "" );
+            //}
 #ifdef _DOLOGDEBUG_
             LogDebug( "After call to SaveBitmapToFile()" );
@@ -96,4 +261,5 @@
 #define CB_20HEADER       sizeof (BITMAPINFOHEADER2) // == 64
 
+#if 0
 VOID SaveBitmapToFile (HBITMAP hbm, PSZ psz, HPS hps)
 {
@@ -274,5 +440,7 @@
         fwrite (pb, cbBits, 1, pf);
         fclose (pf);
-    } else {
+    }
+
+    /*else {
 #ifdef _DOLOGDEBUG_
         LogDebug( "SaveBitmapToFile(): Starting 'else' path." );
@@ -304,8 +472,8 @@
         // Open target file.
         HMMIO hmmioTarget;
-        if( ! ( hmmioTarget = pset->GetMMIO()->pfmmioOpen( psz,
+        if( ! ( hmmioTarget = mmioOpen( psz,
                                         &mmioinfoTarget,
                                         MMIO_CREATE | MMIO_WRITE |
-                                        MMIO_DENYWRITE | MMIO_NOIDENTIFY ) ) ) {
+                                        MMIO_DENYWRITE | MMIO_NOIDENTIFY ))) {
             DisplayError ("mmioOpen()-Error",
                           "mmioOpen()-Error %ld", mmioinfoTarget.ulErrorRet );
@@ -319,9 +487,9 @@
         ULONG ulImageHeaderLength;
 
-        xmmioQueryHeaderLength( hmmioTarget, (PLONG)&ulImageHeaderLength, 0L, 0L );
+        mmioQueryHeaderLength( hmmioTarget, (PLONG)&ulImageHeaderLength, 0L, 0L );
         if( ulImageHeaderLength != sizeof( MMIMAGEHEADER ) )
         {
             // We have a problem.....possibly incompatible versions.
-            xmmioClose( hmmioTarget, 0L );
+            mmioClose( hmmioTarget, 0L );
             DisplayError ("mmioQueryHeaderLength()-Error",
                           "mmioQueryHeaderLength()-Error",
@@ -355,10 +523,10 @@
 #endif
         ULONG ulBytesRead;
-        if( ( rc = (LONG)xmmioSetHeader( hmmioTarget, (MMIMAGEHEADER*)&mmImgHdr,
+        if( ( rc = (LONG)mmioSetHeader( hmmioTarget, (MMIMAGEHEADER*)&mmImgHdr,
                                          (LONG)sizeof( MMIMAGEHEADER ), (PLONG)&ulBytesRead,
                                          0L, 0L ) ) != MMIO_SUCCESS )
         {
             // Header unavailable.
-            xmmioClose( hmmioTarget, 0L );
+            mmioClose( hmmioTarget, 0L );
             DisplayError ("mmioSetHeader()-Error",
                           "mmioSetHeader()-Error %ld", rc );
@@ -370,5 +538,5 @@
 
         // write the actual bitmap data bits
-        if( ( rc = xmmioWrite( hmmioTarget, pb, cbBits ) ) == MMIO_ERROR )
+        if( ( rc = mmioWrite( hmmioTarget, (CHAR *) pb, cbBits ) ) == MMIO_ERROR )
             DisplayError ("mmioWrite()-Error",
                           "mmioWrite()-Error %ld", rc );
@@ -377,5 +545,5 @@
 #endif
 
-        if( ( rc = xmmioClose( hmmioTarget, 0 ) ) != MMIO_SUCCESS )
+        if( ( rc = mmioClose( hmmioTarget, 0 ) ) != MMIO_SUCCESS )
             DisplayError ("mmioClose()-Error",
                           "mmioClose()-Error %ld", rc );
@@ -383,5 +551,5 @@
         LogDebug( "SaveBitmapToFile(): xmmioClose ok." );
 #endif
-    }
+    } */
 
 #ifdef _DOLOGDEBUG_
@@ -399,5 +567,5 @@
 #endif
 }
-
+#endif
 // ** SetEAs ************************************************************** /*FOLD00*/
 
@@ -620,5 +788,5 @@
 }
 
-// ** AddExtensionToFilename ********************************************** /*fold00*/
+// ** AddExtensionToFilename ********************************************** /*FOLD00*/
 
 PSZ AddExtensionToFilename( PSZ psz )
@@ -629,6 +797,7 @@
     PSZ pszExtension;
 
-    PSZ apszValidExtensions[10] =
-    { "bmp", "tif", "tiff", "tga", "targa", "pcx", "gif", "jpg", "jpeg", "dib" };
+    PSZ apszValidExtensions[16] =
+    { "bmp", "tif", "tiff", "tga", "targa", "pcx", "pnm", "jpg", "jpeg",
+    "jpg2", "jbg", "jbig", "png", "ppm", "raw", "dib" };
 
     if( ! ( pszExtension = strrchr( psz, '.' ) ) ) {
@@ -641,5 +810,5 @@
         // Some extension, but not the correct one - change or append.
         BOOL fValidExtension = FALSE;
-        for( int i = 0; i < 10; i++ ) {
+        for( int i = 0; i < 16; i++ ) {
             if( stricmp( pszExtension+1, apszValidExtensions[i] ) == 0 ) {
                 fValidExtension = TRUE;
