]> 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 IndexPacket ConstrainColormapIndex(Image *image,
38   const unsigned long index)
39 {
40   if (index < image->colors)
41     return((IndexPacket) index);
42   (void) ThrowMagickException(&image->exception,GetMagickModule(),
43     CorruptImageError,"InvalidColormapIndex","`%s'",image->filename);
44   return((IndexPacket) 0);
45 }
46
47 static inline MagickBooleanType IsGray(const PixelPacket *pixel)
48 {
49   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
50     return(MagickTrue);
51   return(MagickFalse);
52 }
53
54 static inline MagickBooleanType IsMagickColorEqual(const MagickPixelPacket *p,
55   const MagickPixelPacket *q)
56 {
57   if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
58       (p->opacity != OpaqueOpacity))
59     return(MagickFalse);
60   if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
61       (q->opacity != OpaqueOpacity))
62     return(MagickFalse);
63   if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
64     {
65       if (p->opacity != q->opacity)
66         return(MagickFalse);
67       if (p->opacity == TransparentOpacity)
68         return(MagickTrue);
69     }
70   if (p->red != q->red)
71     return(MagickFalse);
72   if (p->green != q->green)
73     return(MagickFalse);
74   if (p->blue != q->blue)
75     return(MagickFalse);
76   if ((p->colorspace == CMYKColorspace) && (p->index != q->index))
77     return(MagickFalse);
78   return(MagickTrue);
79 }
80
81 static inline MagickBooleanType IsMagickGray(const MagickPixelPacket *pixel)
82 {
83   if (pixel->colorspace != RGBColorspace)
84     return(MagickFalse);
85   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
86     return(MagickTrue);
87   return(MagickFalse);
88 }
89
90 static inline MagickRealType MagickPixelIntensity(
91   const MagickPixelPacket *pixel)
92 {
93   MagickRealType
94     intensity;
95
96   intensity=0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue;
97   return(intensity);
98 }
99
100 static inline Quantum MagickPixelIntensityToQuantum(
101   const MagickPixelPacket *pixel)
102 {
103 #if !defined(MAGICKCORE_HDRI_SUPPORT)
104   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
105 #else
106   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
107 #endif
108 }
109
110 static inline MagickRealType MagickPixelLuminance(
111   const MagickPixelPacket *pixel)
112 {
113   MagickRealType
114     luminance;
115
116   luminance=0.21267*pixel->red+0.71516*pixel->green+0.07217*pixel->blue;
117   return(luminance);
118 }
119
120 static inline MagickRealType PixelIntensity(const PixelPacket *pixel)
121 {
122   MagickRealType
123     intensity;
124
125   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
126     return((MagickRealType) pixel->red);
127   intensity=(MagickRealType) (0.299*pixel->red+0.587*pixel->green+0.114*
128     pixel->blue);
129   return(intensity);
130 }
131
132 static inline Quantum PixelIntensityToQuantum(const PixelPacket *pixel)
133 {
134 #if !defined(MAGICKCORE_HDRI_SUPPORT)
135   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
136     return(pixel->red);
137 #if (MAGICKCORE_QUANTUM_DEPTH <= 16)
138   return((Quantum) ((306U*(unsigned int) pixel->red+
139     601U*(unsigned int) pixel->green+117U*(unsigned int) pixel->blue) >> 10U));
140 #else
141   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
142 #endif
143 #else
144   if ((fabs(pixel->red-pixel->green) <= MagickEpsilon) &&
145       (fabs(pixel->green-pixel->blue) <= MagickEpsilon))
146     return((Quantum) pixel->red);
147   return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
148 #endif
149 }
150
151 #if defined(__cplusplus) || defined(c_plusplus)
152 }
153 #endif
154
155 #endif