]>
iEval git - xfishtank.git/blob - read.c
8 #define DEF_BLACK BlackPixel(Dpy, DefaultScreen(Dpy))
9 #define DEF_WHITE WhitePixel(Dpy, DefaultScreen(Dpy))
13 extern unsigned char *ReadGifBitmap();
21 1, 2, 4, 8, 16, 32, 64, 128
27 * dscan is a quick replacement for:
29 * fscanf (fp, "%d", &ip);
46 fprintf(stderr
, "dscanf: EOF");
49 else if (nerrors
++ > 4)
65 v
= i
- '0' + (v
* 10);
78 fprintf(stderr
, "dscanf: non numeric character (0x%x) '%c'\n", i
, i
);
90 ReadXpmPixmap(fp
, w
, h
, colrs
, Colors
, CharsPP
)
93 struct colr_data
*colrs
;
96 unsigned char *pixels
;
101 char line
[BUFSIZ
], name_and_type
[MAX_LINE
];
102 unsigned char *dataP
;
110 fprintf(stderr
, "Can't find Colors.\n");
111 return ((unsigned char *) NULL
);
114 fprintf(stderr
, "Invalid width.\n");
115 return ((unsigned char *) NULL
);
118 fprintf(stderr
, "Invalid height.\n");
119 return ((unsigned char *) NULL
);
122 Color_Vals
= (char **) malloc(sizeof(char *) * Colors
);
123 for (i
= 0; i
< Colors
; i
++) {
125 while ((tchar
!= '"') && (tchar
!= EOF
)) {
128 Color_Vals
[i
] = (char *) malloc(sizeof(char) * (CharsPP
+ 1));
131 while ((tchar
!= '"') && (tchar
!= EOF
) && (j
< CharsPP
)) {
132 Color_Vals
[i
][j
] = (char) tchar
;
136 Color_Vals
[i
][j
] = '\0';
139 while ((tchar
!= '"') && (tchar
!= EOF
)) {
144 while ((tchar
!= '"') && (tchar
!= EOF
)) {
149 while ((tchar
!= '"') && (tchar
!= EOF
)) {
150 line
[j
] = (char) tchar
;
155 XParseColor(Dpy
, DefaultColormap(Dpy
, DefaultScreen(Dpy
)), line
, &tmpcolr
);
156 colrs
[i
].red
= tmpcolr
.red
;
157 colrs
[i
].green
= tmpcolr
.green
;
158 colrs
[i
].blue
= tmpcolr
.blue
;
160 for (i
= Colors
; i
< 256; i
++) {
166 while ((tchar
!= ';') && (tchar
!= EOF
)) {
171 if (!(fgets(line
, MAX_LINE
, fp
))) {
172 fprintf(stderr
, "Can't find Pixels\n");
173 return ((unsigned char *) NULL
);
174 } else if (sscanf(line
, "static char * %s = {", name_and_type
) == 1) {
175 if ((t
= (char *) rindex(name_and_type
, '_')) == NULL
) {
180 if ((t2
= (char *) index(name_and_type
, '[')) != NULL
) {
183 if (!strcmp("pixels", t
)) {
188 pixels
= (unsigned char *) malloc((*w
) * (*h
));
189 if (pixels
== NULL
) {
190 fprintf(stderr
, "Not enough memory for data.\n");
191 return ((unsigned char *) NULL
);
198 while ((tchar
!= '"') && (tchar
!= EOF
)) {
202 for (j
= 0; j
< (*h
); j
++) {
203 for (i
= 0; i
< (*w
); i
++) {
205 while ((tchar
!= '"') && (tchar
!= EOF
) && (k
< CharsPP
)) {
206 line
[k
] = (char) tchar
;
210 if ((k
== 0) && (tchar
== '"')) {
212 while ((tchar
!= '"') && (tchar
!= EOF
)) {
217 while ((tchar
!= '"') && (tchar
!= EOF
) && (k
< CharsPP
)) {
218 line
[k
] = (char) tchar
;
225 for (k
= 0; k
< Colors
; k
++) {
226 if (strncmp(Color_Vals
[k
], line
, CharsPP
) == 0) {
227 *dataP
++ = (unsigned char) k
;
233 fprintf(stderr
, "Invalid Pixel (%2s) in file\n", line
);
234 *dataP
++ = (unsigned char) 0;
239 for (i
= 0; i
< ((*w
) * (*h
)); i
++) {
240 if ((int) *bitp
> (256 - 1))
241 *bitp
= (unsigned char) 0;
244 for (i
= 0; i
< Colors
; i
++) {
245 free((char *) Color_Vals
[i
]);
247 free((char *) Color_Vals
);
253 ReadXbmBitmap(fp
, w
, h
, colrs
)
256 struct colr_data
*colrs
;
258 int blackbit
= BlackPixel(Dpy
, DefaultScreen(Dpy
));
259 int whitebit
= WhitePixel(Dpy
, DefaultScreen(Dpy
));
260 char line
[MAX_LINE
], name_and_type
[MAX_LINE
];
263 unsigned char *ptr
, *dataP
;
264 int bytes_per_line
, version10p
, raster_length
, padding
;
265 int i
, bytes
, temp
, value
;
266 int Ncolors
, charspp
, xpmformat
;
274 if (!(fgets(line
, MAX_LINE
, fp
)))
276 if (strlen(line
) == (MAX_LINE
- 1)) {
277 fprintf(stderr
, "Line too long.\n");
278 return ((unsigned char *) NULL
);
280 if (sscanf(line
, "#define %s %d", name_and_type
, &value
) == 2) {
281 if (!(t
= (char *) rindex(name_and_type
, '_')))
285 if (!strcmp("width", t
))
287 if (!strcmp("height", t
))
289 if (!strcmp("ncolors", t
))
291 if (!strcmp("pixel", t
))
295 if (sscanf(line
, "static short %s = {", name_and_type
) == 1) {
298 } else if (sscanf(line
, "static char * %s = {", name_and_type
) == 1) {
300 if (!(t
= (char *) rindex(name_and_type
, '_')))
304 if ((t2
= (char *) index(name_and_type
, '[')) != NULL
)
306 if (!strcmp("mono", t
))
310 } else if (sscanf(line
, "static char %s = {", name_and_type
) == 1) {
317 dataP
= ReadXpmPixmap(fp
, w
, h
, colrs
, Ncolors
, charspp
);
321 fprintf(stderr
, "Invalid width.\n");
322 return ((unsigned char *) NULL
);
325 fprintf(stderr
, "Invalid height.\n");
326 return ((unsigned char *) NULL
);
329 if (((*w
% 16) >= 1) && ((*w
% 16) <= 8) && version10p
) {
332 bytes_per_line
= ((*w
+ 7) / 8) + padding
;
333 raster_length
= bytes_per_line
* *h
;
334 dataP
= (unsigned char *) malloc((*w
) * (*h
));
336 fprintf(stderr
, "Not enough memory.\n");
337 return ((unsigned char *) NULL
);
342 int lim
= (bytes_per_line
- padding
) * 8;
343 for (bytes
= 0; bytes
< raster_length
; bytes
+= 2) {
344 if (fscanf(fp
, " 0x%x%*[,}]%*[ \n]", &value
) != 1) {
345 fprintf(stderr
, "Error scanning bits item.\n");
346 return ((unsigned char *) NULL
);
350 for (i
= 0; i
< 8; i
++) {
352 if (value
& nibMask
[i
])
360 if ((!padding
) || ((bytes
+ 2) % bytes_per_line
)) {
362 for (i
= 0; i
< 8; i
++) {
364 if (value
& nibMask
[i
])
376 int lim
= bytes_per_line
* 8;
377 for (bytes
= 0; bytes
< raster_length
; bytes
++) {
378 if (fscanf(fp
, " 0x%x%*[,}]%*[ \n]", &value
) != 1) {
379 fprintf(stderr
, "Error scanning bits item.\n");
380 return ((unsigned char *) NULL
);
382 for (i
= 0; i
< 8; i
++) {
384 if (value
& nibMask
[i
])
400 ReadEbmBitmap(fp
, w
, h
, colrs
)
403 struct colr_data
*colrs
;
406 unsigned int bmap_size
;
408 int red
, green
, blue
;
410 unsigned char *bit_data
;
417 if ((fscanf(fp
, "%12s %d %d %d\n", line
, w
, h
, &scale
) == 4) &&
418 (strcmp(line
, "xstitch_data") == 0)) {
419 for (i
= 0; i
< 3; i
++) {
420 fscanf(fp
, "%s", line
);
421 if (strcmp(line
, "red") == 0) {
423 } else if (strcmp(line
, "green") == 0) {
425 } else if (strcmp(line
, "blue") == 0) {
428 fprintf(stderr
, "File has improper format\n");
429 return ((unsigned char *) NULL
);
432 fscanf(fp
, "[^\n]\n");
433 for (i
= 0; i
< scale
; i
++) {
435 if (fscanf(fp
, "%d %d %d%[^\n]\n", ptr
[0], ptr
[1], ptr
[2], line
) < 3) {
436 fprintf(stderr
, "Bad color data #%d\n", i
);
438 colrs
[i
].red
= red
* 65536 / scale
;
439 colrs
[i
].green
= green
* 65536 / scale
;
440 colrs
[i
].blue
= blue
* 65536 / scale
;
442 for (i
= scale
; i
< 256; i
++) {
448 bmap_size
= (*w
) * (*h
);
449 bit_data
= (unsigned char *) malloc(bmap_size
);
450 if (bit_data
== NULL
) {
451 fprintf(stderr
, "Cannot allocate space for bitmap\n");
454 for (i
= 0; i
< bmap_size
; i
++) {
455 if (dscan(fp
, &temp
) == -1)
457 if (temp
> (256 - 1))
459 *bitp
= (unsigned char) temp
;
463 return ((unsigned char *) NULL
);
477 out
= out
| ((in
>> 24) & 0xff);
478 out
= out
| ((in
>> 8) & 0xff00);
479 out
= out
| ((in
<< 8) & 0xff0000);
480 out
= out
| ((in
<< 24) & 0xff000000);
486 ReadCompactEbmBitmap(fp
, w
, h
, colrs
)
489 struct colr_data
*colrs
;
492 unsigned int bmap_size
;
494 unsigned char *bit_data
;
497 struct rgb_color color_data
;
505 fread(&Hdr
, sizeof(struct ebmhdr
), 1, fp
);
506 if (strcmp(Hdr
.magic
, "xstitch") == 0) {
508 * a horrible hack to deal with different byte ordering on
509 * different machines. Is based on assumption of only 2
510 * possible byte orderings, and never more than 256 colors.
512 if (Hdr
.num_colors
> 256) {
514 Hdr
.width
= swapint(Hdr
.width
);
515 Hdr
.height
= swapint(Hdr
.height
);
516 Hdr
.num_colors
= swapint(Hdr
.num_colors
);
521 scale
= Hdr
.num_colors
;
522 for (i
= 0; i
< Hdr
.num_colors
; i
++) {
523 fread(&color_data
, sizeof(struct rgb_color
), 1, fp
);
525 color_data
.red
= swapint(color_data
.red
);
526 color_data
.green
= swapint(color_data
.green
);
527 color_data
.blue
= swapint(color_data
.blue
);
528 color_data
.pix_len
= swapint(color_data
.pix_len
);
530 colrs
[i
].red
= color_data
.red
* 65536 / scale
;
531 colrs
[i
].green
= color_data
.green
* 65536 / scale
;
532 colrs
[i
].blue
= color_data
.blue
* 65536 / scale
;
533 if (color_data
.pix_len
!= 0) {
534 if (color_data
.pix_len
> 1023) {
535 fprintf(stderr
, "bad pattern\n");
536 return ((unsigned char *) NULL
);
538 fread(pix_file
, sizeof(char), color_data
.pix_len
, fp
);
541 for (i
= scale
; i
< 256; i
++) {
547 bmap_size
= (*w
) * (*h
);
548 bit_data
= (unsigned char *) malloc(bmap_size
);
549 if (bit_data
== NULL
) {
550 fprintf(stderr
, "Cannot allocate space for bitmap\n");
552 fread(bit_data
, sizeof(unsigned char), ((*w
) * (*h
)), fp
);
555 for (i
= 0; i
< bmap_size
; i
++) {
556 if ((int) *bitp
> (256 - 1))
557 *bitp
= (unsigned char) 0;
561 return ((unsigned char *) NULL
);
569 ReadBitmap(fp
, w
, h
, colrs
)
572 struct colr_data
*colrs
;
574 unsigned char *bit_data
;
579 bit_data
= ReadEbmBitmap(fp
, w
, h
, colrs
);
580 if (bit_data
!= NULL
) {
584 bit_data
= ReadCompactEbmBitmap(fp
, w
, h
, colrs
);
585 if (bit_data
!= NULL
) {
590 bit_data
= ReadGifBitmap(fp
, w
, h
, colrs
);
591 if (bit_data
!= NULL
) {
596 bit_data
= ReadXbmBitmap(fp
, w
, h
, colrs
);
597 if (bit_data
!= NULL
) {
601 return ((unsigned char *) NULL
);
This page took 0.072918 seconds and 4 git commands to generate.