00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
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
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
00174 GxsmPlugin primitive_im_export_pi = {
00175 NULL,
00176 NULL,
00177 0,
00178 NULL,
00179
00180
00181 "Primitive_Im_Export",
00182 NULL,
00183
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,
00191 NULL,
00192 primitive_im_export_init,
00193 primitive_im_export_query,
00194
00195
00196 primitive_im_export_about,
00197
00198
00199 primitive_im_export_configure,
00200
00201
00202 NULL,
00203
00204
00205 primitive_im_export_cleanup
00206 };
00207
00208
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
00220
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
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
00263
00264
00265
00266
00267
00268 static void primitive_im_export_init(void)
00269 {
00270 PI_DEBUG (DBG_L2, "primitive_im_export Plugin Init" );
00271 }
00272
00273
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
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
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
00316
00317
00318
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
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
00335 if (fname == NULL || strlen(fname) < 4)
00336 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00337
00338
00339
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
00349
00350
00351 if ((ret=matsRead (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00352 return ret;
00353
00354
00355 if ((ret=d2dRead (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00356 return ret;
00357
00358
00359 if ((ret=nscRead (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00360 return ret;
00361
00362
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
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;
00381
00382 char *cfname;
00383
00384
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
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){
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))
00417 return FIO_NO_GNUFILE;
00418
00419 PI_DEBUG (DBG_L2, "nb=" << nb );
00420
00421
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));
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
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;
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
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
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);
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
00633
00634
00635
00636
00637
00638
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
00654
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
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
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
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
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;
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
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;
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;
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)
00979 val=scan->mem2d->GetDataPkt(i,j);
00980 else
00981 if(MapMode==1)
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
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:
00993
00994 break;
00995 }
00996
00997 switch(ft){
00998 case BYTFIL: case PGMFIL: *pb++ = (char)val; break;
00999
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
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
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
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
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){
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){
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
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;
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
01110
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
01128 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
01129 *fn=NULL;
01130
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
01135 *fn=NULL;
01136
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
01162 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
01163 *fn=NULL;
01164 PI_DEBUG (DBG_L2, "Write Error " << ((int)ret) << "!!!!!!!!" );
01165 }else{
01166
01167 *fn=NULL;
01168 }
01169 }else{
01170 PI_DEBUG (DBG_L2, "primitive_im_export_filecheck_save: Skipping >" << *fn << "<" );
01171 }
01172 }
01173
01174
01175
01176 static void primitive_im_export_import_callback(GtkWidget *w, void *data){
01177
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
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 }