Debian Codegraph @

ext/scale2x/scalebit.cpp

raw symbols SCIP
1 /*
2 * This file is part of the Scale2x project.
3 *
4 * Copyright (C) 2003, 2004 Andrea Mazzoleni
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 /*
18 * This file contains an example implementation of the Scale effect
19 * applyed to a generic bitmap.
20 *
21 * You can find an high level description of the effect at :
22 *
23 * http://www.scale2x.it/
24 */
25
26 #include "scale2x.h"
27 #include "scale3x.h"
28
29 #include <assert.h>
30 #include <stdlib.h>
31
32 #define SSDST(bits, num) (scale2x_uint ## bits *)dst ## num
33 #define SSSRC(bits, num) (const scale2x_uint ## bits *)src ## num
34
35 /**
36 * Apply the Scale2x effect on a group of rows. Used internally.
37 */
38 static inline void stage_scale2x(void* dst0, void* dst1, const void* src0, const void* src1, const void* src2, unsigned pixel, unsigned pixel_per_row)
39 {
40 switch (pixel) {
41 #ifdef USE_SCALE2X_SSE2
42 case 1 : scale2x_8_sse2(SSDST(8, 0), SSDST(8, 1), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
43 case 2 : scale2x_16_sse2(SSDST(16, 0), SSDST(16, 1), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
44 case 4 : scale2x_32_sse2(SSDST(32, 0), SSDST(32, 1), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
45 #else
46 case 1 : scale2x_8_def(SSDST(8, 0), SSDST(8, 1), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
47 case 2 : scale2x_16_def(SSDST(16, 0), SSDST(16, 1), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
48 case 4 : scale2x_32_def(SSDST(32, 0), SSDST(32, 1), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
49 #endif
50 }
51 }
52
53 /**
54 * Apply the Scale2x3 effect on a group of rows. Used internally.
55 */
56 static inline void stage_scale2x3(void* dst0, void* dst1, void* dst2, const void* src0, const void* src1, const void* src2, unsigned pixel, unsigned pixel_per_row)
57 {
58 switch (pixel) {
59 #ifdef USE_SCALE2X_SSE2
60 case 1 : scale2x3_8_sse2(SSDST(8, 0), SSDST(8, 1), SSDST(8, 2), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
61 case 2 : scale2x3_16_sse2(SSDST(16, 0), SSDST(16, 1), SSDST(16, 2), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
62 case 4 : scale2x3_32_sse2(SSDST(32, 0), SSDST(32, 1), SSDST(32, 2), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
63 #else
64 case 1 : scale2x3_8_def(SSDST(8, 0), SSDST(8, 1), SSDST(8, 2), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
65 case 2 : scale2x3_16_def(SSDST(16, 0), SSDST(16, 1), SSDST(16, 2), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
66 case 4 : scale2x3_32_def(SSDST(32, 0), SSDST(32, 1), SSDST(32, 2), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
67 #endif
68 }
69 }
70
71 /**
72 * Apply the Scale2x4 effect on a group of rows. Used internally.
73 */
74 static inline void stage_scale2x4(void* dst0, void* dst1, void* dst2, void* dst3, const void* src0, const void* src1, const void* src2, unsigned pixel, unsigned pixel_per_row)
75 {
76 switch (pixel) {
77 #ifdef USE_SCALE2X_SSE2
78 case 1 : scale2x4_8_sse2(SSDST(8, 0), SSDST(8, 1), SSDST(8, 2), SSDST(8, 3), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
79 case 2 : scale2x4_16_sse2(SSDST(16, 0), SSDST(16, 1), SSDST(16, 2), SSDST(16, 3), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
80 case 4 : scale2x4_32_sse2(SSDST(32, 0), SSDST(32, 1), SSDST(32, 2), SSDST(32, 3), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
81 #else
82 case 1 : scale2x4_8_def(SSDST(8, 0), SSDST(8, 1), SSDST(8, 2), SSDST(8, 3), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
83 case 2 : scale2x4_16_def(SSDST(16, 0), SSDST(16, 1), SSDST(16, 2), SSDST(16, 3), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
84 case 4 : scale2x4_32_def(SSDST(32, 0), SSDST(32, 1), SSDST(32, 2), SSDST(32, 3), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
85 #endif
86 }
87 }
88
89 /**
90 * Apply the Scale3x effect on a group of rows. Used internally.
91 */
92 static inline void stage_scale3x(void* dst0, void* dst1, void* dst2, const void* src0, const void* src1, const void* src2, unsigned pixel, unsigned pixel_per_row)
93 {
94 switch (pixel) {
95 case 1 : scale3x_8_def(SSDST(8, 0), SSDST(8, 1), SSDST(8, 2), SSSRC(8, 0), SSSRC(8, 1), SSSRC(8, 2), pixel_per_row); break;
96 case 2 : scale3x_16_def(SSDST(16, 0), SSDST(16, 1), SSDST(16, 2), SSSRC(16, 0), SSSRC(16, 1), SSSRC(16, 2), pixel_per_row); break;
97 case 4 : scale3x_32_def(SSDST(32, 0), SSDST(32, 1), SSDST(32, 2), SSSRC(32, 0), SSSRC(32, 1), SSSRC(32, 2), pixel_per_row); break;
98 }
99 }
100
101 /**
102 * Apply the Scale4x effect on a group of rows. Used internally.
103 */
104 static inline void stage_scale4x(void* dst0, void* dst1, void* dst2, void* dst3, const void* src0, const void* src1, const void* src2, const void* src3, unsigned pixel, unsigned pixel_per_row)
105 {
106 stage_scale2x(dst0, dst1, src0, src1, src2, pixel, 2 * pixel_per_row);
107 stage_scale2x(dst2, dst3, src1, src2, src3, pixel, 2 * pixel_per_row);
108 }
109
110 #define SCDST(i) (dst + (i) * dst_slice)
111 #define SCSRC(i) (src + (i) * src_slice)
112 #define SCMID(i) (mid[(i)])
113
114 /**
115 * Apply the Scale2x effect on a bitmap.
116 * The destination bitmap is filled with the scaled version of the source bitmap.
117 * The source bitmap isn't modified.
118 * The destination bitmap must be manually allocated before calling the function,
119 * note that the resulting size is exactly 2x2 times the size of the source bitmap.
120 * \param void_dst Pointer at the first pixel of the destination bitmap.
121 * \param dst_slice Size in bytes of a destination bitmap row.
122 * \param void_src Pointer at the first pixel of the source bitmap.
123 * \param src_slice Size in bytes of a source bitmap row.
124 * \param pixel Bytes per pixel of the source and destination bitmap.
125 * \param width Horizontal size in pixels of the source bitmap.
126 * \param height Vertical size in pixels of the source bitmap.
127 */
128 static void scale2x(void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
129 {
130 unsigned char* dst = (unsigned char*)void_dst;
131 const unsigned char* src = (const unsigned char*)void_src;
132 unsigned count;
133
134 assert(height >= 2);
135
136 count = height;
137
138 stage_scale2x(SCDST(0), SCDST(1), SCSRC(0), SCSRC(0), SCSRC(1), pixel, width);
139
140 dst = SCDST(2);
141
142 count -= 2;
143 while (count) {
144 stage_scale2x(SCDST(0), SCDST(1), SCSRC(0), SCSRC(1), SCSRC(2), pixel, width);
145
146 dst = SCDST(2);
147 src = SCSRC(1);
148
149 --count;
150 }
151
152 stage_scale2x(SCDST(0), SCDST(1), SCSRC(0), SCSRC(1), SCSRC(1), pixel, width);
153 }
154
155 /**
156 * Apply the Scale2x3 effect on a bitmap.
157 * The destination bitmap is filled with the scaled version of the source bitmap.
158 * The source bitmap isn't modified.
159 * The destination bitmap must be manually allocated before calling the function,
160 * note that the resulting size is exactly 2x3 times the size of the source bitmap.
161 * \param void_dst Pointer at the first pixel of the destination bitmap.
162 * \param dst_slice Size in bytes of a destination bitmap row.
163 * \param void_src Pointer at the first pixel of the source bitmap.
164 * \param src_slice Size in bytes of a source bitmap row.
165 * \param pixel Bytes per pixel of the source and destination bitmap.
166 * \param width Horizontal size in pixels of the source bitmap.
167 * \param height Vertical size in pixels of the source bitmap.
168 */
169 static void scale2x3(void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
170 {
171 unsigned char* dst = (unsigned char*)void_dst;
172 const unsigned char* src = (const unsigned char*)void_src;
173 unsigned count;
174
175 assert(height >= 2);
176
177 count = height;
178
179 stage_scale2x3(SCDST(0), SCDST(1), SCDST(2), SCSRC(0), SCSRC(0), SCSRC(1), pixel, width);
180
181 dst = SCDST(3);
182
183 count -= 2;
184 while (count) {
185 stage_scale2x3(SCDST(0), SCDST(1), SCDST(2), SCSRC(0), SCSRC(1), SCSRC(2), pixel, width);
186
187 dst = SCDST(3);
188 src = SCSRC(1);
189
190 --count;
191 }
192
193 stage_scale2x3(SCDST(0), SCDST(1), SCDST(2), SCSRC(0), SCSRC(1), SCSRC(1), pixel, width);
194 }
195
196 /**
197 * Apply the Scale2x4 effect on a bitmap.
198 * The destination bitmap is filled with the scaled version of the source bitmap.
199 * The source bitmap isn't modified.
200 * The destination bitmap must be manually allocated before calling the function,
201 * note that the resulting size is exactly 2x4 times the size of the source bitmap.
202 * \param void_dst Pointer at the first pixel of the destination bitmap.
203 * \param dst_slice Size in bytes of a destination bitmap row.
204 * \param void_src Pointer at the first pixel of the source bitmap.
205 * \param src_slice Size in bytes of a source bitmap row.
206 * \param pixel Bytes per pixel of the source and destination bitmap.
207 * \param width Horizontal size in pixels of the source bitmap.
208 * \param height Vertical size in pixels of the source bitmap.
209 */
210 static void scale2x4(void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
211 {
212 unsigned char* dst = (unsigned char*)void_dst;
213 const unsigned char* src = (const unsigned char*)void_src;
214 unsigned count;
215
216 assert(height >= 2);
217
218 count = height;
219
220 stage_scale2x4(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCSRC(0), SCSRC(0), SCSRC(1), pixel, width);
221
222 dst = SCDST(4);
223
224 count -= 2;
225 while (count) {
226 stage_scale2x4(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCSRC(0), SCSRC(1), SCSRC(2), pixel, width);
227
228 dst = SCDST(4);
229 src = SCSRC(1);
230
231 --count;
232 }
233
234 stage_scale2x4(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCSRC(0), SCSRC(1), SCSRC(1), pixel, width);
235 }
236
237 /**
238 * Apply the Scale3x effect on a bitmap.
239 * The destination bitmap is filled with the scaled version of the source bitmap.
240 * The source bitmap isn't modified.
241 * The destination bitmap must be manually allocated before calling the function,
242 * note that the resulting size is exactly 3x3 times the size of the source bitmap.
243 * \param void_dst Pointer at the first pixel of the destination bitmap.
244 * \param dst_slice Size in bytes of a destination bitmap row.
245 * \param void_src Pointer at the first pixel of the source bitmap.
246 * \param src_slice Size in bytes of a source bitmap row.
247 * \param pixel Bytes per pixel of the source and destination bitmap.
248 * \param width Horizontal size in pixels of the source bitmap.
249 * \param height Vertical size in pixels of the source bitmap.
250 */
251 static void scale3x(void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
252 {
253 unsigned char* dst = (unsigned char*)void_dst;
254 const unsigned char* src = (const unsigned char*)void_src;
255 unsigned count;
256
257 assert(height >= 2);
258
259 count = height;
260
261 stage_scale3x(SCDST(0), SCDST(1), SCDST(2), SCSRC(0), SCSRC(0), SCSRC(1), pixel, width);
262
263 dst = SCDST(3);
264
265 count -= 2;
266 while (count) {
267 stage_scale3x(SCDST(0), SCDST(1), SCDST(2), SCSRC(0), SCSRC(1), SCSRC(2), pixel, width);
268
269 dst = SCDST(3);
270 src = SCSRC(1);
271
272 --count;
273 }
274
275 stage_scale3x(SCDST(0), SCDST(1), SCDST(2), SCSRC(0), SCSRC(1), SCSRC(1), pixel, width);
276 }
277
278 /**
279 * Apply the Scale4x effect on a bitmap.
280 * The destination bitmap is filled with the scaled version of the source bitmap.
281 * The source bitmap isn't modified.
282 * The destination bitmap must be manually allocated before calling the function,
283 * note that the resulting size is exactly 4x4 times the size of the source bitmap.
284 * \note This function requires also a small buffer bitmap used internally to store
285 * intermediate results. This bitmap must have at least an horizontal size in bytes of 2*width*pixel,
286 * and a vertical size of 6 rows. The memory of this buffer must not be allocated
287 * in video memory because it's also read and not only written. Generally
288 * a heap (malloc) or a stack (alloca) buffer is the best choice.
289 * \param void_dst Pointer at the first pixel of the destination bitmap.
290 * \param dst_slice Size in bytes of a destination bitmap row.
291 * \param void_mid Pointer at the first pixel of the buffer bitmap.
292 * \param mid_slice Size in bytes of a buffer bitmap row.
293 * \param void_src Pointer at the first pixel of the source bitmap.
294 * \param src_slice Size in bytes of a source bitmap row.
295 * \param pixel Bytes per pixel of the source and destination bitmap.
296 * \param width Horizontal size in pixels of the source bitmap.
297 * \param height Vertical size in pixels of the source bitmap.
298 */
299 static void scale4x_buf(void* void_dst, unsigned dst_slice, void* void_mid, unsigned mid_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
300 {
301 unsigned char* dst = (unsigned char*)void_dst;
302 const unsigned char* src = (const unsigned char*)void_src;
303 unsigned count;
304 unsigned char* mid[6];
305
306 assert(height >= 4);
307
308 count = height;
309
310 /* set the 6 buffer pointers */
311 mid[0] = (unsigned char*)void_mid;
312 mid[1] = mid[0] + mid_slice;
313 mid[2] = mid[1] + mid_slice;
314 mid[3] = mid[2] + mid_slice;
315 mid[4] = mid[3] + mid_slice;
316 mid[5] = mid[4] + mid_slice;
317
318 stage_scale2x(SCMID(-2 + 6), SCMID(-1 + 6), SCSRC(0), SCSRC(0), SCSRC(1), pixel, width);
319 stage_scale2x(SCMID(0), SCMID(1), SCSRC(0), SCSRC(1), SCSRC(2), pixel, width);
320 stage_scale2x(SCMID(2), SCMID(3), SCSRC(1), SCSRC(2), SCSRC(3), pixel, width);
321 stage_scale4x(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCMID(-2 + 6), SCMID(-2 + 6), SCMID(-1 + 6), SCMID(0), pixel, width);
322
323 dst = SCDST(4);
324
325 stage_scale4x(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCMID(-1 + 6), SCMID(0), SCMID(1), SCMID(2), pixel, width);
326
327 dst = SCDST(4);
328
329 count -= 4;
330 while (count) {
331 unsigned char* tmp;
332
333 stage_scale2x(SCMID(4), SCMID(5), SCSRC(2), SCSRC(3), SCSRC(4), pixel, width);
334 stage_scale4x(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCMID(1), SCMID(2), SCMID(3), SCMID(4), pixel, width);
335
336 dst = SCDST(4);
337 src = SCSRC(1);
338
339 tmp = SCMID(0); /* shift by 2 position */
340 SCMID(0) = SCMID(2);
341 SCMID(2) = SCMID(4);
342 SCMID(4) = tmp;
343 tmp = SCMID(1);
344 SCMID(1) = SCMID(3);
345 SCMID(3) = SCMID(5);
346 SCMID(5) = tmp;
347
348 --count;
349 }
350
351 stage_scale2x(SCMID(4), SCMID(5), SCSRC(2), SCSRC(3), SCSRC(3), pixel, width);
352 stage_scale4x(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCMID(1), SCMID(2), SCMID(3), SCMID(4), pixel, width);
353
354 dst = SCDST(4);
355
356 stage_scale4x(SCDST(0), SCDST(1), SCDST(2), SCDST(3), SCMID(3), SCMID(4), SCMID(5), SCMID(5), pixel, width);
357 }
358
359 /**
360 * Apply the Scale4x effect on a bitmap.
361 * The destination bitmap is filled with the scaled version of the source bitmap.
362 * The source bitmap isn't modified.
363 * The destination bitmap must be manually allocated before calling the function,
364 * note that the resulting size is exactly 4x4 times the size of the source bitmap.
365 * \note This function operates like ::scale4x_buf() but the intermediate buffer is
366 * automatically allocated in the stack.
367 * \param void_dst Pointer at the first pixel of the destination bitmap.
368 * \param dst_slice Size in bytes of a destination bitmap row.
369 * \param void_src Pointer at the first pixel of the source bitmap.
370 * \param src_slice Size in bytes of a source bitmap row.
371 * \param pixel Bytes per pixel of the source and destination bitmap.
372 * \param width Horizontal size in pixels of the source bitmap.
373 * \param height Vertical size in pixels of the source bitmap.
374 */
375 static void scale4x(void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
376 {
377 unsigned mid_slice;
378 void* mid_ptr;
379 void* mid_alloc;
380
381 /* required space for 1 row buffer */
382 mid_slice = scale2x_align_size(2 * pixel * width);
383
384 mid_alloc = malloc(6 * mid_slice + SCALE2X_ALIGN_ALLOC); /* allocate space for 6 row buffers */
385
386 if (!mid_alloc)
387 return;
388
389 mid_ptr = scale2x_align_ptr(mid_alloc);
390
391 scale4x_buf(void_dst, dst_slice, mid_ptr, mid_slice, void_src, src_slice, pixel, width, height);
392
393 free(mid_alloc);
394 }
395
396 /**
397 * Check if the scale implementation is applicable at the given arguments.
398 * \param scale Scale factor. 2, 203 (fox 2x3), 204 (for 2x4), 3 or 4.
399 * \param pixel Bytes per pixel of the source and destination bitmap.
400 * \param width Horizontal size in pixels of the source bitmap.
401 * \param height Vertical size in pixels of the source bitmap.
402 * \return
403 * - -1 on precondition violated.
404 * - 0 on success.
405 */
406 int scale_precondition(unsigned scale, unsigned pixel, unsigned width, unsigned height)
407 {
408 if (pixel != 1 && pixel != 2 && pixel != 4)
409 return -1;
410
411 switch (scale) {
412 case 202 :
413 case 203 :
414 case 204 :
415 case 2 :
416 case 303 :
417 case 3 :
418 if (height < 2)
419 return -1;
420 break;
421 case 404 :
422 case 4 :
423 if (height < 4)
424 return -1;
425 break;
426 default :
427 return -1;
428 }
429
430 if (width < 2)
431 return -1;
432
433 return 0;
434 }
435
436 /**
437 * Apply the Scale effect on a bitmap.
438 * This function is simply a common interface for ::scale2x(), ::scale3x() and ::scale4x().
439 * \param scale Scale factor. 2, 203 (fox 2x3), 204 (for 2x4), 3 or 4.
440 * \param void_dst Pointer at the first pixel of the destination bitmap.
441 * \param dst_slice Size in bytes of a destination bitmap row.
442 * \param void_src Pointer at the first pixel of the source bitmap.
443 * \param src_slice Size in bytes of a source bitmap row.
444 * \param pixel Bytes per pixel of the source and destination bitmap.
445 * \param width Horizontal size in pixels of the source bitmap.
446 * \param height Vertical size in pixels of the source bitmap.
447 */
448 void scale(unsigned scale, void* void_dst, unsigned dst_slice, const void* void_src, unsigned src_slice, unsigned pixel, unsigned width, unsigned height)
449 {
450 switch (scale) {
451 case 202 :
452 case 2 :
453 scale2x(void_dst, dst_slice, void_src, src_slice, pixel, width, height);
454 break;
455 case 203 :
456 scale2x3(void_dst, dst_slice, void_src, src_slice, pixel, width, height);
457 break;
458 case 204 :
459 scale2x4(void_dst, dst_slice, void_src, src_slice, pixel, width, height);
460 break;
461 case 303 :
462 case 3 :
463 scale3x(void_dst, dst_slice, void_src, src_slice, pixel, width, height);
464 break;
465 case 404 :
466 case 4 :
467 scale4x(void_dst, dst_slice, void_src, src_slice, pixel, width, height);
468 break;
469 }
470 }
471