aboutsummaryrefslogtreecommitdiffstats
path: root/src/bmp.c
blob: 046fad602f6f4695dc4764ea5314225ff40ee06a (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
158
#include <stdio.h>
#include <stdlib.h>

#include "meh.h"

unsigned short getshort(FILE *f){
	unsigned short ret;
	ret = getc(f);
	ret = ret | (getc(f) << 8);
	return ret;
}

unsigned long getlong(FILE *f){
	unsigned short ret;
	ret = getc(f);
	ret = ret | (getc(f) << 8);
	ret = ret | (getc(f) << 16);
	ret = ret | (getc(f) << 24);
	return ret;
}

struct rgb_t{
	unsigned char r, g, b;
};

struct bmp_t{
	struct image img;
	unsigned long bitmapoffset;
	int compression;
	int bpp;
	int ncolors;
	struct rgb_t *colours;
	unsigned int rowwidth;
};

struct image *bmp_open(FILE *f){
	struct bmp_t *b;
	unsigned long headersize;
	
	rewind(f);
	if(getc(f) != 'B' || getc(f) != 'M')
		return NULL;

	b = malloc(sizeof(struct bmp_t));
	b->img.f = f;

	fseek(f, 10, SEEK_SET);
	b->bitmapoffset = getlong(f);

	fseek(f, 14, SEEK_SET);
	headersize = getlong(f);

	if(headersize == 12){ /* OS/2 v1 */
		b->ncolors = 0;
		fseek(f, 18, SEEK_SET);
		b->img.width = getshort(f);
		b->img.height = getshort(f);
		b->compression = 0;
	}else{
		fseek(f, 18, SEEK_SET);
		b->img.width = getlong(f);
		b->img.height = getlong(f);

		fseek(f, 28, SEEK_SET);
		b->bpp = getshort(f);

		fseek(f, 30, SEEK_SET);
		b->compression = getlong(f);

		fseek(f, 46, SEEK_SET);
		b->ncolors = getlong(f);
	}

	if(!b->ncolors){
		b->ncolors = 1 << b->bpp;
	}

	if(b->compression){
		fprintf(stderr, "unsupported compression method %i\n", b->compression);
		return NULL;
	}

	if(b->bpp >= 16){
		b->rowwidth = b->img.width * b->bpp;
	}else{
		int i;
		b->colours = malloc(b->ncolors * sizeof(struct rgb_t));
		fseek(f, 14+headersize, SEEK_SET);
		for(i = 0; i < b->ncolors; i++){
			b->colours[i].b = getc(f);
			b->colours[i].g = getc(f);
			b->colours[i].r = getc(f);
			if(headersize != 12)
				getc(f);
		}
		b->rowwidth = (b->img.width * b->bpp + 7) / 8;
	}
	if(b->rowwidth & 3){
		b->rowwidth += 4 - (b->rowwidth & 3);
	}

	return (struct image *)b;
}

static void readrow(struct image *img, unsigned char *row, unsigned char *buf){
	struct bmp_t *b = (struct bmp_t *)img;
	int x;
	int i = 0;
	if(b->bpp == 24 || b->bpp == 32){
		for(x = 0; x < img->width * 3; x+=3){
			buf[x + 2] = row[i++];
			buf[x + 1] = row[i++];
			buf[x + 0] = row[i++];
			if(b->bpp == 32)
				i++;
		}
	}else if(b->bpp <= 8){
		int mask;
		int pixelsperbit = 8 / b->bpp;
		mask = ~((~0) << b->bpp);
		for(x = 0; x < img->width; x++){
			unsigned char c = ((row[i / pixelsperbit]) >> ((8 - ((i+1) % pixelsperbit) * b->bpp)) % 8) & mask;
			*buf++ = b->colours[c].r;
			*buf++ = b->colours[c].g;
			*buf++ = b->colours[c].b;
			i++;
		}
	}else{
		fprintf(stderr, "bad bpp %i\n", b->bpp);
		return;
	}
}

int bmp_read(struct image *img){
	struct bmp_t *b = (struct bmp_t *)img;
	FILE *f = img->f;

	fseek(f, b->bitmapoffset, SEEK_SET);
	int i, y;
	int dy = img->width * 3;
	i = img->height * dy;
	unsigned char row[b->rowwidth];
	for(y = img->height; y; y--){
		i -= dy;
		if(fread(row, 1, b->rowwidth, f) != b->rowwidth)
			return 1;
		readrow(img, row, &img->buf[i]);
	}

	return 0;
}

struct imageformat gif = {
	bmp_open,
	bmp_read
};