File: image.c

package info (click to toggle)
bb 1.3rc1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,424 kB
  • ctags: 1,330
  • sloc: ansic: 32,502; sh: 329; makefile: 54
file content (206 lines) | stat: -rw-r--r-- 5,688 bytes parent folder | download | duplicates (10)
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
/*
 * BB: The portable demo
 *
 * (C) 1997 by AA-group (e-mail: aa@horac.ta.jcu.cz)
 *
 * 3rd August 1997
 * version: 1.2 [final3]
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public Licences as by published
 * by the Free Software Foundation; either version 2; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Publis License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <math.h>
#include "minilzo.h"
#include <string.h>
#include <stdlib.h>
#include "bb.h"
#include "image.h"
typedef double atype;
static void fastcscale(char *b1, char *b2, int x1, int x2, int y1, int y2, int width1, int width2)
{
    int ddx1, ddx, spx = 0, ex;
    int ddy1, ddy, spy = 0, ey;
    int x;
    char *bb1 = b1;
    width2 -= x2;
    if (!x1 || !x2 || !y1 || !y2)
	return;
    ddx = x1 + x1;
    ddx1 = x2 + x2;
    if (ddx1 < ddx)
	spx = ddx / ddx1, ddx %= ddx1;
    ddy = y1 + y1;
    ddy1 = y2 + y2;
    if (ddy1 < ddy)
	spy = (ddy / ddy1) * width1, ddy %= ddy1;
    ey = -ddy1;
    for (; y2; y2--) {
	ex = -ddx1;
	for (x = x2; x; x--) {
	    *b2 = *b1;
	    b2++;
	    b1 += spx;
	    ex += ddx;
	    if (ex > 0) {
		b1++;
		ex -= ddx1;
	    }
	}
	b2 += width2;
	bb1 += spy;
	ey += ddy;
	if (ey > 0) {
	    bb1 += width1;
	    ey -= ddy1;
	}
	b1 = bb1;
    }
}

void scale(struct image *image, int x1, int y1, int x2, int y2)
{
    aa_context *c = context;
    int width = aa_imgwidth(c);
    int height = aa_imgheight(c);
    int imgwidth = image->width;
    int imgheight = image->height;
    int d = 0;
    static atype xstep, ystep;
    char *imgdata;
    if (x2 < 0 || x1 >= imgwidth || y2 < 0 || y1 >= imgheight) {
	memset(c->imagebuffer, 0, aa_imgwidth(c) * aa_imgheight(c));
	return;
    }
    if (image->decompressed == NULL)
	decompressimg(image), d = 1;
    imgdata = image->decompressed;
    if (x1 < 0 || y1 < 0 || x2 >= imgwidth || y2 >= imgheight) {
	int xx1 = 0, xx2 = aa_imgwidth(c) - 1, yy1 = 0, yy2 = aa_imgheight(c) - 1;
	memset(c->imagebuffer, 0, aa_imgwidth(c) * aa_imgheight(c));
	xstep = ((atype) width) / (x2 - x1 - 1);
	ystep = ((atype) height) / (y2 - y1 - 1);
	if (x2 >= imgwidth) {
	    xx2 = (imgwidth - 1 - x1) * xstep;
	    x2 = imgwidth - 1;
	}
	if (y2 >= imgheight) {
	    yy2 = (imgheight - 1 - y1) * ystep;
	    y2 = imgheight - 1;
	}
	if (x1 < 0) {
	    xx1 = -xstep * x1;
	    x1 = 0;
	}
	if (y1 < 0) {
	    yy1 = -ystep * y1;
	    y1 = 0;
	}
	fastcscale(imgdata + x1 + imgwidth * y1, c->imagebuffer + xx1 + yy1 * width, x2 - x1, xx2 - xx1, y2 - y1, yy2 - yy1, imgwidth, aa_imgwidth(c));
	return;
    }
    else
	fastcscale(imgdata + x1 + imgwidth * y1, c->imagebuffer, x2 - x1, aa_imgwidth(c), y2 - y1, aa_imgheight(c), imgwidth, aa_imgwidth(c));
    if (d) {
	free(image->decompressed);
	image->decompressed = NULL;
    }
    return;
}
int dualmode = 0;
void scale2(struct image *image, int x1, int y1, int x2, int y2)
{
    aa_context *c = context;
    int width = aa_imgwidth(c) / 2;
    int height = aa_imgheight(c);
    int imgwidth = image->width;
    int imgheight = image->height;
    int d = 0;
    static atype xstep, ystep;
    char *imgdata;
    dualmode ^= 1;
    if (image->decompressed == NULL)
	decompressimg(image), d = 1;
    imgdata = image->decompressed;
    /*memset(c->imagebuffer, 0, aa_imgwidth(c) * aa_imgheight(c)); */
    if (x1 < 0 || y1 < 0 || x2 >= imgwidth || y2 >= imgheight) {
	int xx1 = 0, xx2 = aa_imgwidth(c) / 2 - 1, yy1 = 0, yy2 = aa_imgheight(c) - 1;
	xstep = ((atype) width) / (x2 - x1 - 1);
	ystep = ((atype) height) / (y2 - y1 - 1);
	if (x2 >= imgwidth) {
	    xx2 = (imgwidth - 1 - x1) * xstep;
	    x2 = imgwidth - 1;
	}
	if (y2 >= imgheight) {
	    yy2 = (imgheight - 1 - y1) * ystep;
	    y2 = imgheight - 1;
	}
	if (x1 < 0) {
	    xx1 = -xstep * x1;
	    x1 = 0;
	}
	if (y1 < 0) {
	    yy1 = -ystep * y1;
	    y1 = 0;
	}
	fastcscale(imgdata + x1 + imgwidth * y1, c->imagebuffer + width * dualmode + xx1 + yy1 * width, x2 - x1, xx2 - xx1, y2 - y1, yy2 - yy1, imgwidth, aa_imgwidth(c));
	return;
    }
    else
	fastcscale(imgdata + x1 + imgwidth * y1, c->imagebuffer + width * dualmode, x2 - x1, aa_imgwidth(c) / 2, y2 - y1, aa_imgheight(c), imgwidth, aa_imgwidth(c));
    if (d) {
	free(image->decompressed);
	image->decompressed = NULL;
    }
    return;
}

char *decompressimg(struct image *image)
{
    char *data = malloc(image->width * image->height + 5);
    int size = image->width * image->height;
    int r;
    r = lzo1x_decompress(image->data, image->size, data, &size, NULL);
    if (r != LZO_E_OK) {
	printf("compression failed: %d %i %i %i\n", r, image->size, image->width, image->height);
	exit(-1);
    }
    image->decompressed = data;

    return (data);
}
void dispimg(struct image *image, int dm)
{
    int x1, y1, x2, y2;
    int imgwidth = image->width;
    int imgheight = image->height;
    int width = aa_mmwidth(context);
    if (dm)
	width /= 2;
    x2 = imgwidth;
    y2 = imgheight;
    if (imgwidth * aa_mmheight(context) / width > imgheight)
	y2 = imgwidth * aa_mmheight(context) / width;
    else
	x2 = imgheight * width / aa_mmheight(context);
    x1 = (imgwidth - x2) / 2;
    x2 += x1;
    y1 = (imgheight - y2) / 2;
    y2 += y1;
    if (dm)
	scale2(image, x1, y1, x2, y2);
    else
	scale(image, x1, y1, x2, y2);

}