Codebase list imview / eba15666-00c2-4d63-af5d-caf07788990a/main saveSpect.cxx
eba15666-00c2-4d63-af5d-caf07788990a/main

Tree @eba15666-00c2-4d63-af5d-caf07788990a/main (Download .tar.gz)

saveSpect.cxx @eba15666-00c2-4d63-af5d-caf07788990a/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * $Id: saveSpect.cxx,v 4.1 2008/10/27 15:34:11 hut66au Exp $
 *
 * Imview, the portable image analysis application
 * http://www.cmis.csiro.au/Hugues.Talbot/imview
 * ----------------------------------------------------------
 *
 *  Imview is an attempt to provide an image display application
 *  suitable for professional image analysis. It was started in
 *  1997 and is mostly the result of the efforts of Hugues Talbot,
 *  Image Analysis Project, CSIRO Mathematical and Information
 *  Sciences, with help from others (see the CREDITS files for
 *  more information)
 *
 *  Imview is Copyrighted (C) 1997-2001 by Hugues Talbot and was
 *  supported in parts by the Australian Commonwealth Science and 
 *  Industry Research Organisation. Please see the COPYRIGHT file 
 *  for full details. Imview also includes the contributions of 
 *  many others. Please see the CREDITS file for full details.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 * */

/*------------------------------------------------------------------------
 *
 * A save preference panel using fluid
 *
 * Hugues Talbot	27 Apr 1998
 *      
 *-----------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include "imunistd.h"
#include <string.h>
#include <errno.h>
#include <FL/Fl_File_Chooser.H>
#include <FL/fl_ask.H>
#include "imview.hxx"
#include "imageIO.hxx"
#include "imageViewer.hxx"
#include "spectraBox.hxx"
#include "profileBox.hxx"
#include "saveSpect.hxx"
#include "io/savetiff.hxx"
#include "io/saveZimage.hxx"

// 
typedef void (menucb)(Fl_Menu_ *, void *);

// application components
extern char appName[];
extern imageIO *IOBlackBox;
extern spectra     *spectraPanel;
extern profile     *profilePanel;
extern savespect   *saveSpectPanel;

static const char *formatExtensionList[4] = {
    ".tif",
    "_z",
    ".euro",
    0
};


savespect::savespect()
{
    dbgprintf("Save spectrum panel constructed\n");
    saveWindow = 0; // fluid depends on that
    return;
}


void savespect::setDefaults()
{
    static char buff[100];

    dbgprintf("Setting default for save panel\n");
    
    // default value for file
    sprintf(buff, "%s.euro", appName);
    
    filenameInput->value(buff);
    formatChoice->value(EURO_FMT);
    whichPanel = SPECTRAPANEL;
    
    return;
}


// this functions changes the .tiff in _z, etc...
void savespect::toggleExtension(int format)
{
    static char inputContent[1024]; 
    char *substr;

    strcpy(inputContent, filenameInput->value());
    
    dbgprintf("Looking for extensions\n");
    if (inputContent != 0) {
	for (int i = 0 ; formatExtensionList[i] != 0 ; i++) {
	    // look for the common extension of the format
	    if (((substr = strstr(inputContent, formatExtensionList[i])) != 0)
		&& (format < LAST_FMT)) {
		strcpy(substr, formatExtensionList[format]);
		filenameInput->value(inputContent);
		dbgprintf("Extension found\n");
		break;
	    }
	}
    }
    
    return;
}



void savespect::saveFileName(const char *fn)
{
    // this copies it to something more permanent.
    filenameInput->value(fn);
    return;
}


int savespect::doTheSaving()
{
    int retval = 0;

    dbgprintf("Doing the saving now\n");
    // we can't go any further until we have a writexxx procedure...
    const char *filename = filenameInput->value();

    // trivial user-generated possible errors
    if ((filename == 0) || (filename[0] == '\0')) {
	errprintf("Please specify a file to save to\n");
	return(1);
    } else {
	if (access(filename, W_OK) == 0) {
	    // the file already exists
	    static char alrtmsg[1024] ;
	    sprintf(alrtmsg,
		    "File %s already exists\n"
		    "Do you want to overwrite?\n",
		    filename);
	    if (fl_choice(alrtmsg, "Cancel", "Overwrite", NULL) == 0) {
		// no, we don't want to overwrite...
		return(2);
	    }
	}
    }

    switch (formatChoice->value()) {
      case EURO_FMT: {
	  // this is so simple that I thought there would be
	  // no need to put this in a separate function
	  int nx = 0;

	  if (whichPanel == SPECTRAPANEL)
	      nx = spectraPanel->getNbVal();
	  else if (whichPanel == PROFILEPANEL)
	      nx = profilePanel->getNbVal();
	  
	  if (nx > 0) {
	      FILE *spct = fopen(filename, "wb");
	      if (spct != NULL) {
		  if (whichPanel == SPECTRAPANEL) {
		      double *dbuff= spectraPanel->getData();
		      fprintf(spct, "%d 1\n", nx);
		      for (int i = 0 ; i < nx ; i++) {
			  fprintf(spct, "%10.5g%c",dbuff[i],(i % 5 == 4)? '\n':' ');
		      }
		  } else if (whichPanel == PROFILEPANEL) {
		      double *rbuff = profilePanel->getDataR();
		      double *gbuff = profilePanel->getDataG();
		      double *bbuff = profilePanel->getDataB();
			  int     i;

		      if ((gbuff == 0) && (rbuff != 0)) {
			  fprintf(spct, "%d 1\n", nx);
			  for (i = 0 ; i < nx ; i++) {
			      fprintf(spct, "%10.5g%c",rbuff[i],(i % 5 == 4)? '\n':' ');
			  }
		      } else if ((rbuff != 0) && (gbuff != 0) && (bbuff != 0)) {
			  fprintf(spct, "%d 3\n", nx);
			  for (i = 0 ; i < nx ; i++) {
			      fprintf(spct, "%10.5g%c",rbuff[i],(i % 5 == 4)? '\n':' ');
			  }
			  fprintf(spct, "\n");
			  for (i = 0 ; i < nx ; i++) {
			      fprintf(spct, "%10.5g%c",gbuff[i],(i % 5 == 4)? '\n':' ');
			  }
			  fprintf(spct, "\n");
			  for (i = 0 ; i < nx ; i++) {
			      fprintf(spct, "%10.5g%c",bbuff[i],(i % 5 == 4)? '\n':' ');
			  }
		      }
		  }
		      
		  fprintf(spct, "\n");
		  fclose(spct);
	      } else
		  errprintf("Error in Euro saving code\n"
			    "while writing %s: %s\n",
			    filename, strerror(errno));
	  }
	  break;
      }

      case ZIMAGE_FMT: {
	int nx[3], ny[3], nz[3], x0[3], y0[3], z0[3], ptype[3];
	void *outbuffp[3];
	
	if (whichPanel == SPECTRAPANEL) {
	    if ((nx[0] = spectraPanel->getNbVal()) > 0) {
		nx[1] = nx[2] = 1;
		ny[0] = ny[1] = ny[2] = 1; // this is a vector
		nz[0] = nz[1] = nz[2] = 1; // not a 3D image...
		x0[0] = x0[1] = x0[2] = 0;
		y0[0] = y0[1] = y0[2] = 0;
		z0[0] = z0[1] = z0[2] = 0;
		ptype[0] = IM_DOUBLE;
		outbuffp[0] = spectraPanel->getData();
		outbuffp[1] = outbuffp[2] = 0;
		retval = save_zimage(filename, 1,
				     nx, ny, nz,
				     x0, y0, z0,
				     ptype,
				     outbuffp);
		if (retval != 0)
		    errprintf("Error in Z-IMAGE saving code\n"
			      "while writing %s: %s\n",
			      filename, strerror(errno));
	    }
	} else if (whichPanel == PROFILEPANEL) {
	    if ((nx[0] = profilePanel->getNbVal()) > 0) {
		ny[0] = ny[1] = ny[2] = 1; // this is a vector
		nz[0] = nz[1] = nz[2] = 1; // not a 3D image...
		x0[0] = x0[1] = x0[2] = 0;
		y0[0] = y0[1] = y0[2] = 0;
		z0[0] = z0[1] = z0[2] = 0;
		ptype[0] = IM_DOUBLE;
		outbuffp[0] = profilePanel->getDataR();
		outbuffp[1] = profilePanel->getDataG();
		outbuffp[2] = profilePanel->getDataB();
		if ((outbuffp[0] != 0) && (outbuffp[1] == 0) && (outbuffp[2] == 0)) {
		    nx[1] = nx[2] = 1;
		    retval = save_zimage(filename, 1,
					 nx, ny, nz,
					 x0, y0, z0,
					 ptype,
					 outbuffp);
		} else if ((outbuffp[0] != 0) && (outbuffp[1] != 0) && (outbuffp[2] != 0)) {
		    nx[1] = nx[2] = nx[0];
		    ptype[1] = ptype[2] = IM_DOUBLE;
		    retval = save_zimage(filename, 3,
					 nx, ny, nz,
					 x0, y0, z0,
					 ptype,
					 outbuffp);
		}
		    
		if (retval != 0)
		    errprintf("Error in Z-IMAGE saving code\n"
			      "while writing %s: %s\n",
			      filename, strerror(errno));
	    }
	}
	break;
      }

      case TIFF_FMT: {
	  int    pi, sf, spp, bps, nbdata;
	  int    pixeltype, imagetype;
	  int    start[2], end[2];
	  void  *outbuffp[3];

	  if (whichPanel == SPECTRAPANEL) {
	      if ((nbdata = spectraPanel->getNbVal()) > 0) {
		  // get the pixtype & imgtype
		  pixeltype = IM_DOUBLE;
		  outbuffp[0] = spectraPanel->getData();
		  outbuffp[1] = outbuffp[2] = 0;
		  imagetype = IM_SPECTRUM;
		  spp = 1;
		  setTifftype(pixeltype, imagetype, &pi, &sf, &spp, &bps);
		  if (spp != 1) {
		      errprintf("This can't happen: spp(%d) != 1\n",
				spp);
		      break;
		  }
		  start[0] = 0;
		  end[0] = nbdata -1;
		  start[1] = 0;
		  end[1] = 0;
		  retval = save_tiff(outbuffp, 0,
				     start, end,
				     pi, sf, spp, bps,
				     0, 0,
				     filename,
				     0); // always uncompressed
	      } 



	  } else {
	      int nbcomps;
	      
	      if ((nbdata = profilePanel->getNbVal()) > 0) {
		  // get the pixtype & imgtype
		  pixeltype = IM_DOUBLE;
		  outbuffp[0] = profilePanel->getDataR();
		  outbuffp[1] = profilePanel->getDataG();
		  outbuffp[2] = profilePanel->getDataB();
		  
		  if ((outbuffp[0] != 0) && (outbuffp[1] == 0) && (outbuffp[2] == 0)) {
		      imagetype = IM_SPECTRUM;
		      nbcomps = 1;
		      setTifftype(pixeltype, imagetype, &pi, &sf, &spp, &bps);
		  } else {
		      imagetype = IM_RGB;
		      nbcomps = 3;
		      setTifftype(pixeltype, imagetype, &pi, &sf, &spp, &bps);
		  }
		  spp = nbcomps;
		  if (spp != nbcomps) {
		      errprintf("This can't happen: spp(%d) != nbcomps(%d)\n",
				spp, nbcomps);
		      break;
		  }
		  start[0] = 0;
		  end[0] = nbdata -1;
		  start[1] = 0;
		  end[1] = 0;
		  retval = save_tiff(outbuffp, 0,
				     start, end,
				     pi, sf, spp, bps,
				     0, 0,
				     filename,
				     0); // always uncompressed
	      }
	  }

	  break;
      }
      
      default:
	errprintf("This format is not supported yet, sorry!\n");
	retval = 5;
	break;
    }

    return retval;
}

void savespect::show()
{
    saveWindow->show();
}

void savespect::hide()
{
    saveWindow->hide();
}


// save panel associated callbacks

void filenameinput_cb(Fl_Input *, savespect *)
{
    dbgprintf("File name input callback\n");
    return;
}


void browsebutton_cb(Fl_Button *, savespect *panel)
{
    const char *currentfile;
    static const char *p = 0;
    
    dbgprintf("Browse button pressed on save prefs panel\n");
    
    currentfile = panel->filenameInputValue();
    switch (panel->selectedFileFormat()) {
      case EURO_FMT:
	p = fl_file_chooser("Save to file", "*.[Ee][Uu][Rr][Oo]", currentfile);
	break;
      case ZIMAGE_FMT:
	p = fl_file_chooser("Save to file", "*_z", currentfile);
	break;
      case TIFF_FMT:
	p = fl_file_chooser("Save to file", "*.[tT][iI][fF]*", currentfile);
	break;
      default:
	p = fl_file_chooser("Save to file", "*", currentfile);
	break;
    }
			
    if (p) panel->saveFileName(p);
    
    return;
}

void formatchoice_cb(Fl_Choice *, savespect *)
{
    dbgprintf("Format choice menu callback\n");
    return;
}


void cancelbutton_cb(Fl_Button *, savespect *panel)
{
    dbgprintf("Cancel Button pressed on save prefs panel\n");
    // hide the window
    panel->hide();
    return;
}

void okbutton_cb(Fl_Return_Button *, savespect *panel)
{
    dbgprintf("Print Button pressed on save prefs panel\n");
    if (panel->doTheSaving() == 0) {
	// hide the window
	panel->hide();
    }
    return;
}

// callback for the choice menu

void chosenSpectFormat_cb(Fl_Menu_ *, void *fmt)
{
    long val = (long)fmt;
    
    switch (val) {
      case EURO_FMT:
	dbgprintf("Euro Format selected\n");
	break;
      case ZIMAGE_FMT:
	dbgprintf("Z-IMAGE format selected\n");
	break;
      case TIFF_FMT:
	dbgprintf("TIFF format selected\n");
	break;
    }
    saveSpectPanel->toggleExtension(val);
}