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 #include <gtk/gtk.h>
00055 #include "config.h"
00056 #include "gxsm/plugin.h"
00057 #include "gxsm/dataio.h"
00058 #include "gxsm/action_id.h"
00059 #include "gxsm/util.h"
00060 #include "batch.h"
00061 #include "fileio.c"
00062 #include "psihdf.h"
00063
00064 using namespace std;
00065
00066 #define IMGMAXCOLORS 64
00067
00068
00069 static void PsiHDF_im_export_init (void);
00070 static void PsiHDF_im_export_query (void);
00071 static void PsiHDF_im_export_about (void);
00072 static void PsiHDF_im_export_configure (void);
00073 static void PsiHDF_im_export_cleanup (void);
00074
00075 static void PsiHDF_im_export_filecheck_load_callback (gpointer data );
00076 static void PsiHDF_im_export_filecheck_save_callback (gpointer data );
00077
00078 static void PsiHDF_im_export_import_callback (GtkWidget *w, void *data);
00079 static void PsiHDF_im_export_export_callback (GtkWidget *w, void *data);
00080
00081
00082 GxsmPlugin PsiHDF_im_export_pi = {
00083 NULL,
00084 NULL,
00085 0,
00086 NULL,
00087
00088
00089 "PsiHDF-ImExport",
00090 NULL,
00091
00092 "Im/Export of PsiHDF file format.",
00093 "Percy Zahl",
00094 N_("_File/_Import/,_File/_Export/"),
00095 N_("PsiHDF,PsiHDF"),
00096 N_("PsiHDF import,PsiHDF export"),
00097 N_("Park Scientific HDF import and export filter."),
00098 NULL,
00099 NULL,
00100 PsiHDF_im_export_init,
00101 PsiHDF_im_export_query,
00102
00103
00104 PsiHDF_im_export_about,
00105
00106
00107 PsiHDF_im_export_configure,
00108
00109
00110 NULL,
00111
00112
00113 PsiHDF_im_export_cleanup
00114 };
00115
00116
00117 static const char *about_text = N_("This GXSM plugin allows im- and export of the Park Scientifi HDF file format"
00118 " "
00119 );
00120
00121 static const char *file_mask = "*.hdf";
00122
00123
00124
00125 GxsmPlugin *get_gxsm_plugin_info ( void ){
00126 PsiHDF_im_export_pi.description = g_strdup_printf(N_("Gxsm im_export plugin %s"), VERSION);
00127 return &PsiHDF_im_export_pi;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 static void PsiHDF_im_export_query(void)
00139 {
00140 gchar **path = g_strsplit ( PsiHDF_im_export_pi.menupath, ",", 2);
00141 gchar **entry = g_strsplit ( PsiHDF_im_export_pi.menuentry, ",", 2);
00142 gchar **help = g_strsplit ( PsiHDF_im_export_pi.help, ",", 2);
00143
00144 static GnomeUIInfo menuinfo_i[] = {
00145 { GNOME_APP_UI_ITEM, NULL, NULL,
00146 NULL, NULL,
00147 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN,
00148 0, GDK_CONTROL_MASK, NULL },
00149 GNOMEUIINFO_END
00150 };
00151 menuinfo_i[0].label = entry[0];
00152 menuinfo_i[0].hint = help[0];
00153 menuinfo_i[0].moreinfo = (gpointer) PsiHDF_im_export_import_callback;
00154 menuinfo_i[0].user_data = PsiHDF_im_export_pi.name;
00155
00156 gnome_app_insert_menus (
00157 GNOME_APP(PsiHDF_im_export_pi.app->getApp()),
00158 path[0],
00159 menuinfo_i
00160 );
00161
00162
00163 static GnomeUIInfo menuinfo_e[] = {
00164 { GNOME_APP_UI_ITEM, NULL, NULL,
00165 NULL, NULL,
00166 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_SAVE,
00167 0, GDK_CONTROL_MASK, NULL },
00168 GNOMEUIINFO_END
00169 };
00170 menuinfo_e[0].label = entry[1];
00171 menuinfo_e[0].hint = help[1];
00172 menuinfo_e[0].moreinfo = (gpointer) PsiHDF_im_export_export_callback;
00173 menuinfo_e[0].user_data = PsiHDF_im_export_pi.name;
00174
00175 gnome_app_insert_menus (
00176 GNOME_APP(PsiHDF_im_export_pi.app->getApp()),
00177 path[1],
00178 menuinfo_e
00179 );
00180
00181 if(PsiHDF_im_export_pi.status) g_free(PsiHDF_im_export_pi.status);
00182 PsiHDF_im_export_pi.status = g_strconcat (
00183 N_("Plugin query has attached "),
00184 PsiHDF_im_export_pi.name,
00185 N_(": File IO Filters are ready to use."),
00186 NULL);
00187
00188
00189 g_strfreev (path);
00190 g_strfreev (entry);
00191 g_strfreev (help);
00192
00193
00194
00195
00196 PsiHDF_im_export_pi.app->ConnectPluginToLoadFileEvent (PsiHDF_im_export_filecheck_load_callback);
00197 PsiHDF_im_export_pi.app->ConnectPluginToSaveFileEvent (PsiHDF_im_export_filecheck_save_callback);
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207 static void PsiHDF_im_export_init(void)
00208 {
00209 PI_DEBUG (DBG_L2, PsiHDF_im_export_pi.name << "Plugin Init");
00210 }
00211
00212
00213 static void PsiHDF_im_export_about(void)
00214 {
00215 const gchar *authors[] = { PsiHDF_im_export_pi.authors, NULL};
00216 gtk_widget_show(gnome_about_new ( PsiHDF_im_export_pi.name,
00217 VERSION,
00218 N_("(C) 2001 the Free Software Foundation"),
00219 about_text,
00220 authors,
00221 NULL, NULL, NULL
00222 ));
00223 }
00224
00225
00226 static void PsiHDF_im_export_configure(void)
00227 {
00228 if(PsiHDF_im_export_pi.app)
00229 PsiHDF_im_export_pi.app->message("PsiHDF_im_export Plugin Configuration");
00230 }
00231
00232
00233 static void PsiHDF_im_export_cleanup(void)
00234 {
00235 gchar **path = g_strsplit (PsiHDF_im_export_pi.menupath, ",", 2);
00236 gchar **entry = g_strsplit (PsiHDF_im_export_pi.menuentry, ",", 2);
00237
00238 gchar *tmp = g_strconcat (path[0], entry[0], NULL);
00239 gnome_app_remove_menus (GNOME_APP (PsiHDF_im_export_pi.app->getApp()), tmp, 1);
00240 g_free (tmp);
00241
00242 tmp = g_strconcat (path[1], entry[1], NULL);
00243 gnome_app_remove_menus (GNOME_APP (PsiHDF_im_export_pi.app->getApp()), tmp, 1);
00244 g_free (tmp);
00245
00246 g_strfreev (path);
00247 g_strfreev (entry);
00248
00249 PI_DEBUG (DBG_L2, "Plugin Cleanup done.");
00250 }
00251
00252
00253 class PsiHDF_ImExportFile : public Dataio{
00254 public:
00255 PsiHDF_ImExportFile(Scan *s, const char *n) : Dataio(s,n){ };
00256 virtual FIO_STATUS Read();
00257 virtual FIO_STATUS Write();
00258 private:
00259 FIO_STATUS import(const char *fname);
00260 int numDD;
00261 };
00262
00263 FIO_STATUS PsiHDF_ImExportFile::Read(){
00264 FIO_STATUS ret;
00265 gchar *fname=NULL;
00266
00267 fname = (gchar*)name;
00268
00269
00270 if (fname == NULL || strlen(fname) < 4)
00271 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00272
00273
00274
00275 ifstream f;
00276 f.open(fname, ios::in);
00277 if(!f.good()){
00278 PI_DEBUG (DBG_L2, "File Fehler");
00279 return status=FIO_OPEN_ERR;
00280 }
00281 f.close();
00282
00283
00284
00285
00286 if ((ret=import (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00287 return ret;
00288
00289 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299 FIO_STATUS PsiHDF_ImExportFile::import(const char *fname){
00300 ifstream f;
00301 gchar *tmp = NULL;
00302 GString *FileList=NULL;
00303
00304 f.open(name, ios::in);
00305 if (!f.good())
00306 return status=FIO_OPEN_ERR;
00307
00308
00309 if (TRUE){
00310 f.close ();
00311 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00312 }
00313
00314 time_t t;
00315 time(&t);
00316 tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00317 scan->data.ui.SetName (fname);
00318 scan->data.ui.SetOriginalName (fname);
00319 scan->data.ui.SetType ("old dat");
00320
00321
00322
00323 scan->data.s.ntimes = 1;
00324 scan->data.s.nvalues = 1;
00325
00326
00327
00328 if(getlogin()){
00329 scan->data.ui.SetUser (getlogin());
00330 }
00331 else{
00332 scan->data.ui.SetUser ("unkonwn user");
00333 }
00334
00335
00336 scan->data.s.nx = 1;
00337 scan->data.s.ny = 1;
00338 scan->data.s.dx = 1;
00339 scan->data.s.dy = 1;
00340 scan->data.s.dz = 1;
00341 scan->data.s.rx = scan->data.s.nx;
00342 scan->data.s.ry = scan->data.s.ny;
00343 scan->data.s.x0 = 0;
00344 scan->data.s.y0 = 0;
00345 scan->data.s.alpha = 0;
00346
00347
00348 scan->data.display.cpshigh = 1e3;
00349 scan->data.display.cpslow = 1.;
00350 scan->data.display.cnttime = 1.;
00351
00352
00353 scan->data.display.bright = 32.;
00354 scan->data.display.contrast = 1.0;
00355
00356
00357
00358
00359
00360
00361
00362 FileList = g_string_new ("Imported by GXSM from old dat filetype.\n");
00363 g_string_sprintfa (FileList, "Original Filename: %s\n", fname);
00364
00365
00366 int magic;
00367 PsiHEADER header;
00368
00369 f.open(name, std::ios::in);
00370 if(!f.good())
00371 return status=FIO_OPEN_ERR;
00372
00373
00374 f.seekg(0, std::ios::beg);
00375 f.read((char*)(&magic), 4);
00376
00377 if (magic != HDF_MAGIC_NUM) {
00378 XSM_DEBUG_ERROR (DBG_L1, "PsiHDF: wrong magic number! (magic = " << magic << " )");
00379 return FIO_READ_ERR;
00380 }
00381
00382
00383 f.seekg(PSI_HEADER_OFFSET, std::ios::beg);
00384 f.read((char*)&header, sizeof(PsiHEADER));
00385
00386
00387
00388
00389
00390 # define XOFFS(X) ((char*)(&X) - (char*)(&header))
00391 f.seekg(PSI_HEADER_OFFSET + XOFFS(header.fXScanSize) + 2, std::ios::beg);
00392 f.read((char*)&header.fXScanSize, sizeof(PsiHEADER) - XOFFS(header.fXScanSize));
00393
00394 if(f.fail()){
00395 f.close();
00396 return status=FIO_READ_ERR;
00397 }
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 scan->data.s.nx = header.nCols;
00432 scan->data.s.ny = header.nRows;
00433 scan->data.s.dx = header.fXScanSize * 10000. / header.nCols;
00434 scan->data.s.dy = header.fYScanSize * 10000. / header.nRows;
00435 scan->data.s.dz = header.fDataGain * 10000.;
00436 scan->data.s.rx = scan->data.s.nx*scan->data.s.dx;
00437 scan->data.s.ry = scan->data.s.ny*scan->data.s.dy;
00438 scan->data.s.x0 = header.fXOffset * 10000.;
00439 scan->data.s.y0 = header.fYOffset * 10000.;
00440 scan->data.s.alpha = 0;
00441 scan->data.ui.SetUser ("PSI SXM");
00442 tmp = g_strconcat ("PSI Mode: ", header.szImageMode, ", Source: ", header.szSourceName, NULL);
00443 scan->data.ui.SetComment (tmp);
00444 g_free (tmp);
00445
00446
00447 scan->mem2d->Resize (scan->data.s.nx, scan->data.s.ny);
00448 f.seekg (FILE_HEADER_SIZE, std::ios::beg);
00449 scan->mem2d->DataRead (f);
00450
00451 scan->data.orgmode = SCAN_ORG_CENTER;
00452 scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00453 scan->mem2d->data->MkYLookup (-scan->data.s.ry/2., scan->data.s.ry/2.);
00454
00455 if(f.fail()){
00456 f.close();
00457 return status=FIO_READ_ERR;
00458 }
00459
00460
00461
00462
00463
00464
00465 scan->data.ui.SetComment (FileList->str);
00466 g_string_free(FileList, TRUE);
00467 FileList=NULL;
00468
00469 scan->data.s.dx = scan->data.s.rx / scan->data.s.nx;
00470 scan->data.s.dy = scan->data.s.ry / scan->data.s.ny;
00471
00472
00473
00474
00475
00476
00477
00478 scan->mem2d->Resize(scan->data.s.nx, scan->data.s.ny);
00479 scan->mem2d->DataRead(f, 1);
00480
00481 scan->data.orgmode = SCAN_ORG_CENTER;
00482 scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00483 scan->mem2d->data->MkYLookup (-scan->data.s.ry/2., scan->data.s.ry/2.);
00484
00485
00486 return FIO_OK;
00487 }
00488
00489
00490 FIO_STATUS PsiHDF_ImExportFile::Write(){
00491
00492 GtkWidget *dialog = gtk_message_dialog_new (NULL,
00493 GTK_DIALOG_DESTROY_WITH_PARENT,
00494 GTK_MESSAGE_INFO,
00495 GTK_BUTTONS_OK,
00496 N_("Sorry, not yet implemented.")
00497 );
00498 gtk_dialog_run (GTK_DIALOG (dialog));
00499 gtk_widget_destroy (dialog);
00500
00501 #if 0
00502 gchar tmp[0x4004];
00503 const gchar *fname;
00504 ofstream f;
00505
00506 memset (tmp, 0, sizeof (tmp));
00507
00508 if(strlen(name)>0)
00509 fname = (const char*)name;
00510 else
00511 fname = gapp->file_dialog("File Export: PsiHDF"," ","*.hdf","","PsiHDF write");
00512 if (fname == NULL) return FIO_NO_NAME;
00513
00514
00515 if (strncmp(fname+strlen(fname)-4,".hdf",4))
00516 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00517
00518
00519 f.open(name, ios::out);
00520 if (!f.good())
00521 return status=FIO_OPEN_ERR;
00522
00523
00524
00525
00526 int pcnt=0;
00527 std::ofstream f;
00528
00529 f.open(name, std::ios::out | std::ios::trunc);
00530 if(!f.good())
00531 return status=FIO_OPEN_ERR;
00532
00533 f.seekp(0, std::ios::beg);
00534
00535
00536 scan->data.ui.SetName (name);
00537 Kopf.ScMode = TopoGraphic;
00538 Kopf.Kennung = 0xABCE;
00539 if(strlen(scan->data.ui.user) > 39)
00540 XSM_SHOW_ALERT(HINT_WARN, HINT_UNAME_TRUNC, " ", 0);
00541 if(strlen(scan->data.ui.comment) > 255)
00542 XSM_SHOW_ALERT(HINT_WARN, HINT_COMMENT_TRUNC, " ", 0);
00543 strncpy(Kopf.UserName, scan->data.ui.user, 39);
00544 strncpy(Kopf.comment, scan->data.ui.comment, 255);
00545 Kopf.nx[pcnt] = scan->data.s.nx;
00546 Kopf.ny[pcnt] = scan->data.s.ny;
00547
00548 Kopf.dx[pcnt] = R2INT(scan->data.s.dx/gapp->xsm->Inst->XResolution());
00549 Kopf.dy[pcnt] = R2INT(scan->data.s.dy/gapp->xsm->Inst->YResolution());
00550 Kopf.x_Offset[pcnt] = R2INT(scan->data.s.x0/gapp->xsm->Inst->XResolution());
00551 Kopf.y_Offset[pcnt] = R2INT(scan->data.s.y0/gapp->xsm->Inst->XResolution());
00552 Kopf.DAtoAng_X = gapp->xsm->Inst->XResolution();
00553 Kopf.DAtoAng_Y = gapp->xsm->Inst->YResolution();
00554 Kopf.DAtoAng_Z = gapp->xsm->Inst->ZResolution();
00555 Kopf.Rotation = (short)(rint(scan->data.s.alpha));
00556 Kopf.brightfac = scan->data.display.bright;
00557 Kopf.greyfac = VRangeZ_to_Contrast (scan->data.display.vrange_z, scan->data.s.dz);
00558
00559 Kopf.forw_delay=10L;
00560 Kopf.back_delay=10L;
00561 Kopf.NumOfTopAve=1;
00562
00563
00564 f.write((const char*)&Kopf, sizeof(HEADER));
00565 if(f.fail()){
00566 f.close();
00567 return status=FIO_WRITE_ERR;
00568 }
00569
00570 scan->mem2d->DataWrite(f);
00571
00572 if(f.fail()){
00573 f.close();
00574 return status=FIO_WRITE_ERR;
00575 }
00576 f.close();
00577
00578
00579
00580 #endif
00581 return FIO_OK;
00582 }
00583
00584
00585
00586
00587
00588
00589 static void PsiHDF_im_export_filecheck_load_callback (gpointer data ){
00590 gchar **fn = (gchar**)data;
00591 if (*fn){
00592 PI_DEBUG (DBG_L2,
00593 "Check File: PsiHDF_im_export_filecheck_load_callback called with >"
00594 << *fn << "<" );
00595
00596 Scan *dst = gapp->xsm->GetActiveScan();
00597 if(!dst){
00598 gapp->xsm->ActivateFreeChannel();
00599 dst = gapp->xsm->GetActiveScan();
00600 }
00601 PsiHDF_ImExportFile fileobj (dst, *fn);
00602
00603 FIO_STATUS ret = fileobj.Read();
00604 if (ret != FIO_OK){
00605
00606 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00607 *fn=NULL;
00608
00609 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00610 PI_DEBUG (DBG_L2, "Read Error " << ((int)ret) << "!!!!!!!!" );
00611 }else{
00612
00613 *fn=NULL;
00614
00615
00616 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00617 gapp->spm_update_all();
00618 dst->draw();
00619 }
00620 }else{
00621 PI_DEBUG (DBG_L2, "PsiHDF_im_export_filecheck_load: Skipping" );
00622 }
00623 }
00624
00625 static void PsiHDF_im_export_filecheck_save_callback (gpointer data ){
00626 gchar **fn = (gchar**)data;
00627 if (*fn){
00628 Scan *src;
00629 PI_DEBUG (DBG_L2,
00630 "Check File: PsiHDF_im_export_filecheck_save_callback called with >"
00631 << *fn << "<" );
00632
00633 PsiHDF_ImExportFile fileobj (src = gapp->xsm->GetActiveScan(), *fn);
00634
00635 FIO_STATUS ret;
00636 ret = fileobj.Write();
00637
00638 if(ret != FIO_OK){
00639
00640 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00641 *fn=NULL;
00642 PI_DEBUG (DBG_L2, "Write Error " << ((int)ret) << "!!!!!!!!" );
00643 }else{
00644
00645 *fn=NULL;
00646 }
00647 }else{
00648 PI_DEBUG (DBG_L2, "PsiHDF_im_export_filecheck_save: Skipping >" << *fn << "<" );
00649 }
00650 }
00651
00652
00653
00654 static void PsiHDF_im_export_import_callback(GtkWidget *w, void *data){
00655 gchar **help = g_strsplit (PsiHDF_im_export_pi.help, ",", 2);
00656 gchar *dlgid = g_strconcat (PsiHDF_im_export_pi.name, "-import", NULL);
00657 gchar *fn = gapp->file_dialog(help[0], NULL, file_mask, NULL, dlgid);
00658 g_strfreev (help);
00659 g_free (dlgid);
00660 PsiHDF_im_export_filecheck_load_callback (&fn );
00661 }
00662
00663 static void PsiHDF_im_export_export_callback(GtkWidget *w, void *data){
00664 gchar **help = g_strsplit (PsiHDF_im_export_pi.help, ",", 2);
00665 gchar *dlgid = g_strconcat (PsiHDF_im_export_pi.name, "-export", NULL);
00666 gchar *fn = gapp->file_dialog(help[1], NULL, file_mask, NULL, dlgid);
00667 g_strfreev (help);
00668 g_free (dlgid);
00669 PsiHDF_im_export_filecheck_save_callback (&fn );
00670 }