12 #define IO_LEN (1<<30)
15 #if defined(THINK_C) || defined(__MWERKS__)
17 #define IO_LEN (1<<14)
26 extern char *sys_errlist[];
27 char *strerror(int err)
29 return sys_errlist[err];
33 #include "pixmap_io.h"
35 #define MAGIC_PGM "P5\n"
36 #define MAGIC_PPM "P6\n"
38 static int match_key(int fd, char *key)
42 read(fd, buf, strlen(key));
43 if( strncmp(buf, key, strlen(key)) != 0 )
49 static void skip_comment(int fd, char code, char *c)
53 while( (read(fd, c, 1) == 1 ) && (*c != '\n') ) ;
58 static void read_header_line(int fd, char *buf)
63 while( (read(fd, &buf[i], 1) == 1 ) && (buf[i] != '\n') && (buf[i] != '\r') && (i<79) )
68 static int get_pgm_header(int fd, char *magic, int *width, int *height)
72 if( !match_key(fd, magic) )
75 skip_comment(fd, '#', buf);
76 read_header_line(fd, buf);
77 sscanf(buf, "%d %d", width, height);
79 skip_comment(fd, '#', buf);
80 read_header_line(fd, buf);
84 static int open_read(char *filename)
88 if( (fd = open(filename, O_BINARY|O_RDONLY)) < 0 )
89 fprintf(stderr, "can't reset file `%s': %s\n", filename, strerror(errno));
93 static int open_read_pixmap(char *filename, char *magic, int *width, int *height)
97 if( (fd = open_read(filename)) < 0)
99 if( !get_pgm_header(fd, magic, width, height) )
101 fprintf(stderr, "can't read header of %s\n", filename);
107 static unsigned char *alloc_pixmap(long size)
111 if( (data = (unsigned char *)malloc(size)) == NULL )
113 fprintf(stderr, "malloc error\n");
119 static void load_data(int fd, unsigned char *data, long size)
124 buffer = (char *)data;
130 read(fd, buffer, count);
136 static void load_chunky(int fd, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height)
138 unsigned char *buffer, *buf, *buf_R, *buf_G, *buf_B;
141 buffer = alloc_pixmap(3L*width);
145 for( ; height > 0; height-- )
147 load_data(fd, buffer, 3L*width);
160 unsigned char *load_pixmap(char *filename, int *width, int *height)
166 if( (fd = open_read_pixmap(filename, MAGIC_PGM, width, height)) < 0)
168 size = (long)*width * *height;
169 data = alloc_pixmap(size);
171 load_data(fd, data, size);
176 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data)
181 if( (fd = open_read_pixmap(filename, MAGIC_PPM, width, height)) < 0)
183 size = (long)*width * *height;
184 *R_data = alloc_pixmap(size);
185 *G_data = alloc_pixmap(size);
186 *B_data = alloc_pixmap(size);
188 if( (*R_data != NULL) && (*G_data != NULL ) && (*B_data != NULL ))
190 load_chunky(fd, *R_data, *G_data, *B_data, *width, *height);
196 if( *R_data == NULL )
199 if( *G_data == NULL )
208 static void put_header_line(int fd, char *buf)
210 write(fd, buf, strlen(buf));
213 static void put_header_info(int fd, char *mark, char *filename)
218 sprintf(buf, "%sTitle: %s\n", mark, filename);
219 put_header_line(fd, buf);
221 sprintf(buf, "%sCreationDate: %s", mark, ctime(&now));
222 put_header_line(fd, buf);
223 sprintf(buf, "%sCreator: pixmap_io, P. Chassignet\n", mark);
224 put_header_line(fd, buf);
227 static void put_pgm_header(int fd, char *magic, int width, int height, char *filename)
231 put_header_line(fd, magic);
232 put_header_info(fd, "# ", filename);
233 sprintf(buf, "%d %d\n255\n", width, height);
234 put_header_line(fd, buf);
237 static int open_write(char *filename)
241 #if defined(THINK_C) || defined(__MWERKS__)
242 if( (fd = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_RDWR)) < 0 )
244 if( (fd = open(filename, O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE)) < 0 )
246 fprintf(stderr, "can't rewrite file `%s': %s\n", filename, strerror(errno));
250 static void store_data(int fd, unsigned char *data, long size)
255 buffer = (char *)data;
261 write(fd, buffer, count);
267 static void store_chunky(int fd, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height)
269 unsigned char *buffer, *buf, *buf_R, *buf_G, *buf_B;
272 buffer = alloc_pixmap(3L*width);
276 for( ; height > 0; height-- )
286 store_data(fd, buffer, 3L*width);
291 void store_pixmap(char *filename, unsigned char *data, int width, int height)
295 if( (fd = open_write(filename)) < 0 )
297 put_pgm_header(fd, MAGIC_PGM, width, height, filename);
298 store_data(fd, data, (long)width*height);
302 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height)
306 if( (fd = open_write(filename)) < 0 )
308 put_pgm_header(fd, MAGIC_PPM, width, height, filename);
309 store_chunky(fd, R_data, G_data, B_data, width, height);