aboutsummaryrefslogtreecommitdiffstats
path: root/src/netpbm.c
blob: f30dbbc1a7d6e291fc36aaf9570c913bf6d9a63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "meh.h"

struct netpbm_t{
	struct image img;
	FILE *f;
	char format;
	unsigned int maxval;
};

void skipspace(FILE *f){
	int c;
	for(;;){
		c = fgetc(f);
		while(isspace(c))
			c = fgetc(f);
		if(c == '#')
			while(fgetc(f) != '\n');
		else
			break;
	}
	ungetc(c, f);
}

struct image *netpbm_open(FILE *f){
	struct netpbm_t *b;
	rewind(f);

	char format;
	if(fgetc(f) != 'P')
		return NULL;
	format = fgetc(f);
	if(format > '6' || format < '1')
		return NULL;

	b = malloc(sizeof(struct netpbm_t));
	b->format = format;

	skipspace(f);
	fscanf(f, "%u", &b->img.bufwidth);
	skipspace(f);
	fscanf(f, "%u", &b->img.bufheight);
	if(format == '1' || format == '4'){
		b->maxval = 1;
	}else{
		skipspace(f);
		fscanf(f, "%u", &b->maxval);
	}

	/* whitespace character */
	fgetc(f);

	b->f = f;
	b->img.fmt = &netpbm;

	return (struct image *)b;
}

static unsigned char readvali(struct netpbm_t *b){
	skipspace(b->f);
	int val;
	fscanf(b->f, "%i", &val);
	return val * 255 / b->maxval;
}

static unsigned char readvalb(struct netpbm_t *b){
	if(b->maxval == 65535){
		int val = fgetc(b->f) << 8;
		val |= fgetc(b->f);
		return val * 255 / b->maxval;
	}else{
		int val = fgetc(b->f);
		return val * 255 / b->maxval;
	}
}

int netpbm_read(struct image *img){
	struct netpbm_t *b = (struct netpbm_t *)img;
	FILE *f = b->f;
	int a = 0;

	int left = img->bufwidth * img->bufheight;
	int j, c, val;

	if(b->format == '1'){
		while(left--){
			skipspace(f);
			val = fgetc(f);
			val = val == '1' ? 0 : 255;
			img->buf[a++] = val;
			img->buf[a++] = val;
			img->buf[a++] = val;
		}
	}else if(b->format == '2'){
		while(left--){
			val = readvali(b);
			img->buf[a++] = val;
			img->buf[a++] = val;
			img->buf[a++] = val;
		}
	}else if(b->format == '3'){
		while(left--){
			img->buf[a++] = readvali(b);
			img->buf[a++] = readvali(b);
			img->buf[a++] = readvali(b);
		}
	}else if(b->format == '4'){
		while(left){
			c = fgetc(f);
			for(j = 0; j < 8 && left; j++){
				int val = (c & 1) ? 0 : 255;
				img->buf[a++] = val;
				img->buf[a++] = val;
				img->buf[a++] = val;
				c >>= 1;
				left--;
			}
		}
	}else if(b->format == '5'){
		while(left--){
			val = readvalb(b);
			img->buf[a++] = val;
			img->buf[a++] = val;
			img->buf[a++] = val;
		}
	}else if(b->format == '6'){
		if(b->maxval == 255){
			fread(img->buf, 1, left * 3, f);
		}else{
			while(left--){
				img->buf[a++] = readvalb(b);
				img->buf[a++] = readvalb(b);
				img->buf[a++] = readvalb(b);
			}
		}
	}

	img->state |= LOADED | SLOWLOADED;
	return 0;
}

void netpbm_close(struct image *img){
	struct netpbm_t *b = (struct netpbm_t *)img;
	fclose(b->f);
}

struct imageformat netpbm = {
	netpbm_open,
	NULL,
	netpbm_read,
	netpbm_close
};