Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/filter/image.c
blob: 14784192494b7528acd9ebc7a956783156a7e34f (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifndef _plan9_
#include <string.h>
#include <fconfig.h>
#include <assert.h>
#ifdef NO_MALLOC_H
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>
#else
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include <fconfig.h>
#endif
#include <filter.h>
void flipgeneric(struct image *img)
{
    pixel_t **line;
    assert(img->nimages == 2);
    img->currimage ^= 1;
    line = img->currlines;
    img->currlines = img->oldlines;
    img->oldlines = line;
}

int bytesperpixel(int type)
{
    switch (type) {
    case MBITMAP:
    case LBITMAP:
    case LIBITMAP:
    case MIBITMAP:
	return 0;
    case SMALLITER:
    case FIXEDCOLOR:
    case GRAYSCALE:
    case C256:
	return 1;
    case LARGEITER:
    case TRUECOLOR16:
	return 2;
    case TRUECOLOR24:
	return 3;
    case TRUECOLOR:
	return 4;
    default:
	assert(0);
	return 0;
    }
}

struct image *create_image_lines(int width, int height,
				 int nimages, pixel_t ** lines1,
				 pixel_t ** lines2,
				 struct palette *palette,
				 void (*flip) (struct image * img),
				 int flags, float pixelwidth,
				 float pixelheight)
{
    int i;
    static int version = 1;
    struct image *img = (struct image *) calloc(1, sizeof(*img));
    if (img == NULL)
	return NULL;
    if (flip == NULL)
	flip = flipgeneric;
    img->width = width;
    img->height = height;
    img->nimages = nimages;
    img->bytesperpixel = bytesperpixel(palette->type);
    img->palette = palette;
    img->currimage = 0;
    img->flip = flip;
    img->flags = flags;
    img->version = version;
    version += 65535;
    img->currlines = lines1;
    img->oldlines = lines2;
    img->pixelwidth = pixelwidth;
    img->pixelheight = pixelheight;
    if (lines1 != NULL && (nimages != 2 || lines2 != NULL)) {
	img->scanline = (int) (lines1[1] - lines1[0]);
	if (img->scanline < 0)
	    img->scanline = -1;
	else {
	    for (i = 0; i < height; i++) {
		if (lines1[0] - lines1[i] != img->scanline * i) {
		    img->scanline = -1;
		    break;
		}
		if (nimages == 2
		    && lines2[0] - lines2[i] != img->scanline * i) {
		    img->scanline = -1;
		    break;
		}
	    }
	}
    } else
	img->scanline = -1;
    return (img);
}

struct image *create_image_cont(int width, int height, int scanlinesize,
				int nimages, pixel_t * buf1,
				pixel_t * buf2, struct palette *palette,
				void (*flip) (struct image * img),
				int flags, float pixelwidth,
				float pixelheight)
{
    struct image *img =
	create_image_lines(width, height, nimages, NULL, NULL, palette,
			   flip,
			   flags, pixelwidth, pixelheight);
    int i;
    if (img == NULL) {
	return NULL;
    }
    if ((img->currlines =
	 (pixel_t **) malloc(sizeof(*img->currlines) * height)) == NULL) {
	free(img);
	return NULL;
    }
    if (nimages == 2) {
	if ((img->oldlines =
	     (pixel_t **) malloc(sizeof(*img->oldlines) * height)) == NULL)
	{
	    free(img->currlines);
	    free(img);
	    return NULL;
	}
    }
    for (i = 0; i < img->height; i++) {
	img->currlines[i] = buf1;
	buf1 += scanlinesize;
    }
    if (nimages == 2)
	for (i = 0; i < img->height; i++) {
	    img->oldlines[i] = buf2;
	    buf2 += scanlinesize;
	}
    img->flags |= FREELINES;
    img->scanline = scanlinesize;
    return (img);
}

struct image *create_image_mem(int width, int height, int nimages,
			       struct palette *palette, float pixelwidth,
			       float pixelheight)
{
    unsigned char *data =
	(unsigned char *) calloc(((width + 3) & ~3) * height,
				 bytesperpixel(palette->type));
    unsigned char *data1 =
	(unsigned char *) (nimages ==
			   2 ? calloc(((width + 3) & ~3) * height,
				      bytesperpixel(palette->
						    type)) : NULL);
    struct image *img;
    if (data == NULL) {
#ifdef DEBUG
	printf("Image:out of memory\n");
#endif
	return (NULL);
    }
    if (nimages == 2 && data1 == NULL) {
	free(data);
#ifdef DEBUG
	printf("Image:out of memory2\n");
#endif
	return NULL;
    }
    img =
	create_image_cont(width, height,
			  ((width +
			    3) & ~3) * bytesperpixel(palette->type),
			  nimages, data, data1, palette, NULL, 0,
			  pixelwidth, pixelheight);
    if (img == NULL) {
	free(data);
	if (data1 != NULL)
	    free(data1);
	return NULL;
    }
    img->flags |= FREEDATA;
    return (img);
}

struct image *create_subimage(struct image *simg, int width, int height,
			      int nimages, struct palette *palette,
			      float pixelwidth, float pixelheight)
{
    int size = height * bytesperpixel(palette->type);
    int i;
    int shift1 = 0, shift2 = 0;
    struct image *img;
    if (size > simg->height * simg->bytesperpixel || height > simg->height
	|| (nimages == 2 && simg->nimages == 1))
	return (create_image_mem
		(width, height, nimages, palette, pixelwidth,
		 pixelheight));
    nimages = simg->nimages;
    img =
	create_image_lines(width, height, nimages, NULL, NULL, palette,
			   NULL, 0, pixelwidth, pixelheight);
    if (img == NULL)
	return NULL;
    if ((img->currlines =
	 (pixel_t **) malloc(sizeof(*img->currlines) * height)) == NULL) {
	free(img);
	return NULL;
    }
    if (nimages == 2) {
	if ((img->oldlines =
	     (pixel_t **) malloc(sizeof(*img->oldlines) * height)) == NULL)
	{
	    free(img->currlines);
	    free(img);
	    return NULL;
	}
    }
    shift1 = simg->height - img->height;
    shift2 =
	simg->width * simg->bytesperpixel -
	img->width * img->bytesperpixel;
    for (i = 0; i < img->height; i++) {
	img->currlines[i] = simg->currlines[i + shift1] + shift2;
    }
    if (nimages == 2)
	for (i = 0; i < img->height; i++) {
	    img->oldlines[i] = simg->oldlines[i + shift1] + shift2;
	}
    img->flags |= FREELINES;
    img->currimage = simg->currimage;
    return (img);
}

void destroy_image(struct image *img)
{
    if (img->flags & FREEDATA) {
	free(*img->currlines);
	if (img->nimages == 2)
	    free(*img->oldlines);
    }
    if (img->flags & FREELINES) {
	free(img->currlines);
	if (img->nimages == 2)
	    free(img->oldlines);
    }
    free(img);
}

void clear_image(struct image *img)
{
    int i;
    int color = img->palette->pixels[0];
    int width = img->width * img->bytesperpixel;
    if (img->palette->npreallocated)
	color = img->palette->index[0];
    if (!width) {
	width = (img->width + 7) / 8;
	if (color)
	    color = 255;
    }
    for (i = 0; i < img->height; i++)
	memset(img->currlines[i], color, width);
}