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 #include <gtk/gtk.h>
00067 #include "config.h"
00068 #include "gxsm/plugin.h"
00069 #include "gxsm/dataio.h"
00070 #include "gxsm/action_id.h"
00071
00072 #include <stdio.h>
00073 #include <time.h>
00074 #include <math.h>
00075 #include <string.h>
00076
00077 using namespace std;
00078
00079 #define IMGMAXCOLORS 64
00080
00081
00082 static void rhk_spm32_import_init (void);
00083 static void rhk_spm32_import_query (void);
00084 static void rhk_spm32_import_about (void);
00085 static void rhk_spm32_import_configure (void);
00086 static void rhk_spm32_import_cleanup (void);
00087
00088 static void rhk_spm32_import_filecheck_load_callback (gpointer data );
00089 static void rhk_spm32_import_filecheck_save_callback (gpointer data );
00090
00091 static void rhk_spm32_import_import_callback (GtkWidget *w, void *data);
00092 static void rhk_spm32_import_export_callback (GtkWidget *w, void *data);
00093
00094
00095 GxsmPlugin rhk_spm32_import_pi = {
00096 NULL,
00097 NULL,
00098 0,
00099 NULL,
00100
00101
00102 "RHK_SPM32_Import",
00103 NULL,
00104
00105 "Import of SPM32 files from RHK STM-1000 systems.",
00106 "Percy Zahl",
00107 N_("_File/_Import/"),
00108 N_("RHK SPM32"),
00109 N_("Import RHK SPM32 files."),
00110 N_("RHK SPM32 file import."),
00111 NULL,
00112 NULL,
00113 rhk_spm32_import_init,
00114 rhk_spm32_import_query,
00115
00116
00117 rhk_spm32_import_about,
00118
00119
00120 rhk_spm32_import_configure,
00121
00122
00123 NULL,
00124
00125
00126 rhk_spm32_import_cleanup
00127 };
00128
00129
00130 static const char *about_text = N_("Gxsm RHK SPM32 file import plug-in\n\n"
00131 " "
00132 );
00133
00134 static const char *file_mask = "*.sm2";
00135
00136
00137
00138 GxsmPlugin *get_gxsm_plugin_info ( void ){
00139 rhk_spm32_import_pi.description = g_strdup_printf(N_("Gxsm rhk_spm32_import plugin %s"), VERSION);
00140 return &rhk_spm32_import_pi;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 static void rhk_spm32_import_query(void)
00152 {
00153 gchar **path = g_strsplit (rhk_spm32_import_pi.menupath, ",", 2);
00154 gchar **entry = g_strsplit (rhk_spm32_import_pi.menuentry, ",", 2);
00155 gchar **help = g_strsplit (rhk_spm32_import_pi.help, ",", 2);
00156
00157 static GnomeUIInfo menuinfo_i[] = {
00158 { GNOME_APP_UI_ITEM, NULL, NULL,
00159 NULL, NULL,
00160 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN,
00161 0, GDK_CONTROL_MASK, NULL },
00162 GNOMEUIINFO_END
00163 };
00164 menuinfo_i[0].label = entry[0];
00165 menuinfo_i[0].hint = help[0];
00166 menuinfo_i[0].moreinfo = (gpointer) rhk_spm32_import_import_callback;
00167 menuinfo_i[0].user_data = rhk_spm32_import_pi.name;
00168
00169 gnome_app_insert_menus (
00170 GNOME_APP(rhk_spm32_import_pi.app->getApp()),
00171 path[0],
00172 menuinfo_i
00173 );
00174
00175
00176 static GnomeUIInfo menuinfo_e[] = {
00177 { GNOME_APP_UI_ITEM, NULL, NULL,
00178 NULL, NULL,
00179 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_SAVE,
00180 0, GDK_CONTROL_MASK, NULL },
00181 GNOMEUIINFO_END
00182 };
00183 menuinfo_e[0].label = entry[1];
00184 menuinfo_e[0].hint = help[1];
00185 menuinfo_e[0].moreinfo = (gpointer) rhk_spm32_import_export_callback;
00186 menuinfo_e[0].user_data = rhk_spm32_import_pi.name;
00187
00188
00189
00190
00191
00192
00193
00194 if(rhk_spm32_import_pi.status) g_free(rhk_spm32_import_pi.status);
00195 rhk_spm32_import_pi.status = g_strconcat (
00196 N_("Plugin query has attached "),
00197 rhk_spm32_import_pi.name,
00198 N_(": File IO Filters are ready to use."),
00199 NULL);
00200
00201
00202 g_strfreev (path);
00203 g_strfreev (entry);
00204 g_strfreev (help);
00205
00206
00207
00208
00209 rhk_spm32_import_pi.app->ConnectPluginToLoadFileEvent (rhk_spm32_import_filecheck_load_callback);
00210 rhk_spm32_import_pi.app->ConnectPluginToSaveFileEvent (rhk_spm32_import_filecheck_save_callback);
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220 static void rhk_spm32_import_init(void)
00221 {
00222 PI_DEBUG (DBG_L2, "rhk_spm32_import Plugin Init");
00223 }
00224
00225
00226 static void rhk_spm32_import_about(void)
00227 {
00228 const gchar *authors[] = { rhk_spm32_import_pi.authors, NULL};
00229 gtk_widget_show(gnome_about_new ( rhk_spm32_import_pi.name,
00230 VERSION,
00231 N_("(C) 2001 the Free Software Foundation"),
00232 about_text,
00233 authors,
00234 NULL, NULL, NULL
00235 ));
00236 }
00237
00238
00239 static void rhk_spm32_import_configure(void)
00240 {
00241 if(rhk_spm32_import_pi.app)
00242 rhk_spm32_import_pi.app->message("rhk_spm32_import Plugin Configuration");
00243 }
00244
00245
00246 static void rhk_spm32_import_cleanup(void)
00247 {
00248 gchar **path = g_strsplit (rhk_spm32_import_pi.menupath, ",", 2);
00249 gchar **entry = g_strsplit (rhk_spm32_import_pi.menuentry, ",", 2);
00250
00251 gchar *tmp = g_strconcat (path[0], entry[0], NULL);
00252 gnome_app_remove_menus (GNOME_APP (rhk_spm32_import_pi.app->getApp()), tmp, 1);
00253 g_free (tmp);
00254
00255
00256
00257
00258
00259 g_strfreev (path);
00260 g_strfreev (entry);
00261
00262 PI_DEBUG (DBG_L2, "Plugin Cleanup done.");
00263 }
00264
00265
00266 class RHK_SPM32_ImportFile : public Dataio{
00267 public:
00268 RHK_SPM32_ImportFile(Scan *s, const char *n) : Dataio(s,n){ };
00269 virtual FIO_STATUS Read();
00270 virtual FIO_STATUS Write();
00271 private:
00272 FIO_STATUS import(const char *fname);
00273 };
00274
00275
00276 FIO_STATUS RHK_SPM32_ImportFile::Read(){
00277 FIO_STATUS ret;
00278 gchar *fname=NULL;
00279
00280 fname = (gchar*)name;
00281
00282
00283 if (fname == NULL || strlen(fname) < 4)
00284 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00285
00286
00287
00288 ifstream f;
00289 f.open(fname, ios::in);
00290 if(!f.good()){
00291 PI_DEBUG (DBG_L2, "File Error");
00292 return status=FIO_OPEN_ERR;
00293 }
00294 f.close();
00295
00296
00297
00298
00299 if ((ret=import (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00300 return ret;
00301
00302 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00303 }
00304
00305 FIO_STATUS RHK_SPM32_ImportFile::import(const char *fname){
00306
00307 ifstream f;
00308 char header[512];
00309 GString *comment=NULL;
00310 int datamode = 16;
00311
00312 float xscale, yscale, zscale, xyscale, angle, xoffset, yoffset, \
00313 zoffset, current, bias, period;
00314 char date[9], scantime[9], xunits[10], yunits[10], zunits[10], \
00315 label[10], text[160];
00316 long int x_size, y_size, size, type, data_type, line_type, \
00317 page_type, scandir, file_id, data_offset = 512;
00318
00319 f.open(fname, ios::in);
00320 if (!f.good())
00321 return status=FIO_OPEN_ERR;
00322
00323
00324 f.read(header, 512);
00325
00326
00327
00328 if (strncmp (header, "STiMage 3.1", 11) != 0){
00329 f.close ();
00330 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00331 }
00332 PI_DEBUG (DBG_L2, "rhk_spm32_import.C: Identified SPM32 file.");
00333
00334
00335
00336 if(getlogin()){
00337 scan->data.ui.SetUser (getlogin());
00338 }
00339 else{
00340 scan->data.ui.SetUser ("unkonwn user");
00341 }
00342 time_t t;
00343 time(&t);
00344 gchar *tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00345 scan->data.ui.SetName (fname);
00346 scan->data.ui.SetOriginalName (fname);
00347 scan->data.ui.SetType ("RHK SPM32");
00348
00349 comment = g_string_new ("Imported by Gxsm from RHK SPM32 file.\n");
00350 scan->data.ui.SetComment (comment->str);
00351 g_string_free(comment, TRUE);
00352 comment=NULL;
00353
00354
00355 scan->data.s.ntimes = 1;
00356 scan->data.s.nvalues = 1;
00357
00358
00359 scan->data.s.nx = 1;
00360 scan->data.s.ny = 1;
00361 scan->data.s.dx = 1;
00362 scan->data.s.dy = 1;
00363 scan->data.s.dz = -1;
00364 scan->data.s.rx = scan->data.s.nx;
00365 scan->data.s.ry = scan->data.s.ny;
00366 scan->data.s.x0 = 0;
00367 scan->data.s.y0 = 0;
00368 scan->data.s.alpha = 0;
00369
00370
00371 scan->data.display.cpshigh = 1e3;
00372 scan->data.display.cpslow = 1.;
00373 scan->data.display.cnttime = 1.;
00374
00375
00376 scan->data.display.bright = 32.;
00377 scan->data.display.contrast = 1.0;
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 sscanf (header + 0x20, "%d %d %d %d %d %d %d",
00389 &type, &data_type, &line_type, &x_size, &y_size, &size, &page_type);
00390
00391 if (type != 0) {
00392 f.close ();
00393 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00394 }
00395
00396 switch (data_type){
00397 case 1:
00398 break;
00399 default:
00400 f.close ();
00401 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00402 break;
00403 }
00404
00405 if ( x_size > 2048 || y_size > 2048 || x_size < 1 || y_size < 1 ) {
00406 f.close ();
00407 return FIO_INVALID_FILE;
00408 }
00409 scan->data.s.nx = x_size;
00410 scan->data.s.ny = y_size;
00411
00412 comment = g_string_new (scan->data.ui.comment);
00413 switch (page_type){
00414 case 0:
00415 g_string_sprintfa (comment, "Undefined image type.\n");
00416 break;
00417 case 1:
00418 g_string_sprintfa (comment, "Topographic image.\n");
00419 break;
00420 case 2:
00421 g_string_sprintfa (comment, "Current image.\n");
00422 break;
00423 case 3:
00424 g_string_sprintfa (comment, "Aux image.\n");
00425 break;
00426 case 4:
00427 g_string_sprintfa (comment, "Force image.\n");
00428 break;
00429 case 5:
00430 g_string_sprintfa (comment, "Signal image.\n");
00431 break;
00432 case 6:
00433 g_string_sprintfa (comment, "Image FFT transform.\n");
00434 break;
00435 default:
00436 g_string_sprintfa (comment, "Unknown page type.\n");
00437 break;
00438 }
00439 scan->data.ui.SetComment (comment->str);
00440 g_string_free(comment, TRUE);
00441 comment=NULL;
00442
00443 sscanf (header + 0x40, "X %g %g %10s",
00444 &xscale, &xoffset, xunits);
00445 scan->data.s.dx = fabs(xscale) * 1e10;
00446 scan->data.s.rx = scan->data.s.dx * scan->data.s.nx;
00447 scan->data.s.x0 = xoffset * 1e10;
00448
00449 sscanf (header + 0x60, "Y %g %g %10s",
00450 &yscale, &yoffset, yunits);
00451 scan->data.s.dy = fabs(yscale) * 1e10;
00452 scan->data.s.ry = scan->data.s.dy * scan->data.s.ny;
00453 scan->data.s.y0 = yoffset * 1e10;
00454
00455 sscanf (header + 0x80, "Z %g %g %10s",
00456 &zscale, &zoffset, zunits);
00457 scan->data.s.dz = fabs(zscale) * 1e10;
00458
00459 sscanf (header + 0xA0, "XY %g G %g",
00460 &xyscale, &angle);
00461 if (fabs(angle) < 1e-3)
00462 angle = 0.;
00463 scan->data.s.alpha = angle;
00464
00465 sscanf (header + 0xC0, "IV %g %g",
00466 ¤t, &bias);
00467 scan->data.s.Bias = bias;
00468 scan->data.s.Current = current;
00469
00470 sscanf (header + 0x100, "id %ld %ld",
00471 &file_id, &data_offset);
00472 sscanf (header + 0x140, "%20s", label);
00473 strncpy (text, header + 0x160, 160);
00474 comment = g_string_new (scan->data.ui.comment);
00475 g_string_sprintfa (comment, "id: %d; label: %s.\n", file_id, label);
00476 g_string_sprintfa (comment, "%s\n", text);
00477 scan->data.ui.SetComment (comment->str);
00478 g_string_free(comment, TRUE);
00479 comment=NULL;
00480
00481
00482
00483 f.seekg(data_offset, ios::beg);
00484 scan->mem2d->Resize (scan->data.s.nx, scan->data.s.ny, ZD_SHORT);
00485 scan->mem2d->DataRead (f, 1);
00486
00487
00488 scan->data.orgmode = SCAN_ORG_CENTER;
00489
00490 scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00491 scan->mem2d->data->MkYLookup (0., scan->data.s.ry);
00492
00493 f.close ();
00494
00495
00496 return FIO_OK;
00497 }
00498
00499
00500 FIO_STATUS RHK_SPM32_ImportFile::Write(){
00501
00502 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00503
00504 }
00505
00506
00507
00508
00509
00510
00511 static void rhk_spm32_import_filecheck_load_callback (gpointer data ){
00512 gchar **fn = (gchar**)data;
00513 if (*fn){
00514 PI_DEBUG (DBG_L2,
00515 "Check File: rhk_spm32_import_filecheck_load_callback called with >"
00516 << *fn << "<" );
00517
00518 Scan *dst = gapp->xsm->GetActiveScan();
00519 if(!dst){
00520 gapp->xsm->ActivateFreeChannel();
00521 dst = gapp->xsm->GetActiveScan();
00522 }
00523 RHK_SPM32_ImportFile fileobj (dst, *fn);
00524
00525 FIO_STATUS ret = fileobj.Read();
00526 if (ret != FIO_OK){
00527
00528 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00529 *fn=NULL;
00530
00531 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00532 PI_DEBUG (DBG_L2, "Read Error " << ((int)ret) << "!!!!!!!!" );
00533 }else{
00534
00535 *fn=NULL;
00536
00537
00538 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00539 gapp->spm_update_all();
00540 dst->draw();
00541 }
00542 }else{
00543 PI_DEBUG (DBG_L2, "rhk_spm32_import_filecheck_load: Skipping" );
00544 }
00545 }
00546
00547 static void rhk_spm32_import_filecheck_save_callback (gpointer data ){
00548 gchar **fn = (gchar**)data;
00549 if (*fn){
00550 Scan *src;
00551 PI_DEBUG (DBG_L2,
00552 "Check File: rhk_spm32_import_filecheck_save_callback called with >"
00553 << *fn << "<" );
00554
00555 RHK_SPM32_ImportFile fileobj (src = gapp->xsm->GetActiveScan(), *fn);
00556
00557 FIO_STATUS ret;
00558 ret = fileobj.Write();
00559
00560 if(ret != FIO_OK){
00561
00562 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00563 *fn=NULL;
00564 PI_DEBUG (DBG_L2, "Write Error " << ((int)ret) << "!!!!!!!!" );
00565 }else{
00566
00567 *fn=NULL;
00568 }
00569 }else{
00570 PI_DEBUG (DBG_L2, "rhk_spm32_import_filecheck_save: Skipping >" << *fn << "<" );
00571 }
00572 }
00573
00574
00575
00576 static void rhk_spm32_import_import_callback(GtkWidget *w, void *data){
00577 gchar **help = g_strsplit (rhk_spm32_import_pi.help, ",", 2);
00578 gchar *dlgid = g_strconcat (rhk_spm32_import_pi.name, "-import", NULL);
00579 gchar *fn = gapp->file_dialog(help[0], NULL, file_mask, NULL, dlgid);
00580 g_strfreev (help);
00581 g_free (dlgid);
00582 rhk_spm32_import_filecheck_load_callback (&fn );
00583 }
00584
00585 static void rhk_spm32_import_export_callback(GtkWidget *w, void *data){
00586 gchar **help = g_strsplit (rhk_spm32_import_pi.help, ",", 2);
00587 gchar *dlgid = g_strconcat (rhk_spm32_import_pi.name, "-export", NULL);
00588 gchar *fn = gapp->file_dialog(help[1], NULL, file_mask, NULL, dlgid);
00589 g_strfreev (help);
00590 g_free (dlgid);
00591 rhk_spm32_import_filecheck_save_callback (&fn );
00592 }