2 Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 http://www.imagemagick.org/script/license.php
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.
16 MagickCore pixel accessor methods.
18 #ifndef _MAGICKCORE_PIXEL_ACCESSOR_H
19 #define _MAGICKCORE_PIXEL_ACCESSOR_H
21 #if defined(__cplusplus) || defined(c_plusplus)
26 #include <MagickCore/cache-view.h>
27 #include <MagickCore/color.h>
28 #include <MagickCore/image.h>
32 static inline Quantum GetPixelAlpha(const Image *image,const Quantum *pixel)
34 return(pixel[image->channel_map[AlphaPixelChannel].channel]);
37 static inline PixelTrait GetPixelAlphaTraits(const Image *image)
39 return(image->channel_map[AlphaPixelChannel].traits);
42 static inline Quantum GetPixelBlack(const Image *image,const Quantum *pixel)
44 return(pixel[image->channel_map[BlackPixelChannel].channel]);
47 static inline PixelTrait GetPixelBlackTraits(const Image *image)
49 return(image->channel_map[BlackPixelChannel].traits);
52 static inline Quantum GetPixelBlue(const Image *image,const Quantum *pixel)
54 return(pixel[image->channel_map[BluePixelChannel].channel]);
57 static inline PixelTrait GetPixelBlueTraits(const Image *image)
59 return(image->channel_map[BluePixelChannel].traits);
62 static inline Quantum GetPixelCb(const Image *image,const Quantum *pixel)
64 return(pixel[image->channel_map[CbPixelChannel].channel]);
67 static inline PixelTrait GetPixelCbTraits(const Image *image)
69 return(image->channel_map[CbPixelChannel].traits);
72 static inline Quantum GetPixelChannel(const Image *image,
73 const PixelChannel channel,const Quantum *pixel)
75 return(pixel[image->channel_map[channel].channel]);
78 static inline PixelChannel GetPixelChannelMapChannel(const Image *image,
79 const PixelChannel channel)
81 return(image->channel_map[channel].channel);
84 static inline PixelTrait GetPixelChannelMapTraits(const Image *image,
85 const PixelChannel channel)
87 return(image->channel_map[channel].traits);
90 static inline size_t GetPixelChannels(const Image *image)
92 return(image->number_channels);
95 static inline Quantum GetPixelCr(const Image *image,const Quantum *pixel)
97 return(pixel[image->channel_map[CrPixelChannel].channel]);
100 static inline PixelTrait GetPixelCrTraits(const Image *image)
102 return(image->channel_map[CrPixelChannel].traits);
105 static inline Quantum GetPixelCyan(const Image *image,const Quantum *pixel)
107 return(pixel[image->channel_map[CyanPixelChannel].channel]);
110 static inline PixelTrait GetPixelCyanTraits(const Image *image)
112 return(image->channel_map[CyanPixelChannel].traits);
115 static inline Quantum GetPixelGray(const Image *image,const Quantum *pixel)
117 return(pixel[image->channel_map[GrayPixelChannel].channel]);
120 static inline PixelTrait GetPixelGrayTraits(const Image *image)
122 return(image->channel_map[GrayPixelChannel].traits);
125 static inline Quantum GetPixelGreen(const Image *image,const Quantum *pixel)
127 return(pixel[image->channel_map[GreenPixelChannel].channel]);
130 static inline PixelTrait GetPixelGreenTraits(const Image *image)
132 return(image->channel_map[GreenPixelChannel].traits);
135 static inline Quantum GetPixelIndex(const Image *image,const Quantum *pixel)
137 return(pixel[image->channel_map[IndexPixelChannel].channel]);
140 static inline PixelTrait GetPixelIndexTraits(const Image *image)
142 return(image->channel_map[IndexPixelChannel].traits);
145 static inline Quantum GetPixelInfoIntensity(const PixelInfo *pixel_info)
147 #if !defined(MAGICKCORE_HDRI_SUPPORT)
148 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
149 pixel_info->blue+0.5));
151 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
156 static inline Quantum GetPixelInfoLuminance(const PixelInfo *pixel_info)
161 #if !defined(MAGICKCORE_HDRI_SUPPORT)
162 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
163 0.07217*pixel_info->blue+0.5);
165 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
166 0.07217*pixel_info->blue);
168 return((Quantum) luminance);
171 static inline Quantum GetPixelMagenta(const Image *image,const Quantum *pixel)
173 return(pixel[image->channel_map[MagentaPixelChannel].channel]);
176 static inline PixelTrait GetPixelMagentaTraits(const Image *image)
178 return(image->channel_map[MagentaPixelChannel].traits);
181 static inline size_t GetPixelMetaChannels(const Image *image)
183 return(image->number_meta_channels);
186 static inline size_t GetPixelMetacontentExtent(const Image *image)
188 return(image->metacontent_extent);
191 static inline Quantum GetPixelRed(const Image *image,const Quantum *pixel)
193 return(pixel[image->channel_map[RedPixelChannel].channel]);
196 static inline PixelTrait GetPixelRedTraits(const Image *image)
198 return(image->channel_map[RedPixelChannel].traits);
201 static inline void GetPixelInfoPixel(const Image *image,const Quantum *pixel,
204 packet->red=(double) pixel[image->channel_map[RedPixelChannel].channel];
205 packet->green=(double) pixel[image->channel_map[GreenPixelChannel].channel];
206 packet->blue=(double) pixel[image->channel_map[BluePixelChannel].channel];
207 packet->alpha=OpaqueAlpha;
208 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
209 packet->alpha=(double) pixel[image->channel_map[AlphaPixelChannel].channel];
212 static inline PixelTrait GetPixelTraits(const Image *image,
213 const PixelChannel channel)
215 return(image->channel_map[channel].traits);
218 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
220 return(pixel[image->channel_map[YPixelChannel].channel]);
223 static inline PixelTrait GetPixelYTraits(const Image *image)
225 return(image->channel_map[YPixelChannel].traits);
228 static inline Quantum GetPixelYellow(const Image *image,const Quantum *pixel)
230 return(pixel[image->channel_map[YellowPixelChannel].channel]);
233 static inline PixelTrait GetPixelYellowTraits(const Image *image)
235 return(image->channel_map[YellowPixelChannel].traits);
238 static inline MagickBooleanType IsPixelEquivalent(const Image *image,
239 const Quantum *p,const PixelInfo *q)
241 if (((double) p[image->channel_map[RedPixelChannel].channel] == q->red) &&
242 ((double) p[image->channel_map[GreenPixelChannel].channel] == q->green) &&
243 ((double) p[image->channel_map[BluePixelChannel].channel] == q->blue))
248 static inline MagickBooleanType IsPixelGray(const Image *image,
249 const Quantum *pixel)
251 #if !defined(MAGICKCORE_HDRI_SUPPORT)
252 if ((pixel[image->channel_map[RedPixelChannel].channel] ==
253 pixel[image->channel_map[GreenPixelChannel].channel]) &&
254 (pixel[image->channel_map[GreenPixelChannel].channel] ==
255 pixel[image->channel_map[BluePixelChannel].channel]))
263 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
264 pixel[image->channel_map[GreenPixelChannel].channel];
265 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
266 pixel[image->channel_map[BluePixelChannel].channel];
267 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
274 static inline MagickBooleanType IsPixelInfoEquivalent(const PixelInfo *p,
277 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
278 (fabs(p->alpha-OpaqueAlpha) > 0.5))
280 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
281 (fabs(q->alpha-OpaqueAlpha)) > 0.5)
283 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
285 if (fabs(p->alpha-q->alpha) > 0.5)
287 if (fabs(p->alpha-TransparentAlpha) <= 0.5)
290 if (fabs(p->red-q->red) > 0.5)
292 if (fabs(p->green-q->green) > 0.5)
294 if (fabs(p->blue-q->blue) > 0.5)
296 if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
301 static inline MagickBooleanType IsPixelMonochrome(const Image *image,
302 const Quantum *pixel)
304 #if !defined(MAGICKCORE_HDRI_SUPPORT)
305 if (((pixel[image->channel_map[RedPixelChannel].channel] == 0) ||
306 (pixel[image->channel_map[RedPixelChannel].channel] == (Quantum) QuantumRange)) &&
307 (pixel[image->channel_map[RedPixelChannel].channel] ==
308 pixel[image->channel_map[GreenPixelChannel].channel]) &&
309 (pixel[image->channel_map[GreenPixelChannel].channel] ==
310 pixel[image->channel_map[BluePixelChannel].channel]))
318 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
319 pixel[image->channel_map[GreenPixelChannel].channel];
320 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
321 pixel[image->channel_map[BluePixelChannel].channel];
322 if (((fabs(pixel[image->channel_map[RedPixelChannel].channel]) <=
324 (fabs(pixel[image->channel_map[RedPixelChannel].channel]-
325 QuantumRange) <= MagickEpsilon)) &&
326 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
333 static inline MagickBooleanType IsPixelInfoGray(const PixelInfo *pixel)
335 #if !defined(MAGICKCORE_HDRI_SUPPORT)
336 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
344 alpha=pixel->red-(double) pixel->green;
345 beta=pixel->green-(double) pixel->blue;
346 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
353 static inline MagickBooleanType IsPixelInfoMonochrome(
354 const PixelInfo *pixel)
356 #if !defined(MAGICKCORE_HDRI_SUPPORT)
357 if (((pixel->red == 0) || (pixel->red == (Quantum) QuantumRange)) &&
358 (pixel->red == pixel->green) && (pixel->green == pixel->blue))
366 alpha=pixel->red-(double) pixel->green;
367 beta=pixel->green-(double) pixel->blue;
368 if (((fabs(pixel->red) <= MagickEpsilon) ||
369 (fabs(pixel->red-QuantumRange) <= MagickEpsilon)) &&
370 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
377 static inline void SetPacketPixelInfo(const Image *image,
378 const PixelInfo *pixel_info,PixelInfo *packet)
380 packet->red=pixel_info->red;
381 packet->green=pixel_info->green;
382 packet->blue=pixel_info->blue;
384 if (image->channel_map[BlackPixelChannel].traits != UndefinedPixelTrait)
385 packet->black=pixel_info->black;
386 packet->alpha=OpaqueAlpha;
387 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
388 packet->alpha=pixel_info->alpha;
390 if (image->channel_map[IndexPixelChannel].traits != UndefinedPixelTrait)
391 packet->index=pixel_info->index;
394 static inline void SetPixelAlpha(const Image *image,const Quantum alpha,
397 pixel[image->channel_map[AlphaPixelChannel].channel]=alpha;
400 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
402 image->channel_map[AlphaPixelChannel].traits=traits;
405 static inline void SetPixelBlack(const Image *image,const Quantum black,
408 pixel[image->channel_map[BlackPixelChannel].channel]=black;
411 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
413 image->channel_map[BlackPixelChannel].traits=traits;
416 static inline void SetPixelBlue(const Image *image,const Quantum blue,
419 pixel[image->channel_map[BluePixelChannel].channel]=blue;
422 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
424 image->channel_map[BluePixelChannel].traits=traits;
427 static inline void SetPixelCb(const Image *image,const Quantum cb,
430 pixel[image->channel_map[CbPixelChannel].channel]=cb;
433 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
435 image->channel_map[CbPixelChannel].traits=traits;
438 static inline void SetPixelChannel(const Image *image,
439 const PixelChannel channel,const Quantum quantum,Quantum *pixel)
441 pixel[image->channel_map[channel].channel]=quantum;
444 static inline void SetPixelChannelMapChannel(const Image *image,
445 const PixelChannel channel,const PixelChannel channels)
447 image->channel_map[channel].channel=channels;
450 static inline void SetPixelChannels(Image *image,const size_t number_channels)
452 image->number_channels=number_channels;
455 static inline void SetPixelChannelTraits(Image *image,
456 const PixelChannel channel,const PixelTrait traits)
458 image->channel_map[channel].traits=traits;
461 static inline void SetPixelChannelMapTraits(Image *image,
462 const PixelChannel channel,const PixelTrait traits)
464 image->channel_map[channel].traits=traits;
467 static inline void SetPixelCr(const Image *image,const Quantum cr,
470 pixel[image->channel_map[CrPixelChannel].channel]=cr;
473 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
475 image->channel_map[CrPixelChannel].traits=traits;
478 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
481 pixel[image->channel_map[CyanPixelChannel].channel]=cyan;
484 static inline void SetPixelGray(const Image *image,const Quantum gray,
487 pixel[image->channel_map[GrayPixelChannel].channel]=gray;
490 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
492 image->channel_map[GrayPixelChannel].traits=traits;
495 static inline void SetPixelGreen(const Image *image,const Quantum green,
498 pixel[image->channel_map[GreenPixelChannel].channel]=green;
501 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
503 image->channel_map[GreenPixelChannel].traits=traits;
506 static inline void SetPixelIndex(const Image *image,const Quantum index,
509 pixel[image->channel_map[IndexPixelChannel].channel]=index;
512 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
514 image->channel_map[IndexPixelChannel].traits=traits;
517 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
518 PixelInfo *pixel_info)
520 pixel_info->red=(MagickRealType)
521 pixel[image->channel_map[RedPixelChannel].channel];
522 pixel_info->green=(MagickRealType)
523 pixel[image->channel_map[GreenPixelChannel].channel];
524 pixel_info->blue=(MagickRealType)
525 pixel[image->channel_map[BluePixelChannel].channel];
527 if (image->channel_map[BlackPixelChannel].traits != UndefinedPixelTrait)
528 pixel_info->black=(MagickRealType)
529 pixel[image->channel_map[BlackPixelChannel].channel];
530 pixel_info->alpha=OpaqueAlpha;
531 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
532 pixel_info->alpha=(MagickRealType)
533 pixel[image->channel_map[AlphaPixelChannel].channel];
535 if (image->channel_map[IndexPixelChannel].traits != UndefinedPixelTrait)
536 pixel_info->index=(MagickRealType)
537 pixel[image->channel_map[IndexPixelChannel].channel];
540 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
543 pixel[image->channel_map[MagentaPixelChannel].channel]=magenta;
546 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
548 image->channel_map[MagentaPixelChannel].traits=traits;
551 static inline void SetPixelMetaChannels(Image *image,
552 const size_t number_meta_channels)
554 image->number_meta_channels=number_meta_channels;
557 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
559 image->metacontent_extent=extent;
562 static inline void SetPixelRed(const Image *image,const Quantum red,
565 pixel[image->channel_map[RedPixelChannel].channel]=red;
568 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
570 image->channel_map[RedPixelChannel].traits=traits;
573 static inline void SetPixelPixelInfo(const Image *image,
574 const PixelInfo *pixel_info,Quantum *pixel)
576 pixel[image->channel_map[RedPixelChannel].channel]=
577 ClampToQuantum(pixel_info->red);
578 pixel[image->channel_map[GreenPixelChannel].channel]=
579 ClampToQuantum(pixel_info->green);
580 pixel[image->channel_map[BluePixelChannel].channel]=
581 ClampToQuantum(pixel_info->blue);
582 if (image->colorspace == CMYKColorspace)
583 pixel[image->channel_map[BlackPixelChannel].channel]=
584 ClampToQuantum(pixel_info->black);
585 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
586 pixel[image->channel_map[AlphaPixelChannel].channel]=
587 ClampToQuantum(pixel_info->alpha);
590 static inline void SetPixelYellow(const Image *image,const Quantum yellow,
593 pixel[image->channel_map[YellowPixelChannel].channel]=yellow;
596 static inline void SetPixelYellowTraits(Image *image,const PixelTrait traits)
598 image->channel_map[YellowPixelChannel].traits=traits;
601 static inline void SetPixelY(const Image *image,const Quantum y,Quantum *pixel)
603 pixel[image->channel_map[YPixelChannel].channel]=y;
606 static inline void SetPixelYTraits(Image *image,const PixelTrait traits)
608 image->channel_map[YPixelChannel].traits=traits;
611 static inline Quantum GetPixelIntensity(const Image *image,const Quantum *pixel)
613 #if !defined(MAGICKCORE_HDRI_SUPPORT)
614 if ((pixel[image->channel_map[RedPixelChannel].channel] ==
615 pixel[image->channel_map[GreenPixelChannel].channel]) &&
616 (pixel[image->channel_map[GreenPixelChannel].channel] ==
617 pixel[image->channel_map[BluePixelChannel].channel]))
618 return(pixel[image->channel_map[RedPixelChannel].channel]);
619 return((Quantum) (0.299*pixel[image->channel_map[RedPixelChannel].channel]+
620 0.587*pixel[image->channel_map[GreenPixelChannel].channel]+0.114*
621 pixel[image->channel_map[BluePixelChannel].channel]+0.5));
628 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
629 pixel[image->channel_map[GreenPixelChannel].channel];
630 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
631 pixel[image->channel_map[BluePixelChannel].channel];
632 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
633 return(pixel[image->channel_map[RedPixelChannel].channel]);
634 return((Quantum) (0.299*pixel[image->channel_map[RedPixelChannel].channel]+
635 0.587*pixel[image->channel_map[GreenPixelChannel].channel]+0.114*
636 pixel[image->channel_map[BluePixelChannel].channel]));
641 static inline Quantum GetPixelLuminance(const Image *image,const Quantum *pixel)
643 #if !defined(MAGICKCORE_HDRI_SUPPORT)
644 return((Quantum) (0.21267*pixel[image->channel_map[RedPixelChannel].channel]+
645 0.71516*pixel[image->channel_map[GreenPixelChannel].channel]+0.07217*
646 pixel[image->channel_map[BluePixelChannel].channel]+0.5));
648 return((Quantum) (0.21267*pixel[image->channel_map[RedPixelChannel].channel]+
649 0.71516*pixel[image->channel_map[GreenPixelChannel].channel]+0.07217*
650 pixel[image->channel_map[BluePixelChannel].channel]));
654 #if defined(__cplusplus) || defined(c_plusplus)