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>
30 static inline Quantum GetPixelAlpha(const Image *image,const Quantum *pixel)
32 return(pixel[image->channel_map[AlphaPixelChannel].channel]);
35 static inline PixelTrait GetPixelAlphaTraits(const Image *image)
37 return(image->channel_map[AlphaPixelChannel].traits);
40 static inline Quantum GetPixelBlack(const Image *image,const Quantum *pixel)
42 return(pixel[image->channel_map[BlackPixelChannel].channel]);
45 static inline PixelTrait GetPixelBlackTraits(const Image *image)
47 return(image->channel_map[BlackPixelChannel].traits);
50 static inline Quantum GetPixelBlue(const Image *image,const Quantum *pixel)
52 return(pixel[image->channel_map[BluePixelChannel].channel]);
55 static inline PixelTrait GetPixelBlueTraits(const Image *image)
57 return(image->channel_map[BluePixelChannel].traits);
60 static inline Quantum GetPixelCb(const Image *image,const Quantum *pixel)
62 return(pixel[image->channel_map[CbPixelChannel].channel]);
65 static inline PixelTrait GetPixelCbTraits(const Image *image)
67 return(image->channel_map[CbPixelChannel].traits);
70 static inline Quantum GetPixelChannel(const Image *image,
71 const PixelChannel channel,const Quantum *pixel)
73 return(pixel[image->channel_map[channel].channel]);
76 static inline PixelChannel GetPixelChannelMapChannel(const Image *image,
77 const PixelChannel channel)
79 return(image->channel_map[channel].channel);
82 static inline PixelTrait GetPixelChannelMapTraits(const Image *image,
83 const PixelChannel channel)
85 return(image->channel_map[channel].traits);
88 static inline size_t GetPixelChannels(const Image *image)
90 return(image->number_channels);
93 static inline Quantum GetPixelCr(const Image *image,const Quantum *pixel)
95 return(pixel[image->channel_map[CrPixelChannel].channel]);
98 static inline PixelTrait GetPixelCrTraits(const Image *image)
100 return(image->channel_map[CrPixelChannel].traits);
103 static inline Quantum GetPixelCyan(const Image *image,const Quantum *pixel)
105 return(pixel[image->channel_map[CyanPixelChannel].channel]);
108 static inline PixelTrait GetPixelCyanTraits(const Image *image)
110 return(image->channel_map[CyanPixelChannel].traits);
113 static inline Quantum GetPixelGray(const Image *image,const Quantum *pixel)
115 return(pixel[image->channel_map[GrayPixelChannel].channel]);
118 static inline PixelTrait GetPixelGrayTraits(const Image *image)
120 return(image->channel_map[GrayPixelChannel].traits);
123 static inline Quantum GetPixelGreen(const Image *image,const Quantum *pixel)
125 return(pixel[image->channel_map[GreenPixelChannel].channel]);
128 static inline PixelTrait GetPixelGreenTraits(const Image *image)
130 return(image->channel_map[GreenPixelChannel].traits);
133 static inline Quantum GetPixelIndex(const Image *image,const Quantum *pixel)
135 return(pixel[image->channel_map[IndexPixelChannel].channel]);
138 static inline PixelTrait GetPixelIndexTraits(const Image *image)
140 return(image->channel_map[IndexPixelChannel].traits);
143 static inline Quantum GetPixelInfoIntensity(const PixelInfo *pixel_info)
145 #if !defined(MAGICKCORE_HDRI_SUPPORT)
146 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
147 pixel_info->blue+0.5));
149 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
154 static inline Quantum GetPixelInfoLuminance(const PixelInfo *pixel_info)
159 #if !defined(MAGICKCORE_HDRI_SUPPORT)
160 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
161 0.07217*pixel_info->blue+0.5);
163 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
164 0.07217*pixel_info->blue);
166 return((Quantum) luminance);
169 static inline Quantum GetPixelMagenta(const Image *image,const Quantum *pixel)
171 return(pixel[image->channel_map[MagentaPixelChannel].channel]);
174 static inline PixelTrait GetPixelMagentaTraits(const Image *image)
176 return(image->channel_map[MagentaPixelChannel].traits);
179 static inline size_t GetPixelMetaChannels(const Image *image)
181 return(image->number_meta_channels);
184 static inline size_t GetPixelMetacontentExtent(const Image *image)
186 return(image->metacontent_extent);
189 static inline Quantum GetPixelRed(const Image *image,const Quantum *pixel)
191 return(pixel[image->channel_map[RedPixelChannel].channel]);
194 static inline PixelTrait GetPixelRedTraits(const Image *image)
196 return(image->channel_map[RedPixelChannel].traits);
199 static inline void GetPixelPacket(const Image *image,const Quantum *pixel,
202 packet->red=GetPixelRed(image,pixel);
203 packet->green=GetPixelGreen(image,pixel);
204 packet->blue=GetPixelBlue(image,pixel);
205 packet->alpha=GetPixelAlpha(image,pixel);
208 static inline Quantum GetPixelPacketIntensity(const PixelPacket *pixel)
210 #if !defined(MAGICKCORE_HDRI_SUPPORT)
211 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
213 return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
220 alpha=pixel->red-pixel->green;
221 beta=pixel->green-pixel->blue;
222 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
224 return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
229 static inline PixelTrait GetPixelTraits(const Image *image,
230 const PixelChannel channel)
232 return(image->channel_map[channel].traits);
235 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
237 return(pixel[image->channel_map[YPixelChannel].channel]);
240 static inline PixelTrait GetPixelYTraits(const Image *image)
242 return(image->channel_map[YPixelChannel].traits);
245 static inline Quantum GetPixelYellow(const Image *image,const Quantum *pixel)
247 return(pixel[image->channel_map[YellowPixelChannel].channel]);
250 static inline PixelTrait GetPixelYellowTraits(const Image *image)
252 return(image->channel_map[YellowPixelChannel].traits);
255 static inline MagickBooleanType IsPixelEquivalent(const Image *image,
256 const Quantum *p,const PixelPacket *q)
258 if ((GetPixelRed(image,p) == q->red) &&
259 (GetPixelGreen(image,p) == q->green) &&
260 (GetPixelBlue(image,p) == q->blue))
265 static inline MagickBooleanType IsPixelGray(const Image *image,
266 const Quantum *pixel)
268 #if !defined(MAGICKCORE_HDRI_SUPPORT)
269 if ((GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
270 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
278 alpha=GetPixelRed(image,pixel)-(double) GetPixelGreen(image,pixel);
279 beta=GetPixelGreen(image,pixel)-(double) GetPixelBlue(image,pixel);
280 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
287 static inline MagickBooleanType IsPixelInfoEquivalent(const PixelInfo *p,
290 #if !defined(MAGICKCORE_HDRI_SUPPORT)
291 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
292 (p->alpha != OpaqueAlpha))
294 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
295 (q->alpha != OpaqueAlpha))
297 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
299 if (p->alpha != q->alpha)
301 if (p->alpha == TransparentAlpha)
304 if (p->red != q->red)
306 if (p->green != q->green)
308 if (p->blue != q->blue)
310 if ((p->colorspace == CMYKColorspace) && (p->black != q->black))
313 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
314 (fabs(p->alpha-OpaqueAlpha) > 0.5))
316 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
317 (fabs(q->alpha-OpaqueAlpha)) > 0.5)
319 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
321 if (fabs(p->alpha-q->alpha) > 0.5)
323 if (fabs(p->alpha-TransparentAlpha) <= 0.5)
326 if (fabs(p->red-q->red) > 0.5)
328 if (fabs(p->green-q->green) > 0.5)
330 if (fabs(p->blue-q->blue) > 0.5)
332 if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
338 static inline MagickBooleanType IsPixelMonochrome(const Image *image,
339 const Quantum *pixel)
341 #if !defined(MAGICKCORE_HDRI_SUPPORT)
342 if (((GetPixelRed(image,pixel) == 0) ||
343 (GetPixelRed(image,pixel) == (Quantum) QuantumRange)) &&
344 (GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
345 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
353 alpha=GetPixelRed(image,pixel)-(double) GetPixelGreen(image,pixel);
354 beta=GetPixelGreen(image,pixel)-(double) GetPixelBlue(image,pixel);
355 if (((fabs(GetPixelRed(image,pixel)) <= MagickEpsilon) ||
356 (fabs(GetPixelRed(image,pixel)-QuantumRange) <= MagickEpsilon)) &&
357 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
364 static inline MagickBooleanType IsPixelPacketEquivalent(const PixelPacket *p,
365 const PixelPacket *q)
367 if ((p->red == q->red) && (p->green == q->green) && (p->blue == q->blue))
372 static inline MagickBooleanType IsPixelPacketGray(const PixelPacket *pixel)
374 #if !defined(MAGICKCORE_HDRI_SUPPORT)
375 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
383 alpha=pixel->red-(double) pixel->green;
384 beta=pixel->green-(double) pixel->blue;
385 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
392 static inline MagickBooleanType IsPixelPacketMonochrome(
393 const PixelPacket *pixel)
395 #if !defined(MAGICKCORE_HDRI_SUPPORT)
396 if (((pixel->red == 0) || (pixel->red == (Quantum) QuantumRange)) &&
397 (pixel->red == pixel->green) && (pixel->green == pixel->blue))
405 alpha=pixel->red-(double) pixel->green;
406 beta=pixel->green-(double) pixel->blue;
407 if (((fabs(pixel->red) <= MagickEpsilon) ||
408 (fabs(pixel->red-QuantumRange) <= MagickEpsilon)) &&
409 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
416 static inline void SetPacketPixelInfo(const Image *image,
417 const PixelInfo *pixel_info,PixelPacket *packet)
419 packet->red=ClampToQuantum(pixel_info->red);
420 packet->green=ClampToQuantum(pixel_info->green);
421 packet->blue=ClampToQuantum(pixel_info->blue);
422 packet->alpha=ClampToQuantum(pixel_info->alpha);
423 if (image->colorspace == CMYKColorspace)
424 packet->black=ClampToQuantum(pixel_info->black);
425 if (image->storage_class == PseudoClass)
426 packet->index=ClampToQuantum(pixel_info->index);
429 static inline void SetPixelAlpha(const Image *image,const Quantum alpha,
432 pixel[image->channel_map[AlphaPixelChannel].channel]=alpha;
435 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
437 image->channel_map[AlphaPixelChannel].traits=traits;
440 static inline void SetPixelBlack(const Image *image,const Quantum black,
443 pixel[image->channel_map[BlackPixelChannel].channel]=black;
446 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
448 image->channel_map[BlackPixelChannel].traits=traits;
451 static inline void SetPixelBlue(const Image *image,const Quantum blue,
454 pixel[image->channel_map[BluePixelChannel].channel]=blue;
457 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
459 image->channel_map[BluePixelChannel].traits=traits;
462 static inline void SetPixelCb(const Image *image,const Quantum cb,
465 pixel[image->channel_map[CbPixelChannel].channel]=cb;
468 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
470 image->channel_map[CbPixelChannel].traits=traits;
473 static inline void SetPixelChannel(const Image *image,
474 const PixelChannel channel,const Quantum quantum,Quantum *pixel)
476 pixel[image->channel_map[channel].channel]=quantum;
479 static inline void SetPixelChannelMapChannel(const Image *image,
480 const PixelChannel channel,const PixelChannel channels)
482 image->channel_map[channel].channel=channels;
485 static inline void SetPixelChannels(Image *image,const size_t number_channels)
487 image->number_channels=number_channels;
490 static inline void SetPixelChannelTraits(Image *image,
491 const PixelChannel channel,const PixelTrait traits)
493 image->channel_map[channel].traits=traits;
496 static inline void SetPixelChannelMapTraits(Image *image,
497 const PixelChannel channel,const PixelTrait traits)
499 image->channel_map[channel].traits=traits;
502 static inline void SetPixelCr(const Image *image,const Quantum cr,
505 pixel[image->channel_map[CrPixelChannel].channel]=cr;
508 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
510 image->channel_map[CrPixelChannel].traits=traits;
513 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
516 pixel[image->channel_map[CyanPixelChannel].channel]=cyan;
519 static inline void SetPixelGray(const Image *image,const Quantum gray,
522 pixel[image->channel_map[GrayPixelChannel].channel]=gray;
525 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
527 image->channel_map[GrayPixelChannel].traits=traits;
530 static inline void SetPixelGreen(const Image *image,const Quantum green,
533 pixel[image->channel_map[GreenPixelChannel].channel]=green;
536 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
538 image->channel_map[GreenPixelChannel].traits=traits;
541 static inline void SetPixelIndex(const Image *image,const Quantum index,
544 pixel[image->channel_map[IndexPixelChannel].channel]=index;
547 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
549 image->channel_map[IndexPixelChannel].traits=traits;
552 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
553 PixelInfo *pixel_info)
555 pixel_info->red=(MagickRealType) GetPixelRed(image,pixel);
556 pixel_info->green=(MagickRealType) GetPixelGreen(image,pixel);
557 pixel_info->blue=(MagickRealType) GetPixelBlue(image,pixel);
558 pixel_info->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
559 if (image->colorspace == CMYKColorspace)
560 pixel_info->black=(MagickRealType) GetPixelBlack(image,pixel);
561 if (image->storage_class == PseudoClass)
562 pixel_info->index=(MagickRealType) GetPixelIndex(image,pixel);
565 static inline void SetPixelInfoBias(const Image *image,PixelInfo *pixel_info)
568 Obsoleted by MorphologyApply().
570 pixel_info->red=image->bias;
571 pixel_info->green=image->bias;
572 pixel_info->blue=image->bias;
573 pixel_info->alpha=image->bias;
574 pixel_info->black=image->bias;
577 static inline void SetPixelInfoPacket(const Image *image,
578 const PixelPacket *pixel,PixelInfo *pixel_info)
580 pixel_info->red=(MagickRealType) pixel->red;
581 pixel_info->green=(MagickRealType) pixel->green;
582 pixel_info->blue=(MagickRealType) pixel->blue;
583 pixel_info->alpha=(MagickRealType) pixel->alpha;
584 if (image->colorspace == CMYKColorspace)
585 pixel_info->black=(MagickRealType) pixel->black;
586 if (image->storage_class == PseudoClass)
587 pixel_info->index=(MagickRealType) pixel->index;
590 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
593 pixel[image->channel_map[MagentaPixelChannel].channel]=
597 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
599 image->channel_map[MagentaPixelChannel].traits=traits;
602 static inline void SetPixelMetaChannels(Image *image,
603 const size_t number_meta_channels)
605 image->number_meta_channels=number_meta_channels;
608 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
610 image->metacontent_extent=extent;
613 static inline void SetPixelRed(const Image *image,const Quantum red,
616 pixel[image->channel_map[RedPixelChannel].channel]=red;
619 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
621 image->channel_map[RedPixelChannel].traits=traits;
624 static inline void SetPixelPacket(const Image *image,const PixelPacket *packet,
627 SetPixelRed(image,packet->red,pixel);
628 SetPixelGreen(image,packet->green,pixel);
629 SetPixelBlue(image,packet->blue,pixel);
630 SetPixelAlpha(image,packet->alpha,pixel);
633 static inline void SetPixelPixelInfo(const Image *image,
634 const PixelInfo *pixel_info,Quantum *packet)
636 SetPixelRed(image,ClampToQuantum(pixel_info->red),packet);
637 SetPixelGreen(image,ClampToQuantum(pixel_info->green),packet);
638 SetPixelBlue(image,ClampToQuantum(pixel_info->blue),packet);
639 SetPixelAlpha(image,ClampToQuantum(pixel_info->alpha),packet);
640 if (image->colorspace == CMYKColorspace)
641 SetPixelBlack(image,ClampToQuantum(pixel_info->black),packet);
644 static inline void SetPixelYellow(const Image *image,const Quantum yellow,
647 pixel[image->channel_map[YellowPixelChannel].channel]=
651 static inline void SetPixelYellowTraits(Image *image,const PixelTrait traits)
653 image->channel_map[YellowPixelChannel].traits=traits;
656 static inline void SetPixelY(const Image *image,const Quantum y,Quantum *pixel)
658 pixel[image->channel_map[YPixelChannel].channel]=y;
661 static inline void SetPixelYTraits(Image *image,const PixelTrait traits)
663 image->channel_map[YPixelChannel].traits=traits;
666 static inline Quantum GetPixelIntensity(const Image *image,const Quantum *pixel)
668 #if !defined(MAGICKCORE_HDRI_SUPPORT)
669 if ((GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
670 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
671 return(GetPixelRed(image,pixel));
672 return((Quantum) (0.299*GetPixelRed(image,pixel)+0.587*
673 GetPixelGreen(image,pixel)+0.114*GetPixelBlue(image,pixel)+0.5));
680 alpha=GetPixelRed(image,pixel)-(double) GetPixelGreen(image,pixel);
681 beta=GetPixelGreen(image,pixel)-(double) GetPixelBlue(image,pixel);
682 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
683 return(GetPixelRed(image,pixel));
684 return((Quantum) (0.299*GetPixelRed(image,pixel)+0.587*
685 GetPixelGreen(image,pixel)+0.114*GetPixelBlue(image,pixel)));
690 static inline Quantum GetPixelLuminance(const Image *image,const Quantum *pixel)
692 #if !defined(MAGICKCORE_HDRI_SUPPORT)
693 return((Quantum) (0.21267*GetPixelRed(image,pixel)+0.71516*
694 GetPixelGreen(image,pixel)+0.07217*GetPixelBlue(image,pixel)+0.5));
696 return((Quantum) (0.21267*GetPixelRed(image,pixel)+0.71516*
697 GetPixelGreen(image,pixel)+0.07217*GetPixelBlue(image,pixel)));
701 #if defined(__cplusplus) || defined(c_plusplus)