Changeset 5743 for trunk/stac/src/stacRead.c
- Timestamp:
- Dec 7, 2005, 2:57:14 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/stac/src/stacRead.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/stac/src/stacRead.c
r3681 r5743 1 1 #include <stdio.h> 2 2 #include <string.h> 3 #include <assert.h> 3 4 #include "pslib.h" 4 5 #include "stac.h" 5 6 6 #define BUFFER 100 // Size of buffer for incrementally reading coordinates 7 8 psArray *stacReadImages(psArray *filenames // The file names of the images 7 #define BUFFER 100 // Size of buffer for incrementally reading coordinates 8 9 psArray *stacReadImages(psArray **headers, // The image headers, to be returned 10 psArray *filenames // The file names of the images 9 11 ) 10 12 { 11 int nFiles = filenames->n; // The number of input files13 int nFiles = filenames->n; // The number of input files 12 14 psArray *images = psArrayAlloc(nFiles); // The input files, to be returned 13 psRegion *imageRegion = psRegionAlloc(0, 0, 0, 0); // Region of image to read 15 assert(!headers || ! *headers || (*headers)->n == nFiles); 16 if (headers && ! *headers) { 17 *headers = psArrayAlloc(nFiles); 18 } 19 20 psRegion imageRegion = {0, 0, 0, 0}; // Region of image to read 14 21 15 22 psTrace("stac.read.images", 1, "Reading input images....\n"); 16 23 for (int i = 0; i < nFiles; i++) { 17 psTrace("stac.read.images", 2, "Reading input image %s....\n",filenames->data[i]); 18 psFits *imageFile = psFitsAlloc(filenames->data[i]); 19 // We only read PHUs --- not mucking around with extensions for now 20 psImage *image = psFitsReadImage(NULL, imageFile, *imageRegion, 0); 21 if (image == NULL) { 22 psErrorStackPrint(stderr,"Fatal error: Unable to read %s\n",filenames->data[i]); 23 exit(EXIT_FAILURE); 24 } 25 psTrace("stac.read.images", 4, "Image %s is %dx%d\n", filenames->data[i], image->numCols, 26 image->numRows); 27 // Convert to 32-bit floating point, in necessary 28 if (image->type.type != PS_TYPE_F32) { 29 psTrace("stac.read.images", 3, "Converting %s to floating point in memory....", 30 filenames->data[i]); 31 images->data[i] = psImageCopy(NULL, image, PS_TYPE_F32); 32 psFree(image); 33 } else { 34 int numNaN = psImageClipNaN(image, FLT_MAX); 35 if (numNaN) { 36 psTrace("stac.read.images", 5, "Clipped %d NaN pixels.\n", numNaN); 37 } 38 images->data[i] = image; 39 } 40 psFree(imageFile); 24 psTrace("stac.read.images", 2, "Reading input image %s....\n",filenames->data[i]); 25 psFits *imageFile = psFitsOpen(filenames->data[i], "r"); 26 // We only read PHUs --- not mucking around with extensions for now 27 if (headers) { 28 (*headers)->data[i] = psFitsReadHeader(NULL, imageFile); 29 } 30 psImage *image = psFitsReadImage(NULL, imageFile, imageRegion, 0); 31 if (image == NULL) { 32 psErrorStackPrint(stderr,"Fatal error: Unable to read %s\n",filenames->data[i]); 33 exit(EXIT_FAILURE); 34 } 35 psFitsClose(imageFile); 36 psTrace("stac.read.images", 4, "Image %s is %dx%d\n", filenames->data[i], image->numCols, 37 image->numRows); 38 // Convert to 32-bit floating point, in necessary 39 if (image->type.type != PS_TYPE_F32) { 40 psTrace("stac.read.images", 3, "Converting %s to floating point in memory....\n", 41 filenames->data[i]); 42 images->data[i] = psImageCopy(NULL, image, PS_TYPE_F32); 43 psFree(image); 44 } 45 int numNaN = psImageClipNaN(image, 0.0); 46 if (numNaN) { 47 psTrace("stac.read.images", 5, "Clipped %d NaN pixels to zero.\n", numNaN); 48 } 49 images->data[i] = image; 41 50 } 42 51 psTrace("stac.read.images",1,"%d input images read.\n",nFiles); 43 psFree(imageRegion);44 52 45 53 return images; … … 50 58 FILE *file = fopen(filename, "r"); 51 59 if (file == NULL) { 52 psLogMsg("stac.read.coords", PS_LOG_ERROR, "Cannot open coordinate file, %s\n", filename);53 return NULL;60 psLogMsg("stac.read.coords", PS_LOG_ERROR, "Cannot open coordinate file, %s\n", filename); 61 return NULL; 54 62 } 55 63 … … 57 65 58 66 psArray *coords = psArrayAlloc(BUFFER); // The array of coordinates to be returned 59 float x, y; // Coordinates to read60 int num = 0; // Number of coordinates read67 float x, y; // Coordinates to read 68 int num = 0; // Number of coordinates read 61 69 while (fscanf(file, "%f %f\n", &x, &y) == 2) { 62 psPlane *coord = psPlaneAlloc();// A coordinate63 coord->x = x;64 coord->y = y;65 coords->data[num] = coord;66 num++;67 if (num % BUFFER) {68 coords = psArrayRealloc(coords, num + BUFFER);69 }70 psPlane *coord = psPlaneAlloc();// A coordinate 71 coord->x = x; 72 coord->y = y; 73 coords->data[num] = coord; 74 num++; 75 if (num % BUFFER) { 76 coords = psArrayRealloc(coords, num + BUFFER); 77 } 70 78 } 71 79 coords->n = num; … … 82 90 FILE *mapfp = fopen(filename, "r"); 83 91 if (mapfp == NULL) { 84 psLogMsg("stac.read.map", PS_LOG_ERROR, "Cannot open map file, %s\n", filename);85 return NULL;92 psLogMsg("stac.read.map", PS_LOG_ERROR, "Cannot open map file, %s\n", filename); 93 return NULL; 86 94 } 87 95 // Read the file 88 96 psTrace("stac.read.map", 5, "Reading map file %s....\n", filename); 89 97 90 98 // Format is now: 91 99 // order 92 100 // x coefficients 93 101 // y coefficients 94 // 102 // 95 103 // where the order is 1 for linear, 2 for quadratic, 3 for cubic. 96 104 // and the coefficients are read by the following pseudo-code: 97 // 98 // for (int k = 0; k < order + 1; k++)99 // for (int j = 0; j < k; j++)100 // int i = k - j;105 // 106 // for (int k = 0; k < order + 1; k++) 107 // for (int j = 0; j < k; j++) 108 // int i = k - j; 101 109 // read coefficient of x^i y^j 102 // 110 // 103 111 // This produces the ordering: 104 112 // x^0 y^0, x^1 y^0, x^0 y^1, x^2 y^0, x^1 y^1, x^0 y^2, x^3 y^0, x^2 y^1, x^1 y^2, x^0 y^3 105 // 113 // 106 114 // This is, of course, for third order polynomials. 107 115 // For lower orders, the list is truncated at the appropriate level. 108 116 109 int order = 0; // Polynomial order117 int order = 0; // Polynomial order 110 118 if (fscanf(mapfp, "%d", &order) != 1) { 111 psLogMsg("stac.read.map", PS_LOG_ERROR, "Unable to read map file %s\n", filename);112 fclose(mapfp);113 return NULL;114 } 115 119 psLogMsg("stac.read.map", PS_LOG_ERROR, "Unable to read map file %s\n", filename); 120 fclose(mapfp); 121 return NULL; 122 } 123 116 124 psTrace("stac.read.map", 5, "Polynomial order: %d\n", order); 117 125 118 126 psPlaneTransform *map = psPlaneTransformAlloc(order + 1, order + 1); // The transformation 119 127 // Set coefficient masks 120 128 for (int i = 0; i < order + 1; i++) { 121 for (int j = 0; j < order + 1; j++) {122 if (i + j > order + 1) {123 map->x->mask[i][j] = 1;124 map->y->mask[i][j] = 1;125 } else {126 map->x->mask[i][j] = 0;127 map->y->mask[i][j] = 0;128 }129 }130 } 131 129 for (int j = 0; j < order + 1; j++) { 130 if (i + j > order + 1) { 131 map->x->mask[i][j] = 1; 132 map->y->mask[i][j] = 1; 133 } else { 134 map->x->mask[i][j] = 0; 135 map->y->mask[i][j] = 0; 136 } 137 } 138 } 139 132 140 // Read x coefficients 133 141 psTrace("stac.read.map", 7, "x' = \n"); 134 142 for (int k = 0; k < order + 1; k++) { 135 for (int j = 0; j < k + 1; j++) {136 int i = k - j;137 if (fscanf(mapfp, "%lf", &map->x->coeff[i][j]) != 1) {138 psLogMsg("stac.read.map", PS_LOG_ERROR, "Unable to read map file %s\n", filename);139 fclose(mapfp);140 psFree(map);141 return NULL;142 }143 psTrace("stac.read.map", 7, " %f x^%d y^%d\n", map->x->coeff[i][j], i, j);144 }143 for (int j = 0; j < k + 1; j++) { 144 int i = k - j; 145 if (fscanf(mapfp, "%lf", &map->x->coeff[i][j]) != 1) { 146 psLogMsg("stac.read.map", PS_LOG_ERROR, "Unable to read map file %s\n", filename); 147 fclose(mapfp); 148 psFree(map); 149 return NULL; 150 } 151 psTrace("stac.read.map", 7, " %f x^%d y^%d\n", map->x->coeff[i][j], i, j); 152 } 145 153 } 146 154 // Read y coefficients 147 155 psTrace("stac.read.maps", 7, "y' = \n"); 148 156 for (int k = 0; k < order + 1; k++) { 149 for (int j = 0; j < k + 1; j++) {150 int i = k - j;151 if (fscanf(mapfp, "%lf", &map->y->coeff[i][j]) != 1) {152 psLogMsg("stac.read.map", PS_LOG_ERROR, "Unable to read map file %s\n", filename);153 fclose(mapfp);154 psFree(map);155 return NULL;156 }157 psTrace("stac.read.map", 7, " %f x^%d y^%d\n", map->y->coeff[i][j], i, j);158 }159 } 160 157 for (int j = 0; j < k + 1; j++) { 158 int i = k - j; 159 if (fscanf(mapfp, "%lf", &map->y->coeff[i][j]) != 1) { 160 psLogMsg("stac.read.map", PS_LOG_ERROR, "Unable to read map file %s\n", filename); 161 fclose(mapfp); 162 psFree(map); 163 return NULL; 164 } 165 psTrace("stac.read.map", 7, " %f x^%d y^%d\n", map->y->coeff[i][j], i, j); 166 } 167 } 168 161 169 fclose(mapfp); 162 170 163 171 return map; 164 172 } 165 173 166 174 167 175 … … 169 177 ) 170 178 { 171 int nFiles = filenames->n; // The number of input files179 int nFiles = filenames->n; // The number of input files 172 180 psArray *maps = psArrayAlloc(nFiles); // The maps, to be returned 173 char mapfile[MAXCHAR]; // Filename of map174 181 char mapfile[MAXCHAR]; // Filename of map 182 175 183 psTrace("stac.read.maps",1,"Reading maps....\n"); 176 184 for (int i = 0; i < nFiles; i++) { 177 if (strlen(filenames->data[i]) > MAXCHAR - 4) {178 psLogMsg("stac.read.maps",PS_LOG_ERROR,"Filename %s is too long.\n",filenames->data[i]);179 exit(EXIT_FAILURE);180 }181 // Read the file182 sprintf(mapfile,"%s.map",filenames->data[i]);183 maps->data[i] = stacReadMap(mapfile);184 if (maps->data[i] == NULL) {185 psLogMsg("stac.read.maps", PS_LOG_ERROR, "Unable to read map: %s\n", mapfile);186 }185 if (strlen(filenames->data[i]) > MAXCHAR - 4) { 186 psLogMsg("stac.read.maps",PS_LOG_ERROR,"Filename %s is too long.\n",filenames->data[i]); 187 exit(EXIT_FAILURE); 188 } 189 // Read the file 190 sprintf(mapfile,"%s.map",filenames->data[i]); 191 maps->data[i] = stacReadMap(mapfile); 192 if (maps->data[i] == NULL) { 193 psLogMsg("stac.read.maps", PS_LOG_ERROR, "Unable to read map: %s\n", mapfile); 194 } 187 195 188 196 }
Note:
See TracChangeset
for help on using the changeset viewer.
