blob: fefb107ec420cefc96e77ab8b76257fae59d45ec [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 * This file defines some of the standard utility macros and definitions
28 * used throughout the image conversion package header files.
29 */
30
31#include "img_globals.h"
32
33#define ALPHASHIFT 24
34#define REDSHIFT 16
35#define GREENSHIFT 8
36#define BLUESHIFT 0
37
38/*
39 * The following mapping is used between coordinates when scaling an
40 * image:
41 *
42 * srcXY = floor(((dstXY + .5) * srcWH) / dstWH)
43 * = floor((dstXY * srcWH + .5 * srcWH) / dstWH)
44 * = floor((2 * dstXY * srcWH + srcWH) / (2 * dstWH))
45 *
46 * Since the numerator can always be assumed to be non-negative for
47 * all values of dstXY >= 0 and srcWH,dstWH >= 1, then the floor
48 * function can be calculated using the standard C integer division
49 * operator.
50 *
51 * To calculate back from a source range of pixels to the destination
52 * range of pixels that they will affect, we need to find a srcXY
53 * that satisfies the following inequality based upon the above mapping
54 * function:
55 *
56 * srcXY <= (2 * dstXY * srcWH + srcWH) / (2 * dstWH) < (srcXY+1)
57 * 2 * srcXY * dstWH <= 2 * dstXY * srcWH + srcWH < 2 * (srcXY+1) * dstWH
58 *
59 * To calculate the lowest dstXY that satisfies these constraints, we use
60 * the first half of the inequality:
61 *
62 * 2 * dstXY * srcWH + srcWH >= 2 * srcXY * dstWH
63 * 2 * dstXY * srcWH >= 2 * srcXY * dstWH - srcWH
64 * dstXY >= (2 * srcXY * dstWH - srcWH) / (2 * srcWH)
65 * dstXY = ceil((2 * srcXY * dstWH - srcWH) / (2 * srcWH))
66 * dstXY = floor((2 * srcXY * dstWH - srcWH + 2*srcWH - 1) / (2 * srcWH))
67 * dstXY = floor((2 * srcXY * dstWH + srcWH - 1) / (2 * srcWH))
68 *
69 * Since the numerator can be shown to be non-negative, we can calculate
70 * this with the standard C integer division operator.
71 *
72 * To calculate the highest dstXY that satisfies these constraints, we use
73 * the second half of the inequality:
74 *
75 * 2 * dstXY * srcWH + srcWH < 2 * (srcXY+1) * dstWH
76 * 2 * dstXY * srcWH < 2 * (srcXY+1) * dstWH - srcWH
77 * dstXY < (2 * (srcXY+1) * dstWH - srcWH) / (2 * srcWH)
78 * dstXY = ceil((2 * (srcXY+1) * dstWH - srcWH) / (2 * srcWH)) - 1
79 * dstXY = floor((2 * (srcXY+1) * dstWH - srcWH + 2 * srcWH - 1)
80 * / (2 * srcWH)) - 1
81 * dstXY = floor((2 * (srcXY+1) * dstWH + srcWH - 1) / (2 * srcWH)) - 1
82 *
83 * Again, the numerator is always non-negative so we can use integer division.
84 */
85
86#define SRC_XY(dstXY, srcWH, dstWH) \
87 (((2 * (dstXY) * (srcWH)) + (srcWH)) / (2 * (dstWH)))
88
89#define DEST_XY_RANGE_START(srcXY, srcWH, dstWH) \
90 (((2 * (srcXY) * (dstWH)) + (srcWH) - 1) / (2 * (srcWH)))
91
92#define DEST_XY_RANGE_END(srcXY, srcWH, dstWH) \
93 (((2 * ((srcXY) + 1) * (dstWH)) + (srcWH) - 1) / (2 * (srcWH)) - 1)
94
95/*
96 * This union is a utility structure for manipulating pixel pointers
97 * of variable depths.
98 */
99typedef union {
100 void *vp;
101 unsigned char *bp;
102 unsigned short *sp;
103 unsigned int *ip;
104} pixptr;
105
106#define RGBTOGRAY(r, g, b) ((int) (.299 * r + .587 * g + .114 * b))
107
108#define ComponentBound(c) \
109 (((c) < 0) ? 0 : (((c) > 255) ? 255 : (c)))
110
111#define paddedwidth(number, boundary) \
112 (((number) + ((boundary) - 1)) & (~((boundary) - 1)))