00001 #include "sadie.h"
00002
00003 #define BUFFER_SIZE 1000
00004
00005 #define UCHAR(c) ((unsigned char) (c))
00006
00007
00008
00009
00010
00011
00012 #define MAX_MEMORY 10000
00013
00014
00015
00016
00017
00018 #define PGM 1
00019 #define PPM 2
00020
00021
00022
00023
00024
00025
00026 static int ReadPNMFileHeader(FILE *fp,
00027 int *widthPtr, int *heightPtr,
00028 int *maxIntensityPtr);
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 void PNM2IMG (name,out)
00039 char *name;
00040 IMAGE **out;
00041
00042 {
00043 int i,j,k;
00044 char msg[SLEN];
00045 int fileWidth, fileHeight, maxIntensity;
00046 int nLines, nBytes, h, type, count;
00047 FILE *fp=NULL;
00048 unsigned char *pixelPtr=NULL, *data=NULL;
00049
00050
00051 if (!(fp=fopen(name,"r"))) {
00052 sprintf(msg," Can't open file %s.",name);
00053 MESSAGE('E',msg);
00054 goto the_end;
00055 }
00056
00057
00058 type = ReadPNMFileHeader(fp, &fileWidth, &fileHeight, &maxIntensity);
00059 if (type == 0) {
00060 sprintf(msg, "Couldn't read raw PNM header from file %s.",name);
00061 }
00062 if ((fileWidth <= 0) || (fileHeight <= 0)) {
00063 sprintf(msg, "PNM image file %s has dimension(s) <= 0", name);
00064 MESSAGE('E',msg);
00065 goto the_end;
00066 }
00067 if ((maxIntensity <= 0) || (maxIntensity >= 256)) {
00068 sprintf(msg, "PNM image file %s has bad maximum intensity value %d", name, maxIntensity);
00069 MESSAGE('E',msg);
00070 goto the_end;
00071 }
00072
00073 if (type == PGM) {
00074
00075 if (!CHECKIMG(*out)) GETMEM(1,fileHeight,fileWidth,out);
00076 if (!CHECKIMG(*out)) {
00077 sprintf(msg,"Can't allocate memory for image.");
00078 MESSAGE('E',msg);
00079 goto the_end;
00080 }
00081 }
00082 else {
00083
00084 if (!CHECKIMG(*out)) GETMEM(3,fileHeight,fileWidth,out);
00085 if (!CHECKIMG(*out)) {
00086 sprintf(msg,"Can't allocate memory for image.");
00087 MESSAGE('E',msg);
00088 goto the_end;
00089 }
00090 }
00091
00092 if (NAMES) {
00093 MESSAGE('I',"");
00094 MESSAGE('I',"PNM2IMG");
00095 MESSAGE('I',"");
00096 sprintf(msg," Input file: %s",name);
00097 MESSAGE('I',msg);
00098 MESSAGE('I'," Input format: PNM");
00099 sprintf(msg," Number of bands, lines, pixels: %d, %d, %d",(*out)->nbnd,(*out)->nlin,(*out)->npix);
00100 MESSAGE('I',msg);
00101 sprintf(msg," Number of bits/pixel/band: %d",8);
00102 MESSAGE('I',msg);
00103 MESSAGE('I'," ...............");
00104 }
00105
00106
00107 nBytes = (*out)->npix * (*out)->nbnd;
00108 pixelPtr = (unsigned char *) malloc((unsigned) nBytes);
00109 if (!pixelPtr) {
00110 MESSAGE('E',"Could not allocate memory to read image.");
00111 goto the_end;
00112 }
00113
00114 for (j=0; j<(*out)->nlin; j++) {
00115 data = pixelPtr;
00116 count = fread(data, 1, nBytes, fp);
00117
00118 if (count != nBytes) {
00119 sprintf(msg, "Error reading PNM image file %s: not enough data",name);
00120 MESSAGE('E',msg);
00121 goto the_end;
00122 }
00123 for (k=0; k<(*out)->npix; k++) {
00124 for (i=0; i<(*out)->nbnd; i++) {
00125 (*out)->data[i][j][k] = (PIXEL)*data++;
00126 }
00127 }
00128 }
00129
00130 the_end:
00131
00132 if (pixelPtr) free(pixelPtr);
00133
00134 if (fp) {
00135 if (fclose(fp)) {
00136 sprintf(msg," Can't close file %s.",name);
00137 MESSAGE('W',msg);
00138 }
00139 }
00140 }
00141
00142
00143 static int
00144 ReadPNMFileHeader(fp, widthPtr, heightPtr, maxIntensityPtr)
00145 FILE *fp;
00146 int *widthPtr, *heightPtr;
00147
00148 int *maxIntensityPtr;
00149
00150 {
00151 char buffer[BUFFER_SIZE];
00152 int i, numFields, firstInLine;
00153 int type = 0;
00154 char c;
00155
00156
00157
00158
00159
00160
00161 if (fread(&c, 1, 1, fp) != 1) {
00162 return 0;
00163 }
00164 firstInLine = 1;
00165 i = 0;
00166 for (numFields = 0; numFields < 4; numFields++) {
00167
00168
00169
00170
00171 while (1) {
00172 while (isspace(UCHAR(c))) {
00173 firstInLine = (c == '\n');
00174 if (fread(&c, 1, 1, fp) != 1) {
00175 return 0;
00176 }
00177 }
00178 if (c != '#') {
00179 break;
00180 }
00181 do {
00182 if (fread(&c, 1, 1, fp) != 1) {
00183 return 0;
00184 }
00185 } while (c != '\n');
00186 firstInLine = 1;
00187 }
00188
00189
00190
00191
00192
00193 while (!isspace(UCHAR(c))) {
00194 if (i < (BUFFER_SIZE-2)) {
00195 buffer[i] = c;
00196 i++;
00197 }
00198 if (fread(&c, 1, 1, fp) != 1) {
00199 goto done;
00200 }
00201 }
00202 if (i < (BUFFER_SIZE-1)) {
00203 buffer[i] = ' ';
00204 i++;
00205 }
00206 firstInLine = 0;
00207 }
00208 done:
00209 buffer[i] = 0;
00210
00211
00212
00213
00214
00215 if (strncmp(buffer, "P6 ", 3) == 0) {
00216 type = PPM;
00217 } else if (strncmp(buffer, "P5 ", 3) == 0) {
00218 type = PGM;
00219 } else {
00220 return 0;
00221 }
00222 if (sscanf(buffer+3, "%d %d %d", widthPtr, heightPtr, maxIntensityPtr)
00223 != 3) {
00224 return 0;
00225 }
00226 return type;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241