]> granicus.if.org Git - imagemagick/blob - magick/color-private.h
(no commit message)
[imagemagick] / magick / color-private.h
1 /*
2   Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization
3   dedicated to making software imaging solutions freely available.
4
5   You may not use this file except in compliance with the License.
6   obtain a copy of the License at
7
8     http://www.imagemagick.org/script/license.php
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15
16   MagickCore image color methods.
17 */
18 #ifndef _MAGICKCORE_COLOR_PRIVATE_H
19 #define _MAGICKCORE_COLOR_PRIVATE_H
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 #include <magick/image.h>
26 #include <magick/color.h>
27 #include <magick/exception-private.h>
28
29 static inline MagickBooleanType IsColorEqual(const PixelPacket *p,
30   const PixelPacket *q)
31 {
32   if ((p->red == q->red) && (p->green == q->green) && (p->blue == q->blue))
33     return(MagickTrue);
34   return(MagickFalse);
35 }
36
37 static inline MagickBooleanType IsGray(const PixelPacket *pixel)
38 {
39   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
40     return(MagickTrue);
41   return(MagickFalse);
42 }
43
44 static inline MagickBooleanType IsMagickColorEqual(const MagickPixelPacket *p,
45   const MagickPixelPacket *q)
46 {
47 #if !defined(MAGICKCORE_HDRI_SUPPORT)
48   if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
49       (p->opacity != OpaqueOpacity))
50     return(MagickFalse);
51   if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
52       (q->opacity != OpaqueOpacity))
53     return(MagickFalse);
54   if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
55     {
56       if (p->opacity != q->opacity)
57         return(MagickFalse);
58       if (p->opacity == TransparentOpacity)
59         return(MagickTrue);
60     }
61   if (p->red != q->red)
62     return(MagickFalse);
63   if (p->green != q->green)
64     return(MagickFalse);
65   if (p->blue != q->blue)
66     return(MagickFalse);
67   if ((p->colorspace == CMYKColorspace) && (p->index != q->index))
68     return(MagickFalse);
69 #else
70   if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
71       (fabs(p->opacity-OpaqueOpacity) > 0.5))
72     return(MagickFalse);
73   if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
74       (fabs(q->opacity-OpaqueOpacity)) > 0.5)
75     return(MagickFalse);
76   if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
77     {
78       if (fabs(p->opacity-q->opacity) > 0.5)
79         return(MagickFalse);
80       if (fabs(p->opacity-TransparentOpacity) <= 0.5)
81         return(MagickTrue);
82     }
83   if (fabs(p->red-q->red) > 0.5)
84     return(MagickFalse);
85   if (fabs(p->green-q->green) > 0.5)
86     return(MagickFalse);
87   if (fabs(p->blue-q->blue) > 0.5)
88     return(MagickFalse);
89   if ((p->colorspace == CMYKColorspace) && (fabs(p->index-q->index) > 0.5))
90     return(MagickFalse);
91 #endif
92   return(MagickTrue);
93 }
94
95 static inline MagickBooleanType IsMagickGray(const MagickPixelPacket *pixel)
96 {
97   if (pixel->colorspace != RGBColorspace)
98     return(MagickFalse);
99   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
100     return(MagickTrue);
101   return(MagickFalse);
102 }
103
104 static inline MagickRealType MagickPixelIntensity(
105   const MagickPixelPacket *pixel)
106 {
107   MagickRealType
108     intensity;
109
110   intensity=0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue;
111   return(intensity);
112 }
113
114 static inline Quantum MagickPixelIntensityToQuantum(
115   const MagickPixelPacket *pixel)
116 {
117 #if !defined(MAGICKCORE_HDRI_SUPPORT)
118   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
119 #else
120   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
121 #endif
122 }
123
124 static inline MagickRealType MagickPixelLuminance(
125   const MagickPixelPacket *pixel)
126 {
127   MagickRealType
128     luminance;
129
130   luminance=0.21267*pixel->red+0.71516*pixel->green+0.07217*pixel->blue;
131   return(luminance);
132 }
133
134 static inline MagickRealType PixelIntensity(const PixelPacket *pixel)
135 {
136   MagickRealType
137     intensity;
138
139   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
140     return((MagickRealType) pixel->red);
141   intensity=(MagickRealType) (0.299*pixel->red+0.587*pixel->green+0.114*
142     pixel->blue);
143   return(intensity);
144 }
145
146 static inline Quantum PixelIntensityToQuantum(const PixelPacket *pixel)
147 {
148 #if !defined(MAGICKCORE_HDRI_SUPPORT)
149   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
150     return(pixel->red);
151 #if (MAGICKCORE_QUANTUM_DEPTH <= 16)
152   return((Quantum) ((306U*(unsigned int) pixel->red+
153     601U*(unsigned int) pixel->green+117U*(unsigned int) pixel->blue) >> 10U));
154 #else
155   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
156 #endif
157 #else
158   if ((fabs(pixel->red-pixel->green) <= MagickEpsilon) &&
159       (fabs(pixel->green-pixel->blue) <= MagickEpsilon))
160     return((Quantum) pixel->red);
161   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
162 #endif
163 }
164
165 #if defined(__cplusplus) || defined(c_plusplus)
166 }
167 #endif
168
169 #endif