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=(double) pixel[image->channel_map[AlphaPixelChannel].channel];
210 static inline PixelTrait GetPixelTraits(const Image *image,
211 const PixelChannel channel)
213 return(image->channel_map[channel].traits);
216 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
218 return(pixel[image->channel_map[YPixelChannel].channel]);
221 static inline PixelTrait GetPixelYTraits(const Image *image)
223 return(image->channel_map[YPixelChannel].traits);
226 static inline Quantum GetPixelYellow(const Image *image,const Quantum *pixel)
228 return(pixel[image->channel_map[YellowPixelChannel].channel]);
231 static inline PixelTrait GetPixelYellowTraits(const Image *image)
233 return(image->channel_map[YellowPixelChannel].traits);
236 static inline MagickBooleanType IsPixelEquivalent(const Image *image,
237 const Quantum *p,const PixelInfo *q)
239 if (((double) p[image->channel_map[RedPixelChannel].channel] == q->red) &&
240 ((double) p[image->channel_map[GreenPixelChannel].channel] == q->green) &&
241 ((double) p[image->channel_map[BluePixelChannel].channel] == q->blue))
246 static inline MagickBooleanType IsPixelGray(const Image *image,
247 const Quantum *pixel)
249 #if !defined(MAGICKCORE_HDRI_SUPPORT)
250 if ((pixel[image->channel_map[RedPixelChannel].channel] ==
251 pixel[image->channel_map[GreenPixelChannel].channel]) &&
252 (pixel[image->channel_map[GreenPixelChannel].channel] ==
253 pixel[image->channel_map[BluePixelChannel].channel]))
261 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
262 pixel[image->channel_map[GreenPixelChannel].channel];
263 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
264 pixel[image->channel_map[BluePixelChannel].channel];
265 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
272 static inline MagickBooleanType IsPixelInfoEquivalent(const PixelInfo *p,
275 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
276 (fabs(p->alpha-OpaqueAlpha) > 0.5))
278 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
279 (fabs(q->alpha-OpaqueAlpha)) > 0.5)
281 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
283 if (fabs(p->alpha-q->alpha) > 0.5)
285 if (fabs(p->alpha-TransparentAlpha) <= 0.5)
288 if (fabs(p->red-q->red) > 0.5)
290 if (fabs(p->green-q->green) > 0.5)
292 if (fabs(p->blue-q->blue) > 0.5)
294 if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
299 static inline MagickBooleanType IsPixelMonochrome(const Image *image,
300 const Quantum *pixel)
302 #if !defined(MAGICKCORE_HDRI_SUPPORT)
303 if (((pixel[image->channel_map[RedPixelChannel].channel] == 0) ||
304 (pixel[image->channel_map[RedPixelChannel].channel] == (Quantum) QuantumRange)) &&
305 (pixel[image->channel_map[RedPixelChannel].channel] ==
306 pixel[image->channel_map[GreenPixelChannel].channel]) &&
307 (pixel[image->channel_map[GreenPixelChannel].channel] ==
308 pixel[image->channel_map[BluePixelChannel].channel]))
316 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
317 pixel[image->channel_map[GreenPixelChannel].channel];
318 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
319 pixel[image->channel_map[BluePixelChannel].channel];
320 if (((fabs(pixel[image->channel_map[RedPixelChannel].channel]) <= MagickEpsilon) ||
321 (fabs(pixel[image->channel_map[RedPixelChannel].channel]-QuantumRange) <= MagickEpsilon)) &&
322 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
329 static inline MagickBooleanType IsPixelInfoGray(const PixelInfo *pixel)
331 #if !defined(MAGICKCORE_HDRI_SUPPORT)
332 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
340 alpha=pixel->red-(double) pixel->green;
341 beta=pixel->green-(double) pixel->blue;
342 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
349 static inline MagickBooleanType IsPixelInfoMonochrome(
350 const PixelInfo *pixel)
352 #if !defined(MAGICKCORE_HDRI_SUPPORT)
353 if (((pixel->red == 0) || (pixel->red == (Quantum) QuantumRange)) &&
354 (pixel->red == pixel->green) && (pixel->green == pixel->blue))
362 alpha=pixel->red-(double) pixel->green;
363 beta=pixel->green-(double) pixel->blue;
364 if (((fabs(pixel->red) <= MagickEpsilon) ||
365 (fabs(pixel->red-QuantumRange) <= MagickEpsilon)) &&
366 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
373 static inline void SetPacketPixelInfo(const Image *image,
374 const PixelInfo *pixel_info,PixelInfo *packet)
376 packet->red=pixel_info->red;
377 packet->green=pixel_info->green;
378 packet->blue=pixel_info->blue;
379 packet->alpha=pixel_info->alpha;
380 if (image->colorspace == CMYKColorspace)
381 packet->black=pixel_info->black;
382 if (image->storage_class == PseudoClass)
383 packet->index=pixel_info->index;
386 static inline void SetPixelAlpha(const Image *image,const Quantum alpha,
389 pixel[image->channel_map[AlphaPixelChannel].channel]=alpha;
392 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
394 image->channel_map[AlphaPixelChannel].traits=traits;
397 static inline void SetPixelBlack(const Image *image,const Quantum black,
400 pixel[image->channel_map[BlackPixelChannel].channel]=black;
403 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
405 image->channel_map[BlackPixelChannel].traits=traits;
408 static inline void SetPixelBlue(const Image *image,const Quantum blue,
411 pixel[image->channel_map[BluePixelChannel].channel]=blue;
414 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
416 image->channel_map[BluePixelChannel].traits=traits;
419 static inline void SetPixelCb(const Image *image,const Quantum cb,
422 pixel[image->channel_map[CbPixelChannel].channel]=cb;
425 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
427 image->channel_map[CbPixelChannel].traits=traits;
430 static inline void SetPixelChannel(const Image *image,
431 const PixelChannel channel,const Quantum quantum,Quantum *pixel)
433 pixel[image->channel_map[channel].channel]=quantum;
436 static inline void SetPixelChannelMapChannel(const Image *image,
437 const PixelChannel channel,const PixelChannel channels)
439 image->channel_map[channel].channel=channels;
442 static inline void SetPixelChannels(Image *image,const size_t number_channels)
444 image->number_channels=number_channels;
447 static inline void SetPixelChannelTraits(Image *image,
448 const PixelChannel channel,const PixelTrait traits)
450 image->channel_map[channel].traits=traits;
453 static inline void SetPixelChannelMapTraits(Image *image,
454 const PixelChannel channel,const PixelTrait traits)
456 image->channel_map[channel].traits=traits;
459 static inline void SetPixelCr(const Image *image,const Quantum cr,
462 pixel[image->channel_map[CrPixelChannel].channel]=cr;
465 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
467 image->channel_map[CrPixelChannel].traits=traits;
470 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
473 pixel[image->channel_map[CyanPixelChannel].channel]=cyan;
476 static inline void SetPixelGray(const Image *image,const Quantum gray,
479 pixel[image->channel_map[GrayPixelChannel].channel]=gray;
482 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
484 image->channel_map[GrayPixelChannel].traits=traits;
487 static inline void SetPixelGreen(const Image *image,const Quantum green,
490 pixel[image->channel_map[GreenPixelChannel].channel]=green;
493 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
495 image->channel_map[GreenPixelChannel].traits=traits;
498 static inline void SetPixelIndex(const Image *image,const Quantum index,
501 pixel[image->channel_map[IndexPixelChannel].channel]=index;
504 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
506 image->channel_map[IndexPixelChannel].traits=traits;
509 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
510 PixelInfo *pixel_info)
512 pixel_info->red=(MagickRealType)
513 pixel[image->channel_map[RedPixelChannel].channel];
514 pixel_info->green=(MagickRealType)
515 pixel[image->channel_map[GreenPixelChannel].channel];
516 pixel_info->blue=(MagickRealType)
517 pixel[image->channel_map[BluePixelChannel].channel];
518 if (image->colorspace == CMYKColorspace)
519 pixel_info->black=(MagickRealType)
520 pixel[image->channel_map[BlackPixelChannel].channel];
521 pixel_info->alpha=(MagickRealType)
522 pixel[image->channel_map[AlphaPixelChannel].channel];
523 if (image->storage_class == PseudoClass)
524 pixel_info->index=(MagickRealType)
525 pixel[image->channel_map[IndexPixelChannel].channel];
528 static inline void SetPixelInfoPacket(const Image *image,
529 const PixelInfo *pixel,PixelInfo *pixel_info)
531 pixel_info->red=(MagickRealType) pixel->red;
532 pixel_info->green=(MagickRealType) pixel->green;
533 pixel_info->blue=(MagickRealType) pixel->blue;
534 pixel_info->alpha=(MagickRealType) pixel->alpha;
535 if (image->colorspace == CMYKColorspace)
536 pixel_info->black=(MagickRealType) pixel->black;
537 if (image->storage_class == PseudoClass)
538 pixel_info->index=(MagickRealType) pixel->index;
541 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
544 pixel[image->channel_map[MagentaPixelChannel].channel]=magenta;
547 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
549 image->channel_map[MagentaPixelChannel].traits=traits;
552 static inline void SetPixelMetaChannels(Image *image,
553 const size_t number_meta_channels)
555 image->number_meta_channels=number_meta_channels;
558 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
560 image->metacontent_extent=extent;
563 static inline void SetPixelRed(const Image *image,const Quantum red,
566 pixel[image->channel_map[RedPixelChannel].channel]=red;
569 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
571 image->channel_map[RedPixelChannel].traits=traits;
574 static inline void SetPixelPixelInfo(const Image *image,
575 const PixelInfo *pixel_info,Quantum *pixel)
577 pixel[image->channel_map[RedPixelChannel].channel]=
578 ClampToQuantum(pixel_info->red);
579 pixel[image->channel_map[GreenPixelChannel].channel]=
580 ClampToQuantum(pixel_info->green);
581 pixel[image->channel_map[BluePixelChannel].channel]=
582 ClampToQuantum(pixel_info->blue);
583 pixel[image->channel_map[AlphaPixelChannel].channel]=
584 ClampToQuantum(pixel_info->alpha);
585 if (image->colorspace == CMYKColorspace)
586 pixel[image->channel_map[BlackPixelChannel].channel]=
587 ClampToQuantum(pixel_info->black);
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)