1/*
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * This file contains macro definitions for the Decoding category of
28 * the macros used by the generic scaleloop function.
29 *
30 * This implementation can decode the pixel information associated
31 * with any Java DirectColorModel object.  This implementation will
32 * scale the decoded color components to 8-bit quantities if needed.
33 * Another file is provided to optimize DCM parsing when the masks
34 * are guaranteed to be at least 8-bits wide.  This implementation
35 * examines some of the private fields of the DirectColorModel
36 * object and decodes the red, green, blue, and possibly alpha values
37 * directly rather than calling the getRGB method on the Java object.
38 */
39
40/*
41 * These definitions vector the standard macro names to the "DCM"
42 * versions of those macros only if the "DecodeDeclared" keyword has
43 * not yet been defined elsewhere.  The "DecodeDeclared" keyword is
44 * also defined here to claim ownership of the primary implementation
45 * even though this file does not rely on the definitions in any other
46 * files.
47 */
48#ifndef DecodeDeclared
49#define DeclareDecodeVars       DeclareDCMVars
50#define InitPixelDecode(CM)     InitPixelDCM(unhand(CM))
51#define PixelDecode             PixelDCMDecode
52#define DecodeDeclared
53#endif
54
55#define DeclareDCMVars                                          \
56    IfAlpha(int alpha_mask;                                     \
57            int alpha_scale;                                    \
58            unsigned int alpha_off;)                            \
59    int red_mask, green_mask, blue_mask;                        \
60    int red_scale, green_scale, blue_scale;                     \
61    unsigned int red_off, green_off, blue_off;                  \
62    int scale;
63
64#define InitPixelDCM(CM)                                                \
65    do {                                                                \
66        Classjava_awt_image_DirectColorModel *dcm =                     \
67            (Classjava_awt_image_DirectColorModel *) CM;                \
68        red_mask = dcm->red_mask;                                       \
69        red_off = dcm->red_offset;                                      \
70        red_scale = dcm->red_scale;                                     \
71        green_mask = dcm->green_mask;                                   \
72        green_off = dcm->green_offset;                                  \
73        green_scale = dcm->green_scale;                                 \
74        blue_mask = dcm->blue_mask;                                     \
75        blue_off = dcm->blue_offset;                                    \
76        blue_scale = dcm->blue_scale;                                   \
77        IfAlpha(alpha_mask = dcm->alpha_mask;                           \
78                alpha_off = dcm->alpha_offset;                          \
79                alpha_scale = dcm->alpha_scale;)                        \
80        scale = (red_scale | green_scale | blue_scale                   \
81                 IfAlpha(| alpha_scale));                               \
82    } while (0)
83
84#define PixelDCMDecode(CM, pixel, red, green, blue, alpha)              \
85    do {                                                                \
86        IfAlpha(alpha = ((alpha_mask == 0)                              \
87                         ? 255                                          \
88                         : ((pixel & alpha_mask) >> alpha_off));)       \
89        red = ((pixel & red_mask) >> red_off);                          \
90        green = ((pixel & green_mask) >> green_off);                    \
91        blue = ((pixel & blue_mask) >> blue_off);                       \
92        if (scale) {                                                    \
93            if (red_scale) {                                            \
94                red = red * 255 / (red_scale);                          \
95            }                                                           \
96            if (green_scale) {                                          \
97                green = green * 255 / (green_scale);                    \
98            }                                                           \
99            if (blue_scale) {                                           \
100                blue = blue * 255 / (blue_scale);                       \
101            }                                                           \
102            IfAlpha(if (alpha_scale) {                                  \
103                alpha = alpha * 255 / (alpha_scale);                    \
104            })                                                          \
105        }                                                               \
106    } while (0)
107