1    | /***************************************
2    |   $Header$
3    | 
4    |   This file contains functions for importing and exporting data to/from foreign
5    |   file formats.  Thus far only .tiff images are supported.
6    | ***************************************/
7    | 
8    | 
9    | #include "illuminator.h"     /* Also includes petscda.h */
10   | 
11   | /* Build with -DDEBUG for debugging output */
12   | #undef DPRINTF
13   | #ifdef DEBUG
14   | #define DPRINTF(fmt, args...) ierr = PetscPrintf (comm, "%s: " fmt, __FUNCT__, args); CHKERRQ (ierr)
15   | #else
16   | #define DPRINTF(fmt, args...)
17   | #endif
18   | 
19   | 
20   | #undef __FUNCT__
21   | #define __FUNCT__ "IllImageWrite"
22   | 
23   | 
24   | /*++++++++++++++++++++++++++++++++++++++
25   |   This writes a single monochrome .tif image from a double array for IllImageSave().
26   | 
27   |   inline int IllImageWrite It returns zero or an error code.
28   | 
29   |   double *array Array of values to use as intensity values in the image,
30   |   pointing to first red value.
31   | 
32   |   int xm Width of the array rows.
33   | 
34   |   int ym Number of rows in the array.
35   | 
36   |   int stride Number of doubles per point.
37   | 
38   |   int gxm Number of points between row starts.
39   | 
40   |   int goff Offest for green field.
41   | 
42   |   int boff Offest for blue field.
43   | 
44   |   char *basename Base file name; if the last four chars aren't ".tif" this
45   |   appends that.
46   | 
47   |   PetscScalar minval Array value mapping to black.
48   | 
49   |   PetscScalar maxval Array value mapping to white.
50   |   ++++++++++++++++++++++++++++++++++++++*/
51   | 
52   | static inline int IllImageWrite
53   | (double *array, int xm,int ym,int stride,int gxm, int goff,int boff,
54   |  char *basename, PetscScalar minval,PetscScalar maxval)
55   | {
56   |   /* Make the file name */
57   |   /* Open the file */
58   |   /* Make the header */
59   |   /* Loop through and write the data */
60   |   /* Close everything */
61   | }
62   | 
63   | 
64   | #undef __FUNCT__
65   | #define __FUNCT__ "IllImageSave"
66   | 
67   | /*++++++++++++++++++++++++++++++++++++++
68   |   This function stores one field as an image.  In 2-D, it stores the whole
69   |   thing; in 3-D, it stores one image per layer.
70   | 
71   |   int IllImageSave It returns zero or an error code.
72   | 
73   |   MPI_COMM comm MPI communicator, if NULL it uses PETSC_COMM_WORLD.
74   | 
75   |   DA theda Distributed array object controlling data saved.
76   | 
77   |   Vec X Vector whose data are actually being saved.
78   | 
79   |   char *basename Base file name.
80   | 
81   |   int field Field index.
82   | 
83   |   PetscScalar *rgbmin Field values to use for image R,G,B zero.
84   | 
85   |   PetscScalar *rgbmax Field values to use for image R,G,B max.
86   | 
87   |   int *coordrange Minimum and maximum x, y, z to store (PETSC_NULL for the
88   |   whole thing).
89   | 
90   |   IllLayerStyle layer Layer style to use for 3-D DA.
91   | 
92   |   IllImageFormat format Image format.
93   |   ++++++++++++++++++++++++++++++++++++++*/
94   | 
95   | int IllImageSave
96   | (MPI_Comm comm, DA theda, Vec X, char *basename,
97   |  int redfield,int greenfield,int bluefield,
98   |  PetscScalar *rgbmin,PetscScalar *rgbmax, int *coordrange,
99   |  IllLayerStyle layer, IllImageFormat format)
100  | {
101  | 
102  |   /* Get size, if > 1 then SETERRQ only one processor for now */
103  |   /* Get array */
104  |   /* Get dimensions, if 2 then fall through to IllImageWrite */
105  |   /* If layer isn't DEFAULT or Z_UP then SETERRQ only Z_UP for now */
106  |   /* For 3-D: loop through planes, calling IllImageWrite for each */
107  | 
108  |   /* if (snprintf (filename, 999, "%s.cpu%.4d.data", basename, rank) > 999)
109  |     SETERRQ1 (PETSC_ERR_ARG_SIZ,
110  | 	      "Base file name too long (%d chars, 960 max)",
111  | 	      strlen (basename));
112  |   if (!(outfile = fopen (filename, "w")))
113  |     SETERRQ4 (PETSC_ERR_FILE_OPEN,
114  | 	      "CPU %d: error %d (%s) opening output file \"%s\"",
115  | 	      rank, errno, strerror (errno), filename);
116  | 
117  |   switch (compressed & COMPRESS_INT_MASK)
118  |     {
119  |     case COMPRESS_INT_LONG:
120  |       {
121  | 	guint32 *storage;
122  | 	if (!(storage = (guint32 *) malloc
123  | 	      (gridpoints * dof * sizeof (guint32))))
124  | 	  SETERRQ (PETSC_ERR_MEM, "Can't allocate data compression buffer");
125  | 	for (i=0; i<gridpoints*dof; i++)
126  | 	  storage [i] = (guint32)
127  | 	    (4294967295. * (globalarray [i] - fieldmin [i%dof]) /
128  | 	     (fieldmax [i%dof] - fieldmin [i%dof]));
129  | 	writeout = fwrite (storage, sizeof (guint32), gridpoints*dof, outfile);
130  | 	free (storage);
131  | 	break;
132  |       }
133  |     case COMPRESS_INT_SHORT:
134  |       {
135  | 	guint16 *storage;
136  | 	if (!(storage = (guint16 *) malloc
137  | 	      (gridpoints * dof * sizeof (guint16))))
138  | 	  SETERRQ (PETSC_ERR_MEM, "Can't allocate data compression buffer");
139  | 	for (i=0; i<gridpoints*dof; i++)
140  | 	  storage [i] = (guint16)
141  | 	    (65535. * (globalarray [i] - fieldmin [i%dof]) /
142  | 	     (fieldmax [i%dof] - fieldmin [i%dof]));
143  | 	writeout = fwrite (storage, sizeof (guint16),  gridpoints*dof,outfile);
144  | 	free (storage);
145  | 	break;
146  |       }
147  |     case COMPRESS_INT_CHAR:
148  |       {
149  | 	guint8 *storage;
150  | 	if (!(storage = (guint8 *) malloc
151  | 	      (gridpoints * dof * sizeof (guint8))))
152  | 	  SETERRQ (PETSC_ERR_MEM, "Can't allocate data compression buffer");
153  | 	for (i=0; i<gridpoints*dof; i++)
154  | 	  storage [i] = (guint8)
155  | 	    (255. * (globalarray [i] - fieldmin [i%dof]) /
156  | 	     (fieldmax [i%dof] - fieldmin [i%dof]));
157  | 	writeout = fwrite (storage, sizeof (guint8),  gridpoints*dof, outfile);
158  | 	free (storage);
159  | 	break;
160  |       }
161  |     default:
162  |       writeout = fwrite (globalarray, sizeof (PetscScalar), gridpoints*dof,
163  | 			 outfile);
164  |     }
165  | 
166  |   if (compressed & COMPRESS_GZIP_MASK)
167  |     pclose (outfile);
168  |   else
169  |     fclose (outfile);
170  | 
171  |   if (writeout < gridpoints*dof)
172  |     {
173  |       int writerr = ferror (outfile);
174  |       SETERRQ5 (PETSC_ERR_FILE_READ,
175  | 		"CPU %d: error %d (%s) during write, sent %d of %d items",
176  | 		rank, writerr, strerror (writerr), writeout, gridpoints*dof);
177  | 		} */
178  | 
179  |   return 0;
180  | }