primitiveimexport.C

Go to the documentation of this file.
00001 /* Gnome gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Gxsm Plugin Name: primitive_im_export.C
00006  * ========================================
00007  * 
00008  * Copyright (C) 1999 The Free Software Foundation
00009  *
00010  * Authors: Percy Zahl <zahl@fkp.uni-hannover.de>
00011  * additional features: Andreas Klust <klust@fkp.uni-hannover.de>
00012  *
00013  * This program is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00026  */
00027 
00028 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00029 
00030 /* Please do not change the Begin/End lines of this comment section!
00031  * this is a LaTeX style section used for auto generation of the PlugIn Manual 
00032  * Chapter. Add a complete PlugIn documentation inbetween the Begin/End marks!
00033  * --------------------------------------------------------------------------------
00034 % BeginPlugInDocuSection
00035 % PlugInDocuCaption: Import/export of non-SPM file formats
00036 
00037 % PlugInName: primitiveimexport
00038 
00039 % PlugInAuthor: Percy Zahl
00040 
00041 % PlugInAuthorEmail: zahl@users.sf.net
00042 
00043 % PlugInMenuPath: File/Import/Primitive Auto Import and File/Export/Primitive Auto Export
00044 
00045 % PlugInDescription
00046 This plug-in is responsible for importing and exporting a wide variety of
00047 file formats.  Most of them are file formats not specifically designed for
00048 storage of scientific data such as the Portable Graymap (pgm) format.
00049 Nethertheless, the support of these formats allows to exchange data between
00050 \Gxsm\ and countless other programs, e.g.\ The GIMP for the final "touch up"
00051 of images.
00052 
00053 % OptPlugInSubSection: Supported file formats
00054 
00055 The file formats supported by the \GxsmEmph{primitiveimexport} plug-in are
00056 listed in Tab.\ \ref{tab:primitiveimexport:formats}.  The ".dat" format
00057 refers to the old, STM specific data format used by the OS/2 software PMSTM
00058 and the Linux based Xxsm, both predecessors of \Gxsm.  The ".d2d" format is
00059 used for SPA-LEED by the SPA V program.  The primitive formats ".byt",
00060 ".sht", ".flt", and ".dbl" are described in more detail below.
00061 
00062 \begin{table}
00063   \begin{tabular}[h]{l|l}
00064     Suffix & description \\\hline\hline
00065     .dat   & (old) STM data format, used by e.g.\ PMSTM, dicontinued.\\
00066     .byt   & byte format: raw 8 Bit data\\
00067     .sht   & Short format: raw 16 Bit data\\
00068     .flt   & Float format: floats, single precision\\
00069     .dbl   & Double format: floats, double precision\\
00070     .pgm   & Portable Greymap (P5) format\\
00071     \hline\hline
00072     \multicolumn{2}{l}{Read-only supported formats:}\\
00073     \hline
00074     .d2d   & (old) SPA--LEED data format, used by SPA V\\
00075     .nsc   & a very old version of nanoscope import filter (obsolete),\\
00076            & use the new Nanoscope Import PlugIn!\\
00077     .h16   & Markus/Mats simulations program data import \\
00078     \hline\hline
00079     \multicolumn{2}{l}{Write-only supported formats:}\\
00080     \hline
00081     .tga   & TARGA bitmap format (8, 16 and 24 Bit colordepth)\\
00082            & 24bit: only option to write color images (usage of palette)\\
00083   \end{tabular}  
00084   \caption{file formats supported by the \GxsmEmph{primitiveimexport}
00085 plug-in.}
00086   \label{tab:primitiveimexport:formats}
00087 \end{table}
00088 
00089 % OptPlugInSubSection: The primitive file formats
00090 The primitive file formats ".byt", ".sht", ".flt", and ".dbl" are supported
00091 by the \GxsmEmph{primitiveimexport} plug-in for both reading and writing.
00092 These file formats have a simple structure and therefore present a
00093 considerable alternative to more complex formats for quickly hacked
00094 programs.  They are binary files in PC byte order starting with two 16 bit
00095 integer numbers denoting the number of rows and columns in the image.
00096 Following them, the image data is written in raw binary form using the
00097 following data types to represent a single pixel:  char (".byt"), 16 bit
00098 integer (".sht"), single precision floating point (".flt"), or double
00099 precision floating point (".dbl") numbers.
00100 
00101 For the manipulation of data in these formats, the \Gxsm\ project provides
00102 an extensive set of small tools included in the \Gxsm\ source code.  For
00103 instance, these tools include programs to create, filter, and doing simple
00104 algebra operations.  All these tools are designed for the use in shell
00105 scripts.
00106 
00107 % PlugInUsage
00108 The file formats are recognized by their filename suffix. For instance,
00109 exporting to \GxsmFile{myimage.pgm} results in a greymap image in the binary
00110 Portable Greymap (pgm) format.  For other supported formats and their
00111 correspondent suffixes see Tab.\ \ref{tab:primitiveimexport:formats}.
00112 
00113 % OptPlugInHints
00114 It is always a good idea to do a background correction before
00115 exporting to any 8 bit format, including 24 bit color exports as well.
00116 Then switch to the direct view mode before exporting. If you are not
00117 exporting to a 24bit TARGA file, please also switch off the usage of a
00118 palette to assure a correct grey scaleing.\\
00119 %
00120 The 16 bit TARGA bitmap format looks quite weird when watched at in an
00121 image viewer.  It physically outputs 24 bit TARGA files using only the
00122 red and green values (16 of the 24 bits).  This format is intended for
00123 rendering images using the POVRAY raytracer which is able to
00124 interprete it as a height profile with the full 16 bit resolution.\\
00125 %
00126 There is also a tool \GxsmFile{nctopng} available, which by default
00127 creates small icons using the \GxsmFile{.png} (Portable Network Graphics)
00128 format, but using it with a commandline option (\GxsmFile{nctopng gxsmncfile.nc myimage.png width\_in\_pixels})
00129 full size images can be created as well.
00130 %\begin{verbatim}
00131 %pzahl@charon:~$ nctopng --help
00132 %Usage: nctopng gxsmncfile.nc myimage.png 
00133 %       [new_nx] [-v | --verbose]
00134 %\end{verbatim}
00135 
00136 %% OptPlugInKnownBugs
00137 
00138 % EndPlugInDocuSection
00139  * -------------------------------------------------------------------------------- 
00140  */
00141 
00142 
00143 #include <gtk/gtk.h>
00144 #include "config.h"
00145 #include "gxsm/plugin.h"
00146 #include "gxsm/dataio.h"
00147 #include "gxsm/action_id.h"
00148 #include "gxsm/util.h"
00149 #include "gxsm/glbvars.h"
00150 #include "batch.h"
00151 #include "g_dat_types.h"
00152 #include "fileio.c"
00153 
00154 
00155 using namespace std;
00156 
00157 
00158 #define IMGMAXCOLORS 64
00159 
00160 // Plugin Prototypes
00161 static void primitive_im_export_init (void);
00162 static void primitive_im_export_query (void);
00163 static void primitive_im_export_about (void);
00164 static void primitive_im_export_configure (void);
00165 static void primitive_im_export_cleanup (void);
00166 
00167 static void primitive_im_export_filecheck_load_callback (gpointer data );
00168 static void primitive_im_export_filecheck_save_callback (gpointer data );
00169 
00170 static void primitive_im_export_import_callback (GtkWidget *w, void *data);
00171 static void primitive_im_export_export_callback (GtkWidget *w, void *data);
00172 
00173 // Fill in the GxsmPlugin Description here
00174 GxsmPlugin primitive_im_export_pi = {
00175   NULL,                   // filled in and used by Gxsm, don't touch !
00176   NULL,                   // filled in and used by Gxsm, don't touch !
00177   0,                      // filled in and used by Gxsm, don't touch !
00178   NULL,                   // The Gxsm-App Class Ref.pointer (called "gapp" in Gxsm) is 
00179                           // filled in here by Gxsm on Plugin load, 
00180                           // just after init() is called !!!
00181   "Primitive_Im_Export",
00182   NULL,
00183   // Description, is shown by PluginViewer (Plugin: listplugin, Tools->Plugin Details)
00184   "Im/Export of primitive data formats: all gnu, pgm, tga, mats",
00185   "Percy Zahl",
00186   N_("File/Im,Export"),
00187   N_("..."),
00188   NULL,
00189   "no more info",
00190   NULL,          // error msg, plugin may put error status msg here later
00191   NULL,          // Plugin Status, managed by Gxsm, plugin may manipulate it too
00192   primitive_im_export_init,  
00193   primitive_im_export_query,  
00194   // about-function, can be "NULL"
00195   // can be called by "Plugin Details"
00196   primitive_im_export_about,
00197   // configure-function, can be "NULL"
00198   // can be called by "Plugin Details"
00199   primitive_im_export_configure,
00200   // run-function, can be "NULL", if non-Zero and no query defined, 
00201   // it is called on menupath->"plugin"
00202   NULL,
00203   // cleanup-function, can be "NULL"
00204   // called if present at plugin removeal
00205   primitive_im_export_cleanup
00206 };
00207 
00208 // Text used in Aboutbox, please update!!
00209 static const char *about_text = N_("Gxsm primitve Data File Import/Export Plugin\n\n"
00210                                    "This plugin reads in a datafiles of various\n"
00211                                    "primitive formats:\n"
00212                                    "GNUs: .byt, .sht, .flt, .dbl, but not: .cpx\n"
00213                                    "The simple PGM format (8bit grey)\n"
00214                                    "Export only: TGA, TGA16 (for povray), TGA Color\n"
00215                                    "MATS format\n"
00216                                    ".ncd (very old DI NanoScope format)"
00217         );
00218 
00219 // Symbol "get_gxsm_plugin_info" is resolved by dlsym from Gxsm, used to get Plugin's info!! 
00220 // Essential Plugin Function!!
00221 GxsmPlugin *get_gxsm_plugin_info ( void ){ 
00222   primitive_im_export_pi.description = g_strdup_printf(N_("Gxsm primitive_im_export plugin %s"), VERSION);
00223   return &primitive_im_export_pi; 
00224 }
00225 
00226 // Query Function, installs Plugin's features in Menupath(s)!
00227 static void primitive_im_export_query(void)
00228 {
00229   static GnomeUIInfo menuinfo_Import[] = { 
00230     { GNOME_APP_UI_ITEM, 
00231       N_("Primitive Auto Import"), N_("Automatic Import of primitive Formats: GNUs, PGM, .."), 
00232       (gpointer) primitive_im_export_import_callback, NULL,
00233       NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN,
00234       0, GDK_CONTROL_MASK, NULL },
00235     GNOMEUIINFO_END
00236   };
00237 
00238   gnome_app_insert_menus(GNOME_APP(primitive_im_export_pi.app->getApp()), N_("File/Import/"), menuinfo_Import);
00239 
00240   static GnomeUIInfo menuinfo_Export[] = { 
00241     { GNOME_APP_UI_ITEM, 
00242       N_("Primitive Auto Export"), N_("Automatic Export of primitive Formats: GNUs, PGM, TGA, .."), 
00243       (gpointer) primitive_im_export_export_callback, NULL,
00244       NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_SAVE, 
00245       0, GDK_CONTROL_MASK, NULL },
00246     GNOMEUIINFO_END
00247   };
00248 
00249   gnome_app_insert_menus(GNOME_APP(primitive_im_export_pi.app->getApp()), N_("File/Export/"), menuinfo_Export);
00250 
00251   if(primitive_im_export_pi.status) g_free(primitive_im_export_pi.status); 
00252   primitive_im_export_pi.status = g_strconcat(N_("Plugin query has attached "),
00253                           primitive_im_export_pi.name, 
00254                           N_(": File IO Filters are ready to use"),
00255                           NULL);
00256 
00257   primitive_im_export_pi.app->ConnectPluginToLoadFileEvent (primitive_im_export_filecheck_load_callback);
00258   primitive_im_export_pi.app->ConnectPluginToSaveFileEvent (primitive_im_export_filecheck_save_callback);
00259 }
00260 
00261 
00262 // 5.) Start here with the plugins code, vars def., etc.... here.
00263 // ----------------------------------------------------------------------
00264 //
00265 
00266 
00267 // init-Function
00268 static void primitive_im_export_init(void)
00269 {
00270   PI_DEBUG (DBG_L2, "primitive_im_export Plugin Init" );
00271 }
00272 
00273 // about-Function
00274 static void primitive_im_export_about(void)
00275 {
00276   const gchar *authors[] = { primitive_im_export_pi.authors, NULL};
00277   gtk_widget_show(gnome_about_new ( primitive_im_export_pi.name,
00278                                     VERSION,
00279                                     N_("(C) 2001 the Free Software Foundation"),
00280                                     about_text,
00281                                     authors,
00282                                     NULL, NULL, NULL
00283                                     ));
00284 }
00285 
00286 // configure-Function
00287 static void primitive_im_export_configure(void)
00288 {
00289   if(primitive_im_export_pi.app)
00290     primitive_im_export_pi.app->message("primitive_im_export Plugin Configuration");
00291 }
00292 
00293 // cleanup-Function
00294 static void primitive_im_export_cleanup(void)
00295 {
00296   PI_DEBUG (DBG_L2, "primitive_im_export Plugin Cleanup" );
00297   gnome_app_remove_menus (GNOME_APP (primitive_im_export_pi.app->getApp()),
00298                           N_("File/Import/Primitive Auto Import"), 1);
00299   gnome_app_remove_menus (GNOME_APP (primitive_im_export_pi.app->getApp()),
00300                           N_("File/Export/Primitive Auto Export"), 1);
00301 }
00302 
00303 class PrimitiveImExportFile : public Dataio{
00304  public:
00305   PrimitiveImExportFile(Scan *s, const char *n) : Dataio(s,n){ };
00306   virtual FIO_STATUS Read();
00307   virtual FIO_STATUS Write();
00308  private:
00309   FIO_STATUS gnuRead(const char *fname);
00310   FIO_STATUS d2dRead(const char *fname);
00311   FIO_STATUS nscRead(const char *fname);
00312   FIO_STATUS matsRead(const char *fname);
00313 };
00314 // ==================================================
00315 // GNU File Types Extension
00316 // and tga export
00317 
00318 // für TGA Export
00319 
00320 typedef struct { short k[6], x,y, pixsz; } TGA_head;
00321 typedef struct { unsigned char val[3]; } TGA_pix;
00322 
00323 
00324 
00325 // d2d import :=)
00326 FIO_STATUS PrimitiveImExportFile::Read(){
00327         FIO_STATUS ret;
00328         gchar *fname=NULL;
00329 
00330         PI_DEBUG (DBG_L2, "Primitive Import erreicht" );
00331 
00332         fname = (gchar*)name;
00333 
00334         // name should have at least 4 chars: ".ext"
00335         if (fname == NULL || strlen(fname) < 4)
00336                 return  FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00337  
00338         // check for file exists and is OK !
00339         // else open File Dlg
00340         ifstream f;
00341         f.open(fname, ios::in);
00342         if(!f.good()){
00343                 PI_DEBUG (DBG_L2, "File Fehler" );
00344                 return status=FIO_OPEN_ERR;
00345         }
00346         f.close();
00347 
00348         // Check all known File Types:
00349         
00350         // Mats ".h16" type?
00351         if ((ret=matsRead (fname)) !=  FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00352                 return ret;
00353 
00354         // SPA4 ".d2d" type?
00355         if ((ret=d2dRead (fname)) !=  FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00356                 return ret;
00357 
00358         // very old DI NanoScope ".nsc" type?
00359         if ((ret=nscRead (fname)) !=  FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00360                 return ret;
00361 
00362         // one of the "GNU" (.byt, .sht, .dat, .flt, .dbl, not .cpx, .pgm) types?
00363         if ((ret=gnuRead (fname)) !=  FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00364                 return ret;
00365 
00366         return  FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00367 }
00368 
00369 FIO_STATUS PrimitiveImExportFile::gnuRead(const char *fname){
00370 // für Gnu-Paket:
00371   FILETYPE ft=BYTFIL;
00372   int i,j;
00373   long nb, anz;
00374   char *buf;
00375   unsigned char *pb;
00376   short *ps;
00377   float *pf;
00378   double *pd;
00379   FILE *fh;
00380   FILEHEAD fhead;   /* union  class */
00381   // const char *fname;
00382   char *cfname;
00383   
00384   // Am I resposible for that file, is it one of the ""GNU"" formats ?
00385   if (strncmp (fname+strlen(fname)-4,".sht",4) &&
00386       strncmp (fname+strlen(fname)-4,".dat",4) &&
00387       strncmp (fname+strlen(fname)-4,".flt",4) &&
00388       strncmp (fname+strlen(fname)-4,".dbl",4) &&
00389       strncmp (fname+strlen(fname)-4,".byt",4) &&
00390       strncmp (fname+strlen(fname)-4,".pgm",4)    )
00391           return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00392 
00393   if (strncmp (fname+strlen(fname)-4,".dat",4) == 0){
00394           // make sure it's not a GME-Dat file!!
00395           ifstream f;
00396           gchar line[64];
00397           f.open (name, ios::in);
00398           if (!f.good ())
00399                   return status=FIO_OPEN_ERR;
00400           
00401           f.getline(line, 64);
00402           if (strncmp (line, "[Parameter]", 11) == 0){ // check for a GME Dat. file
00403                   f.close ();
00404                   return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00405           }
00406   }
00407 
00408   PI_DEBUG (DBG_L2, "It's a gnu File!" );
00409   
00410   cfname = strdup (fname);
00411   nb = FileCheck (cfname, &ft, &fhead, &fh);
00412 
00413   if (nb<=0)
00414           return FIO_NO_GNUFILE;
00415  
00416   if (nb > (4096*4096)) // just a savety limit, may be removed (no .dat valid magic available)
00417           return FIO_NO_GNUFILE;
00418 
00419   PI_DEBUG (DBG_L2, "nb=" << nb );
00420   
00421   // load additional Info from File name.xxx.sklinfo or /tmp/labinfo if exists
00422   ifstream labf;
00423   char *infoname = new char[strlen(cfname)+10];
00424   strcpy(infoname,cfname);
00425   strcat(infoname, ".sklinfo");
00426   
00427   free(cfname);
00428   
00429   labf.open(infoname,ios::in);
00430   PI_DEBUG (DBG_L2, "Looking for:" << infoname );
00431   if(labf.good()){
00432           PI_DEBUG (DBG_L2, "LABI:" << infoname );
00433   } else {
00434           labf.open("/tmp/labinfo.asc",ios::in);
00435           if(labf.good())
00436                   PI_DEBUG (DBG_L2, "LABI:" << "/tmp/labinfo" );
00437   }
00438   
00439   struct { int valid; double xmin,xmax,ymin,ymax; int ydir; int xn,yn; char labx[40], laby[40], tit[40], prec[20]; } sklinfo;
00440   sklinfo.valid = 0;
00441   sklinfo.ydir  = 1;
00442   if(labf.good()){
00443     labf >> sklinfo.labx >> sklinfo.xmin >> sklinfo.xmax >> sklinfo.xn;
00444     labf >> sklinfo.laby >> sklinfo.ymin >> sklinfo.ymax >> sklinfo.yn;
00445     labf.getline(sklinfo.tit, sizeof(sklinfo.tit)); // rest of line...
00446     labf.getline(sklinfo.tit, sizeof(sklinfo.tit));
00447     if(sklinfo.ymin > sklinfo.ymax){
00448       double ytmp;
00449       sklinfo.ydir = -1;
00450       ytmp = sklinfo.ymax;
00451       sklinfo.ymax = sklinfo.ymin;
00452       sklinfo.ymin = ytmp;
00453     }
00454     PI_DEBUG (DBG_L2, "LabDataX:" << sklinfo.labx << ": " << sklinfo.xmin << " .. " << sklinfo.xmax );
00455     PI_DEBUG (DBG_L2, "LabDataY:" << sklinfo.laby << ": " << sklinfo.ymin << " .. " << sklinfo.ymax );
00456   }
00457   
00458   if(( buf = (char*)salloc(nb,1)) == 0){
00459     XSM_SHOW_ALERT(ERR_SORRY, ERR_NOMEM," ",1);
00460     return FIO_NO_MEM;
00461   }
00462   
00463   if (FileRead (fh, buf, nb) != 0) {
00464     XSM_SHOW_ALERT(ERR_SORRY, ERR_FILEREAD,fname,1);
00465     sfree (buf);
00466     return FIO_READ_ERR;
00467   }
00468   
00469   if(ft == CPXFIL){
00470     XSM_SHOW_ALERT(ERR_SORRY, ERR_NOGNUCPX,fname,1);
00471     sfree (buf);
00472     return FIO_NO_GNUFILE;
00473   }
00474   if (ft == DATFIL){
00475     anz = (long)fhead.kopf.nx[0]*(long)fhead.kopf.ny[0];
00476     scan->mem2d->Resize(scan->data.s.nx = (int)fhead.kopf.nx[0], 
00477                         scan->data.s.ny = (int)fhead.kopf.ny[0]);
00478   }
00479   else{
00480     anz = (long)fhead.xydim.x*(long)fhead.xydim.y;
00481     scan->mem2d->Resize(scan->data.s.nx = (int)fhead.xydim.x, 
00482                         scan->data.s.ny = (int)fhead.xydim.y,
00483                         ft == FLTFIL ? ZD_FLOAT :
00484                         ft == DBLFIL ? ZD_DOUBLE :
00485                         ZD_SHORT
00486                         );
00487     PI_DEBUG (DBG_L2, "nanz,x,y=" << anz << " " 
00488          << (int)fhead.xydim.x << " " << (int)fhead.xydim.y << " " );
00489   }
00490   
00491   scan->data.s.ntimes  = 1;
00492   scan->data.s.nvalues = 1;
00493   
00494   pb = (unsigned char*)buf;
00495   ps = (short*)buf;
00496   pf = (float*)buf;
00497   pd = (double*)buf;
00498   for(j=0; j<scan->data.s.ny; j++)
00499     for(i=0; i<scan->data.s.nx; i++)
00500       switch(ft){
00501       case BYTFIL: case PGMFIL: 
00502         scan->mem2d->PutDataPkt((SHT)(*pb++), i, sklinfo.ydir>0? j:scan->data.s.ny-1-j); break;
00503       case DATFIL: case SHTFIL: 
00504         scan->mem2d->PutDataPkt((SHT)(*ps++), i, sklinfo.ydir>0? j:scan->data.s.ny-1-j);
00505         break;
00506       case FLTFIL: 
00507         scan->mem2d->PutDataPkt(*pf++, i, sklinfo.ydir>0? j:scan->data.s.ny-1-j); break;
00508       case DBLFIL: 
00509         scan->mem2d->PutDataPkt(*pd++, i, sklinfo.ydir>0? j:scan->data.s.ny-1-j); break;
00510         //      case CPXFIL: mem2d->PutDataPkt((CNT)(*pd++), i, sklinfo.ydir>0? j:scan->data.s.ny-1-j); break;
00511       default: break;
00512       }
00513   sfree(buf);
00514   
00515   if(getlogin()){
00516     scan->data.ui.SetUser (getlogin());
00517   }
00518   else{
00519     scan->data.ui.SetUser ("unkonwn user");
00520   }
00521   time_t t; // Scan - Startzeit eintragen 
00522   time(&t);
00523   gchar *tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00524   scan->data.ui.SetName (fname);
00525   scan->data.ui.SetOriginalName (fname);
00526   switch(ft){
00527   case BYTFIL: scan->data.ui.SetType ("Gnu Type: BYT "); break;
00528   case PGMFIL: scan->data.ui.SetType ("Gnu Type: PGM "); break;
00529   case DATFIL: scan->data.ui.SetType ("Gnu Type: DAT "); break;
00530   case SHTFIL: scan->data.ui.SetType ("Gnu Type: SHT "); break;
00531   case FLTFIL: scan->data.ui.SetType ("Gnu Type: FLT "); break;
00532   case DBLFIL: scan->data.ui.SetType ("Gnu Type: DBL "); break;
00533   case CPXFIL: scan->data.ui.SetType ("Gnu Type: CPX "); break;
00534   default: scan->data.ui.SetType ("Gnu Type: ??? "); break;
00535   }
00536   
00537   if(labf.good()){
00538     labf.close();
00539     gchar *tmp=g_strconcat(scan->data.ui.comment, " + Skl.Info from File", NULL);
00540     scan->data.ui.SetComment (tmp);
00541     g_free(tmp);
00542     scan->data.s.rx = sklinfo.xmax-sklinfo.xmin;
00543     scan->data.s.ry = sklinfo.ymax-sklinfo.ymin;
00544     scan->data.s.dx = (sklinfo.xmax-sklinfo.xmin)/scan->data.s.nx;
00545     scan->data.s.dy = (sklinfo.ymin-sklinfo.ymax)/scan->data.s.ny;
00546     scan->data.s.dz = 1;
00547     scan->data.s.x0 = sklinfo.xmin;
00548     scan->data.s.y0 = sklinfo.ymax;
00549     scan->data.s.alpha = 0;
00550     scan->mem2d->data->MkYLookup(sklinfo.ymax, sklinfo.ymin);
00551     scan->mem2d->data->MkXLookup(sklinfo.xmin, sklinfo.xmax);
00552     tmp=g_strconcat (scan->data.ui.comment, 
00553                      "Imported by xxsm from\n",
00554                      fname, "\n",
00555                      sklinfo.tit,
00556                      NULL);
00557     scan->data.ui.SetComment (tmp);
00558     g_free(tmp);
00559   }
00560   else{
00561     gchar *tmp=g_strconcat (scan->data.ui.comment, 
00562                             "Imported by xxsm from\n", fname,
00563                             NULL);
00564     scan->data.ui.SetComment (tmp);
00565     g_free(tmp);
00566     scan->data.s.dx = 1;
00567     scan->data.s.dy = 1;
00568     scan->data.s.dz = 1;
00569     scan->data.s.rx = scan->data.s.nx;
00570     scan->data.s.ry = scan->data.s.ny;
00571     scan->data.s.x0 = 0;
00572     scan->data.s.y0 = 0;
00573     scan->data.s.alpha = 0;
00574     scan->mem2d->data->MkXLookup(-scan->data.s.rx/2., scan->data.s.rx/2.);
00575     scan->mem2d->data->MkYLookup(-scan->data.s.ry/2., scan->data.s.ry/2.);
00576   }
00577   scan->data.display.cpshigh       = 1e3;
00578   scan->data.display.cpslow        = 1.;
00579   scan->data.display.cnttime       = 1.;
00580   
00581   switch(ft){
00582   case BYTFIL: case PGMFIL:
00583     scan->data.display.bright = 0.;
00584     scan->data.display.contrast = 0.25;
00585     break;
00586   default:
00587     scan->data.display.bright = 32.;
00588     scan->data.display.contrast = 0.1;
00589     break;
00590   }
00591   scan->data.display.vrange_z  = scan->data.s.dz*256.;
00592   scan->data.display.voffset_z = 0.;
00593 
00594   // gnu read done.
00595   return FIO_OK; 
00596 }
00597 
00598 
00599 FIO_STATUS PrimitiveImExportFile::d2dRead(const char *fname){
00600   D2D_SPA_DAT spa4header;
00601   ifstream f;
00602 
00603   // Am I resposible for that file, is it a "d2d" ?
00604   if (strncasecmp(fname+strlen(fname)-3, "d2d", 3))
00605           return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00606     
00607   f.open(fname, ios::in);
00608   if(!f.good())
00609     return FIO_OPEN_ERR;
00610 
00611 
00612   f.read((char*)&spa4header, sizeof(D2D_SPA_DAT));
00613   f.seekg(0x180, ios::beg); // Auf Datenanfang Positionieren
00614   
00615   scan->data.s.ntimes  = 1;
00616   scan->data.s.nvalues = 1;
00617   scan->data.s.nx = spa4header.PointsX;
00618   scan->data.s.ny = spa4header.PointsY;
00619   scan->data.s.x0 = spa4header.Xo;
00620   scan->data.s.y0 = spa4header.Yo;
00621   scan->data.s.rx = spa4header.XYdist  ;
00622   scan->data.s.ry = spa4header.XYdist*scan->data.s.ny/scan->data.s.nx;
00623   scan->data.s.dx = scan->data.s.rx / scan->data.s.nx;
00624   scan->data.s.dy = scan->data.s.ry / scan->data.s.ny;
00625   scan->data.s.alpha = spa4header.Alpha  ;
00626   scan->data.s.GateTime   = spa4header.GateTime*1e-3;
00627   scan->data.display.cnttime = spa4header.GateTime*1e-3;;
00628   scan->data.s.dz = 1./scan->data.display.cnttime;
00629   scan->data.display.cpshigh         = spa4header.CpsHigh;
00630   scan->data.display.cpslow          = spa4header.CpsLow;
00631   scan->data.s.Energy     = spa4header.Energy ;
00632   //  spa->data.s.Focus1 = spa4header.Focus1 ;
00633   //  spa->data.s.Current = spa4header.Current;
00634   //  spa->data.s.dS=spa->data.s.usr[0]  = spa4header.usr1;
00635   //  spa->data.s.d0=spa->data.s.usr[1]  = spa4header.usr2;
00636   //  spa->data.s.usr[2]  = spa4header.usr3;
00637   //  spa->data.s.usr[3]  = spa4header.usr4;
00638   //  spa->data.s.Scanmarker = spa4header.Scanmarker;
00639   scan->data.ui.SetDateOfScan (spa4header.dateofscan);
00640   gchar *tmp = g_strdup_printf ("%s\n E=%6.1feV G=%6.1fms\nCPS[%g:%g]\n"
00641                                 "Focus=%g, Current=%g\nusr1..4: %g, %g, %g, %g",
00642                                 spa4header.comment,
00643                                 spa4header.Energy, 
00644                                 spa4header.GateTime, 
00645                                 spa4header.CpsLow, spa4header.CpsHigh,
00646                                 spa4header.Focus1, spa4header.Current,
00647                                 spa4header.usr1, spa4header.usr2, spa4header.usr3, spa4header.usr4
00648           );
00649   
00650   scan->data.ui.SetComment (tmp);
00651   g_free (tmp);
00652 
00653   //  if(*(spa4header.comment+59)=='*');
00654   //     strcpy(spa->data.s.dateofscan, spa4header.comment+60);
00655   scan->data.ui.SetUser ("D2D");
00656   scan->data.ui.SetName (fname);
00657   scan->data.ui.SetOriginalName (fname);
00658   scan->data.ui.SetType ("D2D type");
00659   
00660   scan->mem2d->Resize(scan->data.s.nx, scan->data.s.ny);
00661   // load Paraemter and Data
00662   scan->mem2d->DataD2DRead(f, spa4header.GateTime);
00663   if(f.fail()){
00664     f.close();
00665     return status=FIO_READ_ERR; 
00666   }
00667   f.close();
00668   scan->mem2d->data->MkXLookup(-scan->data.s.rx/2., scan->data.s.rx/2.);
00669   scan->mem2d->data->MkYLookup(-scan->data.s.ry/2., scan->data.s.ry/2.);
00670   return FIO_OK; 
00671 }
00672 
00673 // Nanoscope Import
00674 FIO_STATUS PrimitiveImExportFile::nscRead(const char *fname){
00675   int i,j; 
00676   FIO_STATUS ret=FIO_OK;
00677   unsigned long npix;
00678   short *buf, *ps;
00679   double size=0., zscale=0., zmag;
00680   gchar line[128];
00681 
00682   // Am I resposible for that file, is it a "nsc" plain short ?
00683   if (strncasecmp(fname+strlen(fname)-3, "nsc", 3))
00684           return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00685 
00686   FILE *f = fopen(fname,"r");
00687   for(i=0; i<145; i++){
00688     fgets(line, 127, f);
00689     if(strncmp(line, "\\Samps/line: ",12) == 0){
00690       printf("%s\n",line);
00691       scan->data.s.nx = atoi(&line[12]);
00692     }
00693     if(strncmp(line, "\\Number of lines: ",17) == 0){
00694       printf("%s\n",line);
00695       scan->data.s.ny = atoi(&line[17]);
00696     }
00697     if(strncmp(line, "\\Scan size: ",11) == 0){
00698       printf("%s\n",line);
00699       size = atof(&line[11]);
00700     }
00701     if(strncmp(line, "\\Z scale: ",10) == 0){
00702       printf("%s\n",line);
00703       zscale = atof(&line[10]);
00704     }
00705     if(strncmp(line, "\\Z magnify image: ",18) == 0){
00706       printf("%s\n",line);
00707       zmag = atof(&line[18]);
00708     }
00709   }
00710   fclose(f);
00711 
00712   if(!(buf = g_new(short, npix = scan->data.s.nx*scan->data.s.ny)))
00713     return FIO_NO_MEM;
00714 
00715   f = fopen(fname,"r");
00716   fseek(f, 0x2000L, SEEK_SET);
00717   if(fread((void*)buf, sizeof(short), npix, f) < npix){
00718     fprintf(stderr,"In File corrupt / too short !\n");
00719     //    tmp=info; info=g_str_concat(tmp, "In File corrupt / too short !\n");
00720     ret=FIO_NSC_ERR;
00721   }
00722   fclose(f);
00723 
00724   scan->mem2d->Resize(scan->data.s.nx,
00725                       scan->data.s.ny,
00726                       ZD_SHORT
00727                       );
00728 
00729   scan->data.s.ntimes  = 1;
00730   scan->data.s.nvalues = 1;
00731   
00732   ps=buf;
00733   for(j=0; j<scan->data.s.ny; j++)
00734     for(i=0; i<scan->data.s.nx; i++)
00735       scan->mem2d->PutDataPkt(*ps++, i, j);
00736   
00737   g_free(buf);
00738 
00739   if(getlogin()){
00740     scan->data.ui.SetUser (getlogin());
00741   }
00742   else{
00743     scan->data.ui.SetUser ("unkonwn user");
00744   }
00745   time_t t; // Scan - Startzeit eintragen 
00746   time(&t);
00747   gchar *tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00748   scan->data.ui.SetName (fname);
00749   scan->data.ui.SetOriginalName (fname);
00750   scan->data.ui.SetType ("Nanoscope Type "); 
00751   tmp=g_strconcat (scan->data.ui.comment,
00752                    "Imported by gxsm from\n",
00753                    fname, 
00754                    NULL);
00755   scan->data.ui.SetComment (tmp);
00756   g_free(tmp);
00757   
00758   scan->data.s.dx = size*10./(double)scan->data.s.nx;
00759   scan->data.s.dy = size*10./(double)scan->data.s.ny;
00760   scan->data.s.dz = zscale*10.;
00761   scan->data.s.rx = size*10.;
00762   scan->data.s.ry = size*10.;
00763   scan->data.s.x0 = 0;
00764   scan->data.s.y0 = 0;
00765   scan->data.s.alpha = 0;
00766   scan->mem2d->data->MkXLookup(-scan->data.s.rx/2., scan->data.s.rx/2.);
00767   scan->mem2d->data->MkYLookup(-scan->data.s.ry/2., scan->data.s.ry/2.);
00768 
00769   scan->data.display.bright = 32.;
00770   scan->data.display.contrast = 0.1;
00771   scan->data.display.ViewFlg = SCAN_V_QUICK;
00772 
00773   scan->data.display.vrange_z  = scan->data.s.dz*256.;
00774   scan->data.display.voffset_z = 0.;
00775 
00776   return ret; 
00777 }
00778 
00779 FIO_STATUS PrimitiveImExportFile::matsRead(const char *fname){
00780         int i,j;
00781         long npix;
00782         short *buf, *ps;
00783         ifstream f;
00784         
00785         // Am I resposible for that file, is it a "Mats" plain short ?
00786         if (strncasecmp(fname+strlen(fname)-3, "h16", 3))
00787                 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00788         
00789         f.open(fname, ios::in);
00790         if(!f.good())
00791                 return FIO_OPEN_ERR;
00792         
00793         double num=256.;
00794         gapp->ValueRequest("Enter X Size", "# X-Point", "Number of Points in X Direction for Matsfile",
00795                            gapp->xsm->Unity, 2., 4096., ".0f", &num);
00796         scan->data.s.nx=(int)num;
00797         
00798         num=128.;
00799         gapp->ValueRequest("Enter Y Size", "# Y-Point", "Number of Points in Y Direction for Matsfile",
00800                            gapp->xsm->Unity, 2., 4096., ".0f", &num);
00801         scan->data.s.ny=(int)num;
00802         
00803         
00804         if(!(buf = g_new(short, npix = scan->data.s.nx*scan->data.s.ny)))
00805                 return FIO_NO_MEM;
00806         
00807         f.read((char*)buf, sizeof(short)*npix);
00808         
00809         
00810         scan->mem2d->Resize(scan->data.s.nx,
00811                             scan->data.s.ny,
00812                             ZD_SHORT
00813                 );
00814         
00815         scan->data.s.ntimes  = 1;
00816         scan->data.s.nvalues = 1;
00817         
00818         ps=buf;
00819         for(j=0; j<scan->data.s.ny; j++)
00820                 for(i=0; i<scan->data.s.nx; i++)
00821                         scan->mem2d->PutDataPkt(*ps++, i, j);
00822         
00823         g_free(buf);
00824         
00825         if(getlogin()){
00826                 scan->data.ui.SetUser (getlogin());
00827         }
00828         else{
00829                 scan->data.ui.SetUser ("unkonwn user");
00830         }
00831         time_t t; // Scan - Startzeit eintragen 
00832         time(&t);
00833         gchar *tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00834         scan->data.ui.SetName (fname);
00835         scan->data.ui.SetOriginalName (fname);
00836         scan->data.ui.SetType ("Mats Type "); 
00837         tmp=g_strconcat (scan->data.ui.comment,
00838                          "Imported by gxsm from\n",
00839                          fname, 
00840                          NULL);
00841         scan->data.ui.SetComment (tmp);
00842         g_free(tmp);
00843         
00844         scan->data.s.dx = 1;
00845         scan->data.s.dy = 1;
00846         scan->data.s.dz = 1;
00847         scan->data.s.rx = scan->data.s.nx;
00848         scan->data.s.ry = scan->data.s.ny;
00849         scan->data.s.x0 = 0;
00850         scan->data.s.y0 = 0;
00851         scan->data.s.alpha = 0;
00852 
00853         scan->data.orgmode = SCAN_ORG_CENTER;
00854         scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00855         scan->mem2d->data->MkYLookup (-scan->data.s.ry/2., scan->data.s.ry/2.);
00856         
00857         scan->data.display.bright = 0.;
00858         scan->data.display.contrast = 4.;
00859         scan->data.display.ViewFlg = SCAN_V_DIRECT;
00860 
00861         scan->data.display.vrange_z  = scan->data.s.dz*100.;
00862         scan->data.display.voffset_z = 0.;
00863 
00864         return FIO_OK; 
00865 }
00866 
00867 
00868 
00869 
00870 FIO_STATUS PrimitiveImExportFile::Write(){
00871   FILETYPE ft;
00872   int MapMode=1;
00873   int i,j,Nx,Ny;
00874   long nb, PixSize;
00875   char *buf;
00876   char *pb;
00877   short *ps;
00878   float *pf;
00879   double *pd;
00880   FILEHEAD fhead;   /* union  class */
00881   const char *fname;
00882   double val=0.;
00883   int maxcol=1024;
00884   unsigned char tgapal[1024][3];
00885   PixSize=0;
00886 
00887   if(strlen(name)>0)
00888     fname = (const char*)name;
00889   else
00890     fname = gapp->file_dialog("File Export: all GNU, PGM, TGA"," ","*.[FfBbDdSsCcPpTt][LlYyAaHhPpBbGg][TtXxLlMmAa]","","gnuwrite");
00891   if (fname == NULL) return FIO_NO_NAME;
00892   if (strncmp(fname+strlen(fname)-4,".sht",4) == 0)
00893     ft=SHTFIL, PixSize=sizeof(short);
00894   else
00895     if (strncmp(fname+strlen(fname)-4,".flt",4) == 0)
00896       ft=FLTFIL, PixSize=sizeof(float);
00897     else
00898       if (strncmp(fname+strlen(fname)-4,".dbl",4) == 0)
00899         ft=DBLFIL, PixSize=sizeof(double);
00900       else
00901         if (strncmp(fname+strlen(fname)-4,".byt",4) == 0)
00902           ft=BYTFIL, PixSize=sizeof(char);
00903         else
00904           if (strncmp(fname+strlen(fname)-4,".pgm",4) == 0)
00905             ft=PGMFIL, PixSize=sizeof(char);
00906           else
00907             if (strncmp(fname+strlen(fname)-4,".tga",4) == 0)
00908               ft=TGAFIL, PixSize=sizeof(short);
00909             else{
00910               XSM_SHOW_ALERT(ERR_SORRY, ERR_WRONGGNUTYPE,fname,1);
00911               return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00912             }
00913   nb = (Nx=scan->mem2d->GetNx()) * (Ny=scan->mem2d->GetNy()) * PixSize;
00914   if(( buf = (char*)salloc(nb,1)) == 0){
00915     XSM_SHOW_ALERT(ERR_SORRY, ERR_NOMEM," ",1);
00916     return FIO_NO_MEM;
00917   }
00918 
00919   pb = (char*)buf;
00920   ps = (short*)buf;
00921   pf = (float*)buf;
00922   pd = (double*)buf;
00923 
00924 #define MAXGREYS 255
00925 #define GFAC     (double)((MAXGREYS+1)/IMGMAXCOLORS)
00926 
00927   if(scan->data.display.ViewFlg == SCAN_V_DIRECT && ft == TGAFIL){
00928     MapMode=1+XSM_SHOW_CHOICE(Q_DIRECTEXPORT, fname, Q_DIRECTMODE, 3, L_8bit, L_16bit, L_24bit, 1);
00929   }
00930   else
00931     switch(ft){
00932     case PGMFIL:
00933     case BYTFIL: MapMode=1; break;
00934     default: MapMode=2; break;
00935     }
00936 
00937   if(MapMode == 3){
00938           int cval;
00939   
00940           ifstream cpal;
00941           char pline[256];
00942           int r,g,b,nx,ny;
00943           cpal.open(xsmres.Palette, ios::in);
00944           PI_DEBUG (DBG_L2, "Using Palette: " << xsmres.Palette );
00945           if(cpal.good()){
00946                   cpal.getline(pline, 255);
00947                   cpal.getline(pline, 255);
00948                   cpal >> nx >> ny;
00949                   cpal.getline(pline, 255);
00950                   cpal.getline(pline, 255);
00951                   
00952                   for(maxcol=min(nx, 1024), cval=0; cval<maxcol; ++cval){
00953                           cpal >> r >> g >> b ;
00954                           tgapal[cval][0] = r;
00955                           tgapal[cval][1] = g;
00956                           tgapal[cval][2] = b;
00957                   }
00958 
00959                   cpal.close();
00960           }
00961           else{
00962                   PI_DEBUG (DBG_L2, "Using Palette faild, fallback to greyscale!" );
00963                   for (maxcol=64, cval=0; cval<maxcol; ++cval)
00964                           tgapal[cval][2] = tgapal[cval][1] = tgapal[cval][0] = cval * 255 / maxcol; 
00965           }
00966           PI_DEBUG (DBG_L2, "MapMode: " << MapMode );
00967           PI_DEBUG (DBG_L2, "MaxCol#: " << maxcol );
00968           PI_DEBUG (DBG_L2, "VFlg:    " << scan->data.display.ViewFlg );
00969   }
00970 
00971   for(j=0; j<Ny; j++)
00972     for(i=0; i<Nx; i++){
00973       switch(scan->data.display.ViewFlg){ 
00974       case SCAN_V_QUICK:
00975         val=((val=(GFAC*(scan->mem2d->GetDataPktLineReg(i,j)*scan->data.display.contrast+scan->data.display.bright))) > MAXGREYS ? MAXGREYS : val < 0 ? 0 : val);
00976         break;
00977       case SCAN_V_DIRECT:
00978         if(MapMode==2) // 16 bit Pov
00979           val=scan->mem2d->GetDataPkt(i,j); 
00980         else
00981           if(MapMode==1) // 8 bit
00982             val=((val=(GFAC*(scan->mem2d->GetDataPkt(i,j)*scan->data.display.contrast+scan->data.display.bright))) > MAXGREYS ? MAXGREYS : val < 0 ? 0 : val);
00983           else // 24bit
00984             val=((val=(scan->mem2d->GetDataPkt(i,j)*scan->data.display.contrast+scan->data.display.bright)) >= maxcol ? maxcol-1 : val < 0 ? 0 : val);
00985         break;
00986       case SCAN_V_PERIODIC:
00987         val=(SHT)(GFAC*(scan->mem2d->GetDataPkt(i,j)*scan->data.display.contrast+scan->data.display.bright)) & MAXGREYS;
00988         break;
00989       case SCAN_V_HORIZONTAL:
00990         val=(val=GFAC*(scan->mem2d->GetDataPktHorizont(i,j)*scan->data.display.contrast+scan->data.display.bright)) > MAXGREYS ? MAXGREYS : val < 0 ? 0 : val;
00991           break;
00992       default: // primitive
00993         //      val=(scan->mem2d->GetDataPkt(i,j))&MAXGREYS; 
00994         break;
00995       }
00996 
00997       switch(ft){
00998       case BYTFIL: case PGMFIL: *pb++ = (char)val; break;
00999         //      case SHTFIL: *ps++ = (short)((double)(mem2d->GetDataPkt(i, j) - CntLow)*65535./CntRange - 32767.); break;
01000       case TGAFIL:
01001       case SHTFIL: *ps++ = (short)val; break;
01002       case FLTFIL: *pf++ = (float)scan->mem2d->GetDataPkt(i,j); break;
01003       case DBLFIL: *pd++ = (double)scan->mem2d->GetDataPkt(i,j); break;
01004         //      case CPXFIL: mem2d->PutDataPkt((CNT)(*pd++), i, j); break;
01005       default: break;
01006       }
01007     }
01008   if(ft == TGAFIL){
01009     ofstream f;
01010     TGA_head TGAhead;
01011     TGA_pix *TGAline;
01012     long LineSize;
01013     unsigned short Val;
01014      /* Speicher für eine Zeile */
01015     TGAline = (TGA_pix *) malloc( LineSize=Nx*sizeof(TGA_pix));
01016     if (TGAline == NULL) {
01017       return FIO_NO_MEM;
01018     }
01019     memset(TGAline, 0, LineSize);
01020     
01021     /* open file, schreibe Header */
01022     f.open(fname, ios::out | ios::trunc);
01023     if(!f.good()){
01024       sfree (buf);
01025       return FIO_WRITE_ERR;
01026     }
01027 
01028     TGAhead.k[0]=0; TGAhead.k[1]=2;
01029     TGAhead.k[2]=0; TGAhead.k[3]=0;
01030     TGAhead.k[4]=0; TGAhead.k[5]=0;
01031     TGAhead.pixsz = 0x2018;
01032     TGAhead.x = (short)Nx;
01033     TGAhead.y = (short)Ny;
01034     if (WORDS_BIGENDIAN){
01035             PI_DEBUG (DBG_L2, "TGA Export, fix of WORDS_BIGENDIAN!!" );
01036             // correct byteorder
01037             unsigned char *swp = (unsigned char*)&TGAhead;
01038             for(unsigned int ii=0; ii<sizeof(TGA_head); ii+=2){
01039                     unsigned char tmp = *swp;
01040                     swp[0] = swp[1];
01041                     swp[1] = tmp;
01042                     swp += 2;
01043             }
01044     }
01045     f.write((char *)&TGAhead,sizeof(TGA_head));
01046 
01047     ps = (short*)buf;
01048     for(i=0;i<Ny;i++){
01049       switch(scan->data.display.ViewFlg){ 
01050       case SCAN_V_QUICK:
01051         for(j=0;j<Nx;j++){
01052           Val = (unsigned short)(*ps++);
01053           (TGAline+j)->val[0] = (unsigned char)(Val&0x00ff);
01054           (TGAline+j)->val[1] = (unsigned char)(Val&0x00ff);
01055           (TGAline+j)->val[2] = (unsigned char)(Val&0x00ff);
01056         }
01057         break;
01058       case SCAN_V_DIRECT:
01059         if(MapMode==2){ // 16bit für Povray
01060           for(j=0;j<Nx;j++){
01061             Val = (unsigned short)((long)(-(*ps++))+32767L);
01062             (TGAline+j)->val[1] = (unsigned char)(Val&0x00ff);
01063             (TGAline+j)->val[2] = (unsigned char)((Val>>8)&0x00ff);
01064           }
01065         }
01066         else
01067           if(MapMode==3){ // 24bit True Color
01068             for(j=0;j<Nx;j++){
01069               Val = (unsigned short)(*ps++);
01070               if(Val>=maxcol) Val=maxcol-1;
01071               (TGAline+j)->val[0] = tgapal[Val][2];
01072               (TGAline+j)->val[1] = tgapal[Val][1];
01073               (TGAline+j)->val[2] = tgapal[Val][0];
01074             }
01075           }
01076           else // 8bit Grey
01077             for(j=0;j<Nx;j++){
01078               Val = (unsigned short)(*ps++);
01079               (TGAline+j)->val[0] = (unsigned char)(Val&0x00ff);
01080               (TGAline+j)->val[1] = (unsigned char)(Val&0x00ff);
01081               (TGAline+j)->val[2] = (unsigned char)(Val&0x00ff);
01082             }
01083         break;
01084       }
01085       f.write((char*)TGAline, LineSize); 
01086     
01087       if(f.fail()){
01088         f.close();
01089         return FIO_WRITE_ERR; // FIO_DISK_FULL
01090       }
01091     }
01092     f.close();
01093     free(TGAline); 
01094   }
01095   else{
01096     fhead.xydim.x = Nx;
01097     fhead.xydim.y = Ny;
01098     
01099     if(FileWrite((char*)fname, buf, &fhead) != 0){
01100       sfree (buf);
01101       return FIO_WRITE_ERR;
01102     }
01103   }
01104   sfree(buf);
01105 
01106   return FIO_OK; 
01107 }
01108 
01109 // Plugin's Notify Cb's, registeres to be called on file load/save to check file
01110 // return via filepointer, it is set to Zero or passed as Zero if file has been processed!
01111 
01112 static void primitive_im_export_filecheck_load_callback (gpointer data ){
01113         gchar **fn = (gchar**)data;
01114         if (*fn){
01115                 PI_DEBUG (DBG_L2, "Check File: primitive_im_export_filecheck_load_callback called with >"
01116                      << *fn << "<" );
01117 
01118                 Scan *dst = gapp->xsm->GetActiveScan();
01119                 if(!dst){ 
01120                         gapp->xsm->ActivateFreeChannel();
01121                         dst = gapp->xsm->GetActiveScan();
01122                 }
01123                 PrimitiveImExportFile fileobj (dst, *fn);
01124 
01125                 FIO_STATUS ret = fileobj.Read(); 
01126                 if (ret != FIO_OK){ 
01127                         // I'am responsible! (But failed)
01128                         if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
01129                                 *fn=NULL;
01130                         // no more data: remove allocated and unused scan now, force!
01131                         gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE); 
01132                         PI_DEBUG (DBG_L2, "primitive_im_export_filecheck_load: Read Error " << ((int)ret) << "!!!!!!!!" );
01133                 }else{
01134                         // got it!
01135                         *fn=NULL;
01136                         // Now update
01137 
01138                         dst->GetDataSet(gapp->xsm->data);
01139                         gapp->spm_update_all();
01140                         dst->draw();
01141                         PI_DEBUG (DBG_L2, "primitive_im_export_filecheck_load: got it!" );
01142                 }
01143         }else{
01144                 PI_DEBUG (DBG_L2, "primitive_im_export_filecheck_load: Skipping" );
01145         }
01146 }
01147 
01148 static void primitive_im_export_filecheck_save_callback (gpointer data ){
01149         gchar **fn = (gchar**)data;
01150         if (*fn){
01151                 Scan *src;
01152                 PI_DEBUG (DBG_L2, "Check File: primitive_im_export_filecheck_save_callback called with >"
01153                      << *fn << "<" );
01154 
01155                 PrimitiveImExportFile fileobj (src = gapp->xsm->GetActiveScan(), *fn);
01156 
01157                 FIO_STATUS ret;
01158                 ret = fileobj.Write(); 
01159 
01160                 if(ret != FIO_OK){
01161                         // I'am responsible! (But failed)
01162                         if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
01163                                 *fn=NULL;
01164                         PI_DEBUG (DBG_L2, "Write Error " << ((int)ret) << "!!!!!!!!" );
01165                 }else{
01166                         // write done!
01167                         *fn=NULL;
01168                 }
01169         }else{
01170                 PI_DEBUG (DBG_L2, "primitive_im_export_filecheck_save: Skipping >" << *fn << "<" );
01171         }
01172 }
01173 
01174 // Menu Cb Fkt
01175 
01176 static void primitive_im_export_import_callback(GtkWidget *w, void *data){
01177 //                                "known extensions: pgm h16 nsc d2d dat sht byt flt dbl",
01178         gchar *fn = gapp->file_dialog("Primitive Auto Import", NULL,
01179                                   "*.[PpHhNnDdSsBbFf][Gg1Ss2AaHhYyLlBb][Mm6CcDdTtLl]", 
01180                                   NULL, "primitive-Import");
01181 
01182     PI_DEBUG (DBG_L2, "FLDLG-IM::" << fn );
01183     primitive_im_export_filecheck_load_callback (&fn );
01184 }
01185 
01186 static void primitive_im_export_export_callback(GtkWidget *w, void *data){
01187 //                                    "known extensions: pgm tga dat sht byt flt dbl",
01188         gchar *fn = gapp->file_dialog("Primitive Auto Export", NULL,
01189                                       "*.[PpTtDdSsBbFf][GgAaHhYyLlBb][MmAaTtLl]",
01190                                       "","primitive-Export");
01191 
01192     PI_DEBUG (DBG_L2, "FLDLG-EX::" << fn );
01193     primitive_im_export_filecheck_save_callback (&fn );
01194 }

Generated on Sat Apr 1 09:04:26 2006 for GXSM by  doxygen 1.4.6